]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/rrm.c
Remove deprecated text for ap_scan=0
[thirdparty/hostap.git] / wpa_supplicant / rrm.c
CommitLineData
ec493469
AS
1/*
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"
76196ddb
AS
17#include "scan.h"
18#include "p2p_supplicant.h"
ec493469
AS
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;
76196ddb 52 wpas_clear_beacon_rep_data(wpa_s);
ec493469
AS
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) {
bf3ab50f 82 wpa_msg(wpa_s, MSG_INFO, "RRM: Unexpected neighbor report");
ec493469
AS
83 return;
84 }
85
86 /* skipping the first byte, which is only an id (dialog token) */
87 neighbor_rep = wpabuf_alloc(report_len - 1);
6657bb15
JM
88 if (!neighbor_rep) {
89 wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
ec493469 90 return;
6657bb15 91 }
ec493469 92 wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
bf3ab50f
BG
93 wpa_dbg(wpa_s, MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
94 report[0]);
ec493469
AS
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 */
bf15b155 104#ifndef ENOTCONN
ec493469 105#define ENOTCONN -1
bf15b155
JM
106#endif
107#ifndef EOPNOTSUPP
ec493469 108#define EOPNOTSUPP -1
bf15b155
JM
109#endif
110#ifndef ECANCELED
ec493469
AS
111#define ECANCELED -1
112#endif
bf15b155 113#endif
ec493469
AS
114
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) {
bf3ab50f 151 wpa_dbg(wpa_s, MSG_DEBUG, "RRM: No connection, no RRM.");
ec493469
AS
152 return -ENOTCONN;
153 }
154
155 if (!wpa_s->rrm.rrm_used) {
bf3ab50f 156 wpa_dbg(wpa_s, MSG_DEBUG, "RRM: No RRM in current connection.");
ec493469
AS
157 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)) {
bf3ab50f
BG
164 wpa_dbg(wpa_s, MSG_DEBUG,
165 "RRM: No network support for Neighbor Report.");
ec493469
AS
166 return -EOPNOTSUPP;
167 }
168
ec493469
AS
169 /* Refuse if there's a live request */
170 if (wpa_s->rrm.notify_neighbor_rep) {
bf3ab50f
BG
171 wpa_dbg(wpa_s, MSG_DEBUG,
172 "RRM: Currently handling previous Neighbor Report.");
ec493469
AS
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) {
bf3ab50f
BG
181 wpa_dbg(wpa_s, MSG_DEBUG,
182 "RRM: Failed to allocate Neighbor Report Request");
ec493469
AS
183 return -ENOMEM;
184 }
185
bf3ab50f
BG
186 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);
ec493469
AS
190
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) {
bf3ab50f
BG
271 wpa_dbg(wpa_s, MSG_DEBUG,
272 "RRM: Failed to send Neighbor Report Request");
ec493469
AS
273 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
5cda3508 288static int wpas_rrm_report_elem(struct wpabuf **buf, u8 token, u8 mode, u8 type,
e1b96e11
AS
289 const u8 *data, size_t data_len)
290{
5cda3508 291 if (wpabuf_resize(buf, 5 + data_len))
e1b96e11
AS
292 return -1;
293
5cda3508
JM
294 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);
e1b96e11
AS
299
300 if (data_len)
5cda3508 301 wpabuf_put_data(*buf, data, data_len);
e1b96e11
AS
302
303 return 0;
304}
305
306
3c716fdb
AS
307static int
308wpas_rrm_build_lci_report(struct wpa_supplicant *wpa_s,
309 const struct rrm_measurement_request_element *req,
310 struct wpabuf **buf)
ec493469 311{
3c716fdb 312 u8 subject;
ec493469
AS
313 u16 max_age = 0;
314 struct os_reltime t, diff;
315 unsigned long diff_l;
ec493469 316 const u8 *subelem;
3c716fdb
AS
317 const u8 *request = req->variable;
318 size_t len = req->len - 3;
319
7187e209 320 if (len < 1)
3c716fdb 321 return -1;
ec493469 322
3c716fdb
AS
323 if (!wpa_s->lci)
324 goto reject;
ec493469 325
ec493469 326 subject = *request++;
3c716fdb 327 len--;
ec493469 328
3c716fdb
AS
329 wpa_printf(MSG_DEBUG, "Measurement request location subject=%u",
330 subject);
ec493469 331
3c716fdb 332 if (subject != LOCATION_SUBJECT_REMOTE) {
ec493469 333 wpa_printf(MSG_INFO,
3c716fdb
AS
334 "Not building LCI report - bad location subject");
335 return 0;
ec493469
AS
336 }
337
338 /* Subelements are formatted exactly like elements */
7187e209 339 wpa_hexdump(MSG_DEBUG, "LCI request subelements", request, len);
ec493469
AS
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))
3c716fdb 345 goto reject;
ec493469
AS
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)
3c716fdb
AS
352 goto reject;
353
5cda3508 354 if (wpas_rrm_report_elem(buf, req->token,
3c716fdb
AS
355 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;
ec493469 363
3c716fdb 364reject:
b3c148e9
AS
365 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
366 wpas_rrm_report_elem(buf, req->token,
3c716fdb
AS
367 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 }
ec493469 372
3c716fdb 373 return 0;
ec493469
AS
374}
375
376
0c73e410
AS
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
ecef0687
AS
402static 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;
56ac1f9d
AS
407 struct rrm_measurement_beacon_report *rep = NULL;
408 u8 *subelem;
ecef0687 409
56ac1f9d 410 /* Find the last beacon report element */
ecef0687
AS
411 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
56ac1f9d 423 if (msr_rep->type == MEASURE_TYPE_BEACON)
ecef0687
AS
424 rep = (struct rrm_measurement_beacon_report *)
425 msr_rep->variable;
ecef0687
AS
426
427 pos += pos[1] + 2;
428 }
429
56ac1f9d
AS
430 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
ecef0687
AS
444 return 0;
445}
446
447
0c73e410
AS
448static void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
449 struct wpabuf *buf)
450{
451 int len = wpabuf_len(buf);
ecef0687 452 u8 *pos = wpabuf_mhead_u8(buf), *next = pos;
0c73e410
AS
453
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
ecef0687
AS
459 if (send_len == len)
460 wpas_rrm_beacon_rep_update_last_frame(pos, len);
461
0c73e410
AS
462 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
b696f791
JM
469 if (len)
470 next += next[1] + 2;
0c73e410
AS
471 }
472#undef MPDU_REPORT_LEN
473}
474
475
76196ddb
AS
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);
40e9a3f3
JM
485 /* 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;
76196ddb
AS
491 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,
504 struct hostapd_hw_modes *mode, int active,
505 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;
89450024
JM
532 enum chan_allowed res = verify_channel(mode, op->op_class, chan,
533 op->bw);
76196ddb
AS
534
535 if (res == NOT_ALLOWED || (res == NO_IR && active))
536 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,
557 struct hostapd_hw_modes *mode, int active)
558{
559 u8 channels_80mhz[] = { 42, 58, 106, 122, 138, 155 };
560 u8 channels_160mhz[] = { 50, 114 };
561
562 /*
563 * When adding all channels in the operating class, 80 + 80 MHz
564 * operating classes are like 80 MHz channels because we add all valid
565 * channels anyway.
566 */
567 if (op->bw == BW80 || op->bw == BW80P80)
568 return wpas_add_channels(op, mode, active, channels_80mhz,
569 ARRAY_SIZE(channels_80mhz));
570
571 if (op->bw == BW160)
572 return wpas_add_channels(op, mode, active, channels_160mhz,
573 ARRAY_SIZE(channels_160mhz));
574
575 return wpas_add_channels(op, mode, active, NULL, 0);
576}
577
578
579static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s, int active,
580 const char *country, const u8 *subelems,
581 size_t len)
582{
583 int *freqs = NULL, *new_freqs;
584 const u8 *end = subelems + len;
585
586 while (end - subelems > 2) {
587 const struct oper_class_map *op;
588 const u8 *ap_chan_elem, *pos;
589 u8 left;
590 struct hostapd_hw_modes *mode;
591
592 ap_chan_elem = get_ie(subelems, end - subelems,
593 WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
594 if (!ap_chan_elem)
595 break;
596 pos = ap_chan_elem + 2;
597 left = ap_chan_elem[1];
598 if (left < 1)
599 break;
600 subelems = ap_chan_elem + 2 + left;
601
602 op = get_oper_class(country, *pos);
603 if (!op) {
604 wpa_printf(MSG_DEBUG,
605 "Beacon request: unknown operating class in AP Channel Report subelement %u",
606 *pos);
607 goto out;
608 }
609 pos++;
610 left--;
611
612 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
613 if (!mode)
614 continue;
615
616 /*
617 * For 80 + 80 MHz operating classes, this AP Channel Report
618 * element should be followed by another element specifying
619 * the second 80 MHz channel. For now just add this 80 MHz
620 * channel, the second 80 MHz channel will be added when the
621 * next element is parsed.
622 * TODO: Verify that this AP Channel Report element is followed
623 * by a corresponding AP Channel Report element as specified in
624 * IEEE Std 802.11-2016, 11.11.9.1.
625 */
626 new_freqs = wpas_add_channels(op, mode, active, pos, left);
627 if (new_freqs)
628 int_array_concat(&freqs, new_freqs);
629
630 os_free(new_freqs);
631 }
632
633 return freqs;
634out:
635 os_free(freqs);
636 return NULL;
637}
638
639
640static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
641 u8 op_class, u8 chan, int active,
642 const u8 *subelems, size_t len)
643{
644 int *freqs = NULL, *ext_freqs = NULL;
645 struct hostapd_hw_modes *mode;
646 const char *country = NULL;
647 const struct oper_class_map *op;
648 const u8 *elem;
649
650 if (!wpa_s->current_bss)
651 return NULL;
652 elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
653 if (elem && elem[1] >= 2)
654 country = (const char *) (elem + 2);
655
656 op = get_oper_class(country, op_class);
657 if (!op) {
658 wpa_printf(MSG_DEBUG,
659 "Beacon request: invalid operating class %d",
660 op_class);
661 return NULL;
662 }
663
664 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
665 if (!mode)
666 return NULL;
667
668 switch (chan) {
669 case 0:
670 freqs = wpas_op_class_freqs(op, mode, active);
671 if (!freqs)
672 return NULL;
673 break;
674 case 255:
675 /* freqs will be added from AP channel subelements */
676 break;
677 default:
678 freqs = wpas_add_channels(op, mode, active, &chan, 1);
679 if (!freqs)
680 return NULL;
681 break;
682 }
683
684 ext_freqs = wpas_channel_report_freqs(wpa_s, active, country, subelems,
685 len);
686 if (ext_freqs) {
687 int_array_concat(&freqs, ext_freqs);
688 os_free(ext_freqs);
922dcf1b 689 int_array_sort_unique(freqs);
76196ddb
AS
690 }
691
692 return freqs;
693}
694
695
30eddf35
JB
696static int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
697 u8 *op_class, u8 *chan, u8 *phy_type)
76196ddb
AS
698{
699 const u8 *ie;
700 int sec_chan = 0, vht = 0;
701 struct ieee80211_ht_operation *ht_oper = NULL;
702 struct ieee80211_vht_operation *vht_oper = NULL;
703 u8 seg0, seg1;
704
705 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
706 if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
707 u8 sec_chan_offset;
708
709 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
710 sec_chan_offset = ht_oper->ht_param &
711 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
712 if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
713 sec_chan = 1;
714 else if (sec_chan_offset ==
715 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
716 sec_chan = -1;
717 }
718
719 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
720 if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
721 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
722
723 switch (vht_oper->vht_op_info_chwidth) {
724 case 1:
725 seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
726 seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
727 if (seg1 && abs(seg1 - seg0) == 8)
464dcfd0 728 vht = CHANWIDTH_160MHZ;
76196ddb 729 else if (seg1)
464dcfd0 730 vht = CHANWIDTH_80P80MHZ;
76196ddb 731 else
464dcfd0 732 vht = CHANWIDTH_80MHZ;
76196ddb
AS
733 break;
734 case 2:
464dcfd0 735 vht = CHANWIDTH_160MHZ;
76196ddb
AS
736 break;
737 case 3:
464dcfd0 738 vht = CHANWIDTH_80P80MHZ;
76196ddb
AS
739 break;
740 default:
464dcfd0 741 vht = CHANWIDTH_USE_HT;
76196ddb
AS
742 break;
743 }
744 }
745
746 if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
747 chan) == NUM_HOSTAPD_MODES) {
748 wpa_printf(MSG_DEBUG,
749 "Cannot determine operating class and channel");
750 return -1;
751 }
752
753 *phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
754 vht_oper != NULL);
755 if (*phy_type == PHY_TYPE_UNSPECIFIED) {
756 wpa_printf(MSG_DEBUG, "Cannot determine phy type");
757 return -1;
758 }
759
760 return 0;
761}
762
763
764static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
765 enum beacon_report_detail detail,
766 struct wpa_bss *bss, u8 *buf,
3ec11878
AS
767 size_t buf_len, u8 **ies_buf,
768 size_t *ie_len, int add_fixed)
76196ddb 769{
3ec11878
AS
770 u8 *ies = *ies_buf;
771 size_t ies_len = *ie_len;
76196ddb
AS
772 u8 *pos = buf;
773 int rem_len;
774
775 rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
3ec11878
AS
776 sizeof(struct rrm_measurement_report_element) - 2 -
777 REPORTED_FRAME_BODY_SUBELEM_LEN;
76196ddb
AS
778
779 if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
780 wpa_printf(MSG_DEBUG,
781 "Beacon Request: Invalid reporting detail: %d",
782 detail);
783 return -1;
784 }
785
786 if (detail == BEACON_REPORT_DETAIL_NONE)
787 return 0;
788
789 /*
790 * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
791 * beacon interval(2) + capabilities(2) = 14 bytes
792 */
3ec11878
AS
793 if (add_fixed && buf_len < 14)
794 return -1;
76196ddb
AS
795
796 *pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
797 /* The length will be filled later */
798 pos++;
3ec11878
AS
799
800 if (add_fixed) {
801 WPA_PUT_LE64(pos, bss->tsf);
802 pos += sizeof(bss->tsf);
803 WPA_PUT_LE16(pos, bss->beacon_int);
804 pos += 2;
805 WPA_PUT_LE16(pos, bss->caps);
806 pos += 2;
807 }
76196ddb
AS
808
809 rem_len -= pos - buf;
810
811 /*
812 * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
813 * body subelement causes the element to exceed the maximum element
814 * size, the subelement is truncated so that the last IE is a complete
815 * IE. So even when required to report all IEs, add elements one after
816 * the other and stop once there is no more room in the measurement
817 * element.
818 */
819 while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
820 if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
821 (eids && bitfield_is_set(eids, ies[0]))) {
3ec11878 822 u8 elen = ies[1];
76196ddb
AS
823
824 if (2 + elen > buf + buf_len - pos ||
825 2 + elen > rem_len)
826 break;
827
828 *pos++ = ies[0];
829 *pos++ = elen;
830 os_memcpy(pos, ies + 2, elen);
831 pos += elen;
832 rem_len -= 2 + elen;
833 }
834
835 ies_len -= 2 + ies[1];
836 ies += 2 + ies[1];
837 }
838
3ec11878
AS
839 *ie_len = ies_len;
840 *ies_buf = ies;
841
76196ddb
AS
842 /* Now the length is known */
843 buf[1] = pos - buf - 2;
844 return pos - buf;
845}
846
847
3ec11878
AS
848static int wpas_add_beacon_rep_elem(struct beacon_rep_data *data,
849 struct wpa_bss *bss,
850 struct wpabuf **wpa_buf,
851 struct rrm_measurement_beacon_report *rep,
852 u8 **ie, size_t *ie_len, u8 idx)
853{
854 int ret;
855 u8 *buf, *pos;
ecef0687
AS
856 u32 subelems_len = REPORTED_FRAME_BODY_SUBELEM_LEN +
857 (data->last_indication ?
858 BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN : 0);
3ec11878
AS
859
860 /* Maximum element length: Beacon Report element + Reported Frame Body
861 * subelement + all IEs of the reported Beacon frame + Reported Frame
862 * Body Fragment ID subelement */
ecef0687 863 buf = os_malloc(sizeof(*rep) + 14 + *ie_len + subelems_len);
3ec11878
AS
864 if (!buf)
865 return -1;
866
867 os_memcpy(buf, rep, sizeof(*rep));
868
869 ret = wpas_beacon_rep_add_frame_body(data->eids, data->report_detail,
870 bss, buf + sizeof(*rep),
871 14 + *ie_len, ie, ie_len,
872 idx == 0);
873 if (ret < 0)
874 goto out;
875
876 pos = buf + ret + sizeof(*rep);
877 pos[0] = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY_FRAGMENT_ID;
878 pos[1] = 2;
879
880 /*
881 * Only one Beacon Report Measurement is supported at a time, so
882 * the Beacon Report ID can always be set to 1.
883 */
884 pos[2] = 1;
885
886 /* Fragment ID Number (bits 0..6) and More Frame Body Fragments (bit 7)
887 */
888 pos[3] = idx;
889 if (data->report_detail != BEACON_REPORT_DETAIL_NONE && *ie_len)
890 pos[3] |= REPORTED_FRAME_BODY_MORE_FRAGMENTS;
891 else
892 pos[3] &= ~REPORTED_FRAME_BODY_MORE_FRAGMENTS;
893
ecef0687
AS
894 pos += REPORTED_FRAME_BODY_SUBELEM_LEN;
895
896 if (data->last_indication) {
897 pos[0] = WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION;
898 pos[1] = 1;
899
900 /* This field will be updated later if this is the last frame */
901 pos[2] = 0;
902 }
903
3ec11878
AS
904 ret = wpas_rrm_report_elem(wpa_buf, data->token,
905 MEASUREMENT_REPORT_MODE_ACCEPT,
906 MEASURE_TYPE_BEACON, buf,
ecef0687 907 ret + sizeof(*rep) + subelems_len);
3ec11878
AS
908out:
909 os_free(buf);
910 return ret;
911}
912
913
76196ddb
AS
914static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
915 struct wpabuf **wpa_buf, struct wpa_bss *bss,
916 u64 start, u64 parent_tsf)
917{
918 struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
3ec11878
AS
919 u8 *ies = (u8 *) (bss + 1);
920 u8 *pos = ies;
921 size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
922 struct rrm_measurement_beacon_report rep;
923 u8 idx = 0;
76196ddb
AS
924
925 if (os_memcmp(data->bssid, broadcast_ether_addr, ETH_ALEN) != 0 &&
926 os_memcmp(data->bssid, bss->bssid, ETH_ALEN) != 0)
927 return 0;
928
929 if (data->ssid_len &&
930 (data->ssid_len != bss->ssid_len ||
931 os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
932 return 0;
933
3ec11878
AS
934 if (wpas_get_op_chan_phy(bss->freq, ies, ies_len, &rep.op_class,
935 &rep.channel, &rep.report_info) < 0)
936 return 0;
891aa65b 937
3ec11878
AS
938 rep.start_time = host_to_le64(start);
939 rep.duration = host_to_le16(data->scan_params.duration);
940 rep.rcpi = rssi_to_rcpi(bss->level);
941 rep.rsni = 255; /* 255 indicates that RSNI is not available */
942 os_memcpy(rep.bssid, bss->bssid, ETH_ALEN);
943 rep.antenna_id = 0; /* unknown */
944 rep.parent_tsf = host_to_le32(parent_tsf);
76196ddb 945
3ec11878
AS
946 do {
947 int ret;
76196ddb 948
3ec11878
AS
949 ret = wpas_add_beacon_rep_elem(data, bss, wpa_buf, &rep,
950 &pos, &ies_len, idx++);
951 if (ret)
952 return ret;
953 } while (data->report_detail != BEACON_REPORT_DETAIL_NONE &&
954 ies_len >= 2);
76196ddb 955
3ec11878 956 return 0;
76196ddb
AS
957}
958
959
960static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
961 struct wpabuf **buf)
962{
5cda3508 963 return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
76196ddb
AS
964 MEASUREMENT_REPORT_MODE_ACCEPT,
965 MEASURE_TYPE_BEACON, NULL, 0);
966}
967
968
969static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
970 struct wpabuf **buf)
971{
972 size_t i;
973
974 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
975 if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
976 0, 0) < 0)
977 break;
978 }
979
980 if (!(*buf))
981 wpas_beacon_rep_no_results(wpa_s, buf);
982
983 wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
984}
985
986
3756acfd 987void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
76196ddb 988{
b3c148e9
AS
989 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr)) {
990 struct wpabuf *buf = NULL;
991
992 if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
993 MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
994 MEASURE_TYPE_BEACON, NULL, 0)) {
995 wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
996 wpabuf_free(buf);
997 return;
998 }
76196ddb 999
b3c148e9 1000 wpas_rrm_send_msr_report(wpa_s, buf);
76196ddb 1001 wpabuf_free(buf);
76196ddb
AS
1002 }
1003
76196ddb 1004 wpas_clear_beacon_rep_data(wpa_s);
76196ddb
AS
1005}
1006
1007
1008static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1009{
1010 struct wpa_supplicant *wpa_s = eloop_ctx;
1011 struct wpa_driver_scan_params *params =
1012 &wpa_s->beacon_rep_data.scan_params;
d7342014 1013 u16 prev_duration = params->duration;
76196ddb
AS
1014
1015 if (!wpa_s->current_bss)
1016 return;
1017
d7342014
JM
1018 if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
1019 params->duration) {
1020 wpa_printf(MSG_DEBUG,
1021 "RRM: Cannot set scan duration due to missing driver support");
1022 params->duration = 0;
1023 }
1024 os_get_reltime(&wpa_s->beacon_rep_scan);
76196ddb
AS
1025 if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
1026 wpa_supplicant_trigger_scan(wpa_s, params))
1027 wpas_rrm_refuse_request(wpa_s);
d7342014 1028 params->duration = prev_duration;
76196ddb
AS
1029}
1030
1031
1032static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
1033 struct beacon_rep_data *data,
1034 u8 sid, u8 slen, const u8 *subelem)
1035{
1036 u8 report_info, i;
1037
1038 switch (sid) {
1039 case WLAN_BEACON_REQUEST_SUBELEM_SSID:
1040 if (!slen) {
1041 wpa_printf(MSG_DEBUG,
1042 "SSID subelement with zero length - wildcard SSID");
1043 break;
1044 }
1045
1046 if (slen > SSID_MAX_LEN) {
1047 wpa_printf(MSG_DEBUG,
1048 "Invalid SSID subelement length: %u", slen);
1049 return -1;
1050 }
1051
1052 data->ssid_len = slen;
1053 os_memcpy(data->ssid, subelem, data->ssid_len);
1054 break;
1055 case WLAN_BEACON_REQUEST_SUBELEM_INFO:
1056 if (slen != 2) {
1057 wpa_printf(MSG_DEBUG,
1058 "Invalid reporting information subelement length: %u",
1059 slen);
1060 return -1;
1061 }
1062
1063 report_info = subelem[0];
1064 if (report_info != 0) {
1065 wpa_printf(MSG_DEBUG,
1066 "reporting information=%u is not supported",
1067 report_info);
1068 return 0;
1069 }
1070 break;
1071 case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
1072 if (slen != 1) {
1073 wpa_printf(MSG_DEBUG,
1074 "Invalid reporting detail subelement length: %u",
1075 slen);
1076 return -1;
1077 }
1078
1079 data->report_detail = subelem[0];
1080 if (data->report_detail >
1081 BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
1082 wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
1083 subelem[0]);
705e2909 1084 return -1;
76196ddb
AS
1085 }
1086
1087 break;
1088 case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
1089 if (data->report_detail !=
1090 BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
1091 wpa_printf(MSG_DEBUG,
1092 "Beacon request: request subelement is present but report detail is %u",
1093 data->report_detail);
1094 return -1;
1095 }
1096
1097 if (!slen) {
1098 wpa_printf(MSG_DEBUG,
1099 "Invalid request subelement length: %u",
1100 slen);
1101 return -1;
1102 }
1103
1104 if (data->eids) {
1105 wpa_printf(MSG_DEBUG,
1106 "Beacon Request: Request subelement appears more than once");
1107 return -1;
1108 }
1109
1110 data->eids = bitfield_alloc(255);
1111 if (!data->eids) {
1112 wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
1113 return -1;
1114 }
1115
1116 for (i = 0; i < slen; i++)
1117 bitfield_set(data->eids, subelem[i]);
1118 break;
1119 case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
1120 /* Skip - it will be processed when freqs are added */
1121 break;
ecef0687
AS
1122 case WLAN_BEACON_REQUEST_SUBELEM_LAST_INDICATION:
1123 if (slen != 1) {
1124 wpa_printf(MSG_DEBUG,
1125 "Beacon request: Invalid last indication request subelement length: %u",
1126 slen);
1127 return -1;
1128 }
1129
1130 data->last_indication = subelem[0];
1131 break;
76196ddb
AS
1132 default:
1133 wpa_printf(MSG_DEBUG,
1134 "Beacon request: Unknown subelement id %u", sid);
1135 break;
1136 }
1137
1138 return 1;
1139}
1140
1141
1142/**
1143 * Returns 0 if the next element can be processed, 1 if some operation was
1144 * triggered, and -1 if processing failed (i.e., the element is in invalid
1145 * format or an internal error occurred).
1146 */
1147static int
1148wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
1149 u8 elem_token, int duration_mandatory,
1150 const struct rrm_measurement_beacon_request *req,
1151 size_t len, struct wpabuf **buf)
1152{
1153 struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1154 struct wpa_driver_scan_params *params = &data->scan_params;
1155 const u8 *subelems;
1156 size_t elems_len;
1157 u16 rand_interval;
1158 u32 interval_usec;
1159 u32 _rand;
1160 int ret = 0, res;
705e2909 1161 u8 reject_mode;
76196ddb 1162
76196ddb
AS
1163 if (len < sizeof(*req))
1164 return -1;
de6ec8b5 1165
d7342014
JM
1166 if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
1167 req->mode != BEACON_REPORT_MODE_ACTIVE &&
1168 req->mode != BEACON_REPORT_MODE_TABLE)
de6ec8b5
JM
1169 return 0;
1170
76196ddb
AS
1171 subelems = req->variable;
1172 elems_len = len - sizeof(*req);
1173 rand_interval = le_to_host16(req->rand_interval);
1174
4d70b2a4 1175 os_free(params->freqs);
76196ddb
AS
1176 os_memset(params, 0, sizeof(*params));
1177
1178 data->token = elem_token;
1179
1180 /* default reporting detail is all fixed length fields and all
1181 * elements */
1182 data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
1183 os_memcpy(data->bssid, req->bssid, ETH_ALEN);
1184
1185 while (elems_len >= 2) {
1186 if (subelems[1] > elems_len - 2) {
1187 wpa_printf(MSG_DEBUG,
1188 "Beacon Request: Truncated subelement");
1189 ret = -1;
1190 goto out;
1191 }
1192
1193 res = wpas_rm_handle_beacon_req_subelem(
1194 wpa_s, data, subelems[0], subelems[1], &subelems[2]);
705e2909 1195 if (res < 0) {
76196ddb
AS
1196 ret = res;
1197 goto out;
705e2909
AS
1198 } else if (!res) {
1199 reject_mode = MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE;
1200 goto out_reject;
76196ddb
AS
1201 }
1202
1203 elems_len -= 2 + subelems[1];
1204 subelems += 2 + subelems[1];
1205 }
1206
1207 if (req->mode == BEACON_REPORT_MODE_TABLE) {
1208 wpas_beacon_rep_table(wpa_s, buf);
1209 goto out;
1210 }
1211
1212 params->freqs = wpas_beacon_request_freqs(
1213 wpa_s, req->oper_class, req->channel,
1214 req->mode == BEACON_REPORT_MODE_ACTIVE,
1215 req->variable, len - sizeof(*req));
1216 if (!params->freqs) {
1217 wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
705e2909
AS
1218 reject_mode = MEASUREMENT_REPORT_MODE_REJECT_REFUSED;
1219 goto out_reject;
76196ddb
AS
1220 }
1221
1222 params->duration = le_to_host16(req->duration);
1223 params->duration_mandatory = duration_mandatory;
1224 if (!params->duration) {
1225 wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
1226 ret = -1;
1227 goto out;
1228 }
1229
1230 params->only_new_results = 1;
1231
1232 if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
1233 params->ssids[params->num_ssids].ssid = data->ssid;
1234 params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
1235 }
1236
1237 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
1238 _rand = os_random();
1239 interval_usec = (_rand % (rand_interval + 1)) * 1024;
1240 eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
1241 NULL);
1242 return 1;
705e2909
AS
1243out_reject:
1244 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
1245 wpas_rrm_report_elem(buf, elem_token, reject_mode,
1246 MEASURE_TYPE_BEACON, NULL, 0) < 0) {
1247 wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1248 ret = -1;
1249 }
76196ddb
AS
1250out:
1251 wpas_clear_beacon_rep_data(wpa_s);
1252 return ret;
1253}
1254
1255
9664ab8b
AS
1256static int
1257wpas_rrm_handle_msr_req_element(
1258 struct wpa_supplicant *wpa_s,
1259 const struct rrm_measurement_request_element *req,
1260 struct wpabuf **buf)
ec493469 1261{
76196ddb
AS
1262 int duration_mandatory;
1263
9664ab8b
AS
1264 wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
1265 req->type, req->token);
1266
e1b96e11
AS
1267 if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
1268 /* Enable bit is not supported for now */
1269 wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
1270 return 0;
1271 }
1272
1273 if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
1274 req->type > MEASURE_TYPE_RPI_HIST) {
1275 /* Parallel measurements are not supported for now */
1276 wpa_printf(MSG_DEBUG,
1277 "RRM: Parallel measurements are not supported, reject");
1278 goto reject;
1279 }
1280
76196ddb
AS
1281 duration_mandatory =
1282 !!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
1283
9664ab8b
AS
1284 switch (req->type) {
1285 case MEASURE_TYPE_LCI:
3c716fdb 1286 return wpas_rrm_build_lci_report(wpa_s, req, buf);
76196ddb 1287 case MEASURE_TYPE_BEACON:
d7342014
JM
1288 if (duration_mandatory &&
1289 !(wpa_s->drv_rrm_flags &
1290 WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
1291 wpa_printf(MSG_DEBUG,
1292 "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
1293 goto reject;
1294 }
76196ddb
AS
1295 return wpas_rm_handle_beacon_req(wpa_s, req->token,
1296 duration_mandatory,
1297 (const void *) req->variable,
1298 req->len - 3, buf);
9664ab8b 1299 default:
ec493469 1300 wpa_printf(MSG_INFO,
9664ab8b
AS
1301 "RRM: Unsupported radio measurement type %u",
1302 req->type);
1303 break;
ec493469
AS
1304 }
1305
e1b96e11 1306reject:
b3c148e9
AS
1307 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
1308 wpas_rrm_report_elem(buf, req->token,
e1b96e11
AS
1309 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
1310 req->type, NULL, 0) < 0) {
1311 wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1312 return -1;
1313 }
1314
9664ab8b
AS
1315 return 0;
1316}
ec493469 1317
ec493469 1318
9664ab8b
AS
1319static struct wpabuf *
1320wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
1321 size_t len)
1322{
1323 struct wpabuf *buf = NULL;
ec493469 1324
9664ab8b 1325 while (len) {
332bf5d3 1326 const struct rrm_measurement_request_element *req;
9664ab8b 1327 int res;
ec493469 1328
9664ab8b 1329 if (len < 2) {
332bf5d3
AS
1330 wpa_printf(MSG_DEBUG, "RRM: Truncated element");
1331 goto out;
1332 }
1333
9664ab8b 1334 req = (const struct rrm_measurement_request_element *) pos;
332bf5d3
AS
1335 if (req->eid != WLAN_EID_MEASURE_REQUEST) {
1336 wpa_printf(MSG_DEBUG,
1337 "RRM: Expected Measurement Request element, but EID is %u",
1338 req->eid);
1339 goto out;
1340 }
1341
1342 if (req->len < 3) {
1343 wpa_printf(MSG_DEBUG, "RRM: Element length too short");
1344 goto out;
1345 }
332bf5d3 1346
9664ab8b 1347 if (req->len > len - 2) {
332bf5d3
AS
1348 wpa_printf(MSG_DEBUG, "RRM: Element length too long");
1349 goto out;
1350 }
ec493469 1351
9664ab8b
AS
1352 res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
1353 if (res < 0)
1354 goto out;
ec493469 1355
9664ab8b
AS
1356 pos += req->len + 2;
1357 len -= req->len + 2;
ec493469
AS
1358 }
1359
9664ab8b
AS
1360 return buf;
1361
1362out:
1363 wpabuf_free(buf);
1364 return NULL;
1365}
1366
1367
1368void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
b3c148e9 1369 const u8 *src, const u8 *dst,
9664ab8b
AS
1370 const u8 *frame, size_t len)
1371{
0c73e410 1372 struct wpabuf *report;
9664ab8b
AS
1373
1374 if (wpa_s->wpa_state != WPA_COMPLETED) {
1375 wpa_printf(MSG_INFO,
1376 "RRM: Ignoring radio measurement request: Not associated");
1377 return;
1378 }
1379
1380 if (!wpa_s->rrm.rrm_used) {
1381 wpa_printf(MSG_INFO,
1382 "RRM: Ignoring radio measurement request: Not RRM network");
1383 return;
1384 }
1385
1386 if (len < 3) {
1387 wpa_printf(MSG_INFO,
1388 "RRM: Ignoring too short radio measurement request");
1389 return;
1390 }
1391
0c73e410 1392 wpa_s->rrm.token = *frame;
b3c148e9 1393 os_memcpy(wpa_s->rrm.dst_addr, dst, ETH_ALEN);
9664ab8b
AS
1394
1395 /* Number of repetitions is not supported */
1396
1397 report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
ec493469
AS
1398 if (!report)
1399 return;
1400
0c73e410 1401 wpas_rrm_send_msr_report(wpa_s, report);
332bf5d3 1402 wpabuf_free(report);
ec493469
AS
1403}
1404
1405
1406void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
1407 const u8 *src,
1408 const u8 *frame, size_t len,
1409 int rssi)
1410{
1411 struct wpabuf *buf;
1412 const struct rrm_link_measurement_request *req;
1413 struct rrm_link_measurement_report report;
1414
1415 if (wpa_s->wpa_state != WPA_COMPLETED) {
1416 wpa_printf(MSG_INFO,
1417 "RRM: Ignoring link measurement request. Not associated");
1418 return;
1419 }
1420
1421 if (!wpa_s->rrm.rrm_used) {
1422 wpa_printf(MSG_INFO,
1423 "RRM: Ignoring link measurement request. Not RRM network");
1424 return;
1425 }
1426
1427 if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
1428 wpa_printf(MSG_INFO,
1429 "RRM: Measurement report failed. TX power insertion not supported");
1430 return;
1431 }
1432
1433 req = (const struct rrm_link_measurement_request *) frame;
1434 if (len < sizeof(*req)) {
1435 wpa_printf(MSG_INFO,
1436 "RRM: Link measurement report failed. Request too short");
1437 return;
1438 }
1439
1440 os_memset(&report, 0, sizeof(report));
33468e53 1441 report.dialog_token = req->dialog_token;
ec493469
AS
1442 report.tpc.eid = WLAN_EID_TPC_REPORT;
1443 report.tpc.len = 2;
33468e53
JM
1444 /* Note: The driver is expected to update report.tpc.tx_power and
1445 * report.tpc.link_margin subfields when sending out this frame.
1446 * Similarly, the driver would need to update report.rx_ant_id and
1447 * report.tx_ant_id subfields. */
ec493469 1448 report.rsni = 255; /* 255 indicates that RSNI is not available */
b3060bf9 1449 report.rcpi = rssi_to_rcpi(rssi);
ec493469
AS
1450
1451 /* action_category + action_code */
1452 buf = wpabuf_alloc(2 + sizeof(report));
1453 if (buf == NULL) {
1454 wpa_printf(MSG_ERROR,
1455 "RRM: Link measurement report failed. Buffer allocation failed");
1456 return;
1457 }
1458
1459 wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
1460 wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
1461 wpabuf_put_data(buf, &report, sizeof(report));
7ba94fc4 1462 wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
ec493469
AS
1463
1464 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
1465 wpa_s->own_addr, wpa_s->bssid,
1466 wpabuf_head(buf), wpabuf_len(buf), 0)) {
1467 wpa_printf(MSG_ERROR,
1468 "RRM: Link measurement report failed. Send action failed");
1469 }
1470 wpabuf_free(buf);
1471}
76196ddb
AS
1472
1473
1474int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
1475 struct wpa_scan_results *scan_res,
1476 struct scan_info *info)
1477{
1478 size_t i = 0;
1479 struct wpabuf *buf = NULL;
1480
1481 if (!wpa_s->beacon_rep_data.token)
1482 return 0;
1483
1484 if (!wpa_s->current_bss)
1485 goto out;
1486
1487 /* If the measurement was aborted, don't report partial results */
1488 if (info->aborted)
1489 goto out;
1490
1491 wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
1492 MAC2STR(info->scan_start_tsf_bssid),
1493 MAC2STR(wpa_s->current_bss->bssid));
d7342014
JM
1494 if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1495 os_memcmp(info->scan_start_tsf_bssid, wpa_s->current_bss->bssid,
1496 ETH_ALEN) != 0) {
1497 wpa_printf(MSG_DEBUG,
1498 "RRM: Ignore scan results due to mismatching TSF BSSID");
76196ddb 1499 goto out;
d7342014 1500 }
76196ddb
AS
1501
1502 for (i = 0; i < scan_res->num; i++) {
1503 struct wpa_bss *bss =
1504 wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
1505
1506 if (!bss)
1507 continue;
1508
d7342014
JM
1509 if ((wpa_s->drv_rrm_flags &
1510 WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1511 os_memcmp(scan_res->res[i]->tsf_bssid,
1512 wpa_s->current_bss->bssid, ETH_ALEN) != 0) {
1513 wpa_printf(MSG_DEBUG,
1514 "RRM: Ignore scan result for " MACSTR
1515 " due to mismatching TSF BSSID" MACSTR,
1516 MAC2STR(scan_res->res[i]->bssid),
1517 MAC2STR(scan_res->res[i]->tsf_bssid));
76196ddb 1518 continue;
d7342014
JM
1519 }
1520
23eead4d
AS
1521 /*
1522 * Don't report results that were not received during the
1523 * current measurement.
1524 */
d7342014
JM
1525 if (!(wpa_s->drv_rrm_flags &
1526 WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
1527 struct os_reltime update_time, diff;
1528
1529 /* For now, allow 8 ms older results due to some
1530 * unknown issue with cfg80211 BSS table updates during
1531 * a scan with the current BSS.
1532 * TODO: Fix this more properly to avoid having to have
1533 * this type of hacks in place. */
1534 calculate_update_time(&scan_res->fetch_time,
1535 scan_res->res[i]->age,
1536 &update_time);
1537 os_reltime_sub(&wpa_s->beacon_rep_scan,
1538 &update_time, &diff);
1539 if (os_reltime_before(&update_time,
1540 &wpa_s->beacon_rep_scan) &&
1541 (diff.sec || diff.usec >= 8000)) {
1542 wpa_printf(MSG_DEBUG,
1543 "RRM: Ignore scan result for " MACSTR
1544 " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
1545 MAC2STR(scan_res->res[i]->bssid),
1546 scan_res->res[i]->age,
1547 (unsigned int) diff.sec,
1548 (unsigned int) diff.usec);
1549 continue;
1550 }
23eead4d
AS
1551 } else if (info->scan_start_tsf >
1552 scan_res->res[i]->parent_tsf) {
76196ddb 1553 continue;
23eead4d 1554 }
76196ddb
AS
1555
1556 if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
1557 scan_res->res[i]->parent_tsf) < 0)
1558 break;
1559 }
1560
1561 if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
1562 goto out;
1563
1564 wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
1565
1566 wpas_rrm_send_msr_report(wpa_s, buf);
1567 wpabuf_free(buf);
1568
1569out:
1570 wpas_clear_beacon_rep_data(wpa_s);
1571 return 1;
1572}
1573
1574
1575void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
1576{
1577 struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1578
1579 eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
1580 bitfield_free(data->eids);
1581 os_free(data->scan_params.freqs);
1582 os_memset(data, 0, sizeof(*data));
1583}