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