blob: 7ce854be0612d297311eb1cdb4190ed71a4b47c4 [file] [log] [blame]
Dmitry Shmidt29333592017-01-09 12:27:11 -08001/*
2 * wpa_supplicant - Radio Measurements
3 * Copyright (c) 2003-2016, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_common.h"
14#include "wpa_supplicant_i.h"
15#include "driver_i.h"
16#include "bss.h"
17#include "scan.h"
18#include "p2p_supplicant.h"
19
20
21static void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
22{
23 struct rrm_data *rrm = data;
24
25 if (!rrm->notify_neighbor_rep) {
26 wpa_printf(MSG_ERROR,
27 "RRM: Unexpected neighbor report timeout");
28 return;
29 }
30
31 wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
32 rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
33
34 rrm->notify_neighbor_rep = NULL;
35 rrm->neighbor_rep_cb_ctx = NULL;
36}
37
38
39/*
40 * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
41 * @wpa_s: Pointer to wpa_supplicant
42 */
43void wpas_rrm_reset(struct wpa_supplicant *wpa_s)
44{
45 wpa_s->rrm.rrm_used = 0;
46
47 eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
48 NULL);
49 if (wpa_s->rrm.notify_neighbor_rep)
50 wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
51 wpa_s->rrm.next_neighbor_rep_token = 1;
52 wpas_clear_beacon_rep_data(wpa_s);
53}
54
55
56/*
57 * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
58 * @wpa_s: Pointer to wpa_supplicant
59 * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
60 * @report_len: Length of neighbor report buffer
61 */
62void wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
63 const u8 *report, size_t report_len)
64{
65 struct wpabuf *neighbor_rep;
66
67 wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
68 if (report_len < 1)
69 return;
70
71 if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
72 wpa_printf(MSG_DEBUG,
73 "RRM: Discarding neighbor report with token %d (expected %d)",
74 report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
75 return;
76 }
77
78 eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
79 NULL);
80
81 if (!wpa_s->rrm.notify_neighbor_rep) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080082 wpa_msg(wpa_s, MSG_INFO, "RRM: Unexpected neighbor report");
Dmitry Shmidt29333592017-01-09 12:27:11 -080083 return;
84 }
85
86 /* skipping the first byte, which is only an id (dialog token) */
87 neighbor_rep = wpabuf_alloc(report_len - 1);
88 if (!neighbor_rep) {
89 wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
90 return;
91 }
92 wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080093 wpa_dbg(wpa_s, MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
94 report[0]);
Dmitry Shmidt29333592017-01-09 12:27:11 -080095 wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
96 neighbor_rep);
97 wpa_s->rrm.notify_neighbor_rep = NULL;
98 wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
99}
100
101
102#if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
103/* Workaround different, undefined for Windows, error codes used here */
Hai Shalomc3565922019-10-28 11:58:20 -0700104#ifndef ENOTCONN
Dmitry Shmidt29333592017-01-09 12:27:11 -0800105#define ENOTCONN -1
Hai Shalomc3565922019-10-28 11:58:20 -0700106#endif
107#ifndef EOPNOTSUPP
Dmitry Shmidt29333592017-01-09 12:27:11 -0800108#define EOPNOTSUPP -1
Hai Shalomc3565922019-10-28 11:58:20 -0700109#endif
110#ifndef ECANCELED
Dmitry Shmidt29333592017-01-09 12:27:11 -0800111#define ECANCELED -1
112#endif
Hai Shalomc3565922019-10-28 11:58:20 -0700113#endif
Dmitry Shmidt29333592017-01-09 12:27:11 -0800114
115/* Measurement Request element + Location Subject + Maximum Age subelement */
116#define MEASURE_REQUEST_LCI_LEN (3 + 1 + 4)
117/* Measurement Request element + Location Civic Request */
118#define MEASURE_REQUEST_CIVIC_LEN (3 + 5)
119
120
121/**
122 * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
123 * @wpa_s: Pointer to wpa_supplicant
124 * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
125 * is sent in the request.
126 * @lci: if set, neighbor request will include LCI request
127 * @civic: if set, neighbor request will include civic location request
128 * @cb: Callback function to be called once the requested report arrives, or
129 * timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
130 * In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
131 * the requester's responsibility to free it.
132 * In the latter case NULL will be sent in 'neighbor_rep'.
133 * @cb_ctx: Context value to send the callback function
134 * Returns: 0 in case of success, negative error code otherwise
135 *
136 * In case there is a previous request which has not been answered yet, the
137 * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
138 * Request must contain a callback function.
139 */
140int wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
141 const struct wpa_ssid_value *ssid,
142 int lci, int civic,
143 void (*cb)(void *ctx,
144 struct wpabuf *neighbor_rep),
145 void *cb_ctx)
146{
147 struct wpabuf *buf;
148 const u8 *rrm_ie;
149
150 if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800151 wpa_dbg(wpa_s, MSG_DEBUG, "RRM: No connection, no RRM.");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800152 return -ENOTCONN;
153 }
154
155 if (!wpa_s->rrm.rrm_used) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800156 wpa_dbg(wpa_s, MSG_DEBUG, "RRM: No RRM in current connection.");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800157 return -EOPNOTSUPP;
158 }
159
160 rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
161 WLAN_EID_RRM_ENABLED_CAPABILITIES);
162 if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
163 !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800164 wpa_dbg(wpa_s, MSG_DEBUG,
165 "RRM: No network support for Neighbor Report.");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800166 return -EOPNOTSUPP;
167 }
168
Dmitry Shmidt29333592017-01-09 12:27:11 -0800169 /* Refuse if there's a live request */
170 if (wpa_s->rrm.notify_neighbor_rep) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800171 wpa_dbg(wpa_s, MSG_DEBUG,
172 "RRM: Currently handling previous Neighbor Report.");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800173 return -EBUSY;
174 }
175
176 /* 3 = action category + action code + dialog token */
177 buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0) +
178 (lci ? 2 + MEASURE_REQUEST_LCI_LEN : 0) +
179 (civic ? 2 + MEASURE_REQUEST_CIVIC_LEN : 0));
180 if (buf == NULL) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800181 wpa_dbg(wpa_s, MSG_DEBUG,
182 "RRM: Failed to allocate Neighbor Report Request");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800183 return -ENOMEM;
184 }
185
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800186 wpa_dbg(wpa_s, MSG_DEBUG,
187 "RRM: Neighbor report request (for %s), token=%d",
188 (ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
189 wpa_s->rrm.next_neighbor_rep_token);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800190
191 wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
192 wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
193 wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
194 if (ssid) {
195 wpabuf_put_u8(buf, WLAN_EID_SSID);
196 wpabuf_put_u8(buf, ssid->ssid_len);
197 wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
198 }
199
200 if (lci) {
201 /* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
202 wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
203 wpabuf_put_u8(buf, MEASURE_REQUEST_LCI_LEN);
204
205 /*
206 * Measurement token; nonzero number that is unique among the
207 * Measurement Request elements in a particular frame.
208 */
209 wpabuf_put_u8(buf, 1); /* Measurement Token */
210
211 /*
212 * Parallel, Enable, Request, and Report bits are 0, Duration is
213 * reserved.
214 */
215 wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
216 wpabuf_put_u8(buf, MEASURE_TYPE_LCI); /* Measurement Type */
217
218 /* IEEE P802.11-REVmc/D5.0 9.4.2.21.10 - LCI request */
219 /* Location Subject */
220 wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
221
222 /* Optional Subelements */
223 /*
224 * IEEE P802.11-REVmc/D5.0 Figure 9-170
225 * The Maximum Age subelement is required, otherwise the AP can
226 * send only data that was determined after receiving the
227 * request. Setting it here to unlimited age.
228 */
229 wpabuf_put_u8(buf, LCI_REQ_SUBELEM_MAX_AGE);
230 wpabuf_put_u8(buf, 2);
231 wpabuf_put_le16(buf, 0xffff);
232 }
233
234 if (civic) {
235 /* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
236 wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
237 wpabuf_put_u8(buf, MEASURE_REQUEST_CIVIC_LEN);
238
239 /*
240 * Measurement token; nonzero number that is unique among the
241 * Measurement Request elements in a particular frame.
242 */
243 wpabuf_put_u8(buf, 2); /* Measurement Token */
244
245 /*
246 * Parallel, Enable, Request, and Report bits are 0, Duration is
247 * reserved.
248 */
249 wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
250 /* Measurement Type */
251 wpabuf_put_u8(buf, MEASURE_TYPE_LOCATION_CIVIC);
252
253 /* IEEE P802.11-REVmc/D5.0 9.4.2.21.14:
254 * Location Civic request */
255 /* Location Subject */
256 wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
257 wpabuf_put_u8(buf, 0); /* Civic Location Type: IETF RFC 4776 */
258 /* Location Service Interval Units: Seconds */
259 wpabuf_put_u8(buf, 0);
260 /* Location Service Interval: 0 - Only one report is requested
261 */
262 wpabuf_put_le16(buf, 0);
263 /* No optional subelements */
264 }
265
266 wpa_s->rrm.next_neighbor_rep_token++;
267
268 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
269 wpa_s->own_addr, wpa_s->bssid,
270 wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800271 wpa_dbg(wpa_s, MSG_DEBUG,
272 "RRM: Failed to send Neighbor Report Request");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800273 wpabuf_free(buf);
274 return -ECANCELED;
275 }
276
277 wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
278 wpa_s->rrm.notify_neighbor_rep = cb;
279 eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
280 wpas_rrm_neighbor_rep_timeout_handler,
281 &wpa_s->rrm, NULL);
282
283 wpabuf_free(buf);
284 return 0;
285}
286
287
Paul Stewart092955c2017-02-06 09:13:09 -0800288static int wpas_rrm_report_elem(struct wpabuf **buf, u8 token, u8 mode, u8 type,
Dmitry Shmidt29333592017-01-09 12:27:11 -0800289 const u8 *data, size_t data_len)
290{
Paul Stewart092955c2017-02-06 09:13:09 -0800291 if (wpabuf_resize(buf, 5 + data_len))
Dmitry Shmidt29333592017-01-09 12:27:11 -0800292 return -1;
293
Paul Stewart092955c2017-02-06 09:13:09 -0800294 wpabuf_put_u8(*buf, WLAN_EID_MEASURE_REPORT);
295 wpabuf_put_u8(*buf, 3 + data_len);
296 wpabuf_put_u8(*buf, token);
297 wpabuf_put_u8(*buf, mode);
298 wpabuf_put_u8(*buf, type);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800299
300 if (data_len)
Paul Stewart092955c2017-02-06 09:13:09 -0800301 wpabuf_put_data(*buf, data, data_len);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800302
303 return 0;
304}
305
306
307static int
308wpas_rrm_build_lci_report(struct wpa_supplicant *wpa_s,
309 const struct rrm_measurement_request_element *req,
310 struct wpabuf **buf)
311{
312 u8 subject;
313 u16 max_age = 0;
314 struct os_reltime t, diff;
315 unsigned long diff_l;
316 const u8 *subelem;
317 const u8 *request = req->variable;
318 size_t len = req->len - 3;
319
320 if (len < 1)
321 return -1;
322
323 if (!wpa_s->lci)
324 goto reject;
325
326 subject = *request++;
327 len--;
328
329 wpa_printf(MSG_DEBUG, "Measurement request location subject=%u",
330 subject);
331
332 if (subject != LOCATION_SUBJECT_REMOTE) {
333 wpa_printf(MSG_INFO,
334 "Not building LCI report - bad location subject");
335 return 0;
336 }
337
338 /* Subelements are formatted exactly like elements */
339 wpa_hexdump(MSG_DEBUG, "LCI request subelements", request, len);
340 subelem = get_ie(request, len, LCI_REQ_SUBELEM_MAX_AGE);
341 if (subelem && subelem[1] == 2)
342 max_age = WPA_GET_LE16(subelem + 2);
343
344 if (os_get_reltime(&t))
345 goto reject;
346
347 os_reltime_sub(&t, &wpa_s->lci_time, &diff);
348 /* LCI age is calculated in 10th of a second units. */
349 diff_l = diff.sec * 10 + diff.usec / 100000;
350
351 if (max_age != 0xffff && max_age < diff_l)
352 goto reject;
353
Paul Stewart092955c2017-02-06 09:13:09 -0800354 if (wpas_rrm_report_elem(buf, req->token,
Dmitry Shmidt29333592017-01-09 12:27:11 -0800355 MEASUREMENT_REPORT_MODE_ACCEPT, req->type,
356 wpabuf_head_u8(wpa_s->lci),
357 wpabuf_len(wpa_s->lci)) < 0) {
358 wpa_printf(MSG_DEBUG, "Failed to add LCI report element");
359 return -1;
360 }
361
362 return 0;
363
364reject:
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700365 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
366 wpas_rrm_report_elem(buf, req->token,
Dmitry Shmidt29333592017-01-09 12:27:11 -0800367 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
368 req->type, NULL, 0) < 0) {
369 wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
370 return -1;
371 }
372
373 return 0;
374}
375
376
377static void wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant *wpa_s,
378 const u8 *data, size_t len)
379{
380 struct wpabuf *report = wpabuf_alloc(len + 3);
381
382 if (!report)
383 return;
384
385 wpabuf_put_u8(report, WLAN_ACTION_RADIO_MEASUREMENT);
386 wpabuf_put_u8(report, WLAN_RRM_RADIO_MEASUREMENT_REPORT);
387 wpabuf_put_u8(report, wpa_s->rrm.token);
388
389 wpabuf_put_data(report, data, len);
390
391 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
392 wpa_s->own_addr, wpa_s->bssid,
393 wpabuf_head(report), wpabuf_len(report), 0)) {
394 wpa_printf(MSG_ERROR,
395 "RRM: Radio measurement report failed: Sending Action frame failed");
396 }
397
398 wpabuf_free(report);
399}
400
401
Hai Shalom74f70d42019-02-11 14:42:39 -0800402static int wpas_rrm_beacon_rep_update_last_frame(u8 *pos, size_t len)
403{
404 struct rrm_measurement_report_element *msr_rep;
405 u8 *end = pos + len;
406 u8 *msr_rep_end;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700407 struct rrm_measurement_beacon_report *rep = NULL;
408 u8 *subelem;
Hai Shalom74f70d42019-02-11 14:42:39 -0800409
Hai Shalom5f92bc92019-04-18 11:54:11 -0700410 /* Find the last beacon report element */
Hai Shalom74f70d42019-02-11 14:42:39 -0800411 while (end - pos >= (int) sizeof(*msr_rep)) {
412 msr_rep = (struct rrm_measurement_report_element *) pos;
413 msr_rep_end = pos + msr_rep->len + 2;
414
415 if (msr_rep->eid != WLAN_EID_MEASURE_REPORT ||
416 msr_rep_end > end) {
417 /* Should not happen. This indicates a bug. */
418 wpa_printf(MSG_ERROR,
419 "RRM: non-measurement report element in measurement report frame");
420 return -1;
421 }
422
Hai Shalom5f92bc92019-04-18 11:54:11 -0700423 if (msr_rep->type == MEASURE_TYPE_BEACON)
Hai Shalom74f70d42019-02-11 14:42:39 -0800424 rep = (struct rrm_measurement_beacon_report *)
425 msr_rep->variable;
Hai Shalom74f70d42019-02-11 14:42:39 -0800426
427 pos += pos[1] + 2;
428 }
429
Hai Shalom5f92bc92019-04-18 11:54:11 -0700430 if (!rep)
431 return 0;
432
433 subelem = rep->variable;
434 while (subelem + 2 < msr_rep_end &&
435 subelem[0] != WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION)
436 subelem += 2 + subelem[1];
437
438 if (subelem + 2 < msr_rep_end &&
439 subelem[0] == WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION &&
440 subelem[1] == 1 &&
441 subelem + BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN <= end)
442 subelem[2] = 1;
443
Hai Shalom74f70d42019-02-11 14:42:39 -0800444 return 0;
445}
446
447
Dmitry Shmidt29333592017-01-09 12:27:11 -0800448static void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
449 struct wpabuf *buf)
450{
451 int len = wpabuf_len(buf);
Hai Shalom74f70d42019-02-11 14:42:39 -0800452 u8 *pos = wpabuf_mhead_u8(buf), *next = pos;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800453
454#define MPDU_REPORT_LEN (int) (IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN - 3)
455
456 while (len) {
457 int send_len = (len > MPDU_REPORT_LEN) ? next - pos : len;
458
Hai Shalom74f70d42019-02-11 14:42:39 -0800459 if (send_len == len)
460 wpas_rrm_beacon_rep_update_last_frame(pos, len);
461
Dmitry Shmidt29333592017-01-09 12:27:11 -0800462 if (send_len == len ||
463 (send_len + next[1] + 2) > MPDU_REPORT_LEN) {
464 wpas_rrm_send_msr_report_mpdu(wpa_s, pos, send_len);
465 len -= send_len;
466 pos = next;
467 }
468
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700469 if (len)
470 next += next[1] + 2;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800471 }
472#undef MPDU_REPORT_LEN
473}
474
475
476static int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
477 int *freqs)
478{
479 size_t i;
480
481 for (i = 0; i < num_primary_channels; i++) {
482 u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
483
484 freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
Paul Stewart092955c2017-02-06 09:13:09 -0800485 /* ieee80211_chan_to_freq() is not really meant for this
486 * conversion of 20 MHz primary channel numbers for wider VHT
487 * channels, so handle those as special cases here for now. */
488 if (freqs[i] < 0 &&
489 (op_class == 128 || op_class == 129 || op_class == 130))
490 freqs[i] = 5000 + 5 * primary_chan;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800491 if (freqs[i] < 0) {
492 wpa_printf(MSG_DEBUG,
493 "Beacon Report: Invalid channel %u",
494 chan);
495 return -1;
496 }
497 }
498
499 return 0;
500}
501
502
503static int * wpas_add_channels(const struct oper_class_map *op,
Sunil Ravia04bd252022-05-02 22:54:18 -0700504 struct hostapd_hw_modes *mode,
Dmitry Shmidt29333592017-01-09 12:27:11 -0800505 const u8 *channels, const u8 size)
506{
507 int *freqs, *next_freq;
508 u8 num_primary_channels, i;
509 u8 num_chans;
510
511 num_chans = channels ? size :
512 (op->max_chan - op->min_chan) / op->inc + 1;
513
514 if (op->bw == BW80 || op->bw == BW80P80)
515 num_primary_channels = 4;
516 else if (op->bw == BW160)
517 num_primary_channels = 8;
518 else
519 num_primary_channels = 1;
520
521 /* one extra place for the zero-terminator */
522 freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
523 if (!freqs) {
524 wpa_printf(MSG_ERROR,
525 "Beacon Report: Failed to allocate freqs array");
526 return NULL;
527 }
528
529 next_freq = freqs;
530 for (i = 0; i < num_chans; i++) {
531 u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
Hai Shalomc3565922019-10-28 11:58:20 -0700532 enum chan_allowed res = verify_channel(mode, op->op_class, chan,
533 op->bw);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800534
Sunil Ravia04bd252022-05-02 22:54:18 -0700535 if (res == NOT_ALLOWED)
Dmitry Shmidt29333592017-01-09 12:27:11 -0800536 continue;
537
538 if (wpas_add_channel(op->op_class, chan, num_primary_channels,
539 next_freq) < 0) {
540 os_free(freqs);
541 return NULL;
542 }
543
544 next_freq += num_primary_channels;
545 }
546
547 if (!freqs[0]) {
548 os_free(freqs);
549 return NULL;
550 }
551
552 return freqs;
553}
554
555
556static int * wpas_op_class_freqs(const struct oper_class_map *op,
Sunil Ravia04bd252022-05-02 22:54:18 -0700557 struct hostapd_hw_modes *mode)
Dmitry Shmidt29333592017-01-09 12:27:11 -0800558{
Hai Shalom60840252021-02-19 19:02:11 -0800559 u8 channels_80mhz_5ghz[] = { 42, 58, 106, 122, 138, 155, 171 };
560 u8 channels_160mhz_5ghz[] = { 50, 114, 163 };
561 u8 channels_80mhz_6ghz[] = { 7, 23, 39, 55, 71, 87, 103, 119, 135, 151,
562 167, 183, 199, 215 };
563 u8 channels_160mhz_6ghz[] = { 15, 47, 79, 111, 143, 175, 207 };
564 const u8 *channels = NULL;
565 size_t num_chan = 0;
566 bool is_6ghz = is_6ghz_op_class(op->op_class);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800567
568 /*
569 * When adding all channels in the operating class, 80 + 80 MHz
570 * operating classes are like 80 MHz channels because we add all valid
571 * channels anyway.
572 */
Hai Shalom60840252021-02-19 19:02:11 -0800573 if (op->bw == BW80 || op->bw == BW80P80) {
574 channels = is_6ghz ? channels_80mhz_6ghz : channels_80mhz_5ghz;
575 num_chan = is_6ghz ? ARRAY_SIZE(channels_80mhz_6ghz) :
576 ARRAY_SIZE(channels_80mhz_5ghz);
577 } else if (op->bw == BW160) {
578 channels = is_6ghz ? channels_160mhz_6ghz :
579 channels_160mhz_5ghz;
580 num_chan = is_6ghz ? ARRAY_SIZE(channels_160mhz_6ghz) :
581 ARRAY_SIZE(channels_160mhz_5ghz);
582 }
Dmitry Shmidt29333592017-01-09 12:27:11 -0800583
Sunil Ravia04bd252022-05-02 22:54:18 -0700584 return wpas_add_channels(op, mode, channels, num_chan);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800585}
586
587
Sunil Ravia04bd252022-05-02 22:54:18 -0700588static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s,
Dmitry Shmidt29333592017-01-09 12:27:11 -0800589 const char *country, const u8 *subelems,
590 size_t len)
591{
592 int *freqs = NULL, *new_freqs;
593 const u8 *end = subelems + len;
594
595 while (end - subelems > 2) {
596 const struct oper_class_map *op;
597 const u8 *ap_chan_elem, *pos;
598 u8 left;
599 struct hostapd_hw_modes *mode;
600
601 ap_chan_elem = get_ie(subelems, end - subelems,
602 WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
603 if (!ap_chan_elem)
604 break;
605 pos = ap_chan_elem + 2;
606 left = ap_chan_elem[1];
607 if (left < 1)
608 break;
609 subelems = ap_chan_elem + 2 + left;
610
611 op = get_oper_class(country, *pos);
612 if (!op) {
613 wpa_printf(MSG_DEBUG,
614 "Beacon request: unknown operating class in AP Channel Report subelement %u",
615 *pos);
616 goto out;
617 }
618 pos++;
619 left--;
620
Hai Shalomfdcde762020-04-02 11:19:20 -0700621 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode,
622 is_6ghz_op_class(op->op_class));
Dmitry Shmidt29333592017-01-09 12:27:11 -0800623 if (!mode)
624 continue;
625
626 /*
627 * For 80 + 80 MHz operating classes, this AP Channel Report
628 * element should be followed by another element specifying
629 * the second 80 MHz channel. For now just add this 80 MHz
630 * channel, the second 80 MHz channel will be added when the
631 * next element is parsed.
632 * TODO: Verify that this AP Channel Report element is followed
633 * by a corresponding AP Channel Report element as specified in
634 * IEEE Std 802.11-2016, 11.11.9.1.
635 */
Sunil Ravia04bd252022-05-02 22:54:18 -0700636 new_freqs = wpas_add_channels(op, mode, pos, left);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800637 if (new_freqs)
638 int_array_concat(&freqs, new_freqs);
639
640 os_free(new_freqs);
641 }
642
643 return freqs;
644out:
645 os_free(freqs);
646 return NULL;
647}
648
649
650static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
Sunil Ravia04bd252022-05-02 22:54:18 -0700651 u8 op_class, u8 chan,
Dmitry Shmidt29333592017-01-09 12:27:11 -0800652 const u8 *subelems, size_t len)
653{
654 int *freqs = NULL, *ext_freqs = NULL;
655 struct hostapd_hw_modes *mode;
656 const char *country = NULL;
657 const struct oper_class_map *op;
658 const u8 *elem;
659
660 if (!wpa_s->current_bss)
661 return NULL;
662 elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
663 if (elem && elem[1] >= 2)
664 country = (const char *) (elem + 2);
665
666 op = get_oper_class(country, op_class);
667 if (!op) {
668 wpa_printf(MSG_DEBUG,
669 "Beacon request: invalid operating class %d",
670 op_class);
671 return NULL;
672 }
673
Hai Shalomfdcde762020-04-02 11:19:20 -0700674 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode,
675 is_6ghz_op_class(op->op_class));
Dmitry Shmidt29333592017-01-09 12:27:11 -0800676 if (!mode)
677 return NULL;
678
679 switch (chan) {
680 case 0:
Sunil Ravia04bd252022-05-02 22:54:18 -0700681 freqs = wpas_op_class_freqs(op, mode);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800682 if (!freqs)
683 return NULL;
684 break;
685 case 255:
686 /* freqs will be added from AP channel subelements */
687 break;
688 default:
Sunil Ravia04bd252022-05-02 22:54:18 -0700689 freqs = wpas_add_channels(op, mode, &chan, 1);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800690 if (!freqs)
691 return NULL;
692 break;
693 }
694
Sunil Ravia04bd252022-05-02 22:54:18 -0700695 ext_freqs = wpas_channel_report_freqs(wpa_s, country, subelems, len);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800696 if (ext_freqs) {
697 int_array_concat(&freqs, ext_freqs);
698 os_free(ext_freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700699 int_array_sort_unique(freqs);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800700 }
701
702 return freqs;
703}
704
705
Hai Shalomfdcde762020-04-02 11:19:20 -0700706int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
707 u8 *op_class, u8 *chan, u8 *phy_type)
Dmitry Shmidt29333592017-01-09 12:27:11 -0800708{
709 const u8 *ie;
710 int sec_chan = 0, vht = 0;
711 struct ieee80211_ht_operation *ht_oper = NULL;
712 struct ieee80211_vht_operation *vht_oper = NULL;
713 u8 seg0, seg1;
714
715 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
716 if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
717 u8 sec_chan_offset;
718
719 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
720 sec_chan_offset = ht_oper->ht_param &
721 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
722 if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
723 sec_chan = 1;
724 else if (sec_chan_offset ==
725 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
726 sec_chan = -1;
727 }
728
729 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
730 if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
731 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
732
733 switch (vht_oper->vht_op_info_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000734 case CHANWIDTH_80MHZ:
Dmitry Shmidt29333592017-01-09 12:27:11 -0800735 seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
736 seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
737 if (seg1 && abs(seg1 - seg0) == 8)
Sunil8cd6f4d2022-06-28 18:40:46 +0000738 vht = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800739 else if (seg1)
Sunil8cd6f4d2022-06-28 18:40:46 +0000740 vht = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800741 else
Sunil8cd6f4d2022-06-28 18:40:46 +0000742 vht = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800743 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000744 case CHANWIDTH_160MHZ:
745 vht = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800746 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000747 case CHANWIDTH_80P80MHZ:
748 vht = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800749 break;
750 default:
Sunil8cd6f4d2022-06-28 18:40:46 +0000751 vht = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800752 break;
753 }
754 }
755
756 if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
757 chan) == NUM_HOSTAPD_MODES) {
758 wpa_printf(MSG_DEBUG,
759 "Cannot determine operating class and channel");
760 return -1;
761 }
762
763 *phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
764 vht_oper != NULL);
765 if (*phy_type == PHY_TYPE_UNSPECIFIED) {
766 wpa_printf(MSG_DEBUG, "Cannot determine phy type");
767 return -1;
768 }
769
770 return 0;
771}
772
773
774static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000775 struct bitfield *ext_eids,
Dmitry Shmidt29333592017-01-09 12:27:11 -0800776 enum beacon_report_detail detail,
777 struct wpa_bss *bss, u8 *buf,
Hai Shalom60840252021-02-19 19:02:11 -0800778 size_t buf_len, const u8 **ies_buf,
Hai Shalom74f70d42019-02-11 14:42:39 -0800779 size_t *ie_len, int add_fixed)
Dmitry Shmidt29333592017-01-09 12:27:11 -0800780{
Hai Shalom60840252021-02-19 19:02:11 -0800781 const u8 *ies = *ies_buf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800782 size_t ies_len = *ie_len;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800783 u8 *pos = buf;
784 int rem_len;
785
786 rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
Hai Shalom74f70d42019-02-11 14:42:39 -0800787 sizeof(struct rrm_measurement_report_element) - 2 -
788 REPORTED_FRAME_BODY_SUBELEM_LEN;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800789
790 if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
791 wpa_printf(MSG_DEBUG,
792 "Beacon Request: Invalid reporting detail: %d",
793 detail);
794 return -1;
795 }
796
797 if (detail == BEACON_REPORT_DETAIL_NONE)
798 return 0;
799
800 /*
801 * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
802 * beacon interval(2) + capabilities(2) = 14 bytes
803 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800804 if (add_fixed && buf_len < 14)
805 return -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800806
807 *pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
808 /* The length will be filled later */
809 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -0800810
811 if (add_fixed) {
812 WPA_PUT_LE64(pos, bss->tsf);
813 pos += sizeof(bss->tsf);
814 WPA_PUT_LE16(pos, bss->beacon_int);
815 pos += 2;
816 WPA_PUT_LE16(pos, bss->caps);
817 pos += 2;
818 }
Dmitry Shmidt29333592017-01-09 12:27:11 -0800819
820 rem_len -= pos - buf;
821
822 /*
823 * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
824 * body subelement causes the element to exceed the maximum element
825 * size, the subelement is truncated so that the last IE is a complete
826 * IE. So even when required to report all IEs, add elements one after
827 * the other and stop once there is no more room in the measurement
828 * element.
829 */
830 while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
831 if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000832 (eids && bitfield_is_set(eids, ies[0])) ||
833 (ext_eids && ies[0] == WLAN_EID_EXTENSION && ies[1] &&
834 bitfield_is_set(ext_eids, ies[2]))) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800835 u8 elen = ies[1];
Dmitry Shmidt29333592017-01-09 12:27:11 -0800836
837 if (2 + elen > buf + buf_len - pos ||
838 2 + elen > rem_len)
839 break;
840
841 *pos++ = ies[0];
842 *pos++ = elen;
843 os_memcpy(pos, ies + 2, elen);
844 pos += elen;
845 rem_len -= 2 + elen;
846 }
847
848 ies_len -= 2 + ies[1];
849 ies += 2 + ies[1];
850 }
851
Hai Shalom74f70d42019-02-11 14:42:39 -0800852 *ie_len = ies_len;
853 *ies_buf = ies;
854
Dmitry Shmidt29333592017-01-09 12:27:11 -0800855 /* Now the length is known */
856 buf[1] = pos - buf - 2;
857 return pos - buf;
858}
859
860
Hai Shalom74f70d42019-02-11 14:42:39 -0800861static int wpas_add_beacon_rep_elem(struct beacon_rep_data *data,
862 struct wpa_bss *bss,
863 struct wpabuf **wpa_buf,
864 struct rrm_measurement_beacon_report *rep,
Hai Shalom60840252021-02-19 19:02:11 -0800865 const u8 **ie, size_t *ie_len, u8 idx)
Hai Shalom74f70d42019-02-11 14:42:39 -0800866{
867 int ret;
868 u8 *buf, *pos;
869 u32 subelems_len = REPORTED_FRAME_BODY_SUBELEM_LEN +
870 (data->last_indication ?
871 BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN : 0);
872
873 /* Maximum element length: Beacon Report element + Reported Frame Body
874 * subelement + all IEs of the reported Beacon frame + Reported Frame
875 * Body Fragment ID subelement */
876 buf = os_malloc(sizeof(*rep) + 14 + *ie_len + subelems_len);
877 if (!buf)
878 return -1;
879
880 os_memcpy(buf, rep, sizeof(*rep));
881
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000882 ret = wpas_beacon_rep_add_frame_body(data->eids, data->ext_eids,
883 data->report_detail,
Hai Shalom74f70d42019-02-11 14:42:39 -0800884 bss, buf + sizeof(*rep),
885 14 + *ie_len, ie, ie_len,
886 idx == 0);
887 if (ret < 0)
888 goto out;
889
890 pos = buf + ret + sizeof(*rep);
891 pos[0] = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY_FRAGMENT_ID;
892 pos[1] = 2;
893
894 /*
895 * Only one Beacon Report Measurement is supported at a time, so
896 * the Beacon Report ID can always be set to 1.
897 */
898 pos[2] = 1;
899
900 /* Fragment ID Number (bits 0..6) and More Frame Body Fragments (bit 7)
901 */
902 pos[3] = idx;
903 if (data->report_detail != BEACON_REPORT_DETAIL_NONE && *ie_len)
904 pos[3] |= REPORTED_FRAME_BODY_MORE_FRAGMENTS;
905 else
906 pos[3] &= ~REPORTED_FRAME_BODY_MORE_FRAGMENTS;
907
908 pos += REPORTED_FRAME_BODY_SUBELEM_LEN;
909
910 if (data->last_indication) {
911 pos[0] = WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION;
912 pos[1] = 1;
913
914 /* This field will be updated later if this is the last frame */
915 pos[2] = 0;
916 }
917
918 ret = wpas_rrm_report_elem(wpa_buf, data->token,
919 MEASUREMENT_REPORT_MODE_ACCEPT,
920 MEASURE_TYPE_BEACON, buf,
921 ret + sizeof(*rep) + subelems_len);
922out:
923 os_free(buf);
924 return ret;
925}
926
927
Dmitry Shmidt29333592017-01-09 12:27:11 -0800928static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
929 struct wpabuf **wpa_buf, struct wpa_bss *bss,
930 u64 start, u64 parent_tsf)
931{
932 struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
Hai Shalom60840252021-02-19 19:02:11 -0800933 const u8 *ies = wpa_bss_ie_ptr(bss);
934 const u8 *pos = ies;
Hai Shalom74f70d42019-02-11 14:42:39 -0800935 size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
936 struct rrm_measurement_beacon_report rep;
937 u8 idx = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800938
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000939 if (!ether_addr_equal(data->bssid, broadcast_ether_addr) &&
940 !ether_addr_equal(data->bssid, bss->bssid))
Dmitry Shmidt29333592017-01-09 12:27:11 -0800941 return 0;
942
943 if (data->ssid_len &&
944 (data->ssid_len != bss->ssid_len ||
945 os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
946 return 0;
947
Hai Shalom74f70d42019-02-11 14:42:39 -0800948 if (wpas_get_op_chan_phy(bss->freq, ies, ies_len, &rep.op_class,
949 &rep.channel, &rep.report_info) < 0)
950 return 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700951
Hai Shalom74f70d42019-02-11 14:42:39 -0800952 rep.start_time = host_to_le64(start);
953 rep.duration = host_to_le16(data->scan_params.duration);
954 rep.rcpi = rssi_to_rcpi(bss->level);
955 rep.rsni = 255; /* 255 indicates that RSNI is not available */
956 os_memcpy(rep.bssid, bss->bssid, ETH_ALEN);
957 rep.antenna_id = 0; /* unknown */
958 rep.parent_tsf = host_to_le32(parent_tsf);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800959
Hai Shalom74f70d42019-02-11 14:42:39 -0800960 do {
961 int ret;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800962
Hai Shalom74f70d42019-02-11 14:42:39 -0800963 ret = wpas_add_beacon_rep_elem(data, bss, wpa_buf, &rep,
964 &pos, &ies_len, idx++);
965 if (ret)
966 return ret;
967 } while (data->report_detail != BEACON_REPORT_DETAIL_NONE &&
968 ies_len >= 2);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800969
Hai Shalom74f70d42019-02-11 14:42:39 -0800970 return 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800971}
972
973
974static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
975 struct wpabuf **buf)
976{
Paul Stewart092955c2017-02-06 09:13:09 -0800977 return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
Dmitry Shmidt29333592017-01-09 12:27:11 -0800978 MEASUREMENT_REPORT_MODE_ACCEPT,
979 MEASURE_TYPE_BEACON, NULL, 0);
980}
981
982
983static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
984 struct wpabuf **buf)
985{
986 size_t i;
987
988 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
989 if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
990 0, 0) < 0)
991 break;
992 }
993
994 if (!(*buf))
995 wpas_beacon_rep_no_results(wpa_s, buf);
996
997 wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
998}
999
1000
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001001void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
Dmitry Shmidt29333592017-01-09 12:27:11 -08001002{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001003 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr)) {
1004 struct wpabuf *buf = NULL;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001005
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001006 if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
1007 MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
1008 MEASURE_TYPE_BEACON, NULL, 0)) {
1009 wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
1010 wpabuf_free(buf);
1011 return;
1012 }
1013
1014 wpas_rrm_send_msr_report(wpa_s, buf);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001015 wpabuf_free(buf);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001016 }
1017
Dmitry Shmidt29333592017-01-09 12:27:11 -08001018 wpas_clear_beacon_rep_data(wpa_s);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001019}
1020
1021
1022static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1023{
1024 struct wpa_supplicant *wpa_s = eloop_ctx;
1025 struct wpa_driver_scan_params *params =
1026 &wpa_s->beacon_rep_data.scan_params;
1027 u16 prev_duration = params->duration;
1028
1029 if (!wpa_s->current_bss)
1030 return;
1031
1032 if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
1033 params->duration) {
1034 wpa_printf(MSG_DEBUG,
1035 "RRM: Cannot set scan duration due to missing driver support");
1036 params->duration = 0;
1037 }
1038 os_get_reltime(&wpa_s->beacon_rep_scan);
1039 if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001040 wpa_supplicant_trigger_scan(wpa_s, params, true, false))
Dmitry Shmidt29333592017-01-09 12:27:11 -08001041 wpas_rrm_refuse_request(wpa_s);
1042 params->duration = prev_duration;
1043}
1044
1045
1046static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
1047 struct beacon_rep_data *data,
1048 u8 sid, u8 slen, const u8 *subelem)
1049{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001050 struct bitfield *eids;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001051 u8 report_info, i;
1052
1053 switch (sid) {
1054 case WLAN_BEACON_REQUEST_SUBELEM_SSID:
1055 if (!slen) {
1056 wpa_printf(MSG_DEBUG,
1057 "SSID subelement with zero length - wildcard SSID");
1058 break;
1059 }
1060
1061 if (slen > SSID_MAX_LEN) {
1062 wpa_printf(MSG_DEBUG,
1063 "Invalid SSID subelement length: %u", slen);
1064 return -1;
1065 }
1066
1067 data->ssid_len = slen;
1068 os_memcpy(data->ssid, subelem, data->ssid_len);
1069 break;
1070 case WLAN_BEACON_REQUEST_SUBELEM_INFO:
1071 if (slen != 2) {
1072 wpa_printf(MSG_DEBUG,
1073 "Invalid reporting information subelement length: %u",
1074 slen);
1075 return -1;
1076 }
1077
1078 report_info = subelem[0];
1079 if (report_info != 0) {
1080 wpa_printf(MSG_DEBUG,
1081 "reporting information=%u is not supported",
1082 report_info);
1083 return 0;
1084 }
1085 break;
1086 case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
1087 if (slen != 1) {
1088 wpa_printf(MSG_DEBUG,
1089 "Invalid reporting detail subelement length: %u",
1090 slen);
1091 return -1;
1092 }
1093
1094 data->report_detail = subelem[0];
1095 if (data->report_detail >
1096 BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
1097 wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
1098 subelem[0]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001099 return -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001100 }
1101
1102 break;
1103 case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001104 case WLAN_BEACON_REQUEST_SUBELEM_EXT_REQUEST:
Dmitry Shmidt29333592017-01-09 12:27:11 -08001105 if (data->report_detail !=
1106 BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
1107 wpa_printf(MSG_DEBUG,
1108 "Beacon request: request subelement is present but report detail is %u",
1109 data->report_detail);
1110 return -1;
1111 }
1112
1113 if (!slen) {
1114 wpa_printf(MSG_DEBUG,
1115 "Invalid request subelement length: %u",
1116 slen);
1117 return -1;
1118 }
1119
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001120 if (sid == WLAN_BEACON_REQUEST_SUBELEM_EXT_REQUEST) {
1121 if (slen < 2) {
1122 wpa_printf(MSG_DEBUG,
1123 "Invalid extended request");
1124 return -1;
1125 }
1126 if (subelem[0] != WLAN_EID_EXTENSION) {
1127 wpa_printf(MSG_DEBUG,
1128 "Skip unknown Requested Element ID %u in Extended Request subelement",
1129 subelem[0]);
1130 break;
1131 }
1132
1133 /* Skip the Requested Element ID field */
1134 subelem++;
1135 slen--;
1136 }
1137
1138 if ((sid == WLAN_BEACON_REQUEST_SUBELEM_REQUEST &&
1139 data->eids) ||
1140 (sid == WLAN_BEACON_REQUEST_SUBELEM_EXT_REQUEST &&
1141 data->ext_eids)) {
Dmitry Shmidt29333592017-01-09 12:27:11 -08001142 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001143 "Beacon Request: Request sub elements appear more than once");
Dmitry Shmidt29333592017-01-09 12:27:11 -08001144 return -1;
1145 }
1146
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001147 eids = bitfield_alloc(255);
1148 if (!eids) {
Dmitry Shmidt29333592017-01-09 12:27:11 -08001149 wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
1150 return -1;
1151 }
1152
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001153 if (sid == WLAN_BEACON_REQUEST_SUBELEM_REQUEST)
1154 data->eids = eids;
1155 else
1156 data->ext_eids = eids;
1157
Dmitry Shmidt29333592017-01-09 12:27:11 -08001158 for (i = 0; i < slen; i++)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001159 bitfield_set(eids, subelem[i]);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001160 break;
1161 case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
1162 /* Skip - it will be processed when freqs are added */
1163 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08001164 case WLAN_BEACON_REQUEST_SUBELEM_LAST_INDICATION:
1165 if (slen != 1) {
1166 wpa_printf(MSG_DEBUG,
1167 "Beacon request: Invalid last indication request subelement length: %u",
1168 slen);
1169 return -1;
1170 }
1171
1172 data->last_indication = subelem[0];
1173 break;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001174 default:
1175 wpa_printf(MSG_DEBUG,
1176 "Beacon request: Unknown subelement id %u", sid);
1177 break;
1178 }
1179
1180 return 1;
1181}
1182
1183
1184/**
1185 * Returns 0 if the next element can be processed, 1 if some operation was
1186 * triggered, and -1 if processing failed (i.e., the element is in invalid
1187 * format or an internal error occurred).
1188 */
1189static int
1190wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
1191 u8 elem_token, int duration_mandatory,
1192 const struct rrm_measurement_beacon_request *req,
1193 size_t len, struct wpabuf **buf)
1194{
1195 struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1196 struct wpa_driver_scan_params *params = &data->scan_params;
1197 const u8 *subelems;
1198 size_t elems_len;
1199 u16 rand_interval;
1200 u32 interval_usec;
1201 u32 _rand;
1202 int ret = 0, res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001203 u8 reject_mode;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001204
1205 if (len < sizeof(*req))
1206 return -1;
1207
1208 if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
1209 req->mode != BEACON_REPORT_MODE_ACTIVE &&
1210 req->mode != BEACON_REPORT_MODE_TABLE)
1211 return 0;
1212
1213 subelems = req->variable;
1214 elems_len = len - sizeof(*req);
1215 rand_interval = le_to_host16(req->rand_interval);
1216
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001217 os_free(params->freqs);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001218 os_memset(params, 0, sizeof(*params));
1219
1220 data->token = elem_token;
1221
1222 /* default reporting detail is all fixed length fields and all
1223 * elements */
1224 data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
1225 os_memcpy(data->bssid, req->bssid, ETH_ALEN);
1226
1227 while (elems_len >= 2) {
1228 if (subelems[1] > elems_len - 2) {
1229 wpa_printf(MSG_DEBUG,
1230 "Beacon Request: Truncated subelement");
1231 ret = -1;
1232 goto out;
1233 }
1234
1235 res = wpas_rm_handle_beacon_req_subelem(
1236 wpa_s, data, subelems[0], subelems[1], &subelems[2]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001237 if (res < 0) {
Dmitry Shmidt29333592017-01-09 12:27:11 -08001238 ret = res;
1239 goto out;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001240 } else if (!res) {
1241 reject_mode = MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE;
1242 goto out_reject;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001243 }
1244
1245 elems_len -= 2 + subelems[1];
1246 subelems += 2 + subelems[1];
1247 }
1248
1249 if (req->mode == BEACON_REPORT_MODE_TABLE) {
1250 wpas_beacon_rep_table(wpa_s, buf);
1251 goto out;
1252 }
1253
Sunil Ravia04bd252022-05-02 22:54:18 -07001254 params->freqs = wpas_beacon_request_freqs(wpa_s, req->oper_class,
1255 req->channel, req->variable,
1256 len - sizeof(*req));
Dmitry Shmidt29333592017-01-09 12:27:11 -08001257 if (!params->freqs) {
1258 wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001259 reject_mode = MEASUREMENT_REPORT_MODE_REJECT_REFUSED;
1260 goto out_reject;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001261 }
1262
1263 params->duration = le_to_host16(req->duration);
1264 params->duration_mandatory = duration_mandatory;
1265 if (!params->duration) {
1266 wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
1267 ret = -1;
1268 goto out;
1269 }
1270
1271 params->only_new_results = 1;
1272
1273 if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
1274 params->ssids[params->num_ssids].ssid = data->ssid;
1275 params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
1276 }
1277
1278 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
1279 _rand = os_random();
1280 interval_usec = (_rand % (rand_interval + 1)) * 1024;
1281 eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
1282 NULL);
1283 return 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001284out_reject:
1285 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
1286 wpas_rrm_report_elem(buf, elem_token, reject_mode,
1287 MEASURE_TYPE_BEACON, NULL, 0) < 0) {
1288 wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1289 ret = -1;
1290 }
Dmitry Shmidt29333592017-01-09 12:27:11 -08001291out:
1292 wpas_clear_beacon_rep_data(wpa_s);
1293 return ret;
1294}
1295
1296
1297static int
1298wpas_rrm_handle_msr_req_element(
1299 struct wpa_supplicant *wpa_s,
1300 const struct rrm_measurement_request_element *req,
1301 struct wpabuf **buf)
1302{
1303 int duration_mandatory;
1304
1305 wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
1306 req->type, req->token);
1307
1308 if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
1309 /* Enable bit is not supported for now */
1310 wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
1311 return 0;
1312 }
1313
1314 if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
1315 req->type > MEASURE_TYPE_RPI_HIST) {
1316 /* Parallel measurements are not supported for now */
1317 wpa_printf(MSG_DEBUG,
1318 "RRM: Parallel measurements are not supported, reject");
1319 goto reject;
1320 }
1321
1322 duration_mandatory =
1323 !!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
1324
1325 switch (req->type) {
1326 case MEASURE_TYPE_LCI:
1327 return wpas_rrm_build_lci_report(wpa_s, req, buf);
1328 case MEASURE_TYPE_BEACON:
1329 if (duration_mandatory &&
1330 !(wpa_s->drv_rrm_flags &
1331 WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
1332 wpa_printf(MSG_DEBUG,
1333 "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
1334 goto reject;
1335 }
1336 return wpas_rm_handle_beacon_req(wpa_s, req->token,
1337 duration_mandatory,
1338 (const void *) req->variable,
1339 req->len - 3, buf);
1340 default:
1341 wpa_printf(MSG_INFO,
1342 "RRM: Unsupported radio measurement type %u",
1343 req->type);
1344 break;
1345 }
1346
1347reject:
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001348 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
1349 wpas_rrm_report_elem(buf, req->token,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001350 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
1351 req->type, NULL, 0) < 0) {
1352 wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1353 return -1;
1354 }
1355
1356 return 0;
1357}
1358
1359
1360static struct wpabuf *
1361wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
1362 size_t len)
1363{
1364 struct wpabuf *buf = NULL;
1365
1366 while (len) {
1367 const struct rrm_measurement_request_element *req;
1368 int res;
1369
1370 if (len < 2) {
1371 wpa_printf(MSG_DEBUG, "RRM: Truncated element");
1372 goto out;
1373 }
1374
1375 req = (const struct rrm_measurement_request_element *) pos;
1376 if (req->eid != WLAN_EID_MEASURE_REQUEST) {
1377 wpa_printf(MSG_DEBUG,
1378 "RRM: Expected Measurement Request element, but EID is %u",
1379 req->eid);
1380 goto out;
1381 }
1382
1383 if (req->len < 3) {
1384 wpa_printf(MSG_DEBUG, "RRM: Element length too short");
1385 goto out;
1386 }
1387
1388 if (req->len > len - 2) {
1389 wpa_printf(MSG_DEBUG, "RRM: Element length too long");
1390 goto out;
1391 }
1392
1393 res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
1394 if (res < 0)
1395 goto out;
1396
1397 pos += req->len + 2;
1398 len -= req->len + 2;
1399 }
1400
1401 return buf;
1402
1403out:
1404 wpabuf_free(buf);
1405 return NULL;
1406}
1407
1408
1409void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001410 const u8 *src, const u8 *dst,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001411 const u8 *frame, size_t len)
1412{
1413 struct wpabuf *report;
1414
1415 if (wpa_s->wpa_state != WPA_COMPLETED) {
1416 wpa_printf(MSG_INFO,
1417 "RRM: Ignoring radio measurement request: Not associated");
1418 return;
1419 }
1420
1421 if (!wpa_s->rrm.rrm_used) {
1422 wpa_printf(MSG_INFO,
1423 "RRM: Ignoring radio measurement request: Not RRM network");
1424 return;
1425 }
1426
1427 if (len < 3) {
1428 wpa_printf(MSG_INFO,
1429 "RRM: Ignoring too short radio measurement request");
1430 return;
1431 }
1432
1433 wpa_s->rrm.token = *frame;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001434 os_memcpy(wpa_s->rrm.dst_addr, dst, ETH_ALEN);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001435
1436 /* Number of repetitions is not supported */
1437
1438 report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
1439 if (!report)
1440 return;
1441
1442 wpas_rrm_send_msr_report(wpa_s, report);
1443 wpabuf_free(report);
1444}
1445
1446
1447void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
1448 const u8 *src,
1449 const u8 *frame, size_t len,
1450 int rssi)
1451{
1452 struct wpabuf *buf;
1453 const struct rrm_link_measurement_request *req;
1454 struct rrm_link_measurement_report report;
1455
1456 if (wpa_s->wpa_state != WPA_COMPLETED) {
1457 wpa_printf(MSG_INFO,
1458 "RRM: Ignoring link measurement request. Not associated");
1459 return;
1460 }
1461
1462 if (!wpa_s->rrm.rrm_used) {
1463 wpa_printf(MSG_INFO,
1464 "RRM: Ignoring link measurement request. Not RRM network");
1465 return;
1466 }
1467
1468 if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
1469 wpa_printf(MSG_INFO,
1470 "RRM: Measurement report failed. TX power insertion not supported");
1471 return;
1472 }
1473
1474 req = (const struct rrm_link_measurement_request *) frame;
1475 if (len < sizeof(*req)) {
1476 wpa_printf(MSG_INFO,
1477 "RRM: Link measurement report failed. Request too short");
1478 return;
1479 }
1480
1481 os_memset(&report, 0, sizeof(report));
Paul Stewart092955c2017-02-06 09:13:09 -08001482 report.dialog_token = req->dialog_token;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001483 report.tpc.eid = WLAN_EID_TPC_REPORT;
1484 report.tpc.len = 2;
Paul Stewart092955c2017-02-06 09:13:09 -08001485 /* Note: The driver is expected to update report.tpc.tx_power and
1486 * report.tpc.link_margin subfields when sending out this frame.
1487 * Similarly, the driver would need to update report.rx_ant_id and
1488 * report.tx_ant_id subfields. */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001489 report.rsni = 255; /* 255 indicates that RSNI is not available */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001490 report.rcpi = rssi_to_rcpi(rssi);
1491
1492 /* action_category + action_code */
1493 buf = wpabuf_alloc(2 + sizeof(report));
1494 if (buf == NULL) {
1495 wpa_printf(MSG_ERROR,
1496 "RRM: Link measurement report failed. Buffer allocation failed");
1497 return;
1498 }
1499
1500 wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
1501 wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
1502 wpabuf_put_data(buf, &report, sizeof(report));
Paul Stewart092955c2017-02-06 09:13:09 -08001503 wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001504
1505 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
1506 wpa_s->own_addr, wpa_s->bssid,
1507 wpabuf_head(buf), wpabuf_len(buf), 0)) {
1508 wpa_printf(MSG_ERROR,
1509 "RRM: Link measurement report failed. Send action failed");
1510 }
1511 wpabuf_free(buf);
1512}
1513
1514
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001515static bool wpas_beacon_rep_scan_match(struct wpa_supplicant *wpa_s,
1516 const u8 *bssid)
1517{
1518 u8 i;
1519
1520 if (!wpa_s->valid_links)
1521 return ether_addr_equal(wpa_s->current_bss->bssid, bssid);
1522
1523 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
1524 if (!(wpa_s->valid_links & BIT(i)))
1525 continue;
1526
1527 if (ether_addr_equal(wpa_s->links[i].bssid, bssid))
1528 return true;
1529 }
1530
1531 wpa_printf(MSG_DEBUG, "RRM: MLD: no match for TSF BSSID=" MACSTR,
1532 MAC2STR(bssid));
1533
1534 return false;
1535}
1536
1537
Dmitry Shmidt29333592017-01-09 12:27:11 -08001538int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
1539 struct wpa_scan_results *scan_res,
1540 struct scan_info *info)
1541{
1542 size_t i = 0;
1543 struct wpabuf *buf = NULL;
1544
1545 if (!wpa_s->beacon_rep_data.token)
1546 return 0;
1547
1548 if (!wpa_s->current_bss)
1549 goto out;
1550
1551 /* If the measurement was aborted, don't report partial results */
1552 if (info->aborted)
1553 goto out;
1554
1555 wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
1556 MAC2STR(info->scan_start_tsf_bssid),
1557 MAC2STR(wpa_s->current_bss->bssid));
1558 if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001559 !wpas_beacon_rep_scan_match(wpa_s, info->scan_start_tsf_bssid)) {
Dmitry Shmidt29333592017-01-09 12:27:11 -08001560 wpa_printf(MSG_DEBUG,
1561 "RRM: Ignore scan results due to mismatching TSF BSSID");
1562 goto out;
1563 }
1564
1565 for (i = 0; i < scan_res->num; i++) {
1566 struct wpa_bss *bss =
1567 wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
1568
1569 if (!bss)
1570 continue;
1571
1572 if ((wpa_s->drv_rrm_flags &
1573 WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001574 !wpas_beacon_rep_scan_match(wpa_s,
1575 scan_res->res[i]->tsf_bssid)) {
Dmitry Shmidt29333592017-01-09 12:27:11 -08001576 wpa_printf(MSG_DEBUG,
1577 "RRM: Ignore scan result for " MACSTR
1578 " due to mismatching TSF BSSID" MACSTR,
1579 MAC2STR(scan_res->res[i]->bssid),
1580 MAC2STR(scan_res->res[i]->tsf_bssid));
1581 continue;
1582 }
1583
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001584 /*
1585 * Don't report results that were not received during the
1586 * current measurement.
1587 */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001588 if (!(wpa_s->drv_rrm_flags &
1589 WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
1590 struct os_reltime update_time, diff;
1591
1592 /* For now, allow 8 ms older results due to some
1593 * unknown issue with cfg80211 BSS table updates during
1594 * a scan with the current BSS.
1595 * TODO: Fix this more properly to avoid having to have
1596 * this type of hacks in place. */
1597 calculate_update_time(&scan_res->fetch_time,
1598 scan_res->res[i]->age,
1599 &update_time);
1600 os_reltime_sub(&wpa_s->beacon_rep_scan,
1601 &update_time, &diff);
1602 if (os_reltime_before(&update_time,
1603 &wpa_s->beacon_rep_scan) &&
1604 (diff.sec || diff.usec >= 8000)) {
1605 wpa_printf(MSG_DEBUG,
1606 "RRM: Ignore scan result for " MACSTR
1607 " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
1608 MAC2STR(scan_res->res[i]->bssid),
1609 scan_res->res[i]->age,
1610 (unsigned int) diff.sec,
1611 (unsigned int) diff.usec);
1612 continue;
1613 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001614 } else if (info->scan_start_tsf >
1615 scan_res->res[i]->parent_tsf) {
Dmitry Shmidt29333592017-01-09 12:27:11 -08001616 continue;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001617 }
Dmitry Shmidt29333592017-01-09 12:27:11 -08001618
1619 if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
1620 scan_res->res[i]->parent_tsf) < 0)
1621 break;
1622 }
1623
1624 if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
1625 goto out;
1626
1627 wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
1628
1629 wpas_rrm_send_msr_report(wpa_s, buf);
1630 wpabuf_free(buf);
1631
1632out:
1633 wpas_clear_beacon_rep_data(wpa_s);
1634 return 1;
1635}
1636
1637
1638void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
1639{
1640 struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1641
1642 eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
1643 bitfield_free(data->eids);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001644 bitfield_free(data->ext_eids);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001645 os_free(data->scan_params.freqs);
1646 os_memset(data, 0, sizeof(*data));
1647}