xref: /aosp_15_r20/external/wpa_supplicant_8/wpa_supplicant/wnm_sta.c (revision 03f9172ca588f91df233974f4258bab95191f931)
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 
wnm_clear_acceptable(struct wpa_supplicant * wpa_s)617 static void wnm_clear_acceptable(struct wpa_supplicant *wpa_s)
618 {
619 	unsigned int i;
620 
621 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++)
622 		wpa_s->wnm_neighbor_report_elements[i].acceptable = 0;
623 }
624 
625 #ifdef CONFIG_MBO
626 static struct wpa_bss *
get_mbo_transition_candidate(struct wpa_supplicant * wpa_s,enum mbo_transition_reject_reason * reason)627 get_mbo_transition_candidate(struct wpa_supplicant *wpa_s,
628 			     enum mbo_transition_reject_reason *reason)
629 {
630 	struct wpa_bss *target = NULL;
631 	struct wpa_bss_trans_info params;
632 	struct wpa_bss_candidate_info *info = NULL;
633 	struct neighbor_report *nei = wpa_s->wnm_neighbor_report_elements;
634 	u8 *first_candidate_bssid = NULL, *pos;
635 	unsigned int i;
636 
637 	params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
638 	params.n_candidates = 0;
639 	params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
640 	if (!params.bssid)
641 		return NULL;
642 
643 	pos = params.bssid;
644 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; nei++, i++) {
645 		if (nei->is_first)
646 			first_candidate_bssid = nei->bssid;
647 		if (!nei->acceptable)
648 			continue;
649 		os_memcpy(pos, nei->bssid, ETH_ALEN);
650 		pos += ETH_ALEN;
651 		params.n_candidates++;
652 	}
653 
654 	if (!params.n_candidates)
655 		goto end;
656 
657 	info = wpa_drv_get_bss_trans_status(wpa_s, &params);
658 	if (!info) {
659 		/* If failed to get candidate BSS transition status from driver,
660 		 * get the first acceptable candidate from wpa_supplicant.
661 		 */
662 		target = wpa_bss_get_bssid(wpa_s, params.bssid);
663 		goto end;
664 	}
665 
666 	/* Get the first acceptable candidate from driver */
667 	for (i = 0; i < info->num; i++) {
668 		if (info->candidates[i].is_accept) {
669 			target = wpa_bss_get_bssid(wpa_s,
670 						   info->candidates[i].bssid);
671 			goto end;
672 		}
673 	}
674 
675 	/* If Disassociation Imminent is set and driver rejects all the
676 	 * candidate select first acceptable candidate which has
677 	 * rssi > disassoc_imminent_rssi_threshold
678 	 */
679 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
680 		for (i = 0; i < info->num; i++) {
681 			target = wpa_bss_get_bssid(wpa_s,
682 						   info->candidates[i].bssid);
683 			if (target &&
684 			    (target->level <
685 			     wpa_s->conf->disassoc_imminent_rssi_threshold))
686 				continue;
687 			goto end;
688 		}
689 	}
690 
691 	/* While sending BTM reject use reason code of the first candidate
692 	 * received in BTM request frame
693 	 */
694 	if (reason) {
695 		for (i = 0; i < info->num; i++) {
696 			if (first_candidate_bssid &&
697 			    ether_addr_equal(first_candidate_bssid,
698 					     info->candidates[i].bssid)) {
699 				*reason = info->candidates[i].reject_reason;
700 				break;
701 			}
702 		}
703 	}
704 
705 	target = NULL;
706 
707 end:
708 	os_free(params.bssid);
709 	if (info) {
710 		os_free(info->candidates);
711 		os_free(info);
712 	}
713 	return target;
714 }
715 #endif /* CONFIG_MBO */
716 
717 
find_better_target(struct wpa_bss * a,struct wpa_bss * b)718 static struct wpa_bss * find_better_target(struct wpa_bss *a,
719 					   struct wpa_bss *b)
720 {
721 	if (!a)
722 		return b;
723 	if (!b)
724 		return a;
725 
726 	if (a->est_throughput > b->est_throughput) {
727 		wpa_printf(MSG_DEBUG, "WNM: A is better: " MACSTR
728 			   " est-tput: %d  B: " MACSTR " est-tput: %d",
729 			   MAC2STR(a->bssid), a->est_throughput,
730 			   MAC2STR(b->bssid), b->est_throughput);
731 		return a;
732 	}
733 
734 	wpa_printf(MSG_DEBUG, "WNM: B is better, A: " MACSTR
735 		   " est-tput: %d  B: " MACSTR " est-tput: %d",
736 		   MAC2STR(a->bssid), a->est_throughput,
737 		   MAC2STR(b->bssid), b->est_throughput);
738 	return b;
739 }
740 
741 static struct wpa_bss *
compare_scan_neighbor_results(struct wpa_supplicant * wpa_s,enum mbo_transition_reject_reason * reason)742 compare_scan_neighbor_results(struct wpa_supplicant *wpa_s,
743 			      enum mbo_transition_reject_reason *reason)
744 {
745 	u8 i;
746 	struct wpa_bss *bss = wpa_s->current_bss;
747 	struct wpa_bss *target;
748 	struct wpa_bss *best_target = NULL;
749 	struct wpa_bss *bss_in_list = NULL;
750 
751 	if (!bss)
752 		return NULL;
753 
754 	wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
755 		   MAC2STR(wpa_s->bssid), bss->level);
756 
757 	wnm_clear_acceptable(wpa_s);
758 
759 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
760 		struct neighbor_report *nei;
761 
762 		nei = &wpa_s->wnm_neighbor_report_elements[i];
763 
764 		target = wpa_bss_get_bssid(wpa_s, nei->bssid);
765 		if (!target) {
766 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
767 				   " (pref %d) not found in scan results",
768 				   MAC2STR(nei->bssid),
769 				   nei->preference_present ? nei->preference :
770 				   -1);
771 			continue;
772 		}
773 
774 		/*
775 		 * TODO: Could consider allowing transition to another ESS if
776 		 * PMF was enabled for the association.
777 		 */
778 		if (!wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
779 					1, 0)) {
780 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
781 				   " (pref %d) does not match the current network profile",
782 				   MAC2STR(nei->bssid),
783 				   nei->preference_present ? nei->preference :
784 				   -1);
785 			continue;
786 		}
787 
788 		if (target->level < bss->level && target->level < -80) {
789 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
790 				   " (pref %d) does not have sufficient signal level (%d)",
791 				   MAC2STR(nei->bssid),
792 				   nei->preference_present ? nei->preference :
793 				   -1,
794 				   target->level);
795 			continue;
796 		}
797 
798 		nei->acceptable = 1;
799 
800 		best_target = find_better_target(target, best_target);
801 		if (target == bss)
802 			bss_in_list = bss;
803 	}
804 
805 #ifdef CONFIG_MBO
806 	if (wpa_s->wnm_mbo_trans_reason_present)
807 		target = get_mbo_transition_candidate(wpa_s, reason);
808 	else
809 		target = best_target;
810 #else /* CONFIG_MBO */
811 	target = best_target;
812 #endif /* CONFIG_MBO */
813 
814 	if (!target)
815 		return NULL;
816 
817 	wpa_printf(MSG_DEBUG,
818 		   "WNM: Found an acceptable preferred transition candidate BSS "
819 		   MACSTR " (RSSI %d, tput: %d  bss-tput: %d)",
820 		   MAC2STR(target->bssid), target->level,
821 		   target->est_throughput, bss->est_throughput);
822 
823 	if (!bss_in_list)
824 		return target;
825 
826 	if ((!target->est_throughput && !bss_in_list->est_throughput) ||
827 	    (target->est_throughput > bss_in_list->est_throughput &&
828 	     target->est_throughput - bss_in_list->est_throughput >
829 	     bss_in_list->est_throughput >> 4)) {
830 		/* It is more than 100/16 percent better, so switch. */
831 		return target;
832 	}
833 
834 	wpa_printf(MSG_DEBUG,
835 		   "WNM: Stay with our current BSS, not enough change in estimated throughput to switch");
836 	return bss_in_list;
837 }
838 
839 
wpa_bss_ies_eq(struct wpa_bss * a,struct wpa_bss * b,u8 eid)840 static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
841 {
842 	const u8 *ie_a, *ie_b;
843 
844 	if (!a || !b)
845 		return 0;
846 
847 	ie_a = wpa_bss_get_ie(a, eid);
848 	ie_b = wpa_bss_get_ie(b, eid);
849 
850 	if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
851 		return 0;
852 
853 	return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
854 }
855 
856 
wnm_get_bss_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)857 static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
858 {
859 	u32 info = 0;
860 
861 	info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
862 
863 	/*
864 	 * Leave the security and key scope bits unset to indicate that the
865 	 * security information is not available.
866 	 */
867 
868 	if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
869 		info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
870 	if (bss->caps & WLAN_CAPABILITY_QOS)
871 		info |= NEI_REP_BSSID_INFO_QOS;
872 	if (bss->caps & WLAN_CAPABILITY_APSD)
873 		info |= NEI_REP_BSSID_INFO_APSD;
874 	if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
875 		info |= NEI_REP_BSSID_INFO_RM;
876 	if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
877 		info |= NEI_REP_BSSID_INFO_DELAYED_BA;
878 	if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
879 		info |= NEI_REP_BSSID_INFO_IMM_BA;
880 	if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
881 		info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
882 	if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
883 		info |= NEI_REP_BSSID_INFO_HT;
884 
885 	return info;
886 }
887 
888 
wnm_add_nei_rep(struct wpabuf ** buf,const u8 * bssid,u32 bss_info,u8 op_class,u8 chan,u8 phy_type,u8 pref)889 static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
890 			   u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
891 			   u8 pref)
892 {
893 	if (wpabuf_len(*buf) + 18 >
894 	    IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
895 		wpa_printf(MSG_DEBUG,
896 			   "WNM: No room in frame for Neighbor Report element");
897 		return -1;
898 	}
899 
900 	if (wpabuf_resize(buf, 18) < 0) {
901 		wpa_printf(MSG_DEBUG,
902 			   "WNM: Failed to allocate memory for Neighbor Report element");
903 		return -1;
904 	}
905 
906 	wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
907 	/* length: 13 for basic neighbor report + 3 for preference subelement */
908 	wpabuf_put_u8(*buf, 16);
909 	wpabuf_put_data(*buf, bssid, ETH_ALEN);
910 	wpabuf_put_le32(*buf, bss_info);
911 	wpabuf_put_u8(*buf, op_class);
912 	wpabuf_put_u8(*buf, chan);
913 	wpabuf_put_u8(*buf, phy_type);
914 	wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
915 	wpabuf_put_u8(*buf, 1);
916 	wpabuf_put_u8(*buf, pref);
917 	return 0;
918 }
919 
920 
wnm_nei_rep_add_bss(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpabuf ** buf,u8 pref)921 static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
922 			       struct wpa_bss *bss, struct wpabuf **buf,
923 			       u8 pref)
924 {
925 	const u8 *ie;
926 	u8 op_class, chan;
927 	int sec_chan = 0;
928 	enum oper_chan_width vht = CONF_OPER_CHWIDTH_USE_HT;
929 	enum phy_type phy_type;
930 	u32 info;
931 	struct ieee80211_ht_operation *ht_oper = NULL;
932 	struct ieee80211_vht_operation *vht_oper = NULL;
933 
934 	ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
935 	if (ie && ie[1] >= 2) {
936 		ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
937 
938 		if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
939 			sec_chan = 1;
940 		else if (ht_oper->ht_param &
941 			 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
942 			sec_chan = -1;
943 	}
944 
945 	ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
946 	if (ie && ie[1] >= 1) {
947 		vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
948 
949 		if (vht_oper->vht_op_info_chwidth == CHANWIDTH_80MHZ ||
950 		    vht_oper->vht_op_info_chwidth == CHANWIDTH_160MHZ ||
951 		    vht_oper->vht_op_info_chwidth == CHANWIDTH_80P80MHZ)
952 			vht = vht_oper->vht_op_info_chwidth;
953 	}
954 
955 	if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
956 					  &chan) == NUM_HOSTAPD_MODES) {
957 		wpa_printf(MSG_DEBUG,
958 			   "WNM: Cannot determine operating class and channel");
959 		return -2;
960 	}
961 
962 	phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
963 					  (vht_oper != NULL));
964 	if (phy_type == PHY_TYPE_UNSPECIFIED) {
965 		wpa_printf(MSG_DEBUG,
966 			   "WNM: Cannot determine BSS phy type for Neighbor Report");
967 		return -2;
968 	}
969 
970 	info = wnm_get_bss_info(wpa_s, bss);
971 
972 	return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
973 			       pref);
974 }
975 
976 
wnm_add_cand_list(struct wpa_supplicant * wpa_s,struct wpabuf ** buf)977 static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
978 {
979 	unsigned int i, pref = 255;
980 	struct os_reltime now;
981 	struct wpa_ssid *ssid = wpa_s->current_ssid;
982 
983 	if (!ssid)
984 		return;
985 
986 	/*
987 	 * TODO: Define when scan results are no longer valid for the candidate
988 	 * list.
989 	 */
990 	os_get_reltime(&now);
991 	if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
992 		return;
993 
994 	wpa_printf(MSG_DEBUG,
995 		   "WNM: Add candidate list to BSS Transition Management Response frame");
996 	for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
997 		struct wpa_bss *bss = wpa_s->last_scan_res[i];
998 		int res;
999 
1000 		if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
1001 			res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
1002 			if (res == -2)
1003 				continue; /* could not build entry for BSS */
1004 			if (res < 0)
1005 				break; /* no more room for candidates */
1006 			if (pref == 1)
1007 				break;
1008 		}
1009 	}
1010 
1011 	wpa_hexdump_buf(MSG_DEBUG,
1012 			"WNM: BSS Transition Management Response candidate list",
1013 			*buf);
1014 }
1015 
1016 
1017 #define BTM_RESP_MIN_SIZE	5 + ETH_ALEN
1018 
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)1019 static int wnm_send_bss_transition_mgmt_resp(
1020 	struct wpa_supplicant *wpa_s,
1021 	enum bss_trans_mgmt_status_code status,
1022 	enum mbo_transition_reject_reason reason,
1023 	u8 delay, const u8 *target_bssid)
1024 {
1025 	struct wpabuf *buf;
1026 	int res;
1027 
1028 	wpa_s->wnm_reply = 0;
1029 
1030 	wpa_printf(MSG_DEBUG,
1031 		   "WNM: Send BSS Transition Management Response to " MACSTR
1032 		   " dialog_token=%u status=%u reason=%u delay=%d",
1033 		   MAC2STR(wpa_s->bssid), wpa_s->wnm_dialog_token, status,
1034 		   reason, delay);
1035 	if (!wpa_s->current_bss) {
1036 		wpa_printf(MSG_DEBUG,
1037 			   "WNM: Current BSS not known - drop response");
1038 		return -1;
1039 	}
1040 
1041 	buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
1042 	if (!buf) {
1043 		wpa_printf(MSG_DEBUG,
1044 			   "WNM: Failed to allocate memory for BTM response");
1045 		return -1;
1046 	}
1047 
1048 	wpa_s->bss_tm_status = status;
1049 	wpas_notify_bss_tm_status(wpa_s);
1050 
1051 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1052 	wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
1053 	wpabuf_put_u8(buf, wpa_s->wnm_dialog_token);
1054 	wpabuf_put_u8(buf, status);
1055 	wpabuf_put_u8(buf, delay);
1056 	if (target_bssid) {
1057 		wpabuf_put_data(buf, target_bssid, ETH_ALEN);
1058 	} else if (status == WNM_BSS_TM_ACCEPT) {
1059 		/*
1060 		 * P802.11-REVmc clarifies that the Target BSSID field is always
1061 		 * present when status code is zero, so use a fake value here if
1062 		 * no BSSID is yet known.
1063 		 */
1064 		wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
1065 	}
1066 
1067 	if (status == WNM_BSS_TM_ACCEPT && target_bssid)
1068 		wnm_add_cand_list(wpa_s, &buf);
1069 
1070 #ifdef CONFIG_MBO
1071 	if (status != WNM_BSS_TM_ACCEPT &&
1072 	    wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
1073 		u8 mbo[10];
1074 		size_t ret;
1075 
1076 		ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
1077 						   reason);
1078 		if (ret) {
1079 			if (wpabuf_resize(&buf, ret) < 0) {
1080 				wpabuf_free(buf);
1081 				wpa_printf(MSG_DEBUG,
1082 					   "WNM: Failed to allocate memory for MBO IE");
1083 				return -1;
1084 			}
1085 
1086 			wpabuf_put_data(buf, mbo, ret);
1087 		}
1088 	}
1089 #endif /* CONFIG_MBO */
1090 
1091 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1092 				  wpa_s->own_addr, wpa_s->bssid,
1093 				  wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1094 	if (res < 0) {
1095 		wpa_printf(MSG_DEBUG,
1096 			   "WNM: Failed to send BSS Transition Management Response");
1097 	}
1098 
1099 	wpabuf_free(buf);
1100 
1101 	return res;
1102 }
1103 
1104 
wnm_bss_tm_connect(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,int after_new_scan)1105 static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
1106 			       struct wpa_bss *bss, struct wpa_ssid *ssid,
1107 			       int after_new_scan)
1108 {
1109 	struct wpa_radio_work *already_connecting;
1110 
1111 	wpa_dbg(wpa_s, MSG_DEBUG,
1112 		"WNM: Transition to BSS " MACSTR
1113 		" based on BSS Transition Management Request (old BSSID "
1114 		MACSTR " after_new_scan=%d)",
1115 		MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
1116 
1117 	/* Send the BSS Management Response - Accept */
1118 	if (wpa_s->wnm_reply) {
1119 		wpa_s->wnm_target_bss = bss;
1120 		wpa_printf(MSG_DEBUG,
1121 			   "WNM: Sending successful BSS Transition Management Response");
1122 
1123 		/* This function will be called again from the TX handler to
1124 		 * start the actual reassociation after this response has been
1125 		 * delivered to the current AP. */
1126 		if (wnm_send_bss_transition_mgmt_resp(
1127 			    wpa_s, WNM_BSS_TM_ACCEPT,
1128 			    MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1129 			    bss->bssid) >= 0)
1130 			return;
1131 	}
1132 
1133 	if (bss == wpa_s->current_bss) {
1134 		wpa_printf(MSG_DEBUG,
1135 			   "WNM: Already associated with the preferred candidate");
1136 		wnm_btm_reset(wpa_s);
1137 		return;
1138 	}
1139 
1140 	already_connecting = radio_work_pending(wpa_s, "sme-connect");
1141 	wpa_s->reassociate = 1;
1142 	wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
1143 	wpa_supplicant_connect(wpa_s, bss, ssid);
1144 
1145 	/*
1146 	 * Indicate that a BSS transition is in progress so scan results that
1147 	 * come in before the 'sme-connect' radio work gets executed do not
1148 	 * override the original connection attempt.
1149 	 */
1150 	if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
1151 		wpa_s->bss_trans_mgmt_in_progress = true;
1152 }
1153 
1154 
wnm_scan_process(struct wpa_supplicant * wpa_s,bool pre_scan_check)1155 int wnm_scan_process(struct wpa_supplicant *wpa_s, bool pre_scan_check)
1156 {
1157 	struct wpa_bss *bss;
1158 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1159 	enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1160 	enum mbo_transition_reject_reason reason =
1161 		MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
1162 
1163 	if (!wpa_s->wnm_dialog_token)
1164 		return 0;
1165 
1166 	wpa_dbg(wpa_s, MSG_DEBUG,
1167 		"WNM: Process scan results for BSS Transition Management");
1168 	if (!pre_scan_check &&
1169 	    os_reltime_initialized(&wpa_s->wnm_cand_valid_until) &&
1170 	    os_reltime_before(&wpa_s->wnm_cand_valid_until,
1171 			      &wpa_s->scan_trigger_time)) {
1172 		wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
1173 		goto send_bss_resp_fail;
1174 	}
1175 
1176 	if (!pre_scan_check && !wpa_s->wnm_transition_scan)
1177 		return 0;
1178 
1179 	wpa_s->wnm_transition_scan = false;
1180 
1181 	/* Compare the Neighbor Report and scan results */
1182 	bss = compare_scan_neighbor_results(wpa_s, &reason);
1183 
1184 	/*
1185 	 * If this is a pre-scan check, returning 0 will trigger a scan and
1186 	 * another call. In that case, reject "bad" candidates in the hope of
1187 	 * finding a better candidate after scanning.
1188 	 *
1189 	 * Use a simple heuristic to check whether the selection is reasonable
1190 	 * or a scan is a good idea. For that, we need to have found a
1191 	 * candidate BSS (which might be the current one), it is up-to-date,
1192 	 * and we don't want to immediately roam back again.
1193 	 */
1194 	if (pre_scan_check) {
1195 		struct os_reltime age;
1196 
1197 		if (!bss)
1198 			return 0;
1199 
1200 		os_reltime_age(&bss->last_update, &age);
1201 		if (age.sec >= 10)
1202 			return 0;
1203 
1204 #ifndef CONFIG_NO_ROAMING
1205 		if (wpa_s->current_bss && bss != wpa_s->current_bss &&
1206 		    wpa_supplicant_need_to_roam_within_ess(wpa_s,
1207 							   wpa_s->current_bss,
1208 							   bss))
1209 			return 0;
1210 #endif /* CONFIG_NO_ROAMING */
1211 	}
1212 
1213 	if (!bss) {
1214 		wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1215 		status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1216 		goto send_bss_resp_fail;
1217 	}
1218 
1219 	/* Associate to the network */
1220 	wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
1221 	return 1;
1222 
1223 send_bss_resp_fail:
1224 	if (wpa_s->wnm_reply) {
1225 		/* If disassoc imminent is set, we must not reject */
1226 		if (wpa_s->wnm_mode &
1227 		    (WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
1228 		     WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)) {
1229 			wpa_printf(MSG_DEBUG,
1230 				   "WNM: Accept BTM request because disassociation imminent bit is set");
1231 			status = WNM_BSS_TM_ACCEPT;
1232 		}
1233 
1234 		wnm_send_bss_transition_mgmt_resp(wpa_s, status, reason,
1235 						  0, NULL);
1236 	}
1237 
1238 	wnm_btm_reset(wpa_s);
1239 
1240 	return 0;
1241 }
1242 
1243 
cand_pref_compar(const void * a,const void * b)1244 static int cand_pref_compar(const void *a, const void *b)
1245 {
1246 	const struct neighbor_report *aa = a;
1247 	const struct neighbor_report *bb = b;
1248 
1249 	if (aa->disassoc_imminent && !bb->disassoc_imminent)
1250 		return 1;
1251 	if (bb->disassoc_imminent && !aa->disassoc_imminent)
1252 		return -1;
1253 
1254 	if (!aa->preference_present && !bb->preference_present)
1255 		return 0;
1256 	if (!aa->preference_present)
1257 		return 1;
1258 	if (!bb->preference_present)
1259 		return -1;
1260 	if (bb->preference > aa->preference)
1261 		return 1;
1262 	if (bb->preference < aa->preference)
1263 		return -1;
1264 	return 0;
1265 }
1266 
1267 
wnm_sort_cand_list(struct wpa_supplicant * wpa_s)1268 static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1269 {
1270 	if (!wpa_s->wnm_neighbor_report_elements)
1271 		return;
1272 	qsort(wpa_s->wnm_neighbor_report_elements,
1273 	      wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1274 	      cand_pref_compar);
1275 }
1276 
1277 
wnm_dump_cand_list(struct wpa_supplicant * wpa_s)1278 static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1279 {
1280 	unsigned int i;
1281 
1282 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1283 	if (!wpa_s->wnm_neighbor_report_elements)
1284 		return;
1285 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1286 		struct neighbor_report *nei;
1287 
1288 		nei = &wpa_s->wnm_neighbor_report_elements[i];
1289 		wpa_printf(MSG_DEBUG, "%u: " MACSTR
1290 			   " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1291 			   i, MAC2STR(nei->bssid), nei->bssid_info,
1292 			   nei->regulatory_class,
1293 			   nei->channel_number, nei->phy_type,
1294 			   nei->preference_present ? nei->preference : -1,
1295 			   nei->freq);
1296 	}
1297 }
1298 
1299 
chan_supported(struct wpa_supplicant * wpa_s,int freq)1300 static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1301 {
1302 	unsigned int i;
1303 
1304 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
1305 		struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1306 		int j;
1307 
1308 		for (j = 0; j < mode->num_channels; j++) {
1309 			struct hostapd_channel_data *chan;
1310 
1311 			chan = &mode->channels[j];
1312 			if (chan->freq == freq &&
1313 			    !(chan->flag & HOSTAPD_CHAN_DISABLED))
1314 				return 1;
1315 		}
1316 	}
1317 
1318 	return 0;
1319 }
1320 
1321 
wnm_set_scan_freqs(struct wpa_supplicant * wpa_s)1322 static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1323 {
1324 	int *freqs;
1325 	int num_freqs = 0;
1326 	unsigned int i;
1327 
1328 	if (!wpa_s->wnm_neighbor_report_elements)
1329 		return;
1330 
1331 	if (wpa_s->hw.modes == NULL)
1332 		return;
1333 
1334 	os_free(wpa_s->next_scan_freqs);
1335 	wpa_s->next_scan_freqs = NULL;
1336 
1337 	freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1338 	if (freqs == NULL)
1339 		return;
1340 
1341 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1342 		struct neighbor_report *nei;
1343 
1344 		nei = &wpa_s->wnm_neighbor_report_elements[i];
1345 
1346 		if (nei->preference_present && nei->preference == 0)
1347 			continue;
1348 
1349 		if (nei->freq <= 0) {
1350 			wpa_printf(MSG_DEBUG,
1351 				   "WNM: Unknown neighbor operating frequency for "
1352 				   MACSTR " - scan all channels",
1353 				   MAC2STR(nei->bssid));
1354 			os_free(freqs);
1355 			return;
1356 		}
1357 		if (chan_supported(wpa_s, nei->freq))
1358 			add_freq(freqs, &num_freqs, nei->freq);
1359 	}
1360 
1361 	if (num_freqs == 0) {
1362 		os_free(freqs);
1363 		return;
1364 	}
1365 
1366 	wpa_printf(MSG_DEBUG,
1367 		   "WNM: Scan %d frequencies based on transition candidate list",
1368 		   num_freqs);
1369 	wpa_s->next_scan_freqs = freqs;
1370 }
1371 
1372 
wnm_parse_candidate_list(struct wpa_supplicant * wpa_s,const u8 * pos,const u8 * end,int * num_valid_candidates)1373 static int wnm_parse_candidate_list(struct wpa_supplicant *wpa_s,
1374 				    const u8 *pos, const u8 *end,
1375 				    int *num_valid_candidates)
1376 {
1377 	*num_valid_candidates = 0;
1378 
1379 	while (end - pos >= 2 &&
1380 	       wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT) {
1381 		u8 tag = *pos++;
1382 		u8 len = *pos++;
1383 
1384 		wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u", tag);
1385 		if (len > end - pos) {
1386 			wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1387 			return -1;
1388 		}
1389 		if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1390 			struct neighbor_report *rep;
1391 
1392 			if (!wpa_s->wnm_num_neighbor_report) {
1393 				wpa_s->wnm_neighbor_report_elements = os_calloc(
1394 					WNM_MAX_NEIGHBOR_REPORT,
1395 					sizeof(struct neighbor_report));
1396 				if (!wpa_s->wnm_neighbor_report_elements)
1397 					return -1;
1398 			}
1399 
1400 			rep = &wpa_s->wnm_neighbor_report_elements[
1401 				wpa_s->wnm_num_neighbor_report];
1402 			wnm_parse_neighbor_report(wpa_s, pos, len, rep);
1403 			if ((wpa_s->wnm_mode &
1404 			     WNM_BSS_TM_REQ_DISASSOC_IMMINENT) &&
1405 			    ether_addr_equal(rep->bssid, wpa_s->bssid))
1406 				rep->disassoc_imminent = 1;
1407 
1408 			if (rep->preference_present && rep->preference)
1409 				*num_valid_candidates += 1;
1410 
1411 			wpa_s->wnm_num_neighbor_report++;
1412 #ifdef CONFIG_MBO
1413 			if (wpa_s->wnm_mbo_trans_reason_present &&
1414 			    wpa_s->wnm_num_neighbor_report == 1) {
1415 				rep->is_first = 1;
1416 				wpa_printf(MSG_DEBUG,
1417 					   "WNM: First transition candidate is "
1418 					   MACSTR, MAC2STR(rep->bssid));
1419 			}
1420 #endif /* CONFIG_MBO */
1421 		}
1422 
1423 		pos += len;
1424 	}
1425 
1426 	return 0;
1427 }
1428 
1429 
ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant * wpa_s,const u8 * pos,const u8 * end,int reply)1430 static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1431 					     const u8 *pos, const u8 *end,
1432 					     int reply)
1433 {
1434 	unsigned int beacon_int;
1435 	u8 valid_int;
1436 #ifdef CONFIG_MBO
1437 	const u8 *vendor;
1438 #endif /* CONFIG_MBO */
1439 	bool disassoc_imminent;
1440 	int num_valid_candidates;
1441 
1442 	if (wpa_s->disable_mbo_oce || wpa_s->conf->disable_btm)
1443 		return;
1444 
1445 	if (end - pos < 5)
1446 		return;
1447 
1448 #ifdef CONFIG_MBO
1449 	wpa_s->wnm_mbo_cell_pref_present = 0;
1450 	wpa_s->wnm_mbo_cell_preference = 0;
1451 	wpa_s->wnm_mbo_assoc_retry_delay_present = 0;
1452 	wpa_s->wnm_mbo_assoc_retry_delay_sec = 0;
1453 #endif /* CONFIG_MBO */
1454 
1455 	if (wpa_s->current_bss)
1456 		beacon_int = wpa_s->current_bss->beacon_int;
1457 	else
1458 		beacon_int = 100; /* best guess */
1459 
1460 	wnm_btm_reset(wpa_s);
1461 
1462 	wpa_s->wnm_dialog_token = pos[0];
1463 	wpa_s->wnm_mode = pos[1];
1464 	wpa_s->wnm_disassoc_timer = WPA_GET_LE16(pos + 2);
1465 	wpa_s->wnm_link_removal = false;
1466 	valid_int = pos[4];
1467 	wpa_s->wnm_reply = reply;
1468 
1469 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1470 		   "dialog_token=%u request_mode=0x%x "
1471 		   "disassoc_timer=%u validity_interval=%u",
1472 		   wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
1473 		   wpa_s->wnm_disassoc_timer, valid_int);
1474 
1475 	if (!wpa_s->wnm_dialog_token) {
1476 		wpa_printf(MSG_DEBUG, "WNM: Invalid dialog token");
1477 		goto reset;
1478 	}
1479 
1480 #if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1481 	if (wpa_s->reject_btm_req_reason) {
1482 		wpa_printf(MSG_INFO,
1483 			   "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1484 			   wpa_s->reject_btm_req_reason);
1485 		wnm_send_bss_transition_mgmt_resp(
1486 			wpa_s, wpa_s->reject_btm_req_reason,
1487 			MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
1488 		goto reset;
1489 	}
1490 #endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1491 
1492 	pos += 5;
1493 
1494 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
1495 		if (end - pos < 12) {
1496 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1497 			goto reset;
1498 		}
1499 		os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
1500 		pos += 12; /* BSS Termination Duration */
1501 	}
1502 
1503 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
1504 		char url[256];
1505 		u8 url_len;
1506 
1507 		if (end - pos < 1) {
1508 			wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1509 				   "Management Request (URL)");
1510 			goto reset;
1511 		}
1512 		url_len = *pos++;
1513 		if (url_len > end - pos) {
1514 			wpa_printf(MSG_DEBUG,
1515 				   "WNM: Invalid BSS Transition Management Request (URL truncated)");
1516 			goto reset;
1517 		}
1518 		os_memcpy(url, pos, url_len);
1519 		url[url_len] = '\0';
1520 		pos += url_len;
1521 
1522 		wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1523 			wpa_sm_pmf_enabled(wpa_s->wpa),
1524 			wpa_s->wnm_disassoc_timer * beacon_int * 128 / 125,
1525 			url);
1526 	}
1527 
1528 #ifdef CONFIG_MBO
1529 	vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1530 	if (vendor) {
1531 		wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
1532 	}
1533 #endif /* CONFIG_MBO */
1534 	if (wpa_s->conf->btm_offload) {
1535 		wpa_printf(MSG_INFO,
1536 			"WNM: BTM offload enabled. Notify status and return");
1537 		wpa_s->bss_tm_status = WNM_BSS_TM_ACCEPT;
1538 		wpas_notify_bss_tm_status(wpa_s);
1539 		return;
1540 	}
1541 
1542 	disassoc_imminent = wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1543 
1544 	/*
1545 	 * Based on IEEE P802.11be/D5.0, when a station is a non-AP MLD with
1546 	 * more than one affiliated link, the Link Removal Imminent field is
1547 	 * set to 1, and the BSS Termination Included field is set to 1, only
1548 	 * one of the links is removed and the other links remain associated.
1549 	 * Ignore the Disassociation Imminent field in such a case.
1550 	 *
1551 	 * TODO: We should check if the AP has more than one link.
1552 	 * TODO: We should pass the RX link and use that
1553 	 */
1554 	if (disassoc_imminent && wpa_s->valid_links &&
1555 	    (wpa_s->wnm_mode & WNM_BSS_TM_REQ_LINK_REMOVAL_IMMINENT) &&
1556 	    (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED)) {
1557 		/* If we still have a link, then just accept the request */
1558 		if (wpa_s->valid_links & (wpa_s->valid_links - 1)) {
1559 			wpa_printf(MSG_INFO,
1560 				   "WNM: BTM request for a single MLO link - ignore disassociation imminent since other links remain associated");
1561 			disassoc_imminent = false;
1562 
1563 			wnm_send_bss_transition_mgmt_resp(
1564 				wpa_s, WNM_BSS_TM_ACCEPT, 0, 0, NULL);
1565 
1566 			goto reset;
1567 		}
1568 
1569 		/* The last link is being removed (which must be the assoc link)
1570 		 */
1571 		wpa_s->wnm_link_removal = true;
1572 		wpa_s->wnm_disassoc_mld = false;
1573 		os_memcpy(wpa_s->wnm_disassoc_addr,
1574 			  wpa_s->links[wpa_s->mlo_assoc_link_id].bssid,
1575 			  ETH_ALEN);
1576 	} else if (wpa_s->valid_links) {
1577 		wpa_s->wnm_disassoc_mld = true;
1578 		os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->ap_mld_addr,
1579 			  ETH_ALEN);
1580 	} else {
1581 		wpa_s->wnm_disassoc_mld = false;
1582 		os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->bssid, ETH_ALEN);
1583 	}
1584 
1585 	if (disassoc_imminent)
1586 		wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
1587 			"Disassociation Timer %u", wpa_s->wnm_disassoc_timer);
1588 
1589 	if (wnm_parse_candidate_list(wpa_s, pos, end,
1590 				     &num_valid_candidates) < 0)
1591 		goto reset;
1592 
1593 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
1594 		if (!wpa_s->wnm_num_neighbor_report) {
1595 			wpa_printf(MSG_DEBUG,
1596 				   "WNM: Candidate list included bit is set, but no candidates found");
1597 			wnm_send_bss_transition_mgmt_resp(
1598 				wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1599 				MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1600 				NULL);
1601 			goto reset;
1602 		}
1603 		wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
1604 	}
1605 
1606 	if (wpa_s->wnm_num_neighbor_report) {
1607 		unsigned int valid_ms;
1608 
1609 		wnm_sort_cand_list(wpa_s);
1610 		wnm_dump_cand_list(wpa_s);
1611 		valid_ms = valid_int * beacon_int * 128 / 125;
1612 		wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1613 			   valid_ms);
1614 		os_get_reltime(&wpa_s->wnm_cand_valid_until);
1615 		os_reltime_add_ms(&wpa_s->wnm_cand_valid_until, valid_ms);
1616 	} else if (!disassoc_imminent) {
1617 		enum bss_trans_mgmt_status_code status;
1618 
1619 		/* No candidate list and disassociation is not imminent */
1620 
1621 		if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) ||
1622 		    wpa_s->wnm_link_removal)
1623 			status = WNM_BSS_TM_ACCEPT;
1624 		else {
1625 			wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1626 			status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1627 		}
1628 
1629 		if (reply)
1630 			wnm_send_bss_transition_mgmt_resp(
1631 				wpa_s, status,
1632 				MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1633 				NULL);
1634 
1635 		goto reset;
1636 	}
1637 
1638 	/*
1639 	 * Try fetching the latest scan results from the kernel.
1640 	 * This can help in finding more up-to-date information should
1641 	 * the driver have done some internal scanning operations after
1642 	 * the last scan result update in wpa_supplicant.
1643 	 *
1644 	 * It is not a new scan, this does not update the last_scan
1645 	 * timestamp nor will it expire old BSSs.
1646 	 */
1647 	wpa_supplicant_update_scan_results(wpa_s, NULL);
1648 	if (wnm_scan_process(wpa_s, true) > 0)
1649 		return;
1650 	wpa_printf(MSG_DEBUG,
1651 		   "WNM: No valid match in previous scan results - try a new scan");
1652 
1653 	/*
1654 	 * If we have a fixed BSSID configured, just reject at this point.
1655 	 * NOTE: We could actually check if we are allowed to stay (and we do
1656 	 * above if we have scan results available).
1657 	 */
1658 	if (wpa_s->current_ssid && wpa_s->current_ssid->bssid_set) {
1659 		wpa_printf(MSG_DEBUG, "WNM: Fixed BSSID, rejecting request");
1660 
1661 		if (reply)
1662 			wnm_send_bss_transition_mgmt_resp(
1663 				wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1664 				MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1665 				NULL);
1666 
1667 		goto reset;
1668 	}
1669 
1670 	wnm_set_scan_freqs(wpa_s);
1671 	if (num_valid_candidates == 1) {
1672 		/* Any invalid candidate was sorted to the end */
1673 		os_memcpy(wpa_s->next_scan_bssid,
1674 			  wpa_s->wnm_neighbor_report_elements[0].bssid,
1675 			  ETH_ALEN);
1676 		wpa_printf(MSG_DEBUG,
1677 			  "WNM: Scan only for a specific BSSID since there is only a single candidate "
1678 			  MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1679 	}
1680 	wpa_s->wnm_transition_scan = true;
1681 	wpa_supplicant_req_scan(wpa_s, 0, 0);
1682 
1683 	/* Continue from scan handler */
1684 	return;
1685 
1686 reset:
1687 	wnm_btm_reset(wpa_s);
1688 }
1689 
1690 
wnm_btm_resp_tx_status(struct wpa_supplicant * wpa_s,const u8 * data,size_t data_len)1691 int wnm_btm_resp_tx_status(struct wpa_supplicant *wpa_s, const u8 *data,
1692 			   size_t data_len)
1693 {
1694 	const struct ieee80211_mgmt *frame =
1695 		(const struct ieee80211_mgmt *) data;
1696 
1697 	if (data_len <
1698 	    IEEE80211_HDRLEN + sizeof(frame->u.action.u.bss_tm_resp) ||
1699 	    frame->u.action.category != WLAN_ACTION_WNM ||
1700 	    frame->u.action.u.bss_tm_resp.action != WNM_BSS_TRANS_MGMT_RESP ||
1701 	    frame->u.action.u.bss_tm_resp.status_code != WNM_BSS_TM_ACCEPT)
1702 		return -1;
1703 
1704 	/*
1705 	 * If disassoc imminent bit was set in the request, the response may
1706 	 * indicate accept even if no candidate was found, so bail out here.
1707 	 */
1708 	if (!wpa_s->wnm_target_bss) {
1709 		wpa_printf(MSG_DEBUG, "WNM: Target BSS is not set");
1710 		return 0;
1711 	}
1712 
1713 	if (!wpa_s->current_ssid)
1714 		return 0;
1715 
1716 	wnm_bss_tm_connect(wpa_s, wpa_s->wnm_target_bss, wpa_s->current_ssid,
1717 			   0);
1718 
1719 	wpa_s->wnm_target_bss = NULL;
1720 	return 0;
1721 }
1722 
1723 
1724 #define BTM_QUERY_MIN_SIZE	4
1725 
wnm_send_bss_transition_mgmt_query(struct wpa_supplicant * wpa_s,u8 query_reason,const char * btm_candidates,int cand_list)1726 int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
1727 				       u8 query_reason,
1728 				       const char *btm_candidates,
1729 				       int cand_list)
1730 {
1731 	struct wpabuf *buf;
1732 	int ret;
1733 
1734 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
1735 		   MACSTR " query_reason=%u%s",
1736 		   MAC2STR(wpa_s->bssid), query_reason,
1737 		   cand_list ? " candidate list" : "");
1738 
1739 	buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1740 	if (!buf)
1741 		return -1;
1742 
1743 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1744 	wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1745 	wpabuf_put_u8(buf, 1);
1746 	wpabuf_put_u8(buf, query_reason);
1747 
1748 	if (cand_list)
1749 		wnm_add_cand_list(wpa_s, &buf);
1750 
1751 	if (btm_candidates) {
1752 		const size_t max_len = 1000;
1753 
1754 		ret = wpabuf_resize(&buf, max_len);
1755 		if (ret < 0) {
1756 			wpabuf_free(buf);
1757 			return ret;
1758 		}
1759 
1760 		ret = ieee802_11_parse_candidate_list(btm_candidates,
1761 						      wpabuf_put(buf, 0),
1762 						      max_len);
1763 		if (ret < 0) {
1764 			wpabuf_free(buf);
1765 			return ret;
1766 		}
1767 
1768 		wpabuf_put(buf, ret);
1769 	}
1770 
1771 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1772 				  wpa_s->own_addr, wpa_s->bssid,
1773 				  wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1774 
1775 	wpabuf_free(buf);
1776 	return ret;
1777 }
1778 
1779 
ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * data,int len)1780 static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1781 					    const u8 *sa, const u8 *data,
1782 					    int len)
1783 {
1784 	const u8 *pos, *end, *next;
1785 	u8 ie, ie_len;
1786 
1787 	pos = data;
1788 	end = data + len;
1789 
1790 	while (end - pos > 1) {
1791 		ie = *pos++;
1792 		ie_len = *pos++;
1793 		wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1794 			   ie, ie_len);
1795 		if (ie_len > end - pos) {
1796 			wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1797 				   "subelement");
1798 			break;
1799 		}
1800 		next = pos + ie_len;
1801 		if (ie_len < 4) {
1802 			pos = next;
1803 			continue;
1804 		}
1805 		wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1806 			   WPA_GET_BE24(pos), pos[3]);
1807 
1808 #ifdef CONFIG_HS20
1809 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1810 		    WPA_GET_BE24(pos) == OUI_WFA &&
1811 		    pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1812 			/* Subscription Remediation subelement */
1813 			const u8 *ie_end;
1814 			u8 url_len;
1815 			char *url;
1816 			u8 osu_method;
1817 
1818 			wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1819 				   "subelement");
1820 			ie_end = pos + ie_len;
1821 			pos += 4;
1822 			url_len = *pos++;
1823 			if (url_len == 0) {
1824 				wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1825 				url = NULL;
1826 				osu_method = 1;
1827 			} else {
1828 				if (url_len + 1 > ie_end - pos) {
1829 					wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1830 						   url_len,
1831 						   (int) (ie_end - pos));
1832 					break;
1833 				}
1834 				url = os_malloc(url_len + 1);
1835 				if (url == NULL)
1836 					break;
1837 				os_memcpy(url, pos, url_len);
1838 				url[url_len] = '\0';
1839 				osu_method = pos[url_len];
1840 			}
1841 			hs20_rx_subscription_remediation(wpa_s, url,
1842 							 osu_method);
1843 			os_free(url);
1844 			pos = next;
1845 			continue;
1846 		}
1847 
1848 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1849 		    WPA_GET_BE24(pos) == OUI_WFA &&
1850 		    pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1851 			const u8 *ie_end;
1852 			u8 url_len;
1853 			char *url;
1854 			u8 code;
1855 			u16 reauth_delay;
1856 
1857 			ie_end = pos + ie_len;
1858 			pos += 4;
1859 			code = *pos++;
1860 			reauth_delay = WPA_GET_LE16(pos);
1861 			pos += 2;
1862 			url_len = *pos++;
1863 			wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1864 				   "Imminent - Reason Code %u   "
1865 				   "Re-Auth Delay %u  URL Length %u",
1866 				   code, reauth_delay, url_len);
1867 			if (url_len > ie_end - pos)
1868 				break;
1869 			url = os_malloc(url_len + 1);
1870 			if (url == NULL)
1871 				break;
1872 			os_memcpy(url, pos, url_len);
1873 			url[url_len] = '\0';
1874 			hs20_rx_deauth_imminent_notice(wpa_s, code,
1875 						       reauth_delay, url);
1876 			os_free(url);
1877 			pos = next;
1878 			continue;
1879 		}
1880 
1881 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1882 		    WPA_GET_BE24(pos) == OUI_WFA &&
1883 		    pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
1884 			const u8 *ie_end;
1885 			u8 url_len;
1886 			char *url;
1887 
1888 			ie_end = pos + ie_len;
1889 			pos += 4;
1890 			url_len = *pos++;
1891 			wpa_printf(MSG_DEBUG,
1892 				   "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
1893 				   url_len);
1894 			if (url_len > ie_end - pos)
1895 				break;
1896 			url = os_malloc(url_len + 1);
1897 			if (!url)
1898 				break;
1899 			os_memcpy(url, pos, url_len);
1900 			url[url_len] = '\0';
1901 			hs20_rx_t_c_acceptance(wpa_s, url);
1902 			os_free(url);
1903 			pos = next;
1904 			continue;
1905 		}
1906 #endif /* CONFIG_HS20 */
1907 
1908 		pos = next;
1909 	}
1910 }
1911 
1912 
ieee802_11_rx_wnm_notif_req(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * frm,int len)1913 static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1914 					const u8 *sa, const u8 *frm, int len)
1915 {
1916 	const u8 *pos, *end;
1917 	u8 dialog_token, type;
1918 
1919 	/* Dialog Token [1] | Type [1] | Subelements */
1920 
1921 	if (len < 2 || sa == NULL)
1922 		return;
1923 	end = frm + len;
1924 	pos = frm;
1925 	dialog_token = *pos++;
1926 	type = *pos++;
1927 
1928 	wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1929 		"(dialog_token %u type %u sa " MACSTR ")",
1930 		dialog_token, type, MAC2STR(sa));
1931 	wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1932 		    pos, end - pos);
1933 
1934 	if (wpa_s->wpa_state != WPA_COMPLETED ||
1935 	    (!ether_addr_equal(sa, wpa_s->bssid) &&
1936 	     (!wpa_s->valid_links ||
1937 	      !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
1938 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1939 			"from our AP - ignore it");
1940 		return;
1941 	}
1942 
1943 	switch (type) {
1944 	case 1:
1945 		ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1946 		break;
1947 	default:
1948 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1949 			"WNM-Notification type %u", type);
1950 		break;
1951 	}
1952 }
1953 
1954 
ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * frm,int len)1955 static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
1956 					     const u8 *sa, const u8 *frm,
1957 					     int len)
1958 {
1959 	u8 dialog_token, req_info, auto_report, timeout;
1960 
1961 	if (!wpa_s->conf->coloc_intf_reporting)
1962 		return;
1963 
1964 	/* Dialog Token [1] | Request Info [1] */
1965 
1966 	if (len < 2)
1967 		return;
1968 	dialog_token = frm[0];
1969 	req_info = frm[1];
1970 	auto_report = req_info & 0x03;
1971 	timeout = req_info >> 2;
1972 
1973 	wpa_dbg(wpa_s, MSG_DEBUG,
1974 		"WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
1975 		dialog_token, auto_report, timeout, MAC2STR(sa));
1976 
1977 	if (dialog_token == 0)
1978 		return; /* only nonzero values are used for request */
1979 
1980 	if (wpa_s->wpa_state != WPA_COMPLETED ||
1981 	    (!ether_addr_equal(sa, wpa_s->bssid) &&
1982 	     (!wpa_s->valid_links ||
1983 	      !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
1984 		wpa_dbg(wpa_s, MSG_DEBUG,
1985 			"WNM: Collocated Interference Request frame not from current AP - ignore it");
1986 		return;
1987 	}
1988 
1989 	wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
1990 		dialog_token, auto_report, timeout);
1991 	wpa_s->coloc_intf_dialog_token = dialog_token;
1992 	wpa_s->coloc_intf_auto_report = auto_report;
1993 	wpa_s->coloc_intf_timeout = timeout;
1994 }
1995 
1996 
ieee802_11_rx_wnm_action(struct wpa_supplicant * wpa_s,const struct ieee80211_mgmt * mgmt,size_t len)1997 void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
1998 			      const struct ieee80211_mgmt *mgmt, size_t len)
1999 {
2000 	const u8 *pos, *end;
2001 	u8 act;
2002 
2003 	if (len < IEEE80211_HDRLEN + 2)
2004 		return;
2005 
2006 	pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
2007 	act = *pos++;
2008 	end = ((const u8 *) mgmt) + len;
2009 
2010 	wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
2011 		   act, MAC2STR(mgmt->sa));
2012 	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
2013 	    (!ether_addr_equal(mgmt->sa, wpa_s->bssid) &&
2014 	     (!wpa_s->valid_links ||
2015 	      !ether_addr_equal(mgmt->sa, wpa_s->ap_mld_addr)))) {
2016 		wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
2017 			   "frame");
2018 		return;
2019 	}
2020 
2021 	switch (act) {
2022 	case WNM_BSS_TRANS_MGMT_REQ:
2023 		ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
2024 						 !(mgmt->da[0] & 0x01));
2025 		break;
2026 	case WNM_SLEEP_MODE_RESP:
2027 		ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
2028 		break;
2029 	case WNM_NOTIFICATION_REQ:
2030 		ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
2031 		break;
2032 	case WNM_COLLOCATED_INTERFERENCE_REQ:
2033 		ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
2034 						 end - pos);
2035 		break;
2036 	default:
2037 		wpa_printf(MSG_ERROR, "WNM: Unknown request");
2038 		break;
2039 	}
2040 }
2041 
2042 
wnm_send_coloc_intf_report(struct wpa_supplicant * wpa_s,u8 dialog_token,const struct wpabuf * elems)2043 int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
2044 			       const struct wpabuf *elems)
2045 {
2046 	struct wpabuf *buf;
2047 	int ret;
2048 
2049 	if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
2050 		return -1;
2051 
2052 	wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
2053 		   MACSTR " (dialog token %u)",
2054 		   MAC2STR(wpa_s->bssid), dialog_token);
2055 
2056 	buf = wpabuf_alloc(3 + wpabuf_len(elems));
2057 	if (!buf)
2058 		return -1;
2059 
2060 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
2061 	wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
2062 	wpabuf_put_u8(buf, dialog_token);
2063 	wpabuf_put_buf(buf, elems);
2064 
2065 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
2066 				  wpa_s->own_addr, wpa_s->bssid,
2067 				  wpabuf_head_u8(buf), wpabuf_len(buf), 0);
2068 	wpabuf_free(buf);
2069 	return ret;
2070 }
2071 
2072 
wnm_set_coloc_intf_elems(struct wpa_supplicant * wpa_s,struct wpabuf * elems)2073 void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
2074 			      struct wpabuf *elems)
2075 {
2076 	if (elems && wpabuf_len(elems) == 0) {
2077 		wpabuf_free(elems);
2078 		elems = NULL;
2079 	}
2080 
2081 	/* NOTE: The elements are not stored as they are only send out once */
2082 
2083 	if (wpa_s->conf->coloc_intf_reporting && elems &&
2084 	    wpa_s->coloc_intf_dialog_token &&
2085 	    (wpa_s->coloc_intf_auto_report == 1 ||
2086 	     wpa_s->coloc_intf_auto_report == 3)) {
2087 		/* TODO: Check that there has not been less than
2088 		 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
2089 		 */
2090 		wnm_send_coloc_intf_report(wpa_s,
2091 					   wpa_s->coloc_intf_dialog_token,
2092 					   elems);
2093 	}
2094 
2095 	wpabuf_free(elems);
2096 }
2097 
2098 
wnm_clear_coloc_intf_reporting(struct wpa_supplicant * wpa_s)2099 void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
2100 {
2101 	wpa_s->coloc_intf_dialog_token = 0;
2102 	wpa_s->coloc_intf_auto_report = 0;
2103 }
2104 
2105 
wnm_is_bss_excluded(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)2106 bool wnm_is_bss_excluded(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2107 {
2108 	int i;
2109 
2110 	/*
2111 	 * In case disassociation imminent is set, do no try to use a BSS to
2112 	 * which we are connected.
2113 	 */
2114 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
2115 		if (!wpa_s->wnm_disassoc_mld) {
2116 			if (ether_addr_equal(bss->bssid,
2117 					     wpa_s->wnm_disassoc_addr))
2118 				return true;
2119 		} else {
2120 			if (ether_addr_equal(bss->mld_addr,
2121 					     wpa_s->wnm_disassoc_addr))
2122 				return true;
2123 		}
2124 	}
2125 
2126 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
2127 		struct neighbor_report *nei;
2128 
2129 		nei = &wpa_s->wnm_neighbor_report_elements[i];
2130 		if (!ether_addr_equal(nei->bssid, bss->bssid))
2131 			continue;
2132 
2133 		if (nei->preference_present && nei->preference == 0)
2134 			return true;
2135 
2136 		break;
2137 	}
2138 
2139 	/* If the abridged bit is set, the BSS must be a known neighbor. */
2140 	if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ABRIDGED) &&
2141 	    wpa_s->wnm_num_neighbor_report == i)
2142 		return true;
2143 
2144 	return false;
2145 }
2146