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