Coverage Report

Created: 2026-05-30 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hostap/wpa_supplicant/rrm.c
Line
Count
Source
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"
17
#include "scan.h"
18
#include "p2p_supplicant.h"
19
20
21
static void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
22
0
{
23
0
  struct rrm_data *rrm = data;
24
25
0
  if (!rrm->notify_neighbor_rep) {
26
0
    wpa_printf(MSG_ERROR,
27
0
         "RRM: Unexpected neighbor report timeout");
28
0
    return;
29
0
  }
30
31
0
  wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
32
0
  rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
33
34
0
  rrm->notify_neighbor_rep = NULL;
35
0
  rrm->neighbor_rep_cb_ctx = NULL;
36
0
}
37
38
39
/*
40
 * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
41
 * @wpa_s: Pointer to wpa_supplicant
42
 */
43
void wpas_rrm_reset(struct wpa_supplicant *wpa_s)
44
0
{
45
0
  wpa_s->rrm.rrm_used = 0;
46
47
0
  eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
48
0
           NULL);
49
0
  if (wpa_s->rrm.notify_neighbor_rep)
50
0
    wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
51
0
  wpa_s->rrm.next_neighbor_rep_token = 1;
52
0
  wpas_clear_beacon_rep_data(wpa_s);
53
0
}
54
55
56
/*
57
 * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
58
 * @wpa_s: Pointer to wpa_supplicant
59
 * @da: DA of the received frame
60
 * @sa: SA of the received frame
61
 * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
62
 * @report_len: Length of neighbor report buffer
63
 */
64
void wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
65
           const u8 *da, const u8 *sa,
66
           const u8 *report, size_t report_len)
67
0
{
68
0
  struct wpabuf *neighbor_rep;
69
70
0
  if (is_multicast_ether_addr(da)) {
71
0
    wpa_printf(MSG_DEBUG,
72
0
         "RRM: Ignore group-addressed Neighbor Report Response frame (A1="
73
0
         MACSTR " A2=" MACSTR ")",
74
0
         MAC2STR(da), MAC2STR(sa));
75
0
    return;
76
0
  }
77
78
0
  wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
79
0
  if (report_len < 1)
80
0
    return;
81
82
0
  if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
83
0
    wpa_printf(MSG_DEBUG,
84
0
         "RRM: Discarding neighbor report with token %d (expected %d)",
85
0
         report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
86
0
    return;
87
0
  }
88
89
0
  eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
90
0
           NULL);
91
92
0
  if (!wpa_s->rrm.notify_neighbor_rep) {
93
0
    wpa_msg(wpa_s, MSG_INFO, "RRM: Unexpected neighbor report");
94
0
    return;
95
0
  }
96
97
  /* skipping the first byte, which is only an id (dialog token) */
98
0
  neighbor_rep = wpabuf_alloc(report_len - 1);
99
0
  if (!neighbor_rep) {
100
0
    wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
101
0
    return;
102
0
  }
103
0
  wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
104
0
  wpa_dbg(wpa_s, MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
105
0
    report[0]);
106
0
  wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
107
0
               neighbor_rep);
108
0
  wpa_s->rrm.notify_neighbor_rep = NULL;
109
0
  wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
110
0
}
111
112
113
#if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
114
/* Workaround different, undefined for Windows, error codes used here */
115
#ifndef ENOTCONN
116
#define ENOTCONN -1
117
#endif
118
#ifndef EOPNOTSUPP
119
#define EOPNOTSUPP -1
120
#endif
121
#ifndef ECANCELED
122
#define ECANCELED -1
123
#endif
124
#endif
125
126
/* Measurement Request element + Location Subject + Maximum Age subelement */
127
0
#define MEASURE_REQUEST_LCI_LEN (3 + 1 + 4)
128
/* Measurement Request element + Location Civic Request */
129
0
#define MEASURE_REQUEST_CIVIC_LEN (3 + 5)
130
131
132
/**
133
 * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
134
 * @wpa_s: Pointer to wpa_supplicant
135
 * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
136
 *    is sent in the request.
137
 * @lci: if set, neighbor request will include LCI request
138
 * @civic: if set, neighbor request will include civic location request
139
 * @cb: Callback function to be called once the requested report arrives, or
140
 *  timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
141
 *  In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
142
 *  the requester's responsibility to free it.
143
 *  In the latter case NULL will be sent in 'neighbor_rep'.
144
 * @cb_ctx: Context value to send the callback function
145
 * Returns: 0 in case of success, negative error code otherwise
146
 *
147
 * In case there is a previous request which has not been answered yet, the
148
 * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
149
 * Request must contain a callback function.
150
 */
151
int wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
152
               const struct wpa_ssid_value *ssid,
153
               int lci, int civic,
154
               void (*cb)(void *ctx,
155
              struct wpabuf *neighbor_rep),
156
               void *cb_ctx)
157
0
{
158
0
  struct wpabuf *buf;
159
0
  const u8 *rrm_ie;
160
161
0
  if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
162
0
    wpa_dbg(wpa_s, MSG_DEBUG, "RRM: No connection, no RRM.");
163
0
    return -ENOTCONN;
164
0
  }
165
166
0
  if (!wpa_s->rrm.rrm_used) {
167
0
    wpa_dbg(wpa_s, MSG_DEBUG, "RRM: No RRM in current connection.");
168
0
    return -EOPNOTSUPP;
169
0
  }
170
171
0
  rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
172
0
        WLAN_EID_RRM_ENABLED_CAPABILITIES);
173
0
  if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
174
0
      rrm_ie[1] < 1 ||
175
0
      !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
176
0
    wpa_dbg(wpa_s, MSG_DEBUG,
177
0
      "RRM: No network support for Neighbor Report.");
178
0
    return -EOPNOTSUPP;
179
0
  }
180
181
  /* Refuse if there's a live request */
182
0
  if (wpa_s->rrm.notify_neighbor_rep) {
183
0
    wpa_dbg(wpa_s, MSG_DEBUG,
184
0
      "RRM: Currently handling previous Neighbor Report.");
185
0
    return -EBUSY;
186
0
  }
187
188
  /* 3 = action category + action code + dialog token */
189
0
  buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0) +
190
0
         (lci ? 2 + MEASURE_REQUEST_LCI_LEN : 0) +
191
0
         (civic ? 2 + MEASURE_REQUEST_CIVIC_LEN : 0));
192
0
  if (buf == NULL) {
193
0
    wpa_dbg(wpa_s, MSG_DEBUG,
194
0
      "RRM: Failed to allocate Neighbor Report Request");
195
0
    return -ENOMEM;
196
0
  }
197
198
0
  wpa_dbg(wpa_s, MSG_DEBUG,
199
0
    "RRM: Neighbor report request (for %s), token=%d",
200
0
    (ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
201
0
    wpa_s->rrm.next_neighbor_rep_token);
202
203
  /*
204
   * According to IEEE Std 802.11-2024, 11.10.10.2 (Requesting a neighbor
205
   * report) LCI and civic requests depend on FTM responder support.
206
   */
207
0
  if (lci || civic) {
208
0
    const u8 *ext_capab;
209
210
0
    ext_capab = wpa_bss_get_ie(wpa_s->current_bss,
211
0
             WLAN_EID_EXT_CAPAB);
212
0
    if (!ieee802_11_ext_capab(ext_capab,
213
0
            WLAN_EXT_CAPAB_FTM_RESPONDER)) {
214
0
      wpa_printf(MSG_DEBUG,
215
0
           "AP doesn't support FTM responder, can't request LCI and civic");
216
0
      lci = 0;
217
0
      civic = 0;
218
0
    }
219
0
  }
220
221
0
  wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
222
0
  wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
223
0
  wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
224
0
  if (ssid) {
225
0
    wpabuf_put_u8(buf, WLAN_EID_SSID);
226
0
    wpabuf_put_u8(buf, ssid->ssid_len);
227
0
    wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
228
0
  }
229
230
0
  if (lci && rrm_ie[1] >= 2 &&
231
0
      (rrm_ie[3] & WLAN_RRM_CAPS_LCI_MEASUREMENT)) {
232
    /* IEEE Std 802.11-2024, 9.4.2.19 (Measurement Request element)
233
     */
234
0
    wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
235
0
    wpabuf_put_u8(buf, MEASURE_REQUEST_LCI_LEN);
236
237
    /*
238
     * Measurement token; nonzero number that is unique among the
239
     * Measurement Request elements in a particular frame.
240
     */
241
0
    wpabuf_put_u8(buf, 1); /* Measurement Token */
242
243
    /*
244
     * Parallel, Enable, Request, and Report bits are 0, Duration is
245
     * reserved.
246
     */
247
0
    wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
248
0
    wpabuf_put_u8(buf, MEASURE_TYPE_LCI); /* Measurement Type */
249
250
    /* IEEE Std 802.11-2024, 9.4.2.19.10 (LCI request) */
251
    /* Location Subject */
252
0
    wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
253
254
    /* Optional Subelements */
255
    /*
256
     * IEEE Std 802.11-2024, Figure 9-265 (Maximum Age subelement
257
     * format)
258
     * The Maximum Age subelement is required, otherwise the AP can
259
     * send only data that was determined after receiving the
260
     * request. Setting it here to unlimited age.
261
     */
262
0
    wpabuf_put_u8(buf, LCI_REQ_SUBELEM_MAX_AGE);
263
0
    wpabuf_put_u8(buf, 2);
264
0
    wpabuf_put_le16(buf, 0xffff);
265
0
  } else if (lci) {
266
0
    wpa_printf(MSG_DEBUG, "RRM: LCI request isn't supported by AP");
267
0
  }
268
269
0
  if (civic && rrm_ie[1] >= 5 &&
270
0
      (rrm_ie[6] & WLAN_RRM_CAPS_CIVIC_LOCATION_MEASUREMENT)) {
271
    /* IEEE Std 802.11-2024, 9.4.2.19 (Measurement Request element)
272
     */
273
0
    wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
274
0
    wpabuf_put_u8(buf, MEASURE_REQUEST_CIVIC_LEN);
275
276
    /*
277
     * Measurement token; nonzero number that is unique among the
278
     * Measurement Request elements in a particular frame.
279
     */
280
0
    wpabuf_put_u8(buf, 2); /* Measurement Token */
281
282
    /*
283
     * Parallel, Enable, Request, and Report bits are 0, Duration is
284
     * reserved.
285
     */
286
0
    wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
287
    /* Measurement Type */
288
0
    wpabuf_put_u8(buf, MEASURE_TYPE_LOCATION_CIVIC);
289
290
    /* IEEE Std 802.11-2024, 9.4.2.19.14 (Location Civic request) */
291
    /* Location Subject */
292
0
    wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
293
0
    wpabuf_put_u8(buf, 0); /* Civic Location Type: IETF RFC 4776 */
294
    /* Location Service Interval Units: Seconds */
295
0
    wpabuf_put_u8(buf, 0);
296
    /* Location Service Interval: 0 - Only one report is requested
297
     */
298
0
    wpabuf_put_le16(buf, 0);
299
    /* No optional subelements */
300
0
  } else if (civic) {
301
0
    wpa_printf(MSG_DEBUG,
302
0
         "RRM: Civic request isn't supported by AP");
303
0
  }
304
305
0
  wpa_s->rrm.next_neighbor_rep_token++;
306
307
0
  if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
308
0
        wpa_s->own_addr, wpa_s->bssid,
309
0
        wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
310
0
    wpa_dbg(wpa_s, MSG_DEBUG,
311
0
      "RRM: Failed to send Neighbor Report Request");
312
0
    wpabuf_free(buf);
313
0
    return -ECANCELED;
314
0
  }
315
316
0
  wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
317
0
  wpa_s->rrm.notify_neighbor_rep = cb;
318
0
  eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
319
0
             wpas_rrm_neighbor_rep_timeout_handler,
320
0
             &wpa_s->rrm, NULL);
321
322
0
  wpabuf_free(buf);
323
0
  return 0;
324
0
}
325
326
327
static int wpas_rrm_report_elem(struct wpabuf **buf, u8 token, u8 mode, u8 type,
328
        const u8 *data, size_t data_len)
329
0
{
330
0
  if (wpabuf_resize(buf, 5 + data_len))
331
0
    return -1;
332
333
0
  wpabuf_put_u8(*buf, WLAN_EID_MEASURE_REPORT);
334
0
  wpabuf_put_u8(*buf, 3 + data_len);
335
0
  wpabuf_put_u8(*buf, token);
336
0
  wpabuf_put_u8(*buf, mode);
337
0
  wpabuf_put_u8(*buf, type);
338
339
0
  if (data_len)
340
0
    wpabuf_put_data(*buf, data, data_len);
341
342
0
  return 0;
343
0
}
344
345
346
static int
347
wpas_rrm_build_lci_report(struct wpa_supplicant *wpa_s,
348
        const struct rrm_measurement_request_element *req,
349
        struct wpabuf **buf)
350
0
{
351
0
  u8 subject;
352
0
  u16 max_age = 0;
353
0
  struct os_reltime t, diff;
354
0
  unsigned long diff_l;
355
0
  const u8 *subelem;
356
0
  const u8 *request = req->variable;
357
0
  size_t len = req->len - 3;
358
359
0
  if (len < 1)
360
0
    return -1;
361
362
0
  if (!wpa_s->lci)
363
0
    goto reject;
364
365
0
  subject = *request++;
366
0
  len--;
367
368
0
  wpa_printf(MSG_DEBUG, "Measurement request location subject=%u",
369
0
       subject);
370
371
0
  if (subject != LOCATION_SUBJECT_REMOTE) {
372
0
    wpa_printf(MSG_INFO,
373
0
         "Not building LCI report - bad location subject");
374
0
    return 0;
375
0
  }
376
377
  /* Subelements are formatted exactly like elements */
378
0
  wpa_hexdump(MSG_DEBUG, "LCI request subelements", request, len);
379
0
  subelem = get_ie(request, len, LCI_REQ_SUBELEM_MAX_AGE);
380
0
  if (subelem && subelem[1] == 2)
381
0
    max_age = WPA_GET_LE16(subelem + 2);
382
383
0
  if (os_get_reltime(&t))
384
0
    goto reject;
385
386
0
  os_reltime_sub(&t, &wpa_s->lci_time, &diff);
387
  /* LCI age is calculated in 10th of a second units. */
388
0
  diff_l = diff.sec * 10 + diff.usec / 100000;
389
390
0
  if (max_age != 0xffff && max_age < diff_l)
391
0
    goto reject;
392
393
0
  if (wpas_rrm_report_elem(buf, req->token,
394
0
         MEASUREMENT_REPORT_MODE_ACCEPT, req->type,
395
0
         wpabuf_head_u8(wpa_s->lci),
396
0
         wpabuf_len(wpa_s->lci)) < 0) {
397
0
    wpa_printf(MSG_DEBUG, "Failed to add LCI report element");
398
0
    return -1;
399
0
  }
400
401
0
  return 0;
402
403
0
reject:
404
0
  if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
405
0
      wpas_rrm_report_elem(buf, req->token,
406
0
         MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
407
0
         req->type, NULL, 0) < 0) {
408
0
    wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
409
0
    return -1;
410
0
  }
411
412
0
  return 0;
413
0
}
414
415
416
static void wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant *wpa_s,
417
            const u8 *data, size_t len)
418
0
{
419
0
  struct wpabuf *report = wpabuf_alloc(len + 3);
420
421
0
  if (!report)
422
0
    return;
423
424
0
  wpabuf_put_u8(report, WLAN_ACTION_RADIO_MEASUREMENT);
425
0
  wpabuf_put_u8(report, WLAN_RRM_RADIO_MEASUREMENT_REPORT);
426
0
  wpabuf_put_u8(report, wpa_s->rrm.token);
427
428
0
  wpabuf_put_data(report, data, len);
429
430
0
  if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
431
0
        wpa_s->own_addr, wpa_s->bssid,
432
0
        wpabuf_head(report), wpabuf_len(report), 0)) {
433
0
    wpa_printf(MSG_ERROR,
434
0
         "RRM: Radio measurement report failed: Sending Action frame failed");
435
0
  }
436
437
0
  wpabuf_free(report);
438
0
}
439
440
441
static int wpas_rrm_beacon_rep_update_last_frame(u8 *pos, size_t len)
442
0
{
443
0
  struct rrm_measurement_report_element *msr_rep;
444
0
  u8 *end = pos + len;
445
0
  u8 *msr_rep_end;
446
0
  struct rrm_measurement_beacon_report *rep = NULL;
447
0
  u8 *subelem;
448
449
  /* Find the last beacon report element */
450
0
  while (end - pos >= (int) sizeof(*msr_rep)) {
451
0
    msr_rep = (struct rrm_measurement_report_element *) pos;
452
0
    msr_rep_end = pos + msr_rep->len + 2;
453
454
0
    if (msr_rep->eid != WLAN_EID_MEASURE_REPORT ||
455
0
        msr_rep_end > end) {
456
      /* Should not happen. This indicates a bug. */
457
0
      wpa_printf(MSG_ERROR,
458
0
           "RRM: non-measurement report element in measurement report frame");
459
0
      return -1;
460
0
    }
461
462
0
    if (msr_rep->type == MEASURE_TYPE_BEACON)
463
0
      rep = (struct rrm_measurement_beacon_report *)
464
0
        msr_rep->variable;
465
466
0
    pos += pos[1] + 2;
467
0
  }
468
469
0
  if (!rep)
470
0
    return 0;
471
472
0
  subelem = rep->variable;
473
0
  while (subelem + 2 < msr_rep_end &&
474
0
         subelem[0] != WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION)
475
0
    subelem += 2 + subelem[1];
476
477
0
  if (subelem + 2 < msr_rep_end &&
478
0
      subelem[0] == WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION &&
479
0
      subelem[1] == 1 &&
480
0
      subelem + BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN <= end)
481
0
    subelem[2] = 1;
482
483
0
  return 0;
484
0
}
485
486
487
static void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
488
             struct wpabuf *buf)
489
0
{
490
0
  int len = wpabuf_len(buf);
491
0
  u8 *pos = wpabuf_mhead_u8(buf), *next = pos;
492
493
0
#define MPDU_REPORT_LEN (int) (IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN - 3)
494
495
0
  while (len) {
496
0
    int send_len = (len > MPDU_REPORT_LEN) ? next - pos : len;
497
498
0
    if (send_len == len)
499
0
      wpas_rrm_beacon_rep_update_last_frame(pos, len);
500
501
0
    if (send_len == len ||
502
0
        (send_len + next[1] + 2) > MPDU_REPORT_LEN) {
503
0
      wpas_rrm_send_msr_report_mpdu(wpa_s, pos, send_len);
504
0
      len -= send_len;
505
0
      pos = next;
506
0
    }
507
508
0
    if (len)
509
0
      next += next[1] + 2;
510
0
  }
511
0
#undef MPDU_REPORT_LEN
512
0
}
513
514
515
static int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
516
          int *freqs)
517
0
{
518
0
  size_t i;
519
520
0
  for (i = 0; i < num_primary_channels; i++) {
521
0
    u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
522
523
0
    freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
524
    /* ieee80211_chan_to_freq() is not really meant for this
525
     * conversion of 20 MHz primary channel numbers for wider VHT
526
     * channels, so handle those as special cases here for now. */
527
0
    if (freqs[i] < 0 &&
528
0
        (op_class == 128 || op_class == 129 || op_class == 130))
529
0
      freqs[i] = 5000 + 5 * primary_chan;
530
0
    if (freqs[i] < 0) {
531
0
      wpa_printf(MSG_DEBUG,
532
0
           "Beacon Report: Invalid channel %u",
533
0
           chan);
534
0
      return -1;
535
0
    }
536
0
  }
537
538
0
  return 0;
539
0
}
540
541
542
static int * wpas_add_channels(const struct oper_class_map *op,
543
             struct hostapd_hw_modes *mode,
544
             const u8 *channels, const u8 size)
545
0
{
546
0
  int *freqs, *next_freq;
547
0
  u8 num_primary_channels, i;
548
0
  u8 num_chans;
549
550
0
  num_chans = channels ? size :
551
0
    (op->max_chan - op->min_chan) / op->inc + 1;
552
553
0
  if (op->bw == BW80 || op->bw == BW80P80)
554
0
    num_primary_channels = 4;
555
0
  else if (op->bw == BW160)
556
0
    num_primary_channels = 8;
557
0
  else if (op->bw == BW320)
558
0
    num_primary_channels = 16;
559
0
  else
560
0
    num_primary_channels = 1;
561
562
  /* one extra place for the zero-terminator */
563
0
  freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
564
0
  if (!freqs) {
565
0
    wpa_printf(MSG_ERROR,
566
0
         "Beacon Report: Failed to allocate freqs array");
567
0
    return NULL;
568
0
  }
569
570
0
  next_freq = freqs;
571
0
  for  (i = 0; i < num_chans; i++) {
572
0
    u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
573
0
    enum chan_allowed res = verify_channel(mode, op->op_class, chan,
574
0
                   op->bw);
575
576
0
    if (res == NOT_ALLOWED)
577
0
      continue;
578
579
0
    if (wpas_add_channel(op->op_class, chan, num_primary_channels,
580
0
             next_freq) < 0) {
581
0
      os_free(freqs);
582
0
      return NULL;
583
0
    }
584
585
0
    next_freq += num_primary_channels;
586
0
  }
587
588
0
  if (!freqs[0]) {
589
0
    os_free(freqs);
590
0
    return NULL;
591
0
  }
592
593
0
  return freqs;
594
0
}
595
596
597
static int * wpas_op_class_freqs(const struct oper_class_map *op,
598
         struct hostapd_hw_modes *mode)
599
0
{
600
0
  u8 channels_80mhz_5ghz[] = { 42, 58, 106, 122, 138, 155, 171 };
601
0
  u8 channels_160mhz_5ghz[] = { 50, 114, 163 };
602
0
  u8 channels_80mhz_6ghz[] = { 7, 23, 39, 55, 71, 87, 103, 119, 135, 151,
603
0
             167, 183, 199, 215 };
604
0
  u8 channels_160mhz_6ghz[] = { 15, 47, 79, 111, 143, 175, 207 };
605
0
  u8 channels_320mhz_6ghz[] = { 31, 63, 95, 127, 159, 191 };
606
0
  const u8 *channels = NULL;
607
0
  size_t num_chan = 0;
608
0
  bool is_6ghz = is_6ghz_op_class(op->op_class);
609
610
  /*
611
   * When adding all channels in the operating class, 80 + 80 MHz
612
   * operating classes are like 80 MHz channels because we add all valid
613
   * channels anyway.
614
   */
615
0
  if (op->bw == BW80 || op->bw == BW80P80) {
616
0
    channels = is_6ghz ? channels_80mhz_6ghz : channels_80mhz_5ghz;
617
0
    num_chan = is_6ghz ? ARRAY_SIZE(channels_80mhz_6ghz) :
618
0
      ARRAY_SIZE(channels_80mhz_5ghz);
619
0
  } else if (op->bw == BW160) {
620
0
    channels = is_6ghz ? channels_160mhz_6ghz :
621
0
      channels_160mhz_5ghz;
622
0
    num_chan =  is_6ghz ? ARRAY_SIZE(channels_160mhz_6ghz) :
623
0
      ARRAY_SIZE(channels_160mhz_5ghz);
624
0
  } else if (op->bw == BW320) {
625
0
    channels = channels_320mhz_6ghz;
626
0
    num_chan = ARRAY_SIZE(channels_320mhz_6ghz);
627
0
  }
628
629
0
  return wpas_add_channels(op, mode, channels, num_chan);
630
0
}
631
632
633
static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s,
634
               const char *country, const u8 *subelems,
635
               size_t len)
636
0
{
637
0
  int *freqs = NULL, *new_freqs;
638
0
  const u8 *end = subelems + len;
639
640
0
  while (end - subelems > 2) {
641
0
    const struct oper_class_map *op;
642
0
    const u8 *ap_chan_elem, *pos;
643
0
    u8 left;
644
0
    struct hostapd_hw_modes *mode;
645
646
0
    ap_chan_elem = get_ie(subelems, end - subelems,
647
0
              WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
648
0
    if (!ap_chan_elem)
649
0
      break;
650
0
    pos = ap_chan_elem + 2;
651
0
    left = ap_chan_elem[1];
652
0
    if (left < 1)
653
0
      break;
654
0
    subelems = ap_chan_elem + 2 + left;
655
656
0
    op = get_oper_class(country, *pos);
657
0
    if (!op) {
658
0
      wpa_printf(MSG_DEBUG,
659
0
           "Beacon request: unknown operating class in AP Channel Report subelement %u",
660
0
           *pos);
661
0
      goto out;
662
0
    }
663
0
    pos++;
664
0
    left--;
665
666
0
    mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode,
667
0
        is_6ghz_op_class(op->op_class));
668
0
    if (!mode)
669
0
      continue;
670
671
    /*
672
     * For 80 + 80 MHz operating classes, this AP Channel Report
673
     * element should be followed by another element specifying
674
     * the second 80 MHz channel. For now just add this 80 MHz
675
     * channel, the second 80 MHz channel will be added when the
676
     * next element is parsed.
677
     * TODO: Verify that this AP Channel Report element is followed
678
     * by a corresponding AP Channel Report element as specified in
679
     * IEEE Std 802.11-2016, 11.11.9.1.
680
     */
681
0
    new_freqs = wpas_add_channels(op, mode, pos, left);
682
0
    if (new_freqs)
683
0
      int_array_concat(&freqs, new_freqs);
684
685
0
    os_free(new_freqs);
686
0
  }
687
688
0
  return freqs;
689
0
out:
690
0
  os_free(freqs);
691
0
  return NULL;
692
0
}
693
694
695
static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
696
               u8 op_class, u8 chan,
697
               const u8 *subelems, size_t len)
698
0
{
699
0
  int *freqs = NULL, *ext_freqs = NULL;
700
0
  struct hostapd_hw_modes *mode;
701
0
  const char *country = NULL;
702
0
  const struct oper_class_map *op;
703
0
  const u8 *elem;
704
705
0
  if (!wpa_s->current_bss)
706
0
    return NULL;
707
0
  elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
708
0
  if (elem && elem[1] >= 2)
709
0
    country = (const char *) (elem + 2);
710
711
0
  op = get_oper_class(country, op_class);
712
0
  if (!op) {
713
0
    wpa_printf(MSG_DEBUG,
714
0
         "Beacon request: invalid operating class %d",
715
0
         op_class);
716
0
    return NULL;
717
0
  }
718
719
0
  mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode,
720
0
      is_6ghz_op_class(op->op_class));
721
0
  if (!mode)
722
0
    return NULL;
723
724
0
  switch (chan) {
725
0
  case 0:
726
0
    freqs = wpas_op_class_freqs(op, mode);
727
0
    if (!freqs)
728
0
      return NULL;
729
0
    break;
730
0
  case 255:
731
    /* freqs will be added from AP channel subelements */
732
0
    break;
733
0
  default:
734
0
    freqs = wpas_add_channels(op, mode, &chan, 1);
735
0
    if (!freqs)
736
0
      return NULL;
737
0
    break;
738
0
  }
739
740
0
  ext_freqs = wpas_channel_report_freqs(wpa_s, country, subelems, len);
741
0
  if (ext_freqs) {
742
0
    int_array_concat(&freqs, ext_freqs);
743
0
    os_free(ext_freqs);
744
0
    int_array_sort_unique(freqs);
745
0
  }
746
747
0
  return freqs;
748
0
}
749
750
751
int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
752
       u8 *op_class, u8 *chan, u8 *phy_type)
753
0
{
754
0
  int sec_chan = 0, chanwidth = 0;
755
0
  struct ieee802_11_elems elems;
756
0
  struct ieee80211_ht_operation *ht_oper;
757
758
0
  if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
759
0
    return -1;
760
761
0
  chanwidth = get_operation_channel_width(&elems);
762
0
  if (chanwidth == CHAN_WIDTH_UNKNOWN) {
763
0
    wpa_printf(MSG_DEBUG, "Cannot determine channel width");
764
0
    return -1;
765
0
  }
766
767
0
  ht_oper = (struct ieee80211_ht_operation *) elems.ht_operation;
768
0
  if (ht_oper) {
769
0
    u8 sec_chan_offset = ht_oper->ht_param &
770
0
      HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
771
0
    if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
772
0
      sec_chan = 1;
773
0
    else if (sec_chan_offset ==
774
0
       HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
775
0
      sec_chan = -1;
776
0
  }
777
778
0
  if (ieee80211_chaninfo_to_channel(freq, chanwidth, sec_chan, op_class,
779
0
            chan) == NUM_HOSTAPD_MODES) {
780
0
    wpa_printf(MSG_DEBUG,
781
0
         "Cannot determine operating class and channel");
782
0
    return -1;
783
0
  }
784
785
0
  *phy_type = ieee80211_get_phy_type(freq, elems.ht_operation != NULL,
786
0
             elems.vht_operation != NULL,
787
0
             elems.he_operation != NULL);
788
0
  if (*phy_type == PHY_TYPE_UNSPECIFIED) {
789
0
    wpa_printf(MSG_DEBUG, "Cannot determine phy type");
790
0
    return -1;
791
0
  }
792
793
0
  return 0;
794
0
}
795
796
797
static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
798
            struct bitfield *ext_eids,
799
            enum beacon_report_detail detail,
800
            struct wpa_bss *bss, u8 *buf,
801
            size_t buf_len, const u8 **ies_buf,
802
            size_t *ie_len, int add_fixed)
803
0
{
804
0
  const u8 *ies = *ies_buf;
805
0
  size_t ies_len = *ie_len;
806
0
  u8 *pos = buf;
807
0
  int rem_len;
808
809
0
  rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
810
0
    sizeof(struct rrm_measurement_report_element) - 2 -
811
0
    REPORTED_FRAME_BODY_SUBELEM_LEN;
812
813
0
  if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
814
0
    wpa_printf(MSG_DEBUG,
815
0
         "Beacon Request: Invalid reporting detail: %d",
816
0
         detail);
817
0
    return -1;
818
0
  }
819
820
0
  if (detail == BEACON_REPORT_DETAIL_NONE)
821
0
    return 0;
822
823
  /*
824
   * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
825
   * beacon interval(2) + capabilities(2) = 14 bytes
826
   */
827
0
  if (add_fixed && buf_len < 14)
828
0
    return -1;
829
830
0
  *pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
831
  /* The length will be filled later */
832
0
  pos++;
833
834
0
  if (add_fixed) {
835
0
    WPA_PUT_LE64(pos, bss->tsf);
836
0
    pos += sizeof(bss->tsf);
837
0
    WPA_PUT_LE16(pos, bss->beacon_int);
838
0
    pos += 2;
839
0
    WPA_PUT_LE16(pos, bss->caps);
840
0
    pos += 2;
841
0
  }
842
843
0
  rem_len -= pos - buf;
844
845
  /*
846
   * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
847
   * body subelement causes the element to exceed the maximum element
848
   * size, the subelement is truncated so that the last IE is a complete
849
   * IE. So even when required to report all IEs, add elements one after
850
   * the other and stop once there is no more room in the measurement
851
   * element.
852
   */
853
0
  while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
854
0
    if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
855
0
        (eids && bitfield_is_set(eids, ies[0])) ||
856
0
        (ext_eids && ies[0] == WLAN_EID_EXTENSION && ies[1] &&
857
0
         bitfield_is_set(ext_eids, ies[2]))) {
858
0
      u8 elen = ies[1];
859
860
0
      if (2 + elen > buf + buf_len - pos ||
861
0
          2 + elen > rem_len)
862
0
        break;
863
864
0
      *pos++ = ies[0];
865
0
      *pos++ = elen;
866
0
      os_memcpy(pos, ies + 2, elen);
867
0
      pos += elen;
868
0
      rem_len -= 2 + elen;
869
0
    }
870
871
0
    ies_len -= 2 + ies[1];
872
0
    ies += 2 + ies[1];
873
0
  }
874
875
0
  *ie_len = ies_len;
876
0
  *ies_buf = ies;
877
878
  /* Now the length is known */
879
0
  buf[1] = pos - buf - 2;
880
0
  return pos - buf;
881
0
}
882
883
884
static int wpas_add_beacon_rep_elem(struct beacon_rep_data *data,
885
            struct wpa_bss *bss,
886
            struct wpabuf **wpa_buf,
887
            struct rrm_measurement_beacon_report *rep,
888
            const u8 **ie, size_t *ie_len, u8 idx)
889
0
{
890
0
  int ret;
891
0
  u8 *buf, *pos;
892
0
  u32 subelems_len = REPORTED_FRAME_BODY_SUBELEM_LEN +
893
0
    (data->last_indication ?
894
0
     BEACON_REPORT_LAST_INDICATION_SUBELEM_LEN : 0);
895
896
  /* Maximum element length: Beacon Report element + Reported Frame Body
897
   * subelement + all IEs of the reported Beacon frame + Reported Frame
898
   * Body Fragment ID subelement */
899
0
  buf = os_malloc(sizeof(*rep) + 14 + *ie_len + subelems_len);
900
0
  if (!buf)
901
0
    return -1;
902
903
0
  os_memcpy(buf, rep, sizeof(*rep));
904
905
0
  ret = wpas_beacon_rep_add_frame_body(data->eids, data->ext_eids,
906
0
               data->report_detail,
907
0
               bss, buf + sizeof(*rep),
908
0
               14 + *ie_len, ie, ie_len,
909
0
               idx == 0);
910
0
  if (ret < 0)
911
0
    goto out;
912
913
0
  pos = buf + ret + sizeof(*rep);
914
0
  pos[0] = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY_FRAGMENT_ID;
915
0
  pos[1] = 2;
916
917
  /*
918
   * Only one Beacon Report Measurement is supported at a time, so
919
   * the Beacon Report ID can always be set to 1.
920
   */
921
0
  pos[2] = 1;
922
923
  /* Fragment ID Number (bits 0..6) and More Frame Body Fragments (bit 7)
924
 */
925
0
  pos[3] = idx;
926
0
  if (data->report_detail != BEACON_REPORT_DETAIL_NONE && *ie_len)
927
0
    pos[3] |= REPORTED_FRAME_BODY_MORE_FRAGMENTS;
928
0
  else
929
0
    pos[3] &= ~REPORTED_FRAME_BODY_MORE_FRAGMENTS;
930
931
0
  pos += REPORTED_FRAME_BODY_SUBELEM_LEN;
932
933
0
  if (data->last_indication) {
934
0
    pos[0] = WLAN_BEACON_REPORT_SUBELEM_LAST_INDICATION;
935
0
    pos[1] = 1;
936
937
    /* This field will be updated later if this is the last frame */
938
0
    pos[2] = 0;
939
0
  }
940
941
0
  ret = wpas_rrm_report_elem(wpa_buf, data->token,
942
0
           MEASUREMENT_REPORT_MODE_ACCEPT,
943
0
           MEASURE_TYPE_BEACON, buf,
944
0
           ret + sizeof(*rep) + subelems_len);
945
0
out:
946
0
  os_free(buf);
947
0
  return ret;
948
0
}
949
950
951
static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
952
             struct wpabuf **wpa_buf, struct wpa_bss *bss,
953
             u64 start, u64 parent_tsf)
954
0
{
955
0
  struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
956
0
  const u8 *ies = wpa_bss_ie_ptr(bss);
957
0
  const u8 *pos = ies;
958
0
  size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
959
0
  struct rrm_measurement_beacon_report rep;
960
0
  u8 idx = 0;
961
962
0
  if (!ether_addr_equal(data->bssid, broadcast_ether_addr) &&
963
0
      !ether_addr_equal(data->bssid, bss->bssid))
964
0
    return 0;
965
966
0
  if (data->ssid_len &&
967
0
      (data->ssid_len != bss->ssid_len ||
968
0
       os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
969
0
    return 0;
970
971
0
  if (wpas_get_op_chan_phy(bss->freq, ies, ies_len, &rep.op_class,
972
0
         &rep.channel, &rep.report_info) < 0)
973
0
    return 0;
974
975
0
  rep.start_time = host_to_le64(start);
976
0
  rep.duration = host_to_le16(data->scan_params.duration);
977
0
  rep.rcpi = rssi_to_rcpi(bss->level);
978
0
  rep.rsni = 255; /* 255 indicates that RSNI is not available */
979
0
  os_memcpy(rep.bssid, bss->bssid, ETH_ALEN);
980
0
  rep.antenna_id = 0; /* unknown */
981
0
  rep.parent_tsf = host_to_le32(parent_tsf);
982
983
0
  do {
984
0
    int ret;
985
986
0
    ret = wpas_add_beacon_rep_elem(data, bss, wpa_buf, &rep,
987
0
                 &pos, &ies_len, idx++);
988
0
    if (ret)
989
0
      return ret;
990
0
  } while (data->report_detail != BEACON_REPORT_DETAIL_NONE &&
991
0
     ies_len >= 2);
992
993
0
  return 0;
994
0
}
995
996
997
static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
998
              struct wpabuf **buf)
999
0
{
1000
0
  return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
1001
0
            MEASUREMENT_REPORT_MODE_ACCEPT,
1002
0
            MEASURE_TYPE_BEACON, NULL, 0);
1003
0
}
1004
1005
1006
static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
1007
          struct wpabuf **buf)
1008
0
{
1009
0
  size_t i;
1010
1011
0
  for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1012
0
    if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
1013
0
          0, 0) < 0)
1014
0
      break;
1015
0
  }
1016
1017
0
  if (!(*buf))
1018
0
    wpas_beacon_rep_no_results(wpa_s, buf);
1019
1020
0
  wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
1021
0
}
1022
1023
1024
void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
1025
0
{
1026
0
  if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr)) {
1027
0
    struct wpabuf *buf = NULL;
1028
1029
0
    if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
1030
0
           MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
1031
0
           MEASURE_TYPE_BEACON, NULL, 0)) {
1032
0
      wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
1033
0
      wpabuf_free(buf);
1034
0
      return;
1035
0
    }
1036
1037
0
    wpas_rrm_send_msr_report(wpa_s, buf);
1038
0
    wpabuf_free(buf);
1039
0
  }
1040
1041
0
  wpas_clear_beacon_rep_data(wpa_s);
1042
0
}
1043
1044
1045
static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1046
0
{
1047
0
  struct wpa_supplicant *wpa_s = eloop_ctx;
1048
0
  struct wpa_driver_scan_params *params =
1049
0
    &wpa_s->beacon_rep_data.scan_params;
1050
0
  u16 prev_duration = params->duration;
1051
1052
0
  if (!wpa_s->current_bss)
1053
0
    return;
1054
1055
0
  if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
1056
0
      params->duration) {
1057
0
    wpa_printf(MSG_DEBUG,
1058
0
         "RRM: Cannot set scan duration due to missing driver support");
1059
0
    params->duration = 0;
1060
0
  }
1061
0
  os_get_reltime(&wpa_s->beacon_rep_scan);
1062
0
  if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
1063
0
      wpa_supplicant_trigger_scan(wpa_s, params, true, false))
1064
0
    wpas_rrm_refuse_request(wpa_s);
1065
0
  params->duration = prev_duration;
1066
0
}
1067
1068
1069
static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
1070
               struct beacon_rep_data *data,
1071
               u8 sid, u8 slen, const u8 *subelem)
1072
0
{
1073
0
  struct bitfield *eids;
1074
0
  u8 report_info, i;
1075
1076
0
  switch (sid) {
1077
0
  case WLAN_BEACON_REQUEST_SUBELEM_SSID:
1078
0
    if (!slen) {
1079
0
      wpa_printf(MSG_DEBUG,
1080
0
           "SSID subelement with zero length - wildcard SSID");
1081
0
      break;
1082
0
    }
1083
1084
0
    if (slen > SSID_MAX_LEN) {
1085
0
      wpa_printf(MSG_DEBUG,
1086
0
           "Invalid SSID subelement length: %u", slen);
1087
0
      return -1;
1088
0
    }
1089
1090
0
    data->ssid_len = slen;
1091
0
    os_memcpy(data->ssid, subelem, data->ssid_len);
1092
0
    break;
1093
0
  case WLAN_BEACON_REQUEST_SUBELEM_INFO:
1094
0
    if (slen != 2) {
1095
0
      wpa_printf(MSG_DEBUG,
1096
0
           "Invalid reporting information subelement length: %u",
1097
0
           slen);
1098
0
      return -1;
1099
0
    }
1100
1101
0
    report_info = subelem[0];
1102
0
    if (report_info != 0) {
1103
0
      wpa_printf(MSG_DEBUG,
1104
0
           "reporting information=%u is not supported",
1105
0
           report_info);
1106
0
      return 0;
1107
0
    }
1108
0
    break;
1109
0
  case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
1110
0
    if (slen != 1) {
1111
0
      wpa_printf(MSG_DEBUG,
1112
0
           "Invalid reporting detail subelement length: %u",
1113
0
           slen);
1114
0
      return -1;
1115
0
    }
1116
1117
0
    data->report_detail = subelem[0];
1118
0
    if (data->report_detail >
1119
0
        BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
1120
0
      wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
1121
0
           subelem[0]);
1122
0
      return -1;
1123
0
    }
1124
1125
0
    break;
1126
0
  case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
1127
0
  case WLAN_BEACON_REQUEST_SUBELEM_EXT_REQUEST:
1128
0
    if (data->report_detail !=
1129
0
        BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
1130
0
      wpa_printf(MSG_DEBUG,
1131
0
           "Beacon request: request subelement is present but report detail is %u",
1132
0
           data->report_detail);
1133
0
      return -1;
1134
0
    }
1135
1136
0
    if (!slen) {
1137
0
      wpa_printf(MSG_DEBUG,
1138
0
           "Invalid request subelement length: %u",
1139
0
           slen);
1140
0
      return -1;
1141
0
    }
1142
1143
0
    if (sid == WLAN_BEACON_REQUEST_SUBELEM_EXT_REQUEST) {
1144
0
      if (slen < 2) {
1145
0
        wpa_printf(MSG_DEBUG,
1146
0
             "Invalid extended request");
1147
0
        return -1;
1148
0
      }
1149
0
      if (subelem[0] != WLAN_EID_EXTENSION) {
1150
0
        wpa_printf(MSG_DEBUG,
1151
0
             "Skip unknown Requested Element ID %u in Extended Request subelement",
1152
0
             subelem[0]);
1153
0
        break;
1154
0
      }
1155
1156
      /* Skip the Requested Element ID field */
1157
0
      subelem++;
1158
0
      slen--;
1159
0
    }
1160
1161
0
    if ((sid == WLAN_BEACON_REQUEST_SUBELEM_REQUEST &&
1162
0
         data->eids) ||
1163
0
        (sid == WLAN_BEACON_REQUEST_SUBELEM_EXT_REQUEST &&
1164
0
        data->ext_eids)) {
1165
0
      wpa_printf(MSG_DEBUG,
1166
0
           "Beacon Request: Request sub elements appear more than once");
1167
0
      return -1;
1168
0
    }
1169
1170
0
    eids = bitfield_alloc(255);
1171
0
    if (!eids) {
1172
0
      wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
1173
0
      return -1;
1174
0
    }
1175
1176
0
    if (sid == WLAN_BEACON_REQUEST_SUBELEM_REQUEST)
1177
0
      data->eids = eids;
1178
0
    else
1179
0
      data->ext_eids = eids;
1180
1181
0
    for (i = 0; i < slen; i++)
1182
0
      bitfield_set(eids, subelem[i]);
1183
0
    break;
1184
0
  case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
1185
    /* Skip - it will be processed when freqs are added */
1186
0
    break;
1187
0
  case WLAN_BEACON_REQUEST_SUBELEM_LAST_INDICATION:
1188
0
    if (slen != 1) {
1189
0
      wpa_printf(MSG_DEBUG,
1190
0
           "Beacon request: Invalid last indication request subelement length: %u",
1191
0
           slen);
1192
0
      return -1;
1193
0
    }
1194
1195
0
    data->last_indication = subelem[0];
1196
0
    break;
1197
0
  default:
1198
0
    wpa_printf(MSG_DEBUG,
1199
0
         "Beacon request: Unknown subelement id %u", sid);
1200
0
    break;
1201
0
  }
1202
1203
0
  return 1;
1204
0
}
1205
1206
1207
/**
1208
 * Returns 0 if the next element can be processed, 1 if some operation was
1209
 * triggered, and -1 if processing failed (i.e., the element is in invalid
1210
 * format or an internal error occurred).
1211
 */
1212
static int
1213
wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
1214
        u8 elem_token, int duration_mandatory,
1215
        const struct rrm_measurement_beacon_request *req,
1216
        size_t len, struct wpabuf **buf)
1217
0
{
1218
0
  struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1219
0
  struct wpa_driver_scan_params *params = &data->scan_params;
1220
0
  const u8 *subelems;
1221
0
  size_t elems_len;
1222
0
  u16 rand_interval;
1223
0
  u32 interval_usec;
1224
0
  u32 _rand;
1225
0
  int ret = 0, res;
1226
0
  u8 reject_mode;
1227
1228
0
  if (len < sizeof(*req))
1229
0
    return -1;
1230
1231
0
  if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
1232
0
      req->mode != BEACON_REPORT_MODE_ACTIVE &&
1233
0
      req->mode != BEACON_REPORT_MODE_TABLE)
1234
0
    return 0;
1235
1236
0
  subelems = req->variable;
1237
0
  elems_len = len - sizeof(*req);
1238
0
  rand_interval = le_to_host16(req->rand_interval);
1239
1240
0
  os_free(params->freqs);
1241
0
  os_memset(params, 0, sizeof(*params));
1242
1243
0
  data->token = elem_token;
1244
1245
  /* default reporting detail is all fixed length fields and all
1246
   * elements */
1247
0
  data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
1248
0
  os_memcpy(data->bssid, req->bssid, ETH_ALEN);
1249
1250
0
  while (elems_len >= 2) {
1251
0
    if (subelems[1] > elems_len - 2) {
1252
0
      wpa_printf(MSG_DEBUG,
1253
0
           "Beacon Request: Truncated subelement");
1254
0
      ret = -1;
1255
0
      goto out;
1256
0
    }
1257
1258
0
    res = wpas_rm_handle_beacon_req_subelem(
1259
0
      wpa_s, data, subelems[0], subelems[1], &subelems[2]);
1260
0
    if (res < 0) {
1261
0
      ret = res;
1262
0
      goto out;
1263
0
    } else if (!res) {
1264
0
      reject_mode = MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE;
1265
0
      goto out_reject;
1266
0
    }
1267
1268
0
    elems_len -= 2 + subelems[1];
1269
0
    subelems += 2 + subelems[1];
1270
0
  }
1271
1272
0
  if (req->mode == BEACON_REPORT_MODE_TABLE) {
1273
0
    wpas_beacon_rep_table(wpa_s, buf);
1274
0
    goto out;
1275
0
  }
1276
1277
0
  params->freqs = wpas_beacon_request_freqs(wpa_s, req->oper_class,
1278
0
              req->channel, req->variable,
1279
0
              len - sizeof(*req));
1280
0
  if (!params->freqs) {
1281
0
    wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
1282
0
    reject_mode = MEASUREMENT_REPORT_MODE_REJECT_REFUSED;
1283
0
    goto out_reject;
1284
0
  }
1285
1286
0
  params->duration = le_to_host16(req->duration);
1287
0
  params->duration_mandatory = duration_mandatory;
1288
0
  if (!params->duration) {
1289
0
    wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
1290
0
    ret = -1;
1291
0
    goto out;
1292
0
  }
1293
1294
0
  params->only_new_results = 1;
1295
1296
0
  if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
1297
0
    params->ssids[params->num_ssids].ssid = data->ssid;
1298
0
    params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
1299
0
  }
1300
1301
0
  if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
1302
0
    _rand = os_random();
1303
0
  interval_usec = (_rand % (rand_interval + 1)) * 1024;
1304
0
  eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
1305
0
             NULL);
1306
0
  return 1;
1307
0
out_reject:
1308
0
  if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
1309
0
      wpas_rrm_report_elem(buf, elem_token, reject_mode,
1310
0
         MEASURE_TYPE_BEACON, NULL, 0) < 0) {
1311
0
    wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1312
0
    ret = -1;
1313
0
  }
1314
0
out:
1315
0
  wpas_clear_beacon_rep_data(wpa_s);
1316
0
  return ret;
1317
0
}
1318
1319
1320
static int
1321
wpas_rrm_handle_msr_req_element(
1322
  struct wpa_supplicant *wpa_s,
1323
  const struct rrm_measurement_request_element *req,
1324
  struct wpabuf **buf)
1325
0
{
1326
0
  int duration_mandatory;
1327
1328
0
  wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
1329
0
       req->type, req->token);
1330
1331
0
  if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
1332
    /* Enable bit is not supported for now */
1333
0
    wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
1334
0
    return 0;
1335
0
  }
1336
1337
0
  if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
1338
0
      req->type > MEASURE_TYPE_RPI_HIST) {
1339
    /* Parallel measurements are not supported for now */
1340
0
    wpa_printf(MSG_DEBUG,
1341
0
         "RRM: Parallel measurements are not supported, reject");
1342
0
    goto reject;
1343
0
  }
1344
1345
0
  duration_mandatory =
1346
0
    !!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
1347
1348
0
  switch (req->type) {
1349
0
  case MEASURE_TYPE_LCI:
1350
0
    return wpas_rrm_build_lci_report(wpa_s, req, buf);
1351
0
  case MEASURE_TYPE_BEACON:
1352
0
    if (duration_mandatory &&
1353
0
        !(wpa_s->drv_rrm_flags &
1354
0
          WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
1355
0
      wpa_printf(MSG_DEBUG,
1356
0
           "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
1357
0
      goto reject;
1358
0
    }
1359
0
    return wpas_rm_handle_beacon_req(wpa_s, req->token,
1360
0
             duration_mandatory,
1361
0
             (const void *) req->variable,
1362
0
             req->len - 3, buf);
1363
0
  default:
1364
0
    wpa_printf(MSG_INFO,
1365
0
         "RRM: Unsupported radio measurement type %u",
1366
0
         req->type);
1367
0
    break;
1368
0
  }
1369
1370
0
reject:
1371
0
  if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
1372
0
      wpas_rrm_report_elem(buf, req->token,
1373
0
         MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
1374
0
         req->type, NULL, 0) < 0) {
1375
0
    wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1376
0
    return -1;
1377
0
  }
1378
1379
0
  return 0;
1380
0
}
1381
1382
1383
static struct wpabuf *
1384
wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
1385
             size_t len)
1386
0
{
1387
0
  struct wpabuf *buf = NULL;
1388
1389
0
  while (len) {
1390
0
    const struct rrm_measurement_request_element *req;
1391
0
    int res;
1392
1393
0
    if (len < 2) {
1394
0
      wpa_printf(MSG_DEBUG, "RRM: Truncated element");
1395
0
      goto out;
1396
0
    }
1397
1398
0
    req = (const struct rrm_measurement_request_element *) pos;
1399
0
    if (req->eid != WLAN_EID_MEASURE_REQUEST) {
1400
0
      wpa_printf(MSG_DEBUG,
1401
0
           "RRM: Expected Measurement Request element, but EID is %u",
1402
0
           req->eid);
1403
0
      goto out;
1404
0
    }
1405
1406
0
    if (req->len < 3) {
1407
0
      wpa_printf(MSG_DEBUG, "RRM: Element length too short");
1408
0
      goto out;
1409
0
    }
1410
1411
0
    if (req->len > len - 2) {
1412
0
      wpa_printf(MSG_DEBUG, "RRM: Element length too long");
1413
0
      goto out;
1414
0
    }
1415
1416
0
    res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
1417
0
    if (res < 0)
1418
0
      goto out;
1419
1420
0
    pos += req->len + 2;
1421
0
    len -= req->len + 2;
1422
0
  }
1423
1424
0
  return buf;
1425
1426
0
out:
1427
0
  wpabuf_free(buf);
1428
0
  return NULL;
1429
0
}
1430
1431
1432
void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
1433
                 const u8 *src, const u8 *dst,
1434
                 const u8 *frame, size_t len)
1435
0
{
1436
0
  struct wpabuf *report;
1437
1438
0
  if (wpa_s->wpa_state != WPA_COMPLETED) {
1439
0
    wpa_printf(MSG_INFO,
1440
0
         "RRM: Ignoring radio measurement request: Not associated");
1441
0
    return;
1442
0
  }
1443
1444
0
  if (!wpa_s->rrm.rrm_used) {
1445
0
    wpa_printf(MSG_INFO,
1446
0
         "RRM: Ignoring radio measurement request: Not RRM network");
1447
0
    return;
1448
0
  }
1449
1450
0
  if (len < 3) {
1451
0
    wpa_printf(MSG_INFO,
1452
0
         "RRM: Ignoring too short radio measurement request");
1453
0
    return;
1454
0
  }
1455
1456
0
  wpa_s->rrm.token = *frame;
1457
0
  os_memcpy(wpa_s->rrm.dst_addr, dst, ETH_ALEN);
1458
1459
  /* Number of repetitions is not supported */
1460
1461
0
  report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
1462
0
  if (!report)
1463
0
    return;
1464
1465
0
  wpas_rrm_send_msr_report(wpa_s, report);
1466
0
  wpabuf_free(report);
1467
0
}
1468
1469
1470
void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
1471
                const u8 *src,
1472
                const u8 *frame, size_t len,
1473
                int rssi)
1474
0
{
1475
0
  struct wpabuf *buf;
1476
0
  const struct rrm_link_measurement_request *req;
1477
0
  struct rrm_link_measurement_report report;
1478
1479
0
  if (wpa_s->wpa_state != WPA_COMPLETED) {
1480
0
    wpa_printf(MSG_INFO,
1481
0
         "RRM: Ignoring link measurement request. Not associated");
1482
0
    return;
1483
0
  }
1484
1485
0
  if (!wpa_s->rrm.rrm_used) {
1486
0
    wpa_printf(MSG_INFO,
1487
0
         "RRM: Ignoring link measurement request. Not RRM network");
1488
0
    return;
1489
0
  }
1490
1491
0
  if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
1492
0
    wpa_printf(MSG_INFO,
1493
0
         "RRM: Measurement report failed. TX power insertion not supported");
1494
0
    return;
1495
0
  }
1496
1497
0
  req = (const struct rrm_link_measurement_request *) frame;
1498
0
  if (len < sizeof(*req)) {
1499
0
    wpa_printf(MSG_INFO,
1500
0
         "RRM: Link measurement report failed. Request too short");
1501
0
    return;
1502
0
  }
1503
1504
0
  os_memset(&report, 0, sizeof(report));
1505
0
  report.dialog_token = req->dialog_token;
1506
0
  report.tpc.eid = WLAN_EID_TPC_REPORT;
1507
0
  report.tpc.len = 2;
1508
  /* Note: The driver is expected to update report.tpc.tx_power and
1509
   * report.tpc.link_margin subfields when sending out this frame.
1510
   * Similarly, the driver would need to update report.rx_ant_id and
1511
   * report.tx_ant_id subfields. */
1512
0
  report.rsni = 255; /* 255 indicates that RSNI is not available */
1513
0
  report.rcpi = rssi_to_rcpi(rssi);
1514
1515
  /* action_category + action_code */
1516
0
  buf = wpabuf_alloc(2 + sizeof(report));
1517
0
  if (buf == NULL) {
1518
0
    wpa_printf(MSG_ERROR,
1519
0
         "RRM: Link measurement report failed. Buffer allocation failed");
1520
0
    return;
1521
0
  }
1522
1523
0
  wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
1524
0
  wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
1525
0
  wpabuf_put_data(buf, &report, sizeof(report));
1526
0
  wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
1527
1528
0
  if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
1529
0
        wpa_s->own_addr, wpa_s->bssid,
1530
0
        wpabuf_head(buf), wpabuf_len(buf), 0)) {
1531
0
    wpa_printf(MSG_ERROR,
1532
0
         "RRM: Link measurement report failed. Send action failed");
1533
0
  }
1534
0
  wpabuf_free(buf);
1535
0
}
1536
1537
1538
static bool wpas_beacon_rep_scan_match(struct wpa_supplicant *wpa_s,
1539
               const u8 *bssid)
1540
0
{
1541
0
  u8 i;
1542
1543
0
  if (!wpa_s->valid_links)
1544
0
    return ether_addr_equal(wpa_s->current_bss->bssid, bssid);
1545
1546
0
  for_each_link(wpa_s->valid_links, i) {
1547
0
    if (ether_addr_equal(wpa_s->links[i].bssid, bssid))
1548
0
      return true;
1549
0
  }
1550
1551
0
  wpa_printf(MSG_DEBUG, "RRM: MLD: no match for TSF BSSID=" MACSTR,
1552
0
       MAC2STR(bssid));
1553
1554
0
  return false;
1555
0
}
1556
1557
1558
int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
1559
         struct wpa_scan_results *scan_res,
1560
         struct scan_info *info)
1561
0
{
1562
0
  size_t i = 0;
1563
0
  struct wpabuf *buf = NULL;
1564
1565
0
  if (!wpa_s->beacon_rep_data.token)
1566
0
    return 0;
1567
1568
0
  if (!wpa_s->current_bss)
1569
0
    goto out;
1570
1571
  /* If the measurement was aborted, don't report partial results */
1572
0
  if (info->aborted)
1573
0
    goto out_refuse;
1574
1575
0
  wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
1576
0
       MAC2STR(info->scan_start_tsf_bssid),
1577
0
       MAC2STR(wpa_s->current_bss->bssid));
1578
0
  if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1579
0
      !wpas_beacon_rep_scan_match(wpa_s, info->scan_start_tsf_bssid)) {
1580
0
    wpa_printf(MSG_DEBUG,
1581
0
         "RRM: Ignore scan results due to mismatching TSF BSSID");
1582
0
    goto out_refuse;
1583
0
  }
1584
1585
0
  for (i = 0; i < scan_res->num; i++) {
1586
0
    struct wpa_bss *bss =
1587
0
      wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
1588
1589
0
    if (!bss)
1590
0
      continue;
1591
1592
0
    if ((wpa_s->drv_rrm_flags &
1593
0
         WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1594
0
        !wpas_beacon_rep_scan_match(wpa_s,
1595
0
            scan_res->res[i]->tsf_bssid)) {
1596
0
      wpa_printf(MSG_DEBUG,
1597
0
           "RRM: Ignore scan result for " MACSTR
1598
0
           " due to mismatching TSF BSSID" MACSTR,
1599
0
           MAC2STR(scan_res->res[i]->bssid),
1600
0
           MAC2STR(scan_res->res[i]->tsf_bssid));
1601
0
      continue;
1602
0
    }
1603
1604
    /*
1605
     * Don't report results that were not received during the
1606
     * current measurement.
1607
     */
1608
0
    if (!(wpa_s->drv_rrm_flags &
1609
0
          WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
1610
0
      struct os_reltime update_time, diff;
1611
1612
      /* For now, allow 8 ms older results due to some
1613
       * unknown issue with cfg80211 BSS table updates during
1614
       * a scan with the current BSS.
1615
       * TODO: Fix this more properly to avoid having to have
1616
       * this type of hacks in place. */
1617
0
      calculate_update_time(&scan_res->fetch_time,
1618
0
                scan_res->res[i]->age,
1619
0
                &update_time);
1620
0
      os_reltime_sub(&wpa_s->beacon_rep_scan,
1621
0
               &update_time, &diff);
1622
0
      if (os_reltime_before(&update_time,
1623
0
                &wpa_s->beacon_rep_scan) &&
1624
0
          (diff.sec || diff.usec >= 8000)) {
1625
0
        wpa_printf(MSG_DEBUG,
1626
0
             "RRM: Ignore scan result for " MACSTR
1627
0
             " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
1628
0
             MAC2STR(scan_res->res[i]->bssid),
1629
0
             scan_res->res[i]->age,
1630
0
             (unsigned int) diff.sec,
1631
0
             (unsigned int) diff.usec);
1632
0
        continue;
1633
0
      }
1634
0
    } else if (info->scan_start_tsf >
1635
0
         scan_res->res[i]->parent_tsf) {
1636
0
      continue;
1637
0
    }
1638
1639
0
    if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
1640
0
          scan_res->res[i]->parent_tsf) < 0)
1641
0
      break;
1642
0
  }
1643
1644
0
  if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
1645
0
    goto out;
1646
1647
0
  wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
1648
1649
0
  wpas_rrm_send_msr_report(wpa_s, buf);
1650
0
  wpabuf_free(buf);
1651
1652
0
  goto out;
1653
1654
0
out_refuse:
1655
0
  wpas_rrm_refuse_request(wpa_s);
1656
0
out:
1657
0
  wpas_clear_beacon_rep_data(wpa_s);
1658
0
  return 1;
1659
0
}
1660
1661
1662
void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
1663
0
{
1664
0
  struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1665
1666
0
  eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
1667
0
  bitfield_free(data->eids);
1668
0
  bitfield_free(data->ext_eids);
1669
0
  os_free(data->scan_params.freqs);
1670
0
  os_memset(data, 0, sizeof(*data));
1671
0
}