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