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