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 "common/ocv.h"
16 #include "rsn_supp/wpa.h"
17 #include "config.h"
18 #include "wpa_supplicant_i.h"
19 #include "driver_i.h"
20 #include "scan.h"
21 #include "ctrl_iface.h"
22 #include "bss.h"
23 #include "wnm_sta.h"
24 #include "notify.h"
25 #include "hs20_supplicant.h"
26
27 #define MAX_TFS_IE_LEN 1024
28 #define WNM_MAX_NEIGHBOR_REPORT 10
29
30
31 /* get the TFS IE from driver */
ieee80211_11_get_tfs_ie(struct wpa_supplicant * wpa_s,u8 * buf,u16 * buf_len,enum wnm_oper oper)32 static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
33 u16 *buf_len, enum wnm_oper oper)
34 {
35 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
36
37 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
38 }
39
40
41 /* set the TFS IE to driver */
ieee80211_11_set_tfs_ie(struct wpa_supplicant * wpa_s,const u8 * addr,const u8 * buf,u16 buf_len,enum wnm_oper oper)42 static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
43 const u8 *addr, const u8 *buf, u16 buf_len,
44 enum wnm_oper oper)
45 {
46 u16 len = buf_len;
47
48 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
49
50 return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
51 }
52
53
54 /* MLME-SLEEPMODE.request */
ieee802_11_send_wnmsleep_req(struct wpa_supplicant * wpa_s,u8 action,u16 intval,struct wpabuf * tfs_req)55 int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
56 u8 action, u16 intval, struct wpabuf *tfs_req)
57 {
58 struct ieee80211_mgmt *mgmt;
59 int res;
60 size_t len;
61 struct wnm_sleep_element *wnmsleep_ie;
62 u8 *wnmtfs_ie, *oci_ie;
63 u8 wnmsleep_ie_len, oci_ie_len;
64 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
65 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
66 WNM_SLEEP_TFS_REQ_IE_NONE;
67
68 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
69 "action=%s to " MACSTR,
70 action == 0 ? "enter" : "exit",
71 MAC2STR(wpa_s->bssid));
72
73 /* WNM-Sleep Mode IE */
74 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
75 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
76 if (wnmsleep_ie == NULL)
77 return -1;
78 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
79 wnmsleep_ie->len = wnmsleep_ie_len - 2;
80 wnmsleep_ie->action_type = action;
81 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
82 wnmsleep_ie->intval = host_to_le16(intval);
83 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
84 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
85
86 /* TFS IE(s) */
87 if (tfs_req) {
88 wnmtfs_ie_len = wpabuf_len(tfs_req);
89 wnmtfs_ie = os_memdup(wpabuf_head(tfs_req), wnmtfs_ie_len);
90 if (wnmtfs_ie == NULL) {
91 os_free(wnmsleep_ie);
92 return -1;
93 }
94 } else {
95 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
96 if (wnmtfs_ie == NULL) {
97 os_free(wnmsleep_ie);
98 return -1;
99 }
100 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
101 tfs_oper)) {
102 wnmtfs_ie_len = 0;
103 os_free(wnmtfs_ie);
104 wnmtfs_ie = NULL;
105 }
106 }
107 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
108 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
109
110 oci_ie = NULL;
111 oci_ie_len = 0;
112 #ifdef CONFIG_OCV
113 if (action == WNM_SLEEP_MODE_EXIT && wpa_sm_ocv_enabled(wpa_s->wpa)) {
114 struct wpa_channel_info ci;
115
116 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
117 wpa_printf(MSG_WARNING,
118 "Failed to get channel info for OCI element in WNM-Sleep Mode frame");
119 os_free(wnmsleep_ie);
120 os_free(wnmtfs_ie);
121 return -1;
122 }
123 #ifdef CONFIG_TESTING_OPTIONS
124 if (wpa_s->oci_freq_override_wnm_sleep) {
125 wpa_printf(MSG_INFO,
126 "TEST: Override OCI KDE frequency %d -> %d MHz",
127 ci.frequency,
128 wpa_s->oci_freq_override_wnm_sleep);
129 ci.frequency = wpa_s->oci_freq_override_wnm_sleep;
130 }
131 #endif /* CONFIG_TESTING_OPTIONS */
132
133 oci_ie_len = OCV_OCI_EXTENDED_LEN;
134 oci_ie = os_zalloc(oci_ie_len);
135 if (!oci_ie) {
136 wpa_printf(MSG_WARNING,
137 "Failed to allocate buffer for for OCI element in WNM-Sleep Mode frame");
138 os_free(wnmsleep_ie);
139 os_free(wnmtfs_ie);
140 return -1;
141 }
142
143 if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
144 os_free(wnmsleep_ie);
145 os_free(wnmtfs_ie);
146 os_free(oci_ie);
147 return -1;
148 }
149 }
150 #endif /* CONFIG_OCV */
151
152 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len +
153 oci_ie_len);
154 if (mgmt == NULL) {
155 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
156 "WNM-Sleep Request action frame");
157 os_free(wnmsleep_ie);
158 os_free(wnmtfs_ie);
159 return -1;
160 }
161
162 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
163 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
164 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
165 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
166 WLAN_FC_STYPE_ACTION);
167 mgmt->u.action.category = WLAN_ACTION_WNM;
168 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
169 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
170 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
171 wnmsleep_ie_len);
172 /* copy TFS IE here */
173 if (wnmtfs_ie_len > 0) {
174 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
175 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
176 }
177
178 #ifdef CONFIG_OCV
179 /* copy OCV OCI here */
180 if (oci_ie_len > 0) {
181 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
182 wnmsleep_ie_len + wnmtfs_ie_len, oci_ie, oci_ie_len);
183 }
184 #endif /* CONFIG_OCV */
185
186 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
187 wnmtfs_ie_len + oci_ie_len;
188
189 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
190 wpa_s->own_addr, wpa_s->bssid,
191 &mgmt->u.action.category, len, 0);
192 if (res < 0)
193 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
194 "(action=%d, intval=%d)", action, intval);
195 else
196 wpa_s->wnmsleep_used = 1;
197
198 os_free(wnmsleep_ie);
199 os_free(wnmtfs_ie);
200 os_free(oci_ie);
201 os_free(mgmt);
202
203 return res;
204 }
205
206
wnm_sleep_mode_enter_success(struct wpa_supplicant * wpa_s,const u8 * tfsresp_ie_start,const u8 * tfsresp_ie_end)207 static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
208 const u8 *tfsresp_ie_start,
209 const u8 *tfsresp_ie_end)
210 {
211 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
212 wpa_s->bssid, NULL, NULL);
213 /* remove GTK/IGTK ?? */
214
215 /* set the TFS Resp IE(s) */
216 if (tfsresp_ie_start && tfsresp_ie_end &&
217 tfsresp_ie_end - tfsresp_ie_start >= 0) {
218 u16 tfsresp_ie_len;
219 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
220 tfsresp_ie_start;
221 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
222 /* pass the TFS Resp IE(s) to driver for processing */
223 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
224 tfsresp_ie_start,
225 tfsresp_ie_len,
226 WNM_SLEEP_TFS_RESP_IE_SET))
227 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
228 }
229 }
230
231
wnm_sleep_mode_exit_success(struct wpa_supplicant * wpa_s,const u8 * frm,u16 key_len_total)232 static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
233 const u8 *frm, u16 key_len_total)
234 {
235 u8 *ptr, *end;
236 u8 gtk_len;
237
238 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
239 NULL, NULL);
240
241 /* Install GTK/IGTK */
242
243 /* point to key data field */
244 ptr = (u8 *) frm + 1 + 2;
245 end = ptr + key_len_total;
246 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
247
248 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
249 wpa_msg(wpa_s, MSG_INFO,
250 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
251 return;
252 }
253
254 while (end - ptr > 1) {
255 if (2 + ptr[1] > end - ptr) {
256 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
257 "length");
258 if (end > ptr) {
259 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
260 ptr, end - ptr);
261 }
262 break;
263 }
264 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
265 if (ptr[1] < 11 + 5) {
266 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
267 "subelem");
268 break;
269 }
270 gtk_len = *(ptr + 4);
271 if (ptr[1] < 11 + gtk_len ||
272 gtk_len < 5 || gtk_len > 32) {
273 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
274 "subelem");
275 break;
276 }
277 wpa_wnmsleep_install_key(
278 wpa_s->wpa,
279 WNM_SLEEP_SUBELEM_GTK,
280 ptr);
281 ptr += 13 + gtk_len;
282 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
283 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
284 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
285 "subelem");
286 break;
287 }
288 wpa_wnmsleep_install_key(wpa_s->wpa,
289 WNM_SLEEP_SUBELEM_IGTK, ptr);
290 ptr += 10 + WPA_IGTK_LEN;
291 } else if (*ptr == WNM_SLEEP_SUBELEM_BIGTK) {
292 if (ptr[1] < 2 + 6 + WPA_BIGTK_LEN) {
293 wpa_printf(MSG_DEBUG,
294 "WNM: Too short BIGTK subelem");
295 break;
296 }
297 wpa_wnmsleep_install_key(wpa_s->wpa,
298 WNM_SLEEP_SUBELEM_BIGTK, ptr);
299 ptr += 10 + WPA_BIGTK_LEN;
300 } else
301 break; /* skip the loop */
302 }
303 }
304
305
ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant * wpa_s,const u8 * da,const u8 * sa,const u8 * frm,int len)306 static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
307 const u8 *da, const u8 *sa,
308 const u8 *frm, int len)
309 {
310 /*
311 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
312 * WNM-Sleep Mode IE | TFS Response IE
313 */
314 const u8 *pos = frm; /* point to payload after the action field */
315 u16 key_len_total;
316 struct wnm_sleep_element *wnmsleep_ie = NULL;
317 /* multiple TFS Resp IE (assuming consecutive) */
318 const u8 *tfsresp_ie_start = NULL;
319 const u8 *tfsresp_ie_end = NULL;
320 #ifdef CONFIG_OCV
321 const u8 *oci_ie = NULL;
322 u8 oci_ie_len = 0;
323 #endif /* CONFIG_OCV */
324 size_t left;
325
326 if (!wpa_s->wnmsleep_used) {
327 wpa_printf(MSG_DEBUG,
328 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
329 return;
330 }
331
332 if (is_multicast_ether_addr(da)) {
333 wpa_printf(MSG_DEBUG,
334 "WNM: Ignore group-addressed WNM-Sleep Mode Response frame (A1="
335 MACSTR " A2=" MACSTR ")",
336 MAC2STR(da), MAC2STR(sa));
337 return;
338 }
339
340 if (len < 3)
341 return;
342 key_len_total = WPA_GET_LE16(frm + 1);
343
344 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
345 frm[0], key_len_total);
346 left = len - 3;
347 if (key_len_total > left) {
348 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
349 return;
350 }
351 pos += 3 + key_len_total;
352 while (pos - frm + 1 < len) {
353 u8 ie_len = *(pos + 1);
354 if (2 + ie_len > frm + len - pos) {
355 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
356 break;
357 }
358 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
359 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
360 wnmsleep_ie = (struct wnm_sleep_element *) pos;
361 else if (*pos == WLAN_EID_TFS_RESP) {
362 if (!tfsresp_ie_start)
363 tfsresp_ie_start = pos;
364 tfsresp_ie_end = pos;
365 #ifdef CONFIG_OCV
366 } else if (*pos == WLAN_EID_EXTENSION && ie_len >= 1 &&
367 pos[2] == WLAN_EID_EXT_OCV_OCI) {
368 oci_ie = pos + 3;
369 oci_ie_len = ie_len - 1;
370 #endif /* CONFIG_OCV */
371 } else
372 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
373 pos += ie_len + 2;
374 }
375
376 if (!wnmsleep_ie) {
377 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
378 return;
379 }
380
381 #ifdef CONFIG_OCV
382 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT &&
383 wpa_sm_ocv_enabled(wpa_s->wpa)) {
384 struct wpa_channel_info ci;
385
386 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
387 wpa_msg(wpa_s, MSG_WARNING,
388 "Failed to get channel info to validate received OCI in WNM-Sleep Mode frame");
389 return;
390 }
391
392 if (ocv_verify_tx_params(oci_ie, oci_ie_len, &ci,
393 channel_width_to_int(ci.chanwidth),
394 ci.seg1_idx) != OCI_SUCCESS) {
395 wpa_msg(wpa_s, MSG_WARNING, "WNM: OCV failed: %s",
396 ocv_errorstr);
397 return;
398 }
399 }
400 #endif /* CONFIG_OCV */
401
402 wpa_s->wnmsleep_used = 0;
403
404 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
405 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
406 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
407 "frame (action=%d, intval=%d)",
408 wnmsleep_ie->action_type, wnmsleep_ie->intval);
409 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
410 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
411 tfsresp_ie_end);
412 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
413 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
414 }
415 } else {
416 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
417 "(action=%d, intval=%d)",
418 wnmsleep_ie->action_type, wnmsleep_ie->intval);
419 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
420 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
421 wpa_s->bssid, NULL, NULL);
422 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
423 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
424 wpa_s->bssid, NULL, NULL);
425 }
426 }
427
428
wnm_btm_reset(struct wpa_supplicant * wpa_s)429 void wnm_btm_reset(struct wpa_supplicant *wpa_s)
430 {
431 int i;
432
433 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
434 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
435 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
436 }
437
438 wpa_s->wnm_num_neighbor_report = 0;
439 os_free(wpa_s->wnm_neighbor_report_elements);
440 wpa_s->wnm_neighbor_report_elements = NULL;
441
442 wpa_s->wnm_target_bss = NULL;
443
444 wpa_s->wnm_cand_valid_until.sec = 0;
445 wpa_s->wnm_cand_valid_until.usec = 0;
446
447 wpa_s->wnm_mode = 0;
448 wpa_s->wnm_dialog_token = 0;
449 wpa_s->wnm_reply = 0;
450
451 #ifdef CONFIG_MBO
452 wpa_s->wnm_mbo_trans_reason_present = 0;
453 wpa_s->wnm_mbo_transition_reason = 0;
454 #endif /* CONFIG_MBO */
455 }
456
457
wnm_parse_neighbor_report_multi_link(struct neighbor_report * rep,u8 id,u8 elen,const u8 * pos)458 static void wnm_parse_neighbor_report_multi_link(struct neighbor_report *rep,
459 u8 id, u8 elen, const u8 *pos)
460 {
461 const struct ieee80211_eht_ml *ml =
462 (const struct ieee80211_eht_ml *) pos;
463 bool has_link_id;
464 u8 common_info_len;
465
466 /* The Basic Multi-Link subelement has the same body as the Basic MLE.
467 * It includes at least the 2 octet Multi-Link Control field, 1 octet
468 * Common Info Length, and the 6 oxtet MLD MAC Address fields. */
469 if (elen < sizeof(*ml) + 1 + ETH_ALEN) {
470 wpa_printf(MSG_DEBUG, "WNM: Too short ML element");
471 return;
472 }
473
474 /* The ML control should be all zeroes except for the Link ID Info
475 * Present field. */
476 if ((le_to_host16(ml->ml_control) &
477 ~BASIC_MULTI_LINK_CTRL_PRES_LINK_ID))
478 wpa_printf(MSG_DEBUG,
479 "WNM: Ignore unsupported ML Control field bits: 0x%04x",
480 le_to_host16(ml->ml_control) &
481 ~BASIC_MULTI_LINK_CTRL_PRES_LINK_ID);
482
483 has_link_id = !!(le_to_host16(ml->ml_control) &
484 BASIC_MULTI_LINK_CTRL_PRES_LINK_ID);
485
486 /* Followed by the Common Info Length and the MLD MAC Address fields */
487 common_info_len = pos[2];
488 if (common_info_len < 1 + ETH_ALEN) {
489 wpa_printf(MSG_DEBUG, "WNM: Too short ML Common Info: %u < 7",
490 common_info_len);
491 return;
492 }
493
494 /* MLD MAC Address */
495 os_memcpy(rep->mld_addr, &pos[3], ETH_ALEN);
496
497 if (!has_link_id)
498 return;
499
500 if (common_info_len < 1 + ETH_ALEN + 1 || common_info_len + 2 > elen) {
501 wpa_printf(MSG_DEBUG,
502 "WNM: ML Common Info too short or does not fit: %u (elen: %u)",
503 common_info_len, elen);
504 return;
505 }
506
507 /* Link ID Info */
508 if ((pos[9] & EHT_ML_LINK_ID_MSK) >= MAX_NUM_MLD_LINKS) {
509 wpa_printf(MSG_DEBUG,
510 "WNM: ML common info contains invalid link ID");
511 return;
512 }
513
514 rep->mld_links = BIT(pos[9] & EHT_ML_LINK_ID_MSK);
515
516 elen -= common_info_len + 2;
517 pos += common_info_len + 2;
518
519 /* Parse out per-STA information */
520 while (elen >= 2) {
521 u8 sub_elem_len = pos[1];
522
523 if (2 + sub_elem_len > elen) {
524 wpa_printf(MSG_DEBUG,
525 "WNM: Invalid sub-element length: %u %u",
526 2 + sub_elem_len, elen);
527 rep->mld_links = 0;
528 break;
529 }
530
531 if (*pos == MULTI_LINK_SUB_ELEM_ID_PER_STA_PROFILE) {
532 const struct ieee80211_eht_per_sta_profile *sta_prof =
533 (const struct ieee80211_eht_per_sta_profile *)
534 (pos + 2);
535 u16 control;
536 u8 link_id;
537
538 if (sub_elem_len < sizeof(*sta_prof)) {
539 wpa_printf(MSG_DEBUG,
540 "WNM: Invalid STA-profile length: %u",
541 sub_elem_len);
542 rep->mld_links = 0;
543 break;
544 }
545
546 control = le_to_host16(sta_prof->sta_control);
547
548 link_id = control & EHT_PER_STA_RECONF_CTRL_LINK_ID_MSK;
549 if (link_id < MAX_NUM_MLD_LINKS)
550 rep->mld_links |= BIT(link_id);
551 }
552
553 pos += 2 + sub_elem_len;
554 elen -= 2 + sub_elem_len;
555 }
556
557 if (elen != 0) {
558 wpa_printf(MSG_DEBUG,
559 "WNM: Data left at end of multi-link element: %u",
560 elen);
561 rep->mld_links = 0;
562 }
563 }
564
565
wnm_parse_neighbor_report_elem(struct neighbor_report * rep,u8 id,u8 elen,const u8 * pos)566 static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
567 u8 id, u8 elen, const u8 *pos)
568 {
569 switch (id) {
570 case WNM_NEIGHBOR_TSF:
571 if (elen < 2 + 2) {
572 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
573 break;
574 }
575 rep->tsf_offset = WPA_GET_LE16(pos);
576 rep->beacon_int = WPA_GET_LE16(pos + 2);
577 rep->tsf_present = 1;
578 break;
579 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
580 if (elen < 2) {
581 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
582 "country string");
583 break;
584 }
585 os_memcpy(rep->country, pos, 2);
586 rep->country_present = 1;
587 break;
588 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
589 if (elen < 1) {
590 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
591 "candidate");
592 break;
593 }
594 rep->preference = pos[0];
595 rep->preference_present = 1;
596 break;
597 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
598 if (elen < 10) {
599 wpa_printf(MSG_DEBUG,
600 "WNM: Too short BSS termination duration");
601 break;
602 }
603 rep->bss_term_tsf = WPA_GET_LE64(pos);
604 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
605 rep->bss_term_present = 1;
606 break;
607 case WNM_NEIGHBOR_BEARING:
608 if (elen < 8) {
609 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
610 "bearing");
611 break;
612 }
613 rep->bearing = WPA_GET_LE16(pos);
614 rep->distance = WPA_GET_LE32(pos + 2);
615 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
616 rep->bearing_present = 1;
617 break;
618 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
619 if (elen < 1) {
620 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
621 "pilot");
622 break;
623 }
624 os_free(rep->meas_pilot);
625 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
626 if (rep->meas_pilot == NULL)
627 break;
628 rep->meas_pilot->measurement_pilot = pos[0];
629 rep->meas_pilot->subelem_len = elen - 1;
630 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
631 break;
632 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
633 if (elen < 5) {
634 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
635 "capabilities");
636 break;
637 }
638 os_memcpy(rep->rm_capab, pos, 5);
639 rep->rm_capab_present = 1;
640 break;
641 case WNM_NEIGHBOR_MULTIPLE_BSSID:
642 if (elen < 1) {
643 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
644 break;
645 }
646 os_free(rep->mul_bssid);
647 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
648 if (rep->mul_bssid == NULL)
649 break;
650 rep->mul_bssid->max_bssid_indicator = pos[0];
651 rep->mul_bssid->subelem_len = elen - 1;
652 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
653 break;
654 case WNM_NEIGHBOR_MULTI_LINK:
655 wnm_parse_neighbor_report_multi_link(rep, id, elen, pos);
656 break;
657 default:
658 wpa_printf(MSG_DEBUG,
659 "WNM: Unsupported neighbor report subelement id %u",
660 id);
661 break;
662 }
663 }
664
665
wnm_nei_get_chan(struct wpa_supplicant * wpa_s,u8 op_class,u8 chan)666 static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
667 {
668 struct wpa_bss *bss = wpa_s->current_bss;
669 const char *country = NULL;
670 int freq;
671
672 if (bss) {
673 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
674
675 if (elem && elem[1] >= 2)
676 country = (const char *) (elem + 2);
677 }
678
679 freq = ieee80211_chan_to_freq(country, op_class, chan);
680 if (freq <= 0 && (op_class == 0 || op_class == 255)) {
681 /*
682 * Some APs do not advertise correct operating class
683 * information. Try to determine the most likely operating
684 * frequency based on the channel number.
685 */
686 if (chan >= 1 && chan <= 13)
687 freq = 2407 + chan * 5;
688 else if (chan == 14)
689 freq = 2484;
690 else if (chan >= 36 && chan <= 177)
691 freq = 5000 + chan * 5;
692 }
693 return freq;
694 }
695
696
wnm_parse_neighbor_report(struct wpa_supplicant * wpa_s,const u8 * pos,u8 len,struct neighbor_report * rep)697 static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
698 const u8 *pos, u8 len,
699 struct neighbor_report *rep)
700 {
701 u8 left = len;
702
703 if (left < 13) {
704 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
705 return;
706 }
707
708 os_memcpy(rep->bssid, pos, ETH_ALEN);
709 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
710 rep->regulatory_class = *(pos + 10);
711 rep->channel_number = *(pos + 11);
712 rep->phy_type = *(pos + 12);
713
714 pos += 13;
715 left -= 13;
716
717 while (left >= 2) {
718 u8 id, elen;
719
720 id = *pos++;
721 elen = *pos++;
722 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
723 left -= 2;
724 if (elen > left) {
725 wpa_printf(MSG_DEBUG,
726 "WNM: Truncated neighbor report subelement");
727 break;
728 }
729 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
730 left -= elen;
731 pos += elen;
732 }
733
734 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
735 rep->channel_number);
736 }
737
738
739 static void
fetch_drv_mbo_candidate_info(struct wpa_supplicant * wpa_s,enum mbo_transition_reject_reason * reason)740 fetch_drv_mbo_candidate_info(struct wpa_supplicant *wpa_s,
741 enum mbo_transition_reject_reason *reason)
742 {
743 #ifdef CONFIG_MBO
744 struct wpa_bss_trans_info params;
745 struct wpa_bss_candidate_info *info = NULL;
746 struct neighbor_report *nei;
747 u8 *pos;
748 unsigned int i;
749
750 if (!wpa_s->wnm_mbo_trans_reason_present)
751 return;
752
753 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
754 params.n_candidates = 0;
755 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
756 if (!params.bssid)
757 return;
758
759 pos = params.bssid;
760 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
761 nei = &wpa_s->wnm_neighbor_report_elements[i];
762
763 nei->drv_mbo_reject = 0;
764
765 if (nei->preference_present && nei->preference == 0)
766 continue;
767
768 /* Should we query BSSIDs that we reject for other reasons? */
769
770 os_memcpy(pos, nei->bssid, ETH_ALEN);
771 pos += ETH_ALEN;
772 params.n_candidates++;
773 }
774
775 if (!params.n_candidates)
776 goto end;
777
778 info = wpa_drv_get_bss_trans_status(wpa_s, ¶ms);
779 if (!info)
780 goto end;
781
782 for (i = 0; i < info->num; i++) {
783 int j;
784
785 for (j = 0; j < wpa_s->wnm_num_neighbor_report; j++) {
786 nei = &wpa_s->wnm_neighbor_report_elements[j];
787
788 if (!ether_addr_equal(info->candidates[i].bssid,
789 nei->bssid))
790 continue;
791
792 nei->drv_mbo_reject = !info->candidates[i].is_accept;
793
794 /* Use the reject reason from the first candidate */
795 if (i == 0 && nei->drv_mbo_reject)
796 *reason = info->candidates[i].reject_reason;
797
798 break;
799 }
800 }
801
802 end:
803 os_free(params.bssid);
804 if (info) {
805 os_free(info->candidates);
806 os_free(info);
807 }
808 #endif /* CONFIG_MBO */
809 }
810
811
wpa_bss_ies_eq(struct wpa_bss * a,struct wpa_bss * b,u8 eid)812 static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
813 {
814 const u8 *ie_a, *ie_b;
815
816 if (!a || !b)
817 return 0;
818
819 ie_a = wpa_bss_get_ie(a, eid);
820 ie_b = wpa_bss_get_ie(b, eid);
821
822 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
823 return 0;
824
825 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
826 }
827
828
wnm_get_bss_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)829 static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
830 {
831 u32 info = 0;
832
833 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
834
835 /*
836 * Leave the security and key scope bits unset to indicate that the
837 * security information is not available.
838 */
839
840 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
841 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
842 if (bss->caps & WLAN_CAPABILITY_QOS)
843 info |= NEI_REP_BSSID_INFO_QOS;
844 if (bss->caps & WLAN_CAPABILITY_APSD)
845 info |= NEI_REP_BSSID_INFO_APSD;
846 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
847 info |= NEI_REP_BSSID_INFO_RM;
848 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
849 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
850 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
851 info |= NEI_REP_BSSID_INFO_HT;
852
853 return info;
854 }
855
856
wnm_add_nei_rep(struct wpabuf ** buf,const u8 * bssid,u32 bss_info,u8 op_class,u8 chan,u8 phy_type,u8 pref)857 static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
858 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
859 u8 pref)
860 {
861 if (wpabuf_len(*buf) + 18 >
862 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
863 wpa_printf(MSG_DEBUG,
864 "WNM: No room in frame for Neighbor Report element");
865 return -1;
866 }
867
868 if (wpabuf_resize(buf, 18) < 0) {
869 wpa_printf(MSG_DEBUG,
870 "WNM: Failed to allocate memory for Neighbor Report element");
871 return -1;
872 }
873
874 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
875 /* length: 13 for basic neighbor report + 3 for preference subelement */
876 wpabuf_put_u8(*buf, 16);
877 wpabuf_put_data(*buf, bssid, ETH_ALEN);
878 wpabuf_put_le32(*buf, bss_info);
879 wpabuf_put_u8(*buf, op_class);
880 wpabuf_put_u8(*buf, chan);
881 wpabuf_put_u8(*buf, phy_type);
882 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
883 wpabuf_put_u8(*buf, 1);
884 wpabuf_put_u8(*buf, pref);
885 return 0;
886 }
887
888
wnm_nei_rep_add_bss(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpabuf ** buf,u8 pref)889 static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
890 struct wpa_bss *bss, struct wpabuf **buf,
891 u8 pref)
892 {
893 u8 op_class, chan;
894 int sec_chan = 0, chanwidth = 0;
895 struct ieee802_11_elems elems;
896 struct ieee80211_ht_operation *ht_oper;
897 enum phy_type phy_type;
898 u32 info;
899
900 if (ieee802_11_parse_elems(wpa_bss_ie_ptr(bss), bss->ie_len, &elems,
901 1) == ParseFailed)
902 return -2;
903
904 chanwidth = get_operation_channel_width(&elems);
905 if (chanwidth == CHAN_WIDTH_UNKNOWN) {
906 wpa_printf(MSG_DEBUG, "Cannot determine channel width");
907 return -2;
908 }
909 ht_oper = (struct ieee80211_ht_operation *) elems.ht_operation;
910 if (ht_oper) {
911 u8 sec_chan_offset = ht_oper->ht_param &
912 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
913
914 if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
915 sec_chan = 1;
916 else if (sec_chan_offset ==
917 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
918 sec_chan = -1;
919 }
920
921 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, chanwidth,
922 &op_class, &chan) ==
923 NUM_HOSTAPD_MODES) {
924 wpa_printf(MSG_DEBUG,
925 "WNM: Cannot determine operating class and channel");
926 return -2;
927 }
928
929 phy_type = ieee80211_get_phy_type(bss->freq, elems.ht_operation != NULL,
930 elems.vht_operation != NULL,
931 elems.he_operation != NULL);
932 if (phy_type == PHY_TYPE_UNSPECIFIED) {
933 wpa_printf(MSG_DEBUG,
934 "WNM: Cannot determine BSS phy type for Neighbor Report");
935 return -2;
936 }
937
938 info = wnm_get_bss_info(wpa_s, bss);
939
940 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
941 pref);
942 }
943
944
wnm_add_cand_list(struct wpa_supplicant * wpa_s,struct wpabuf ** buf)945 static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
946 {
947 unsigned int i, pref = 255;
948 struct os_reltime now;
949 struct wpa_ssid *ssid = wpa_s->current_ssid;
950
951 if (!ssid)
952 return;
953
954 /*
955 * TODO: Define when scan results are no longer valid for the candidate
956 * list.
957 */
958 os_get_reltime(&now);
959 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
960 return;
961
962 wpa_printf(MSG_DEBUG,
963 "WNM: Add candidate list to BSS Transition Management Response frame");
964 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
965 struct wpa_bss *bss = wpa_s->last_scan_res[i];
966 int res;
967
968 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0, false)) {
969 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
970 if (res == -2)
971 continue; /* could not build entry for BSS */
972 if (res < 0)
973 break; /* no more room for candidates */
974 if (pref == 1)
975 break;
976 }
977 }
978
979 wpa_hexdump_buf(MSG_DEBUG,
980 "WNM: BSS Transition Management Response candidate list",
981 *buf);
982 }
983
984
985 #define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
986
wnm_send_bss_transition_mgmt_resp(struct wpa_supplicant * wpa_s,enum bss_trans_mgmt_status_code status,enum mbo_transition_reject_reason reason,u8 delay,const u8 * target_bssid)987 static int wnm_send_bss_transition_mgmt_resp(
988 struct wpa_supplicant *wpa_s,
989 enum bss_trans_mgmt_status_code status,
990 enum mbo_transition_reject_reason reason,
991 u8 delay, const u8 *target_bssid)
992 {
993 struct wpabuf *buf;
994 int res;
995
996 wpa_s->wnm_reply = 0;
997
998 wpa_printf(MSG_DEBUG,
999 "WNM: Send BSS Transition Management Response to " MACSTR
1000 " dialog_token=%u status=%u reason=%u delay=%d",
1001 MAC2STR(wpa_s->bssid), wpa_s->wnm_dialog_token, status,
1002 reason, delay);
1003 if (!wpa_s->current_bss) {
1004 wpa_printf(MSG_DEBUG,
1005 "WNM: Current BSS not known - drop response");
1006 return -1;
1007 }
1008
1009 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
1010 if (!buf) {
1011 wpa_printf(MSG_DEBUG,
1012 "WNM: Failed to allocate memory for BTM response");
1013 return -1;
1014 }
1015
1016 wpa_s->bss_tm_status = status;
1017 wpas_notify_bss_tm_status(wpa_s);
1018
1019 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1020 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
1021 wpabuf_put_u8(buf, wpa_s->wnm_dialog_token);
1022 wpabuf_put_u8(buf, status);
1023 wpabuf_put_u8(buf, delay);
1024 if (target_bssid) {
1025 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
1026 } else if (status == WNM_BSS_TM_ACCEPT) {
1027 /*
1028 * IEEE Std 802.11-2024, 9.6.13.10 (BSS Transition Management
1029 * Response frame format) clarifies that the Target BSSID field
1030 * is always present when status code is zero, so use a fake
1031 * value here if no BSSID is yet known.
1032 */
1033 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
1034 }
1035
1036 if (status == WNM_BSS_TM_ACCEPT && target_bssid)
1037 wnm_add_cand_list(wpa_s, &buf);
1038
1039 #ifdef CONFIG_MBO
1040 if (status != WNM_BSS_TM_ACCEPT &&
1041 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
1042 u8 mbo[10];
1043 size_t ret;
1044
1045 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
1046 reason);
1047 if (ret) {
1048 if (wpabuf_resize(&buf, ret) < 0) {
1049 wpabuf_free(buf);
1050 wpa_printf(MSG_DEBUG,
1051 "WNM: Failed to allocate memory for MBO IE");
1052 return -1;
1053 }
1054
1055 wpabuf_put_data(buf, mbo, ret);
1056 }
1057 }
1058 #endif /* CONFIG_MBO */
1059
1060 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1061 wpa_s->own_addr, wpa_s->bssid,
1062 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1063 if (res < 0) {
1064 wpa_printf(MSG_DEBUG,
1065 "WNM: Failed to send BSS Transition Management Response");
1066 }
1067
1068 wpabuf_free(buf);
1069
1070 return res;
1071 }
1072
1073
wnm_bss_tm_connect(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,int after_new_scan)1074 static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
1075 struct wpa_bss *bss, struct wpa_ssid *ssid,
1076 int after_new_scan)
1077 {
1078 struct wpa_radio_work *already_connecting;
1079
1080 wpa_dbg(wpa_s, MSG_DEBUG,
1081 "WNM: Transition to BSS " MACSTR
1082 " based on BSS Transition Management Request (old BSSID "
1083 MACSTR " after_new_scan=%d)",
1084 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
1085
1086 /* Send the BSS Management Response - Accept */
1087 if (wpa_s->wnm_reply) {
1088 wpa_s->wnm_target_bss = bss;
1089 wpa_printf(MSG_DEBUG,
1090 "WNM: Sending successful BSS Transition Management Response");
1091
1092 /* This function will be called again from the TX handler to
1093 * start the actual reassociation after this response has been
1094 * delivered to the current AP. */
1095 if (wnm_send_bss_transition_mgmt_resp(
1096 wpa_s, WNM_BSS_TM_ACCEPT,
1097 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1098 bss->bssid) >= 0)
1099 return;
1100 }
1101
1102 if (bss == wpa_s->current_bss) {
1103 wpa_printf(MSG_DEBUG,
1104 "WNM: Already associated with the preferred candidate");
1105 wnm_btm_reset(wpa_s);
1106 return;
1107 }
1108
1109 already_connecting = radio_work_pending(wpa_s, "sme-connect");
1110 wpa_s->reassociate = 1;
1111 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
1112 wpa_supplicant_connect(wpa_s, bss, ssid);
1113
1114 /*
1115 * Indicate that a BSS transition is in progress so scan results that
1116 * come in before the 'sme-connect' radio work gets executed do not
1117 * override the original connection attempt.
1118 */
1119 if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
1120 wpa_s->bss_trans_mgmt_in_progress = true;
1121 }
1122
1123
wnm_scan_process(struct wpa_supplicant * wpa_s,bool pre_scan_check)1124 int wnm_scan_process(struct wpa_supplicant *wpa_s, bool pre_scan_check)
1125 {
1126 struct wpa_bss *bss, *current_bss = wpa_s->current_bss;
1127 struct wpa_ssid *ssid = wpa_s->current_ssid;
1128 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1129 enum mbo_transition_reject_reason reason =
1130 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
1131 struct wpa_ssid *selected_ssid = NULL;
1132
1133 if (!ssid || !wpa_s->wnm_dialog_token)
1134 return 0;
1135
1136 wpa_dbg(wpa_s, MSG_DEBUG,
1137 "WNM: Process scan results for BSS Transition Management");
1138 if (!pre_scan_check &&
1139 os_reltime_initialized(&wpa_s->wnm_cand_valid_until) &&
1140 os_reltime_before(&wpa_s->wnm_cand_valid_until,
1141 &wpa_s->scan_trigger_time)) {
1142 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
1143 goto send_bss_resp_fail;
1144 }
1145
1146 if (!pre_scan_check && !wpa_s->wnm_transition_scan)
1147 return 0;
1148
1149 wpa_s->wnm_transition_scan = false;
1150
1151 /* Fetch MBO transition candidate rejection information from driver */
1152 fetch_drv_mbo_candidate_info(wpa_s, &reason);
1153
1154 /* Compare the Neighbor Report and scan results */
1155 bss = wpa_supplicant_select_bss(wpa_s, ssid, &selected_ssid, 1);
1156 #ifdef CONFIG_MBO
1157 if (!bss && wpa_s->wnm_mbo_trans_reason_present &&
1158 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT)) {
1159 int i;
1160 bool changed = false;
1161
1162 /*
1163 * We didn't find any candidate, the driver had a say about
1164 * which targets to reject and disassociation is immiment.
1165 *
1166 * We should still try to roam, so retry after ignoring the
1167 * driver reject for any BSS that has an RSSI better than
1168 * disassoc_imminent_rssi_threshold.
1169 */
1170 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1171 struct neighbor_report *nei;
1172
1173 nei = &wpa_s->wnm_neighbor_report_elements[i];
1174 bss = wpa_bss_get_bssid(wpa_s, nei->bssid);
1175 if (bss && bss->level >
1176 wpa_s->conf->disassoc_imminent_rssi_threshold) {
1177 nei->drv_mbo_reject = 0;
1178 changed = true;
1179 }
1180 }
1181
1182 if (changed) {
1183 wpa_printf(MSG_DEBUG,
1184 "WNM: Ignore driver rejection due to imminent disassociation and acceptable RSSI");
1185 bss = wpa_supplicant_select_bss(wpa_s, ssid,
1186 &selected_ssid, 1);
1187 }
1188 }
1189 #endif /* CONFIG_MBO */
1190
1191 /*
1192 * If this is a pre-scan check, returning 0 will trigger a scan and
1193 * another call. In that case, reject "bad" candidates in the hope of
1194 * finding a better candidate after scanning.
1195 *
1196 * Use a simple heuristic to check whether the selection is reasonable
1197 * or a scan is a good idea. For that, we need to have found a
1198 * candidate BSS (which might be the current one), it is up-to-date,
1199 * and we don't want to immediately roam back again.
1200 */
1201 if (pre_scan_check) {
1202 struct os_reltime age;
1203
1204 if (!bss)
1205 return 0;
1206
1207 os_reltime_age(&bss->last_update, &age);
1208 if (age.sec >= 10)
1209 return 0;
1210
1211 #ifndef CONFIG_NO_ROAMING
1212 if (current_bss && bss != current_bss &&
1213 wpa_supplicant_need_to_roam_within_ess(wpa_s, bss,
1214 current_bss, false))
1215 return 0;
1216 #endif /* CONFIG_NO_ROAMING */
1217 }
1218
1219 #ifndef CONFIG_NO_ROAMING
1220 /* Apply normal roaming rules if we can stay with the current BSS */
1221 if (current_bss && bss != current_bss &&
1222 wpa_scan_res_match(wpa_s, 0, current_bss, wpa_s->current_ssid,
1223 1, 0, false) &&
1224 !wpa_supplicant_need_to_roam_within_ess(wpa_s, current_bss, bss,
1225 true))
1226 bss = current_bss;
1227 #endif /* CONFIG_NO_ROAMING */
1228
1229 if (!bss) {
1230 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1231 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1232 goto send_bss_resp_fail;
1233 }
1234
1235 wpa_printf(MSG_DEBUG,
1236 "WNM: Found an acceptable preferred transition candidate BSS "
1237 MACSTR " (RSSI %d, tput: %d bss-tput: %d)",
1238 MAC2STR(bss->bssid), bss->level, bss->est_throughput,
1239 current_bss ? (int) current_bss->est_throughput : -1);
1240
1241 /* Associate to the network */
1242 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
1243 return 1;
1244
1245 send_bss_resp_fail:
1246 if (wpa_s->wnm_reply) {
1247 /* If disassoc imminent is set, we must not reject */
1248 if (wpa_s->wnm_mode &
1249 (WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
1250 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)) {
1251 wpa_printf(MSG_DEBUG,
1252 "WNM: Accept BTM request because disassociation imminent bit is set");
1253 status = WNM_BSS_TM_ACCEPT;
1254 }
1255
1256 wnm_send_bss_transition_mgmt_resp(wpa_s, status, reason,
1257 0, NULL);
1258 }
1259
1260 wnm_btm_reset(wpa_s);
1261
1262 return 1;
1263 }
1264
1265
cand_pref_compar(const void * a,const void * b)1266 static int cand_pref_compar(const void *a, const void *b)
1267 {
1268 const struct neighbor_report *aa = a;
1269 const struct neighbor_report *bb = b;
1270
1271 if (aa->disassoc_imminent && !bb->disassoc_imminent)
1272 return 1;
1273 if (bb->disassoc_imminent && !aa->disassoc_imminent)
1274 return -1;
1275
1276 if (!aa->preference_present && !bb->preference_present)
1277 return 0;
1278 if (!aa->preference_present)
1279 return 1;
1280 if (!bb->preference_present)
1281 return -1;
1282 if (bb->preference > aa->preference)
1283 return 1;
1284 if (bb->preference < aa->preference)
1285 return -1;
1286 return 0;
1287 }
1288
1289
wnm_sort_cand_list(struct wpa_supplicant * wpa_s)1290 static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1291 {
1292 if (!wpa_s->wnm_neighbor_report_elements)
1293 return;
1294 qsort(wpa_s->wnm_neighbor_report_elements,
1295 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1296 cand_pref_compar);
1297 }
1298
1299
wnm_dump_cand_list(struct wpa_supplicant * wpa_s)1300 static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1301 {
1302 unsigned int i;
1303
1304 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1305 if (!wpa_s->wnm_neighbor_report_elements)
1306 return;
1307 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1308 struct neighbor_report *nei;
1309 char mld_info[42] = "";
1310
1311 nei = &wpa_s->wnm_neighbor_report_elements[i];
1312
1313 if (!is_zero_ether_addr(nei->mld_addr))
1314 os_snprintf(mld_info, sizeof(mld_info) - 1,
1315 " mld_addr=" MACSTR " links=0x%02x",
1316 MAC2STR(nei->mld_addr), nei->mld_links);
1317
1318 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1319 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d%s",
1320 i, MAC2STR(nei->bssid), nei->bssid_info,
1321 nei->regulatory_class,
1322 nei->channel_number, nei->phy_type,
1323 nei->preference_present ? nei->preference : -1,
1324 nei->freq, mld_info);
1325 }
1326 }
1327
1328
chan_supported(struct wpa_supplicant * wpa_s,int freq)1329 static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1330 {
1331 unsigned int i;
1332
1333 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1334 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1335 int j;
1336
1337 for (j = 0; j < mode->num_channels; j++) {
1338 struct hostapd_channel_data *chan;
1339
1340 chan = &mode->channels[j];
1341 if (chan->freq == freq &&
1342 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1343 return 1;
1344 }
1345 }
1346
1347 return 0;
1348 }
1349
1350
wnm_set_scan_freqs(struct wpa_supplicant * wpa_s)1351 static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1352 {
1353 unsigned int i;
1354 bool has_6ghz = false;
1355
1356 if (!wpa_s->wnm_neighbor_report_elements)
1357 return;
1358
1359 if (wpa_s->hw.modes == NULL)
1360 return;
1361
1362 os_free(wpa_s->next_scan_freqs);
1363 wpa_s->next_scan_freqs = NULL;
1364
1365 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1366 struct neighbor_report *nei;
1367
1368 nei = &wpa_s->wnm_neighbor_report_elements[i];
1369
1370 if (nei->preference_present && nei->preference == 0)
1371 continue;
1372
1373 if (nei->freq <= 0) {
1374 wpa_printf(MSG_DEBUG,
1375 "WNM: Unknown neighbor operating frequency for "
1376 MACSTR " - scan all channels",
1377 MAC2STR(nei->bssid));
1378 os_free(wpa_s->next_scan_freqs);
1379 wpa_s->next_scan_freqs = NULL;
1380 return;
1381 }
1382 if (chan_supported(wpa_s, nei->freq))
1383 int_array_add_unique(&wpa_s->next_scan_freqs,
1384 nei->freq);
1385 has_6ghz |= is_6ghz_freq(nei->freq);
1386 }
1387
1388 if (!wpa_s->next_scan_freqs)
1389 return;
1390
1391 wpa_printf(MSG_DEBUG,
1392 "WNM: Scan %zu frequencies based on transition candidate list",
1393 int_array_len(wpa_s->next_scan_freqs));
1394
1395 /*
1396 * Candidates on 6 GHz channels might be collocated ones, and thus, in
1397 * order to discover them need to include the frequencies on the 2.4
1398 * and 5 GHz bands. Since the scan time can be long, optimize the case
1399 * of a single channel by forcing passive scan instead of doing a
1400 * collocated scan.
1401 */
1402 if (has_6ghz) {
1403 struct hostapd_channel_data *chan;
1404 struct os_reltime now;
1405
1406 /* In case the candidate validity time is too short, force it
1407 * to be long enough to account for the longer scan time.
1408 */
1409 os_get_reltime(&now);
1410 now.sec += 5;
1411
1412 if (os_reltime_initialized(&wpa_s->wnm_cand_valid_until) &&
1413 !os_reltime_before(&wpa_s->wnm_cand_valid_until, &now)) {
1414 wpa_printf(MSG_DEBUG,
1415 "WNM: Scan: 6 GHz: update validity time");
1416
1417 os_memcpy(&wpa_s->wnm_cand_valid_until, &now,
1418 sizeof(now));
1419 }
1420
1421 if (int_array_len(wpa_s->next_scan_freqs) == 1) {
1422 wpa_printf(MSG_DEBUG,
1423 "WNM: Scan: Single 6 GHz channel: passive");
1424
1425 wpa_s->scan_req = MANUAL_SCAN_REQ;
1426 wpa_s->manual_scan_passive = 1;
1427 return;
1428 }
1429
1430 wpa_printf(MSG_DEBUG,
1431 "WNM: Scan: Add 2.4/5 GHz channels as well for 6 GHz discovery");
1432
1433 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1434 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1435 int j;
1436
1437 /* skip all the irrelevant modes */
1438 if ((mode->mode != HOSTAPD_MODE_IEEE80211B &&
1439 mode->mode != HOSTAPD_MODE_IEEE80211G &&
1440 mode->mode != HOSTAPD_MODE_IEEE80211A) ||
1441 mode->is_6ghz)
1442 continue;
1443
1444 for (j = 0; j < mode->num_channels; j++) {
1445 chan = &mode->channels[j];
1446 if (chan->flag & HOSTAPD_CHAN_DISABLED)
1447 continue;
1448
1449 int_array_add_unique(&wpa_s->next_scan_freqs,
1450 chan->freq);
1451 }
1452 }
1453
1454 wpa_printf(MSG_DEBUG,
1455 "WNM: Scan %zu frequencies (including collocated)",
1456 int_array_len(wpa_s->next_scan_freqs));
1457 }
1458 }
1459
1460
wnm_parse_candidate_list(struct wpa_supplicant * wpa_s,const u8 * pos,const u8 * end,int * num_valid_candidates)1461 static int wnm_parse_candidate_list(struct wpa_supplicant *wpa_s,
1462 const u8 *pos, const u8 *end,
1463 int *num_valid_candidates)
1464 {
1465 *num_valid_candidates = 0;
1466
1467 while (end - pos >= 2 &&
1468 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT) {
1469 u8 tag = *pos++;
1470 u8 len = *pos++;
1471
1472 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u", tag);
1473 if (len > end - pos) {
1474 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1475 return -1;
1476 }
1477 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1478 struct neighbor_report *rep;
1479
1480 if (!wpa_s->wnm_num_neighbor_report) {
1481 wpa_s->wnm_neighbor_report_elements = os_calloc(
1482 WNM_MAX_NEIGHBOR_REPORT,
1483 sizeof(struct neighbor_report));
1484 if (!wpa_s->wnm_neighbor_report_elements)
1485 return -1;
1486 }
1487
1488 rep = &wpa_s->wnm_neighbor_report_elements[
1489 wpa_s->wnm_num_neighbor_report];
1490 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
1491 if ((wpa_s->wnm_mode &
1492 WNM_BSS_TM_REQ_DISASSOC_IMMINENT) &&
1493 ether_addr_equal(rep->bssid, wpa_s->bssid))
1494 rep->disassoc_imminent = 1;
1495
1496 if (rep->preference_present && rep->preference)
1497 *num_valid_candidates += 1;
1498
1499 wpa_s->wnm_num_neighbor_report++;
1500 }
1501
1502 pos += len;
1503 }
1504
1505 return 0;
1506 }
1507
1508
ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant * wpa_s,const u8 * pos,const u8 * end,int reply)1509 static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1510 const u8 *pos, const u8 *end,
1511 int reply)
1512 {
1513 unsigned int beacon_int;
1514 u8 valid_int;
1515 #ifdef CONFIG_MBO
1516 const u8 *vendor;
1517 #endif /* CONFIG_MBO */
1518 bool disassoc_imminent;
1519 int num_valid_candidates;
1520
1521 if (wpa_s->disable_mbo_oce || wpa_s->conf->disable_btm)
1522 return;
1523
1524 if (end - pos < 5)
1525 return;
1526
1527 if (wpa_s->current_bss)
1528 beacon_int = wpa_s->current_bss->beacon_int;
1529 else
1530 beacon_int = 100; /* best guess */
1531
1532 wnm_btm_reset(wpa_s);
1533
1534 wpa_s->wnm_dialog_token = pos[0];
1535 wpa_s->wnm_mode = pos[1];
1536 wpa_s->wnm_disassoc_timer = WPA_GET_LE16(pos + 2);
1537 wpa_s->wnm_link_removal = false;
1538 valid_int = pos[4];
1539 wpa_s->wnm_reply = reply;
1540
1541 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1542 "dialog_token=%u request_mode=0x%x "
1543 "disassoc_timer=%u validity_interval=%u",
1544 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
1545 wpa_s->wnm_disassoc_timer, valid_int);
1546
1547 if (!wpa_s->wnm_dialog_token) {
1548 wpa_printf(MSG_DEBUG, "WNM: Invalid dialog token");
1549 goto reset;
1550 }
1551
1552 #if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1553 if (wpa_s->reject_btm_req_reason) {
1554 wpa_printf(MSG_INFO,
1555 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1556 wpa_s->reject_btm_req_reason);
1557 wnm_send_bss_transition_mgmt_resp(
1558 wpa_s, wpa_s->reject_btm_req_reason,
1559 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
1560 goto reset;
1561 }
1562 #endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1563
1564 pos += 5;
1565
1566 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
1567 if (end - pos < 12) {
1568 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1569 goto reset;
1570 }
1571 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
1572 pos += 12; /* BSS Termination Duration */
1573 }
1574
1575 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
1576 char url[256];
1577 u8 url_len;
1578
1579 if (end - pos < 1) {
1580 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1581 "Management Request (URL)");
1582 goto reset;
1583 }
1584 url_len = *pos++;
1585 if (url_len > end - pos) {
1586 wpa_printf(MSG_DEBUG,
1587 "WNM: Invalid BSS Transition Management Request (URL truncated)");
1588 goto reset;
1589 }
1590 os_memcpy(url, pos, url_len);
1591 url[url_len] = '\0';
1592 pos += url_len;
1593
1594 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1595 wpa_sm_pmf_enabled(wpa_s->wpa),
1596 wpa_s->wnm_disassoc_timer * beacon_int * 128 / 125,
1597 url);
1598 }
1599
1600 disassoc_imminent = wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1601
1602 /*
1603 * Based on IEEE Std 802.11be-2024, Table 9-538a (BSS Termination
1604 * Included and Link Removal Imminent fields encoding), when a station
1605 * is a non-AP MLD with more than one affiliated link, the Link Removal
1606 * Imminent field is set to 1, and the BSS Termination Included field
1607 * is set to 1, only one of the links is removed and the other links
1608 * remain associated. Ignore the Disassociation Imminent field in such
1609 * a case.
1610 *
1611 * TODO: We should check if the AP has more than one link.
1612 * TODO: We should pass the RX link and use that
1613 */
1614 if (disassoc_imminent && wpa_s->valid_links &&
1615 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_LINK_REMOVAL_IMMINENT) &&
1616 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED)) {
1617 /* If we still have a link, then just accept the request */
1618 if (wpa_s->valid_links & (wpa_s->valid_links - 1)) {
1619 wpa_printf(MSG_INFO,
1620 "WNM: BTM request for a single MLO link - ignore disassociation imminent since other links remain associated");
1621 disassoc_imminent = false;
1622
1623 wnm_send_bss_transition_mgmt_resp(
1624 wpa_s, WNM_BSS_TM_ACCEPT, 0, 0, NULL);
1625
1626 goto reset;
1627 }
1628
1629 /* The last link is being removed (which must be the assoc link)
1630 */
1631 wpa_s->wnm_link_removal = true;
1632 wpa_s->wnm_disassoc_mld = false;
1633 os_memcpy(wpa_s->wnm_disassoc_addr,
1634 wpa_s->links[wpa_s->mlo_assoc_link_id].bssid,
1635 ETH_ALEN);
1636 } else if (wpa_s->valid_links) {
1637 wpa_s->wnm_disassoc_mld = true;
1638 os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->ap_mld_addr,
1639 ETH_ALEN);
1640 } else {
1641 wpa_s->wnm_disassoc_mld = false;
1642 os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->bssid, ETH_ALEN);
1643 }
1644
1645 if (disassoc_imminent)
1646 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
1647 "Disassociation Timer %u", wpa_s->wnm_disassoc_timer);
1648
1649 #ifdef CONFIG_MBO
1650 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1651 if (vendor)
1652 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
1653 #endif /* CONFIG_MBO */
1654
1655 if (wnm_parse_candidate_list(wpa_s, pos, end,
1656 &num_valid_candidates) < 0)
1657 goto reset;
1658
1659 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
1660 if (!wpa_s->wnm_num_neighbor_report) {
1661 wpa_printf(MSG_DEBUG,
1662 "WNM: Candidate list included bit is set, but no candidates found");
1663 wnm_send_bss_transition_mgmt_resp(
1664 wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1665 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1666 NULL);
1667 goto reset;
1668 }
1669 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
1670 }
1671
1672 if (wpa_s->wnm_num_neighbor_report) {
1673 unsigned int valid_ms;
1674
1675 wnm_sort_cand_list(wpa_s);
1676 wnm_dump_cand_list(wpa_s);
1677 valid_ms = valid_int * beacon_int * 128 / 125;
1678 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1679 valid_ms);
1680 os_get_reltime(&wpa_s->wnm_cand_valid_until);
1681 os_reltime_add_ms(&wpa_s->wnm_cand_valid_until, valid_ms);
1682 } else if (!disassoc_imminent) {
1683 enum bss_trans_mgmt_status_code status;
1684
1685 /* No candidate list and disassociation is not imminent */
1686
1687 if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) ||
1688 wpa_s->wnm_link_removal)
1689 status = WNM_BSS_TM_ACCEPT;
1690 else {
1691 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1692 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1693 }
1694
1695 if (reply)
1696 wnm_send_bss_transition_mgmt_resp(
1697 wpa_s, status,
1698 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1699 NULL);
1700
1701 goto reset;
1702 }
1703
1704 /*
1705 * Try fetching the latest scan results from the kernel.
1706 * This can help in finding more up-to-date information should
1707 * the driver have done some internal scanning operations after
1708 * the last scan result update in wpa_supplicant.
1709 *
1710 * It is not a new scan, this does not update the last_scan
1711 * timestamp nor will it expire old BSSs.
1712 */
1713 wpa_supplicant_update_scan_results(wpa_s, NULL);
1714 if (wnm_scan_process(wpa_s, true) > 0)
1715 return;
1716 wpa_printf(MSG_DEBUG,
1717 "WNM: No valid match in previous scan results - try a new scan");
1718
1719 /*
1720 * If we have a fixed BSSID configured, just reject at this point.
1721 * NOTE: We could actually check if we are allowed to stay (and we do
1722 * above if we have scan results available).
1723 */
1724 if (wpa_s->current_ssid && wpa_s->current_ssid->bssid_set) {
1725 wpa_printf(MSG_DEBUG, "WNM: Fixed BSSID, rejecting request");
1726
1727 if (reply)
1728 wnm_send_bss_transition_mgmt_resp(
1729 wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1730 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1731 NULL);
1732
1733 goto reset;
1734 }
1735
1736 wnm_set_scan_freqs(wpa_s);
1737 if (num_valid_candidates == 1) {
1738 /* Any invalid candidate was sorted to the end */
1739 os_memcpy(wpa_s->next_scan_bssid,
1740 wpa_s->wnm_neighbor_report_elements[0].bssid,
1741 ETH_ALEN);
1742 wpa_printf(MSG_DEBUG,
1743 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1744 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1745 }
1746 wpa_s->wnm_transition_scan = true;
1747 wpa_supplicant_req_scan(wpa_s, 0, 0);
1748
1749 /* Continue from scan handler */
1750 return;
1751
1752 reset:
1753 wnm_btm_reset(wpa_s);
1754 }
1755
1756
wnm_btm_resp_tx_status(struct wpa_supplicant * wpa_s,const u8 * data,size_t data_len)1757 int wnm_btm_resp_tx_status(struct wpa_supplicant *wpa_s, const u8 *data,
1758 size_t data_len)
1759 {
1760 const struct ieee80211_mgmt *frame =
1761 (const struct ieee80211_mgmt *) data;
1762
1763 if (data_len <
1764 IEEE80211_HDRLEN + sizeof(frame->u.action.u.bss_tm_resp) ||
1765 frame->u.action.category != WLAN_ACTION_WNM ||
1766 frame->u.action.u.bss_tm_resp.action != WNM_BSS_TRANS_MGMT_RESP ||
1767 frame->u.action.u.bss_tm_resp.status_code != WNM_BSS_TM_ACCEPT)
1768 return -1;
1769
1770 /*
1771 * If disassoc imminent bit was set in the request, the response may
1772 * indicate accept even if no candidate was found, so bail out here.
1773 */
1774 if (!wpa_s->wnm_target_bss) {
1775 wpa_printf(MSG_DEBUG, "WNM: Target BSS is not set");
1776 return 0;
1777 }
1778
1779 if (!wpa_s->current_ssid)
1780 return 0;
1781
1782 wnm_bss_tm_connect(wpa_s, wpa_s->wnm_target_bss, wpa_s->current_ssid,
1783 0);
1784
1785 wpa_s->wnm_target_bss = NULL;
1786 return 0;
1787 }
1788
1789
1790 #define BTM_QUERY_MIN_SIZE 4
1791
wnm_send_bss_transition_mgmt_query(struct wpa_supplicant * wpa_s,u8 query_reason,const char * btm_candidates,int cand_list)1792 int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
1793 u8 query_reason,
1794 const char *btm_candidates,
1795 int cand_list)
1796 {
1797 struct wpabuf *buf;
1798 int ret;
1799
1800 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
1801 MACSTR " query_reason=%u%s",
1802 MAC2STR(wpa_s->bssid), query_reason,
1803 cand_list ? " candidate list" : "");
1804
1805 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1806 if (!buf)
1807 return -1;
1808
1809 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1810 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1811 wpabuf_put_u8(buf, 1);
1812 wpabuf_put_u8(buf, query_reason);
1813
1814 if (cand_list)
1815 wnm_add_cand_list(wpa_s, &buf);
1816
1817 if (btm_candidates) {
1818 const size_t max_len = 1000;
1819
1820 ret = wpabuf_resize(&buf, max_len);
1821 if (ret < 0) {
1822 wpabuf_free(buf);
1823 return ret;
1824 }
1825
1826 ret = ieee802_11_parse_candidate_list(btm_candidates,
1827 wpabuf_put(buf, 0),
1828 max_len);
1829 if (ret < 0) {
1830 wpabuf_free(buf);
1831 return ret;
1832 }
1833
1834 wpabuf_put(buf, ret);
1835 }
1836
1837 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1838 wpa_s->own_addr, wpa_s->bssid,
1839 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1840
1841 wpabuf_free(buf);
1842 return ret;
1843 }
1844
1845
ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * data,int len)1846 static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1847 const u8 *sa, const u8 *data,
1848 int len)
1849 {
1850 const u8 *pos, *end, *next;
1851 u8 ie, ie_len;
1852
1853 pos = data;
1854 end = data + len;
1855
1856 while (end - pos > 1) {
1857 ie = *pos++;
1858 ie_len = *pos++;
1859 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1860 ie, ie_len);
1861 if (ie_len > end - pos) {
1862 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1863 "subelement");
1864 break;
1865 }
1866 next = pos + ie_len;
1867 if (ie_len < 4) {
1868 pos = next;
1869 continue;
1870 }
1871 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1872 WPA_GET_BE24(pos), pos[3]);
1873
1874 #ifdef CONFIG_HS20
1875 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1876 WPA_GET_BE24(pos) == OUI_WFA &&
1877 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1878 const u8 *ie_end;
1879 u8 url_len;
1880 char *url;
1881 u8 code;
1882 u16 reauth_delay;
1883
1884 ie_end = pos + ie_len;
1885 pos += 4;
1886 code = *pos++;
1887 reauth_delay = WPA_GET_LE16(pos);
1888 pos += 2;
1889 url_len = *pos++;
1890 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1891 "Imminent - Reason Code %u "
1892 "Re-Auth Delay %u URL Length %u",
1893 code, reauth_delay, url_len);
1894 if (url_len > ie_end - pos)
1895 break;
1896 url = os_malloc(url_len + 1);
1897 if (url == NULL)
1898 break;
1899 os_memcpy(url, pos, url_len);
1900 url[url_len] = '\0';
1901 hs20_rx_deauth_imminent_notice(wpa_s, code,
1902 reauth_delay, url);
1903 os_free(url);
1904 pos = next;
1905 continue;
1906 }
1907
1908 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1909 WPA_GET_BE24(pos) == OUI_WFA &&
1910 pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
1911 const u8 *ie_end;
1912 u8 url_len;
1913 char *url;
1914
1915 ie_end = pos + ie_len;
1916 pos += 4;
1917 url_len = *pos++;
1918 wpa_printf(MSG_DEBUG,
1919 "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
1920 url_len);
1921 if (url_len > ie_end - pos)
1922 break;
1923 url = os_malloc(url_len + 1);
1924 if (!url)
1925 break;
1926 os_memcpy(url, pos, url_len);
1927 url[url_len] = '\0';
1928 hs20_rx_t_c_acceptance(wpa_s, url);
1929 os_free(url);
1930 pos = next;
1931 continue;
1932 }
1933 #endif /* CONFIG_HS20 */
1934
1935 pos = next;
1936 }
1937 }
1938
1939
ieee802_11_rx_wnm_notif_req(struct wpa_supplicant * wpa_s,const u8 * da,const u8 * sa,const u8 * frm,int len)1940 static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1941 const u8 *da, const u8 *sa,
1942 const u8 *frm, int len)
1943 {
1944 const u8 *pos, *end;
1945 u8 dialog_token, type;
1946
1947 if (is_multicast_ether_addr(da)) {
1948 wpa_printf(MSG_DEBUG,
1949 "WNM: Ignore group-addressed WNM Notification Request frame (A1="
1950 MACSTR " A2=" MACSTR ")",
1951 MAC2STR(da), MAC2STR(sa));
1952 return;
1953 }
1954
1955 /* Dialog Token [1] | Type [1] | Subelements */
1956
1957 if (len < 2 || sa == NULL)
1958 return;
1959 end = frm + len;
1960 pos = frm;
1961 dialog_token = *pos++;
1962 type = *pos++;
1963
1964 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1965 "(dialog_token %u type %u sa " MACSTR ")",
1966 dialog_token, type, MAC2STR(sa));
1967 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1968 pos, end - pos);
1969
1970 if (wpa_s->wpa_state != WPA_COMPLETED ||
1971 (!ether_addr_equal(sa, wpa_s->bssid) &&
1972 (!wpa_s->valid_links ||
1973 !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
1974 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1975 "from our AP - ignore it");
1976 return;
1977 }
1978
1979 switch (type) {
1980 case 1:
1981 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1982 break;
1983 default:
1984 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1985 "WNM-Notification type %u", type);
1986 break;
1987 }
1988 }
1989
1990
ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * frm,int len)1991 static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
1992 const u8 *sa, const u8 *frm,
1993 int len)
1994 {
1995 u8 dialog_token, req_info, auto_report, timeout;
1996
1997 if (!wpa_s->conf->coloc_intf_reporting)
1998 return;
1999
2000 /* Dialog Token [1] | Request Info [1] */
2001
2002 if (len < 2)
2003 return;
2004 dialog_token = frm[0];
2005 req_info = frm[1];
2006 auto_report = req_info & 0x03;
2007 timeout = req_info >> 2;
2008
2009 wpa_dbg(wpa_s, MSG_DEBUG,
2010 "WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
2011 dialog_token, auto_report, timeout, MAC2STR(sa));
2012
2013 if (dialog_token == 0)
2014 return; /* only nonzero values are used for request */
2015
2016 if (wpa_s->wpa_state != WPA_COMPLETED ||
2017 (!ether_addr_equal(sa, wpa_s->bssid) &&
2018 (!wpa_s->valid_links ||
2019 !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
2020 wpa_dbg(wpa_s, MSG_DEBUG,
2021 "WNM: Collocated Interference Request frame not from current AP - ignore it");
2022 return;
2023 }
2024
2025 wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
2026 dialog_token, auto_report, timeout);
2027 wpa_s->coloc_intf_dialog_token = dialog_token;
2028 wpa_s->coloc_intf_auto_report = auto_report;
2029 wpa_s->coloc_intf_timeout = timeout;
2030 }
2031
2032
ieee802_11_rx_wnm_action(struct wpa_supplicant * wpa_s,const struct ieee80211_mgmt * mgmt,size_t len)2033 void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
2034 const struct ieee80211_mgmt *mgmt, size_t len)
2035 {
2036 const u8 *pos, *end;
2037 u8 act;
2038
2039 if (len < IEEE80211_HDRLEN + 2)
2040 return;
2041
2042 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
2043 act = *pos++;
2044 end = ((const u8 *) mgmt) + len;
2045
2046 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
2047 act, MAC2STR(mgmt->sa));
2048 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
2049 (!ether_addr_equal(mgmt->sa, wpa_s->bssid) &&
2050 (!wpa_s->valid_links ||
2051 !ether_addr_equal(mgmt->sa, wpa_s->ap_mld_addr)))) {
2052 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
2053 "frame");
2054 return;
2055 }
2056
2057 switch (act) {
2058 case WNM_BSS_TRANS_MGMT_REQ:
2059 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
2060 !(mgmt->da[0] & 0x01));
2061 break;
2062 case WNM_SLEEP_MODE_RESP:
2063 ieee802_11_rx_wnmsleep_resp(wpa_s, mgmt->da, mgmt->sa,
2064 pos, end - pos);
2065 break;
2066 case WNM_NOTIFICATION_REQ:
2067 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->da, mgmt->sa,
2068 pos, end - pos);
2069 break;
2070 case WNM_COLLOCATED_INTERFERENCE_REQ:
2071 ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
2072 end - pos);
2073 break;
2074 default:
2075 wpa_printf(MSG_ERROR, "WNM: Unknown request");
2076 break;
2077 }
2078 }
2079
2080
wnm_send_coloc_intf_report(struct wpa_supplicant * wpa_s,u8 dialog_token,const struct wpabuf * elems)2081 int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
2082 const struct wpabuf *elems)
2083 {
2084 struct wpabuf *buf;
2085 int ret;
2086
2087 if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
2088 return -1;
2089
2090 wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
2091 MACSTR " (dialog token %u)",
2092 MAC2STR(wpa_s->bssid), dialog_token);
2093
2094 buf = wpabuf_alloc(3 + wpabuf_len(elems));
2095 if (!buf)
2096 return -1;
2097
2098 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
2099 wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
2100 wpabuf_put_u8(buf, dialog_token);
2101 wpabuf_put_buf(buf, elems);
2102
2103 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
2104 wpa_s->own_addr, wpa_s->bssid,
2105 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
2106 wpabuf_free(buf);
2107 return ret;
2108 }
2109
2110
wnm_set_coloc_intf_elems(struct wpa_supplicant * wpa_s,struct wpabuf * elems)2111 void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
2112 struct wpabuf *elems)
2113 {
2114 if (elems && wpabuf_len(elems) == 0) {
2115 wpabuf_free(elems);
2116 elems = NULL;
2117 }
2118
2119 /* NOTE: The elements are not stored as they are only send out once */
2120
2121 if (wpa_s->conf->coloc_intf_reporting && elems &&
2122 wpa_s->coloc_intf_dialog_token &&
2123 (wpa_s->coloc_intf_auto_report == 1 ||
2124 wpa_s->coloc_intf_auto_report == 3)) {
2125 /* TODO: Check that there has not been less than
2126 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
2127 */
2128 wnm_send_coloc_intf_report(wpa_s,
2129 wpa_s->coloc_intf_dialog_token,
2130 elems);
2131 }
2132
2133 wpabuf_free(elems);
2134 }
2135
2136
wnm_clear_coloc_intf_reporting(struct wpa_supplicant * wpa_s)2137 void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
2138 {
2139 wpa_s->coloc_intf_dialog_token = 0;
2140 wpa_s->coloc_intf_auto_report = 0;
2141 }
2142
2143
wnm_is_bss_excluded(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)2144 bool wnm_is_bss_excluded(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2145 {
2146 int i;
2147
2148 /*
2149 * In case disassociation imminent is set, do no try to use a BSS to
2150 * which we are connected.
2151 */
2152 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
2153 if (!wpa_s->wnm_disassoc_mld) {
2154 if (ether_addr_equal(bss->bssid,
2155 wpa_s->wnm_disassoc_addr))
2156 return true;
2157 } else {
2158 if (ether_addr_equal(bss->mld_addr,
2159 wpa_s->wnm_disassoc_addr))
2160 return true;
2161 }
2162 }
2163
2164 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
2165 struct neighbor_report *nei;
2166
2167 nei = &wpa_s->wnm_neighbor_report_elements[i];
2168 if (!ether_addr_equal(nei->bssid, bss->bssid) &&
2169 (is_zero_ether_addr(bss->mld_addr) ||
2170 !ether_addr_equal(nei->mld_addr, bss->mld_addr)))
2171 continue;
2172
2173 if (nei->preference_present && nei->preference == 0)
2174 return true;
2175
2176 #ifdef CONFIG_MBO
2177 if (nei->drv_mbo_reject)
2178 return true;
2179 #endif /* CONFIG_MBO */
2180
2181 /*
2182 * NOTE: We should select one entry and stick with it, but to
2183 * do that we need to refactor the BSS selection to be MLD
2184 * aware from the beginning. Instead we just check whether the
2185 * link is permitted in any possible configuration. We are not
2186 * supposed to do that, however the AP is able to reject a
2187 * subset of the requested links.
2188 */
2189 if (nei->mld_links && !(nei->mld_links & BIT(bss->mld_link_id)))
2190 continue;
2191
2192 break;
2193 }
2194
2195 /* If the abridged bit is set, the BSS must be a known neighbor. */
2196 if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ABRIDGED) &&
2197 wpa_s->wnm_num_neighbor_report == i)
2198 return true;
2199
2200 return false;
2201 }
2202