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) {
82c1d255d3SCy 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);
93c1d255d3SCy 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 */
104c1d255d3SCy Schubert #ifndef ENOTCONN
10585732ac8SCy Schubert #define ENOTCONN -1
106c1d255d3SCy Schubert #endif
107c1d255d3SCy Schubert #ifndef EOPNOTSUPP
10885732ac8SCy Schubert #define EOPNOTSUPP -1
109c1d255d3SCy Schubert #endif
110c1d255d3SCy Schubert #ifndef ECANCELED
11185732ac8SCy Schubert #define ECANCELED -1
11285732ac8SCy Schubert #endif
113c1d255d3SCy 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) {
151c1d255d3SCy 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) {
156c1d255d3SCy 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)) {
164c1d255d3SCy 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) {
171c1d255d3SCy 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) {
181c1d255d3SCy Schubert wpa_dbg(wpa_s, MSG_DEBUG,
18285732ac8SCy Schubert "RRM: Failed to allocate Neighbor Report Request");
18385732ac8SCy Schubert return -ENOMEM;
18485732ac8SCy Schubert }
18585732ac8SCy Schubert
186c1d255d3SCy Schubert wpa_dbg(wpa_s, MSG_DEBUG,
187c1d255d3SCy 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) {
271c1d255d3SCy 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,const u8 * channels,const u8 size)50385732ac8SCy Schubert static int * wpas_add_channels(const struct oper_class_map *op,
504*a90b9d01SCy Schubert struct hostapd_hw_modes *mode,
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;
518*a90b9d01SCy Schubert else if (op->bw == BW320)
519*a90b9d01SCy Schubert num_primary_channels = 16;
52085732ac8SCy Schubert else
52185732ac8SCy Schubert num_primary_channels = 1;
52285732ac8SCy Schubert
52385732ac8SCy Schubert /* one extra place for the zero-terminator */
52485732ac8SCy Schubert freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
52585732ac8SCy Schubert if (!freqs) {
52685732ac8SCy Schubert wpa_printf(MSG_ERROR,
52785732ac8SCy Schubert "Beacon Report: Failed to allocate freqs array");
52885732ac8SCy Schubert return NULL;
52985732ac8SCy Schubert }
53085732ac8SCy Schubert
53185732ac8SCy Schubert next_freq = freqs;
53285732ac8SCy Schubert for (i = 0; i < num_chans; i++) {
53385732ac8SCy Schubert u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
534c1d255d3SCy Schubert enum chan_allowed res = verify_channel(mode, op->op_class, chan,
535c1d255d3SCy Schubert op->bw);
53685732ac8SCy Schubert
537*a90b9d01SCy Schubert if (res == NOT_ALLOWED)
53885732ac8SCy Schubert continue;
53985732ac8SCy Schubert
54085732ac8SCy Schubert if (wpas_add_channel(op->op_class, chan, num_primary_channels,
54185732ac8SCy Schubert next_freq) < 0) {
54285732ac8SCy Schubert os_free(freqs);
54385732ac8SCy Schubert return NULL;
54485732ac8SCy Schubert }
54585732ac8SCy Schubert
54685732ac8SCy Schubert next_freq += num_primary_channels;
54785732ac8SCy Schubert }
54885732ac8SCy Schubert
54985732ac8SCy Schubert if (!freqs[0]) {
55085732ac8SCy Schubert os_free(freqs);
55185732ac8SCy Schubert return NULL;
55285732ac8SCy Schubert }
55385732ac8SCy Schubert
55485732ac8SCy Schubert return freqs;
55585732ac8SCy Schubert }
55685732ac8SCy Schubert
55785732ac8SCy Schubert
wpas_op_class_freqs(const struct oper_class_map * op,struct hostapd_hw_modes * mode)55885732ac8SCy Schubert static int * wpas_op_class_freqs(const struct oper_class_map *op,
559*a90b9d01SCy Schubert struct hostapd_hw_modes *mode)
56085732ac8SCy Schubert {
561c1d255d3SCy Schubert u8 channels_80mhz_5ghz[] = { 42, 58, 106, 122, 138, 155, 171 };
562c1d255d3SCy Schubert u8 channels_160mhz_5ghz[] = { 50, 114, 163 };
563c1d255d3SCy Schubert u8 channels_80mhz_6ghz[] = { 7, 23, 39, 55, 71, 87, 103, 119, 135, 151,
564c1d255d3SCy Schubert 167, 183, 199, 215 };
565c1d255d3SCy Schubert u8 channels_160mhz_6ghz[] = { 15, 47, 79, 111, 143, 175, 207 };
566*a90b9d01SCy Schubert u8 channels_320mhz_6ghz[] = { 31, 63, 95, 127, 159, 191 };
567c1d255d3SCy Schubert const u8 *channels = NULL;
568c1d255d3SCy Schubert size_t num_chan = 0;
569c1d255d3SCy Schubert bool is_6ghz = is_6ghz_op_class(op->op_class);
57085732ac8SCy Schubert
57185732ac8SCy Schubert /*
57285732ac8SCy Schubert * When adding all channels in the operating class, 80 + 80 MHz
57385732ac8SCy Schubert * operating classes are like 80 MHz channels because we add all valid
57485732ac8SCy Schubert * channels anyway.
57585732ac8SCy Schubert */
576c1d255d3SCy Schubert if (op->bw == BW80 || op->bw == BW80P80) {
577c1d255d3SCy Schubert channels = is_6ghz ? channels_80mhz_6ghz : channels_80mhz_5ghz;
578c1d255d3SCy Schubert num_chan = is_6ghz ? ARRAY_SIZE(channels_80mhz_6ghz) :
579c1d255d3SCy Schubert ARRAY_SIZE(channels_80mhz_5ghz);
580c1d255d3SCy Schubert } else if (op->bw == BW160) {
581c1d255d3SCy Schubert channels = is_6ghz ? channels_160mhz_6ghz :
582c1d255d3SCy Schubert channels_160mhz_5ghz;
583c1d255d3SCy Schubert num_chan = is_6ghz ? ARRAY_SIZE(channels_160mhz_6ghz) :
584c1d255d3SCy Schubert ARRAY_SIZE(channels_160mhz_5ghz);
585*a90b9d01SCy Schubert } else if (op->bw == BW320) {
586*a90b9d01SCy Schubert channels = channels_320mhz_6ghz;
587*a90b9d01SCy Schubert num_chan = ARRAY_SIZE(channels_320mhz_6ghz);
588c1d255d3SCy Schubert }
58985732ac8SCy Schubert
590*a90b9d01SCy Schubert return wpas_add_channels(op, mode, channels, num_chan);
59185732ac8SCy Schubert }
59285732ac8SCy Schubert
59385732ac8SCy Schubert
wpas_channel_report_freqs(struct wpa_supplicant * wpa_s,const char * country,const u8 * subelems,size_t len)594*a90b9d01SCy Schubert static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s,
59585732ac8SCy Schubert const char *country, const u8 *subelems,
59685732ac8SCy Schubert size_t len)
59785732ac8SCy Schubert {
59885732ac8SCy Schubert int *freqs = NULL, *new_freqs;
59985732ac8SCy Schubert const u8 *end = subelems + len;
60085732ac8SCy Schubert
60185732ac8SCy Schubert while (end - subelems > 2) {
60285732ac8SCy Schubert const struct oper_class_map *op;
60385732ac8SCy Schubert const u8 *ap_chan_elem, *pos;
60485732ac8SCy Schubert u8 left;
60585732ac8SCy Schubert struct hostapd_hw_modes *mode;
60685732ac8SCy Schubert
60785732ac8SCy Schubert ap_chan_elem = get_ie(subelems, end - subelems,
60885732ac8SCy Schubert WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
60985732ac8SCy Schubert if (!ap_chan_elem)
61085732ac8SCy Schubert break;
61185732ac8SCy Schubert pos = ap_chan_elem + 2;
61285732ac8SCy Schubert left = ap_chan_elem[1];
61385732ac8SCy Schubert if (left < 1)
61485732ac8SCy Schubert break;
61585732ac8SCy Schubert subelems = ap_chan_elem + 2 + left;
61685732ac8SCy Schubert
61785732ac8SCy Schubert op = get_oper_class(country, *pos);
61885732ac8SCy Schubert if (!op) {
61985732ac8SCy Schubert wpa_printf(MSG_DEBUG,
62085732ac8SCy Schubert "Beacon request: unknown operating class in AP Channel Report subelement %u",
62185732ac8SCy Schubert *pos);
62285732ac8SCy Schubert goto out;
62385732ac8SCy Schubert }
62485732ac8SCy Schubert pos++;
62585732ac8SCy Schubert left--;
62685732ac8SCy Schubert
627c1d255d3SCy Schubert mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode,
628c1d255d3SCy Schubert is_6ghz_op_class(op->op_class));
62985732ac8SCy Schubert if (!mode)
63085732ac8SCy Schubert continue;
63185732ac8SCy Schubert
63285732ac8SCy Schubert /*
63385732ac8SCy Schubert * For 80 + 80 MHz operating classes, this AP Channel Report
63485732ac8SCy Schubert * element should be followed by another element specifying
63585732ac8SCy Schubert * the second 80 MHz channel. For now just add this 80 MHz
63685732ac8SCy Schubert * channel, the second 80 MHz channel will be added when the
63785732ac8SCy Schubert * next element is parsed.
63885732ac8SCy Schubert * TODO: Verify that this AP Channel Report element is followed
63985732ac8SCy Schubert * by a corresponding AP Channel Report element as specified in
64085732ac8SCy Schubert * IEEE Std 802.11-2016, 11.11.9.1.
64185732ac8SCy Schubert */
642*a90b9d01SCy Schubert new_freqs = wpas_add_channels(op, mode, pos, left);
64385732ac8SCy Schubert if (new_freqs)
64485732ac8SCy Schubert int_array_concat(&freqs, new_freqs);
64585732ac8SCy Schubert
64685732ac8SCy Schubert os_free(new_freqs);
64785732ac8SCy Schubert }
64885732ac8SCy Schubert
64985732ac8SCy Schubert return freqs;
65085732ac8SCy Schubert out:
65185732ac8SCy Schubert os_free(freqs);
65285732ac8SCy Schubert return NULL;
65385732ac8SCy Schubert }
65485732ac8SCy Schubert
65585732ac8SCy Schubert
wpas_beacon_request_freqs(struct wpa_supplicant * wpa_s,u8 op_class,u8 chan,const u8 * subelems,size_t len)65685732ac8SCy Schubert static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
657*a90b9d01SCy Schubert u8 op_class, u8 chan,
65885732ac8SCy Schubert const u8 *subelems, size_t len)
65985732ac8SCy Schubert {
66085732ac8SCy Schubert int *freqs = NULL, *ext_freqs = NULL;
66185732ac8SCy Schubert struct hostapd_hw_modes *mode;
66285732ac8SCy Schubert const char *country = NULL;
66385732ac8SCy Schubert const struct oper_class_map *op;
66485732ac8SCy Schubert const u8 *elem;
66585732ac8SCy Schubert
66685732ac8SCy Schubert if (!wpa_s->current_bss)
66785732ac8SCy Schubert return NULL;
66885732ac8SCy Schubert elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
66985732ac8SCy Schubert if (elem && elem[1] >= 2)
67085732ac8SCy Schubert country = (const char *) (elem + 2);
67185732ac8SCy Schubert
67285732ac8SCy Schubert op = get_oper_class(country, op_class);
67385732ac8SCy Schubert if (!op) {
67485732ac8SCy Schubert wpa_printf(MSG_DEBUG,
67585732ac8SCy Schubert "Beacon request: invalid operating class %d",
67685732ac8SCy Schubert op_class);
67785732ac8SCy Schubert return NULL;
67885732ac8SCy Schubert }
67985732ac8SCy Schubert
680c1d255d3SCy Schubert mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode,
681c1d255d3SCy Schubert is_6ghz_op_class(op->op_class));
68285732ac8SCy Schubert if (!mode)
68385732ac8SCy Schubert return NULL;
68485732ac8SCy Schubert
68585732ac8SCy Schubert switch (chan) {
68685732ac8SCy Schubert case 0:
687*a90b9d01SCy Schubert freqs = wpas_op_class_freqs(op, mode);
68885732ac8SCy Schubert if (!freqs)
68985732ac8SCy Schubert return NULL;
69085732ac8SCy Schubert break;
69185732ac8SCy Schubert case 255:
69285732ac8SCy Schubert /* freqs will be added from AP channel subelements */
69385732ac8SCy Schubert break;
69485732ac8SCy Schubert default:
695*a90b9d01SCy Schubert freqs = wpas_add_channels(op, mode, &chan, 1);
69685732ac8SCy Schubert if (!freqs)
69785732ac8SCy Schubert return NULL;
69885732ac8SCy Schubert break;
69985732ac8SCy Schubert }
70085732ac8SCy Schubert
701*a90b9d01SCy Schubert ext_freqs = wpas_channel_report_freqs(wpa_s, country, subelems, len);
70285732ac8SCy Schubert if (ext_freqs) {
70385732ac8SCy Schubert int_array_concat(&freqs, ext_freqs);
70485732ac8SCy Schubert os_free(ext_freqs);
70585732ac8SCy Schubert int_array_sort_unique(freqs);
70685732ac8SCy Schubert }
70785732ac8SCy Schubert
70885732ac8SCy Schubert return freqs;
70985732ac8SCy Schubert }
71085732ac8SCy Schubert
71185732ac8SCy Schubert
wpas_get_op_chan_phy(int freq,const u8 * ies,size_t ies_len,u8 * op_class,u8 * chan,u8 * phy_type)712c1d255d3SCy Schubert int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
71385732ac8SCy Schubert u8 *op_class, u8 *chan, u8 *phy_type)
71485732ac8SCy Schubert {
71585732ac8SCy Schubert const u8 *ie;
71685732ac8SCy Schubert int sec_chan = 0, vht = 0;
71785732ac8SCy Schubert struct ieee80211_ht_operation *ht_oper = NULL;
71885732ac8SCy Schubert struct ieee80211_vht_operation *vht_oper = NULL;
71985732ac8SCy Schubert u8 seg0, seg1;
72085732ac8SCy Schubert
72185732ac8SCy Schubert ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
72285732ac8SCy Schubert if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
72385732ac8SCy Schubert u8 sec_chan_offset;
72485732ac8SCy Schubert
72585732ac8SCy Schubert ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
72685732ac8SCy Schubert sec_chan_offset = ht_oper->ht_param &
72785732ac8SCy Schubert HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
72885732ac8SCy Schubert if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
72985732ac8SCy Schubert sec_chan = 1;
73085732ac8SCy Schubert else if (sec_chan_offset ==
73185732ac8SCy Schubert HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
73285732ac8SCy Schubert sec_chan = -1;
73385732ac8SCy Schubert }
73485732ac8SCy Schubert
73585732ac8SCy Schubert ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
73685732ac8SCy Schubert if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
73785732ac8SCy Schubert vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
73885732ac8SCy Schubert
73985732ac8SCy Schubert switch (vht_oper->vht_op_info_chwidth) {
740*a90b9d01SCy Schubert case CHANWIDTH_80MHZ:
74185732ac8SCy Schubert seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
74285732ac8SCy Schubert seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
74385732ac8SCy Schubert if (seg1 && abs(seg1 - seg0) == 8)
744*a90b9d01SCy Schubert vht = CONF_OPER_CHWIDTH_160MHZ;
74585732ac8SCy Schubert else if (seg1)
746*a90b9d01SCy Schubert vht = CONF_OPER_CHWIDTH_80P80MHZ;
74785732ac8SCy Schubert else
748*a90b9d01SCy Schubert vht = CONF_OPER_CHWIDTH_80MHZ;
74985732ac8SCy Schubert break;
750*a90b9d01SCy Schubert case CHANWIDTH_160MHZ:
751*a90b9d01SCy Schubert vht = CONF_OPER_CHWIDTH_160MHZ;
75285732ac8SCy Schubert break;
753*a90b9d01SCy Schubert case CHANWIDTH_80P80MHZ:
754*a90b9d01SCy Schubert vht = CONF_OPER_CHWIDTH_80P80MHZ;
75585732ac8SCy Schubert break;
75685732ac8SCy Schubert default:
757*a90b9d01SCy Schubert vht = CONF_OPER_CHWIDTH_USE_HT;
75885732ac8SCy Schubert break;
75985732ac8SCy Schubert }
76085732ac8SCy Schubert }
76185732ac8SCy Schubert
76285732ac8SCy Schubert if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
76385732ac8SCy Schubert chan) == NUM_HOSTAPD_MODES) {
76485732ac8SCy Schubert wpa_printf(MSG_DEBUG,
76585732ac8SCy Schubert "Cannot determine operating class and channel");
76685732ac8SCy Schubert return -1;
76785732ac8SCy Schubert }
76885732ac8SCy Schubert
76985732ac8SCy Schubert *phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
77085732ac8SCy Schubert vht_oper != NULL);
77185732ac8SCy Schubert if (*phy_type == PHY_TYPE_UNSPECIFIED) {
77285732ac8SCy Schubert wpa_printf(MSG_DEBUG, "Cannot determine phy type");
77385732ac8SCy Schubert return -1;
77485732ac8SCy Schubert }
77585732ac8SCy Schubert
77685732ac8SCy Schubert return 0;
77785732ac8SCy Schubert }
77885732ac8SCy Schubert
77985732ac8SCy Schubert
wpas_beacon_rep_add_frame_body(struct bitfield * eids,struct bitfield * ext_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)78085732ac8SCy Schubert static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
781*a90b9d01SCy Schubert struct bitfield *ext_eids,
78285732ac8SCy Schubert enum beacon_report_detail detail,
78385732ac8SCy Schubert struct wpa_bss *bss, u8 *buf,
784c1d255d3SCy Schubert size_t buf_len, const u8 **ies_buf,
7854bc52338SCy Schubert size_t *ie_len, int add_fixed)
78685732ac8SCy Schubert {
787c1d255d3SCy Schubert const u8 *ies = *ies_buf;
7884bc52338SCy Schubert size_t ies_len = *ie_len;
78985732ac8SCy Schubert u8 *pos = buf;
79085732ac8SCy Schubert int rem_len;
79185732ac8SCy Schubert
79285732ac8SCy Schubert rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
7934bc52338SCy Schubert sizeof(struct rrm_measurement_report_element) - 2 -
7944bc52338SCy Schubert REPORTED_FRAME_BODY_SUBELEM_LEN;
79585732ac8SCy Schubert
79685732ac8SCy Schubert if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
79785732ac8SCy Schubert wpa_printf(MSG_DEBUG,
79885732ac8SCy Schubert "Beacon Request: Invalid reporting detail: %d",
79985732ac8SCy Schubert detail);
80085732ac8SCy Schubert return -1;
80185732ac8SCy Schubert }
80285732ac8SCy Schubert
80385732ac8SCy Schubert if (detail == BEACON_REPORT_DETAIL_NONE)
80485732ac8SCy Schubert return 0;
80585732ac8SCy Schubert
80685732ac8SCy Schubert /*
80785732ac8SCy Schubert * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
80885732ac8SCy Schubert * beacon interval(2) + capabilities(2) = 14 bytes
80985732ac8SCy Schubert */
8104bc52338SCy Schubert if (add_fixed && buf_len < 14)
8114bc52338SCy Schubert return -1;
81285732ac8SCy Schubert
81385732ac8SCy Schubert *pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
81485732ac8SCy Schubert /* The length will be filled later */
81585732ac8SCy Schubert pos++;
8164bc52338SCy Schubert
8174bc52338SCy Schubert if (add_fixed) {
81885732ac8SCy Schubert WPA_PUT_LE64(pos, bss->tsf);
81985732ac8SCy Schubert pos += sizeof(bss->tsf);
82085732ac8SCy Schubert WPA_PUT_LE16(pos, bss->beacon_int);
82185732ac8SCy Schubert pos += 2;
82285732ac8SCy Schubert WPA_PUT_LE16(pos, bss->caps);
82385732ac8SCy Schubert pos += 2;
8244bc52338SCy Schubert }
82585732ac8SCy Schubert
82685732ac8SCy Schubert rem_len -= pos - buf;
82785732ac8SCy Schubert
82885732ac8SCy Schubert /*
82985732ac8SCy Schubert * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
83085732ac8SCy Schubert * body subelement causes the element to exceed the maximum element
83185732ac8SCy Schubert * size, the subelement is truncated so that the last IE is a complete
83285732ac8SCy Schubert * IE. So even when required to report all IEs, add elements one after
83385732ac8SCy Schubert * the other and stop once there is no more room in the measurement
83485732ac8SCy Schubert * element.
83585732ac8SCy Schubert */
83685732ac8SCy Schubert while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
83785732ac8SCy Schubert if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
838*a90b9d01SCy Schubert (eids && bitfield_is_set(eids, ies[0])) ||
839*a90b9d01SCy Schubert (ext_eids && ies[0] == WLAN_EID_EXTENSION && ies[1] &&
840*a90b9d01SCy Schubert bitfield_is_set(ext_eids, ies[2]))) {
8414bc52338SCy Schubert u8 elen = ies[1];
84285732ac8SCy Schubert
84385732ac8SCy Schubert if (2 + elen > buf + buf_len - pos ||
84485732ac8SCy Schubert 2 + elen > rem_len)
84585732ac8SCy Schubert break;
84685732ac8SCy Schubert
84785732ac8SCy Schubert *pos++ = ies[0];
84885732ac8SCy Schubert *pos++ = elen;
84985732ac8SCy Schubert os_memcpy(pos, ies + 2, elen);
85085732ac8SCy Schubert pos += elen;
85185732ac8SCy Schubert rem_len -= 2 + elen;
85285732ac8SCy Schubert }
85385732ac8SCy Schubert
85485732ac8SCy Schubert ies_len -= 2 + ies[1];
85585732ac8SCy Schubert ies += 2 + ies[1];
85685732ac8SCy Schubert }
85785732ac8SCy Schubert
8584bc52338SCy Schubert *ie_len = ies_len;
8594bc52338SCy Schubert *ies_buf = ies;
8604bc52338SCy Schubert
86185732ac8SCy Schubert /* Now the length is known */
86285732ac8SCy Schubert buf[1] = pos - buf - 2;
86385732ac8SCy Schubert return pos - buf;
86485732ac8SCy Schubert }
86585732ac8SCy Schubert
86685732ac8SCy 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)8674bc52338SCy Schubert static int wpas_add_beacon_rep_elem(struct beacon_rep_data *data,
8684bc52338SCy Schubert struct wpa_bss *bss,
8694bc52338SCy Schubert struct wpabuf **wpa_buf,
8704bc52338SCy Schubert struct rrm_measurement_beacon_report *rep,
871c1d255d3SCy Schubert const u8 **ie, size_t *ie_len, u8 idx)
8724bc52338SCy Schubert {
8734bc52338SCy Schubert int ret;
8744bc52338SCy Schubert u8 *buf, *pos;
8754bc52338SCy Schubert u32 subelems_len = REPORTED_FRAME_BODY_SUBELEM_LEN +
8764bc52338SCy Schubert (data->last_indication ?
8774bc52338SCy Schubert BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN : 0);
8784bc52338SCy Schubert
8794bc52338SCy Schubert /* Maximum element length: Beacon Report element + Reported Frame Body
8804bc52338SCy Schubert * subelement + all IEs of the reported Beacon frame + Reported Frame
8814bc52338SCy Schubert * Body Fragment ID subelement */
8824bc52338SCy Schubert buf = os_malloc(sizeof(*rep) + 14 + *ie_len + subelems_len);
8834bc52338SCy Schubert if (!buf)
8844bc52338SCy Schubert return -1;
8854bc52338SCy Schubert
8864bc52338SCy Schubert os_memcpy(buf, rep, sizeof(*rep));
8874bc52338SCy Schubert
888*a90b9d01SCy Schubert ret = wpas_beacon_rep_add_frame_body(data->eids, data->ext_eids,
889*a90b9d01SCy Schubert data->report_detail,
8904bc52338SCy Schubert bss, buf + sizeof(*rep),
8914bc52338SCy Schubert 14 + *ie_len, ie, ie_len,
8924bc52338SCy Schubert idx == 0);
8934bc52338SCy Schubert if (ret < 0)
8944bc52338SCy Schubert goto out;
8954bc52338SCy Schubert
8964bc52338SCy Schubert pos = buf + ret + sizeof(*rep);
8974bc52338SCy Schubert pos[0] = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY_FRAGMENT_ID;
8984bc52338SCy Schubert pos[1] = 2;
8994bc52338SCy Schubert
9004bc52338SCy Schubert /*
9014bc52338SCy Schubert * Only one Beacon Report Measurement is supported at a time, so
9024bc52338SCy Schubert * the Beacon Report ID can always be set to 1.
9034bc52338SCy Schubert */
9044bc52338SCy Schubert pos[2] = 1;
9054bc52338SCy Schubert
9064bc52338SCy Schubert /* Fragment ID Number (bits 0..6) and More Frame Body Fragments (bit 7)
9074bc52338SCy Schubert */
9084bc52338SCy Schubert pos[3] = idx;
9094bc52338SCy Schubert if (data->report_detail != BEACON_REPORT_DETAIL_NONE && *ie_len)
9104bc52338SCy Schubert pos[3] |= REPORTED_FRAME_BODY_MORE_FRAGMENTS;
9114bc52338SCy Schubert else
9124bc52338SCy Schubert pos[3] &= ~REPORTED_FRAME_BODY_MORE_FRAGMENTS;
9134bc52338SCy Schubert
9144bc52338SCy Schubert pos += REPORTED_FRAME_BODY_SUBELEM_LEN;
9154bc52338SCy Schubert
9164bc52338SCy Schubert if (data->last_indication) {
9174bc52338SCy Schubert pos[0] = WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION;
9184bc52338SCy Schubert pos[1] = 1;
9194bc52338SCy Schubert
9204bc52338SCy Schubert /* This field will be updated later if this is the last frame */
9214bc52338SCy Schubert pos[2] = 0;
9224bc52338SCy Schubert }
9234bc52338SCy Schubert
9244bc52338SCy Schubert ret = wpas_rrm_report_elem(wpa_buf, data->token,
9254bc52338SCy Schubert MEASUREMENT_REPORT_MODE_ACCEPT,
9264bc52338SCy Schubert MEASURE_TYPE_BEACON, buf,
9274bc52338SCy Schubert ret + sizeof(*rep) + subelems_len);
9284bc52338SCy Schubert out:
9294bc52338SCy Schubert os_free(buf);
9304bc52338SCy Schubert return ret;
9314bc52338SCy Schubert }
9324bc52338SCy Schubert
9334bc52338SCy Schubert
wpas_add_beacon_rep(struct wpa_supplicant * wpa_s,struct wpabuf ** wpa_buf,struct wpa_bss * bss,u64 start,u64 parent_tsf)93485732ac8SCy Schubert static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
93585732ac8SCy Schubert struct wpabuf **wpa_buf, struct wpa_bss *bss,
93685732ac8SCy Schubert u64 start, u64 parent_tsf)
93785732ac8SCy Schubert {
93885732ac8SCy Schubert struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
939c1d255d3SCy Schubert const u8 *ies = wpa_bss_ie_ptr(bss);
940c1d255d3SCy Schubert const u8 *pos = ies;
9414bc52338SCy Schubert size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
9424bc52338SCy Schubert struct rrm_measurement_beacon_report rep;
9434bc52338SCy Schubert u8 idx = 0;
94485732ac8SCy Schubert
945*a90b9d01SCy Schubert if (!ether_addr_equal(data->bssid, broadcast_ether_addr) &&
946*a90b9d01SCy Schubert !ether_addr_equal(data->bssid, bss->bssid))
94785732ac8SCy Schubert return 0;
94885732ac8SCy Schubert
94985732ac8SCy Schubert if (data->ssid_len &&
95085732ac8SCy Schubert (data->ssid_len != bss->ssid_len ||
95185732ac8SCy Schubert os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
95285732ac8SCy Schubert return 0;
95385732ac8SCy Schubert
9544bc52338SCy Schubert if (wpas_get_op_chan_phy(bss->freq, ies, ies_len, &rep.op_class,
9554bc52338SCy Schubert &rep.channel, &rep.report_info) < 0)
9564bc52338SCy Schubert return 0;
95785732ac8SCy Schubert
9584bc52338SCy Schubert rep.start_time = host_to_le64(start);
9594bc52338SCy Schubert rep.duration = host_to_le16(data->scan_params.duration);
9604bc52338SCy Schubert rep.rcpi = rssi_to_rcpi(bss->level);
9614bc52338SCy Schubert rep.rsni = 255; /* 255 indicates that RSNI is not available */
9624bc52338SCy Schubert os_memcpy(rep.bssid, bss->bssid, ETH_ALEN);
9634bc52338SCy Schubert rep.antenna_id = 0; /* unknown */
9644bc52338SCy Schubert rep.parent_tsf = host_to_le32(parent_tsf);
96585732ac8SCy Schubert
9664bc52338SCy Schubert do {
9674bc52338SCy Schubert int ret;
96885732ac8SCy Schubert
9694bc52338SCy Schubert ret = wpas_add_beacon_rep_elem(data, bss, wpa_buf, &rep,
9704bc52338SCy Schubert &pos, &ies_len, idx++);
9714bc52338SCy Schubert if (ret)
97285732ac8SCy Schubert return ret;
9734bc52338SCy Schubert } while (data->report_detail != BEACON_REPORT_DETAIL_NONE &&
9744bc52338SCy Schubert ies_len >= 2);
9754bc52338SCy Schubert
9764bc52338SCy Schubert return 0;
97785732ac8SCy Schubert }
97885732ac8SCy Schubert
97985732ac8SCy Schubert
wpas_beacon_rep_no_results(struct wpa_supplicant * wpa_s,struct wpabuf ** buf)98085732ac8SCy Schubert static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
98185732ac8SCy Schubert struct wpabuf **buf)
98285732ac8SCy Schubert {
98385732ac8SCy Schubert return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
98485732ac8SCy Schubert MEASUREMENT_REPORT_MODE_ACCEPT,
98585732ac8SCy Schubert MEASURE_TYPE_BEACON, NULL, 0);
98685732ac8SCy Schubert }
98785732ac8SCy Schubert
98885732ac8SCy Schubert
wpas_beacon_rep_table(struct wpa_supplicant * wpa_s,struct wpabuf ** buf)98985732ac8SCy Schubert static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
99085732ac8SCy Schubert struct wpabuf **buf)
99185732ac8SCy Schubert {
99285732ac8SCy Schubert size_t i;
99385732ac8SCy Schubert
99485732ac8SCy Schubert for (i = 0; i < wpa_s->last_scan_res_used; i++) {
99585732ac8SCy Schubert if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
99685732ac8SCy Schubert 0, 0) < 0)
99785732ac8SCy Schubert break;
99885732ac8SCy Schubert }
99985732ac8SCy Schubert
100085732ac8SCy Schubert if (!(*buf))
100185732ac8SCy Schubert wpas_beacon_rep_no_results(wpa_s, buf);
100285732ac8SCy Schubert
100385732ac8SCy Schubert wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
100485732ac8SCy Schubert }
100585732ac8SCy Schubert
100685732ac8SCy Schubert
wpas_rrm_refuse_request(struct wpa_supplicant * wpa_s)100785732ac8SCy Schubert void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
100885732ac8SCy Schubert {
100985732ac8SCy Schubert if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr)) {
101085732ac8SCy Schubert struct wpabuf *buf = NULL;
101185732ac8SCy Schubert
101285732ac8SCy Schubert if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
101385732ac8SCy Schubert MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
101485732ac8SCy Schubert MEASURE_TYPE_BEACON, NULL, 0)) {
101585732ac8SCy Schubert wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
101685732ac8SCy Schubert wpabuf_free(buf);
101785732ac8SCy Schubert return;
101885732ac8SCy Schubert }
101985732ac8SCy Schubert
102085732ac8SCy Schubert wpas_rrm_send_msr_report(wpa_s, buf);
102185732ac8SCy Schubert wpabuf_free(buf);
102285732ac8SCy Schubert }
102385732ac8SCy Schubert
102485732ac8SCy Schubert wpas_clear_beacon_rep_data(wpa_s);
102585732ac8SCy Schubert }
102685732ac8SCy Schubert
102785732ac8SCy Schubert
wpas_rrm_scan_timeout(void * eloop_ctx,void * timeout_ctx)102885732ac8SCy Schubert static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
102985732ac8SCy Schubert {
103085732ac8SCy Schubert struct wpa_supplicant *wpa_s = eloop_ctx;
103185732ac8SCy Schubert struct wpa_driver_scan_params *params =
103285732ac8SCy Schubert &wpa_s->beacon_rep_data.scan_params;
103385732ac8SCy Schubert u16 prev_duration = params->duration;
103485732ac8SCy Schubert
103585732ac8SCy Schubert if (!wpa_s->current_bss)
103685732ac8SCy Schubert return;
103785732ac8SCy Schubert
103885732ac8SCy Schubert if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
103985732ac8SCy Schubert params->duration) {
104085732ac8SCy Schubert wpa_printf(MSG_DEBUG,
104185732ac8SCy Schubert "RRM: Cannot set scan duration due to missing driver support");
104285732ac8SCy Schubert params->duration = 0;
104385732ac8SCy Schubert }
104485732ac8SCy Schubert os_get_reltime(&wpa_s->beacon_rep_scan);
104585732ac8SCy Schubert if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
1046*a90b9d01SCy Schubert wpa_supplicant_trigger_scan(wpa_s, params, true, false))
104785732ac8SCy Schubert wpas_rrm_refuse_request(wpa_s);
104885732ac8SCy Schubert params->duration = prev_duration;
104985732ac8SCy Schubert }
105085732ac8SCy Schubert
105185732ac8SCy Schubert
wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant * wpa_s,struct beacon_rep_data * data,u8 sid,u8 slen,const u8 * subelem)105285732ac8SCy Schubert static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
105385732ac8SCy Schubert struct beacon_rep_data *data,
105485732ac8SCy Schubert u8 sid, u8 slen, const u8 *subelem)
105585732ac8SCy Schubert {
1056*a90b9d01SCy Schubert struct bitfield *eids;
105785732ac8SCy Schubert u8 report_info, i;
105885732ac8SCy Schubert
105985732ac8SCy Schubert switch (sid) {
106085732ac8SCy Schubert case WLAN_BEACON_REQUEST_SUBELEM_SSID:
106185732ac8SCy Schubert if (!slen) {
106285732ac8SCy Schubert wpa_printf(MSG_DEBUG,
106385732ac8SCy Schubert "SSID subelement with zero length - wildcard SSID");
106485732ac8SCy Schubert break;
106585732ac8SCy Schubert }
106685732ac8SCy Schubert
106785732ac8SCy Schubert if (slen > SSID_MAX_LEN) {
106885732ac8SCy Schubert wpa_printf(MSG_DEBUG,
106985732ac8SCy Schubert "Invalid SSID subelement length: %u", slen);
107085732ac8SCy Schubert return -1;
107185732ac8SCy Schubert }
107285732ac8SCy Schubert
107385732ac8SCy Schubert data->ssid_len = slen;
107485732ac8SCy Schubert os_memcpy(data->ssid, subelem, data->ssid_len);
107585732ac8SCy Schubert break;
107685732ac8SCy Schubert case WLAN_BEACON_REQUEST_SUBELEM_INFO:
107785732ac8SCy Schubert if (slen != 2) {
107885732ac8SCy Schubert wpa_printf(MSG_DEBUG,
107985732ac8SCy Schubert "Invalid reporting information subelement length: %u",
108085732ac8SCy Schubert slen);
108185732ac8SCy Schubert return -1;
108285732ac8SCy Schubert }
108385732ac8SCy Schubert
108485732ac8SCy Schubert report_info = subelem[0];
108585732ac8SCy Schubert if (report_info != 0) {
108685732ac8SCy Schubert wpa_printf(MSG_DEBUG,
108785732ac8SCy Schubert "reporting information=%u is not supported",
108885732ac8SCy Schubert report_info);
108985732ac8SCy Schubert return 0;
109085732ac8SCy Schubert }
109185732ac8SCy Schubert break;
109285732ac8SCy Schubert case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
109385732ac8SCy Schubert if (slen != 1) {
109485732ac8SCy Schubert wpa_printf(MSG_DEBUG,
109585732ac8SCy Schubert "Invalid reporting detail subelement length: %u",
109685732ac8SCy Schubert slen);
109785732ac8SCy Schubert return -1;
109885732ac8SCy Schubert }
109985732ac8SCy Schubert
110085732ac8SCy Schubert data->report_detail = subelem[0];
110185732ac8SCy Schubert if (data->report_detail >
110285732ac8SCy Schubert BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
110385732ac8SCy Schubert wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
110485732ac8SCy Schubert subelem[0]);
110585732ac8SCy Schubert return -1;
110685732ac8SCy Schubert }
110785732ac8SCy Schubert
110885732ac8SCy Schubert break;
110985732ac8SCy Schubert case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
1110*a90b9d01SCy Schubert case WLAN_BEACON_REQUEST_SUBELEM_EXT_REQUEST:
111185732ac8SCy Schubert if (data->report_detail !=
111285732ac8SCy Schubert BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
111385732ac8SCy Schubert wpa_printf(MSG_DEBUG,
111485732ac8SCy Schubert "Beacon request: request subelement is present but report detail is %u",
111585732ac8SCy Schubert data->report_detail);
111685732ac8SCy Schubert return -1;
111785732ac8SCy Schubert }
111885732ac8SCy Schubert
111985732ac8SCy Schubert if (!slen) {
112085732ac8SCy Schubert wpa_printf(MSG_DEBUG,
112185732ac8SCy Schubert "Invalid request subelement length: %u",
112285732ac8SCy Schubert slen);
112385732ac8SCy Schubert return -1;
112485732ac8SCy Schubert }
112585732ac8SCy Schubert
1126*a90b9d01SCy Schubert if (sid == WLAN_BEACON_REQUEST_SUBELEM_EXT_REQUEST) {
1127*a90b9d01SCy Schubert if (slen < 2) {
112885732ac8SCy Schubert wpa_printf(MSG_DEBUG,
1129*a90b9d01SCy Schubert "Invalid extended request");
1130*a90b9d01SCy Schubert return -1;
1131*a90b9d01SCy Schubert }
1132*a90b9d01SCy Schubert if (subelem[0] != WLAN_EID_EXTENSION) {
1133*a90b9d01SCy Schubert wpa_printf(MSG_DEBUG,
1134*a90b9d01SCy Schubert "Skip unknown Requested Element ID %u in Extended Request subelement",
1135*a90b9d01SCy Schubert subelem[0]);
1136*a90b9d01SCy Schubert break;
1137*a90b9d01SCy Schubert }
1138*a90b9d01SCy Schubert
1139*a90b9d01SCy Schubert /* Skip the Requested Element ID field */
1140*a90b9d01SCy Schubert subelem++;
1141*a90b9d01SCy Schubert slen--;
1142*a90b9d01SCy Schubert }
1143*a90b9d01SCy Schubert
1144*a90b9d01SCy Schubert if ((sid == WLAN_BEACON_REQUEST_SUBELEM_REQUEST &&
1145*a90b9d01SCy Schubert data->eids) ||
1146*a90b9d01SCy Schubert (sid == WLAN_BEACON_REQUEST_SUBELEM_EXT_REQUEST &&
1147*a90b9d01SCy Schubert data->ext_eids)) {
1148*a90b9d01SCy Schubert wpa_printf(MSG_DEBUG,
1149*a90b9d01SCy Schubert "Beacon Request: Request sub elements appear more than once");
115085732ac8SCy Schubert return -1;
115185732ac8SCy Schubert }
115285732ac8SCy Schubert
1153*a90b9d01SCy Schubert eids = bitfield_alloc(255);
1154*a90b9d01SCy Schubert if (!eids) {
115585732ac8SCy Schubert wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
115685732ac8SCy Schubert return -1;
115785732ac8SCy Schubert }
115885732ac8SCy Schubert
1159*a90b9d01SCy Schubert if (sid == WLAN_BEACON_REQUEST_SUBELEM_REQUEST)
1160*a90b9d01SCy Schubert data->eids = eids;
1161*a90b9d01SCy Schubert else
1162*a90b9d01SCy Schubert data->ext_eids = eids;
1163*a90b9d01SCy Schubert
116485732ac8SCy Schubert for (i = 0; i < slen; i++)
1165*a90b9d01SCy Schubert bitfield_set(eids, subelem[i]);
116685732ac8SCy Schubert break;
116785732ac8SCy Schubert case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
116885732ac8SCy Schubert /* Skip - it will be processed when freqs are added */
116985732ac8SCy Schubert break;
11704bc52338SCy Schubert case WLAN_BEACON_REQUEST_SUBELEM_LAST_INDICATION:
11714bc52338SCy Schubert if (slen != 1) {
11724bc52338SCy Schubert wpa_printf(MSG_DEBUG,
11734bc52338SCy Schubert "Beacon request: Invalid last indication request subelement length: %u",
11744bc52338SCy Schubert slen);
11754bc52338SCy Schubert return -1;
11764bc52338SCy Schubert }
11774bc52338SCy Schubert
11784bc52338SCy Schubert data->last_indication = subelem[0];
11794bc52338SCy Schubert break;
118085732ac8SCy Schubert default:
118185732ac8SCy Schubert wpa_printf(MSG_DEBUG,
118285732ac8SCy Schubert "Beacon request: Unknown subelement id %u", sid);
118385732ac8SCy Schubert break;
118485732ac8SCy Schubert }
118585732ac8SCy Schubert
118685732ac8SCy Schubert return 1;
118785732ac8SCy Schubert }
118885732ac8SCy Schubert
118985732ac8SCy Schubert
119085732ac8SCy Schubert /**
119185732ac8SCy Schubert * Returns 0 if the next element can be processed, 1 if some operation was
119285732ac8SCy Schubert * triggered, and -1 if processing failed (i.e., the element is in invalid
119385732ac8SCy Schubert * format or an internal error occurred).
119485732ac8SCy Schubert */
119585732ac8SCy 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)119685732ac8SCy Schubert wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
119785732ac8SCy Schubert u8 elem_token, int duration_mandatory,
119885732ac8SCy Schubert const struct rrm_measurement_beacon_request *req,
119985732ac8SCy Schubert size_t len, struct wpabuf **buf)
120085732ac8SCy Schubert {
120185732ac8SCy Schubert struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
120285732ac8SCy Schubert struct wpa_driver_scan_params *params = &data->scan_params;
120385732ac8SCy Schubert const u8 *subelems;
120485732ac8SCy Schubert size_t elems_len;
120585732ac8SCy Schubert u16 rand_interval;
120685732ac8SCy Schubert u32 interval_usec;
120785732ac8SCy Schubert u32 _rand;
120885732ac8SCy Schubert int ret = 0, res;
120985732ac8SCy Schubert u8 reject_mode;
121085732ac8SCy Schubert
121185732ac8SCy Schubert if (len < sizeof(*req))
121285732ac8SCy Schubert return -1;
121385732ac8SCy Schubert
121485732ac8SCy Schubert if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
121585732ac8SCy Schubert req->mode != BEACON_REPORT_MODE_ACTIVE &&
121685732ac8SCy Schubert req->mode != BEACON_REPORT_MODE_TABLE)
121785732ac8SCy Schubert return 0;
121885732ac8SCy Schubert
121985732ac8SCy Schubert subelems = req->variable;
122085732ac8SCy Schubert elems_len = len - sizeof(*req);
122185732ac8SCy Schubert rand_interval = le_to_host16(req->rand_interval);
122285732ac8SCy Schubert
122385732ac8SCy Schubert os_free(params->freqs);
122485732ac8SCy Schubert os_memset(params, 0, sizeof(*params));
122585732ac8SCy Schubert
122685732ac8SCy Schubert data->token = elem_token;
122785732ac8SCy Schubert
122885732ac8SCy Schubert /* default reporting detail is all fixed length fields and all
122985732ac8SCy Schubert * elements */
123085732ac8SCy Schubert data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
123185732ac8SCy Schubert os_memcpy(data->bssid, req->bssid, ETH_ALEN);
123285732ac8SCy Schubert
123385732ac8SCy Schubert while (elems_len >= 2) {
123485732ac8SCy Schubert if (subelems[1] > elems_len - 2) {
123585732ac8SCy Schubert wpa_printf(MSG_DEBUG,
123685732ac8SCy Schubert "Beacon Request: Truncated subelement");
123785732ac8SCy Schubert ret = -1;
123885732ac8SCy Schubert goto out;
123985732ac8SCy Schubert }
124085732ac8SCy Schubert
124185732ac8SCy Schubert res = wpas_rm_handle_beacon_req_subelem(
124285732ac8SCy Schubert wpa_s, data, subelems[0], subelems[1], &subelems[2]);
124385732ac8SCy Schubert if (res < 0) {
124485732ac8SCy Schubert ret = res;
124585732ac8SCy Schubert goto out;
124685732ac8SCy Schubert } else if (!res) {
124785732ac8SCy Schubert reject_mode = MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE;
124885732ac8SCy Schubert goto out_reject;
124985732ac8SCy Schubert }
125085732ac8SCy Schubert
125185732ac8SCy Schubert elems_len -= 2 + subelems[1];
125285732ac8SCy Schubert subelems += 2 + subelems[1];
125385732ac8SCy Schubert }
125485732ac8SCy Schubert
125585732ac8SCy Schubert if (req->mode == BEACON_REPORT_MODE_TABLE) {
125685732ac8SCy Schubert wpas_beacon_rep_table(wpa_s, buf);
125785732ac8SCy Schubert goto out;
125885732ac8SCy Schubert }
125985732ac8SCy Schubert
1260*a90b9d01SCy Schubert params->freqs = wpas_beacon_request_freqs(wpa_s, req->oper_class,
1261*a90b9d01SCy Schubert req->channel, req->variable,
1262*a90b9d01SCy Schubert len - sizeof(*req));
126385732ac8SCy Schubert if (!params->freqs) {
126485732ac8SCy Schubert wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
126585732ac8SCy Schubert reject_mode = MEASUREMENT_REPORT_MODE_REJECT_REFUSED;
126685732ac8SCy Schubert goto out_reject;
126785732ac8SCy Schubert }
126885732ac8SCy Schubert
126985732ac8SCy Schubert params->duration = le_to_host16(req->duration);
127085732ac8SCy Schubert params->duration_mandatory = duration_mandatory;
127185732ac8SCy Schubert if (!params->duration) {
127285732ac8SCy Schubert wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
127385732ac8SCy Schubert ret = -1;
127485732ac8SCy Schubert goto out;
127585732ac8SCy Schubert }
127685732ac8SCy Schubert
127785732ac8SCy Schubert params->only_new_results = 1;
127885732ac8SCy Schubert
127985732ac8SCy Schubert if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
128085732ac8SCy Schubert params->ssids[params->num_ssids].ssid = data->ssid;
128185732ac8SCy Schubert params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
128285732ac8SCy Schubert }
128385732ac8SCy Schubert
128485732ac8SCy Schubert if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
128585732ac8SCy Schubert _rand = os_random();
128685732ac8SCy Schubert interval_usec = (_rand % (rand_interval + 1)) * 1024;
128785732ac8SCy Schubert eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
128885732ac8SCy Schubert NULL);
128985732ac8SCy Schubert return 1;
129085732ac8SCy Schubert out_reject:
129185732ac8SCy Schubert if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
129285732ac8SCy Schubert wpas_rrm_report_elem(buf, elem_token, reject_mode,
129385732ac8SCy Schubert MEASURE_TYPE_BEACON, NULL, 0) < 0) {
129485732ac8SCy Schubert wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
129585732ac8SCy Schubert ret = -1;
129685732ac8SCy Schubert }
129785732ac8SCy Schubert out:
129885732ac8SCy Schubert wpas_clear_beacon_rep_data(wpa_s);
129985732ac8SCy Schubert return ret;
130085732ac8SCy Schubert }
130185732ac8SCy Schubert
130285732ac8SCy Schubert
130385732ac8SCy Schubert static int
wpas_rrm_handle_msr_req_element(struct wpa_supplicant * wpa_s,const struct rrm_measurement_request_element * req,struct wpabuf ** buf)130485732ac8SCy Schubert wpas_rrm_handle_msr_req_element(
130585732ac8SCy Schubert struct wpa_supplicant *wpa_s,
130685732ac8SCy Schubert const struct rrm_measurement_request_element *req,
130785732ac8SCy Schubert struct wpabuf **buf)
130885732ac8SCy Schubert {
130985732ac8SCy Schubert int duration_mandatory;
131085732ac8SCy Schubert
131185732ac8SCy Schubert wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
131285732ac8SCy Schubert req->type, req->token);
131385732ac8SCy Schubert
131485732ac8SCy Schubert if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
131585732ac8SCy Schubert /* Enable bit is not supported for now */
131685732ac8SCy Schubert wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
131785732ac8SCy Schubert return 0;
131885732ac8SCy Schubert }
131985732ac8SCy Schubert
132085732ac8SCy Schubert if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
132185732ac8SCy Schubert req->type > MEASURE_TYPE_RPI_HIST) {
132285732ac8SCy Schubert /* Parallel measurements are not supported for now */
132385732ac8SCy Schubert wpa_printf(MSG_DEBUG,
132485732ac8SCy Schubert "RRM: Parallel measurements are not supported, reject");
132585732ac8SCy Schubert goto reject;
132685732ac8SCy Schubert }
132785732ac8SCy Schubert
132885732ac8SCy Schubert duration_mandatory =
132985732ac8SCy Schubert !!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
133085732ac8SCy Schubert
133185732ac8SCy Schubert switch (req->type) {
133285732ac8SCy Schubert case MEASURE_TYPE_LCI:
133385732ac8SCy Schubert return wpas_rrm_build_lci_report(wpa_s, req, buf);
133485732ac8SCy Schubert case MEASURE_TYPE_BEACON:
133585732ac8SCy Schubert if (duration_mandatory &&
133685732ac8SCy Schubert !(wpa_s->drv_rrm_flags &
133785732ac8SCy Schubert WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
133885732ac8SCy Schubert wpa_printf(MSG_DEBUG,
133985732ac8SCy Schubert "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
134085732ac8SCy Schubert goto reject;
134185732ac8SCy Schubert }
134285732ac8SCy Schubert return wpas_rm_handle_beacon_req(wpa_s, req->token,
134385732ac8SCy Schubert duration_mandatory,
134485732ac8SCy Schubert (const void *) req->variable,
134585732ac8SCy Schubert req->len - 3, buf);
134685732ac8SCy Schubert default:
134785732ac8SCy Schubert wpa_printf(MSG_INFO,
134885732ac8SCy Schubert "RRM: Unsupported radio measurement type %u",
134985732ac8SCy Schubert req->type);
135085732ac8SCy Schubert break;
135185732ac8SCy Schubert }
135285732ac8SCy Schubert
135385732ac8SCy Schubert reject:
135485732ac8SCy Schubert if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
135585732ac8SCy Schubert wpas_rrm_report_elem(buf, req->token,
135685732ac8SCy Schubert MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
135785732ac8SCy Schubert req->type, NULL, 0) < 0) {
135885732ac8SCy Schubert wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
135985732ac8SCy Schubert return -1;
136085732ac8SCy Schubert }
136185732ac8SCy Schubert
136285732ac8SCy Schubert return 0;
136385732ac8SCy Schubert }
136485732ac8SCy Schubert
136585732ac8SCy Schubert
136685732ac8SCy Schubert static struct wpabuf *
wpas_rrm_process_msr_req_elems(struct wpa_supplicant * wpa_s,const u8 * pos,size_t len)136785732ac8SCy Schubert wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
136885732ac8SCy Schubert size_t len)
136985732ac8SCy Schubert {
137085732ac8SCy Schubert struct wpabuf *buf = NULL;
137185732ac8SCy Schubert
137285732ac8SCy Schubert while (len) {
137385732ac8SCy Schubert const struct rrm_measurement_request_element *req;
137485732ac8SCy Schubert int res;
137585732ac8SCy Schubert
137685732ac8SCy Schubert if (len < 2) {
137785732ac8SCy Schubert wpa_printf(MSG_DEBUG, "RRM: Truncated element");
137885732ac8SCy Schubert goto out;
137985732ac8SCy Schubert }
138085732ac8SCy Schubert
138185732ac8SCy Schubert req = (const struct rrm_measurement_request_element *) pos;
138285732ac8SCy Schubert if (req->eid != WLAN_EID_MEASURE_REQUEST) {
138385732ac8SCy Schubert wpa_printf(MSG_DEBUG,
138485732ac8SCy Schubert "RRM: Expected Measurement Request element, but EID is %u",
138585732ac8SCy Schubert req->eid);
138685732ac8SCy Schubert goto out;
138785732ac8SCy Schubert }
138885732ac8SCy Schubert
138985732ac8SCy Schubert if (req->len < 3) {
139085732ac8SCy Schubert wpa_printf(MSG_DEBUG, "RRM: Element length too short");
139185732ac8SCy Schubert goto out;
139285732ac8SCy Schubert }
139385732ac8SCy Schubert
139485732ac8SCy Schubert if (req->len > len - 2) {
139585732ac8SCy Schubert wpa_printf(MSG_DEBUG, "RRM: Element length too long");
139685732ac8SCy Schubert goto out;
139785732ac8SCy Schubert }
139885732ac8SCy Schubert
139985732ac8SCy Schubert res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
140085732ac8SCy Schubert if (res < 0)
140185732ac8SCy Schubert goto out;
140285732ac8SCy Schubert
140385732ac8SCy Schubert pos += req->len + 2;
140485732ac8SCy Schubert len -= req->len + 2;
140585732ac8SCy Schubert }
140685732ac8SCy Schubert
140785732ac8SCy Schubert return buf;
140885732ac8SCy Schubert
140985732ac8SCy Schubert out:
141085732ac8SCy Schubert wpabuf_free(buf);
141185732ac8SCy Schubert return NULL;
141285732ac8SCy Schubert }
141385732ac8SCy Schubert
141485732ac8SCy Schubert
wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant * wpa_s,const u8 * src,const u8 * dst,const u8 * frame,size_t len)141585732ac8SCy Schubert void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
141685732ac8SCy Schubert const u8 *src, const u8 *dst,
141785732ac8SCy Schubert const u8 *frame, size_t len)
141885732ac8SCy Schubert {
141985732ac8SCy Schubert struct wpabuf *report;
142085732ac8SCy Schubert
142185732ac8SCy Schubert if (wpa_s->wpa_state != WPA_COMPLETED) {
142285732ac8SCy Schubert wpa_printf(MSG_INFO,
142385732ac8SCy Schubert "RRM: Ignoring radio measurement request: Not associated");
142485732ac8SCy Schubert return;
142585732ac8SCy Schubert }
142685732ac8SCy Schubert
142785732ac8SCy Schubert if (!wpa_s->rrm.rrm_used) {
142885732ac8SCy Schubert wpa_printf(MSG_INFO,
142985732ac8SCy Schubert "RRM: Ignoring radio measurement request: Not RRM network");
143085732ac8SCy Schubert return;
143185732ac8SCy Schubert }
143285732ac8SCy Schubert
143385732ac8SCy Schubert if (len < 3) {
143485732ac8SCy Schubert wpa_printf(MSG_INFO,
143585732ac8SCy Schubert "RRM: Ignoring too short radio measurement request");
143685732ac8SCy Schubert return;
143785732ac8SCy Schubert }
143885732ac8SCy Schubert
143985732ac8SCy Schubert wpa_s->rrm.token = *frame;
144085732ac8SCy Schubert os_memcpy(wpa_s->rrm.dst_addr, dst, ETH_ALEN);
144185732ac8SCy Schubert
144285732ac8SCy Schubert /* Number of repetitions is not supported */
144385732ac8SCy Schubert
144485732ac8SCy Schubert report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
144585732ac8SCy Schubert if (!report)
144685732ac8SCy Schubert return;
144785732ac8SCy Schubert
144885732ac8SCy Schubert wpas_rrm_send_msr_report(wpa_s, report);
144985732ac8SCy Schubert wpabuf_free(report);
145085732ac8SCy Schubert }
145185732ac8SCy Schubert
145285732ac8SCy Schubert
wpas_rrm_handle_link_measurement_request(struct wpa_supplicant * wpa_s,const u8 * src,const u8 * frame,size_t len,int rssi)145385732ac8SCy Schubert void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
145485732ac8SCy Schubert const u8 *src,
145585732ac8SCy Schubert const u8 *frame, size_t len,
145685732ac8SCy Schubert int rssi)
145785732ac8SCy Schubert {
145885732ac8SCy Schubert struct wpabuf *buf;
145985732ac8SCy Schubert const struct rrm_link_measurement_request *req;
146085732ac8SCy Schubert struct rrm_link_measurement_report report;
146185732ac8SCy Schubert
146285732ac8SCy Schubert if (wpa_s->wpa_state != WPA_COMPLETED) {
146385732ac8SCy Schubert wpa_printf(MSG_INFO,
146485732ac8SCy Schubert "RRM: Ignoring link measurement request. Not associated");
146585732ac8SCy Schubert return;
146685732ac8SCy Schubert }
146785732ac8SCy Schubert
146885732ac8SCy Schubert if (!wpa_s->rrm.rrm_used) {
146985732ac8SCy Schubert wpa_printf(MSG_INFO,
147085732ac8SCy Schubert "RRM: Ignoring link measurement request. Not RRM network");
147185732ac8SCy Schubert return;
147285732ac8SCy Schubert }
147385732ac8SCy Schubert
147485732ac8SCy Schubert if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
147585732ac8SCy Schubert wpa_printf(MSG_INFO,
147685732ac8SCy Schubert "RRM: Measurement report failed. TX power insertion not supported");
147785732ac8SCy Schubert return;
147885732ac8SCy Schubert }
147985732ac8SCy Schubert
148085732ac8SCy Schubert req = (const struct rrm_link_measurement_request *) frame;
148185732ac8SCy Schubert if (len < sizeof(*req)) {
148285732ac8SCy Schubert wpa_printf(MSG_INFO,
148385732ac8SCy Schubert "RRM: Link measurement report failed. Request too short");
148485732ac8SCy Schubert return;
148585732ac8SCy Schubert }
148685732ac8SCy Schubert
148785732ac8SCy Schubert os_memset(&report, 0, sizeof(report));
148885732ac8SCy Schubert report.dialog_token = req->dialog_token;
148985732ac8SCy Schubert report.tpc.eid = WLAN_EID_TPC_REPORT;
149085732ac8SCy Schubert report.tpc.len = 2;
149185732ac8SCy Schubert /* Note: The driver is expected to update report.tpc.tx_power and
149285732ac8SCy Schubert * report.tpc.link_margin subfields when sending out this frame.
149385732ac8SCy Schubert * Similarly, the driver would need to update report.rx_ant_id and
149485732ac8SCy Schubert * report.tx_ant_id subfields. */
149585732ac8SCy Schubert report.rsni = 255; /* 255 indicates that RSNI is not available */
149685732ac8SCy Schubert report.rcpi = rssi_to_rcpi(rssi);
149785732ac8SCy Schubert
149885732ac8SCy Schubert /* action_category + action_code */
149985732ac8SCy Schubert buf = wpabuf_alloc(2 + sizeof(report));
150085732ac8SCy Schubert if (buf == NULL) {
150185732ac8SCy Schubert wpa_printf(MSG_ERROR,
150285732ac8SCy Schubert "RRM: Link measurement report failed. Buffer allocation failed");
150385732ac8SCy Schubert return;
150485732ac8SCy Schubert }
150585732ac8SCy Schubert
150685732ac8SCy Schubert wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
150785732ac8SCy Schubert wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
150885732ac8SCy Schubert wpabuf_put_data(buf, &report, sizeof(report));
150985732ac8SCy Schubert wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
151085732ac8SCy Schubert
151185732ac8SCy Schubert if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
151285732ac8SCy Schubert wpa_s->own_addr, wpa_s->bssid,
151385732ac8SCy Schubert wpabuf_head(buf), wpabuf_len(buf), 0)) {
151485732ac8SCy Schubert wpa_printf(MSG_ERROR,
151585732ac8SCy Schubert "RRM: Link measurement report failed. Send action failed");
151685732ac8SCy Schubert }
151785732ac8SCy Schubert wpabuf_free(buf);
151885732ac8SCy Schubert }
151985732ac8SCy Schubert
152085732ac8SCy Schubert
wpas_beacon_rep_scan_match(struct wpa_supplicant * wpa_s,const u8 * bssid)1521*a90b9d01SCy Schubert static bool wpas_beacon_rep_scan_match(struct wpa_supplicant *wpa_s,
1522*a90b9d01SCy Schubert const u8 *bssid)
1523*a90b9d01SCy Schubert {
1524*a90b9d01SCy Schubert u8 i;
1525*a90b9d01SCy Schubert
1526*a90b9d01SCy Schubert if (!wpa_s->valid_links)
1527*a90b9d01SCy Schubert return ether_addr_equal(wpa_s->current_bss->bssid, bssid);
1528*a90b9d01SCy Schubert
1529*a90b9d01SCy Schubert for_each_link(wpa_s->valid_links, i) {
1530*a90b9d01SCy Schubert if (ether_addr_equal(wpa_s->links[i].bssid, bssid))
1531*a90b9d01SCy Schubert return true;
1532*a90b9d01SCy Schubert }
1533*a90b9d01SCy Schubert
1534*a90b9d01SCy Schubert wpa_printf(MSG_DEBUG, "RRM: MLD: no match for TSF BSSID=" MACSTR,
1535*a90b9d01SCy Schubert MAC2STR(bssid));
1536*a90b9d01SCy Schubert
1537*a90b9d01SCy Schubert return false;
1538*a90b9d01SCy Schubert }
1539*a90b9d01SCy Schubert
1540*a90b9d01SCy Schubert
wpas_beacon_rep_scan_process(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res,struct scan_info * info)154185732ac8SCy Schubert int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
154285732ac8SCy Schubert struct wpa_scan_results *scan_res,
154385732ac8SCy Schubert struct scan_info *info)
154485732ac8SCy Schubert {
154585732ac8SCy Schubert size_t i = 0;
154685732ac8SCy Schubert struct wpabuf *buf = NULL;
154785732ac8SCy Schubert
154885732ac8SCy Schubert if (!wpa_s->beacon_rep_data.token)
154985732ac8SCy Schubert return 0;
155085732ac8SCy Schubert
155185732ac8SCy Schubert if (!wpa_s->current_bss)
155285732ac8SCy Schubert goto out;
155385732ac8SCy Schubert
155485732ac8SCy Schubert /* If the measurement was aborted, don't report partial results */
155585732ac8SCy Schubert if (info->aborted)
155685732ac8SCy Schubert goto out;
155785732ac8SCy Schubert
155885732ac8SCy Schubert wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
155985732ac8SCy Schubert MAC2STR(info->scan_start_tsf_bssid),
156085732ac8SCy Schubert MAC2STR(wpa_s->current_bss->bssid));
156185732ac8SCy Schubert if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1562*a90b9d01SCy Schubert !wpas_beacon_rep_scan_match(wpa_s, info->scan_start_tsf_bssid)) {
156385732ac8SCy Schubert wpa_printf(MSG_DEBUG,
156485732ac8SCy Schubert "RRM: Ignore scan results due to mismatching TSF BSSID");
156585732ac8SCy Schubert goto out;
156685732ac8SCy Schubert }
156785732ac8SCy Schubert
156885732ac8SCy Schubert for (i = 0; i < scan_res->num; i++) {
156985732ac8SCy Schubert struct wpa_bss *bss =
157085732ac8SCy Schubert wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
157185732ac8SCy Schubert
157285732ac8SCy Schubert if (!bss)
157385732ac8SCy Schubert continue;
157485732ac8SCy Schubert
157585732ac8SCy Schubert if ((wpa_s->drv_rrm_flags &
157685732ac8SCy Schubert WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1577*a90b9d01SCy Schubert !wpas_beacon_rep_scan_match(wpa_s,
1578*a90b9d01SCy Schubert scan_res->res[i]->tsf_bssid)) {
157985732ac8SCy Schubert wpa_printf(MSG_DEBUG,
158085732ac8SCy Schubert "RRM: Ignore scan result for " MACSTR
158185732ac8SCy Schubert " due to mismatching TSF BSSID" MACSTR,
158285732ac8SCy Schubert MAC2STR(scan_res->res[i]->bssid),
158385732ac8SCy Schubert MAC2STR(scan_res->res[i]->tsf_bssid));
158485732ac8SCy Schubert continue;
158585732ac8SCy Schubert }
158685732ac8SCy Schubert
158785732ac8SCy Schubert /*
158885732ac8SCy Schubert * Don't report results that were not received during the
158985732ac8SCy Schubert * current measurement.
159085732ac8SCy Schubert */
159185732ac8SCy Schubert if (!(wpa_s->drv_rrm_flags &
159285732ac8SCy Schubert WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
159385732ac8SCy Schubert struct os_reltime update_time, diff;
159485732ac8SCy Schubert
159585732ac8SCy Schubert /* For now, allow 8 ms older results due to some
159685732ac8SCy Schubert * unknown issue with cfg80211 BSS table updates during
159785732ac8SCy Schubert * a scan with the current BSS.
159885732ac8SCy Schubert * TODO: Fix this more properly to avoid having to have
159985732ac8SCy Schubert * this type of hacks in place. */
160085732ac8SCy Schubert calculate_update_time(&scan_res->fetch_time,
160185732ac8SCy Schubert scan_res->res[i]->age,
160285732ac8SCy Schubert &update_time);
160385732ac8SCy Schubert os_reltime_sub(&wpa_s->beacon_rep_scan,
160485732ac8SCy Schubert &update_time, &diff);
160585732ac8SCy Schubert if (os_reltime_before(&update_time,
160685732ac8SCy Schubert &wpa_s->beacon_rep_scan) &&
160785732ac8SCy Schubert (diff.sec || diff.usec >= 8000)) {
160885732ac8SCy Schubert wpa_printf(MSG_DEBUG,
160985732ac8SCy Schubert "RRM: Ignore scan result for " MACSTR
161085732ac8SCy Schubert " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
161185732ac8SCy Schubert MAC2STR(scan_res->res[i]->bssid),
161285732ac8SCy Schubert scan_res->res[i]->age,
161385732ac8SCy Schubert (unsigned int) diff.sec,
161485732ac8SCy Schubert (unsigned int) diff.usec);
161585732ac8SCy Schubert continue;
161685732ac8SCy Schubert }
161785732ac8SCy Schubert } else if (info->scan_start_tsf >
161885732ac8SCy Schubert scan_res->res[i]->parent_tsf) {
161985732ac8SCy Schubert continue;
162085732ac8SCy Schubert }
162185732ac8SCy Schubert
162285732ac8SCy Schubert if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
162385732ac8SCy Schubert scan_res->res[i]->parent_tsf) < 0)
162485732ac8SCy Schubert break;
162585732ac8SCy Schubert }
162685732ac8SCy Schubert
162785732ac8SCy Schubert if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
162885732ac8SCy Schubert goto out;
162985732ac8SCy Schubert
163085732ac8SCy Schubert wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
163185732ac8SCy Schubert
163285732ac8SCy Schubert wpas_rrm_send_msr_report(wpa_s, buf);
163385732ac8SCy Schubert wpabuf_free(buf);
163485732ac8SCy Schubert
163585732ac8SCy Schubert out:
163685732ac8SCy Schubert wpas_clear_beacon_rep_data(wpa_s);
163785732ac8SCy Schubert return 1;
163885732ac8SCy Schubert }
163985732ac8SCy Schubert
164085732ac8SCy Schubert
wpas_clear_beacon_rep_data(struct wpa_supplicant * wpa_s)164185732ac8SCy Schubert void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
164285732ac8SCy Schubert {
164385732ac8SCy Schubert struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
164485732ac8SCy Schubert
164585732ac8SCy Schubert eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
164685732ac8SCy Schubert bitfield_free(data->eids);
1647*a90b9d01SCy Schubert bitfield_free(data->ext_eids);
164885732ac8SCy Schubert os_free(data->scan_params.freqs);
164985732ac8SCy Schubert os_memset(data, 0, sizeof(*data));
165085732ac8SCy Schubert }
1651