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