1341618Scy/*
2341618Scy * wpa_supplicant - Radio Measurements
3341618Scy * Copyright (c) 2003-2016, Jouni Malinen <j@w1.fi>
4341618Scy *
5341618Scy * This software may be distributed under the terms of the BSD license.
6341618Scy * See README for more details.
7341618Scy */
8341618Scy
9341618Scy#include "includes.h"
10341618Scy
11341618Scy#include "utils/common.h"
12341618Scy#include "utils/eloop.h"
13341618Scy#include "common/ieee802_11_common.h"
14341618Scy#include "wpa_supplicant_i.h"
15341618Scy#include "driver_i.h"
16341618Scy#include "bss.h"
17341618Scy#include "scan.h"
18341618Scy#include "p2p_supplicant.h"
19341618Scy
20341618Scy
21341618Scystatic void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
22341618Scy{
23341618Scy	struct rrm_data *rrm = data;
24341618Scy
25341618Scy	if (!rrm->notify_neighbor_rep) {
26341618Scy		wpa_printf(MSG_ERROR,
27341618Scy			   "RRM: Unexpected neighbor report timeout");
28341618Scy		return;
29341618Scy	}
30341618Scy
31341618Scy	wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
32341618Scy	rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
33341618Scy
34341618Scy	rrm->notify_neighbor_rep = NULL;
35341618Scy	rrm->neighbor_rep_cb_ctx = NULL;
36341618Scy}
37341618Scy
38341618Scy
39341618Scy/*
40341618Scy * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
41341618Scy * @wpa_s: Pointer to wpa_supplicant
42341618Scy */
43341618Scyvoid wpas_rrm_reset(struct wpa_supplicant *wpa_s)
44341618Scy{
45341618Scy	wpa_s->rrm.rrm_used = 0;
46341618Scy
47341618Scy	eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
48341618Scy			     NULL);
49341618Scy	if (wpa_s->rrm.notify_neighbor_rep)
50341618Scy		wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
51341618Scy	wpa_s->rrm.next_neighbor_rep_token = 1;
52341618Scy	wpas_clear_beacon_rep_data(wpa_s);
53341618Scy}
54341618Scy
55341618Scy
56341618Scy/*
57341618Scy * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
58341618Scy * @wpa_s: Pointer to wpa_supplicant
59341618Scy * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
60341618Scy * @report_len: Length of neighbor report buffer
61341618Scy */
62341618Scyvoid wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
63341618Scy				   const u8 *report, size_t report_len)
64341618Scy{
65341618Scy	struct wpabuf *neighbor_rep;
66341618Scy
67341618Scy	wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
68341618Scy	if (report_len < 1)
69341618Scy		return;
70341618Scy
71341618Scy	if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
72341618Scy		wpa_printf(MSG_DEBUG,
73341618Scy			   "RRM: Discarding neighbor report with token %d (expected %d)",
74341618Scy			   report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
75341618Scy		return;
76341618Scy	}
77341618Scy
78341618Scy	eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
79341618Scy			     NULL);
80341618Scy
81341618Scy	if (!wpa_s->rrm.notify_neighbor_rep) {
82341618Scy		wpa_printf(MSG_ERROR, "RRM: Unexpected neighbor report");
83341618Scy		return;
84341618Scy	}
85341618Scy
86341618Scy	/* skipping the first byte, which is only an id (dialog token) */
87341618Scy	neighbor_rep = wpabuf_alloc(report_len - 1);
88341618Scy	if (!neighbor_rep) {
89341618Scy		wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
90341618Scy		return;
91341618Scy	}
92341618Scy	wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
93341618Scy	wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
94341618Scy		   report[0]);
95341618Scy	wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
96341618Scy				       neighbor_rep);
97341618Scy	wpa_s->rrm.notify_neighbor_rep = NULL;
98341618Scy	wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
99341618Scy}
100341618Scy
101341618Scy
102341618Scy#if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
103341618Scy/* Workaround different, undefined for Windows, error codes used here */
104341618Scy#define ENOTCONN -1
105341618Scy#define EOPNOTSUPP -1
106341618Scy#define ECANCELED -1
107341618Scy#endif
108341618Scy
109341618Scy/* Measurement Request element + Location Subject + Maximum Age subelement */
110341618Scy#define MEASURE_REQUEST_LCI_LEN (3 + 1 + 4)
111341618Scy/* Measurement Request element + Location Civic Request */
112341618Scy#define MEASURE_REQUEST_CIVIC_LEN (3 + 5)
113341618Scy
114341618Scy
115341618Scy/**
116341618Scy * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
117341618Scy * @wpa_s: Pointer to wpa_supplicant
118341618Scy * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
119341618Scy *	  is sent in the request.
120341618Scy * @lci: if set, neighbor request will include LCI request
121341618Scy * @civic: if set, neighbor request will include civic location request
122341618Scy * @cb: Callback function to be called once the requested report arrives, or
123341618Scy *	timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
124341618Scy *	In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
125341618Scy *	the requester's responsibility to free it.
126341618Scy *	In the latter case NULL will be sent in 'neighbor_rep'.
127341618Scy * @cb_ctx: Context value to send the callback function
128341618Scy * Returns: 0 in case of success, negative error code otherwise
129341618Scy *
130341618Scy * In case there is a previous request which has not been answered yet, the
131341618Scy * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
132341618Scy * Request must contain a callback function.
133341618Scy */
134341618Scyint wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
135341618Scy				       const struct wpa_ssid_value *ssid,
136341618Scy				       int lci, int civic,
137341618Scy				       void (*cb)(void *ctx,
138341618Scy						  struct wpabuf *neighbor_rep),
139341618Scy				       void *cb_ctx)
140341618Scy{
141341618Scy	struct wpabuf *buf;
142341618Scy	const u8 *rrm_ie;
143341618Scy
144341618Scy	if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
145341618Scy		wpa_printf(MSG_DEBUG, "RRM: No connection, no RRM.");
146341618Scy		return -ENOTCONN;
147341618Scy	}
148341618Scy
149341618Scy	if (!wpa_s->rrm.rrm_used) {
150341618Scy		wpa_printf(MSG_DEBUG, "RRM: No RRM in current connection.");
151341618Scy		return -EOPNOTSUPP;
152341618Scy	}
153341618Scy
154341618Scy	rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
155341618Scy				WLAN_EID_RRM_ENABLED_CAPABILITIES);
156341618Scy	if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
157341618Scy	    !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
158341618Scy		wpa_printf(MSG_DEBUG,
159341618Scy			   "RRM: No network support for Neighbor Report.");
160341618Scy		return -EOPNOTSUPP;
161341618Scy	}
162341618Scy
163341618Scy	/* Refuse if there's a live request */
164341618Scy	if (wpa_s->rrm.notify_neighbor_rep) {
165341618Scy		wpa_printf(MSG_DEBUG,
166341618Scy			   "RRM: Currently handling previous Neighbor Report.");
167341618Scy		return -EBUSY;
168341618Scy	}
169341618Scy
170341618Scy	/* 3 = action category + action code + dialog token */
171341618Scy	buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0) +
172341618Scy			   (lci ? 2 + MEASURE_REQUEST_LCI_LEN : 0) +
173341618Scy			   (civic ? 2 + MEASURE_REQUEST_CIVIC_LEN : 0));
174341618Scy	if (buf == NULL) {
175341618Scy		wpa_printf(MSG_DEBUG,
176341618Scy			   "RRM: Failed to allocate Neighbor Report Request");
177341618Scy		return -ENOMEM;
178341618Scy	}
179341618Scy
180341618Scy	wpa_printf(MSG_DEBUG, "RRM: Neighbor report request (for %s), token=%d",
181341618Scy		   (ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
182341618Scy		   wpa_s->rrm.next_neighbor_rep_token);
183341618Scy
184341618Scy	wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
185341618Scy	wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
186341618Scy	wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
187341618Scy	if (ssid) {
188341618Scy		wpabuf_put_u8(buf, WLAN_EID_SSID);
189341618Scy		wpabuf_put_u8(buf, ssid->ssid_len);
190341618Scy		wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
191341618Scy	}
192341618Scy
193341618Scy	if (lci) {
194341618Scy		/* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
195341618Scy		wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
196341618Scy		wpabuf_put_u8(buf, MEASURE_REQUEST_LCI_LEN);
197341618Scy
198341618Scy		/*
199341618Scy		 * Measurement token; nonzero number that is unique among the
200341618Scy		 * Measurement Request elements in a particular frame.
201341618Scy		 */
202341618Scy		wpabuf_put_u8(buf, 1); /* Measurement Token */
203341618Scy
204341618Scy		/*
205341618Scy		 * Parallel, Enable, Request, and Report bits are 0, Duration is
206341618Scy		 * reserved.
207341618Scy		 */
208341618Scy		wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
209341618Scy		wpabuf_put_u8(buf, MEASURE_TYPE_LCI); /* Measurement Type */
210341618Scy
211341618Scy		/* IEEE P802.11-REVmc/D5.0 9.4.2.21.10 - LCI request */
212341618Scy		/* Location Subject */
213341618Scy		wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
214341618Scy
215341618Scy		/* Optional Subelements */
216341618Scy		/*
217341618Scy		 * IEEE P802.11-REVmc/D5.0 Figure 9-170
218341618Scy		 * The Maximum Age subelement is required, otherwise the AP can
219341618Scy		 * send only data that was determined after receiving the
220341618Scy		 * request. Setting it here to unlimited age.
221341618Scy		 */
222341618Scy		wpabuf_put_u8(buf, LCI_REQ_SUBELEM_MAX_AGE);
223341618Scy		wpabuf_put_u8(buf, 2);
224341618Scy		wpabuf_put_le16(buf, 0xffff);
225341618Scy	}
226341618Scy
227341618Scy	if (civic) {
228341618Scy		/* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
229341618Scy		wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
230341618Scy		wpabuf_put_u8(buf, MEASURE_REQUEST_CIVIC_LEN);
231341618Scy
232341618Scy		/*
233341618Scy		 * Measurement token; nonzero number that is unique among the
234341618Scy		 * Measurement Request elements in a particular frame.
235341618Scy		 */
236341618Scy		wpabuf_put_u8(buf, 2); /* Measurement Token */
237341618Scy
238341618Scy		/*
239341618Scy		 * Parallel, Enable, Request, and Report bits are 0, Duration is
240341618Scy		 * reserved.
241341618Scy		 */
242341618Scy		wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
243341618Scy		/* Measurement Type */
244341618Scy		wpabuf_put_u8(buf, MEASURE_TYPE_LOCATION_CIVIC);
245341618Scy
246341618Scy		/* IEEE P802.11-REVmc/D5.0 9.4.2.21.14:
247341618Scy		 * Location Civic request */
248341618Scy		/* Location Subject */
249341618Scy		wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
250341618Scy		wpabuf_put_u8(buf, 0); /* Civic Location Type: IETF RFC 4776 */
251341618Scy		/* Location Service Interval Units: Seconds */
252341618Scy		wpabuf_put_u8(buf, 0);
253341618Scy		/* Location Service Interval: 0 - Only one report is requested
254341618Scy		 */
255341618Scy		wpabuf_put_le16(buf, 0);
256341618Scy		/* No optional subelements */
257341618Scy	}
258341618Scy
259341618Scy	wpa_s->rrm.next_neighbor_rep_token++;
260341618Scy
261341618Scy	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
262341618Scy				wpa_s->own_addr, wpa_s->bssid,
263341618Scy				wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
264341618Scy		wpa_printf(MSG_DEBUG,
265341618Scy			   "RRM: Failed to send Neighbor Report Request");
266341618Scy		wpabuf_free(buf);
267341618Scy		return -ECANCELED;
268341618Scy	}
269341618Scy
270341618Scy	wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
271341618Scy	wpa_s->rrm.notify_neighbor_rep = cb;
272341618Scy	eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
273341618Scy			       wpas_rrm_neighbor_rep_timeout_handler,
274341618Scy			       &wpa_s->rrm, NULL);
275341618Scy
276341618Scy	wpabuf_free(buf);
277341618Scy	return 0;
278341618Scy}
279341618Scy
280341618Scy
281341618Scystatic int wpas_rrm_report_elem(struct wpabuf **buf, u8 token, u8 mode, u8 type,
282341618Scy				const u8 *data, size_t data_len)
283341618Scy{
284341618Scy	if (wpabuf_resize(buf, 5 + data_len))
285341618Scy		return -1;
286341618Scy
287341618Scy	wpabuf_put_u8(*buf, WLAN_EID_MEASURE_REPORT);
288341618Scy	wpabuf_put_u8(*buf, 3 + data_len);
289341618Scy	wpabuf_put_u8(*buf, token);
290341618Scy	wpabuf_put_u8(*buf, mode);
291341618Scy	wpabuf_put_u8(*buf, type);
292341618Scy
293341618Scy	if (data_len)
294341618Scy		wpabuf_put_data(*buf, data, data_len);
295341618Scy
296341618Scy	return 0;
297341618Scy}
298341618Scy
299341618Scy
300341618Scystatic int
301341618Scywpas_rrm_build_lci_report(struct wpa_supplicant *wpa_s,
302341618Scy			  const struct rrm_measurement_request_element *req,
303341618Scy			  struct wpabuf **buf)
304341618Scy{
305341618Scy	u8 subject;
306341618Scy	u16 max_age = 0;
307341618Scy	struct os_reltime t, diff;
308341618Scy	unsigned long diff_l;
309341618Scy	const u8 *subelem;
310341618Scy	const u8 *request = req->variable;
311341618Scy	size_t len = req->len - 3;
312341618Scy
313341618Scy	if (len < 1)
314341618Scy		return -1;
315341618Scy
316341618Scy	if (!wpa_s->lci)
317341618Scy		goto reject;
318341618Scy
319341618Scy	subject = *request++;
320341618Scy	len--;
321341618Scy
322341618Scy	wpa_printf(MSG_DEBUG, "Measurement request location subject=%u",
323341618Scy		   subject);
324341618Scy
325341618Scy	if (subject != LOCATION_SUBJECT_REMOTE) {
326341618Scy		wpa_printf(MSG_INFO,
327341618Scy			   "Not building LCI report - bad location subject");
328341618Scy		return 0;
329341618Scy	}
330341618Scy
331341618Scy	/* Subelements are formatted exactly like elements */
332341618Scy	wpa_hexdump(MSG_DEBUG, "LCI request subelements", request, len);
333341618Scy	subelem = get_ie(request, len, LCI_REQ_SUBELEM_MAX_AGE);
334341618Scy	if (subelem && subelem[1] == 2)
335341618Scy		max_age = WPA_GET_LE16(subelem + 2);
336341618Scy
337341618Scy	if (os_get_reltime(&t))
338341618Scy		goto reject;
339341618Scy
340341618Scy	os_reltime_sub(&t, &wpa_s->lci_time, &diff);
341341618Scy	/* LCI age is calculated in 10th of a second units. */
342341618Scy	diff_l = diff.sec * 10 + diff.usec / 100000;
343341618Scy
344341618Scy	if (max_age != 0xffff && max_age < diff_l)
345341618Scy		goto reject;
346341618Scy
347341618Scy	if (wpas_rrm_report_elem(buf, req->token,
348341618Scy				 MEASUREMENT_REPORT_MODE_ACCEPT, req->type,
349341618Scy				 wpabuf_head_u8(wpa_s->lci),
350341618Scy				 wpabuf_len(wpa_s->lci)) < 0) {
351341618Scy		wpa_printf(MSG_DEBUG, "Failed to add LCI report element");
352341618Scy		return -1;
353341618Scy	}
354341618Scy
355341618Scy	return 0;
356341618Scy
357341618Scyreject:
358341618Scy	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
359341618Scy	    wpas_rrm_report_elem(buf, req->token,
360341618Scy				 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
361341618Scy				 req->type, NULL, 0) < 0) {
362341618Scy		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
363341618Scy		return -1;
364341618Scy	}
365341618Scy
366341618Scy	return 0;
367341618Scy}
368341618Scy
369341618Scy
370341618Scystatic void wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant *wpa_s,
371341618Scy					  const u8 *data, size_t len)
372341618Scy{
373341618Scy	struct wpabuf *report = wpabuf_alloc(len + 3);
374341618Scy
375341618Scy	if (!report)
376341618Scy		return;
377341618Scy
378341618Scy	wpabuf_put_u8(report, WLAN_ACTION_RADIO_MEASUREMENT);
379341618Scy	wpabuf_put_u8(report, WLAN_RRM_RADIO_MEASUREMENT_REPORT);
380341618Scy	wpabuf_put_u8(report, wpa_s->rrm.token);
381341618Scy
382341618Scy	wpabuf_put_data(report, data, len);
383341618Scy
384341618Scy	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
385341618Scy				wpa_s->own_addr, wpa_s->bssid,
386341618Scy				wpabuf_head(report), wpabuf_len(report), 0)) {
387341618Scy		wpa_printf(MSG_ERROR,
388341618Scy			   "RRM: Radio measurement report failed: Sending Action frame failed");
389341618Scy	}
390341618Scy
391341618Scy	wpabuf_free(report);
392341618Scy}
393341618Scy
394341618Scy
395346981Scystatic int wpas_rrm_beacon_rep_update_last_frame(u8 *pos, size_t len)
396346981Scy{
397346981Scy	struct rrm_measurement_report_element *msr_rep;
398346981Scy	u8 *end = pos + len;
399346981Scy	u8 *msr_rep_end;
400346981Scy	struct rrm_measurement_beacon_report *rep = NULL;
401346981Scy	u8 *subelem;
402346981Scy
403346981Scy	/* Find the last beacon report element */
404346981Scy	while (end - pos >= (int) sizeof(*msr_rep)) {
405346981Scy		msr_rep = (struct rrm_measurement_report_element *) pos;
406346981Scy		msr_rep_end = pos + msr_rep->len + 2;
407346981Scy
408346981Scy		if (msr_rep->eid != WLAN_EID_MEASURE_REPORT ||
409346981Scy		    msr_rep_end > end) {
410346981Scy			/* Should not happen. This indicates a bug. */
411346981Scy			wpa_printf(MSG_ERROR,
412346981Scy				   "RRM: non-measurement report element in measurement report frame");
413346981Scy			return -1;
414346981Scy		}
415346981Scy
416346981Scy		if (msr_rep->type == MEASURE_TYPE_BEACON)
417346981Scy			rep = (struct rrm_measurement_beacon_report *)
418346981Scy				msr_rep->variable;
419346981Scy
420346981Scy		pos += pos[1] + 2;
421346981Scy	}
422346981Scy
423346981Scy	if (!rep)
424346981Scy		return 0;
425346981Scy
426346981Scy	subelem = rep->variable;
427346981Scy	while (subelem + 2 < msr_rep_end &&
428346981Scy	       subelem[0] != WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION)
429346981Scy		subelem += 2 + subelem[1];
430346981Scy
431346981Scy	if (subelem + 2 < msr_rep_end &&
432346981Scy	    subelem[0] == WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION &&
433346981Scy	    subelem[1] == 1 &&
434346981Scy	    subelem + BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN <= end)
435346981Scy		subelem[2] = 1;
436346981Scy
437346981Scy	return 0;
438346981Scy}
439346981Scy
440346981Scy
441341618Scystatic void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
442341618Scy				     struct wpabuf *buf)
443341618Scy{
444341618Scy	int len = wpabuf_len(buf);
445346981Scy	u8 *pos = wpabuf_mhead_u8(buf), *next = pos;
446341618Scy
447341618Scy#define MPDU_REPORT_LEN (int) (IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN - 3)
448341618Scy
449341618Scy	while (len) {
450341618Scy		int send_len = (len > MPDU_REPORT_LEN) ? next - pos : len;
451341618Scy
452346981Scy		if (send_len == len)
453346981Scy			wpas_rrm_beacon_rep_update_last_frame(pos, len);
454346981Scy
455341618Scy		if (send_len == len ||
456341618Scy		    (send_len + next[1] + 2) > MPDU_REPORT_LEN) {
457341618Scy			wpas_rrm_send_msr_report_mpdu(wpa_s, pos, send_len);
458341618Scy			len -= send_len;
459341618Scy			pos = next;
460341618Scy		}
461341618Scy
462341618Scy		if (len)
463341618Scy			next += next[1] + 2;
464341618Scy	}
465341618Scy#undef MPDU_REPORT_LEN
466341618Scy}
467341618Scy
468341618Scy
469341618Scystatic int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
470341618Scy			    int *freqs)
471341618Scy{
472341618Scy	size_t i;
473341618Scy
474341618Scy	for (i = 0; i < num_primary_channels; i++) {
475341618Scy		u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
476341618Scy
477341618Scy		freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
478341618Scy		/* ieee80211_chan_to_freq() is not really meant for this
479341618Scy		 * conversion of 20 MHz primary channel numbers for wider VHT
480341618Scy		 * channels, so handle those as special cases here for now. */
481341618Scy		if (freqs[i] < 0 &&
482341618Scy		    (op_class == 128 || op_class == 129 || op_class == 130))
483341618Scy			freqs[i] = 5000 + 5 * primary_chan;
484341618Scy		if (freqs[i] < 0) {
485341618Scy			wpa_printf(MSG_DEBUG,
486341618Scy				   "Beacon Report: Invalid channel %u",
487341618Scy				   chan);
488341618Scy			return -1;
489341618Scy		}
490341618Scy	}
491341618Scy
492341618Scy	return 0;
493341618Scy}
494341618Scy
495341618Scy
496341618Scystatic int * wpas_add_channels(const struct oper_class_map *op,
497341618Scy			       struct hostapd_hw_modes *mode, int active,
498341618Scy			       const u8 *channels, const u8 size)
499341618Scy{
500341618Scy	int *freqs, *next_freq;
501341618Scy	u8 num_primary_channels, i;
502341618Scy	u8 num_chans;
503341618Scy
504341618Scy	num_chans = channels ? size :
505341618Scy		(op->max_chan - op->min_chan) / op->inc + 1;
506341618Scy
507341618Scy	if (op->bw == BW80 || op->bw == BW80P80)
508341618Scy		num_primary_channels = 4;
509341618Scy	else if (op->bw == BW160)
510341618Scy		num_primary_channels = 8;
511341618Scy	else
512341618Scy		num_primary_channels = 1;
513341618Scy
514341618Scy	/* one extra place for the zero-terminator */
515341618Scy	freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
516341618Scy	if (!freqs) {
517341618Scy		wpa_printf(MSG_ERROR,
518341618Scy			   "Beacon Report: Failed to allocate freqs array");
519341618Scy		return NULL;
520341618Scy	}
521341618Scy
522341618Scy	next_freq = freqs;
523341618Scy	for  (i = 0; i < num_chans; i++) {
524341618Scy		u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
525341618Scy		enum chan_allowed res = verify_channel(mode, chan, op->bw);
526341618Scy
527341618Scy		if (res == NOT_ALLOWED || (res == NO_IR && active))
528341618Scy			continue;
529341618Scy
530341618Scy		if (wpas_add_channel(op->op_class, chan, num_primary_channels,
531341618Scy				     next_freq) < 0) {
532341618Scy			os_free(freqs);
533341618Scy			return NULL;
534341618Scy		}
535341618Scy
536341618Scy		next_freq += num_primary_channels;
537341618Scy	}
538341618Scy
539341618Scy	if (!freqs[0]) {
540341618Scy		os_free(freqs);
541341618Scy		return NULL;
542341618Scy	}
543341618Scy
544341618Scy	return freqs;
545341618Scy}
546341618Scy
547341618Scy
548341618Scystatic int * wpas_op_class_freqs(const struct oper_class_map *op,
549341618Scy				 struct hostapd_hw_modes *mode, int active)
550341618Scy{
551341618Scy	u8 channels_80mhz[] = { 42, 58, 106, 122, 138, 155 };
552341618Scy	u8 channels_160mhz[] = { 50, 114 };
553341618Scy
554341618Scy	/*
555341618Scy	 * When adding all channels in the operating class, 80 + 80 MHz
556341618Scy	 * operating classes are like 80 MHz channels because we add all valid
557341618Scy	 * channels anyway.
558341618Scy	 */
559341618Scy	if (op->bw == BW80 || op->bw == BW80P80)
560341618Scy		return wpas_add_channels(op, mode, active, channels_80mhz,
561341618Scy					 ARRAY_SIZE(channels_80mhz));
562341618Scy
563341618Scy	if (op->bw == BW160)
564341618Scy		return wpas_add_channels(op, mode, active, channels_160mhz,
565341618Scy					 ARRAY_SIZE(channels_160mhz));
566341618Scy
567341618Scy	return wpas_add_channels(op, mode, active, NULL, 0);
568341618Scy}
569341618Scy
570341618Scy
571341618Scystatic int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s, int active,
572341618Scy				       const char *country, const u8 *subelems,
573341618Scy				       size_t len)
574341618Scy{
575341618Scy	int *freqs = NULL, *new_freqs;
576341618Scy	const u8 *end = subelems + len;
577341618Scy
578341618Scy	while (end - subelems > 2) {
579341618Scy		const struct oper_class_map *op;
580341618Scy		const u8 *ap_chan_elem, *pos;
581341618Scy		u8 left;
582341618Scy		struct hostapd_hw_modes *mode;
583341618Scy
584341618Scy		ap_chan_elem = get_ie(subelems, end - subelems,
585341618Scy				      WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
586341618Scy		if (!ap_chan_elem)
587341618Scy			break;
588341618Scy		pos = ap_chan_elem + 2;
589341618Scy		left = ap_chan_elem[1];
590341618Scy		if (left < 1)
591341618Scy			break;
592341618Scy		subelems = ap_chan_elem + 2 + left;
593341618Scy
594341618Scy		op = get_oper_class(country, *pos);
595341618Scy		if (!op) {
596341618Scy			wpa_printf(MSG_DEBUG,
597341618Scy				   "Beacon request: unknown operating class in AP Channel Report subelement %u",
598341618Scy				   *pos);
599341618Scy			goto out;
600341618Scy		}
601341618Scy		pos++;
602341618Scy		left--;
603341618Scy
604341618Scy		mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
605341618Scy		if (!mode)
606341618Scy			continue;
607341618Scy
608341618Scy		/*
609341618Scy		 * For 80 + 80 MHz operating classes, this AP Channel Report
610341618Scy		 * element should be followed by another element specifying
611341618Scy		 * the second 80 MHz channel. For now just add this 80 MHz
612341618Scy		 * channel, the second 80 MHz channel will be added when the
613341618Scy		 * next element is parsed.
614341618Scy		 * TODO: Verify that this AP Channel Report element is followed
615341618Scy		 * by a corresponding AP Channel Report element as specified in
616341618Scy		 * IEEE Std 802.11-2016, 11.11.9.1.
617341618Scy		 */
618341618Scy		new_freqs = wpas_add_channels(op, mode, active, pos, left);
619341618Scy		if (new_freqs)
620341618Scy			int_array_concat(&freqs, new_freqs);
621341618Scy
622341618Scy		os_free(new_freqs);
623341618Scy	}
624341618Scy
625341618Scy	return freqs;
626341618Scyout:
627341618Scy	os_free(freqs);
628341618Scy	return NULL;
629341618Scy}
630341618Scy
631341618Scy
632341618Scystatic int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
633341618Scy				       u8 op_class, u8 chan, int active,
634341618Scy				       const u8 *subelems, size_t len)
635341618Scy{
636341618Scy	int *freqs = NULL, *ext_freqs = NULL;
637341618Scy	struct hostapd_hw_modes *mode;
638341618Scy	const char *country = NULL;
639341618Scy	const struct oper_class_map *op;
640341618Scy	const u8 *elem;
641341618Scy
642341618Scy	if (!wpa_s->current_bss)
643341618Scy		return NULL;
644341618Scy	elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
645341618Scy	if (elem && elem[1] >= 2)
646341618Scy		country = (const char *) (elem + 2);
647341618Scy
648341618Scy	op = get_oper_class(country, op_class);
649341618Scy	if (!op) {
650341618Scy		wpa_printf(MSG_DEBUG,
651341618Scy			   "Beacon request: invalid operating class %d",
652341618Scy			   op_class);
653341618Scy		return NULL;
654341618Scy	}
655341618Scy
656341618Scy	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
657341618Scy	if (!mode)
658341618Scy		return NULL;
659341618Scy
660341618Scy	switch (chan) {
661341618Scy	case 0:
662341618Scy		freqs = wpas_op_class_freqs(op, mode, active);
663341618Scy		if (!freqs)
664341618Scy			return NULL;
665341618Scy		break;
666341618Scy	case 255:
667341618Scy		/* freqs will be added from AP channel subelements */
668341618Scy		break;
669341618Scy	default:
670341618Scy		freqs = wpas_add_channels(op, mode, active, &chan, 1);
671341618Scy		if (!freqs)
672341618Scy			return NULL;
673341618Scy		break;
674341618Scy	}
675341618Scy
676341618Scy	ext_freqs = wpas_channel_report_freqs(wpa_s, active, country, subelems,
677341618Scy					      len);
678341618Scy	if (ext_freqs) {
679341618Scy		int_array_concat(&freqs, ext_freqs);
680341618Scy		os_free(ext_freqs);
681341618Scy		int_array_sort_unique(freqs);
682341618Scy	}
683341618Scy
684341618Scy	return freqs;
685341618Scy}
686341618Scy
687341618Scy
688341618Scystatic int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
689341618Scy				u8 *op_class, u8 *chan, u8 *phy_type)
690341618Scy{
691341618Scy	const u8 *ie;
692341618Scy	int sec_chan = 0, vht = 0;
693341618Scy	struct ieee80211_ht_operation *ht_oper = NULL;
694341618Scy	struct ieee80211_vht_operation *vht_oper = NULL;
695341618Scy	u8 seg0, seg1;
696341618Scy
697341618Scy	ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
698341618Scy	if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
699341618Scy		u8 sec_chan_offset;
700341618Scy
701341618Scy		ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
702341618Scy		sec_chan_offset = ht_oper->ht_param &
703341618Scy			HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
704341618Scy		if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
705341618Scy			sec_chan = 1;
706341618Scy		else if (sec_chan_offset ==
707341618Scy			 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
708341618Scy			sec_chan = -1;
709341618Scy	}
710341618Scy
711341618Scy	ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
712341618Scy	if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
713341618Scy		vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
714341618Scy
715341618Scy		switch (vht_oper->vht_op_info_chwidth) {
716341618Scy		case 1:
717341618Scy			seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
718341618Scy			seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
719341618Scy			if (seg1 && abs(seg1 - seg0) == 8)
720351611Scy				vht = CHANWIDTH_160MHZ;
721341618Scy			else if (seg1)
722351611Scy				vht = CHANWIDTH_80P80MHZ;
723341618Scy			else
724351611Scy				vht = CHANWIDTH_80MHZ;
725341618Scy			break;
726341618Scy		case 2:
727351611Scy			vht = CHANWIDTH_160MHZ;
728341618Scy			break;
729341618Scy		case 3:
730351611Scy			vht = CHANWIDTH_80P80MHZ;
731341618Scy			break;
732341618Scy		default:
733351611Scy			vht = CHANWIDTH_USE_HT;
734341618Scy			break;
735341618Scy		}
736341618Scy	}
737341618Scy
738341618Scy	if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
739341618Scy					  chan) == NUM_HOSTAPD_MODES) {
740341618Scy		wpa_printf(MSG_DEBUG,
741341618Scy			   "Cannot determine operating class and channel");
742341618Scy		return -1;
743341618Scy	}
744341618Scy
745341618Scy	*phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
746341618Scy					   vht_oper != NULL);
747341618Scy	if (*phy_type == PHY_TYPE_UNSPECIFIED) {
748341618Scy		wpa_printf(MSG_DEBUG, "Cannot determine phy type");
749341618Scy		return -1;
750341618Scy	}
751341618Scy
752341618Scy	return 0;
753341618Scy}
754341618Scy
755341618Scy
756341618Scystatic int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
757341618Scy					  enum beacon_report_detail detail,
758341618Scy					  struct wpa_bss *bss, u8 *buf,
759346981Scy					  size_t buf_len, u8 **ies_buf,
760346981Scy					  size_t *ie_len, int add_fixed)
761341618Scy{
762346981Scy	u8 *ies = *ies_buf;
763346981Scy	size_t ies_len = *ie_len;
764341618Scy	u8 *pos = buf;
765341618Scy	int rem_len;
766341618Scy
767341618Scy	rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
768346981Scy		sizeof(struct rrm_measurement_report_element) - 2 -
769346981Scy		REPORTED_FRAME_BODY_SUBELEM_LEN;
770341618Scy
771341618Scy	if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
772341618Scy		wpa_printf(MSG_DEBUG,
773341618Scy			   "Beacon Request: Invalid reporting detail: %d",
774341618Scy			   detail);
775341618Scy		return -1;
776341618Scy	}
777341618Scy
778341618Scy	if (detail == BEACON_REPORT_DETAIL_NONE)
779341618Scy		return 0;
780341618Scy
781341618Scy	/*
782341618Scy	 * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
783341618Scy	 * beacon interval(2) + capabilities(2) = 14 bytes
784341618Scy	 */
785346981Scy	if (add_fixed && buf_len < 14)
786346981Scy		return -1;
787341618Scy
788341618Scy	*pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
789341618Scy	/* The length will be filled later */
790341618Scy	pos++;
791341618Scy
792346981Scy	if (add_fixed) {
793346981Scy		WPA_PUT_LE64(pos, bss->tsf);
794346981Scy		pos += sizeof(bss->tsf);
795346981Scy		WPA_PUT_LE16(pos, bss->beacon_int);
796346981Scy		pos += 2;
797346981Scy		WPA_PUT_LE16(pos, bss->caps);
798346981Scy		pos += 2;
799346981Scy	}
800346981Scy
801341618Scy	rem_len -= pos - buf;
802341618Scy
803341618Scy	/*
804341618Scy	 * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
805341618Scy	 * body subelement causes the element to exceed the maximum element
806341618Scy	 * size, the subelement is truncated so that the last IE is a complete
807341618Scy	 * IE. So even when required to report all IEs, add elements one after
808341618Scy	 * the other and stop once there is no more room in the measurement
809341618Scy	 * element.
810341618Scy	 */
811341618Scy	while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
812341618Scy		if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
813341618Scy		    (eids && bitfield_is_set(eids, ies[0]))) {
814346981Scy			u8 elen = ies[1];
815341618Scy
816341618Scy			if (2 + elen > buf + buf_len - pos ||
817341618Scy			    2 + elen > rem_len)
818341618Scy				break;
819341618Scy
820341618Scy			*pos++ = ies[0];
821341618Scy			*pos++ = elen;
822341618Scy			os_memcpy(pos, ies + 2, elen);
823341618Scy			pos += elen;
824341618Scy			rem_len -= 2 + elen;
825341618Scy		}
826341618Scy
827341618Scy		ies_len -= 2 + ies[1];
828341618Scy		ies += 2 + ies[1];
829341618Scy	}
830341618Scy
831346981Scy	*ie_len = ies_len;
832346981Scy	*ies_buf = ies;
833346981Scy
834341618Scy	/* Now the length is known */
835341618Scy	buf[1] = pos - buf - 2;
836341618Scy	return pos - buf;
837341618Scy}
838341618Scy
839341618Scy
840346981Scystatic int wpas_add_beacon_rep_elem(struct beacon_rep_data *data,
841346981Scy				    struct wpa_bss *bss,
842346981Scy				    struct wpabuf **wpa_buf,
843346981Scy				    struct rrm_measurement_beacon_report *rep,
844346981Scy				    u8 **ie, size_t *ie_len, u8 idx)
845346981Scy{
846346981Scy	int ret;
847346981Scy	u8 *buf, *pos;
848346981Scy	u32 subelems_len = REPORTED_FRAME_BODY_SUBELEM_LEN +
849346981Scy		(data->last_indication ?
850346981Scy		 BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN : 0);
851346981Scy
852346981Scy	/* Maximum element length: Beacon Report element + Reported Frame Body
853346981Scy	 * subelement + all IEs of the reported Beacon frame + Reported Frame
854346981Scy	 * Body Fragment ID subelement */
855346981Scy	buf = os_malloc(sizeof(*rep) + 14 + *ie_len + subelems_len);
856346981Scy	if (!buf)
857346981Scy		return -1;
858346981Scy
859346981Scy	os_memcpy(buf, rep, sizeof(*rep));
860346981Scy
861346981Scy	ret = wpas_beacon_rep_add_frame_body(data->eids, data->report_detail,
862346981Scy					     bss, buf + sizeof(*rep),
863346981Scy					     14 + *ie_len, ie, ie_len,
864346981Scy					     idx == 0);
865346981Scy	if (ret < 0)
866346981Scy		goto out;
867346981Scy
868346981Scy	pos = buf + ret + sizeof(*rep);
869346981Scy	pos[0] = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY_FRAGMENT_ID;
870346981Scy	pos[1] = 2;
871346981Scy
872346981Scy	/*
873346981Scy	 * Only one Beacon Report Measurement is supported at a time, so
874346981Scy	 * the Beacon Report ID can always be set to 1.
875346981Scy	 */
876346981Scy	pos[2] = 1;
877346981Scy
878346981Scy	/* Fragment ID Number (bits 0..6) and More Frame Body Fragments (bit 7)
879346981Scy */
880346981Scy	pos[3] = idx;
881346981Scy	if (data->report_detail != BEACON_REPORT_DETAIL_NONE && *ie_len)
882346981Scy		pos[3] |= REPORTED_FRAME_BODY_MORE_FRAGMENTS;
883346981Scy	else
884346981Scy		pos[3] &= ~REPORTED_FRAME_BODY_MORE_FRAGMENTS;
885346981Scy
886346981Scy	pos += REPORTED_FRAME_BODY_SUBELEM_LEN;
887346981Scy
888346981Scy	if (data->last_indication) {
889346981Scy		pos[0] = WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION;
890346981Scy		pos[1] = 1;
891346981Scy
892346981Scy		/* This field will be updated later if this is the last frame */
893346981Scy		pos[2] = 0;
894346981Scy	}
895346981Scy
896346981Scy	ret = wpas_rrm_report_elem(wpa_buf, data->token,
897346981Scy				   MEASUREMENT_REPORT_MODE_ACCEPT,
898346981Scy				   MEASURE_TYPE_BEACON, buf,
899346981Scy				   ret + sizeof(*rep) + subelems_len);
900346981Scyout:
901346981Scy	os_free(buf);
902346981Scy	return ret;
903346981Scy}
904346981Scy
905346981Scy
906341618Scystatic int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
907341618Scy			       struct wpabuf **wpa_buf, struct wpa_bss *bss,
908341618Scy			       u64 start, u64 parent_tsf)
909341618Scy{
910341618Scy	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
911346981Scy	u8 *ies = (u8 *) (bss + 1);
912346981Scy	u8 *pos = ies;
913346981Scy	size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
914346981Scy	struct rrm_measurement_beacon_report rep;
915346981Scy	u8 idx = 0;
916341618Scy
917341618Scy	if (os_memcmp(data->bssid, broadcast_ether_addr, ETH_ALEN) != 0 &&
918341618Scy	    os_memcmp(data->bssid, bss->bssid, ETH_ALEN) != 0)
919341618Scy		return 0;
920341618Scy
921341618Scy	if (data->ssid_len &&
922341618Scy	    (data->ssid_len != bss->ssid_len ||
923341618Scy	     os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
924341618Scy		return 0;
925341618Scy
926346981Scy	if (wpas_get_op_chan_phy(bss->freq, ies, ies_len, &rep.op_class,
927346981Scy				 &rep.channel, &rep.report_info) < 0)
928346981Scy		return 0;
929341618Scy
930346981Scy	rep.start_time = host_to_le64(start);
931346981Scy	rep.duration = host_to_le16(data->scan_params.duration);
932346981Scy	rep.rcpi = rssi_to_rcpi(bss->level);
933346981Scy	rep.rsni = 255; /* 255 indicates that RSNI is not available */
934346981Scy	os_memcpy(rep.bssid, bss->bssid, ETH_ALEN);
935346981Scy	rep.antenna_id = 0; /* unknown */
936346981Scy	rep.parent_tsf = host_to_le32(parent_tsf);
937341618Scy
938346981Scy	do {
939346981Scy		int ret;
940341618Scy
941346981Scy		ret = wpas_add_beacon_rep_elem(data, bss, wpa_buf, &rep,
942346981Scy					       &pos, &ies_len, idx++);
943346981Scy		if (ret)
944346981Scy			return ret;
945346981Scy	} while (data->report_detail != BEACON_REPORT_DETAIL_NONE &&
946346981Scy		 ies_len >= 2);
947341618Scy
948346981Scy	return 0;
949341618Scy}
950341618Scy
951341618Scy
952341618Scystatic int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
953341618Scy				      struct wpabuf **buf)
954341618Scy{
955341618Scy	return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
956341618Scy				    MEASUREMENT_REPORT_MODE_ACCEPT,
957341618Scy				    MEASURE_TYPE_BEACON, NULL, 0);
958341618Scy}
959341618Scy
960341618Scy
961341618Scystatic void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
962341618Scy				  struct wpabuf **buf)
963341618Scy{
964341618Scy	size_t i;
965341618Scy
966341618Scy	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
967341618Scy		if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
968341618Scy					0, 0) < 0)
969341618Scy			break;
970341618Scy	}
971341618Scy
972341618Scy	if (!(*buf))
973341618Scy		wpas_beacon_rep_no_results(wpa_s, buf);
974341618Scy
975341618Scy	wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
976341618Scy}
977341618Scy
978341618Scy
979341618Scyvoid wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
980341618Scy{
981341618Scy	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr)) {
982341618Scy		struct wpabuf *buf = NULL;
983341618Scy
984341618Scy		if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
985341618Scy					 MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
986341618Scy					 MEASURE_TYPE_BEACON, NULL, 0)) {
987341618Scy			wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
988341618Scy			wpabuf_free(buf);
989341618Scy			return;
990341618Scy		}
991341618Scy
992341618Scy		wpas_rrm_send_msr_report(wpa_s, buf);
993341618Scy		wpabuf_free(buf);
994341618Scy	}
995341618Scy
996341618Scy	wpas_clear_beacon_rep_data(wpa_s);
997341618Scy}
998341618Scy
999341618Scy
1000341618Scystatic void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1001341618Scy{
1002341618Scy	struct wpa_supplicant *wpa_s = eloop_ctx;
1003341618Scy	struct wpa_driver_scan_params *params =
1004341618Scy		&wpa_s->beacon_rep_data.scan_params;
1005341618Scy	u16 prev_duration = params->duration;
1006341618Scy
1007341618Scy	if (!wpa_s->current_bss)
1008341618Scy		return;
1009341618Scy
1010341618Scy	if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
1011341618Scy	    params->duration) {
1012341618Scy		wpa_printf(MSG_DEBUG,
1013341618Scy			   "RRM: Cannot set scan duration due to missing driver support");
1014341618Scy		params->duration = 0;
1015341618Scy	}
1016341618Scy	os_get_reltime(&wpa_s->beacon_rep_scan);
1017341618Scy	if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
1018341618Scy	    wpa_supplicant_trigger_scan(wpa_s, params))
1019341618Scy		wpas_rrm_refuse_request(wpa_s);
1020341618Scy	params->duration = prev_duration;
1021341618Scy}
1022341618Scy
1023341618Scy
1024341618Scystatic int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
1025341618Scy					     struct beacon_rep_data *data,
1026341618Scy					     u8 sid, u8 slen, const u8 *subelem)
1027341618Scy{
1028341618Scy	u8 report_info, i;
1029341618Scy
1030341618Scy	switch (sid) {
1031341618Scy	case WLAN_BEACON_REQUEST_SUBELEM_SSID:
1032341618Scy		if (!slen) {
1033341618Scy			wpa_printf(MSG_DEBUG,
1034341618Scy				   "SSID subelement with zero length - wildcard SSID");
1035341618Scy			break;
1036341618Scy		}
1037341618Scy
1038341618Scy		if (slen > SSID_MAX_LEN) {
1039341618Scy			wpa_printf(MSG_DEBUG,
1040341618Scy				   "Invalid SSID subelement length: %u", slen);
1041341618Scy			return -1;
1042341618Scy		}
1043341618Scy
1044341618Scy		data->ssid_len = slen;
1045341618Scy		os_memcpy(data->ssid, subelem, data->ssid_len);
1046341618Scy		break;
1047341618Scy	case WLAN_BEACON_REQUEST_SUBELEM_INFO:
1048341618Scy		if (slen != 2) {
1049341618Scy			wpa_printf(MSG_DEBUG,
1050341618Scy				   "Invalid reporting information subelement length: %u",
1051341618Scy				   slen);
1052341618Scy			return -1;
1053341618Scy		}
1054341618Scy
1055341618Scy		report_info = subelem[0];
1056341618Scy		if (report_info != 0) {
1057341618Scy			wpa_printf(MSG_DEBUG,
1058341618Scy				   "reporting information=%u is not supported",
1059341618Scy				   report_info);
1060341618Scy			return 0;
1061341618Scy		}
1062341618Scy		break;
1063341618Scy	case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
1064341618Scy		if (slen != 1) {
1065341618Scy			wpa_printf(MSG_DEBUG,
1066341618Scy				   "Invalid reporting detail subelement length: %u",
1067341618Scy				   slen);
1068341618Scy			return -1;
1069341618Scy		}
1070341618Scy
1071341618Scy		data->report_detail = subelem[0];
1072341618Scy		if (data->report_detail >
1073341618Scy		    BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
1074341618Scy			wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
1075341618Scy				   subelem[0]);
1076341618Scy			return -1;
1077341618Scy		}
1078341618Scy
1079341618Scy		break;
1080341618Scy	case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
1081341618Scy		if (data->report_detail !=
1082341618Scy		    BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
1083341618Scy			wpa_printf(MSG_DEBUG,
1084341618Scy				   "Beacon request: request subelement is present but report detail is %u",
1085341618Scy				   data->report_detail);
1086341618Scy			return -1;
1087341618Scy		}
1088341618Scy
1089341618Scy		if (!slen) {
1090341618Scy			wpa_printf(MSG_DEBUG,
1091341618Scy				   "Invalid request subelement length: %u",
1092341618Scy				   slen);
1093341618Scy			return -1;
1094341618Scy		}
1095341618Scy
1096341618Scy		if (data->eids) {
1097341618Scy			wpa_printf(MSG_DEBUG,
1098341618Scy				   "Beacon Request: Request subelement appears more than once");
1099341618Scy			return -1;
1100341618Scy		}
1101341618Scy
1102341618Scy		data->eids = bitfield_alloc(255);
1103341618Scy		if (!data->eids) {
1104341618Scy			wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
1105341618Scy			return -1;
1106341618Scy		}
1107341618Scy
1108341618Scy		for (i = 0; i < slen; i++)
1109341618Scy			bitfield_set(data->eids, subelem[i]);
1110341618Scy		break;
1111341618Scy	case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
1112341618Scy		/* Skip - it will be processed when freqs are added */
1113341618Scy		break;
1114346981Scy	case WLAN_BEACON_REQUEST_SUBELEM_LAST_INDICATION:
1115346981Scy		if (slen != 1) {
1116346981Scy			wpa_printf(MSG_DEBUG,
1117346981Scy				   "Beacon request: Invalid last indication request subelement length: %u",
1118346981Scy				   slen);
1119346981Scy			return -1;
1120346981Scy		}
1121346981Scy
1122346981Scy		data->last_indication = subelem[0];
1123346981Scy		break;
1124341618Scy	default:
1125341618Scy		wpa_printf(MSG_DEBUG,
1126341618Scy			   "Beacon request: Unknown subelement id %u", sid);
1127341618Scy		break;
1128341618Scy	}
1129341618Scy
1130341618Scy	return 1;
1131341618Scy}
1132341618Scy
1133341618Scy
1134341618Scy/**
1135341618Scy * Returns 0 if the next element can be processed, 1 if some operation was
1136341618Scy * triggered, and -1 if processing failed (i.e., the element is in invalid
1137341618Scy * format or an internal error occurred).
1138341618Scy */
1139341618Scystatic int
1140341618Scywpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
1141341618Scy			  u8 elem_token, int duration_mandatory,
1142341618Scy			  const struct rrm_measurement_beacon_request *req,
1143341618Scy			  size_t len, struct wpabuf **buf)
1144341618Scy{
1145341618Scy	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1146341618Scy	struct wpa_driver_scan_params *params = &data->scan_params;
1147341618Scy	const u8 *subelems;
1148341618Scy	size_t elems_len;
1149341618Scy	u16 rand_interval;
1150341618Scy	u32 interval_usec;
1151341618Scy	u32 _rand;
1152341618Scy	int ret = 0, res;
1153341618Scy	u8 reject_mode;
1154341618Scy
1155341618Scy	if (len < sizeof(*req))
1156341618Scy		return -1;
1157341618Scy
1158341618Scy	if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
1159341618Scy	    req->mode != BEACON_REPORT_MODE_ACTIVE &&
1160341618Scy	    req->mode != BEACON_REPORT_MODE_TABLE)
1161341618Scy		return 0;
1162341618Scy
1163341618Scy	subelems = req->variable;
1164341618Scy	elems_len = len - sizeof(*req);
1165341618Scy	rand_interval = le_to_host16(req->rand_interval);
1166341618Scy
1167341618Scy	os_free(params->freqs);
1168341618Scy	os_memset(params, 0, sizeof(*params));
1169341618Scy
1170341618Scy	data->token = elem_token;
1171341618Scy
1172341618Scy	/* default reporting detail is all fixed length fields and all
1173341618Scy	 * elements */
1174341618Scy	data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
1175341618Scy	os_memcpy(data->bssid, req->bssid, ETH_ALEN);
1176341618Scy
1177341618Scy	while (elems_len >= 2) {
1178341618Scy		if (subelems[1] > elems_len - 2) {
1179341618Scy			wpa_printf(MSG_DEBUG,
1180341618Scy				   "Beacon Request: Truncated subelement");
1181341618Scy			ret = -1;
1182341618Scy			goto out;
1183341618Scy		}
1184341618Scy
1185341618Scy		res = wpas_rm_handle_beacon_req_subelem(
1186341618Scy			wpa_s, data, subelems[0], subelems[1], &subelems[2]);
1187341618Scy		if (res < 0) {
1188341618Scy			ret = res;
1189341618Scy			goto out;
1190341618Scy		} else if (!res) {
1191341618Scy			reject_mode = MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE;
1192341618Scy			goto out_reject;
1193341618Scy		}
1194341618Scy
1195341618Scy		elems_len -= 2 + subelems[1];
1196341618Scy		subelems += 2 + subelems[1];
1197341618Scy	}
1198341618Scy
1199341618Scy	if (req->mode == BEACON_REPORT_MODE_TABLE) {
1200341618Scy		wpas_beacon_rep_table(wpa_s, buf);
1201341618Scy		goto out;
1202341618Scy	}
1203341618Scy
1204341618Scy	params->freqs = wpas_beacon_request_freqs(
1205341618Scy		wpa_s, req->oper_class, req->channel,
1206341618Scy		req->mode == BEACON_REPORT_MODE_ACTIVE,
1207341618Scy		req->variable, len - sizeof(*req));
1208341618Scy	if (!params->freqs) {
1209341618Scy		wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
1210341618Scy		reject_mode = MEASUREMENT_REPORT_MODE_REJECT_REFUSED;
1211341618Scy		goto out_reject;
1212341618Scy	}
1213341618Scy
1214341618Scy	params->duration = le_to_host16(req->duration);
1215341618Scy	params->duration_mandatory = duration_mandatory;
1216341618Scy	if (!params->duration) {
1217341618Scy		wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
1218341618Scy		ret = -1;
1219341618Scy		goto out;
1220341618Scy	}
1221341618Scy
1222341618Scy	params->only_new_results = 1;
1223341618Scy
1224341618Scy	if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
1225341618Scy		params->ssids[params->num_ssids].ssid = data->ssid;
1226341618Scy		params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
1227341618Scy	}
1228341618Scy
1229341618Scy	if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
1230341618Scy		_rand = os_random();
1231341618Scy	interval_usec = (_rand % (rand_interval + 1)) * 1024;
1232341618Scy	eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
1233341618Scy			       NULL);
1234341618Scy	return 1;
1235341618Scyout_reject:
1236341618Scy	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
1237341618Scy	    wpas_rrm_report_elem(buf, elem_token, reject_mode,
1238341618Scy				 MEASURE_TYPE_BEACON, NULL, 0) < 0) {
1239341618Scy		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1240341618Scy		ret = -1;
1241341618Scy	}
1242341618Scyout:
1243341618Scy	wpas_clear_beacon_rep_data(wpa_s);
1244341618Scy	return ret;
1245341618Scy}
1246341618Scy
1247341618Scy
1248341618Scystatic int
1249341618Scywpas_rrm_handle_msr_req_element(
1250341618Scy	struct wpa_supplicant *wpa_s,
1251341618Scy	const struct rrm_measurement_request_element *req,
1252341618Scy	struct wpabuf **buf)
1253341618Scy{
1254341618Scy	int duration_mandatory;
1255341618Scy
1256341618Scy	wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
1257341618Scy		   req->type, req->token);
1258341618Scy
1259341618Scy	if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
1260341618Scy		/* Enable bit is not supported for now */
1261341618Scy		wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
1262341618Scy		return 0;
1263341618Scy	}
1264341618Scy
1265341618Scy	if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
1266341618Scy	    req->type > MEASURE_TYPE_RPI_HIST) {
1267341618Scy		/* Parallel measurements are not supported for now */
1268341618Scy		wpa_printf(MSG_DEBUG,
1269341618Scy			   "RRM: Parallel measurements are not supported, reject");
1270341618Scy		goto reject;
1271341618Scy	}
1272341618Scy
1273341618Scy	duration_mandatory =
1274341618Scy		!!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
1275341618Scy
1276341618Scy	switch (req->type) {
1277341618Scy	case MEASURE_TYPE_LCI:
1278341618Scy		return wpas_rrm_build_lci_report(wpa_s, req, buf);
1279341618Scy	case MEASURE_TYPE_BEACON:
1280341618Scy		if (duration_mandatory &&
1281341618Scy		    !(wpa_s->drv_rrm_flags &
1282341618Scy		      WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
1283341618Scy			wpa_printf(MSG_DEBUG,
1284341618Scy				   "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
1285341618Scy			goto reject;
1286341618Scy		}
1287341618Scy		return wpas_rm_handle_beacon_req(wpa_s, req->token,
1288341618Scy						 duration_mandatory,
1289341618Scy						 (const void *) req->variable,
1290341618Scy						 req->len - 3, buf);
1291341618Scy	default:
1292341618Scy		wpa_printf(MSG_INFO,
1293341618Scy			   "RRM: Unsupported radio measurement type %u",
1294341618Scy			   req->type);
1295341618Scy		break;
1296341618Scy	}
1297341618Scy
1298341618Scyreject:
1299341618Scy	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
1300341618Scy	    wpas_rrm_report_elem(buf, req->token,
1301341618Scy				 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
1302341618Scy				 req->type, NULL, 0) < 0) {
1303341618Scy		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1304341618Scy		return -1;
1305341618Scy	}
1306341618Scy
1307341618Scy	return 0;
1308341618Scy}
1309341618Scy
1310341618Scy
1311341618Scystatic struct wpabuf *
1312341618Scywpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
1313341618Scy			       size_t len)
1314341618Scy{
1315341618Scy	struct wpabuf *buf = NULL;
1316341618Scy
1317341618Scy	while (len) {
1318341618Scy		const struct rrm_measurement_request_element *req;
1319341618Scy		int res;
1320341618Scy
1321341618Scy		if (len < 2) {
1322341618Scy			wpa_printf(MSG_DEBUG, "RRM: Truncated element");
1323341618Scy			goto out;
1324341618Scy		}
1325341618Scy
1326341618Scy		req = (const struct rrm_measurement_request_element *) pos;
1327341618Scy		if (req->eid != WLAN_EID_MEASURE_REQUEST) {
1328341618Scy			wpa_printf(MSG_DEBUG,
1329341618Scy				   "RRM: Expected Measurement Request element, but EID is %u",
1330341618Scy				   req->eid);
1331341618Scy			goto out;
1332341618Scy		}
1333341618Scy
1334341618Scy		if (req->len < 3) {
1335341618Scy			wpa_printf(MSG_DEBUG, "RRM: Element length too short");
1336341618Scy			goto out;
1337341618Scy		}
1338341618Scy
1339341618Scy		if (req->len > len - 2) {
1340341618Scy			wpa_printf(MSG_DEBUG, "RRM: Element length too long");
1341341618Scy			goto out;
1342341618Scy		}
1343341618Scy
1344341618Scy		res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
1345341618Scy		if (res < 0)
1346341618Scy			goto out;
1347341618Scy
1348341618Scy		pos += req->len + 2;
1349341618Scy		len -= req->len + 2;
1350341618Scy	}
1351341618Scy
1352341618Scy	return buf;
1353341618Scy
1354341618Scyout:
1355341618Scy	wpabuf_free(buf);
1356341618Scy	return NULL;
1357341618Scy}
1358341618Scy
1359341618Scy
1360341618Scyvoid wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
1361341618Scy					       const u8 *src, const u8 *dst,
1362341618Scy					       const u8 *frame, size_t len)
1363341618Scy{
1364341618Scy	struct wpabuf *report;
1365341618Scy
1366341618Scy	if (wpa_s->wpa_state != WPA_COMPLETED) {
1367341618Scy		wpa_printf(MSG_INFO,
1368341618Scy			   "RRM: Ignoring radio measurement request: Not associated");
1369341618Scy		return;
1370341618Scy	}
1371341618Scy
1372341618Scy	if (!wpa_s->rrm.rrm_used) {
1373341618Scy		wpa_printf(MSG_INFO,
1374341618Scy			   "RRM: Ignoring radio measurement request: Not RRM network");
1375341618Scy		return;
1376341618Scy	}
1377341618Scy
1378341618Scy	if (len < 3) {
1379341618Scy		wpa_printf(MSG_INFO,
1380341618Scy			   "RRM: Ignoring too short radio measurement request");
1381341618Scy		return;
1382341618Scy	}
1383341618Scy
1384341618Scy	wpa_s->rrm.token = *frame;
1385341618Scy	os_memcpy(wpa_s->rrm.dst_addr, dst, ETH_ALEN);
1386341618Scy
1387341618Scy	/* Number of repetitions is not supported */
1388341618Scy
1389341618Scy	report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
1390341618Scy	if (!report)
1391341618Scy		return;
1392341618Scy
1393341618Scy	wpas_rrm_send_msr_report(wpa_s, report);
1394341618Scy	wpabuf_free(report);
1395341618Scy}
1396341618Scy
1397341618Scy
1398341618Scyvoid wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
1399341618Scy					      const u8 *src,
1400341618Scy					      const u8 *frame, size_t len,
1401341618Scy					      int rssi)
1402341618Scy{
1403341618Scy	struct wpabuf *buf;
1404341618Scy	const struct rrm_link_measurement_request *req;
1405341618Scy	struct rrm_link_measurement_report report;
1406341618Scy
1407341618Scy	if (wpa_s->wpa_state != WPA_COMPLETED) {
1408341618Scy		wpa_printf(MSG_INFO,
1409341618Scy			   "RRM: Ignoring link measurement request. Not associated");
1410341618Scy		return;
1411341618Scy	}
1412341618Scy
1413341618Scy	if (!wpa_s->rrm.rrm_used) {
1414341618Scy		wpa_printf(MSG_INFO,
1415341618Scy			   "RRM: Ignoring link measurement request. Not RRM network");
1416341618Scy		return;
1417341618Scy	}
1418341618Scy
1419341618Scy	if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
1420341618Scy		wpa_printf(MSG_INFO,
1421341618Scy			   "RRM: Measurement report failed. TX power insertion not supported");
1422341618Scy		return;
1423341618Scy	}
1424341618Scy
1425341618Scy	req = (const struct rrm_link_measurement_request *) frame;
1426341618Scy	if (len < sizeof(*req)) {
1427341618Scy		wpa_printf(MSG_INFO,
1428341618Scy			   "RRM: Link measurement report failed. Request too short");
1429341618Scy		return;
1430341618Scy	}
1431341618Scy
1432341618Scy	os_memset(&report, 0, sizeof(report));
1433341618Scy	report.dialog_token = req->dialog_token;
1434341618Scy	report.tpc.eid = WLAN_EID_TPC_REPORT;
1435341618Scy	report.tpc.len = 2;
1436341618Scy	/* Note: The driver is expected to update report.tpc.tx_power and
1437341618Scy	 * report.tpc.link_margin subfields when sending out this frame.
1438341618Scy	 * Similarly, the driver would need to update report.rx_ant_id and
1439341618Scy	 * report.tx_ant_id subfields. */
1440341618Scy	report.rsni = 255; /* 255 indicates that RSNI is not available */
1441341618Scy	report.rcpi = rssi_to_rcpi(rssi);
1442341618Scy
1443341618Scy	/* action_category + action_code */
1444341618Scy	buf = wpabuf_alloc(2 + sizeof(report));
1445341618Scy	if (buf == NULL) {
1446341618Scy		wpa_printf(MSG_ERROR,
1447341618Scy			   "RRM: Link measurement report failed. Buffer allocation failed");
1448341618Scy		return;
1449341618Scy	}
1450341618Scy
1451341618Scy	wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
1452341618Scy	wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
1453341618Scy	wpabuf_put_data(buf, &report, sizeof(report));
1454341618Scy	wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
1455341618Scy
1456341618Scy	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
1457341618Scy				wpa_s->own_addr, wpa_s->bssid,
1458341618Scy				wpabuf_head(buf), wpabuf_len(buf), 0)) {
1459341618Scy		wpa_printf(MSG_ERROR,
1460341618Scy			   "RRM: Link measurement report failed. Send action failed");
1461341618Scy	}
1462341618Scy	wpabuf_free(buf);
1463341618Scy}
1464341618Scy
1465341618Scy
1466341618Scyint wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
1467341618Scy				 struct wpa_scan_results *scan_res,
1468341618Scy				 struct scan_info *info)
1469341618Scy{
1470341618Scy	size_t i = 0;
1471341618Scy	struct wpabuf *buf = NULL;
1472341618Scy
1473341618Scy	if (!wpa_s->beacon_rep_data.token)
1474341618Scy		return 0;
1475341618Scy
1476341618Scy	if (!wpa_s->current_bss)
1477341618Scy		goto out;
1478341618Scy
1479341618Scy	/* If the measurement was aborted, don't report partial results */
1480341618Scy	if (info->aborted)
1481341618Scy		goto out;
1482341618Scy
1483341618Scy	wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
1484341618Scy		   MAC2STR(info->scan_start_tsf_bssid),
1485341618Scy		   MAC2STR(wpa_s->current_bss->bssid));
1486341618Scy	if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1487341618Scy	    os_memcmp(info->scan_start_tsf_bssid, wpa_s->current_bss->bssid,
1488341618Scy		      ETH_ALEN) != 0) {
1489341618Scy		wpa_printf(MSG_DEBUG,
1490341618Scy			   "RRM: Ignore scan results due to mismatching TSF BSSID");
1491341618Scy		goto out;
1492341618Scy	}
1493341618Scy
1494341618Scy	for (i = 0; i < scan_res->num; i++) {
1495341618Scy		struct wpa_bss *bss =
1496341618Scy			wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
1497341618Scy
1498341618Scy		if (!bss)
1499341618Scy			continue;
1500341618Scy
1501341618Scy		if ((wpa_s->drv_rrm_flags &
1502341618Scy		     WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1503341618Scy		    os_memcmp(scan_res->res[i]->tsf_bssid,
1504341618Scy			      wpa_s->current_bss->bssid, ETH_ALEN) != 0) {
1505341618Scy			wpa_printf(MSG_DEBUG,
1506341618Scy				   "RRM: Ignore scan result for " MACSTR
1507341618Scy				   " due to mismatching TSF BSSID" MACSTR,
1508341618Scy				   MAC2STR(scan_res->res[i]->bssid),
1509341618Scy				   MAC2STR(scan_res->res[i]->tsf_bssid));
1510341618Scy			continue;
1511341618Scy		}
1512341618Scy
1513341618Scy		/*
1514341618Scy		 * Don't report results that were not received during the
1515341618Scy		 * current measurement.
1516341618Scy		 */
1517341618Scy		if (!(wpa_s->drv_rrm_flags &
1518341618Scy		      WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
1519341618Scy			struct os_reltime update_time, diff;
1520341618Scy
1521341618Scy			/* For now, allow 8 ms older results due to some
1522341618Scy			 * unknown issue with cfg80211 BSS table updates during
1523341618Scy			 * a scan with the current BSS.
1524341618Scy			 * TODO: Fix this more properly to avoid having to have
1525341618Scy			 * this type of hacks in place. */
1526341618Scy			calculate_update_time(&scan_res->fetch_time,
1527341618Scy					      scan_res->res[i]->age,
1528341618Scy					      &update_time);
1529341618Scy			os_reltime_sub(&wpa_s->beacon_rep_scan,
1530341618Scy				       &update_time, &diff);
1531341618Scy			if (os_reltime_before(&update_time,
1532341618Scy					      &wpa_s->beacon_rep_scan) &&
1533341618Scy			    (diff.sec || diff.usec >= 8000)) {
1534341618Scy				wpa_printf(MSG_DEBUG,
1535341618Scy					   "RRM: Ignore scan result for " MACSTR
1536341618Scy					   " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
1537341618Scy					   MAC2STR(scan_res->res[i]->bssid),
1538341618Scy					   scan_res->res[i]->age,
1539341618Scy					   (unsigned int) diff.sec,
1540341618Scy					   (unsigned int) diff.usec);
1541341618Scy				continue;
1542341618Scy			}
1543341618Scy		} else if (info->scan_start_tsf >
1544341618Scy			   scan_res->res[i]->parent_tsf) {
1545341618Scy			continue;
1546341618Scy		}
1547341618Scy
1548341618Scy		if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
1549341618Scy					scan_res->res[i]->parent_tsf) < 0)
1550341618Scy			break;
1551341618Scy	}
1552341618Scy
1553341618Scy	if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
1554341618Scy		goto out;
1555341618Scy
1556341618Scy	wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
1557341618Scy
1558341618Scy	wpas_rrm_send_msr_report(wpa_s, buf);
1559341618Scy	wpabuf_free(buf);
1560341618Scy
1561341618Scyout:
1562341618Scy	wpas_clear_beacon_rep_data(wpa_s);
1563341618Scy	return 1;
1564341618Scy}
1565341618Scy
1566341618Scy
1567341618Scyvoid wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
1568341618Scy{
1569341618Scy	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1570341618Scy
1571341618Scy	eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
1572341618Scy	bitfield_free(data->eids);
1573341618Scy	os_free(data->scan_params.freqs);
1574341618Scy	os_memset(data, 0, sizeof(*data));
1575341618Scy}
1576