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 * frm,int len)306 static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
307 const u8 *frm, int len)
308 {
309 /*
310 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
311 * WNM-Sleep Mode IE | TFS Response IE
312 */
313 const u8 *pos = frm; /* point to payload after the action field */
314 u16 key_len_total;
315 struct wnm_sleep_element *wnmsleep_ie = NULL;
316 /* multiple TFS Resp IE (assuming consecutive) */
317 const u8 *tfsresp_ie_start = NULL;
318 const u8 *tfsresp_ie_end = NULL;
319 #ifdef CONFIG_OCV
320 const u8 *oci_ie = NULL;
321 u8 oci_ie_len = 0;
322 #endif /* CONFIG_OCV */
323 size_t left;
324
325 if (!wpa_s->wnmsleep_used) {
326 wpa_printf(MSG_DEBUG,
327 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
328 return;
329 }
330
331 if (len < 3)
332 return;
333 key_len_total = WPA_GET_LE16(frm + 1);
334
335 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
336 frm[0], key_len_total);
337 left = len - 3;
338 if (key_len_total > left) {
339 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
340 return;
341 }
342 pos += 3 + key_len_total;
343 while (pos - frm + 1 < len) {
344 u8 ie_len = *(pos + 1);
345 if (2 + ie_len > frm + len - pos) {
346 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
347 break;
348 }
349 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
350 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
351 wnmsleep_ie = (struct wnm_sleep_element *) pos;
352 else if (*pos == WLAN_EID_TFS_RESP) {
353 if (!tfsresp_ie_start)
354 tfsresp_ie_start = pos;
355 tfsresp_ie_end = pos;
356 #ifdef CONFIG_OCV
357 } else if (*pos == WLAN_EID_EXTENSION && ie_len >= 1 &&
358 pos[2] == WLAN_EID_EXT_OCV_OCI) {
359 oci_ie = pos + 3;
360 oci_ie_len = ie_len - 1;
361 #endif /* CONFIG_OCV */
362 } else
363 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
364 pos += ie_len + 2;
365 }
366
367 if (!wnmsleep_ie) {
368 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
369 return;
370 }
371
372 #ifdef CONFIG_OCV
373 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT &&
374 wpa_sm_ocv_enabled(wpa_s->wpa)) {
375 struct wpa_channel_info ci;
376
377 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
378 wpa_msg(wpa_s, MSG_WARNING,
379 "Failed to get channel info to validate received OCI in WNM-Sleep Mode frame");
380 return;
381 }
382
383 if (ocv_verify_tx_params(oci_ie, oci_ie_len, &ci,
384 channel_width_to_int(ci.chanwidth),
385 ci.seg1_idx) != OCI_SUCCESS) {
386 wpa_msg(wpa_s, MSG_WARNING, "WNM: OCV failed: %s",
387 ocv_errorstr);
388 return;
389 }
390 }
391 #endif /* CONFIG_OCV */
392
393 wpa_s->wnmsleep_used = 0;
394
395 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
396 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
397 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
398 "frame (action=%d, intval=%d)",
399 wnmsleep_ie->action_type, wnmsleep_ie->intval);
400 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
401 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
402 tfsresp_ie_end);
403 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
404 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
405 }
406 } else {
407 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
408 "(action=%d, intval=%d)",
409 wnmsleep_ie->action_type, wnmsleep_ie->intval);
410 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
411 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
412 wpa_s->bssid, NULL, NULL);
413 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
414 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
415 wpa_s->bssid, NULL, NULL);
416 }
417 }
418
419
wnm_btm_reset(struct wpa_supplicant * wpa_s)420 void wnm_btm_reset(struct wpa_supplicant *wpa_s)
421 {
422 int i;
423
424 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
425 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
426 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
427 }
428
429 wpa_s->wnm_num_neighbor_report = 0;
430 os_free(wpa_s->wnm_neighbor_report_elements);
431 wpa_s->wnm_neighbor_report_elements = NULL;
432
433 wpa_s->wnm_cand_valid_until.sec = 0;
434 wpa_s->wnm_cand_valid_until.usec = 0;
435
436 wpa_s->wnm_mode = 0;
437 wpa_s->wnm_dialog_token = 0;
438 wpa_s->wnm_reply = 0;
439
440 #ifdef CONFIG_MBO
441 wpa_s->wnm_mbo_trans_reason_present = 0;
442 wpa_s->wnm_mbo_transition_reason = 0;
443 #endif /* CONFIG_MBO */
444 }
445
446
wnm_parse_neighbor_report_elem(struct neighbor_report * rep,u8 id,u8 elen,const u8 * pos)447 static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
448 u8 id, u8 elen, const u8 *pos)
449 {
450 switch (id) {
451 case WNM_NEIGHBOR_TSF:
452 if (elen < 2 + 2) {
453 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
454 break;
455 }
456 rep->tsf_offset = WPA_GET_LE16(pos);
457 rep->beacon_int = WPA_GET_LE16(pos + 2);
458 rep->tsf_present = 1;
459 break;
460 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
461 if (elen < 2) {
462 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
463 "country string");
464 break;
465 }
466 os_memcpy(rep->country, pos, 2);
467 rep->country_present = 1;
468 break;
469 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
470 if (elen < 1) {
471 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
472 "candidate");
473 break;
474 }
475 rep->preference = pos[0];
476 rep->preference_present = 1;
477 break;
478 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
479 if (elen < 10) {
480 wpa_printf(MSG_DEBUG,
481 "WNM: Too short BSS termination duration");
482 break;
483 }
484 rep->bss_term_tsf = WPA_GET_LE64(pos);
485 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
486 rep->bss_term_present = 1;
487 break;
488 case WNM_NEIGHBOR_BEARING:
489 if (elen < 8) {
490 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
491 "bearing");
492 break;
493 }
494 rep->bearing = WPA_GET_LE16(pos);
495 rep->distance = WPA_GET_LE32(pos + 2);
496 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
497 rep->bearing_present = 1;
498 break;
499 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
500 if (elen < 1) {
501 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
502 "pilot");
503 break;
504 }
505 os_free(rep->meas_pilot);
506 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
507 if (rep->meas_pilot == NULL)
508 break;
509 rep->meas_pilot->measurement_pilot = pos[0];
510 rep->meas_pilot->subelem_len = elen - 1;
511 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
512 break;
513 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
514 if (elen < 5) {
515 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
516 "capabilities");
517 break;
518 }
519 os_memcpy(rep->rm_capab, pos, 5);
520 rep->rm_capab_present = 1;
521 break;
522 case WNM_NEIGHBOR_MULTIPLE_BSSID:
523 if (elen < 1) {
524 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
525 break;
526 }
527 os_free(rep->mul_bssid);
528 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
529 if (rep->mul_bssid == NULL)
530 break;
531 rep->mul_bssid->max_bssid_indicator = pos[0];
532 rep->mul_bssid->subelem_len = elen - 1;
533 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
534 break;
535 default:
536 wpa_printf(MSG_DEBUG,
537 "WNM: Unsupported neighbor report subelement id %u",
538 id);
539 break;
540 }
541 }
542
543
wnm_nei_get_chan(struct wpa_supplicant * wpa_s,u8 op_class,u8 chan)544 static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
545 {
546 struct wpa_bss *bss = wpa_s->current_bss;
547 const char *country = NULL;
548 int freq;
549
550 if (bss) {
551 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
552
553 if (elem && elem[1] >= 2)
554 country = (const char *) (elem + 2);
555 }
556
557 freq = ieee80211_chan_to_freq(country, op_class, chan);
558 if (freq <= 0 && (op_class == 0 || op_class == 255)) {
559 /*
560 * Some APs do not advertise correct operating class
561 * information. Try to determine the most likely operating
562 * frequency based on the channel number.
563 */
564 if (chan >= 1 && chan <= 13)
565 freq = 2407 + chan * 5;
566 else if (chan == 14)
567 freq = 2484;
568 else if (chan >= 36 && chan <= 177)
569 freq = 5000 + chan * 5;
570 }
571 return freq;
572 }
573
574
wnm_parse_neighbor_report(struct wpa_supplicant * wpa_s,const u8 * pos,u8 len,struct neighbor_report * rep)575 static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
576 const u8 *pos, u8 len,
577 struct neighbor_report *rep)
578 {
579 u8 left = len;
580
581 if (left < 13) {
582 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
583 return;
584 }
585
586 os_memcpy(rep->bssid, pos, ETH_ALEN);
587 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
588 rep->regulatory_class = *(pos + 10);
589 rep->channel_number = *(pos + 11);
590 rep->phy_type = *(pos + 12);
591
592 pos += 13;
593 left -= 13;
594
595 while (left >= 2) {
596 u8 id, elen;
597
598 id = *pos++;
599 elen = *pos++;
600 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
601 left -= 2;
602 if (elen > left) {
603 wpa_printf(MSG_DEBUG,
604 "WNM: Truncated neighbor report subelement");
605 break;
606 }
607 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
608 left -= elen;
609 pos += elen;
610 }
611
612 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
613 rep->channel_number);
614 }
615
616
617 static void
fetch_drv_mbo_candidate_info(struct wpa_supplicant * wpa_s,enum mbo_transition_reject_reason * reason)618 fetch_drv_mbo_candidate_info(struct wpa_supplicant *wpa_s,
619 enum mbo_transition_reject_reason *reason)
620 {
621 #ifdef CONFIG_MBO
622 struct wpa_bss_trans_info params;
623 struct wpa_bss_candidate_info *info = NULL;
624 struct neighbor_report *nei;
625 u8 *pos;
626 unsigned int i;
627
628 if (!wpa_s->wnm_mbo_trans_reason_present)
629 return;
630
631 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
632 params.n_candidates = 0;
633 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
634 if (!params.bssid)
635 return;
636
637 pos = params.bssid;
638 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
639 nei = &wpa_s->wnm_neighbor_report_elements[i];
640
641 nei->drv_mbo_reject = 0;
642
643 if (nei->preference_present && nei->preference == 0)
644 continue;
645
646 /* Should we query BSSIDs that we reject for other reasons? */
647
648 os_memcpy(pos, nei->bssid, ETH_ALEN);
649 pos += ETH_ALEN;
650 params.n_candidates++;
651 }
652
653 if (!params.n_candidates)
654 goto end;
655
656 info = wpa_drv_get_bss_trans_status(wpa_s, ¶ms);
657 if (!info)
658 goto end;
659
660 for (i = 0; i < info->num; i++) {
661 int j;
662
663 for (j = 0; j < wpa_s->wnm_num_neighbor_report; j++) {
664 nei = &wpa_s->wnm_neighbor_report_elements[j];
665
666 if (!ether_addr_equal(info->candidates[i].bssid,
667 nei->bssid))
668 continue;
669
670 nei->drv_mbo_reject = !info->candidates[i].is_accept;
671
672 /* Use the reject reason from the first candidate */
673 if (i == 0 && nei->drv_mbo_reject)
674 *reason = info->candidates[i].reject_reason;
675
676 break;
677 }
678 }
679
680 end:
681 os_free(params.bssid);
682 if (info) {
683 os_free(info->candidates);
684 os_free(info);
685 }
686 #endif /* CONFIG_MBO */
687 }
688
689
wpa_bss_ies_eq(struct wpa_bss * a,struct wpa_bss * b,u8 eid)690 static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
691 {
692 const u8 *ie_a, *ie_b;
693
694 if (!a || !b)
695 return 0;
696
697 ie_a = wpa_bss_get_ie(a, eid);
698 ie_b = wpa_bss_get_ie(b, eid);
699
700 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
701 return 0;
702
703 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
704 }
705
706
wnm_get_bss_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)707 static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
708 {
709 u32 info = 0;
710
711 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
712
713 /*
714 * Leave the security and key scope bits unset to indicate that the
715 * security information is not available.
716 */
717
718 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
719 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
720 if (bss->caps & WLAN_CAPABILITY_QOS)
721 info |= NEI_REP_BSSID_INFO_QOS;
722 if (bss->caps & WLAN_CAPABILITY_APSD)
723 info |= NEI_REP_BSSID_INFO_APSD;
724 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
725 info |= NEI_REP_BSSID_INFO_RM;
726 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
727 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
728 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
729 info |= NEI_REP_BSSID_INFO_IMM_BA;
730 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
731 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
732 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
733 info |= NEI_REP_BSSID_INFO_HT;
734
735 return info;
736 }
737
738
wnm_add_nei_rep(struct wpabuf ** buf,const u8 * bssid,u32 bss_info,u8 op_class,u8 chan,u8 phy_type,u8 pref)739 static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
740 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
741 u8 pref)
742 {
743 if (wpabuf_len(*buf) + 18 >
744 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
745 wpa_printf(MSG_DEBUG,
746 "WNM: No room in frame for Neighbor Report element");
747 return -1;
748 }
749
750 if (wpabuf_resize(buf, 18) < 0) {
751 wpa_printf(MSG_DEBUG,
752 "WNM: Failed to allocate memory for Neighbor Report element");
753 return -1;
754 }
755
756 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
757 /* length: 13 for basic neighbor report + 3 for preference subelement */
758 wpabuf_put_u8(*buf, 16);
759 wpabuf_put_data(*buf, bssid, ETH_ALEN);
760 wpabuf_put_le32(*buf, bss_info);
761 wpabuf_put_u8(*buf, op_class);
762 wpabuf_put_u8(*buf, chan);
763 wpabuf_put_u8(*buf, phy_type);
764 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
765 wpabuf_put_u8(*buf, 1);
766 wpabuf_put_u8(*buf, pref);
767 return 0;
768 }
769
770
wnm_nei_rep_add_bss(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpabuf ** buf,u8 pref)771 static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
772 struct wpa_bss *bss, struct wpabuf **buf,
773 u8 pref)
774 {
775 const u8 *ie;
776 u8 op_class, chan;
777 int sec_chan = 0;
778 enum oper_chan_width vht = CONF_OPER_CHWIDTH_USE_HT;
779 enum phy_type phy_type;
780 u32 info;
781 struct ieee80211_ht_operation *ht_oper = NULL;
782 struct ieee80211_vht_operation *vht_oper = NULL;
783
784 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
785 if (ie && ie[1] >= 2) {
786 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
787
788 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
789 sec_chan = 1;
790 else if (ht_oper->ht_param &
791 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
792 sec_chan = -1;
793 }
794
795 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
796 if (ie && ie[1] >= 1) {
797 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
798
799 if (vht_oper->vht_op_info_chwidth == CHANWIDTH_80MHZ ||
800 vht_oper->vht_op_info_chwidth == CHANWIDTH_160MHZ ||
801 vht_oper->vht_op_info_chwidth == CHANWIDTH_80P80MHZ)
802 vht = vht_oper->vht_op_info_chwidth;
803 }
804
805 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
806 &chan) == NUM_HOSTAPD_MODES) {
807 wpa_printf(MSG_DEBUG,
808 "WNM: Cannot determine operating class and channel");
809 return -2;
810 }
811
812 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
813 (vht_oper != NULL));
814 if (phy_type == PHY_TYPE_UNSPECIFIED) {
815 wpa_printf(MSG_DEBUG,
816 "WNM: Cannot determine BSS phy type for Neighbor Report");
817 return -2;
818 }
819
820 info = wnm_get_bss_info(wpa_s, bss);
821
822 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
823 pref);
824 }
825
826
wnm_add_cand_list(struct wpa_supplicant * wpa_s,struct wpabuf ** buf)827 static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
828 {
829 unsigned int i, pref = 255;
830 struct os_reltime now;
831 struct wpa_ssid *ssid = wpa_s->current_ssid;
832
833 if (!ssid)
834 return;
835
836 /*
837 * TODO: Define when scan results are no longer valid for the candidate
838 * list.
839 */
840 os_get_reltime(&now);
841 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
842 return;
843
844 wpa_printf(MSG_DEBUG,
845 "WNM: Add candidate list to BSS Transition Management Response frame");
846 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
847 struct wpa_bss *bss = wpa_s->last_scan_res[i];
848 int res;
849
850 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0, false)) {
851 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
852 if (res == -2)
853 continue; /* could not build entry for BSS */
854 if (res < 0)
855 break; /* no more room for candidates */
856 if (pref == 1)
857 break;
858 }
859 }
860
861 wpa_hexdump_buf(MSG_DEBUG,
862 "WNM: BSS Transition Management Response candidate list",
863 *buf);
864 }
865
866
867 #define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
868
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)869 static int wnm_send_bss_transition_mgmt_resp(
870 struct wpa_supplicant *wpa_s,
871 enum bss_trans_mgmt_status_code status,
872 enum mbo_transition_reject_reason reason,
873 u8 delay, const u8 *target_bssid)
874 {
875 struct wpabuf *buf;
876 int res;
877
878 wpa_s->wnm_reply = 0;
879
880 wpa_printf(MSG_DEBUG,
881 "WNM: Send BSS Transition Management Response to " MACSTR
882 " dialog_token=%u status=%u reason=%u delay=%d",
883 MAC2STR(wpa_s->bssid), wpa_s->wnm_dialog_token, status,
884 reason, delay);
885 if (!wpa_s->current_bss) {
886 wpa_printf(MSG_DEBUG,
887 "WNM: Current BSS not known - drop response");
888 return -1;
889 }
890
891 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
892 if (!buf) {
893 wpa_printf(MSG_DEBUG,
894 "WNM: Failed to allocate memory for BTM response");
895 return -1;
896 }
897
898 wpa_s->bss_tm_status = status;
899 wpas_notify_bss_tm_status(wpa_s);
900
901 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
902 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
903 wpabuf_put_u8(buf, wpa_s->wnm_dialog_token);
904 wpabuf_put_u8(buf, status);
905 wpabuf_put_u8(buf, delay);
906 if (target_bssid) {
907 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
908 } else if (status == WNM_BSS_TM_ACCEPT) {
909 /*
910 * P802.11-REVmc clarifies that the Target BSSID field is always
911 * present when status code is zero, so use a fake value here if
912 * no BSSID is yet known.
913 */
914 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
915 }
916
917 if (status == WNM_BSS_TM_ACCEPT && target_bssid)
918 wnm_add_cand_list(wpa_s, &buf);
919
920 #ifdef CONFIG_MBO
921 if (status != WNM_BSS_TM_ACCEPT &&
922 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
923 u8 mbo[10];
924 size_t ret;
925
926 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
927 reason);
928 if (ret) {
929 if (wpabuf_resize(&buf, ret) < 0) {
930 wpabuf_free(buf);
931 wpa_printf(MSG_DEBUG,
932 "WNM: Failed to allocate memory for MBO IE");
933 return -1;
934 }
935
936 wpabuf_put_data(buf, mbo, ret);
937 }
938 }
939 #endif /* CONFIG_MBO */
940
941 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
942 wpa_s->own_addr, wpa_s->bssid,
943 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
944 if (res < 0) {
945 wpa_printf(MSG_DEBUG,
946 "WNM: Failed to send BSS Transition Management Response");
947 }
948
949 wpabuf_free(buf);
950
951 return res;
952 }
953
954
wnm_bss_tm_connect(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,int after_new_scan)955 static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
956 struct wpa_bss *bss, struct wpa_ssid *ssid,
957 int after_new_scan)
958 {
959 struct wpa_radio_work *already_connecting;
960
961 wpa_dbg(wpa_s, MSG_DEBUG,
962 "WNM: Transition to BSS " MACSTR
963 " based on BSS Transition Management Request (old BSSID "
964 MACSTR " after_new_scan=%d)",
965 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
966
967 /* Send the BSS Management Response - Accept */
968 if (wpa_s->wnm_reply) {
969 wpa_s->wnm_target_bss = bss;
970 wpa_printf(MSG_DEBUG,
971 "WNM: Sending successful BSS Transition Management Response");
972
973 /* This function will be called again from the TX handler to
974 * start the actual reassociation after this response has been
975 * delivered to the current AP. */
976 if (wnm_send_bss_transition_mgmt_resp(
977 wpa_s, WNM_BSS_TM_ACCEPT,
978 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
979 bss->bssid) >= 0)
980 return;
981 }
982
983 if (bss == wpa_s->current_bss) {
984 wpa_printf(MSG_DEBUG,
985 "WNM: Already associated with the preferred candidate");
986 wnm_btm_reset(wpa_s);
987 return;
988 }
989
990 already_connecting = radio_work_pending(wpa_s, "sme-connect");
991 wpa_s->reassociate = 1;
992 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
993 wpa_supplicant_connect(wpa_s, bss, ssid);
994
995 /*
996 * Indicate that a BSS transition is in progress so scan results that
997 * come in before the 'sme-connect' radio work gets executed do not
998 * override the original connection attempt.
999 */
1000 if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
1001 wpa_s->bss_trans_mgmt_in_progress = true;
1002 }
1003
1004
wnm_scan_process(struct wpa_supplicant * wpa_s,bool pre_scan_check)1005 int wnm_scan_process(struct wpa_supplicant *wpa_s, bool pre_scan_check)
1006 {
1007 struct wpa_bss *bss, *current_bss = wpa_s->current_bss;
1008 struct wpa_ssid *ssid = wpa_s->current_ssid;
1009 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1010 enum mbo_transition_reject_reason reason =
1011 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
1012 struct wpa_ssid *selected_ssid = NULL;
1013
1014 if (!ssid || !wpa_s->wnm_dialog_token)
1015 return 0;
1016
1017 wpa_dbg(wpa_s, MSG_DEBUG,
1018 "WNM: Process scan results for BSS Transition Management");
1019 if (!pre_scan_check &&
1020 os_reltime_initialized(&wpa_s->wnm_cand_valid_until) &&
1021 os_reltime_before(&wpa_s->wnm_cand_valid_until,
1022 &wpa_s->scan_trigger_time)) {
1023 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
1024 goto send_bss_resp_fail;
1025 }
1026
1027 if (!pre_scan_check && !wpa_s->wnm_transition_scan)
1028 return 0;
1029
1030 wpa_s->wnm_transition_scan = false;
1031
1032 /* Fetch MBO transition candidate rejection information from driver */
1033 fetch_drv_mbo_candidate_info(wpa_s, &reason);
1034
1035 /* Compare the Neighbor Report and scan results */
1036 bss = wpa_supplicant_select_bss(wpa_s, ssid, &selected_ssid, 1);
1037 #ifdef CONFIG_MBO
1038 if (!bss && wpa_s->wnm_mbo_trans_reason_present &&
1039 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT)) {
1040 int i;
1041 bool changed = false;
1042
1043 /*
1044 * We didn't find any candidate, the driver had a say about
1045 * which targets to reject and disassociation is immiment.
1046 *
1047 * We should still try to roam, so retry after ignoring the
1048 * driver reject for any BSS that has an RSSI better than
1049 * disassoc_imminent_rssi_threshold.
1050 */
1051 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1052 struct neighbor_report *nei;
1053
1054 nei = &wpa_s->wnm_neighbor_report_elements[i];
1055 bss = wpa_bss_get_bssid(wpa_s, nei->bssid);
1056 if (bss && bss->level >
1057 wpa_s->conf->disassoc_imminent_rssi_threshold) {
1058 nei->drv_mbo_reject = 0;
1059 changed = true;
1060 }
1061 }
1062
1063 if (changed) {
1064 wpa_printf(MSG_DEBUG,
1065 "WNM: Ignore driver rejection due to imminent disassociation and acceptable RSSI");
1066 bss = wpa_supplicant_select_bss(wpa_s, ssid,
1067 &selected_ssid, 1);
1068 }
1069 }
1070 #endif /* CONFIG_MBO */
1071
1072 /*
1073 * If this is a pre-scan check, returning 0 will trigger a scan and
1074 * another call. In that case, reject "bad" candidates in the hope of
1075 * finding a better candidate after scanning.
1076 *
1077 * Use a simple heuristic to check whether the selection is reasonable
1078 * or a scan is a good idea. For that, we need to have found a
1079 * candidate BSS (which might be the current one), it is up-to-date,
1080 * and we don't want to immediately roam back again.
1081 */
1082 if (pre_scan_check) {
1083 struct os_reltime age;
1084
1085 if (!bss)
1086 return 0;
1087
1088 os_reltime_age(&bss->last_update, &age);
1089 if (age.sec >= 10)
1090 return 0;
1091
1092 #ifndef CONFIG_NO_ROAMING
1093 if (current_bss && bss != current_bss &&
1094 wpa_supplicant_need_to_roam_within_ess(wpa_s, bss,
1095 current_bss, false))
1096 return 0;
1097 #endif /* CONFIG_NO_ROAMING */
1098 }
1099
1100 #ifndef CONFIG_NO_ROAMING
1101 /* Apply normal roaming rules if we can stay with the current BSS */
1102 if (current_bss && bss != current_bss &&
1103 wpa_scan_res_match(wpa_s, 0, current_bss, wpa_s->current_ssid,
1104 1, 0, false) &&
1105 !wpa_supplicant_need_to_roam_within_ess(wpa_s, current_bss, bss,
1106 true))
1107 bss = current_bss;
1108 #endif /* CONFIG_NO_ROAMING */
1109
1110 if (!bss) {
1111 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1112 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1113 goto send_bss_resp_fail;
1114 }
1115
1116 wpa_printf(MSG_DEBUG,
1117 "WNM: Found an acceptable preferred transition candidate BSS "
1118 MACSTR " (RSSI %d, tput: %d bss-tput: %d)",
1119 MAC2STR(bss->bssid), bss->level, bss->est_throughput,
1120 current_bss ? (int) current_bss->est_throughput : -1);
1121
1122 /* Associate to the network */
1123 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
1124 return 1;
1125
1126 send_bss_resp_fail:
1127 if (wpa_s->wnm_reply) {
1128 /* If disassoc imminent is set, we must not reject */
1129 if (wpa_s->wnm_mode &
1130 (WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
1131 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)) {
1132 wpa_printf(MSG_DEBUG,
1133 "WNM: Accept BTM request because disassociation imminent bit is set");
1134 status = WNM_BSS_TM_ACCEPT;
1135 }
1136
1137 wnm_send_bss_transition_mgmt_resp(wpa_s, status, reason,
1138 0, NULL);
1139 }
1140
1141 wnm_btm_reset(wpa_s);
1142
1143 return 0;
1144 }
1145
1146
cand_pref_compar(const void * a,const void * b)1147 static int cand_pref_compar(const void *a, const void *b)
1148 {
1149 const struct neighbor_report *aa = a;
1150 const struct neighbor_report *bb = b;
1151
1152 if (aa->disassoc_imminent && !bb->disassoc_imminent)
1153 return 1;
1154 if (bb->disassoc_imminent && !aa->disassoc_imminent)
1155 return -1;
1156
1157 if (!aa->preference_present && !bb->preference_present)
1158 return 0;
1159 if (!aa->preference_present)
1160 return 1;
1161 if (!bb->preference_present)
1162 return -1;
1163 if (bb->preference > aa->preference)
1164 return 1;
1165 if (bb->preference < aa->preference)
1166 return -1;
1167 return 0;
1168 }
1169
1170
wnm_sort_cand_list(struct wpa_supplicant * wpa_s)1171 static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1172 {
1173 if (!wpa_s->wnm_neighbor_report_elements)
1174 return;
1175 qsort(wpa_s->wnm_neighbor_report_elements,
1176 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1177 cand_pref_compar);
1178 }
1179
1180
wnm_dump_cand_list(struct wpa_supplicant * wpa_s)1181 static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1182 {
1183 unsigned int i;
1184
1185 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1186 if (!wpa_s->wnm_neighbor_report_elements)
1187 return;
1188 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1189 struct neighbor_report *nei;
1190
1191 nei = &wpa_s->wnm_neighbor_report_elements[i];
1192 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1193 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1194 i, MAC2STR(nei->bssid), nei->bssid_info,
1195 nei->regulatory_class,
1196 nei->channel_number, nei->phy_type,
1197 nei->preference_present ? nei->preference : -1,
1198 nei->freq);
1199 }
1200 }
1201
1202
chan_supported(struct wpa_supplicant * wpa_s,int freq)1203 static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1204 {
1205 unsigned int i;
1206
1207 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1208 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1209 int j;
1210
1211 for (j = 0; j < mode->num_channels; j++) {
1212 struct hostapd_channel_data *chan;
1213
1214 chan = &mode->channels[j];
1215 if (chan->freq == freq &&
1216 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1217 return 1;
1218 }
1219 }
1220
1221 return 0;
1222 }
1223
1224
wnm_set_scan_freqs(struct wpa_supplicant * wpa_s)1225 static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1226 {
1227 int *freqs;
1228 int num_freqs = 0;
1229 unsigned int i;
1230
1231 if (!wpa_s->wnm_neighbor_report_elements)
1232 return;
1233
1234 if (wpa_s->hw.modes == NULL)
1235 return;
1236
1237 os_free(wpa_s->next_scan_freqs);
1238 wpa_s->next_scan_freqs = NULL;
1239
1240 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1241 if (freqs == NULL)
1242 return;
1243
1244 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1245 struct neighbor_report *nei;
1246
1247 nei = &wpa_s->wnm_neighbor_report_elements[i];
1248
1249 if (nei->preference_present && nei->preference == 0)
1250 continue;
1251
1252 if (nei->freq <= 0) {
1253 wpa_printf(MSG_DEBUG,
1254 "WNM: Unknown neighbor operating frequency for "
1255 MACSTR " - scan all channels",
1256 MAC2STR(nei->bssid));
1257 os_free(freqs);
1258 return;
1259 }
1260 if (chan_supported(wpa_s, nei->freq))
1261 add_freq(freqs, &num_freqs, nei->freq);
1262 }
1263
1264 if (num_freqs == 0) {
1265 os_free(freqs);
1266 return;
1267 }
1268
1269 wpa_printf(MSG_DEBUG,
1270 "WNM: Scan %d frequencies based on transition candidate list",
1271 num_freqs);
1272 wpa_s->next_scan_freqs = freqs;
1273 }
1274
1275
wnm_parse_candidate_list(struct wpa_supplicant * wpa_s,const u8 * pos,const u8 * end,int * num_valid_candidates)1276 static int wnm_parse_candidate_list(struct wpa_supplicant *wpa_s,
1277 const u8 *pos, const u8 *end,
1278 int *num_valid_candidates)
1279 {
1280 *num_valid_candidates = 0;
1281
1282 while (end - pos >= 2 &&
1283 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT) {
1284 u8 tag = *pos++;
1285 u8 len = *pos++;
1286
1287 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u", tag);
1288 if (len > end - pos) {
1289 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1290 return -1;
1291 }
1292 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1293 struct neighbor_report *rep;
1294
1295 if (!wpa_s->wnm_num_neighbor_report) {
1296 wpa_s->wnm_neighbor_report_elements = os_calloc(
1297 WNM_MAX_NEIGHBOR_REPORT,
1298 sizeof(struct neighbor_report));
1299 if (!wpa_s->wnm_neighbor_report_elements)
1300 return -1;
1301 }
1302
1303 rep = &wpa_s->wnm_neighbor_report_elements[
1304 wpa_s->wnm_num_neighbor_report];
1305 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
1306 if ((wpa_s->wnm_mode &
1307 WNM_BSS_TM_REQ_DISASSOC_IMMINENT) &&
1308 ether_addr_equal(rep->bssid, wpa_s->bssid))
1309 rep->disassoc_imminent = 1;
1310
1311 if (rep->preference_present && rep->preference)
1312 *num_valid_candidates += 1;
1313
1314 wpa_s->wnm_num_neighbor_report++;
1315 }
1316
1317 pos += len;
1318 }
1319
1320 return 0;
1321 }
1322
1323
ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant * wpa_s,const u8 * pos,const u8 * end,int reply)1324 static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1325 const u8 *pos, const u8 *end,
1326 int reply)
1327 {
1328 unsigned int beacon_int;
1329 u8 valid_int;
1330 #ifdef CONFIG_MBO
1331 const u8 *vendor;
1332 #endif /* CONFIG_MBO */
1333 bool disassoc_imminent;
1334 int num_valid_candidates;
1335
1336 if (wpa_s->disable_mbo_oce || wpa_s->conf->disable_btm)
1337 return;
1338
1339 if (end - pos < 5)
1340 return;
1341
1342 #ifdef CONFIG_MBO
1343 wpa_s->wnm_mbo_cell_pref_present = 0;
1344 wpa_s->wnm_mbo_cell_preference = 0;
1345 wpa_s->wnm_mbo_assoc_retry_delay_present = 0;
1346 wpa_s->wnm_mbo_assoc_retry_delay_sec = 0;
1347 #endif /* CONFIG_MBO */
1348
1349 if (wpa_s->current_bss)
1350 beacon_int = wpa_s->current_bss->beacon_int;
1351 else
1352 beacon_int = 100; /* best guess */
1353
1354 wnm_btm_reset(wpa_s);
1355
1356 wpa_s->wnm_dialog_token = pos[0];
1357 wpa_s->wnm_mode = pos[1];
1358 wpa_s->wnm_disassoc_timer = WPA_GET_LE16(pos + 2);
1359 wpa_s->wnm_link_removal = false;
1360 valid_int = pos[4];
1361 wpa_s->wnm_reply = reply;
1362
1363 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1364 "dialog_token=%u request_mode=0x%x "
1365 "disassoc_timer=%u validity_interval=%u",
1366 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
1367 wpa_s->wnm_disassoc_timer, valid_int);
1368
1369 if (!wpa_s->wnm_dialog_token) {
1370 wpa_printf(MSG_DEBUG, "WNM: Invalid dialog token");
1371 goto reset;
1372 }
1373
1374 #if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1375 if (wpa_s->reject_btm_req_reason) {
1376 wpa_printf(MSG_INFO,
1377 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1378 wpa_s->reject_btm_req_reason);
1379 wnm_send_bss_transition_mgmt_resp(
1380 wpa_s, wpa_s->reject_btm_req_reason,
1381 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
1382 goto reset;
1383 }
1384 #endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1385
1386 pos += 5;
1387
1388 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
1389 if (end - pos < 12) {
1390 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1391 goto reset;
1392 }
1393 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
1394 pos += 12; /* BSS Termination Duration */
1395 }
1396
1397 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
1398 char url[256];
1399 u8 url_len;
1400
1401 if (end - pos < 1) {
1402 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1403 "Management Request (URL)");
1404 goto reset;
1405 }
1406 url_len = *pos++;
1407 if (url_len > end - pos) {
1408 wpa_printf(MSG_DEBUG,
1409 "WNM: Invalid BSS Transition Management Request (URL truncated)");
1410 goto reset;
1411 }
1412 os_memcpy(url, pos, url_len);
1413 url[url_len] = '\0';
1414 pos += url_len;
1415
1416 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1417 wpa_sm_pmf_enabled(wpa_s->wpa),
1418 wpa_s->wnm_disassoc_timer * beacon_int * 128 / 125,
1419 url);
1420 }
1421
1422 #ifdef CONFIG_MBO
1423 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1424 if (vendor) {
1425 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
1426 }
1427 #endif /* CONFIG_MBO */
1428 if (wpa_s->conf->btm_offload) {
1429 wpa_printf(MSG_INFO,
1430 "WNM: BTM offload enabled. Notify status and return");
1431 wpa_s->bss_tm_status = WNM_BSS_TM_ACCEPT;
1432 wpas_notify_bss_tm_status(wpa_s);
1433 return;
1434 }
1435
1436 disassoc_imminent = wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1437
1438 /*
1439 * Based on IEEE P802.11be/D5.0, when a station is a non-AP MLD with
1440 * more than one affiliated link, the Link Removal Imminent field is
1441 * set to 1, and the BSS Termination Included field is set to 1, only
1442 * one of the links is removed and the other links remain associated.
1443 * Ignore the Disassociation Imminent field in such a case.
1444 *
1445 * TODO: We should check if the AP has more than one link.
1446 * TODO: We should pass the RX link and use that
1447 */
1448 if (disassoc_imminent && wpa_s->valid_links &&
1449 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_LINK_REMOVAL_IMMINENT) &&
1450 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED)) {
1451 /* If we still have a link, then just accept the request */
1452 if (wpa_s->valid_links & (wpa_s->valid_links - 1)) {
1453 wpa_printf(MSG_INFO,
1454 "WNM: BTM request for a single MLO link - ignore disassociation imminent since other links remain associated");
1455 disassoc_imminent = false;
1456
1457 wnm_send_bss_transition_mgmt_resp(
1458 wpa_s, WNM_BSS_TM_ACCEPT, 0, 0, NULL);
1459
1460 goto reset;
1461 }
1462
1463 /* The last link is being removed (which must be the assoc link)
1464 */
1465 wpa_s->wnm_link_removal = true;
1466 wpa_s->wnm_disassoc_mld = false;
1467 os_memcpy(wpa_s->wnm_disassoc_addr,
1468 wpa_s->links[wpa_s->mlo_assoc_link_id].bssid,
1469 ETH_ALEN);
1470 } else if (wpa_s->valid_links) {
1471 wpa_s->wnm_disassoc_mld = true;
1472 os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->ap_mld_addr,
1473 ETH_ALEN);
1474 } else {
1475 wpa_s->wnm_disassoc_mld = false;
1476 os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->bssid, ETH_ALEN);
1477 }
1478
1479 if (disassoc_imminent)
1480 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
1481 "Disassociation Timer %u", wpa_s->wnm_disassoc_timer);
1482
1483 if (wnm_parse_candidate_list(wpa_s, pos, end,
1484 &num_valid_candidates) < 0)
1485 goto reset;
1486
1487 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
1488 if (!wpa_s->wnm_num_neighbor_report) {
1489 wpa_printf(MSG_DEBUG,
1490 "WNM: Candidate list included bit is set, but no candidates found");
1491 wnm_send_bss_transition_mgmt_resp(
1492 wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1493 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1494 NULL);
1495 goto reset;
1496 }
1497 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
1498 }
1499
1500 if (wpa_s->wnm_num_neighbor_report) {
1501 unsigned int valid_ms;
1502
1503 wnm_sort_cand_list(wpa_s);
1504 wnm_dump_cand_list(wpa_s);
1505 valid_ms = valid_int * beacon_int * 128 / 125;
1506 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1507 valid_ms);
1508 os_get_reltime(&wpa_s->wnm_cand_valid_until);
1509 os_reltime_add_ms(&wpa_s->wnm_cand_valid_until, valid_ms);
1510 } else if (!disassoc_imminent) {
1511 enum bss_trans_mgmt_status_code status;
1512
1513 /* No candidate list and disassociation is not imminent */
1514
1515 if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) ||
1516 wpa_s->wnm_link_removal)
1517 status = WNM_BSS_TM_ACCEPT;
1518 else {
1519 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1520 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1521 }
1522
1523 if (reply)
1524 wnm_send_bss_transition_mgmt_resp(
1525 wpa_s, status,
1526 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1527 NULL);
1528
1529 goto reset;
1530 }
1531
1532 /*
1533 * Try fetching the latest scan results from the kernel.
1534 * This can help in finding more up-to-date information should
1535 * the driver have done some internal scanning operations after
1536 * the last scan result update in wpa_supplicant.
1537 *
1538 * It is not a new scan, this does not update the last_scan
1539 * timestamp nor will it expire old BSSs.
1540 */
1541 wpa_supplicant_update_scan_results(wpa_s, NULL);
1542 if (wnm_scan_process(wpa_s, true) > 0)
1543 return;
1544 wpa_printf(MSG_DEBUG,
1545 "WNM: No valid match in previous scan results - try a new scan");
1546
1547 /*
1548 * If we have a fixed BSSID configured, just reject at this point.
1549 * NOTE: We could actually check if we are allowed to stay (and we do
1550 * above if we have scan results available).
1551 */
1552 if (wpa_s->current_ssid && wpa_s->current_ssid->bssid_set) {
1553 wpa_printf(MSG_DEBUG, "WNM: Fixed BSSID, rejecting request");
1554
1555 if (reply)
1556 wnm_send_bss_transition_mgmt_resp(
1557 wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1558 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1559 NULL);
1560
1561 goto reset;
1562 }
1563
1564 wnm_set_scan_freqs(wpa_s);
1565 if (num_valid_candidates == 1) {
1566 /* Any invalid candidate was sorted to the end */
1567 os_memcpy(wpa_s->next_scan_bssid,
1568 wpa_s->wnm_neighbor_report_elements[0].bssid,
1569 ETH_ALEN);
1570 wpa_printf(MSG_DEBUG,
1571 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1572 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1573 }
1574 wpa_s->wnm_transition_scan = true;
1575 wpa_supplicant_req_scan(wpa_s, 0, 0);
1576
1577 /* Continue from scan handler */
1578 return;
1579
1580 reset:
1581 wnm_btm_reset(wpa_s);
1582 }
1583
1584
wnm_btm_resp_tx_status(struct wpa_supplicant * wpa_s,const u8 * data,size_t data_len)1585 int wnm_btm_resp_tx_status(struct wpa_supplicant *wpa_s, const u8 *data,
1586 size_t data_len)
1587 {
1588 const struct ieee80211_mgmt *frame =
1589 (const struct ieee80211_mgmt *) data;
1590
1591 if (data_len <
1592 IEEE80211_HDRLEN + sizeof(frame->u.action.u.bss_tm_resp) ||
1593 frame->u.action.category != WLAN_ACTION_WNM ||
1594 frame->u.action.u.bss_tm_resp.action != WNM_BSS_TRANS_MGMT_RESP ||
1595 frame->u.action.u.bss_tm_resp.status_code != WNM_BSS_TM_ACCEPT)
1596 return -1;
1597
1598 /*
1599 * If disassoc imminent bit was set in the request, the response may
1600 * indicate accept even if no candidate was found, so bail out here.
1601 */
1602 if (!wpa_s->wnm_target_bss) {
1603 wpa_printf(MSG_DEBUG, "WNM: Target BSS is not set");
1604 return 0;
1605 }
1606
1607 if (!wpa_s->current_ssid)
1608 return 0;
1609
1610 wnm_bss_tm_connect(wpa_s, wpa_s->wnm_target_bss, wpa_s->current_ssid,
1611 0);
1612
1613 wpa_s->wnm_target_bss = NULL;
1614 return 0;
1615 }
1616
1617
1618 #define BTM_QUERY_MIN_SIZE 4
1619
wnm_send_bss_transition_mgmt_query(struct wpa_supplicant * wpa_s,u8 query_reason,const char * btm_candidates,int cand_list)1620 int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
1621 u8 query_reason,
1622 const char *btm_candidates,
1623 int cand_list)
1624 {
1625 struct wpabuf *buf;
1626 int ret;
1627
1628 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
1629 MACSTR " query_reason=%u%s",
1630 MAC2STR(wpa_s->bssid), query_reason,
1631 cand_list ? " candidate list" : "");
1632
1633 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1634 if (!buf)
1635 return -1;
1636
1637 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1638 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1639 wpabuf_put_u8(buf, 1);
1640 wpabuf_put_u8(buf, query_reason);
1641
1642 if (cand_list)
1643 wnm_add_cand_list(wpa_s, &buf);
1644
1645 if (btm_candidates) {
1646 const size_t max_len = 1000;
1647
1648 ret = wpabuf_resize(&buf, max_len);
1649 if (ret < 0) {
1650 wpabuf_free(buf);
1651 return ret;
1652 }
1653
1654 ret = ieee802_11_parse_candidate_list(btm_candidates,
1655 wpabuf_put(buf, 0),
1656 max_len);
1657 if (ret < 0) {
1658 wpabuf_free(buf);
1659 return ret;
1660 }
1661
1662 wpabuf_put(buf, ret);
1663 }
1664
1665 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1666 wpa_s->own_addr, wpa_s->bssid,
1667 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1668
1669 wpabuf_free(buf);
1670 return ret;
1671 }
1672
1673
ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * data,int len)1674 static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1675 const u8 *sa, const u8 *data,
1676 int len)
1677 {
1678 const u8 *pos, *end, *next;
1679 u8 ie, ie_len;
1680
1681 pos = data;
1682 end = data + len;
1683
1684 while (end - pos > 1) {
1685 ie = *pos++;
1686 ie_len = *pos++;
1687 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1688 ie, ie_len);
1689 if (ie_len > end - pos) {
1690 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1691 "subelement");
1692 break;
1693 }
1694 next = pos + ie_len;
1695 if (ie_len < 4) {
1696 pos = next;
1697 continue;
1698 }
1699 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1700 WPA_GET_BE24(pos), pos[3]);
1701
1702 #ifdef CONFIG_HS20
1703 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1704 WPA_GET_BE24(pos) == OUI_WFA &&
1705 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1706 const u8 *ie_end;
1707 u8 url_len;
1708 char *url;
1709 u8 code;
1710 u16 reauth_delay;
1711
1712 ie_end = pos + ie_len;
1713 pos += 4;
1714 code = *pos++;
1715 reauth_delay = WPA_GET_LE16(pos);
1716 pos += 2;
1717 url_len = *pos++;
1718 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1719 "Imminent - Reason Code %u "
1720 "Re-Auth Delay %u URL Length %u",
1721 code, reauth_delay, url_len);
1722 if (url_len > ie_end - pos)
1723 break;
1724 url = os_malloc(url_len + 1);
1725 if (url == NULL)
1726 break;
1727 os_memcpy(url, pos, url_len);
1728 url[url_len] = '\0';
1729 hs20_rx_deauth_imminent_notice(wpa_s, code,
1730 reauth_delay, url);
1731 os_free(url);
1732 pos = next;
1733 continue;
1734 }
1735
1736 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1737 WPA_GET_BE24(pos) == OUI_WFA &&
1738 pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
1739 const u8 *ie_end;
1740 u8 url_len;
1741 char *url;
1742
1743 ie_end = pos + ie_len;
1744 pos += 4;
1745 url_len = *pos++;
1746 wpa_printf(MSG_DEBUG,
1747 "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
1748 url_len);
1749 if (url_len > ie_end - pos)
1750 break;
1751 url = os_malloc(url_len + 1);
1752 if (!url)
1753 break;
1754 os_memcpy(url, pos, url_len);
1755 url[url_len] = '\0';
1756 hs20_rx_t_c_acceptance(wpa_s, url);
1757 os_free(url);
1758 pos = next;
1759 continue;
1760 }
1761 #endif /* CONFIG_HS20 */
1762
1763 pos = next;
1764 }
1765 }
1766
1767
ieee802_11_rx_wnm_notif_req(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * frm,int len)1768 static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1769 const u8 *sa, const u8 *frm, int len)
1770 {
1771 const u8 *pos, *end;
1772 u8 dialog_token, type;
1773
1774 /* Dialog Token [1] | Type [1] | Subelements */
1775
1776 if (len < 2 || sa == NULL)
1777 return;
1778 end = frm + len;
1779 pos = frm;
1780 dialog_token = *pos++;
1781 type = *pos++;
1782
1783 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1784 "(dialog_token %u type %u sa " MACSTR ")",
1785 dialog_token, type, MAC2STR(sa));
1786 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1787 pos, end - pos);
1788
1789 if (wpa_s->wpa_state != WPA_COMPLETED ||
1790 (!ether_addr_equal(sa, wpa_s->bssid) &&
1791 (!wpa_s->valid_links ||
1792 !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
1793 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1794 "from our AP - ignore it");
1795 return;
1796 }
1797
1798 switch (type) {
1799 case 1:
1800 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1801 break;
1802 default:
1803 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1804 "WNM-Notification type %u", type);
1805 break;
1806 }
1807 }
1808
1809
ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * frm,int len)1810 static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
1811 const u8 *sa, const u8 *frm,
1812 int len)
1813 {
1814 u8 dialog_token, req_info, auto_report, timeout;
1815
1816 if (!wpa_s->conf->coloc_intf_reporting)
1817 return;
1818
1819 /* Dialog Token [1] | Request Info [1] */
1820
1821 if (len < 2)
1822 return;
1823 dialog_token = frm[0];
1824 req_info = frm[1];
1825 auto_report = req_info & 0x03;
1826 timeout = req_info >> 2;
1827
1828 wpa_dbg(wpa_s, MSG_DEBUG,
1829 "WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
1830 dialog_token, auto_report, timeout, MAC2STR(sa));
1831
1832 if (dialog_token == 0)
1833 return; /* only nonzero values are used for request */
1834
1835 if (wpa_s->wpa_state != WPA_COMPLETED ||
1836 (!ether_addr_equal(sa, wpa_s->bssid) &&
1837 (!wpa_s->valid_links ||
1838 !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
1839 wpa_dbg(wpa_s, MSG_DEBUG,
1840 "WNM: Collocated Interference Request frame not from current AP - ignore it");
1841 return;
1842 }
1843
1844 wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
1845 dialog_token, auto_report, timeout);
1846 wpa_s->coloc_intf_dialog_token = dialog_token;
1847 wpa_s->coloc_intf_auto_report = auto_report;
1848 wpa_s->coloc_intf_timeout = timeout;
1849 }
1850
1851
ieee802_11_rx_wnm_action(struct wpa_supplicant * wpa_s,const struct ieee80211_mgmt * mgmt,size_t len)1852 void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
1853 const struct ieee80211_mgmt *mgmt, size_t len)
1854 {
1855 const u8 *pos, *end;
1856 u8 act;
1857
1858 if (len < IEEE80211_HDRLEN + 2)
1859 return;
1860
1861 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
1862 act = *pos++;
1863 end = ((const u8 *) mgmt) + len;
1864
1865 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
1866 act, MAC2STR(mgmt->sa));
1867 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
1868 (!ether_addr_equal(mgmt->sa, wpa_s->bssid) &&
1869 (!wpa_s->valid_links ||
1870 !ether_addr_equal(mgmt->sa, wpa_s->ap_mld_addr)))) {
1871 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1872 "frame");
1873 return;
1874 }
1875
1876 switch (act) {
1877 case WNM_BSS_TRANS_MGMT_REQ:
1878 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
1879 !(mgmt->da[0] & 0x01));
1880 break;
1881 case WNM_SLEEP_MODE_RESP:
1882 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
1883 break;
1884 case WNM_NOTIFICATION_REQ:
1885 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1886 break;
1887 case WNM_COLLOCATED_INTERFERENCE_REQ:
1888 ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
1889 end - pos);
1890 break;
1891 default:
1892 wpa_printf(MSG_ERROR, "WNM: Unknown request");
1893 break;
1894 }
1895 }
1896
1897
wnm_send_coloc_intf_report(struct wpa_supplicant * wpa_s,u8 dialog_token,const struct wpabuf * elems)1898 int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
1899 const struct wpabuf *elems)
1900 {
1901 struct wpabuf *buf;
1902 int ret;
1903
1904 if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
1905 return -1;
1906
1907 wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
1908 MACSTR " (dialog token %u)",
1909 MAC2STR(wpa_s->bssid), dialog_token);
1910
1911 buf = wpabuf_alloc(3 + wpabuf_len(elems));
1912 if (!buf)
1913 return -1;
1914
1915 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1916 wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
1917 wpabuf_put_u8(buf, dialog_token);
1918 wpabuf_put_buf(buf, elems);
1919
1920 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1921 wpa_s->own_addr, wpa_s->bssid,
1922 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1923 wpabuf_free(buf);
1924 return ret;
1925 }
1926
1927
wnm_set_coloc_intf_elems(struct wpa_supplicant * wpa_s,struct wpabuf * elems)1928 void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
1929 struct wpabuf *elems)
1930 {
1931 if (elems && wpabuf_len(elems) == 0) {
1932 wpabuf_free(elems);
1933 elems = NULL;
1934 }
1935
1936 /* NOTE: The elements are not stored as they are only send out once */
1937
1938 if (wpa_s->conf->coloc_intf_reporting && elems &&
1939 wpa_s->coloc_intf_dialog_token &&
1940 (wpa_s->coloc_intf_auto_report == 1 ||
1941 wpa_s->coloc_intf_auto_report == 3)) {
1942 /* TODO: Check that there has not been less than
1943 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
1944 */
1945 wnm_send_coloc_intf_report(wpa_s,
1946 wpa_s->coloc_intf_dialog_token,
1947 elems);
1948 }
1949
1950 wpabuf_free(elems);
1951 }
1952
1953
wnm_clear_coloc_intf_reporting(struct wpa_supplicant * wpa_s)1954 void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
1955 {
1956 wpa_s->coloc_intf_dialog_token = 0;
1957 wpa_s->coloc_intf_auto_report = 0;
1958 }
1959
1960
wnm_is_bss_excluded(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)1961 bool wnm_is_bss_excluded(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1962 {
1963 int i;
1964
1965 /*
1966 * In case disassociation imminent is set, do no try to use a BSS to
1967 * which we are connected.
1968 */
1969 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
1970 if (!wpa_s->wnm_disassoc_mld) {
1971 if (ether_addr_equal(bss->bssid,
1972 wpa_s->wnm_disassoc_addr))
1973 return true;
1974 } else {
1975 if (ether_addr_equal(bss->mld_addr,
1976 wpa_s->wnm_disassoc_addr))
1977 return true;
1978 }
1979 }
1980
1981 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1982 struct neighbor_report *nei;
1983
1984 nei = &wpa_s->wnm_neighbor_report_elements[i];
1985 if (!ether_addr_equal(nei->bssid, bss->bssid))
1986 continue;
1987
1988 if (nei->preference_present && nei->preference == 0)
1989 return true;
1990
1991 #ifdef CONFIG_MBO
1992 if (nei->drv_mbo_reject)
1993 return true;
1994 #endif /* CONFIG_MBO */
1995
1996 break;
1997 }
1998
1999 /* If the abridged bit is set, the BSS must be a known neighbor. */
2000 if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ABRIDGED) &&
2001 wpa_s->wnm_num_neighbor_report == i)
2002 return true;
2003
2004 return false;
2005 }
2006