Coverage Report

Created: 2023-03-26 06:22

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