Home | History | Annotate | Line # | Download | only in wpa_supplicant
rrm.c revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * wpa_supplicant - Radio Measurements
      3  1.1  christos  * Copyright (c) 2003-2016, Jouni Malinen <j (at) w1.fi>
      4  1.1  christos  *
      5  1.1  christos  * This software may be distributed under the terms of the BSD license.
      6  1.1  christos  * See README for more details.
      7  1.1  christos  */
      8  1.1  christos 
      9  1.1  christos #include "includes.h"
     10  1.1  christos 
     11  1.1  christos #include "utils/common.h"
     12  1.1  christos #include "utils/eloop.h"
     13  1.1  christos #include "common/ieee802_11_common.h"
     14  1.1  christos #include "wpa_supplicant_i.h"
     15  1.1  christos #include "driver_i.h"
     16  1.1  christos #include "bss.h"
     17  1.1  christos #include "scan.h"
     18  1.1  christos #include "p2p_supplicant.h"
     19  1.1  christos 
     20  1.1  christos 
     21  1.1  christos static void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
     22  1.1  christos {
     23  1.1  christos 	struct rrm_data *rrm = data;
     24  1.1  christos 
     25  1.1  christos 	if (!rrm->notify_neighbor_rep) {
     26  1.1  christos 		wpa_printf(MSG_ERROR,
     27  1.1  christos 			   "RRM: Unexpected neighbor report timeout");
     28  1.1  christos 		return;
     29  1.1  christos 	}
     30  1.1  christos 
     31  1.1  christos 	wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
     32  1.1  christos 	rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
     33  1.1  christos 
     34  1.1  christos 	rrm->notify_neighbor_rep = NULL;
     35  1.1  christos 	rrm->neighbor_rep_cb_ctx = NULL;
     36  1.1  christos }
     37  1.1  christos 
     38  1.1  christos 
     39  1.1  christos /*
     40  1.1  christos  * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
     41  1.1  christos  * @wpa_s: Pointer to wpa_supplicant
     42  1.1  christos  */
     43  1.1  christos void wpas_rrm_reset(struct wpa_supplicant *wpa_s)
     44  1.1  christos {
     45  1.1  christos 	wpa_s->rrm.rrm_used = 0;
     46  1.1  christos 
     47  1.1  christos 	eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
     48  1.1  christos 			     NULL);
     49  1.1  christos 	if (wpa_s->rrm.notify_neighbor_rep)
     50  1.1  christos 		wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
     51  1.1  christos 	wpa_s->rrm.next_neighbor_rep_token = 1;
     52  1.1  christos 	wpas_clear_beacon_rep_data(wpa_s);
     53  1.1  christos }
     54  1.1  christos 
     55  1.1  christos 
     56  1.1  christos /*
     57  1.1  christos  * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
     58  1.1  christos  * @wpa_s: Pointer to wpa_supplicant
     59  1.1  christos  * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
     60  1.1  christos  * @report_len: Length of neighbor report buffer
     61  1.1  christos  */
     62  1.1  christos void wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
     63  1.1  christos 				   const u8 *report, size_t report_len)
     64  1.1  christos {
     65  1.1  christos 	struct wpabuf *neighbor_rep;
     66  1.1  christos 
     67  1.1  christos 	wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
     68  1.1  christos 	if (report_len < 1)
     69  1.1  christos 		return;
     70  1.1  christos 
     71  1.1  christos 	if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
     72  1.1  christos 		wpa_printf(MSG_DEBUG,
     73  1.1  christos 			   "RRM: Discarding neighbor report with token %d (expected %d)",
     74  1.1  christos 			   report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
     75  1.1  christos 		return;
     76  1.1  christos 	}
     77  1.1  christos 
     78  1.1  christos 	eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
     79  1.1  christos 			     NULL);
     80  1.1  christos 
     81  1.1  christos 	if (!wpa_s->rrm.notify_neighbor_rep) {
     82  1.1  christos 		wpa_printf(MSG_ERROR, "RRM: Unexpected neighbor report");
     83  1.1  christos 		return;
     84  1.1  christos 	}
     85  1.1  christos 
     86  1.1  christos 	/* skipping the first byte, which is only an id (dialog token) */
     87  1.1  christos 	neighbor_rep = wpabuf_alloc(report_len - 1);
     88  1.1  christos 	if (!neighbor_rep) {
     89  1.1  christos 		wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
     90  1.1  christos 		return;
     91  1.1  christos 	}
     92  1.1  christos 	wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
     93  1.1  christos 	wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
     94  1.1  christos 		   report[0]);
     95  1.1  christos 	wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
     96  1.1  christos 				       neighbor_rep);
     97  1.1  christos 	wpa_s->rrm.notify_neighbor_rep = NULL;
     98  1.1  christos 	wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos 
    102  1.1  christos #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
    103  1.1  christos /* Workaround different, undefined for Windows, error codes used here */
    104  1.1  christos #define ENOTCONN -1
    105  1.1  christos #define EOPNOTSUPP -1
    106  1.1  christos #define ECANCELED -1
    107  1.1  christos #endif
    108  1.1  christos 
    109  1.1  christos /* Measurement Request element + Location Subject + Maximum Age subelement */
    110  1.1  christos #define MEASURE_REQUEST_LCI_LEN (3 + 1 + 4)
    111  1.1  christos /* Measurement Request element + Location Civic Request */
    112  1.1  christos #define MEASURE_REQUEST_CIVIC_LEN (3 + 5)
    113  1.1  christos 
    114  1.1  christos 
    115  1.1  christos /**
    116  1.1  christos  * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
    117  1.1  christos  * @wpa_s: Pointer to wpa_supplicant
    118  1.1  christos  * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
    119  1.1  christos  *	  is sent in the request.
    120  1.1  christos  * @lci: if set, neighbor request will include LCI request
    121  1.1  christos  * @civic: if set, neighbor request will include civic location request
    122  1.1  christos  * @cb: Callback function to be called once the requested report arrives, or
    123  1.1  christos  *	timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
    124  1.1  christos  *	In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
    125  1.1  christos  *	the requester's responsibility to free it.
    126  1.1  christos  *	In the latter case NULL will be sent in 'neighbor_rep'.
    127  1.1  christos  * @cb_ctx: Context value to send the callback function
    128  1.1  christos  * Returns: 0 in case of success, negative error code otherwise
    129  1.1  christos  *
    130  1.1  christos  * In case there is a previous request which has not been answered yet, the
    131  1.1  christos  * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
    132  1.1  christos  * Request must contain a callback function.
    133  1.1  christos  */
    134  1.1  christos int wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
    135  1.1  christos 				       const struct wpa_ssid_value *ssid,
    136  1.1  christos 				       int lci, int civic,
    137  1.1  christos 				       void (*cb)(void *ctx,
    138  1.1  christos 						  struct wpabuf *neighbor_rep),
    139  1.1  christos 				       void *cb_ctx)
    140  1.1  christos {
    141  1.1  christos 	struct wpabuf *buf;
    142  1.1  christos 	const u8 *rrm_ie;
    143  1.1  christos 
    144  1.1  christos 	if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
    145  1.1  christos 		wpa_printf(MSG_DEBUG, "RRM: No connection, no RRM.");
    146  1.1  christos 		return -ENOTCONN;
    147  1.1  christos 	}
    148  1.1  christos 
    149  1.1  christos 	if (!wpa_s->rrm.rrm_used) {
    150  1.1  christos 		wpa_printf(MSG_DEBUG, "RRM: No RRM in current connection.");
    151  1.1  christos 		return -EOPNOTSUPP;
    152  1.1  christos 	}
    153  1.1  christos 
    154  1.1  christos 	rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
    155  1.1  christos 				WLAN_EID_RRM_ENABLED_CAPABILITIES);
    156  1.1  christos 	if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
    157  1.1  christos 	    !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
    158  1.1  christos 		wpa_printf(MSG_DEBUG,
    159  1.1  christos 			   "RRM: No network support for Neighbor Report.");
    160  1.1  christos 		return -EOPNOTSUPP;
    161  1.1  christos 	}
    162  1.1  christos 
    163  1.1  christos 	/* Refuse if there's a live request */
    164  1.1  christos 	if (wpa_s->rrm.notify_neighbor_rep) {
    165  1.1  christos 		wpa_printf(MSG_DEBUG,
    166  1.1  christos 			   "RRM: Currently handling previous Neighbor Report.");
    167  1.1  christos 		return -EBUSY;
    168  1.1  christos 	}
    169  1.1  christos 
    170  1.1  christos 	/* 3 = action category + action code + dialog token */
    171  1.1  christos 	buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0) +
    172  1.1  christos 			   (lci ? 2 + MEASURE_REQUEST_LCI_LEN : 0) +
    173  1.1  christos 			   (civic ? 2 + MEASURE_REQUEST_CIVIC_LEN : 0));
    174  1.1  christos 	if (buf == NULL) {
    175  1.1  christos 		wpa_printf(MSG_DEBUG,
    176  1.1  christos 			   "RRM: Failed to allocate Neighbor Report Request");
    177  1.1  christos 		return -ENOMEM;
    178  1.1  christos 	}
    179  1.1  christos 
    180  1.1  christos 	wpa_printf(MSG_DEBUG, "RRM: Neighbor report request (for %s), token=%d",
    181  1.1  christos 		   (ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
    182  1.1  christos 		   wpa_s->rrm.next_neighbor_rep_token);
    183  1.1  christos 
    184  1.1  christos 	wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
    185  1.1  christos 	wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
    186  1.1  christos 	wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
    187  1.1  christos 	if (ssid) {
    188  1.1  christos 		wpabuf_put_u8(buf, WLAN_EID_SSID);
    189  1.1  christos 		wpabuf_put_u8(buf, ssid->ssid_len);
    190  1.1  christos 		wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
    191  1.1  christos 	}
    192  1.1  christos 
    193  1.1  christos 	if (lci) {
    194  1.1  christos 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
    195  1.1  christos 		wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
    196  1.1  christos 		wpabuf_put_u8(buf, MEASURE_REQUEST_LCI_LEN);
    197  1.1  christos 
    198  1.1  christos 		/*
    199  1.1  christos 		 * Measurement token; nonzero number that is unique among the
    200  1.1  christos 		 * Measurement Request elements in a particular frame.
    201  1.1  christos 		 */
    202  1.1  christos 		wpabuf_put_u8(buf, 1); /* Measurement Token */
    203  1.1  christos 
    204  1.1  christos 		/*
    205  1.1  christos 		 * Parallel, Enable, Request, and Report bits are 0, Duration is
    206  1.1  christos 		 * reserved.
    207  1.1  christos 		 */
    208  1.1  christos 		wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
    209  1.1  christos 		wpabuf_put_u8(buf, MEASURE_TYPE_LCI); /* Measurement Type */
    210  1.1  christos 
    211  1.1  christos 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21.10 - LCI request */
    212  1.1  christos 		/* Location Subject */
    213  1.1  christos 		wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
    214  1.1  christos 
    215  1.1  christos 		/* Optional Subelements */
    216  1.1  christos 		/*
    217  1.1  christos 		 * IEEE P802.11-REVmc/D5.0 Figure 9-170
    218  1.1  christos 		 * The Maximum Age subelement is required, otherwise the AP can
    219  1.1  christos 		 * send only data that was determined after receiving the
    220  1.1  christos 		 * request. Setting it here to unlimited age.
    221  1.1  christos 		 */
    222  1.1  christos 		wpabuf_put_u8(buf, LCI_REQ_SUBELEM_MAX_AGE);
    223  1.1  christos 		wpabuf_put_u8(buf, 2);
    224  1.1  christos 		wpabuf_put_le16(buf, 0xffff);
    225  1.1  christos 	}
    226  1.1  christos 
    227  1.1  christos 	if (civic) {
    228  1.1  christos 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
    229  1.1  christos 		wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
    230  1.1  christos 		wpabuf_put_u8(buf, MEASURE_REQUEST_CIVIC_LEN);
    231  1.1  christos 
    232  1.1  christos 		/*
    233  1.1  christos 		 * Measurement token; nonzero number that is unique among the
    234  1.1  christos 		 * Measurement Request elements in a particular frame.
    235  1.1  christos 		 */
    236  1.1  christos 		wpabuf_put_u8(buf, 2); /* Measurement Token */
    237  1.1  christos 
    238  1.1  christos 		/*
    239  1.1  christos 		 * Parallel, Enable, Request, and Report bits are 0, Duration is
    240  1.1  christos 		 * reserved.
    241  1.1  christos 		 */
    242  1.1  christos 		wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
    243  1.1  christos 		/* Measurement Type */
    244  1.1  christos 		wpabuf_put_u8(buf, MEASURE_TYPE_LOCATION_CIVIC);
    245  1.1  christos 
    246  1.1  christos 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21.14:
    247  1.1  christos 		 * Location Civic request */
    248  1.1  christos 		/* Location Subject */
    249  1.1  christos 		wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
    250  1.1  christos 		wpabuf_put_u8(buf, 0); /* Civic Location Type: IETF RFC 4776 */
    251  1.1  christos 		/* Location Service Interval Units: Seconds */
    252  1.1  christos 		wpabuf_put_u8(buf, 0);
    253  1.1  christos 		/* Location Service Interval: 0 - Only one report is requested
    254  1.1  christos 		 */
    255  1.1  christos 		wpabuf_put_le16(buf, 0);
    256  1.1  christos 		/* No optional subelements */
    257  1.1  christos 	}
    258  1.1  christos 
    259  1.1  christos 	wpa_s->rrm.next_neighbor_rep_token++;
    260  1.1  christos 
    261  1.1  christos 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
    262  1.1  christos 				wpa_s->own_addr, wpa_s->bssid,
    263  1.1  christos 				wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
    264  1.1  christos 		wpa_printf(MSG_DEBUG,
    265  1.1  christos 			   "RRM: Failed to send Neighbor Report Request");
    266  1.1  christos 		wpabuf_free(buf);
    267  1.1  christos 		return -ECANCELED;
    268  1.1  christos 	}
    269  1.1  christos 
    270  1.1  christos 	wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
    271  1.1  christos 	wpa_s->rrm.notify_neighbor_rep = cb;
    272  1.1  christos 	eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
    273  1.1  christos 			       wpas_rrm_neighbor_rep_timeout_handler,
    274  1.1  christos 			       &wpa_s->rrm, NULL);
    275  1.1  christos 
    276  1.1  christos 	wpabuf_free(buf);
    277  1.1  christos 	return 0;
    278  1.1  christos }
    279  1.1  christos 
    280  1.1  christos 
    281  1.1  christos static int wpas_rrm_report_elem(struct wpabuf **buf, u8 token, u8 mode, u8 type,
    282  1.1  christos 				const u8 *data, size_t data_len)
    283  1.1  christos {
    284  1.1  christos 	if (wpabuf_resize(buf, 5 + data_len))
    285  1.1  christos 		return -1;
    286  1.1  christos 
    287  1.1  christos 	wpabuf_put_u8(*buf, WLAN_EID_MEASURE_REPORT);
    288  1.1  christos 	wpabuf_put_u8(*buf, 3 + data_len);
    289  1.1  christos 	wpabuf_put_u8(*buf, token);
    290  1.1  christos 	wpabuf_put_u8(*buf, mode);
    291  1.1  christos 	wpabuf_put_u8(*buf, type);
    292  1.1  christos 
    293  1.1  christos 	if (data_len)
    294  1.1  christos 		wpabuf_put_data(*buf, data, data_len);
    295  1.1  christos 
    296  1.1  christos 	return 0;
    297  1.1  christos }
    298  1.1  christos 
    299  1.1  christos 
    300  1.1  christos static int
    301  1.1  christos wpas_rrm_build_lci_report(struct wpa_supplicant *wpa_s,
    302  1.1  christos 			  const struct rrm_measurement_request_element *req,
    303  1.1  christos 			  struct wpabuf **buf)
    304  1.1  christos {
    305  1.1  christos 	u8 subject;
    306  1.1  christos 	u16 max_age = 0;
    307  1.1  christos 	struct os_reltime t, diff;
    308  1.1  christos 	unsigned long diff_l;
    309  1.1  christos 	const u8 *subelem;
    310  1.1  christos 	const u8 *request = req->variable;
    311  1.1  christos 	size_t len = req->len - 3;
    312  1.1  christos 
    313  1.1  christos 	if (len < 1)
    314  1.1  christos 		return -1;
    315  1.1  christos 
    316  1.1  christos 	if (!wpa_s->lci)
    317  1.1  christos 		goto reject;
    318  1.1  christos 
    319  1.1  christos 	subject = *request++;
    320  1.1  christos 	len--;
    321  1.1  christos 
    322  1.1  christos 	wpa_printf(MSG_DEBUG, "Measurement request location subject=%u",
    323  1.1  christos 		   subject);
    324  1.1  christos 
    325  1.1  christos 	if (subject != LOCATION_SUBJECT_REMOTE) {
    326  1.1  christos 		wpa_printf(MSG_INFO,
    327  1.1  christos 			   "Not building LCI report - bad location subject");
    328  1.1  christos 		return 0;
    329  1.1  christos 	}
    330  1.1  christos 
    331  1.1  christos 	/* Subelements are formatted exactly like elements */
    332  1.1  christos 	wpa_hexdump(MSG_DEBUG, "LCI request subelements", request, len);
    333  1.1  christos 	subelem = get_ie(request, len, LCI_REQ_SUBELEM_MAX_AGE);
    334  1.1  christos 	if (subelem && subelem[1] == 2)
    335  1.1  christos 		max_age = WPA_GET_LE16(subelem + 2);
    336  1.1  christos 
    337  1.1  christos 	if (os_get_reltime(&t))
    338  1.1  christos 		goto reject;
    339  1.1  christos 
    340  1.1  christos 	os_reltime_sub(&t, &wpa_s->lci_time, &diff);
    341  1.1  christos 	/* LCI age is calculated in 10th of a second units. */
    342  1.1  christos 	diff_l = diff.sec * 10 + diff.usec / 100000;
    343  1.1  christos 
    344  1.1  christos 	if (max_age != 0xffff && max_age < diff_l)
    345  1.1  christos 		goto reject;
    346  1.1  christos 
    347  1.1  christos 	if (wpas_rrm_report_elem(buf, req->token,
    348  1.1  christos 				 MEASUREMENT_REPORT_MODE_ACCEPT, req->type,
    349  1.1  christos 				 wpabuf_head_u8(wpa_s->lci),
    350  1.1  christos 				 wpabuf_len(wpa_s->lci)) < 0) {
    351  1.1  christos 		wpa_printf(MSG_DEBUG, "Failed to add LCI report element");
    352  1.1  christos 		return -1;
    353  1.1  christos 	}
    354  1.1  christos 
    355  1.1  christos 	return 0;
    356  1.1  christos 
    357  1.1  christos reject:
    358  1.1  christos 	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
    359  1.1  christos 	    wpas_rrm_report_elem(buf, req->token,
    360  1.1  christos 				 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
    361  1.1  christos 				 req->type, NULL, 0) < 0) {
    362  1.1  christos 		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
    363  1.1  christos 		return -1;
    364  1.1  christos 	}
    365  1.1  christos 
    366  1.1  christos 	return 0;
    367  1.1  christos }
    368  1.1  christos 
    369  1.1  christos 
    370  1.1  christos static void wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant *wpa_s,
    371  1.1  christos 					  const u8 *data, size_t len)
    372  1.1  christos {
    373  1.1  christos 	struct wpabuf *report = wpabuf_alloc(len + 3);
    374  1.1  christos 
    375  1.1  christos 	if (!report)
    376  1.1  christos 		return;
    377  1.1  christos 
    378  1.1  christos 	wpabuf_put_u8(report, WLAN_ACTION_RADIO_MEASUREMENT);
    379  1.1  christos 	wpabuf_put_u8(report, WLAN_RRM_RADIO_MEASUREMENT_REPORT);
    380  1.1  christos 	wpabuf_put_u8(report, wpa_s->rrm.token);
    381  1.1  christos 
    382  1.1  christos 	wpabuf_put_data(report, data, len);
    383  1.1  christos 
    384  1.1  christos 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
    385  1.1  christos 				wpa_s->own_addr, wpa_s->bssid,
    386  1.1  christos 				wpabuf_head(report), wpabuf_len(report), 0)) {
    387  1.1  christos 		wpa_printf(MSG_ERROR,
    388  1.1  christos 			   "RRM: Radio measurement report failed: Sending Action frame failed");
    389  1.1  christos 	}
    390  1.1  christos 
    391  1.1  christos 	wpabuf_free(report);
    392  1.1  christos }
    393  1.1  christos 
    394  1.1  christos 
    395  1.1  christos static void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
    396  1.1  christos 				     struct wpabuf *buf)
    397  1.1  christos {
    398  1.1  christos 	int len = wpabuf_len(buf);
    399  1.1  christos 	const u8 *pos = wpabuf_head_u8(buf), *next = pos;
    400  1.1  christos 
    401  1.1  christos #define MPDU_REPORT_LEN (int) (IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN - 3)
    402  1.1  christos 
    403  1.1  christos 	while (len) {
    404  1.1  christos 		int send_len = (len > MPDU_REPORT_LEN) ? next - pos : len;
    405  1.1  christos 
    406  1.1  christos 		if (send_len == len ||
    407  1.1  christos 		    (send_len + next[1] + 2) > MPDU_REPORT_LEN) {
    408  1.1  christos 			wpas_rrm_send_msr_report_mpdu(wpa_s, pos, send_len);
    409  1.1  christos 			len -= send_len;
    410  1.1  christos 			pos = next;
    411  1.1  christos 		}
    412  1.1  christos 
    413  1.1  christos 		if (len)
    414  1.1  christos 			next += next[1] + 2;
    415  1.1  christos 	}
    416  1.1  christos #undef MPDU_REPORT_LEN
    417  1.1  christos }
    418  1.1  christos 
    419  1.1  christos 
    420  1.1  christos static int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
    421  1.1  christos 			    int *freqs)
    422  1.1  christos {
    423  1.1  christos 	size_t i;
    424  1.1  christos 
    425  1.1  christos 	for (i = 0; i < num_primary_channels; i++) {
    426  1.1  christos 		u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
    427  1.1  christos 
    428  1.1  christos 		freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
    429  1.1  christos 		/* ieee80211_chan_to_freq() is not really meant for this
    430  1.1  christos 		 * conversion of 20 MHz primary channel numbers for wider VHT
    431  1.1  christos 		 * channels, so handle those as special cases here for now. */
    432  1.1  christos 		if (freqs[i] < 0 &&
    433  1.1  christos 		    (op_class == 128 || op_class == 129 || op_class == 130))
    434  1.1  christos 			freqs[i] = 5000 + 5 * primary_chan;
    435  1.1  christos 		if (freqs[i] < 0) {
    436  1.1  christos 			wpa_printf(MSG_DEBUG,
    437  1.1  christos 				   "Beacon Report: Invalid channel %u",
    438  1.1  christos 				   chan);
    439  1.1  christos 			return -1;
    440  1.1  christos 		}
    441  1.1  christos 	}
    442  1.1  christos 
    443  1.1  christos 	return 0;
    444  1.1  christos }
    445  1.1  christos 
    446  1.1  christos 
    447  1.1  christos static int * wpas_add_channels(const struct oper_class_map *op,
    448  1.1  christos 			       struct hostapd_hw_modes *mode, int active,
    449  1.1  christos 			       const u8 *channels, const u8 size)
    450  1.1  christos {
    451  1.1  christos 	int *freqs, *next_freq;
    452  1.1  christos 	u8 num_primary_channels, i;
    453  1.1  christos 	u8 num_chans;
    454  1.1  christos 
    455  1.1  christos 	num_chans = channels ? size :
    456  1.1  christos 		(op->max_chan - op->min_chan) / op->inc + 1;
    457  1.1  christos 
    458  1.1  christos 	if (op->bw == BW80 || op->bw == BW80P80)
    459  1.1  christos 		num_primary_channels = 4;
    460  1.1  christos 	else if (op->bw == BW160)
    461  1.1  christos 		num_primary_channels = 8;
    462  1.1  christos 	else
    463  1.1  christos 		num_primary_channels = 1;
    464  1.1  christos 
    465  1.1  christos 	/* one extra place for the zero-terminator */
    466  1.1  christos 	freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
    467  1.1  christos 	if (!freqs) {
    468  1.1  christos 		wpa_printf(MSG_ERROR,
    469  1.1  christos 			   "Beacon Report: Failed to allocate freqs array");
    470  1.1  christos 		return NULL;
    471  1.1  christos 	}
    472  1.1  christos 
    473  1.1  christos 	next_freq = freqs;
    474  1.1  christos 	for  (i = 0; i < num_chans; i++) {
    475  1.1  christos 		u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
    476  1.1  christos 		enum chan_allowed res = verify_channel(mode, chan, op->bw);
    477  1.1  christos 
    478  1.1  christos 		if (res == NOT_ALLOWED || (res == NO_IR && active))
    479  1.1  christos 			continue;
    480  1.1  christos 
    481  1.1  christos 		if (wpas_add_channel(op->op_class, chan, num_primary_channels,
    482  1.1  christos 				     next_freq) < 0) {
    483  1.1  christos 			os_free(freqs);
    484  1.1  christos 			return NULL;
    485  1.1  christos 		}
    486  1.1  christos 
    487  1.1  christos 		next_freq += num_primary_channels;
    488  1.1  christos 	}
    489  1.1  christos 
    490  1.1  christos 	if (!freqs[0]) {
    491  1.1  christos 		os_free(freqs);
    492  1.1  christos 		return NULL;
    493  1.1  christos 	}
    494  1.1  christos 
    495  1.1  christos 	return freqs;
    496  1.1  christos }
    497  1.1  christos 
    498  1.1  christos 
    499  1.1  christos static int * wpas_op_class_freqs(const struct oper_class_map *op,
    500  1.1  christos 				 struct hostapd_hw_modes *mode, int active)
    501  1.1  christos {
    502  1.1  christos 	u8 channels_80mhz[] = { 42, 58, 106, 122, 138, 155 };
    503  1.1  christos 	u8 channels_160mhz[] = { 50, 114 };
    504  1.1  christos 
    505  1.1  christos 	/*
    506  1.1  christos 	 * When adding all channels in the operating class, 80 + 80 MHz
    507  1.1  christos 	 * operating classes are like 80 MHz channels because we add all valid
    508  1.1  christos 	 * channels anyway.
    509  1.1  christos 	 */
    510  1.1  christos 	if (op->bw == BW80 || op->bw == BW80P80)
    511  1.1  christos 		return wpas_add_channels(op, mode, active, channels_80mhz,
    512  1.1  christos 					 ARRAY_SIZE(channels_80mhz));
    513  1.1  christos 
    514  1.1  christos 	if (op->bw == BW160)
    515  1.1  christos 		return wpas_add_channels(op, mode, active, channels_160mhz,
    516  1.1  christos 					 ARRAY_SIZE(channels_160mhz));
    517  1.1  christos 
    518  1.1  christos 	return wpas_add_channels(op, mode, active, NULL, 0);
    519  1.1  christos }
    520  1.1  christos 
    521  1.1  christos 
    522  1.1  christos static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s, int active,
    523  1.1  christos 				       const char *country, const u8 *subelems,
    524  1.1  christos 				       size_t len)
    525  1.1  christos {
    526  1.1  christos 	int *freqs = NULL, *new_freqs;
    527  1.1  christos 	const u8 *end = subelems + len;
    528  1.1  christos 
    529  1.1  christos 	while (end - subelems > 2) {
    530  1.1  christos 		const struct oper_class_map *op;
    531  1.1  christos 		const u8 *ap_chan_elem, *pos;
    532  1.1  christos 		u8 left;
    533  1.1  christos 		struct hostapd_hw_modes *mode;
    534  1.1  christos 
    535  1.1  christos 		ap_chan_elem = get_ie(subelems, end - subelems,
    536  1.1  christos 				      WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
    537  1.1  christos 		if (!ap_chan_elem)
    538  1.1  christos 			break;
    539  1.1  christos 		pos = ap_chan_elem + 2;
    540  1.1  christos 		left = ap_chan_elem[1];
    541  1.1  christos 		if (left < 1)
    542  1.1  christos 			break;
    543  1.1  christos 		subelems = ap_chan_elem + 2 + left;
    544  1.1  christos 
    545  1.1  christos 		op = get_oper_class(country, *pos);
    546  1.1  christos 		if (!op) {
    547  1.1  christos 			wpa_printf(MSG_DEBUG,
    548  1.1  christos 				   "Beacon request: unknown operating class in AP Channel Report subelement %u",
    549  1.1  christos 				   *pos);
    550  1.1  christos 			goto out;
    551  1.1  christos 		}
    552  1.1  christos 		pos++;
    553  1.1  christos 		left--;
    554  1.1  christos 
    555  1.1  christos 		mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
    556  1.1  christos 		if (!mode)
    557  1.1  christos 			continue;
    558  1.1  christos 
    559  1.1  christos 		/*
    560  1.1  christos 		 * For 80 + 80 MHz operating classes, this AP Channel Report
    561  1.1  christos 		 * element should be followed by another element specifying
    562  1.1  christos 		 * the second 80 MHz channel. For now just add this 80 MHz
    563  1.1  christos 		 * channel, the second 80 MHz channel will be added when the
    564  1.1  christos 		 * next element is parsed.
    565  1.1  christos 		 * TODO: Verify that this AP Channel Report element is followed
    566  1.1  christos 		 * by a corresponding AP Channel Report element as specified in
    567  1.1  christos 		 * IEEE Std 802.11-2016, 11.11.9.1.
    568  1.1  christos 		 */
    569  1.1  christos 		new_freqs = wpas_add_channels(op, mode, active, pos, left);
    570  1.1  christos 		if (new_freqs)
    571  1.1  christos 			int_array_concat(&freqs, new_freqs);
    572  1.1  christos 
    573  1.1  christos 		os_free(new_freqs);
    574  1.1  christos 	}
    575  1.1  christos 
    576  1.1  christos 	return freqs;
    577  1.1  christos out:
    578  1.1  christos 	os_free(freqs);
    579  1.1  christos 	return NULL;
    580  1.1  christos }
    581  1.1  christos 
    582  1.1  christos 
    583  1.1  christos static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
    584  1.1  christos 				       u8 op_class, u8 chan, int active,
    585  1.1  christos 				       const u8 *subelems, size_t len)
    586  1.1  christos {
    587  1.1  christos 	int *freqs = NULL, *ext_freqs = NULL;
    588  1.1  christos 	struct hostapd_hw_modes *mode;
    589  1.1  christos 	const char *country = NULL;
    590  1.1  christos 	const struct oper_class_map *op;
    591  1.1  christos 	const u8 *elem;
    592  1.1  christos 
    593  1.1  christos 	if (!wpa_s->current_bss)
    594  1.1  christos 		return NULL;
    595  1.1  christos 	elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
    596  1.1  christos 	if (elem && elem[1] >= 2)
    597  1.1  christos 		country = (const char *) (elem + 2);
    598  1.1  christos 
    599  1.1  christos 	op = get_oper_class(country, op_class);
    600  1.1  christos 	if (!op) {
    601  1.1  christos 		wpa_printf(MSG_DEBUG,
    602  1.1  christos 			   "Beacon request: invalid operating class %d",
    603  1.1  christos 			   op_class);
    604  1.1  christos 		return NULL;
    605  1.1  christos 	}
    606  1.1  christos 
    607  1.1  christos 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
    608  1.1  christos 	if (!mode)
    609  1.1  christos 		return NULL;
    610  1.1  christos 
    611  1.1  christos 	switch (chan) {
    612  1.1  christos 	case 0:
    613  1.1  christos 		freqs = wpas_op_class_freqs(op, mode, active);
    614  1.1  christos 		if (!freqs)
    615  1.1  christos 			return NULL;
    616  1.1  christos 		break;
    617  1.1  christos 	case 255:
    618  1.1  christos 		/* freqs will be added from AP channel subelements */
    619  1.1  christos 		break;
    620  1.1  christos 	default:
    621  1.1  christos 		freqs = wpas_add_channels(op, mode, active, &chan, 1);
    622  1.1  christos 		if (!freqs)
    623  1.1  christos 			return NULL;
    624  1.1  christos 		break;
    625  1.1  christos 	}
    626  1.1  christos 
    627  1.1  christos 	ext_freqs = wpas_channel_report_freqs(wpa_s, active, country, subelems,
    628  1.1  christos 					      len);
    629  1.1  christos 	if (ext_freqs) {
    630  1.1  christos 		int_array_concat(&freqs, ext_freqs);
    631  1.1  christos 		os_free(ext_freqs);
    632  1.1  christos 		int_array_sort_unique(freqs);
    633  1.1  christos 	}
    634  1.1  christos 
    635  1.1  christos 	return freqs;
    636  1.1  christos }
    637  1.1  christos 
    638  1.1  christos 
    639  1.1  christos static int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
    640  1.1  christos 				u8 *op_class, u8 *chan, u8 *phy_type)
    641  1.1  christos {
    642  1.1  christos 	const u8 *ie;
    643  1.1  christos 	int sec_chan = 0, vht = 0;
    644  1.1  christos 	struct ieee80211_ht_operation *ht_oper = NULL;
    645  1.1  christos 	struct ieee80211_vht_operation *vht_oper = NULL;
    646  1.1  christos 	u8 seg0, seg1;
    647  1.1  christos 
    648  1.1  christos 	ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
    649  1.1  christos 	if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
    650  1.1  christos 		u8 sec_chan_offset;
    651  1.1  christos 
    652  1.1  christos 		ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
    653  1.1  christos 		sec_chan_offset = ht_oper->ht_param &
    654  1.1  christos 			HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
    655  1.1  christos 		if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
    656  1.1  christos 			sec_chan = 1;
    657  1.1  christos 		else if (sec_chan_offset ==
    658  1.1  christos 			 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
    659  1.1  christos 			sec_chan = -1;
    660  1.1  christos 	}
    661  1.1  christos 
    662  1.1  christos 	ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
    663  1.1  christos 	if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
    664  1.1  christos 		vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
    665  1.1  christos 
    666  1.1  christos 		switch (vht_oper->vht_op_info_chwidth) {
    667  1.1  christos 		case 1:
    668  1.1  christos 			seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
    669  1.1  christos 			seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
    670  1.1  christos 			if (seg1 && abs(seg1 - seg0) == 8)
    671  1.1  christos 				vht = VHT_CHANWIDTH_160MHZ;
    672  1.1  christos 			else if (seg1)
    673  1.1  christos 				vht = VHT_CHANWIDTH_80P80MHZ;
    674  1.1  christos 			else
    675  1.1  christos 				vht = VHT_CHANWIDTH_80MHZ;
    676  1.1  christos 			break;
    677  1.1  christos 		case 2:
    678  1.1  christos 			vht = VHT_CHANWIDTH_160MHZ;
    679  1.1  christos 			break;
    680  1.1  christos 		case 3:
    681  1.1  christos 			vht = VHT_CHANWIDTH_80P80MHZ;
    682  1.1  christos 			break;
    683  1.1  christos 		default:
    684  1.1  christos 			vht = VHT_CHANWIDTH_USE_HT;
    685  1.1  christos 			break;
    686  1.1  christos 		}
    687  1.1  christos 	}
    688  1.1  christos 
    689  1.1  christos 	if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
    690  1.1  christos 					  chan) == NUM_HOSTAPD_MODES) {
    691  1.1  christos 		wpa_printf(MSG_DEBUG,
    692  1.1  christos 			   "Cannot determine operating class and channel");
    693  1.1  christos 		return -1;
    694  1.1  christos 	}
    695  1.1  christos 
    696  1.1  christos 	*phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
    697  1.1  christos 					   vht_oper != NULL);
    698  1.1  christos 	if (*phy_type == PHY_TYPE_UNSPECIFIED) {
    699  1.1  christos 		wpa_printf(MSG_DEBUG, "Cannot determine phy type");
    700  1.1  christos 		return -1;
    701  1.1  christos 	}
    702  1.1  christos 
    703  1.1  christos 	return 0;
    704  1.1  christos }
    705  1.1  christos 
    706  1.1  christos 
    707  1.1  christos static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
    708  1.1  christos 					  enum beacon_report_detail detail,
    709  1.1  christos 					  struct wpa_bss *bss, u8 *buf,
    710  1.1  christos 					  size_t buf_len)
    711  1.1  christos {
    712  1.1  christos 	u8 *ies = (u8 *) (bss + 1);
    713  1.1  christos 	size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
    714  1.1  christos 	u8 *pos = buf;
    715  1.1  christos 	int rem_len;
    716  1.1  christos 
    717  1.1  christos 	rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
    718  1.1  christos 		sizeof(struct rrm_measurement_report_element) - 2;
    719  1.1  christos 
    720  1.1  christos 	if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
    721  1.1  christos 		wpa_printf(MSG_DEBUG,
    722  1.1  christos 			   "Beacon Request: Invalid reporting detail: %d",
    723  1.1  christos 			   detail);
    724  1.1  christos 		return -1;
    725  1.1  christos 	}
    726  1.1  christos 
    727  1.1  christos 	if (detail == BEACON_REPORT_DETAIL_NONE)
    728  1.1  christos 		return 0;
    729  1.1  christos 
    730  1.1  christos 	/*
    731  1.1  christos 	 * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
    732  1.1  christos 	 * beacon interval(2) + capabilities(2) = 14 bytes
    733  1.1  christos 	 */
    734  1.1  christos 	if (buf_len < 14)
    735  1.1  christos 		return 0;
    736  1.1  christos 
    737  1.1  christos 	*pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
    738  1.1  christos 	/* The length will be filled later */
    739  1.1  christos 	pos++;
    740  1.1  christos 	WPA_PUT_LE64(pos, bss->tsf);
    741  1.1  christos 	pos += sizeof(bss->tsf);
    742  1.1  christos 	WPA_PUT_LE16(pos, bss->beacon_int);
    743  1.1  christos 	pos += 2;
    744  1.1  christos 	WPA_PUT_LE16(pos, bss->caps);
    745  1.1  christos 	pos += 2;
    746  1.1  christos 
    747  1.1  christos 	rem_len -= pos - buf;
    748  1.1  christos 
    749  1.1  christos 	/*
    750  1.1  christos 	 * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
    751  1.1  christos 	 * body subelement causes the element to exceed the maximum element
    752  1.1  christos 	 * size, the subelement is truncated so that the last IE is a complete
    753  1.1  christos 	 * IE. So even when required to report all IEs, add elements one after
    754  1.1  christos 	 * the other and stop once there is no more room in the measurement
    755  1.1  christos 	 * element.
    756  1.1  christos 	 */
    757  1.1  christos 	while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
    758  1.1  christos 		if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
    759  1.1  christos 		    (eids && bitfield_is_set(eids, ies[0]))) {
    760  1.1  christos 			u8 eid = ies[0], elen = ies[1];
    761  1.1  christos 
    762  1.1  christos 			if ((eid == WLAN_EID_TIM || eid == WLAN_EID_RSN) &&
    763  1.1  christos 			    elen > 4)
    764  1.1  christos 				elen = 4;
    765  1.1  christos 			/*
    766  1.1  christos 			 * TODO: Truncate IBSS DFS element as described in
    767  1.1  christos 			 * IEEE Std 802.11-2016, 9.4.2.22.7.
    768  1.1  christos 			 */
    769  1.1  christos 
    770  1.1  christos 			if (2 + elen > buf + buf_len - pos ||
    771  1.1  christos 			    2 + elen > rem_len)
    772  1.1  christos 				break;
    773  1.1  christos 
    774  1.1  christos 			*pos++ = ies[0];
    775  1.1  christos 			*pos++ = elen;
    776  1.1  christos 			os_memcpy(pos, ies + 2, elen);
    777  1.1  christos 			pos += elen;
    778  1.1  christos 			rem_len -= 2 + elen;
    779  1.1  christos 		}
    780  1.1  christos 
    781  1.1  christos 		ies_len -= 2 + ies[1];
    782  1.1  christos 		ies += 2 + ies[1];
    783  1.1  christos 	}
    784  1.1  christos 
    785  1.1  christos 	/* Now the length is known */
    786  1.1  christos 	buf[1] = pos - buf - 2;
    787  1.1  christos 	return pos - buf;
    788  1.1  christos }
    789  1.1  christos 
    790  1.1  christos 
    791  1.1  christos static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
    792  1.1  christos 			       struct wpabuf **wpa_buf, struct wpa_bss *bss,
    793  1.1  christos 			       u64 start, u64 parent_tsf)
    794  1.1  christos {
    795  1.1  christos 	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
    796  1.1  christos 	u8 *ie = (u8 *) (bss + 1);
    797  1.1  christos 	size_t ie_len = bss->ie_len + bss->beacon_ie_len;
    798  1.1  christos 	int ret;
    799  1.1  christos 	u8 *buf;
    800  1.1  christos 	struct rrm_measurement_beacon_report *rep;
    801  1.1  christos 
    802  1.1  christos 	if (os_memcmp(data->bssid, broadcast_ether_addr, ETH_ALEN) != 0 &&
    803  1.1  christos 	    os_memcmp(data->bssid, bss->bssid, ETH_ALEN) != 0)
    804  1.1  christos 		return 0;
    805  1.1  christos 
    806  1.1  christos 	if (data->ssid_len &&
    807  1.1  christos 	    (data->ssid_len != bss->ssid_len ||
    808  1.1  christos 	     os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
    809  1.1  christos 		return 0;
    810  1.1  christos 
    811  1.1  christos 	/* Maximum element length: beacon report element + reported frame body
    812  1.1  christos 	 * subelement + all IEs of the reported beacon */
    813  1.1  christos 	buf = os_malloc(sizeof(*rep) + 14 + ie_len);
    814  1.1  christos 	if (!buf)
    815  1.1  christos 		return -1;
    816  1.1  christos 
    817  1.1  christos 	rep = (struct rrm_measurement_beacon_report *) buf;
    818  1.1  christos 	if (wpas_get_op_chan_phy(bss->freq, ie, ie_len, &rep->op_class,
    819  1.1  christos 				 &rep->channel, &rep->report_info) < 0) {
    820  1.1  christos 		ret = 0;
    821  1.1  christos 		goto out;
    822  1.1  christos 	}
    823  1.1  christos 
    824  1.1  christos 	rep->start_time = host_to_le64(start);
    825  1.1  christos 	rep->duration = host_to_le16(data->scan_params.duration);
    826  1.1  christos 	rep->rcpi = rssi_to_rcpi(bss->level);
    827  1.1  christos 	rep->rsni = 255; /* 255 indicates that RSNI is not available */
    828  1.1  christos 	os_memcpy(rep->bssid, bss->bssid, ETH_ALEN);
    829  1.1  christos 	rep->antenna_id = 0; /* unknown */
    830  1.1  christos 	rep->parent_tsf = host_to_le32(parent_tsf);
    831  1.1  christos 
    832  1.1  christos 	ret = wpas_beacon_rep_add_frame_body(data->eids, data->report_detail,
    833  1.1  christos 					     bss, rep->variable, 14 + ie_len);
    834  1.1  christos 	if (ret < 0)
    835  1.1  christos 		goto out;
    836  1.1  christos 
    837  1.1  christos 	ret = wpas_rrm_report_elem(wpa_buf, wpa_s->beacon_rep_data.token,
    838  1.1  christos 				   MEASUREMENT_REPORT_MODE_ACCEPT,
    839  1.1  christos 				   MEASURE_TYPE_BEACON, buf,
    840  1.1  christos 				   ret + sizeof(*rep));
    841  1.1  christos out:
    842  1.1  christos 	os_free(buf);
    843  1.1  christos 	return ret;
    844  1.1  christos }
    845  1.1  christos 
    846  1.1  christos 
    847  1.1  christos static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
    848  1.1  christos 				      struct wpabuf **buf)
    849  1.1  christos {
    850  1.1  christos 	return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
    851  1.1  christos 				    MEASUREMENT_REPORT_MODE_ACCEPT,
    852  1.1  christos 				    MEASURE_TYPE_BEACON, NULL, 0);
    853  1.1  christos }
    854  1.1  christos 
    855  1.1  christos 
    856  1.1  christos static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
    857  1.1  christos 				  struct wpabuf **buf)
    858  1.1  christos {
    859  1.1  christos 	size_t i;
    860  1.1  christos 
    861  1.1  christos 	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
    862  1.1  christos 		if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
    863  1.1  christos 					0, 0) < 0)
    864  1.1  christos 			break;
    865  1.1  christos 	}
    866  1.1  christos 
    867  1.1  christos 	if (!(*buf))
    868  1.1  christos 		wpas_beacon_rep_no_results(wpa_s, buf);
    869  1.1  christos 
    870  1.1  christos 	wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
    871  1.1  christos }
    872  1.1  christos 
    873  1.1  christos 
    874  1.1  christos void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
    875  1.1  christos {
    876  1.1  christos 	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr)) {
    877  1.1  christos 		struct wpabuf *buf = NULL;
    878  1.1  christos 
    879  1.1  christos 		if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
    880  1.1  christos 					 MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
    881  1.1  christos 					 MEASURE_TYPE_BEACON, NULL, 0)) {
    882  1.1  christos 			wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
    883  1.1  christos 			wpabuf_free(buf);
    884  1.1  christos 			return;
    885  1.1  christos 		}
    886  1.1  christos 
    887  1.1  christos 		wpas_rrm_send_msr_report(wpa_s, buf);
    888  1.1  christos 		wpabuf_free(buf);
    889  1.1  christos 	}
    890  1.1  christos 
    891  1.1  christos 	wpas_clear_beacon_rep_data(wpa_s);
    892  1.1  christos }
    893  1.1  christos 
    894  1.1  christos 
    895  1.1  christos static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
    896  1.1  christos {
    897  1.1  christos 	struct wpa_supplicant *wpa_s = eloop_ctx;
    898  1.1  christos 	struct wpa_driver_scan_params *params =
    899  1.1  christos 		&wpa_s->beacon_rep_data.scan_params;
    900  1.1  christos 	u16 prev_duration = params->duration;
    901  1.1  christos 
    902  1.1  christos 	if (!wpa_s->current_bss)
    903  1.1  christos 		return;
    904  1.1  christos 
    905  1.1  christos 	if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
    906  1.1  christos 	    params->duration) {
    907  1.1  christos 		wpa_printf(MSG_DEBUG,
    908  1.1  christos 			   "RRM: Cannot set scan duration due to missing driver support");
    909  1.1  christos 		params->duration = 0;
    910  1.1  christos 	}
    911  1.1  christos 	os_get_reltime(&wpa_s->beacon_rep_scan);
    912  1.1  christos 	if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
    913  1.1  christos 	    wpa_supplicant_trigger_scan(wpa_s, params))
    914  1.1  christos 		wpas_rrm_refuse_request(wpa_s);
    915  1.1  christos 	params->duration = prev_duration;
    916  1.1  christos }
    917  1.1  christos 
    918  1.1  christos 
    919  1.1  christos static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
    920  1.1  christos 					     struct beacon_rep_data *data,
    921  1.1  christos 					     u8 sid, u8 slen, const u8 *subelem)
    922  1.1  christos {
    923  1.1  christos 	u8 report_info, i;
    924  1.1  christos 
    925  1.1  christos 	switch (sid) {
    926  1.1  christos 	case WLAN_BEACON_REQUEST_SUBELEM_SSID:
    927  1.1  christos 		if (!slen) {
    928  1.1  christos 			wpa_printf(MSG_DEBUG,
    929  1.1  christos 				   "SSID subelement with zero length - wildcard SSID");
    930  1.1  christos 			break;
    931  1.1  christos 		}
    932  1.1  christos 
    933  1.1  christos 		if (slen > SSID_MAX_LEN) {
    934  1.1  christos 			wpa_printf(MSG_DEBUG,
    935  1.1  christos 				   "Invalid SSID subelement length: %u", slen);
    936  1.1  christos 			return -1;
    937  1.1  christos 		}
    938  1.1  christos 
    939  1.1  christos 		data->ssid_len = slen;
    940  1.1  christos 		os_memcpy(data->ssid, subelem, data->ssid_len);
    941  1.1  christos 		break;
    942  1.1  christos 	case WLAN_BEACON_REQUEST_SUBELEM_INFO:
    943  1.1  christos 		if (slen != 2) {
    944  1.1  christos 			wpa_printf(MSG_DEBUG,
    945  1.1  christos 				   "Invalid reporting information subelement length: %u",
    946  1.1  christos 				   slen);
    947  1.1  christos 			return -1;
    948  1.1  christos 		}
    949  1.1  christos 
    950  1.1  christos 		report_info = subelem[0];
    951  1.1  christos 		if (report_info != 0) {
    952  1.1  christos 			wpa_printf(MSG_DEBUG,
    953  1.1  christos 				   "reporting information=%u is not supported",
    954  1.1  christos 				   report_info);
    955  1.1  christos 			return 0;
    956  1.1  christos 		}
    957  1.1  christos 		break;
    958  1.1  christos 	case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
    959  1.1  christos 		if (slen != 1) {
    960  1.1  christos 			wpa_printf(MSG_DEBUG,
    961  1.1  christos 				   "Invalid reporting detail subelement length: %u",
    962  1.1  christos 				   slen);
    963  1.1  christos 			return -1;
    964  1.1  christos 		}
    965  1.1  christos 
    966  1.1  christos 		data->report_detail = subelem[0];
    967  1.1  christos 		if (data->report_detail >
    968  1.1  christos 		    BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
    969  1.1  christos 			wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
    970  1.1  christos 				   subelem[0]);
    971  1.1  christos 			return -1;
    972  1.1  christos 		}
    973  1.1  christos 
    974  1.1  christos 		break;
    975  1.1  christos 	case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
    976  1.1  christos 		if (data->report_detail !=
    977  1.1  christos 		    BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
    978  1.1  christos 			wpa_printf(MSG_DEBUG,
    979  1.1  christos 				   "Beacon request: request subelement is present but report detail is %u",
    980  1.1  christos 				   data->report_detail);
    981  1.1  christos 			return -1;
    982  1.1  christos 		}
    983  1.1  christos 
    984  1.1  christos 		if (!slen) {
    985  1.1  christos 			wpa_printf(MSG_DEBUG,
    986  1.1  christos 				   "Invalid request subelement length: %u",
    987  1.1  christos 				   slen);
    988  1.1  christos 			return -1;
    989  1.1  christos 		}
    990  1.1  christos 
    991  1.1  christos 		if (data->eids) {
    992  1.1  christos 			wpa_printf(MSG_DEBUG,
    993  1.1  christos 				   "Beacon Request: Request subelement appears more than once");
    994  1.1  christos 			return -1;
    995  1.1  christos 		}
    996  1.1  christos 
    997  1.1  christos 		data->eids = bitfield_alloc(255);
    998  1.1  christos 		if (!data->eids) {
    999  1.1  christos 			wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
   1000  1.1  christos 			return -1;
   1001  1.1  christos 		}
   1002  1.1  christos 
   1003  1.1  christos 		for (i = 0; i < slen; i++)
   1004  1.1  christos 			bitfield_set(data->eids, subelem[i]);
   1005  1.1  christos 		break;
   1006  1.1  christos 	case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
   1007  1.1  christos 		/* Skip - it will be processed when freqs are added */
   1008  1.1  christos 		break;
   1009  1.1  christos 	default:
   1010  1.1  christos 		wpa_printf(MSG_DEBUG,
   1011  1.1  christos 			   "Beacon request: Unknown subelement id %u", sid);
   1012  1.1  christos 		break;
   1013  1.1  christos 	}
   1014  1.1  christos 
   1015  1.1  christos 	return 1;
   1016  1.1  christos }
   1017  1.1  christos 
   1018  1.1  christos 
   1019  1.1  christos /**
   1020  1.1  christos  * Returns 0 if the next element can be processed, 1 if some operation was
   1021  1.1  christos  * triggered, and -1 if processing failed (i.e., the element is in invalid
   1022  1.1  christos  * format or an internal error occurred).
   1023  1.1  christos  */
   1024  1.1  christos static int
   1025  1.1  christos wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
   1026  1.1  christos 			  u8 elem_token, int duration_mandatory,
   1027  1.1  christos 			  const struct rrm_measurement_beacon_request *req,
   1028  1.1  christos 			  size_t len, struct wpabuf **buf)
   1029  1.1  christos {
   1030  1.1  christos 	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
   1031  1.1  christos 	struct wpa_driver_scan_params *params = &data->scan_params;
   1032  1.1  christos 	const u8 *subelems;
   1033  1.1  christos 	size_t elems_len;
   1034  1.1  christos 	u16 rand_interval;
   1035  1.1  christos 	u32 interval_usec;
   1036  1.1  christos 	u32 _rand;
   1037  1.1  christos 	int ret = 0, res;
   1038  1.1  christos 	u8 reject_mode;
   1039  1.1  christos 
   1040  1.1  christos 	if (len < sizeof(*req))
   1041  1.1  christos 		return -1;
   1042  1.1  christos 
   1043  1.1  christos 	if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
   1044  1.1  christos 	    req->mode != BEACON_REPORT_MODE_ACTIVE &&
   1045  1.1  christos 	    req->mode != BEACON_REPORT_MODE_TABLE)
   1046  1.1  christos 		return 0;
   1047  1.1  christos 
   1048  1.1  christos 	subelems = req->variable;
   1049  1.1  christos 	elems_len = len - sizeof(*req);
   1050  1.1  christos 	rand_interval = le_to_host16(req->rand_interval);
   1051  1.1  christos 
   1052  1.1  christos 	os_free(params->freqs);
   1053  1.1  christos 	os_memset(params, 0, sizeof(*params));
   1054  1.1  christos 
   1055  1.1  christos 	data->token = elem_token;
   1056  1.1  christos 
   1057  1.1  christos 	/* default reporting detail is all fixed length fields and all
   1058  1.1  christos 	 * elements */
   1059  1.1  christos 	data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
   1060  1.1  christos 	os_memcpy(data->bssid, req->bssid, ETH_ALEN);
   1061  1.1  christos 
   1062  1.1  christos 	while (elems_len >= 2) {
   1063  1.1  christos 		if (subelems[1] > elems_len - 2) {
   1064  1.1  christos 			wpa_printf(MSG_DEBUG,
   1065  1.1  christos 				   "Beacon Request: Truncated subelement");
   1066  1.1  christos 			ret = -1;
   1067  1.1  christos 			goto out;
   1068  1.1  christos 		}
   1069  1.1  christos 
   1070  1.1  christos 		res = wpas_rm_handle_beacon_req_subelem(
   1071  1.1  christos 			wpa_s, data, subelems[0], subelems[1], &subelems[2]);
   1072  1.1  christos 		if (res < 0) {
   1073  1.1  christos 			ret = res;
   1074  1.1  christos 			goto out;
   1075  1.1  christos 		} else if (!res) {
   1076  1.1  christos 			reject_mode = MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE;
   1077  1.1  christos 			goto out_reject;
   1078  1.1  christos 		}
   1079  1.1  christos 
   1080  1.1  christos 		elems_len -= 2 + subelems[1];
   1081  1.1  christos 		subelems += 2 + subelems[1];
   1082  1.1  christos 	}
   1083  1.1  christos 
   1084  1.1  christos 	if (req->mode == BEACON_REPORT_MODE_TABLE) {
   1085  1.1  christos 		wpas_beacon_rep_table(wpa_s, buf);
   1086  1.1  christos 		goto out;
   1087  1.1  christos 	}
   1088  1.1  christos 
   1089  1.1  christos 	params->freqs = wpas_beacon_request_freqs(
   1090  1.1  christos 		wpa_s, req->oper_class, req->channel,
   1091  1.1  christos 		req->mode == BEACON_REPORT_MODE_ACTIVE,
   1092  1.1  christos 		req->variable, len - sizeof(*req));
   1093  1.1  christos 	if (!params->freqs) {
   1094  1.1  christos 		wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
   1095  1.1  christos 		reject_mode = MEASUREMENT_REPORT_MODE_REJECT_REFUSED;
   1096  1.1  christos 		goto out_reject;
   1097  1.1  christos 	}
   1098  1.1  christos 
   1099  1.1  christos 	params->duration = le_to_host16(req->duration);
   1100  1.1  christos 	params->duration_mandatory = duration_mandatory;
   1101  1.1  christos 	if (!params->duration) {
   1102  1.1  christos 		wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
   1103  1.1  christos 		ret = -1;
   1104  1.1  christos 		goto out;
   1105  1.1  christos 	}
   1106  1.1  christos 
   1107  1.1  christos 	params->only_new_results = 1;
   1108  1.1  christos 
   1109  1.1  christos 	if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
   1110  1.1  christos 		params->ssids[params->num_ssids].ssid = data->ssid;
   1111  1.1  christos 		params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
   1112  1.1  christos 	}
   1113  1.1  christos 
   1114  1.1  christos 	if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
   1115  1.1  christos 		_rand = os_random();
   1116  1.1  christos 	interval_usec = (_rand % (rand_interval + 1)) * 1024;
   1117  1.1  christos 	eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
   1118  1.1  christos 			       NULL);
   1119  1.1  christos 	return 1;
   1120  1.1  christos out_reject:
   1121  1.1  christos 	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
   1122  1.1  christos 	    wpas_rrm_report_elem(buf, elem_token, reject_mode,
   1123  1.1  christos 				 MEASURE_TYPE_BEACON, NULL, 0) < 0) {
   1124  1.1  christos 		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
   1125  1.1  christos 		ret = -1;
   1126  1.1  christos 	}
   1127  1.1  christos out:
   1128  1.1  christos 	wpas_clear_beacon_rep_data(wpa_s);
   1129  1.1  christos 	return ret;
   1130  1.1  christos }
   1131  1.1  christos 
   1132  1.1  christos 
   1133  1.1  christos static int
   1134  1.1  christos wpas_rrm_handle_msr_req_element(
   1135  1.1  christos 	struct wpa_supplicant *wpa_s,
   1136  1.1  christos 	const struct rrm_measurement_request_element *req,
   1137  1.1  christos 	struct wpabuf **buf)
   1138  1.1  christos {
   1139  1.1  christos 	int duration_mandatory;
   1140  1.1  christos 
   1141  1.1  christos 	wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
   1142  1.1  christos 		   req->type, req->token);
   1143  1.1  christos 
   1144  1.1  christos 	if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
   1145  1.1  christos 		/* Enable bit is not supported for now */
   1146  1.1  christos 		wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
   1147  1.1  christos 		return 0;
   1148  1.1  christos 	}
   1149  1.1  christos 
   1150  1.1  christos 	if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
   1151  1.1  christos 	    req->type > MEASURE_TYPE_RPI_HIST) {
   1152  1.1  christos 		/* Parallel measurements are not supported for now */
   1153  1.1  christos 		wpa_printf(MSG_DEBUG,
   1154  1.1  christos 			   "RRM: Parallel measurements are not supported, reject");
   1155  1.1  christos 		goto reject;
   1156  1.1  christos 	}
   1157  1.1  christos 
   1158  1.1  christos 	duration_mandatory =
   1159  1.1  christos 		!!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
   1160  1.1  christos 
   1161  1.1  christos 	switch (req->type) {
   1162  1.1  christos 	case MEASURE_TYPE_LCI:
   1163  1.1  christos 		return wpas_rrm_build_lci_report(wpa_s, req, buf);
   1164  1.1  christos 	case MEASURE_TYPE_BEACON:
   1165  1.1  christos 		if (duration_mandatory &&
   1166  1.1  christos 		    !(wpa_s->drv_rrm_flags &
   1167  1.1  christos 		      WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
   1168  1.1  christos 			wpa_printf(MSG_DEBUG,
   1169  1.1  christos 				   "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
   1170  1.1  christos 			goto reject;
   1171  1.1  christos 		}
   1172  1.1  christos 		return wpas_rm_handle_beacon_req(wpa_s, req->token,
   1173  1.1  christos 						 duration_mandatory,
   1174  1.1  christos 						 (const void *) req->variable,
   1175  1.1  christos 						 req->len - 3, buf);
   1176  1.1  christos 	default:
   1177  1.1  christos 		wpa_printf(MSG_INFO,
   1178  1.1  christos 			   "RRM: Unsupported radio measurement type %u",
   1179  1.1  christos 			   req->type);
   1180  1.1  christos 		break;
   1181  1.1  christos 	}
   1182  1.1  christos 
   1183  1.1  christos reject:
   1184  1.1  christos 	if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
   1185  1.1  christos 	    wpas_rrm_report_elem(buf, req->token,
   1186  1.1  christos 				 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
   1187  1.1  christos 				 req->type, NULL, 0) < 0) {
   1188  1.1  christos 		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
   1189  1.1  christos 		return -1;
   1190  1.1  christos 	}
   1191  1.1  christos 
   1192  1.1  christos 	return 0;
   1193  1.1  christos }
   1194  1.1  christos 
   1195  1.1  christos 
   1196  1.1  christos static struct wpabuf *
   1197  1.1  christos wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
   1198  1.1  christos 			       size_t len)
   1199  1.1  christos {
   1200  1.1  christos 	struct wpabuf *buf = NULL;
   1201  1.1  christos 
   1202  1.1  christos 	while (len) {
   1203  1.1  christos 		const struct rrm_measurement_request_element *req;
   1204  1.1  christos 		int res;
   1205  1.1  christos 
   1206  1.1  christos 		if (len < 2) {
   1207  1.1  christos 			wpa_printf(MSG_DEBUG, "RRM: Truncated element");
   1208  1.1  christos 			goto out;
   1209  1.1  christos 		}
   1210  1.1  christos 
   1211  1.1  christos 		req = (const struct rrm_measurement_request_element *) pos;
   1212  1.1  christos 		if (req->eid != WLAN_EID_MEASURE_REQUEST) {
   1213  1.1  christos 			wpa_printf(MSG_DEBUG,
   1214  1.1  christos 				   "RRM: Expected Measurement Request element, but EID is %u",
   1215  1.1  christos 				   req->eid);
   1216  1.1  christos 			goto out;
   1217  1.1  christos 		}
   1218  1.1  christos 
   1219  1.1  christos 		if (req->len < 3) {
   1220  1.1  christos 			wpa_printf(MSG_DEBUG, "RRM: Element length too short");
   1221  1.1  christos 			goto out;
   1222  1.1  christos 		}
   1223  1.1  christos 
   1224  1.1  christos 		if (req->len > len - 2) {
   1225  1.1  christos 			wpa_printf(MSG_DEBUG, "RRM: Element length too long");
   1226  1.1  christos 			goto out;
   1227  1.1  christos 		}
   1228  1.1  christos 
   1229  1.1  christos 		res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
   1230  1.1  christos 		if (res < 0)
   1231  1.1  christos 			goto out;
   1232  1.1  christos 
   1233  1.1  christos 		pos += req->len + 2;
   1234  1.1  christos 		len -= req->len + 2;
   1235  1.1  christos 	}
   1236  1.1  christos 
   1237  1.1  christos 	return buf;
   1238  1.1  christos 
   1239  1.1  christos out:
   1240  1.1  christos 	wpabuf_free(buf);
   1241  1.1  christos 	return NULL;
   1242  1.1  christos }
   1243  1.1  christos 
   1244  1.1  christos 
   1245  1.1  christos void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
   1246  1.1  christos 					       const u8 *src, const u8 *dst,
   1247  1.1  christos 					       const u8 *frame, size_t len)
   1248  1.1  christos {
   1249  1.1  christos 	struct wpabuf *report;
   1250  1.1  christos 
   1251  1.1  christos 	if (wpa_s->wpa_state != WPA_COMPLETED) {
   1252  1.1  christos 		wpa_printf(MSG_INFO,
   1253  1.1  christos 			   "RRM: Ignoring radio measurement request: Not associated");
   1254  1.1  christos 		return;
   1255  1.1  christos 	}
   1256  1.1  christos 
   1257  1.1  christos 	if (!wpa_s->rrm.rrm_used) {
   1258  1.1  christos 		wpa_printf(MSG_INFO,
   1259  1.1  christos 			   "RRM: Ignoring radio measurement request: Not RRM network");
   1260  1.1  christos 		return;
   1261  1.1  christos 	}
   1262  1.1  christos 
   1263  1.1  christos 	if (len < 3) {
   1264  1.1  christos 		wpa_printf(MSG_INFO,
   1265  1.1  christos 			   "RRM: Ignoring too short radio measurement request");
   1266  1.1  christos 		return;
   1267  1.1  christos 	}
   1268  1.1  christos 
   1269  1.1  christos 	wpa_s->rrm.token = *frame;
   1270  1.1  christos 	os_memcpy(wpa_s->rrm.dst_addr, dst, ETH_ALEN);
   1271  1.1  christos 
   1272  1.1  christos 	/* Number of repetitions is not supported */
   1273  1.1  christos 
   1274  1.1  christos 	report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
   1275  1.1  christos 	if (!report)
   1276  1.1  christos 		return;
   1277  1.1  christos 
   1278  1.1  christos 	wpas_rrm_send_msr_report(wpa_s, report);
   1279  1.1  christos 	wpabuf_free(report);
   1280  1.1  christos }
   1281  1.1  christos 
   1282  1.1  christos 
   1283  1.1  christos void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
   1284  1.1  christos 					      const u8 *src,
   1285  1.1  christos 					      const u8 *frame, size_t len,
   1286  1.1  christos 					      int rssi)
   1287  1.1  christos {
   1288  1.1  christos 	struct wpabuf *buf;
   1289  1.1  christos 	const struct rrm_link_measurement_request *req;
   1290  1.1  christos 	struct rrm_link_measurement_report report;
   1291  1.1  christos 
   1292  1.1  christos 	if (wpa_s->wpa_state != WPA_COMPLETED) {
   1293  1.1  christos 		wpa_printf(MSG_INFO,
   1294  1.1  christos 			   "RRM: Ignoring link measurement request. Not associated");
   1295  1.1  christos 		return;
   1296  1.1  christos 	}
   1297  1.1  christos 
   1298  1.1  christos 	if (!wpa_s->rrm.rrm_used) {
   1299  1.1  christos 		wpa_printf(MSG_INFO,
   1300  1.1  christos 			   "RRM: Ignoring link measurement request. Not RRM network");
   1301  1.1  christos 		return;
   1302  1.1  christos 	}
   1303  1.1  christos 
   1304  1.1  christos 	if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
   1305  1.1  christos 		wpa_printf(MSG_INFO,
   1306  1.1  christos 			   "RRM: Measurement report failed. TX power insertion not supported");
   1307  1.1  christos 		return;
   1308  1.1  christos 	}
   1309  1.1  christos 
   1310  1.1  christos 	req = (const struct rrm_link_measurement_request *) frame;
   1311  1.1  christos 	if (len < sizeof(*req)) {
   1312  1.1  christos 		wpa_printf(MSG_INFO,
   1313  1.1  christos 			   "RRM: Link measurement report failed. Request too short");
   1314  1.1  christos 		return;
   1315  1.1  christos 	}
   1316  1.1  christos 
   1317  1.1  christos 	os_memset(&report, 0, sizeof(report));
   1318  1.1  christos 	report.dialog_token = req->dialog_token;
   1319  1.1  christos 	report.tpc.eid = WLAN_EID_TPC_REPORT;
   1320  1.1  christos 	report.tpc.len = 2;
   1321  1.1  christos 	/* Note: The driver is expected to update report.tpc.tx_power and
   1322  1.1  christos 	 * report.tpc.link_margin subfields when sending out this frame.
   1323  1.1  christos 	 * Similarly, the driver would need to update report.rx_ant_id and
   1324  1.1  christos 	 * report.tx_ant_id subfields. */
   1325  1.1  christos 	report.rsni = 255; /* 255 indicates that RSNI is not available */
   1326  1.1  christos 	report.rcpi = rssi_to_rcpi(rssi);
   1327  1.1  christos 
   1328  1.1  christos 	/* action_category + action_code */
   1329  1.1  christos 	buf = wpabuf_alloc(2 + sizeof(report));
   1330  1.1  christos 	if (buf == NULL) {
   1331  1.1  christos 		wpa_printf(MSG_ERROR,
   1332  1.1  christos 			   "RRM: Link measurement report failed. Buffer allocation failed");
   1333  1.1  christos 		return;
   1334  1.1  christos 	}
   1335  1.1  christos 
   1336  1.1  christos 	wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
   1337  1.1  christos 	wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
   1338  1.1  christos 	wpabuf_put_data(buf, &report, sizeof(report));
   1339  1.1  christos 	wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
   1340  1.1  christos 
   1341  1.1  christos 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
   1342  1.1  christos 				wpa_s->own_addr, wpa_s->bssid,
   1343  1.1  christos 				wpabuf_head(buf), wpabuf_len(buf), 0)) {
   1344  1.1  christos 		wpa_printf(MSG_ERROR,
   1345  1.1  christos 			   "RRM: Link measurement report failed. Send action failed");
   1346  1.1  christos 	}
   1347  1.1  christos 	wpabuf_free(buf);
   1348  1.1  christos }
   1349  1.1  christos 
   1350  1.1  christos 
   1351  1.1  christos int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
   1352  1.1  christos 				 struct wpa_scan_results *scan_res,
   1353  1.1  christos 				 struct scan_info *info)
   1354  1.1  christos {
   1355  1.1  christos 	size_t i = 0;
   1356  1.1  christos 	struct wpabuf *buf = NULL;
   1357  1.1  christos 
   1358  1.1  christos 	if (!wpa_s->beacon_rep_data.token)
   1359  1.1  christos 		return 0;
   1360  1.1  christos 
   1361  1.1  christos 	if (!wpa_s->current_bss)
   1362  1.1  christos 		goto out;
   1363  1.1  christos 
   1364  1.1  christos 	/* If the measurement was aborted, don't report partial results */
   1365  1.1  christos 	if (info->aborted)
   1366  1.1  christos 		goto out;
   1367  1.1  christos 
   1368  1.1  christos 	wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
   1369  1.1  christos 		   MAC2STR(info->scan_start_tsf_bssid),
   1370  1.1  christos 		   MAC2STR(wpa_s->current_bss->bssid));
   1371  1.1  christos 	if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
   1372  1.1  christos 	    os_memcmp(info->scan_start_tsf_bssid, wpa_s->current_bss->bssid,
   1373  1.1  christos 		      ETH_ALEN) != 0) {
   1374  1.1  christos 		wpa_printf(MSG_DEBUG,
   1375  1.1  christos 			   "RRM: Ignore scan results due to mismatching TSF BSSID");
   1376  1.1  christos 		goto out;
   1377  1.1  christos 	}
   1378  1.1  christos 
   1379  1.1  christos 	for (i = 0; i < scan_res->num; i++) {
   1380  1.1  christos 		struct wpa_bss *bss =
   1381  1.1  christos 			wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
   1382  1.1  christos 
   1383  1.1  christos 		if (!bss)
   1384  1.1  christos 			continue;
   1385  1.1  christos 
   1386  1.1  christos 		if ((wpa_s->drv_rrm_flags &
   1387  1.1  christos 		     WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
   1388  1.1  christos 		    os_memcmp(scan_res->res[i]->tsf_bssid,
   1389  1.1  christos 			      wpa_s->current_bss->bssid, ETH_ALEN) != 0) {
   1390  1.1  christos 			wpa_printf(MSG_DEBUG,
   1391  1.1  christos 				   "RRM: Ignore scan result for " MACSTR
   1392  1.1  christos 				   " due to mismatching TSF BSSID" MACSTR,
   1393  1.1  christos 				   MAC2STR(scan_res->res[i]->bssid),
   1394  1.1  christos 				   MAC2STR(scan_res->res[i]->tsf_bssid));
   1395  1.1  christos 			continue;
   1396  1.1  christos 		}
   1397  1.1  christos 
   1398  1.1  christos 		/*
   1399  1.1  christos 		 * Don't report results that were not received during the
   1400  1.1  christos 		 * current measurement.
   1401  1.1  christos 		 */
   1402  1.1  christos 		if (!(wpa_s->drv_rrm_flags &
   1403  1.1  christos 		      WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
   1404  1.1  christos 			struct os_reltime update_time, diff;
   1405  1.1  christos 
   1406  1.1  christos 			/* For now, allow 8 ms older results due to some
   1407  1.1  christos 			 * unknown issue with cfg80211 BSS table updates during
   1408  1.1  christos 			 * a scan with the current BSS.
   1409  1.1  christos 			 * TODO: Fix this more properly to avoid having to have
   1410  1.1  christos 			 * this type of hacks in place. */
   1411  1.1  christos 			calculate_update_time(&scan_res->fetch_time,
   1412  1.1  christos 					      scan_res->res[i]->age,
   1413  1.1  christos 					      &update_time);
   1414  1.1  christos 			os_reltime_sub(&wpa_s->beacon_rep_scan,
   1415  1.1  christos 				       &update_time, &diff);
   1416  1.1  christos 			if (os_reltime_before(&update_time,
   1417  1.1  christos 					      &wpa_s->beacon_rep_scan) &&
   1418  1.1  christos 			    (diff.sec || diff.usec >= 8000)) {
   1419  1.1  christos 				wpa_printf(MSG_DEBUG,
   1420  1.1  christos 					   "RRM: Ignore scan result for " MACSTR
   1421  1.1  christos 					   " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
   1422  1.1  christos 					   MAC2STR(scan_res->res[i]->bssid),
   1423  1.1  christos 					   scan_res->res[i]->age,
   1424  1.1  christos 					   (unsigned int) diff.sec,
   1425  1.1  christos 					   (unsigned int) diff.usec);
   1426  1.1  christos 				continue;
   1427  1.1  christos 			}
   1428  1.1  christos 		} else if (info->scan_start_tsf >
   1429  1.1  christos 			   scan_res->res[i]->parent_tsf) {
   1430  1.1  christos 			continue;
   1431  1.1  christos 		}
   1432  1.1  christos 
   1433  1.1  christos 		if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
   1434  1.1  christos 					scan_res->res[i]->parent_tsf) < 0)
   1435  1.1  christos 			break;
   1436  1.1  christos 	}
   1437  1.1  christos 
   1438  1.1  christos 	if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
   1439  1.1  christos 		goto out;
   1440  1.1  christos 
   1441  1.1  christos 	wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
   1442  1.1  christos 
   1443  1.1  christos 	wpas_rrm_send_msr_report(wpa_s, buf);
   1444  1.1  christos 	wpabuf_free(buf);
   1445  1.1  christos 
   1446  1.1  christos out:
   1447  1.1  christos 	wpas_clear_beacon_rep_data(wpa_s);
   1448  1.1  christos 	return 1;
   1449  1.1  christos }
   1450  1.1  christos 
   1451  1.1  christos 
   1452  1.1  christos void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
   1453  1.1  christos {
   1454  1.1  christos 	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
   1455  1.1  christos 
   1456  1.1  christos 	eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
   1457  1.1  christos 	bitfield_free(data->eids);
   1458  1.1  christos 	os_free(data->scan_params.freqs);
   1459  1.1  christos 	os_memset(data, 0, sizeof(*data));
   1460  1.1  christos }
   1461