Coverage Report

Created: 2025-07-11 06:14

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