xref: /freebsd/contrib/wpa/wpa_supplicant/rrm.c (revision c1d255d3)
185732ac8SCy Schubert /*
285732ac8SCy Schubert  * wpa_supplicant - Radio Measurements
385732ac8SCy Schubert  * Copyright (c) 2003-2016, Jouni Malinen <j@w1.fi>
485732ac8SCy Schubert  *
585732ac8SCy Schubert  * This software may be distributed under the terms of the BSD license.
685732ac8SCy Schubert  * See README for more details.
785732ac8SCy Schubert  */
885732ac8SCy Schubert 
985732ac8SCy Schubert #include "includes.h"
1085732ac8SCy Schubert 
1185732ac8SCy Schubert #include "utils/common.h"
1285732ac8SCy Schubert #include "utils/eloop.h"
1385732ac8SCy Schubert #include "common/ieee802_11_common.h"
1485732ac8SCy Schubert #include "wpa_supplicant_i.h"
1585732ac8SCy Schubert #include "driver_i.h"
1685732ac8SCy Schubert #include "bss.h"
1785732ac8SCy Schubert #include "scan.h"
1885732ac8SCy Schubert #include "p2p_supplicant.h"
1985732ac8SCy Schubert 
2085732ac8SCy Schubert 
wpas_rrm_neighbor_rep_timeout_handler(void * data,void * user_ctx)2185732ac8SCy Schubert static void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
2285732ac8SCy Schubert {
2385732ac8SCy Schubert 	struct rrm_data *rrm = data;
2485732ac8SCy Schubert 
2585732ac8SCy Schubert 	if (!rrm->notify_neighbor_rep) {
2685732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
2785732ac8SCy Schubert 			   "RRM: Unexpected neighbor report timeout");
2885732ac8SCy Schubert 		return;
2985732ac8SCy Schubert 	}
3085732ac8SCy Schubert 
3185732ac8SCy Schubert 	wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
3285732ac8SCy Schubert 	rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
3385732ac8SCy Schubert 
3485732ac8SCy Schubert 	rrm->notify_neighbor_rep = NULL;
3585732ac8SCy Schubert 	rrm->neighbor_rep_cb_ctx = NULL;
3685732ac8SCy Schubert }
3785732ac8SCy Schubert 
3885732ac8SCy Schubert 
3985732ac8SCy Schubert /*
4085732ac8SCy Schubert  * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
4185732ac8SCy Schubert  * @wpa_s: Pointer to wpa_supplicant
4285732ac8SCy Schubert  */
wpas_rrm_reset(struct wpa_supplicant * wpa_s)4385732ac8SCy Schubert void wpas_rrm_reset(struct wpa_supplicant *wpa_s)
4485732ac8SCy Schubert {
4585732ac8SCy Schubert 	wpa_s->rrm.rrm_used = 0;
4685732ac8SCy Schubert 
4785732ac8SCy Schubert 	eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
4885732ac8SCy Schubert 			     NULL);
4985732ac8SCy Schubert 	if (wpa_s->rrm.notify_neighbor_rep)
5085732ac8SCy Schubert 		wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
5185732ac8SCy Schubert 	wpa_s->rrm.next_neighbor_rep_token = 1;
5285732ac8SCy Schubert 	wpas_clear_beacon_rep_data(wpa_s);
5385732ac8SCy Schubert }
5485732ac8SCy Schubert 
5585732ac8SCy Schubert 
5685732ac8SCy Schubert /*
5785732ac8SCy Schubert  * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
5885732ac8SCy Schubert  * @wpa_s: Pointer to wpa_supplicant
5985732ac8SCy Schubert  * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
6085732ac8SCy Schubert  * @report_len: Length of neighbor report buffer
6185732ac8SCy Schubert  */
wpas_rrm_process_neighbor_rep(struct wpa_supplicant * wpa_s,const u8 * report,size_t report_len)6285732ac8SCy Schubert void wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
6385732ac8SCy Schubert 				   const u8 *report, size_t report_len)
6485732ac8SCy Schubert {
6585732ac8SCy Schubert 	struct wpabuf *neighbor_rep;
6685732ac8SCy Schubert 
6785732ac8SCy Schubert 	wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
6885732ac8SCy Schubert 	if (report_len < 1)
6985732ac8SCy Schubert 		return;
7085732ac8SCy Schubert 
7185732ac8SCy Schubert 	if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
7285732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
7385732ac8SCy Schubert 			   "RRM: Discarding neighbor report with token %d (expected %d)",
7485732ac8SCy Schubert 			   report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
7585732ac8SCy Schubert 		return;
7685732ac8SCy Schubert 	}
7785732ac8SCy Schubert 
7885732ac8SCy Schubert 	eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
7985732ac8SCy Schubert 			     NULL);
8085732ac8SCy Schubert 
8185732ac8SCy Schubert 	if (!wpa_s->rrm.notify_neighbor_rep) {
82*c1d255d3SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, "RRM: Unexpected neighbor report");
8385732ac8SCy Schubert 		return;
8485732ac8SCy Schubert 	}
8585732ac8SCy Schubert 
8685732ac8SCy Schubert 	/* skipping the first byte, which is only an id (dialog token) */
8785732ac8SCy Schubert 	neighbor_rep = wpabuf_alloc(report_len - 1);
8885732ac8SCy Schubert 	if (!neighbor_rep) {
8985732ac8SCy Schubert 		wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
9085732ac8SCy Schubert 		return;
9185732ac8SCy Schubert 	}
9285732ac8SCy Schubert 	wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
93*c1d255d3SCy Schubert 	wpa_dbg(wpa_s, MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
9485732ac8SCy Schubert 		report[0]);
9585732ac8SCy Schubert 	wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
9685732ac8SCy Schubert 				       neighbor_rep);
9785732ac8SCy Schubert 	wpa_s->rrm.notify_neighbor_rep = NULL;
9885732ac8SCy Schubert 	wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
9985732ac8SCy Schubert }
10085732ac8SCy Schubert 
10185732ac8SCy Schubert 
10285732ac8SCy Schubert #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
10385732ac8SCy Schubert /* Workaround different, undefined for Windows, error codes used here */
104*c1d255d3SCy Schubert #ifndef ENOTCONN
10585732ac8SCy Schubert #define ENOTCONN -1
106*c1d255d3SCy Schubert #endif
107*c1d255d3SCy Schubert #ifndef EOPNOTSUPP
10885732ac8SCy Schubert #define EOPNOTSUPP -1
109*c1d255d3SCy Schubert #endif
110*c1d255d3SCy Schubert #ifndef ECANCELED
11185732ac8SCy Schubert #define ECANCELED -1
11285732ac8SCy Schubert #endif
113*c1d255d3SCy Schubert #endif
11485732ac8SCy Schubert 
11585732ac8SCy Schubert /* Measurement Request element + Location Subject + Maximum Age subelement */
11685732ac8SCy Schubert #define MEASURE_REQUEST_LCI_LEN (3 + 1 + 4)
11785732ac8SCy Schubert /* Measurement Request element + Location Civic Request */
11885732ac8SCy Schubert #define MEASURE_REQUEST_CIVIC_LEN (3 + 5)
11985732ac8SCy Schubert 
12085732ac8SCy Schubert 
12185732ac8SCy Schubert /**
12285732ac8SCy Schubert  * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
12385732ac8SCy Schubert  * @wpa_s: Pointer to wpa_supplicant
12485732ac8SCy Schubert  * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
12585732ac8SCy Schubert  *	  is sent in the request.
12685732ac8SCy Schubert  * @lci: if set, neighbor request will include LCI request
12785732ac8SCy Schubert  * @civic: if set, neighbor request will include civic location request
12885732ac8SCy Schubert  * @cb: Callback function to be called once the requested report arrives, or
12985732ac8SCy Schubert  *	timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
13085732ac8SCy Schubert  *	In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
13185732ac8SCy Schubert  *	the requester's responsibility to free it.
13285732ac8SCy Schubert  *	In the latter case NULL will be sent in 'neighbor_rep'.
13385732ac8SCy Schubert  * @cb_ctx: Context value to send the callback function
13485732ac8SCy Schubert  * Returns: 0 in case of success, negative error code otherwise
13585732ac8SCy Schubert  *
13685732ac8SCy Schubert  * In case there is a previous request which has not been answered yet, the
13785732ac8SCy Schubert  * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
13885732ac8SCy Schubert  * Request must contain a callback function.
13985732ac8SCy Schubert  */
wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant * wpa_s,const struct wpa_ssid_value * ssid,int lci,int civic,void (* cb)(void * ctx,struct wpabuf * neighbor_rep),void * cb_ctx)14085732ac8SCy Schubert int wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
14185732ac8SCy Schubert 				       const struct wpa_ssid_value *ssid,
14285732ac8SCy Schubert 				       int lci, int civic,
14385732ac8SCy Schubert 				       void (*cb)(void *ctx,
14485732ac8SCy Schubert 						  struct wpabuf *neighbor_rep),
14585732ac8SCy Schubert 				       void *cb_ctx)
14685732ac8SCy Schubert {
14785732ac8SCy Schubert 	struct wpabuf *buf;
14885732ac8SCy Schubert 	const u8 *rrm_ie;
14985732ac8SCy Schubert 
15085732ac8SCy Schubert 	if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
151*c1d255d3SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG, "RRM: No connection, no RRM.");
15285732ac8SCy Schubert 		return -ENOTCONN;
15385732ac8SCy Schubert 	}
15485732ac8SCy Schubert 
15585732ac8SCy Schubert 	if (!wpa_s->rrm.rrm_used) {
156*c1d255d3SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG, "RRM: No RRM in current connection.");
15785732ac8SCy Schubert 		return -EOPNOTSUPP;
15885732ac8SCy Schubert 	}
15985732ac8SCy Schubert 
16085732ac8SCy Schubert 	rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
16185732ac8SCy Schubert 				WLAN_EID_RRM_ENABLED_CAPABILITIES);
16285732ac8SCy Schubert 	if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
16385732ac8SCy Schubert 	    !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
164*c1d255d3SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG,
16585732ac8SCy Schubert 			"RRM: No network support for Neighbor Report.");
16685732ac8SCy Schubert 		return -EOPNOTSUPP;
16785732ac8SCy Schubert 	}
16885732ac8SCy Schubert 
16985732ac8SCy Schubert 	/* Refuse if there's a live request */
17085732ac8SCy Schubert 	if (wpa_s->rrm.notify_neighbor_rep) {
171*c1d255d3SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG,
17285732ac8SCy Schubert 			"RRM: Currently handling previous Neighbor Report.");
17385732ac8SCy Schubert 		return -EBUSY;
17485732ac8SCy Schubert 	}
17585732ac8SCy Schubert 
17685732ac8SCy Schubert 	/* 3 = action category + action code + dialog token */
17785732ac8SCy Schubert 	buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0) +
17885732ac8SCy Schubert 			   (lci ? 2 + MEASURE_REQUEST_LCI_LEN : 0) +
17985732ac8SCy Schubert 			   (civic ? 2 + MEASURE_REQUEST_CIVIC_LEN : 0));
18085732ac8SCy Schubert 	if (buf == NULL) {
181*c1d255d3SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG,
18285732ac8SCy Schubert 			"RRM: Failed to allocate Neighbor Report Request");
18385732ac8SCy Schubert 		return -ENOMEM;
18485732ac8SCy Schubert 	}
18585732ac8SCy Schubert 
186*c1d255d3SCy Schubert 	wpa_dbg(wpa_s, MSG_DEBUG,
187*c1d255d3SCy Schubert 		"RRM: Neighbor report request (for %s), token=%d",
18885732ac8SCy Schubert 		(ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
18985732ac8SCy Schubert 		wpa_s->rrm.next_neighbor_rep_token);
19085732ac8SCy Schubert 
19185732ac8SCy Schubert 	wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
19285732ac8SCy Schubert 	wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
19385732ac8SCy Schubert 	wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
19485732ac8SCy Schubert 	if (ssid) {
19585732ac8SCy Schubert 		wpabuf_put_u8(buf, WLAN_EID_SSID);
19685732ac8SCy Schubert 		wpabuf_put_u8(buf, ssid->ssid_len);
19785732ac8SCy Schubert 		wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
19885732ac8SCy Schubert 	}
19985732ac8SCy Schubert 
20085732ac8SCy Schubert 	if (lci) {
20185732ac8SCy Schubert 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
20285732ac8SCy Schubert 		wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
20385732ac8SCy Schubert 		wpabuf_put_u8(buf, MEASURE_REQUEST_LCI_LEN);
20485732ac8SCy Schubert 
20585732ac8SCy Schubert 		/*
20685732ac8SCy Schubert 		 * Measurement token; nonzero number that is unique among the
20785732ac8SCy Schubert 		 * Measurement Request elements in a particular frame.
20885732ac8SCy Schubert 		 */
20985732ac8SCy Schubert 		wpabuf_put_u8(buf, 1); /* Measurement Token */
21085732ac8SCy Schubert 
21185732ac8SCy Schubert 		/*
21285732ac8SCy Schubert 		 * Parallel, Enable, Request, and Report bits are 0, Duration is
21385732ac8SCy Schubert 		 * reserved.
21485732ac8SCy Schubert 		 */
21585732ac8SCy Schubert 		wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
21685732ac8SCy Schubert 		wpabuf_put_u8(buf, MEASURE_TYPE_LCI); /* Measurement Type */
21785732ac8SCy Schubert 
21885732ac8SCy Schubert 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21.10 - LCI request */
21985732ac8SCy Schubert 		/* Location Subject */
22085732ac8SCy Schubert 		wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
22185732ac8SCy Schubert 
22285732ac8SCy Schubert 		/* Optional Subelements */
22385732ac8SCy Schubert 		/*
22485732ac8SCy Schubert 		 * IEEE P802.11-REVmc/D5.0 Figure 9-170
22585732ac8SCy Schubert 		 * The Maximum Age subelement is required, otherwise the AP can
22685732ac8SCy Schubert 		 * send only data that was determined after receiving the
22785732ac8SCy Schubert 		 * request. Setting it here to unlimited age.
22885732ac8SCy Schubert 		 */
22985732ac8SCy Schubert 		wpabuf_put_u8(buf, LCI_REQ_SUBELEM_MAX_AGE);
23085732ac8SCy Schubert 		wpabuf_put_u8(buf, 2);
23185732ac8SCy Schubert 		wpabuf_put_le16(buf, 0xffff);
23285732ac8SCy Schubert 	}
23385732ac8SCy Schubert 
23485732ac8SCy Schubert 	if (civic) {
23585732ac8SCy Schubert 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
23685732ac8SCy Schubert 		wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
23785732ac8SCy Schubert 		wpabuf_put_u8(buf, MEASURE_REQUEST_CIVIC_LEN);
23885732ac8SCy Schubert 
23985732ac8SCy Schubert 		/*
24085732ac8SCy Schubert 		 * Measurement token; nonzero number that is unique among the
24185732ac8SCy Schubert 		 * Measurement Request elements in a particular frame.
24285732ac8SCy Schubert 		 */
24385732ac8SCy Schubert 		wpabuf_put_u8(buf, 2); /* Measurement Token */
24485732ac8SCy Schubert 
24585732ac8SCy Schubert 		/*
24685732ac8SCy Schubert 		 * Parallel, Enable, Request, and Report bits are 0, Duration is
24785732ac8SCy Schubert 		 * reserved.
24885732ac8SCy Schubert 		 */
24985732ac8SCy Schubert 		wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
25085732ac8SCy Schubert 		/* Measurement Type */
25185732ac8SCy Schubert 		wpabuf_put_u8(buf, MEASURE_TYPE_LOCATION_CIVIC);
25285732ac8SCy Schubert 
25385732ac8SCy Schubert 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21.14:
25485732ac8SCy Schubert 		 * Location Civic request */
25585732ac8SCy Schubert 		/* Location Subject */
25685732ac8SCy Schubert 		wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
25785732ac8SCy Schubert 		wpabuf_put_u8(buf, 0); /* Civic Location Type: IETF RFC 4776 */
25885732ac8SCy Schubert 		/* Location Service Interval Units: Seconds */
25985732ac8SCy Schubert 		wpabuf_put_u8(buf, 0);
26085732ac8SCy Schubert 		/* Location Service Interval: 0 - Only one report is requested
26185732ac8SCy Schubert 		 */
26285732ac8SCy Schubert 		wpabuf_put_le16(buf, 0);
26385732ac8SCy Schubert 		/* No optional subelements */
26485732ac8SCy Schubert 	}
26585732ac8SCy Schubert 
26685732ac8SCy Schubert 	wpa_s->rrm.next_neighbor_rep_token++;
26785732ac8SCy Schubert 
26885732ac8SCy Schubert 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
26985732ac8SCy Schubert 				wpa_s->own_addr, wpa_s->bssid,
27085732ac8SCy Schubert 				wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
271*c1d255d3SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG,
27285732ac8SCy Schubert 			"RRM: Failed to send Neighbor Report Request");
27385732ac8SCy Schubert 		wpabuf_free(buf);
27485732ac8SCy Schubert 		return -ECANCELED;
27585732ac8SCy Schubert 	}
27685732ac8SCy Schubert 
27785732ac8SCy Schubert 	wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
27885732ac8SCy Schubert 	wpa_s->rrm.notify_neighbor_rep = cb;
27985732ac8SCy Schubert 	eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
28085732ac8SCy Schubert 			       wpas_rrm_neighbor_rep_timeout_handler,
28185732ac8SCy Schubert 			       &wpa_s->rrm, NULL);
28285732ac8SCy Schubert 
28385732ac8SCy Schubert 	wpabuf_free(buf);
28485732ac8SCy Schubert 	return 0;
28585732ac8SCy Schubert }
28685732ac8SCy Schubert 
28785732ac8SCy Schubert 
wpas_rrm_report_elem(struct wpabuf ** buf,u8 token,u8 mode,u8 type,const u8 * data,size_t data_len)28885732ac8SCy Schubert static int wpas_rrm_report_elem(struct wpabuf **buf, u8 token, u8 mode, u8 type,
28985732ac8SCy Schubert 				const u8 *data, size_t data_len)
29085732ac8SCy Schubert {
29185732ac8SCy Schubert 	if (wpabuf_resize(buf, 5 + data_len))
29285732ac8SCy Schubert 		return -1;
29385732ac8SCy Schubert 
29485732ac8SCy Schubert 	wpabuf_put_u8(*buf, WLAN_EID_MEASURE_REPORT);
29585732ac8SCy Schubert 	wpabuf_put_u8(*buf, 3 + data_len);
29685732ac8SCy Schubert 	wpabuf_put_u8(*buf, token);
29785732ac8SCy Schubert 	wpabuf_put_u8(*buf, mode);
29885732ac8SCy Schubert 	wpabuf_put_u8(*buf, type);
29985732ac8SCy Schubert 
30085732ac8SCy Schubert 	if (data_len)
30185732ac8SCy Schubert 		wpabuf_put_data(*buf, data, data_len);
30285732ac8SCy Schubert 
30385732ac8SCy Schubert 	return 0;
30485732ac8SCy Schubert }
30585732ac8SCy Schubert 
30685732ac8SCy Schubert 
30785732ac8SCy Schubert static int
wpas_rrm_build_lci_report(struct wpa_supplicant * wpa_s,const struct rrm_measurement_request_element * req,struct wpabuf ** buf)30885732ac8SCy Schubert wpas_rrm_build_lci_report(struct wpa_supplicant *wpa_s,
30985732ac8SCy Schubert 			  const struct rrm_measurement_request_element *req,
31085732ac8SCy Schubert 			  struct wpabuf **buf)
31185732ac8SCy Schubert {
31285732ac8SCy Schubert 	u8 subject;
31385732ac8SCy Schubert 	u16 max_age = 0;
31485732ac8SCy Schubert 	struct os_reltime t, diff;
31585732ac8SCy Schubert 	unsigned long diff_l;
31685732ac8SCy Schubert 	const u8 *subelem;
31785732ac8SCy Schubert 	const u8 *request = req->variable;
31885732ac8SCy Schubert 	size_t len = req->len - 3;
31985732ac8SCy Schubert 
32085732ac8SCy Schubert 	if (len < 1)
32185732ac8SCy Schubert 		return -1;
32285732ac8SCy Schubert 
32385732ac8SCy Schubert 	if (!wpa_s->lci)
32485732ac8SCy Schubert 		goto reject;
32585732ac8SCy Schubert 
32685732ac8SCy Schubert 	subject = *request++;
32785732ac8SCy Schubert 	len--;
32885732ac8SCy Schubert 
32985732ac8SCy Schubert 	wpa_printf(MSG_DEBUG, "Measurement request location subject=%u",
33085732ac8SCy Schubert 		   subject);
33185732ac8SCy Schubert 
33285732ac8SCy Schubert 	if (subject != LOCATION_SUBJECT_REMOTE) {
33385732ac8SCy Schubert 		wpa_printf(MSG_INFO,
33485732ac8SCy Schubert 			   "Not building LCI report - bad location subject");
33585732ac8SCy Schubert 		return 0;
33685732ac8SCy Schubert 	}
33785732ac8SCy Schubert 
33885732ac8SCy Schubert 	/* Subelements are formatted exactly like elements */
33985732ac8SCy Schubert 	wpa_hexdump(MSG_DEBUG, "LCI request subelements", request, len);
34085732ac8SCy Schubert 	subelem = get_ie(request, len, LCI_REQ_SUBELEM_MAX_AGE);
34185732ac8SCy Schubert 	if (subelem && subelem[1] == 2)
34285732ac8SCy Schubert 		max_age = WPA_GET_LE16(subelem + 2);
34385732ac8SCy Schubert 
34485732ac8SCy Schubert 	if (os_get_reltime(&t))
34585732ac8SCy Schubert 		goto reject;
34685732ac8SCy Schubert 
34785732ac8SCy Schubert 	os_reltime_sub(&t, &wpa_s->lci_time, &diff);
34885732ac8SCy Schubert 	/* LCI age is calculated in 10th of a second units. */
34985732ac8SCy Schubert 	diff_l = diff.sec * 10 + diff.usec / 100000;
35085732ac8SCy Schubert 
35185732ac8SCy Schubert 	if (max_age != 0xffff && max_age < diff_l)
35285732ac8SCy Schubert 		goto reject;
35385732ac8SCy Schubert 
35485732ac8SCy Schubert 	if (wpas_rrm_report_elem(buf, req->token,
35585732ac8SCy Schubert 				 MEASUREMENT_REPORT_MODE_ACCEPT, req->type,
35685732ac8SCy Schubert 				 wpabuf_head_u8(wpa_s->lci),
35785732ac8SCy Schubert 				 wpabuf_len(wpa_s->lci)) < 0) {
35885732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "Failed to add LCI report element");
35985732ac8SCy Schubert 		return -1;
36085732ac8SCy Schubert 	}
36185732ac8SCy Schubert 
36285732ac8SCy Schubert 	return 0;
36385732ac8SCy Schubert 
36485732ac8SCy Schubert reject:
36585732ac8SCy Schubert 	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
36685732ac8SCy Schubert 	    wpas_rrm_report_elem(buf, req->token,
36785732ac8SCy Schubert 				 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
36885732ac8SCy Schubert 				 req->type, NULL, 0) < 0) {
36985732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
37085732ac8SCy Schubert 		return -1;
37185732ac8SCy Schubert 	}
37285732ac8SCy Schubert 
37385732ac8SCy Schubert 	return 0;
37485732ac8SCy Schubert }
37585732ac8SCy Schubert 
37685732ac8SCy Schubert 
wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant * wpa_s,const u8 * data,size_t len)37785732ac8SCy Schubert static void wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant *wpa_s,
37885732ac8SCy Schubert 					  const u8 *data, size_t len)
37985732ac8SCy Schubert {
38085732ac8SCy Schubert 	struct wpabuf *report = wpabuf_alloc(len + 3);
38185732ac8SCy Schubert 
38285732ac8SCy Schubert 	if (!report)
38385732ac8SCy Schubert 		return;
38485732ac8SCy Schubert 
38585732ac8SCy Schubert 	wpabuf_put_u8(report, WLAN_ACTION_RADIO_MEASUREMENT);
38685732ac8SCy Schubert 	wpabuf_put_u8(report, WLAN_RRM_RADIO_MEASUREMENT_REPORT);
38785732ac8SCy Schubert 	wpabuf_put_u8(report, wpa_s->rrm.token);
38885732ac8SCy Schubert 
38985732ac8SCy Schubert 	wpabuf_put_data(report, data, len);
39085732ac8SCy Schubert 
39185732ac8SCy Schubert 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
39285732ac8SCy Schubert 				wpa_s->own_addr, wpa_s->bssid,
39385732ac8SCy Schubert 				wpabuf_head(report), wpabuf_len(report), 0)) {
39485732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
39585732ac8SCy Schubert 			   "RRM: Radio measurement report failed: Sending Action frame failed");
39685732ac8SCy Schubert 	}
39785732ac8SCy Schubert 
39885732ac8SCy Schubert 	wpabuf_free(report);
39985732ac8SCy Schubert }
40085732ac8SCy Schubert 
40185732ac8SCy Schubert 
wpas_rrm_beacon_rep_update_last_frame(u8 * pos,size_t len)4024bc52338SCy Schubert static int wpas_rrm_beacon_rep_update_last_frame(u8 *pos, size_t len)
4034bc52338SCy Schubert {
4044bc52338SCy Schubert 	struct rrm_measurement_report_element *msr_rep;
4054bc52338SCy Schubert 	u8 *end = pos + len;
4064bc52338SCy Schubert 	u8 *msr_rep_end;
4074bc52338SCy Schubert 	struct rrm_measurement_beacon_report *rep = NULL;
4084bc52338SCy Schubert 	u8 *subelem;
4094bc52338SCy Schubert 
4104bc52338SCy Schubert 	/* Find the last beacon report element */
4114bc52338SCy Schubert 	while (end - pos >= (int) sizeof(*msr_rep)) {
4124bc52338SCy Schubert 		msr_rep = (struct rrm_measurement_report_element *) pos;
4134bc52338SCy Schubert 		msr_rep_end = pos + msr_rep->len + 2;
4144bc52338SCy Schubert 
4154bc52338SCy Schubert 		if (msr_rep->eid != WLAN_EID_MEASURE_REPORT ||
4164bc52338SCy Schubert 		    msr_rep_end > end) {
4174bc52338SCy Schubert 			/* Should not happen. This indicates a bug. */
4184bc52338SCy Schubert 			wpa_printf(MSG_ERROR,
4194bc52338SCy Schubert 				   "RRM: non-measurement report element in measurement report frame");
4204bc52338SCy Schubert 			return -1;
4214bc52338SCy Schubert 		}
4224bc52338SCy Schubert 
4234bc52338SCy Schubert 		if (msr_rep->type == MEASURE_TYPE_BEACON)
4244bc52338SCy Schubert 			rep = (struct rrm_measurement_beacon_report *)
4254bc52338SCy Schubert 				msr_rep->variable;
4264bc52338SCy Schubert 
4274bc52338SCy Schubert 		pos += pos[1] + 2;
4284bc52338SCy Schubert 	}
4294bc52338SCy Schubert 
4304bc52338SCy Schubert 	if (!rep)
4314bc52338SCy Schubert 		return 0;
4324bc52338SCy Schubert 
4334bc52338SCy Schubert 	subelem = rep->variable;
4344bc52338SCy Schubert 	while (subelem + 2 < msr_rep_end &&
4354bc52338SCy Schubert 	       subelem[0] != WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION)
4364bc52338SCy Schubert 		subelem += 2 + subelem[1];
4374bc52338SCy Schubert 
4384bc52338SCy Schubert 	if (subelem + 2 < msr_rep_end &&
4394bc52338SCy Schubert 	    subelem[0] == WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION &&
4404bc52338SCy Schubert 	    subelem[1] == 1 &&
4414bc52338SCy Schubert 	    subelem + BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN <= end)
4424bc52338SCy Schubert 		subelem[2] = 1;
4434bc52338SCy Schubert 
4444bc52338SCy Schubert 	return 0;
4454bc52338SCy Schubert }
4464bc52338SCy Schubert 
4474bc52338SCy Schubert 
wpas_rrm_send_msr_report(struct wpa_supplicant * wpa_s,struct wpabuf * buf)44885732ac8SCy Schubert static void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
44985732ac8SCy Schubert 				     struct wpabuf *buf)
45085732ac8SCy Schubert {
45185732ac8SCy Schubert 	int len = wpabuf_len(buf);
4524bc52338SCy Schubert 	u8 *pos = wpabuf_mhead_u8(buf), *next = pos;
45385732ac8SCy Schubert 
45485732ac8SCy Schubert #define MPDU_REPORT_LEN (int) (IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN - 3)
45585732ac8SCy Schubert 
45685732ac8SCy Schubert 	while (len) {
45785732ac8SCy Schubert 		int send_len = (len > MPDU_REPORT_LEN) ? next - pos : len;
45885732ac8SCy Schubert 
4594bc52338SCy Schubert 		if (send_len == len)
4604bc52338SCy Schubert 			wpas_rrm_beacon_rep_update_last_frame(pos, len);
4614bc52338SCy Schubert 
46285732ac8SCy Schubert 		if (send_len == len ||
46385732ac8SCy Schubert 		    (send_len + next[1] + 2) > MPDU_REPORT_LEN) {
46485732ac8SCy Schubert 			wpas_rrm_send_msr_report_mpdu(wpa_s, pos, send_len);
46585732ac8SCy Schubert 			len -= send_len;
46685732ac8SCy Schubert 			pos = next;
46785732ac8SCy Schubert 		}
46885732ac8SCy Schubert 
46985732ac8SCy Schubert 		if (len)
47085732ac8SCy Schubert 			next += next[1] + 2;
47185732ac8SCy Schubert 	}
47285732ac8SCy Schubert #undef MPDU_REPORT_LEN
47385732ac8SCy Schubert }
47485732ac8SCy Schubert 
47585732ac8SCy Schubert 
wpas_add_channel(u8 op_class,u8 chan,u8 num_primary_channels,int * freqs)47685732ac8SCy Schubert static int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
47785732ac8SCy Schubert 			    int *freqs)
47885732ac8SCy Schubert {
47985732ac8SCy Schubert 	size_t i;
48085732ac8SCy Schubert 
48185732ac8SCy Schubert 	for (i = 0; i < num_primary_channels; i++) {
48285732ac8SCy Schubert 		u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
48385732ac8SCy Schubert 
48485732ac8SCy Schubert 		freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
48585732ac8SCy Schubert 		/* ieee80211_chan_to_freq() is not really meant for this
48685732ac8SCy Schubert 		 * conversion of 20 MHz primary channel numbers for wider VHT
48785732ac8SCy Schubert 		 * channels, so handle those as special cases here for now. */
48885732ac8SCy Schubert 		if (freqs[i] < 0 &&
48985732ac8SCy Schubert 		    (op_class == 128 || op_class == 129 || op_class == 130))
49085732ac8SCy Schubert 			freqs[i] = 5000 + 5 * primary_chan;
49185732ac8SCy Schubert 		if (freqs[i] < 0) {
49285732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
49385732ac8SCy Schubert 				   "Beacon Report: Invalid channel %u",
49485732ac8SCy Schubert 				   chan);
49585732ac8SCy Schubert 			return -1;
49685732ac8SCy Schubert 		}
49785732ac8SCy Schubert 	}
49885732ac8SCy Schubert 
49985732ac8SCy Schubert 	return 0;
50085732ac8SCy Schubert }
50185732ac8SCy Schubert 
50285732ac8SCy Schubert 
wpas_add_channels(const struct oper_class_map * op,struct hostapd_hw_modes * mode,int active,const u8 * channels,const u8 size)50385732ac8SCy Schubert static int * wpas_add_channels(const struct oper_class_map *op,
50485732ac8SCy Schubert 			       struct hostapd_hw_modes *mode, int active,
50585732ac8SCy Schubert 			       const u8 *channels, const u8 size)
50685732ac8SCy Schubert {
50785732ac8SCy Schubert 	int *freqs, *next_freq;
50885732ac8SCy Schubert 	u8 num_primary_channels, i;
50985732ac8SCy Schubert 	u8 num_chans;
51085732ac8SCy Schubert 
51185732ac8SCy Schubert 	num_chans = channels ? size :
51285732ac8SCy Schubert 		(op->max_chan - op->min_chan) / op->inc + 1;
51385732ac8SCy Schubert 
51485732ac8SCy Schubert 	if (op->bw == BW80 || op->bw == BW80P80)
51585732ac8SCy Schubert 		num_primary_channels = 4;
51685732ac8SCy Schubert 	else if (op->bw == BW160)
51785732ac8SCy Schubert 		num_primary_channels = 8;
51885732ac8SCy Schubert 	else
51985732ac8SCy Schubert 		num_primary_channels = 1;
52085732ac8SCy Schubert 
52185732ac8SCy Schubert 	/* one extra place for the zero-terminator */
52285732ac8SCy Schubert 	freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
52385732ac8SCy Schubert 	if (!freqs) {
52485732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
52585732ac8SCy Schubert 			   "Beacon Report: Failed to allocate freqs array");
52685732ac8SCy Schubert 		return NULL;
52785732ac8SCy Schubert 	}
52885732ac8SCy Schubert 
52985732ac8SCy Schubert 	next_freq = freqs;
53085732ac8SCy Schubert 	for  (i = 0; i < num_chans; i++) {
53185732ac8SCy Schubert 		u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
532*c1d255d3SCy Schubert 		enum chan_allowed res = verify_channel(mode, op->op_class, chan,
533*c1d255d3SCy Schubert 						       op->bw);
53485732ac8SCy Schubert 
53585732ac8SCy Schubert 		if (res == NOT_ALLOWED || (res == NO_IR && active))
53685732ac8SCy Schubert 			continue;
53785732ac8SCy Schubert 
53885732ac8SCy Schubert 		if (wpas_add_channel(op->op_class, chan, num_primary_channels,
53985732ac8SCy Schubert 				     next_freq) < 0) {
54085732ac8SCy Schubert 			os_free(freqs);
54185732ac8SCy Schubert 			return NULL;
54285732ac8SCy Schubert 		}
54385732ac8SCy Schubert 
54485732ac8SCy Schubert 		next_freq += num_primary_channels;
54585732ac8SCy Schubert 	}
54685732ac8SCy Schubert 
54785732ac8SCy Schubert 	if (!freqs[0]) {
54885732ac8SCy Schubert 		os_free(freqs);
54985732ac8SCy Schubert 		return NULL;
55085732ac8SCy Schubert 	}
55185732ac8SCy Schubert 
55285732ac8SCy Schubert 	return freqs;
55385732ac8SCy Schubert }
55485732ac8SCy Schubert 
55585732ac8SCy Schubert 
wpas_op_class_freqs(const struct oper_class_map * op,struct hostapd_hw_modes * mode,int active)55685732ac8SCy Schubert static int * wpas_op_class_freqs(const struct oper_class_map *op,
55785732ac8SCy Schubert 				 struct hostapd_hw_modes *mode, int active)
55885732ac8SCy Schubert {
559*c1d255d3SCy Schubert 	u8 channels_80mhz_5ghz[] = { 42, 58, 106, 122, 138, 155, 171 };
560*c1d255d3SCy Schubert 	u8 channels_160mhz_5ghz[] = { 50, 114, 163 };
561*c1d255d3SCy Schubert 	u8 channels_80mhz_6ghz[] = { 7, 23, 39, 55, 71, 87, 103, 119, 135, 151,
562*c1d255d3SCy Schubert 				     167, 183, 199, 215 };
563*c1d255d3SCy Schubert 	u8 channels_160mhz_6ghz[] = { 15, 47, 79, 111, 143, 175, 207 };
564*c1d255d3SCy Schubert 	const u8 *channels = NULL;
565*c1d255d3SCy Schubert 	size_t num_chan = 0;
566*c1d255d3SCy Schubert 	bool is_6ghz = is_6ghz_op_class(op->op_class);
56785732ac8SCy Schubert 
56885732ac8SCy Schubert 	/*
56985732ac8SCy Schubert 	 * When adding all channels in the operating class, 80 + 80 MHz
57085732ac8SCy Schubert 	 * operating classes are like 80 MHz channels because we add all valid
57185732ac8SCy Schubert 	 * channels anyway.
57285732ac8SCy Schubert 	 */
573*c1d255d3SCy Schubert 	if (op->bw == BW80 || op->bw == BW80P80) {
574*c1d255d3SCy Schubert 		channels = is_6ghz ? channels_80mhz_6ghz : channels_80mhz_5ghz;
575*c1d255d3SCy Schubert 		num_chan = is_6ghz ? ARRAY_SIZE(channels_80mhz_6ghz) :
576*c1d255d3SCy Schubert 			ARRAY_SIZE(channels_80mhz_5ghz);
577*c1d255d3SCy Schubert 	} else if (op->bw == BW160) {
578*c1d255d3SCy Schubert 		channels = is_6ghz ? channels_160mhz_6ghz :
579*c1d255d3SCy Schubert 			channels_160mhz_5ghz;
580*c1d255d3SCy Schubert 		num_chan =  is_6ghz ? ARRAY_SIZE(channels_160mhz_6ghz) :
581*c1d255d3SCy Schubert 			ARRAY_SIZE(channels_160mhz_5ghz);
582*c1d255d3SCy Schubert 	}
58385732ac8SCy Schubert 
584*c1d255d3SCy Schubert 	return wpas_add_channels(op, mode, active, channels, num_chan);
58585732ac8SCy Schubert }
58685732ac8SCy Schubert 
58785732ac8SCy Schubert 
wpas_channel_report_freqs(struct wpa_supplicant * wpa_s,int active,const char * country,const u8 * subelems,size_t len)58885732ac8SCy Schubert static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s, int active,
58985732ac8SCy Schubert 				       const char *country, const u8 *subelems,
59085732ac8SCy Schubert 				       size_t len)
59185732ac8SCy Schubert {
59285732ac8SCy Schubert 	int *freqs = NULL, *new_freqs;
59385732ac8SCy Schubert 	const u8 *end = subelems + len;
59485732ac8SCy Schubert 
59585732ac8SCy Schubert 	while (end - subelems > 2) {
59685732ac8SCy Schubert 		const struct oper_class_map *op;
59785732ac8SCy Schubert 		const u8 *ap_chan_elem, *pos;
59885732ac8SCy Schubert 		u8 left;
59985732ac8SCy Schubert 		struct hostapd_hw_modes *mode;
60085732ac8SCy Schubert 
60185732ac8SCy Schubert 		ap_chan_elem = get_ie(subelems, end - subelems,
60285732ac8SCy Schubert 				      WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
60385732ac8SCy Schubert 		if (!ap_chan_elem)
60485732ac8SCy Schubert 			break;
60585732ac8SCy Schubert 		pos = ap_chan_elem + 2;
60685732ac8SCy Schubert 		left = ap_chan_elem[1];
60785732ac8SCy Schubert 		if (left < 1)
60885732ac8SCy Schubert 			break;
60985732ac8SCy Schubert 		subelems = ap_chan_elem + 2 + left;
61085732ac8SCy Schubert 
61185732ac8SCy Schubert 		op = get_oper_class(country, *pos);
61285732ac8SCy Schubert 		if (!op) {
61385732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
61485732ac8SCy Schubert 				   "Beacon request: unknown operating class in AP Channel Report subelement %u",
61585732ac8SCy Schubert 				   *pos);
61685732ac8SCy Schubert 			goto out;
61785732ac8SCy Schubert 		}
61885732ac8SCy Schubert 		pos++;
61985732ac8SCy Schubert 		left--;
62085732ac8SCy Schubert 
621*c1d255d3SCy Schubert 		mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode,
622*c1d255d3SCy Schubert 				is_6ghz_op_class(op->op_class));
62385732ac8SCy Schubert 		if (!mode)
62485732ac8SCy Schubert 			continue;
62585732ac8SCy Schubert 
62685732ac8SCy Schubert 		/*
62785732ac8SCy Schubert 		 * For 80 + 80 MHz operating classes, this AP Channel Report
62885732ac8SCy Schubert 		 * element should be followed by another element specifying
62985732ac8SCy Schubert 		 * the second 80 MHz channel. For now just add this 80 MHz
63085732ac8SCy Schubert 		 * channel, the second 80 MHz channel will be added when the
63185732ac8SCy Schubert 		 * next element is parsed.
63285732ac8SCy Schubert 		 * TODO: Verify that this AP Channel Report element is followed
63385732ac8SCy Schubert 		 * by a corresponding AP Channel Report element as specified in
63485732ac8SCy Schubert 		 * IEEE Std 802.11-2016, 11.11.9.1.
63585732ac8SCy Schubert 		 */
63685732ac8SCy Schubert 		new_freqs = wpas_add_channels(op, mode, active, pos, left);
63785732ac8SCy Schubert 		if (new_freqs)
63885732ac8SCy Schubert 			int_array_concat(&freqs, new_freqs);
63985732ac8SCy Schubert 
64085732ac8SCy Schubert 		os_free(new_freqs);
64185732ac8SCy Schubert 	}
64285732ac8SCy Schubert 
64385732ac8SCy Schubert 	return freqs;
64485732ac8SCy Schubert out:
64585732ac8SCy Schubert 	os_free(freqs);
64685732ac8SCy Schubert 	return NULL;
64785732ac8SCy Schubert }
64885732ac8SCy Schubert 
64985732ac8SCy Schubert 
wpas_beacon_request_freqs(struct wpa_supplicant * wpa_s,u8 op_class,u8 chan,int active,const u8 * subelems,size_t len)65085732ac8SCy Schubert static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
65185732ac8SCy Schubert 				       u8 op_class, u8 chan, int active,
65285732ac8SCy Schubert 				       const u8 *subelems, size_t len)
65385732ac8SCy Schubert {
65485732ac8SCy Schubert 	int *freqs = NULL, *ext_freqs = NULL;
65585732ac8SCy Schubert 	struct hostapd_hw_modes *mode;
65685732ac8SCy Schubert 	const char *country = NULL;
65785732ac8SCy Schubert 	const struct oper_class_map *op;
65885732ac8SCy Schubert 	const u8 *elem;
65985732ac8SCy Schubert 
66085732ac8SCy Schubert 	if (!wpa_s->current_bss)
66185732ac8SCy Schubert 		return NULL;
66285732ac8SCy Schubert 	elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
66385732ac8SCy Schubert 	if (elem && elem[1] >= 2)
66485732ac8SCy Schubert 		country = (const char *) (elem + 2);
66585732ac8SCy Schubert 
66685732ac8SCy Schubert 	op = get_oper_class(country, op_class);
66785732ac8SCy Schubert 	if (!op) {
66885732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
66985732ac8SCy Schubert 			   "Beacon request: invalid operating class %d",
67085732ac8SCy Schubert 			   op_class);
67185732ac8SCy Schubert 		return NULL;
67285732ac8SCy Schubert 	}
67385732ac8SCy Schubert 
674*c1d255d3SCy Schubert 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode,
675*c1d255d3SCy Schubert 			is_6ghz_op_class(op->op_class));
67685732ac8SCy Schubert 	if (!mode)
67785732ac8SCy Schubert 		return NULL;
67885732ac8SCy Schubert 
67985732ac8SCy Schubert 	switch (chan) {
68085732ac8SCy Schubert 	case 0:
68185732ac8SCy Schubert 		freqs = wpas_op_class_freqs(op, mode, active);
68285732ac8SCy Schubert 		if (!freqs)
68385732ac8SCy Schubert 			return NULL;
68485732ac8SCy Schubert 		break;
68585732ac8SCy Schubert 	case 255:
68685732ac8SCy Schubert 		/* freqs will be added from AP channel subelements */
68785732ac8SCy Schubert 		break;
68885732ac8SCy Schubert 	default:
68985732ac8SCy Schubert 		freqs = wpas_add_channels(op, mode, active, &chan, 1);
69085732ac8SCy Schubert 		if (!freqs)
69185732ac8SCy Schubert 			return NULL;
69285732ac8SCy Schubert 		break;
69385732ac8SCy Schubert 	}
69485732ac8SCy Schubert 
69585732ac8SCy Schubert 	ext_freqs = wpas_channel_report_freqs(wpa_s, active, country, subelems,
69685732ac8SCy Schubert 					      len);
69785732ac8SCy Schubert 	if (ext_freqs) {
69885732ac8SCy Schubert 		int_array_concat(&freqs, ext_freqs);
69985732ac8SCy Schubert 		os_free(ext_freqs);
70085732ac8SCy Schubert 		int_array_sort_unique(freqs);
70185732ac8SCy Schubert 	}
70285732ac8SCy Schubert 
70385732ac8SCy Schubert 	return freqs;
70485732ac8SCy Schubert }
70585732ac8SCy Schubert 
70685732ac8SCy Schubert 
wpas_get_op_chan_phy(int freq,const u8 * ies,size_t ies_len,u8 * op_class,u8 * chan,u8 * phy_type)707*c1d255d3SCy Schubert int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
70885732ac8SCy Schubert 			 u8 *op_class, u8 *chan, u8 *phy_type)
70985732ac8SCy Schubert {
71085732ac8SCy Schubert 	const u8 *ie;
71185732ac8SCy Schubert 	int sec_chan = 0, vht = 0;
71285732ac8SCy Schubert 	struct ieee80211_ht_operation *ht_oper = NULL;
71385732ac8SCy Schubert 	struct ieee80211_vht_operation *vht_oper = NULL;
71485732ac8SCy Schubert 	u8 seg0, seg1;
71585732ac8SCy Schubert 
71685732ac8SCy Schubert 	ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
71785732ac8SCy Schubert 	if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
71885732ac8SCy Schubert 		u8 sec_chan_offset;
71985732ac8SCy Schubert 
72085732ac8SCy Schubert 		ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
72185732ac8SCy Schubert 		sec_chan_offset = ht_oper->ht_param &
72285732ac8SCy Schubert 			HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
72385732ac8SCy Schubert 		if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
72485732ac8SCy Schubert 			sec_chan = 1;
72585732ac8SCy Schubert 		else if (sec_chan_offset ==
72685732ac8SCy Schubert 			 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
72785732ac8SCy Schubert 			sec_chan = -1;
72885732ac8SCy Schubert 	}
72985732ac8SCy Schubert 
73085732ac8SCy Schubert 	ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
73185732ac8SCy Schubert 	if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
73285732ac8SCy Schubert 		vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
73385732ac8SCy Schubert 
73485732ac8SCy Schubert 		switch (vht_oper->vht_op_info_chwidth) {
73585732ac8SCy Schubert 		case 1:
73685732ac8SCy Schubert 			seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
73785732ac8SCy Schubert 			seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
73885732ac8SCy Schubert 			if (seg1 && abs(seg1 - seg0) == 8)
739206b73d0SCy Schubert 				vht = CHANWIDTH_160MHZ;
74085732ac8SCy Schubert 			else if (seg1)
741206b73d0SCy Schubert 				vht = CHANWIDTH_80P80MHZ;
74285732ac8SCy Schubert 			else
743206b73d0SCy Schubert 				vht = CHANWIDTH_80MHZ;
74485732ac8SCy Schubert 			break;
74585732ac8SCy Schubert 		case 2:
746206b73d0SCy Schubert 			vht = CHANWIDTH_160MHZ;
74785732ac8SCy Schubert 			break;
74885732ac8SCy Schubert 		case 3:
749206b73d0SCy Schubert 			vht = CHANWIDTH_80P80MHZ;
75085732ac8SCy Schubert 			break;
75185732ac8SCy Schubert 		default:
752206b73d0SCy Schubert 			vht = CHANWIDTH_USE_HT;
75385732ac8SCy Schubert 			break;
75485732ac8SCy Schubert 		}
75585732ac8SCy Schubert 	}
75685732ac8SCy Schubert 
75785732ac8SCy Schubert 	if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
75885732ac8SCy Schubert 					  chan) == NUM_HOSTAPD_MODES) {
75985732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
76085732ac8SCy Schubert 			   "Cannot determine operating class and channel");
76185732ac8SCy Schubert 		return -1;
76285732ac8SCy Schubert 	}
76385732ac8SCy Schubert 
76485732ac8SCy Schubert 	*phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
76585732ac8SCy Schubert 					   vht_oper != NULL);
76685732ac8SCy Schubert 	if (*phy_type == PHY_TYPE_UNSPECIFIED) {
76785732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "Cannot determine phy type");
76885732ac8SCy Schubert 		return -1;
76985732ac8SCy Schubert 	}
77085732ac8SCy Schubert 
77185732ac8SCy Schubert 	return 0;
77285732ac8SCy Schubert }
77385732ac8SCy Schubert 
77485732ac8SCy Schubert 
wpas_beacon_rep_add_frame_body(struct bitfield * eids,enum beacon_report_detail detail,struct wpa_bss * bss,u8 * buf,size_t buf_len,const u8 ** ies_buf,size_t * ie_len,int add_fixed)77585732ac8SCy Schubert static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
77685732ac8SCy Schubert 					  enum beacon_report_detail detail,
77785732ac8SCy Schubert 					  struct wpa_bss *bss, u8 *buf,
778*c1d255d3SCy Schubert 					  size_t buf_len, const u8 **ies_buf,
7794bc52338SCy Schubert 					  size_t *ie_len, int add_fixed)
78085732ac8SCy Schubert {
781*c1d255d3SCy Schubert 	const u8 *ies = *ies_buf;
7824bc52338SCy Schubert 	size_t ies_len = *ie_len;
78385732ac8SCy Schubert 	u8 *pos = buf;
78485732ac8SCy Schubert 	int rem_len;
78585732ac8SCy Schubert 
78685732ac8SCy Schubert 	rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
7874bc52338SCy Schubert 		sizeof(struct rrm_measurement_report_element) - 2 -
7884bc52338SCy Schubert 		REPORTED_FRAME_BODY_SUBELEM_LEN;
78985732ac8SCy Schubert 
79085732ac8SCy Schubert 	if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
79185732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
79285732ac8SCy Schubert 			   "Beacon Request: Invalid reporting detail: %d",
79385732ac8SCy Schubert 			   detail);
79485732ac8SCy Schubert 		return -1;
79585732ac8SCy Schubert 	}
79685732ac8SCy Schubert 
79785732ac8SCy Schubert 	if (detail == BEACON_REPORT_DETAIL_NONE)
79885732ac8SCy Schubert 		return 0;
79985732ac8SCy Schubert 
80085732ac8SCy Schubert 	/*
80185732ac8SCy Schubert 	 * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
80285732ac8SCy Schubert 	 * beacon interval(2) + capabilities(2) = 14 bytes
80385732ac8SCy Schubert 	 */
8044bc52338SCy Schubert 	if (add_fixed && buf_len < 14)
8054bc52338SCy Schubert 		return -1;
80685732ac8SCy Schubert 
80785732ac8SCy Schubert 	*pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
80885732ac8SCy Schubert 	/* The length will be filled later */
80985732ac8SCy Schubert 	pos++;
8104bc52338SCy Schubert 
8114bc52338SCy Schubert 	if (add_fixed) {
81285732ac8SCy Schubert 		WPA_PUT_LE64(pos, bss->tsf);
81385732ac8SCy Schubert 		pos += sizeof(bss->tsf);
81485732ac8SCy Schubert 		WPA_PUT_LE16(pos, bss->beacon_int);
81585732ac8SCy Schubert 		pos += 2;
81685732ac8SCy Schubert 		WPA_PUT_LE16(pos, bss->caps);
81785732ac8SCy Schubert 		pos += 2;
8184bc52338SCy Schubert 	}
81985732ac8SCy Schubert 
82085732ac8SCy Schubert 	rem_len -= pos - buf;
82185732ac8SCy Schubert 
82285732ac8SCy Schubert 	/*
82385732ac8SCy Schubert 	 * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
82485732ac8SCy Schubert 	 * body subelement causes the element to exceed the maximum element
82585732ac8SCy Schubert 	 * size, the subelement is truncated so that the last IE is a complete
82685732ac8SCy Schubert 	 * IE. So even when required to report all IEs, add elements one after
82785732ac8SCy Schubert 	 * the other and stop once there is no more room in the measurement
82885732ac8SCy Schubert 	 * element.
82985732ac8SCy Schubert 	 */
83085732ac8SCy Schubert 	while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
83185732ac8SCy Schubert 		if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
83285732ac8SCy Schubert 		    (eids && bitfield_is_set(eids, ies[0]))) {
8334bc52338SCy Schubert 			u8 elen = ies[1];
83485732ac8SCy Schubert 
83585732ac8SCy Schubert 			if (2 + elen > buf + buf_len - pos ||
83685732ac8SCy Schubert 			    2 + elen > rem_len)
83785732ac8SCy Schubert 				break;
83885732ac8SCy Schubert 
83985732ac8SCy Schubert 			*pos++ = ies[0];
84085732ac8SCy Schubert 			*pos++ = elen;
84185732ac8SCy Schubert 			os_memcpy(pos, ies + 2, elen);
84285732ac8SCy Schubert 			pos += elen;
84385732ac8SCy Schubert 			rem_len -= 2 + elen;
84485732ac8SCy Schubert 		}
84585732ac8SCy Schubert 
84685732ac8SCy Schubert 		ies_len -= 2 + ies[1];
84785732ac8SCy Schubert 		ies += 2 + ies[1];
84885732ac8SCy Schubert 	}
84985732ac8SCy Schubert 
8504bc52338SCy Schubert 	*ie_len = ies_len;
8514bc52338SCy Schubert 	*ies_buf = ies;
8524bc52338SCy Schubert 
85385732ac8SCy Schubert 	/* Now the length is known */
85485732ac8SCy Schubert 	buf[1] = pos - buf - 2;
85585732ac8SCy Schubert 	return pos - buf;
85685732ac8SCy Schubert }
85785732ac8SCy Schubert 
85885732ac8SCy Schubert 
wpas_add_beacon_rep_elem(struct beacon_rep_data * data,struct wpa_bss * bss,struct wpabuf ** wpa_buf,struct rrm_measurement_beacon_report * rep,const u8 ** ie,size_t * ie_len,u8 idx)8594bc52338SCy Schubert static int wpas_add_beacon_rep_elem(struct beacon_rep_data *data,
8604bc52338SCy Schubert 				    struct wpa_bss *bss,
8614bc52338SCy Schubert 				    struct wpabuf **wpa_buf,
8624bc52338SCy Schubert 				    struct rrm_measurement_beacon_report *rep,
863*c1d255d3SCy Schubert 				    const u8 **ie, size_t *ie_len, u8 idx)
8644bc52338SCy Schubert {
8654bc52338SCy Schubert 	int ret;
8664bc52338SCy Schubert 	u8 *buf, *pos;
8674bc52338SCy Schubert 	u32 subelems_len = REPORTED_FRAME_BODY_SUBELEM_LEN +
8684bc52338SCy Schubert 		(data->last_indication ?
8694bc52338SCy Schubert 		 BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN : 0);
8704bc52338SCy Schubert 
8714bc52338SCy Schubert 	/* Maximum element length: Beacon Report element + Reported Frame Body
8724bc52338SCy Schubert 	 * subelement + all IEs of the reported Beacon frame + Reported Frame
8734bc52338SCy Schubert 	 * Body Fragment ID subelement */
8744bc52338SCy Schubert 	buf = os_malloc(sizeof(*rep) + 14 + *ie_len + subelems_len);
8754bc52338SCy Schubert 	if (!buf)
8764bc52338SCy Schubert 		return -1;
8774bc52338SCy Schubert 
8784bc52338SCy Schubert 	os_memcpy(buf, rep, sizeof(*rep));
8794bc52338SCy Schubert 
8804bc52338SCy Schubert 	ret = wpas_beacon_rep_add_frame_body(data->eids, data->report_detail,
8814bc52338SCy Schubert 					     bss, buf + sizeof(*rep),
8824bc52338SCy Schubert 					     14 + *ie_len, ie, ie_len,
8834bc52338SCy Schubert 					     idx == 0);
8844bc52338SCy Schubert 	if (ret < 0)
8854bc52338SCy Schubert 		goto out;
8864bc52338SCy Schubert 
8874bc52338SCy Schubert 	pos = buf + ret + sizeof(*rep);
8884bc52338SCy Schubert 	pos[0] = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY_FRAGMENT_ID;
8894bc52338SCy Schubert 	pos[1] = 2;
8904bc52338SCy Schubert 
8914bc52338SCy Schubert 	/*
8924bc52338SCy Schubert 	 * Only one Beacon Report Measurement is supported at a time, so
8934bc52338SCy Schubert 	 * the Beacon Report ID can always be set to 1.
8944bc52338SCy Schubert 	 */
8954bc52338SCy Schubert 	pos[2] = 1;
8964bc52338SCy Schubert 
8974bc52338SCy Schubert 	/* Fragment ID Number (bits 0..6) and More Frame Body Fragments (bit 7)
8984bc52338SCy Schubert  */
8994bc52338SCy Schubert 	pos[3] = idx;
9004bc52338SCy Schubert 	if (data->report_detail != BEACON_REPORT_DETAIL_NONE && *ie_len)
9014bc52338SCy Schubert 		pos[3] |= REPORTED_FRAME_BODY_MORE_FRAGMENTS;
9024bc52338SCy Schubert 	else
9034bc52338SCy Schubert 		pos[3] &= ~REPORTED_FRAME_BODY_MORE_FRAGMENTS;
9044bc52338SCy Schubert 
9054bc52338SCy Schubert 	pos += REPORTED_FRAME_BODY_SUBELEM_LEN;
9064bc52338SCy Schubert 
9074bc52338SCy Schubert 	if (data->last_indication) {
9084bc52338SCy Schubert 		pos[0] = WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION;
9094bc52338SCy Schubert 		pos[1] = 1;
9104bc52338SCy Schubert 
9114bc52338SCy Schubert 		/* This field will be updated later if this is the last frame */
9124bc52338SCy Schubert 		pos[2] = 0;
9134bc52338SCy Schubert 	}
9144bc52338SCy Schubert 
9154bc52338SCy Schubert 	ret = wpas_rrm_report_elem(wpa_buf, data->token,
9164bc52338SCy Schubert 				   MEASUREMENT_REPORT_MODE_ACCEPT,
9174bc52338SCy Schubert 				   MEASURE_TYPE_BEACON, buf,
9184bc52338SCy Schubert 				   ret + sizeof(*rep) + subelems_len);
9194bc52338SCy Schubert out:
9204bc52338SCy Schubert 	os_free(buf);
9214bc52338SCy Schubert 	return ret;
9224bc52338SCy Schubert }
9234bc52338SCy Schubert 
9244bc52338SCy Schubert 
wpas_add_beacon_rep(struct wpa_supplicant * wpa_s,struct wpabuf ** wpa_buf,struct wpa_bss * bss,u64 start,u64 parent_tsf)92585732ac8SCy Schubert static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
92685732ac8SCy Schubert 			       struct wpabuf **wpa_buf, struct wpa_bss *bss,
92785732ac8SCy Schubert 			       u64 start, u64 parent_tsf)
92885732ac8SCy Schubert {
92985732ac8SCy Schubert 	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
930*c1d255d3SCy Schubert 	const u8 *ies = wpa_bss_ie_ptr(bss);
931*c1d255d3SCy Schubert 	const u8 *pos = ies;
9324bc52338SCy Schubert 	size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
9334bc52338SCy Schubert 	struct rrm_measurement_beacon_report rep;
9344bc52338SCy Schubert 	u8 idx = 0;
93585732ac8SCy Schubert 
93685732ac8SCy Schubert 	if (os_memcmp(data->bssid, broadcast_ether_addr, ETH_ALEN) != 0 &&
93785732ac8SCy Schubert 	    os_memcmp(data->bssid, bss->bssid, ETH_ALEN) != 0)
93885732ac8SCy Schubert 		return 0;
93985732ac8SCy Schubert 
94085732ac8SCy Schubert 	if (data->ssid_len &&
94185732ac8SCy Schubert 	    (data->ssid_len != bss->ssid_len ||
94285732ac8SCy Schubert 	     os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
94385732ac8SCy Schubert 		return 0;
94485732ac8SCy Schubert 
9454bc52338SCy Schubert 	if (wpas_get_op_chan_phy(bss->freq, ies, ies_len, &rep.op_class,
9464bc52338SCy Schubert 				 &rep.channel, &rep.report_info) < 0)
9474bc52338SCy Schubert 		return 0;
94885732ac8SCy Schubert 
9494bc52338SCy Schubert 	rep.start_time = host_to_le64(start);
9504bc52338SCy Schubert 	rep.duration = host_to_le16(data->scan_params.duration);
9514bc52338SCy Schubert 	rep.rcpi = rssi_to_rcpi(bss->level);
9524bc52338SCy Schubert 	rep.rsni = 255; /* 255 indicates that RSNI is not available */
9534bc52338SCy Schubert 	os_memcpy(rep.bssid, bss->bssid, ETH_ALEN);
9544bc52338SCy Schubert 	rep.antenna_id = 0; /* unknown */
9554bc52338SCy Schubert 	rep.parent_tsf = host_to_le32(parent_tsf);
95685732ac8SCy Schubert 
9574bc52338SCy Schubert 	do {
9584bc52338SCy Schubert 		int ret;
95985732ac8SCy Schubert 
9604bc52338SCy Schubert 		ret = wpas_add_beacon_rep_elem(data, bss, wpa_buf, &rep,
9614bc52338SCy Schubert 					       &pos, &ies_len, idx++);
9624bc52338SCy Schubert 		if (ret)
96385732ac8SCy Schubert 			return ret;
9644bc52338SCy Schubert 	} while (data->report_detail != BEACON_REPORT_DETAIL_NONE &&
9654bc52338SCy Schubert 		 ies_len >= 2);
9664bc52338SCy Schubert 
9674bc52338SCy Schubert 	return 0;
96885732ac8SCy Schubert }
96985732ac8SCy Schubert 
97085732ac8SCy Schubert 
wpas_beacon_rep_no_results(struct wpa_supplicant * wpa_s,struct wpabuf ** buf)97185732ac8SCy Schubert static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
97285732ac8SCy Schubert 				      struct wpabuf **buf)
97385732ac8SCy Schubert {
97485732ac8SCy Schubert 	return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
97585732ac8SCy Schubert 				    MEASUREMENT_REPORT_MODE_ACCEPT,
97685732ac8SCy Schubert 				    MEASURE_TYPE_BEACON, NULL, 0);
97785732ac8SCy Schubert }
97885732ac8SCy Schubert 
97985732ac8SCy Schubert 
wpas_beacon_rep_table(struct wpa_supplicant * wpa_s,struct wpabuf ** buf)98085732ac8SCy Schubert static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
98185732ac8SCy Schubert 				  struct wpabuf **buf)
98285732ac8SCy Schubert {
98385732ac8SCy Schubert 	size_t i;
98485732ac8SCy Schubert 
98585732ac8SCy Schubert 	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
98685732ac8SCy Schubert 		if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
98785732ac8SCy Schubert 					0, 0) < 0)
98885732ac8SCy Schubert 			break;
98985732ac8SCy Schubert 	}
99085732ac8SCy Schubert 
99185732ac8SCy Schubert 	if (!(*buf))
99285732ac8SCy Schubert 		wpas_beacon_rep_no_results(wpa_s, buf);
99385732ac8SCy Schubert 
99485732ac8SCy Schubert 	wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
99585732ac8SCy Schubert }
99685732ac8SCy Schubert 
99785732ac8SCy Schubert 
wpas_rrm_refuse_request(struct wpa_supplicant * wpa_s)99885732ac8SCy Schubert void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
99985732ac8SCy Schubert {
100085732ac8SCy Schubert 	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr)) {
100185732ac8SCy Schubert 		struct wpabuf *buf = NULL;
100285732ac8SCy Schubert 
100385732ac8SCy Schubert 		if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
100485732ac8SCy Schubert 					 MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
100585732ac8SCy Schubert 					 MEASURE_TYPE_BEACON, NULL, 0)) {
100685732ac8SCy Schubert 			wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
100785732ac8SCy Schubert 			wpabuf_free(buf);
100885732ac8SCy Schubert 			return;
100985732ac8SCy Schubert 		}
101085732ac8SCy Schubert 
101185732ac8SCy Schubert 		wpas_rrm_send_msr_report(wpa_s, buf);
101285732ac8SCy Schubert 		wpabuf_free(buf);
101385732ac8SCy Schubert 	}
101485732ac8SCy Schubert 
101585732ac8SCy Schubert 	wpas_clear_beacon_rep_data(wpa_s);
101685732ac8SCy Schubert }
101785732ac8SCy Schubert 
101885732ac8SCy Schubert 
wpas_rrm_scan_timeout(void * eloop_ctx,void * timeout_ctx)101985732ac8SCy Schubert static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
102085732ac8SCy Schubert {
102185732ac8SCy Schubert 	struct wpa_supplicant *wpa_s = eloop_ctx;
102285732ac8SCy Schubert 	struct wpa_driver_scan_params *params =
102385732ac8SCy Schubert 		&wpa_s->beacon_rep_data.scan_params;
102485732ac8SCy Schubert 	u16 prev_duration = params->duration;
102585732ac8SCy Schubert 
102685732ac8SCy Schubert 	if (!wpa_s->current_bss)
102785732ac8SCy Schubert 		return;
102885732ac8SCy Schubert 
102985732ac8SCy Schubert 	if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
103085732ac8SCy Schubert 	    params->duration) {
103185732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
103285732ac8SCy Schubert 			   "RRM: Cannot set scan duration due to missing driver support");
103385732ac8SCy Schubert 		params->duration = 0;
103485732ac8SCy Schubert 	}
103585732ac8SCy Schubert 	os_get_reltime(&wpa_s->beacon_rep_scan);
103685732ac8SCy Schubert 	if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
103785732ac8SCy Schubert 	    wpa_supplicant_trigger_scan(wpa_s, params))
103885732ac8SCy Schubert 		wpas_rrm_refuse_request(wpa_s);
103985732ac8SCy Schubert 	params->duration = prev_duration;
104085732ac8SCy Schubert }
104185732ac8SCy Schubert 
104285732ac8SCy Schubert 
wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant * wpa_s,struct beacon_rep_data * data,u8 sid,u8 slen,const u8 * subelem)104385732ac8SCy Schubert static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
104485732ac8SCy Schubert 					     struct beacon_rep_data *data,
104585732ac8SCy Schubert 					     u8 sid, u8 slen, const u8 *subelem)
104685732ac8SCy Schubert {
104785732ac8SCy Schubert 	u8 report_info, i;
104885732ac8SCy Schubert 
104985732ac8SCy Schubert 	switch (sid) {
105085732ac8SCy Schubert 	case WLAN_BEACON_REQUEST_SUBELEM_SSID:
105185732ac8SCy Schubert 		if (!slen) {
105285732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
105385732ac8SCy Schubert 				   "SSID subelement with zero length - wildcard SSID");
105485732ac8SCy Schubert 			break;
105585732ac8SCy Schubert 		}
105685732ac8SCy Schubert 
105785732ac8SCy Schubert 		if (slen > SSID_MAX_LEN) {
105885732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
105985732ac8SCy Schubert 				   "Invalid SSID subelement length: %u", slen);
106085732ac8SCy Schubert 			return -1;
106185732ac8SCy Schubert 		}
106285732ac8SCy Schubert 
106385732ac8SCy Schubert 		data->ssid_len = slen;
106485732ac8SCy Schubert 		os_memcpy(data->ssid, subelem, data->ssid_len);
106585732ac8SCy Schubert 		break;
106685732ac8SCy Schubert 	case WLAN_BEACON_REQUEST_SUBELEM_INFO:
106785732ac8SCy Schubert 		if (slen != 2) {
106885732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
106985732ac8SCy Schubert 				   "Invalid reporting information subelement length: %u",
107085732ac8SCy Schubert 				   slen);
107185732ac8SCy Schubert 			return -1;
107285732ac8SCy Schubert 		}
107385732ac8SCy Schubert 
107485732ac8SCy Schubert 		report_info = subelem[0];
107585732ac8SCy Schubert 		if (report_info != 0) {
107685732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
107785732ac8SCy Schubert 				   "reporting information=%u is not supported",
107885732ac8SCy Schubert 				   report_info);
107985732ac8SCy Schubert 			return 0;
108085732ac8SCy Schubert 		}
108185732ac8SCy Schubert 		break;
108285732ac8SCy Schubert 	case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
108385732ac8SCy Schubert 		if (slen != 1) {
108485732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
108585732ac8SCy Schubert 				   "Invalid reporting detail subelement length: %u",
108685732ac8SCy Schubert 				   slen);
108785732ac8SCy Schubert 			return -1;
108885732ac8SCy Schubert 		}
108985732ac8SCy Schubert 
109085732ac8SCy Schubert 		data->report_detail = subelem[0];
109185732ac8SCy Schubert 		if (data->report_detail >
109285732ac8SCy Schubert 		    BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
109385732ac8SCy Schubert 			wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
109485732ac8SCy Schubert 				   subelem[0]);
109585732ac8SCy Schubert 			return -1;
109685732ac8SCy Schubert 		}
109785732ac8SCy Schubert 
109885732ac8SCy Schubert 		break;
109985732ac8SCy Schubert 	case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
110085732ac8SCy Schubert 		if (data->report_detail !=
110185732ac8SCy Schubert 		    BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
110285732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
110385732ac8SCy Schubert 				   "Beacon request: request subelement is present but report detail is %u",
110485732ac8SCy Schubert 				   data->report_detail);
110585732ac8SCy Schubert 			return -1;
110685732ac8SCy Schubert 		}
110785732ac8SCy Schubert 
110885732ac8SCy Schubert 		if (!slen) {
110985732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
111085732ac8SCy Schubert 				   "Invalid request subelement length: %u",
111185732ac8SCy Schubert 				   slen);
111285732ac8SCy Schubert 			return -1;
111385732ac8SCy Schubert 		}
111485732ac8SCy Schubert 
111585732ac8SCy Schubert 		if (data->eids) {
111685732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
111785732ac8SCy Schubert 				   "Beacon Request: Request subelement appears more than once");
111885732ac8SCy Schubert 			return -1;
111985732ac8SCy Schubert 		}
112085732ac8SCy Schubert 
112185732ac8SCy Schubert 		data->eids = bitfield_alloc(255);
112285732ac8SCy Schubert 		if (!data->eids) {
112385732ac8SCy Schubert 			wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
112485732ac8SCy Schubert 			return -1;
112585732ac8SCy Schubert 		}
112685732ac8SCy Schubert 
112785732ac8SCy Schubert 		for (i = 0; i < slen; i++)
112885732ac8SCy Schubert 			bitfield_set(data->eids, subelem[i]);
112985732ac8SCy Schubert 		break;
113085732ac8SCy Schubert 	case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
113185732ac8SCy Schubert 		/* Skip - it will be processed when freqs are added */
113285732ac8SCy Schubert 		break;
11334bc52338SCy Schubert 	case WLAN_BEACON_REQUEST_SUBELEM_LAST_INDICATION:
11344bc52338SCy Schubert 		if (slen != 1) {
11354bc52338SCy Schubert 			wpa_printf(MSG_DEBUG,
11364bc52338SCy Schubert 				   "Beacon request: Invalid last indication request subelement length: %u",
11374bc52338SCy Schubert 				   slen);
11384bc52338SCy Schubert 			return -1;
11394bc52338SCy Schubert 		}
11404bc52338SCy Schubert 
11414bc52338SCy Schubert 		data->last_indication = subelem[0];
11424bc52338SCy Schubert 		break;
114385732ac8SCy Schubert 	default:
114485732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
114585732ac8SCy Schubert 			   "Beacon request: Unknown subelement id %u", sid);
114685732ac8SCy Schubert 		break;
114785732ac8SCy Schubert 	}
114885732ac8SCy Schubert 
114985732ac8SCy Schubert 	return 1;
115085732ac8SCy Schubert }
115185732ac8SCy Schubert 
115285732ac8SCy Schubert 
115385732ac8SCy Schubert /**
115485732ac8SCy Schubert  * Returns 0 if the next element can be processed, 1 if some operation was
115585732ac8SCy Schubert  * triggered, and -1 if processing failed (i.e., the element is in invalid
115685732ac8SCy Schubert  * format or an internal error occurred).
115785732ac8SCy Schubert  */
115885732ac8SCy Schubert static int
wpas_rm_handle_beacon_req(struct wpa_supplicant * wpa_s,u8 elem_token,int duration_mandatory,const struct rrm_measurement_beacon_request * req,size_t len,struct wpabuf ** buf)115985732ac8SCy Schubert wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
116085732ac8SCy Schubert 			  u8 elem_token, int duration_mandatory,
116185732ac8SCy Schubert 			  const struct rrm_measurement_beacon_request *req,
116285732ac8SCy Schubert 			  size_t len, struct wpabuf **buf)
116385732ac8SCy Schubert {
116485732ac8SCy Schubert 	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
116585732ac8SCy Schubert 	struct wpa_driver_scan_params *params = &data->scan_params;
116685732ac8SCy Schubert 	const u8 *subelems;
116785732ac8SCy Schubert 	size_t elems_len;
116885732ac8SCy Schubert 	u16 rand_interval;
116985732ac8SCy Schubert 	u32 interval_usec;
117085732ac8SCy Schubert 	u32 _rand;
117185732ac8SCy Schubert 	int ret = 0, res;
117285732ac8SCy Schubert 	u8 reject_mode;
117385732ac8SCy Schubert 
117485732ac8SCy Schubert 	if (len < sizeof(*req))
117585732ac8SCy Schubert 		return -1;
117685732ac8SCy Schubert 
117785732ac8SCy Schubert 	if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
117885732ac8SCy Schubert 	    req->mode != BEACON_REPORT_MODE_ACTIVE &&
117985732ac8SCy Schubert 	    req->mode != BEACON_REPORT_MODE_TABLE)
118085732ac8SCy Schubert 		return 0;
118185732ac8SCy Schubert 
118285732ac8SCy Schubert 	subelems = req->variable;
118385732ac8SCy Schubert 	elems_len = len - sizeof(*req);
118485732ac8SCy Schubert 	rand_interval = le_to_host16(req->rand_interval);
118585732ac8SCy Schubert 
118685732ac8SCy Schubert 	os_free(params->freqs);
118785732ac8SCy Schubert 	os_memset(params, 0, sizeof(*params));
118885732ac8SCy Schubert 
118985732ac8SCy Schubert 	data->token = elem_token;
119085732ac8SCy Schubert 
119185732ac8SCy Schubert 	/* default reporting detail is all fixed length fields and all
119285732ac8SCy Schubert 	 * elements */
119385732ac8SCy Schubert 	data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
119485732ac8SCy Schubert 	os_memcpy(data->bssid, req->bssid, ETH_ALEN);
119585732ac8SCy Schubert 
119685732ac8SCy Schubert 	while (elems_len >= 2) {
119785732ac8SCy Schubert 		if (subelems[1] > elems_len - 2) {
119885732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
119985732ac8SCy Schubert 				   "Beacon Request: Truncated subelement");
120085732ac8SCy Schubert 			ret = -1;
120185732ac8SCy Schubert 			goto out;
120285732ac8SCy Schubert 		}
120385732ac8SCy Schubert 
120485732ac8SCy Schubert 		res = wpas_rm_handle_beacon_req_subelem(
120585732ac8SCy Schubert 			wpa_s, data, subelems[0], subelems[1], &subelems[2]);
120685732ac8SCy Schubert 		if (res < 0) {
120785732ac8SCy Schubert 			ret = res;
120885732ac8SCy Schubert 			goto out;
120985732ac8SCy Schubert 		} else if (!res) {
121085732ac8SCy Schubert 			reject_mode = MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE;
121185732ac8SCy Schubert 			goto out_reject;
121285732ac8SCy Schubert 		}
121385732ac8SCy Schubert 
121485732ac8SCy Schubert 		elems_len -= 2 + subelems[1];
121585732ac8SCy Schubert 		subelems += 2 + subelems[1];
121685732ac8SCy Schubert 	}
121785732ac8SCy Schubert 
121885732ac8SCy Schubert 	if (req->mode == BEACON_REPORT_MODE_TABLE) {
121985732ac8SCy Schubert 		wpas_beacon_rep_table(wpa_s, buf);
122085732ac8SCy Schubert 		goto out;
122185732ac8SCy Schubert 	}
122285732ac8SCy Schubert 
122385732ac8SCy Schubert 	params->freqs = wpas_beacon_request_freqs(
122485732ac8SCy Schubert 		wpa_s, req->oper_class, req->channel,
122585732ac8SCy Schubert 		req->mode == BEACON_REPORT_MODE_ACTIVE,
122685732ac8SCy Schubert 		req->variable, len - sizeof(*req));
122785732ac8SCy Schubert 	if (!params->freqs) {
122885732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
122985732ac8SCy Schubert 		reject_mode = MEASUREMENT_REPORT_MODE_REJECT_REFUSED;
123085732ac8SCy Schubert 		goto out_reject;
123185732ac8SCy Schubert 	}
123285732ac8SCy Schubert 
123385732ac8SCy Schubert 	params->duration = le_to_host16(req->duration);
123485732ac8SCy Schubert 	params->duration_mandatory = duration_mandatory;
123585732ac8SCy Schubert 	if (!params->duration) {
123685732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
123785732ac8SCy Schubert 		ret = -1;
123885732ac8SCy Schubert 		goto out;
123985732ac8SCy Schubert 	}
124085732ac8SCy Schubert 
124185732ac8SCy Schubert 	params->only_new_results = 1;
124285732ac8SCy Schubert 
124385732ac8SCy Schubert 	if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
124485732ac8SCy Schubert 		params->ssids[params->num_ssids].ssid = data->ssid;
124585732ac8SCy Schubert 		params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
124685732ac8SCy Schubert 	}
124785732ac8SCy Schubert 
124885732ac8SCy Schubert 	if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
124985732ac8SCy Schubert 		_rand = os_random();
125085732ac8SCy Schubert 	interval_usec = (_rand % (rand_interval + 1)) * 1024;
125185732ac8SCy Schubert 	eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
125285732ac8SCy Schubert 			       NULL);
125385732ac8SCy Schubert 	return 1;
125485732ac8SCy Schubert out_reject:
125585732ac8SCy Schubert 	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
125685732ac8SCy Schubert 	    wpas_rrm_report_elem(buf, elem_token, reject_mode,
125785732ac8SCy Schubert 				 MEASURE_TYPE_BEACON, NULL, 0) < 0) {
125885732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
125985732ac8SCy Schubert 		ret = -1;
126085732ac8SCy Schubert 	}
126185732ac8SCy Schubert out:
126285732ac8SCy Schubert 	wpas_clear_beacon_rep_data(wpa_s);
126385732ac8SCy Schubert 	return ret;
126485732ac8SCy Schubert }
126585732ac8SCy Schubert 
126685732ac8SCy Schubert 
126785732ac8SCy Schubert static int
wpas_rrm_handle_msr_req_element(struct wpa_supplicant * wpa_s,const struct rrm_measurement_request_element * req,struct wpabuf ** buf)126885732ac8SCy Schubert wpas_rrm_handle_msr_req_element(
126985732ac8SCy Schubert 	struct wpa_supplicant *wpa_s,
127085732ac8SCy Schubert 	const struct rrm_measurement_request_element *req,
127185732ac8SCy Schubert 	struct wpabuf **buf)
127285732ac8SCy Schubert {
127385732ac8SCy Schubert 	int duration_mandatory;
127485732ac8SCy Schubert 
127585732ac8SCy Schubert 	wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
127685732ac8SCy Schubert 		   req->type, req->token);
127785732ac8SCy Schubert 
127885732ac8SCy Schubert 	if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
127985732ac8SCy Schubert 		/* Enable bit is not supported for now */
128085732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
128185732ac8SCy Schubert 		return 0;
128285732ac8SCy Schubert 	}
128385732ac8SCy Schubert 
128485732ac8SCy Schubert 	if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
128585732ac8SCy Schubert 	    req->type > MEASURE_TYPE_RPI_HIST) {
128685732ac8SCy Schubert 		/* Parallel measurements are not supported for now */
128785732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
128885732ac8SCy Schubert 			   "RRM: Parallel measurements are not supported, reject");
128985732ac8SCy Schubert 		goto reject;
129085732ac8SCy Schubert 	}
129185732ac8SCy Schubert 
129285732ac8SCy Schubert 	duration_mandatory =
129385732ac8SCy Schubert 		!!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
129485732ac8SCy Schubert 
129585732ac8SCy Schubert 	switch (req->type) {
129685732ac8SCy Schubert 	case MEASURE_TYPE_LCI:
129785732ac8SCy Schubert 		return wpas_rrm_build_lci_report(wpa_s, req, buf);
129885732ac8SCy Schubert 	case MEASURE_TYPE_BEACON:
129985732ac8SCy Schubert 		if (duration_mandatory &&
130085732ac8SCy Schubert 		    !(wpa_s->drv_rrm_flags &
130185732ac8SCy Schubert 		      WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
130285732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
130385732ac8SCy Schubert 				   "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
130485732ac8SCy Schubert 			goto reject;
130585732ac8SCy Schubert 		}
130685732ac8SCy Schubert 		return wpas_rm_handle_beacon_req(wpa_s, req->token,
130785732ac8SCy Schubert 						 duration_mandatory,
130885732ac8SCy Schubert 						 (const void *) req->variable,
130985732ac8SCy Schubert 						 req->len - 3, buf);
131085732ac8SCy Schubert 	default:
131185732ac8SCy Schubert 		wpa_printf(MSG_INFO,
131285732ac8SCy Schubert 			   "RRM: Unsupported radio measurement type %u",
131385732ac8SCy Schubert 			   req->type);
131485732ac8SCy Schubert 		break;
131585732ac8SCy Schubert 	}
131685732ac8SCy Schubert 
131785732ac8SCy Schubert reject:
131885732ac8SCy Schubert 	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
131985732ac8SCy Schubert 	    wpas_rrm_report_elem(buf, req->token,
132085732ac8SCy Schubert 				 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
132185732ac8SCy Schubert 				 req->type, NULL, 0) < 0) {
132285732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
132385732ac8SCy Schubert 		return -1;
132485732ac8SCy Schubert 	}
132585732ac8SCy Schubert 
132685732ac8SCy Schubert 	return 0;
132785732ac8SCy Schubert }
132885732ac8SCy Schubert 
132985732ac8SCy Schubert 
133085732ac8SCy Schubert static struct wpabuf *
wpas_rrm_process_msr_req_elems(struct wpa_supplicant * wpa_s,const u8 * pos,size_t len)133185732ac8SCy Schubert wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
133285732ac8SCy Schubert 			       size_t len)
133385732ac8SCy Schubert {
133485732ac8SCy Schubert 	struct wpabuf *buf = NULL;
133585732ac8SCy Schubert 
133685732ac8SCy Schubert 	while (len) {
133785732ac8SCy Schubert 		const struct rrm_measurement_request_element *req;
133885732ac8SCy Schubert 		int res;
133985732ac8SCy Schubert 
134085732ac8SCy Schubert 		if (len < 2) {
134185732ac8SCy Schubert 			wpa_printf(MSG_DEBUG, "RRM: Truncated element");
134285732ac8SCy Schubert 			goto out;
134385732ac8SCy Schubert 		}
134485732ac8SCy Schubert 
134585732ac8SCy Schubert 		req = (const struct rrm_measurement_request_element *) pos;
134685732ac8SCy Schubert 		if (req->eid != WLAN_EID_MEASURE_REQUEST) {
134785732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
134885732ac8SCy Schubert 				   "RRM: Expected Measurement Request element, but EID is %u",
134985732ac8SCy Schubert 				   req->eid);
135085732ac8SCy Schubert 			goto out;
135185732ac8SCy Schubert 		}
135285732ac8SCy Schubert 
135385732ac8SCy Schubert 		if (req->len < 3) {
135485732ac8SCy Schubert 			wpa_printf(MSG_DEBUG, "RRM: Element length too short");
135585732ac8SCy Schubert 			goto out;
135685732ac8SCy Schubert 		}
135785732ac8SCy Schubert 
135885732ac8SCy Schubert 		if (req->len > len - 2) {
135985732ac8SCy Schubert 			wpa_printf(MSG_DEBUG, "RRM: Element length too long");
136085732ac8SCy Schubert 			goto out;
136185732ac8SCy Schubert 		}
136285732ac8SCy Schubert 
136385732ac8SCy Schubert 		res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
136485732ac8SCy Schubert 		if (res < 0)
136585732ac8SCy Schubert 			goto out;
136685732ac8SCy Schubert 
136785732ac8SCy Schubert 		pos += req->len + 2;
136885732ac8SCy Schubert 		len -= req->len + 2;
136985732ac8SCy Schubert 	}
137085732ac8SCy Schubert 
137185732ac8SCy Schubert 	return buf;
137285732ac8SCy Schubert 
137385732ac8SCy Schubert out:
137485732ac8SCy Schubert 	wpabuf_free(buf);
137585732ac8SCy Schubert 	return NULL;
137685732ac8SCy Schubert }
137785732ac8SCy Schubert 
137885732ac8SCy Schubert 
wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant * wpa_s,const u8 * src,const u8 * dst,const u8 * frame,size_t len)137985732ac8SCy Schubert void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
138085732ac8SCy Schubert 					       const u8 *src, const u8 *dst,
138185732ac8SCy Schubert 					       const u8 *frame, size_t len)
138285732ac8SCy Schubert {
138385732ac8SCy Schubert 	struct wpabuf *report;
138485732ac8SCy Schubert 
138585732ac8SCy Schubert 	if (wpa_s->wpa_state != WPA_COMPLETED) {
138685732ac8SCy Schubert 		wpa_printf(MSG_INFO,
138785732ac8SCy Schubert 			   "RRM: Ignoring radio measurement request: Not associated");
138885732ac8SCy Schubert 		return;
138985732ac8SCy Schubert 	}
139085732ac8SCy Schubert 
139185732ac8SCy Schubert 	if (!wpa_s->rrm.rrm_used) {
139285732ac8SCy Schubert 		wpa_printf(MSG_INFO,
139385732ac8SCy Schubert 			   "RRM: Ignoring radio measurement request: Not RRM network");
139485732ac8SCy Schubert 		return;
139585732ac8SCy Schubert 	}
139685732ac8SCy Schubert 
139785732ac8SCy Schubert 	if (len < 3) {
139885732ac8SCy Schubert 		wpa_printf(MSG_INFO,
139985732ac8SCy Schubert 			   "RRM: Ignoring too short radio measurement request");
140085732ac8SCy Schubert 		return;
140185732ac8SCy Schubert 	}
140285732ac8SCy Schubert 
140385732ac8SCy Schubert 	wpa_s->rrm.token = *frame;
140485732ac8SCy Schubert 	os_memcpy(wpa_s->rrm.dst_addr, dst, ETH_ALEN);
140585732ac8SCy Schubert 
140685732ac8SCy Schubert 	/* Number of repetitions is not supported */
140785732ac8SCy Schubert 
140885732ac8SCy Schubert 	report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
140985732ac8SCy Schubert 	if (!report)
141085732ac8SCy Schubert 		return;
141185732ac8SCy Schubert 
141285732ac8SCy Schubert 	wpas_rrm_send_msr_report(wpa_s, report);
141385732ac8SCy Schubert 	wpabuf_free(report);
141485732ac8SCy Schubert }
141585732ac8SCy Schubert 
141685732ac8SCy Schubert 
wpas_rrm_handle_link_measurement_request(struct wpa_supplicant * wpa_s,const u8 * src,const u8 * frame,size_t len,int rssi)141785732ac8SCy Schubert void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
141885732ac8SCy Schubert 					      const u8 *src,
141985732ac8SCy Schubert 					      const u8 *frame, size_t len,
142085732ac8SCy Schubert 					      int rssi)
142185732ac8SCy Schubert {
142285732ac8SCy Schubert 	struct wpabuf *buf;
142385732ac8SCy Schubert 	const struct rrm_link_measurement_request *req;
142485732ac8SCy Schubert 	struct rrm_link_measurement_report report;
142585732ac8SCy Schubert 
142685732ac8SCy Schubert 	if (wpa_s->wpa_state != WPA_COMPLETED) {
142785732ac8SCy Schubert 		wpa_printf(MSG_INFO,
142885732ac8SCy Schubert 			   "RRM: Ignoring link measurement request. Not associated");
142985732ac8SCy Schubert 		return;
143085732ac8SCy Schubert 	}
143185732ac8SCy Schubert 
143285732ac8SCy Schubert 	if (!wpa_s->rrm.rrm_used) {
143385732ac8SCy Schubert 		wpa_printf(MSG_INFO,
143485732ac8SCy Schubert 			   "RRM: Ignoring link measurement request. Not RRM network");
143585732ac8SCy Schubert 		return;
143685732ac8SCy Schubert 	}
143785732ac8SCy Schubert 
143885732ac8SCy Schubert 	if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
143985732ac8SCy Schubert 		wpa_printf(MSG_INFO,
144085732ac8SCy Schubert 			   "RRM: Measurement report failed. TX power insertion not supported");
144185732ac8SCy Schubert 		return;
144285732ac8SCy Schubert 	}
144385732ac8SCy Schubert 
144485732ac8SCy Schubert 	req = (const struct rrm_link_measurement_request *) frame;
144585732ac8SCy Schubert 	if (len < sizeof(*req)) {
144685732ac8SCy Schubert 		wpa_printf(MSG_INFO,
144785732ac8SCy Schubert 			   "RRM: Link measurement report failed. Request too short");
144885732ac8SCy Schubert 		return;
144985732ac8SCy Schubert 	}
145085732ac8SCy Schubert 
145185732ac8SCy Schubert 	os_memset(&report, 0, sizeof(report));
145285732ac8SCy Schubert 	report.dialog_token = req->dialog_token;
145385732ac8SCy Schubert 	report.tpc.eid = WLAN_EID_TPC_REPORT;
145485732ac8SCy Schubert 	report.tpc.len = 2;
145585732ac8SCy Schubert 	/* Note: The driver is expected to update report.tpc.tx_power and
145685732ac8SCy Schubert 	 * report.tpc.link_margin subfields when sending out this frame.
145785732ac8SCy Schubert 	 * Similarly, the driver would need to update report.rx_ant_id and
145885732ac8SCy Schubert 	 * report.tx_ant_id subfields. */
145985732ac8SCy Schubert 	report.rsni = 255; /* 255 indicates that RSNI is not available */
146085732ac8SCy Schubert 	report.rcpi = rssi_to_rcpi(rssi);
146185732ac8SCy Schubert 
146285732ac8SCy Schubert 	/* action_category + action_code */
146385732ac8SCy Schubert 	buf = wpabuf_alloc(2 + sizeof(report));
146485732ac8SCy Schubert 	if (buf == NULL) {
146585732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
146685732ac8SCy Schubert 			   "RRM: Link measurement report failed. Buffer allocation failed");
146785732ac8SCy Schubert 		return;
146885732ac8SCy Schubert 	}
146985732ac8SCy Schubert 
147085732ac8SCy Schubert 	wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
147185732ac8SCy Schubert 	wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
147285732ac8SCy Schubert 	wpabuf_put_data(buf, &report, sizeof(report));
147385732ac8SCy Schubert 	wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
147485732ac8SCy Schubert 
147585732ac8SCy Schubert 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
147685732ac8SCy Schubert 				wpa_s->own_addr, wpa_s->bssid,
147785732ac8SCy Schubert 				wpabuf_head(buf), wpabuf_len(buf), 0)) {
147885732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
147985732ac8SCy Schubert 			   "RRM: Link measurement report failed. Send action failed");
148085732ac8SCy Schubert 	}
148185732ac8SCy Schubert 	wpabuf_free(buf);
148285732ac8SCy Schubert }
148385732ac8SCy Schubert 
148485732ac8SCy Schubert 
wpas_beacon_rep_scan_process(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res,struct scan_info * info)148585732ac8SCy Schubert int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
148685732ac8SCy Schubert 				 struct wpa_scan_results *scan_res,
148785732ac8SCy Schubert 				 struct scan_info *info)
148885732ac8SCy Schubert {
148985732ac8SCy Schubert 	size_t i = 0;
149085732ac8SCy Schubert 	struct wpabuf *buf = NULL;
149185732ac8SCy Schubert 
149285732ac8SCy Schubert 	if (!wpa_s->beacon_rep_data.token)
149385732ac8SCy Schubert 		return 0;
149485732ac8SCy Schubert 
149585732ac8SCy Schubert 	if (!wpa_s->current_bss)
149685732ac8SCy Schubert 		goto out;
149785732ac8SCy Schubert 
149885732ac8SCy Schubert 	/* If the measurement was aborted, don't report partial results */
149985732ac8SCy Schubert 	if (info->aborted)
150085732ac8SCy Schubert 		goto out;
150185732ac8SCy Schubert 
150285732ac8SCy Schubert 	wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
150385732ac8SCy Schubert 		   MAC2STR(info->scan_start_tsf_bssid),
150485732ac8SCy Schubert 		   MAC2STR(wpa_s->current_bss->bssid));
150585732ac8SCy Schubert 	if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
150685732ac8SCy Schubert 	    os_memcmp(info->scan_start_tsf_bssid, wpa_s->current_bss->bssid,
150785732ac8SCy Schubert 		      ETH_ALEN) != 0) {
150885732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
150985732ac8SCy Schubert 			   "RRM: Ignore scan results due to mismatching TSF BSSID");
151085732ac8SCy Schubert 		goto out;
151185732ac8SCy Schubert 	}
151285732ac8SCy Schubert 
151385732ac8SCy Schubert 	for (i = 0; i < scan_res->num; i++) {
151485732ac8SCy Schubert 		struct wpa_bss *bss =
151585732ac8SCy Schubert 			wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
151685732ac8SCy Schubert 
151785732ac8SCy Schubert 		if (!bss)
151885732ac8SCy Schubert 			continue;
151985732ac8SCy Schubert 
152085732ac8SCy Schubert 		if ((wpa_s->drv_rrm_flags &
152185732ac8SCy Schubert 		     WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
152285732ac8SCy Schubert 		    os_memcmp(scan_res->res[i]->tsf_bssid,
152385732ac8SCy Schubert 			      wpa_s->current_bss->bssid, ETH_ALEN) != 0) {
152485732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
152585732ac8SCy Schubert 				   "RRM: Ignore scan result for " MACSTR
152685732ac8SCy Schubert 				   " due to mismatching TSF BSSID" MACSTR,
152785732ac8SCy Schubert 				   MAC2STR(scan_res->res[i]->bssid),
152885732ac8SCy Schubert 				   MAC2STR(scan_res->res[i]->tsf_bssid));
152985732ac8SCy Schubert 			continue;
153085732ac8SCy Schubert 		}
153185732ac8SCy Schubert 
153285732ac8SCy Schubert 		/*
153385732ac8SCy Schubert 		 * Don't report results that were not received during the
153485732ac8SCy Schubert 		 * current measurement.
153585732ac8SCy Schubert 		 */
153685732ac8SCy Schubert 		if (!(wpa_s->drv_rrm_flags &
153785732ac8SCy Schubert 		      WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
153885732ac8SCy Schubert 			struct os_reltime update_time, diff;
153985732ac8SCy Schubert 
154085732ac8SCy Schubert 			/* For now, allow 8 ms older results due to some
154185732ac8SCy Schubert 			 * unknown issue with cfg80211 BSS table updates during
154285732ac8SCy Schubert 			 * a scan with the current BSS.
154385732ac8SCy Schubert 			 * TODO: Fix this more properly to avoid having to have
154485732ac8SCy Schubert 			 * this type of hacks in place. */
154585732ac8SCy Schubert 			calculate_update_time(&scan_res->fetch_time,
154685732ac8SCy Schubert 					      scan_res->res[i]->age,
154785732ac8SCy Schubert 					      &update_time);
154885732ac8SCy Schubert 			os_reltime_sub(&wpa_s->beacon_rep_scan,
154985732ac8SCy Schubert 				       &update_time, &diff);
155085732ac8SCy Schubert 			if (os_reltime_before(&update_time,
155185732ac8SCy Schubert 					      &wpa_s->beacon_rep_scan) &&
155285732ac8SCy Schubert 			    (diff.sec || diff.usec >= 8000)) {
155385732ac8SCy Schubert 				wpa_printf(MSG_DEBUG,
155485732ac8SCy Schubert 					   "RRM: Ignore scan result for " MACSTR
155585732ac8SCy Schubert 					   " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
155685732ac8SCy Schubert 					   MAC2STR(scan_res->res[i]->bssid),
155785732ac8SCy Schubert 					   scan_res->res[i]->age,
155885732ac8SCy Schubert 					   (unsigned int) diff.sec,
155985732ac8SCy Schubert 					   (unsigned int) diff.usec);
156085732ac8SCy Schubert 				continue;
156185732ac8SCy Schubert 			}
156285732ac8SCy Schubert 		} else if (info->scan_start_tsf >
156385732ac8SCy Schubert 			   scan_res->res[i]->parent_tsf) {
156485732ac8SCy Schubert 			continue;
156585732ac8SCy Schubert 		}
156685732ac8SCy Schubert 
156785732ac8SCy Schubert 		if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
156885732ac8SCy Schubert 					scan_res->res[i]->parent_tsf) < 0)
156985732ac8SCy Schubert 			break;
157085732ac8SCy Schubert 	}
157185732ac8SCy Schubert 
157285732ac8SCy Schubert 	if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
157385732ac8SCy Schubert 		goto out;
157485732ac8SCy Schubert 
157585732ac8SCy Schubert 	wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
157685732ac8SCy Schubert 
157785732ac8SCy Schubert 	wpas_rrm_send_msr_report(wpa_s, buf);
157885732ac8SCy Schubert 	wpabuf_free(buf);
157985732ac8SCy Schubert 
158085732ac8SCy Schubert out:
158185732ac8SCy Schubert 	wpas_clear_beacon_rep_data(wpa_s);
158285732ac8SCy Schubert 	return 1;
158385732ac8SCy Schubert }
158485732ac8SCy Schubert 
158585732ac8SCy Schubert 
wpas_clear_beacon_rep_data(struct wpa_supplicant * wpa_s)158685732ac8SCy Schubert void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
158785732ac8SCy Schubert {
158885732ac8SCy Schubert 	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
158985732ac8SCy Schubert 
159085732ac8SCy Schubert 	eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
159185732ac8SCy Schubert 	bitfield_free(data->eids);
159285732ac8SCy Schubert 	os_free(data->scan_params.freqs);
159385732ac8SCy Schubert 	os_memset(data, 0, sizeof(*data));
159485732ac8SCy Schubert }
1595