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