Coverage Report

Created: 2025-08-26 06:04

/src/hostap/wpa_supplicant/scan.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * WPA Supplicant - Scanning
3
 * Copyright (c) 2003-2019, 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 "utils/includes.h"
10
11
#include "utils/common.h"
12
#include "utils/eloop.h"
13
#include "common/ieee802_11_defs.h"
14
#include "common/wpa_ctrl.h"
15
#include "config.h"
16
#include "wpa_supplicant_i.h"
17
#include "driver_i.h"
18
#include "wps_supplicant.h"
19
#include "p2p_supplicant.h"
20
#include "p2p/p2p.h"
21
#include "hs20_supplicant.h"
22
#include "notify.h"
23
#include "bss.h"
24
#include "scan.h"
25
#include "mesh.h"
26
27
static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s);
28
29
30
static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
31
0
{
32
0
  struct wpa_ssid *ssid;
33
0
  union wpa_event_data data;
34
35
0
  ssid = wpa_supplicant_get_ssid(wpa_s);
36
0
  if (ssid == NULL)
37
0
    return;
38
39
0
  if (wpa_s->current_ssid == NULL) {
40
0
    wpa_s->current_ssid = ssid;
41
0
    wpas_notify_network_changed(wpa_s);
42
0
  }
43
0
  wpa_supplicant_initiate_eapol(wpa_s);
44
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
45
0
    "network - generating associated event");
46
0
  os_memset(&data, 0, sizeof(data));
47
0
  wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
48
0
}
49
50
51
#ifdef CONFIG_WPS
52
static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
53
         enum wps_request_type *req_type)
54
{
55
  struct wpa_ssid *ssid;
56
  int wps = 0;
57
58
  for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
59
    if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
60
      continue;
61
62
    wps = 1;
63
    *req_type = wpas_wps_get_req_type(ssid);
64
    if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
65
      return 2;
66
  }
67
68
#ifdef CONFIG_P2P
69
  if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
70
      !wpa_s->conf->p2p_disabled) {
71
    wpa_s->wps->dev.p2p = 1;
72
    if (!wps) {
73
      wps = 1;
74
      *req_type = WPS_REQ_ENROLLEE_INFO;
75
    }
76
  }
77
#endif /* CONFIG_P2P */
78
79
  return wps;
80
}
81
#endif /* CONFIG_WPS */
82
83
84
static int wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params *params,
85
            const u8 *mac_addr)
86
0
{
87
0
  u8 *tmp;
88
89
0
  if (params->mac_addr) {
90
0
    params->mac_addr_mask = NULL;
91
0
    os_free(params->mac_addr);
92
0
    params->mac_addr = NULL;
93
0
  }
94
95
0
  params->mac_addr_rand = 1;
96
97
0
  if (!mac_addr)
98
0
    return 0;
99
100
0
  tmp = os_malloc(2 * ETH_ALEN);
101
0
  if (!tmp)
102
0
    return -1;
103
104
0
  os_memcpy(tmp, mac_addr, 2 * ETH_ALEN);
105
0
  params->mac_addr = tmp;
106
0
  params->mac_addr_mask = tmp + ETH_ALEN;
107
0
  return 0;
108
0
}
109
110
111
/**
112
 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
113
 * @wpa_s: Pointer to wpa_supplicant data
114
 * Returns: 0 if no networks are enabled, >0 if networks are enabled
115
 *
116
 * This function is used to figure out whether any networks (or Interworking
117
 * with enabled credentials and auto_interworking) are present in the current
118
 * configuration.
119
 */
120
int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
121
0
{
122
0
  struct wpa_ssid *ssid = wpa_s->conf->ssid;
123
0
  int count = 0, disabled = 0;
124
125
0
  if (wpa_s->p2p_mgmt)
126
0
    return 0; /* no normal network profiles on p2p_mgmt interface */
127
128
0
  while (ssid) {
129
0
    if (!wpas_network_disabled(wpa_s, ssid))
130
0
      count++;
131
0
    else
132
0
      disabled++;
133
0
    ssid = ssid->next;
134
0
  }
135
0
  if (wpa_s->conf->cred && wpa_s->conf->interworking &&
136
0
      wpa_s->conf->auto_interworking)
137
0
    count++;
138
0
  if (count == 0 && disabled > 0) {
139
0
    wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
140
0
      "networks)", disabled);
141
0
  }
142
0
  return count;
143
0
}
144
145
146
static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
147
             struct wpa_ssid *ssid)
148
0
{
149
0
  int min_temp_disabled = 0;
150
151
0
  while (ssid) {
152
0
    if (!wpas_network_disabled(wpa_s, ssid)) {
153
0
      int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
154
155
0
      if (temp_disabled <= 0)
156
0
        break;
157
158
0
      if (!min_temp_disabled ||
159
0
          temp_disabled < min_temp_disabled)
160
0
        min_temp_disabled = temp_disabled;
161
0
    }
162
0
    ssid = ssid->next;
163
0
  }
164
165
  /* ap_scan=2 mode - try to associate with each SSID. */
166
0
  if (ssid == NULL) {
167
0
    wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
168
0
      "end of scan list - go back to beginning");
169
0
    wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
170
0
    wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
171
0
    return;
172
0
  }
173
0
  if (ssid->next) {
174
    /* Continue from the next SSID on the next attempt. */
175
0
    wpa_s->prev_scan_ssid = ssid;
176
0
  } else {
177
    /* Start from the beginning of the SSID list. */
178
0
    wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
179
0
  }
180
0
  wpa_supplicant_associate(wpa_s, NULL, ssid);
181
0
}
182
183
184
static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
185
0
{
186
0
  struct wpa_supplicant *wpa_s = work->wpa_s;
187
0
  struct wpa_driver_scan_params *params = work->ctx;
188
0
  int ret;
189
190
0
  if (deinit) {
191
0
    if (!work->started) {
192
0
      wpa_scan_free_params(params);
193
0
      return;
194
0
    }
195
0
    wpa_supplicant_notify_scanning(wpa_s, 0);
196
0
    wpas_notify_scan_done(wpa_s, 0);
197
0
    wpa_s->scan_work = NULL;
198
0
    return;
199
0
  }
200
201
0
  if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
202
0
      wpa_s->wpa_state <= WPA_SCANNING)
203
0
    wpa_setup_mac_addr_rand_params(params, wpa_s->mac_addr_scan);
204
205
0
  if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
206
0
    wpa_msg(wpa_s, MSG_INFO,
207
0
      "Failed to assign random MAC address for a scan");
208
0
    wpa_scan_free_params(params);
209
0
    wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
210
0
    radio_work_done(work);
211
0
    return;
212
0
  }
213
214
0
  wpa_supplicant_notify_scanning(wpa_s, 1);
215
216
0
  if (wpa_s->clear_driver_scan_cache) {
217
0
    wpa_printf(MSG_DEBUG,
218
0
         "Request driver to clear scan cache due to local BSS flush");
219
0
    params->only_new_results = 1;
220
0
  }
221
0
  ret = wpa_drv_scan(wpa_s, params);
222
  /*
223
   * Store the obtained vendor scan cookie (if any) in wpa_s context.
224
   * The current design is to allow only one scan request on each
225
   * interface, hence having this scan cookie stored in wpa_s context is
226
   * fine for now.
227
   *
228
   * Revisit this logic if concurrent scan operations per interface
229
   * is supported.
230
   */
231
0
  if (ret == 0)
232
0
    wpa_s->curr_scan_cookie = params->scan_cookie;
233
0
  wpa_scan_free_params(params);
234
0
  work->ctx = NULL;
235
0
  if (ret) {
236
0
    int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
237
0
      !wpa_s->beacon_rep_data.token;
238
239
0
    if (wpa_s->disconnected)
240
0
      retry = 0;
241
242
    /* do not retry if operation is not supported */
243
0
    if (ret == -EOPNOTSUPP)
244
0
      retry = 0;
245
246
0
    wpa_supplicant_notify_scanning(wpa_s, 0);
247
0
    wpas_notify_scan_done(wpa_s, 0);
248
0
    if (wpa_s->wpa_state == WPA_SCANNING)
249
0
      wpa_supplicant_set_state(wpa_s,
250
0
             wpa_s->scan_prev_wpa_state);
251
0
    wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
252
0
      ret, retry ? " retry=1" : "");
253
0
    radio_work_done(work);
254
255
0
    if (retry) {
256
      /* Restore scan_req since we will try to scan again */
257
0
      wpa_s->scan_req = wpa_s->last_scan_req;
258
0
      wpa_supplicant_req_scan(wpa_s, 1, 0);
259
0
    } else if (wpa_s->scan_res_handler) {
260
      /* Clear the scan_res_handler */
261
0
      wpa_s->scan_res_handler = NULL;
262
0
    }
263
264
0
#ifndef CONFIG_NO_RRM
265
0
    if (wpa_s->beacon_rep_data.token)
266
0
      wpas_rrm_refuse_request(wpa_s);
267
0
#endif /* CONFIG_NO_RRM */
268
269
0
    return;
270
0
  }
271
272
0
  os_get_reltime(&wpa_s->scan_trigger_time);
273
0
  wpa_s->scan_runs++;
274
0
  wpa_s->normal_scans++;
275
0
  wpa_s->own_scan_requested = 1;
276
0
  wpa_s->clear_driver_scan_cache = 0;
277
0
  wpa_s->scan_work = work;
278
0
}
279
280
281
/**
282
 * wpa_supplicant_trigger_scan - Request driver to start a scan
283
 * @wpa_s: Pointer to wpa_supplicant data
284
 * @params: Scan parameters
285
 * @default_ies: Whether or not to use the default IEs in the Probe Request
286
 * frames. Note that this will free any existing IEs set in @params, so this
287
 * shouldn't be set if the IEs have already been set with
288
 * wpa_supplicant_extra_ies(). Otherwise, wpabuf_free() will lead to a
289
 * double-free.
290
 * @next: Whether or not to perform this scan as the next radio work
291
 * Returns: 0 on success, -1 on failure
292
 */
293
int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
294
        struct wpa_driver_scan_params *params,
295
        bool default_ies, bool next)
296
0
{
297
0
  struct wpa_driver_scan_params *ctx;
298
0
  struct wpabuf *ies = NULL;
299
300
0
  if (wpa_s->scan_work) {
301
0
    wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
302
0
    return -1;
303
0
  }
304
305
0
  if (default_ies) {
306
0
    if (params->extra_ies_len) {
307
0
      os_free((u8 *) params->extra_ies);
308
0
      params->extra_ies = NULL;
309
0
      params->extra_ies_len = 0;
310
0
    }
311
0
    ies = wpa_supplicant_extra_ies(wpa_s);
312
0
    if (ies) {
313
0
      params->extra_ies = wpabuf_head(ies);
314
0
      params->extra_ies_len = wpabuf_len(ies);
315
0
    }
316
0
  }
317
0
  ctx = wpa_scan_clone_params(params);
318
0
  if (ies) {
319
0
    wpabuf_free(ies);
320
0
    params->extra_ies = NULL;
321
0
    params->extra_ies_len = 0;
322
0
  }
323
0
  wpa_s->last_scan_all_chan = !params->freqs;
324
0
  wpa_s->last_scan_non_coloc_6ghz = params->non_coloc_6ghz;
325
326
0
  if (wpa_s->crossed_6ghz_dom) {
327
0
    wpa_printf(MSG_DEBUG, "First scan after crossing 6 GHz domain");
328
0
    wpa_s->crossed_6ghz_dom = false;
329
0
  }
330
331
0
  if (!ctx ||
332
0
      radio_add_work(wpa_s, 0, "scan", next, wpas_trigger_scan_cb,
333
0
         ctx) < 0) {
334
0
    wpa_scan_free_params(ctx);
335
0
    wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
336
0
    return -1;
337
0
  }
338
339
0
  wpa_s->wps_scan_done = false;
340
341
0
  return 0;
342
0
}
343
344
345
static void
346
wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
347
0
{
348
0
  struct wpa_supplicant *wpa_s = eloop_ctx;
349
350
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
351
352
0
  if (wpa_supplicant_req_sched_scan(wpa_s))
353
0
    wpa_supplicant_req_scan(wpa_s, 0, 0);
354
0
}
355
356
357
static void
358
wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
359
0
{
360
0
  struct wpa_supplicant *wpa_s = eloop_ctx;
361
362
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
363
364
0
  wpa_s->sched_scan_timed_out = 1;
365
0
  wpa_supplicant_cancel_sched_scan(wpa_s);
366
0
}
367
368
369
static int
370
wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
371
        struct wpa_driver_scan_params *params)
372
0
{
373
0
  int ret;
374
375
0
  wpa_supplicant_notify_scanning(wpa_s, 1);
376
0
  ret = wpa_drv_sched_scan(wpa_s, params);
377
0
  if (ret)
378
0
    wpa_supplicant_notify_scanning(wpa_s, 0);
379
0
  else
380
0
    wpa_s->sched_scanning = 1;
381
382
0
  return ret;
383
0
}
384
385
386
static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
387
0
{
388
0
  int ret;
389
390
0
  ret = wpa_drv_stop_sched_scan(wpa_s);
391
0
  if (ret) {
392
0
    wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
393
    /* TODO: what to do if stopping fails? */
394
0
    return -1;
395
0
  }
396
397
0
  return ret;
398
0
}
399
400
401
static struct wpa_driver_scan_filter *
402
wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
403
0
{
404
0
  struct wpa_driver_scan_filter *ssids;
405
0
  struct wpa_ssid *ssid;
406
0
  size_t count;
407
408
0
  *num_ssids = 0;
409
0
  if (!conf->filter_ssids)
410
0
    return NULL;
411
412
0
  for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
413
0
    if (ssid->ssid && ssid->ssid_len)
414
0
      count++;
415
0
  }
416
0
  if (count == 0)
417
0
    return NULL;
418
0
  ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
419
0
  if (ssids == NULL)
420
0
    return NULL;
421
422
0
  for (ssid = conf->ssid; ssid; ssid = ssid->next) {
423
0
    if (!ssid->ssid || !ssid->ssid_len)
424
0
      continue;
425
0
    os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
426
0
    ssids[*num_ssids].ssid_len = ssid->ssid_len;
427
0
    (*num_ssids)++;
428
0
  }
429
430
0
  return ssids;
431
0
}
432
433
434
static void wpa_supplicant_optimize_freqs(
435
  struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
436
0
{
437
#ifdef CONFIG_P2P
438
  if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
439
      wpa_s->go_params) {
440
    /* Optimize provisioning state scan based on GO information */
441
    if (wpa_s->p2p_in_provisioning < 5 &&
442
        wpa_s->go_params->freq > 0) {
443
      wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
444
        "preferred frequency %d MHz",
445
        wpa_s->go_params->freq);
446
      params->freqs = os_calloc(2, sizeof(int));
447
      if (params->freqs)
448
        params->freqs[0] = wpa_s->go_params->freq;
449
    } else if (wpa_s->p2p_in_provisioning < 8 &&
450
         wpa_s->go_params->freq_list[0]) {
451
      wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
452
        "channels");
453
      int_array_concat(&params->freqs,
454
           wpa_s->go_params->freq_list);
455
      if (params->freqs)
456
        int_array_sort_unique(params->freqs);
457
    }
458
    wpa_s->p2p_in_provisioning++;
459
  }
460
461
  if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
462
    struct wpa_ssid *ssid = wpa_s->current_ssid;
463
464
    /*
465
     * Perform a single-channel scan if the GO has already been
466
     * discovered on another non-P2P interface. Note that a scan
467
     * initiated by a P2P interface (e.g., the device interface)
468
     * should already have sufficient IEs and scan results will be
469
     * fetched on interface creation in that case.
470
     */
471
    if (wpa_s->p2p_in_invitation == 1 && ssid) {
472
      struct wpa_supplicant *ifs;
473
      struct wpa_bss *bss = NULL;
474
      const u8 *bssid = ssid->bssid_set ? ssid->bssid : NULL;
475
476
      dl_list_for_each(ifs, &wpa_s->radio->ifaces,
477
           struct wpa_supplicant, radio_list) {
478
        bss = wpa_bss_get(ifs, bssid, ssid->ssid,
479
              ssid->ssid_len);
480
        if (bss)
481
          break;
482
      }
483
      if (bss && !disabled_freq(wpa_s, bss->freq)) {
484
        params->freqs = os_calloc(2, sizeof(int));
485
        if (params->freqs) {
486
          wpa_dbg(wpa_s, MSG_DEBUG,
487
            "P2P: Scan only the known GO frequency %d MHz during invitation",
488
            bss->freq);
489
          params->freqs[0] = bss->freq;
490
        }
491
      }
492
    }
493
494
    /*
495
     * Optimize scan based on GO information during persistent
496
     * group reinvocation
497
     */
498
    if (!params->freqs && wpa_s->p2p_in_invitation < 5 &&
499
        wpa_s->p2p_invite_go_freq > 0) {
500
      if (wpa_s->p2p_invite_go_freq == 2 ||
501
          wpa_s->p2p_invite_go_freq == 5) {
502
        enum hostapd_hw_mode mode;
503
504
        wpa_dbg(wpa_s, MSG_DEBUG,
505
          "P2P: Scan only GO preferred band %d GHz during invitation",
506
          wpa_s->p2p_invite_go_freq);
507
508
        if (!wpa_s->hw.modes)
509
          return;
510
        mode = wpa_s->p2p_invite_go_freq == 5 ?
511
          HOSTAPD_MODE_IEEE80211A :
512
          HOSTAPD_MODE_IEEE80211G;
513
        if (wpa_s->p2p_in_invitation <= 2)
514
          wpa_add_scan_freqs_list(wpa_s, mode,
515
                params, false,
516
                false, true);
517
        if (!params->freqs || params->freqs[0] == 0)
518
          wpa_add_scan_freqs_list(wpa_s, mode,
519
                params, false,
520
                false, false);
521
      } else {
522
        wpa_dbg(wpa_s, MSG_DEBUG,
523
          "P2P: Scan only GO preferred frequency %d MHz during invitation",
524
          wpa_s->p2p_invite_go_freq);
525
        params->freqs = os_calloc(2, sizeof(int));
526
        if (params->freqs)
527
          params->freqs[0] =
528
              wpa_s->p2p_invite_go_freq;
529
      }
530
    }
531
    wpa_s->p2p_in_invitation++;
532
    if (wpa_s->p2p_in_invitation > 20) {
533
      /*
534
       * This should not really happen since the variable is
535
       * cleared on group removal, but if it does happen, make
536
       * sure we do not get stuck in special invitation scan
537
       * mode.
538
       */
539
      wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
540
      wpa_s->p2p_in_invitation = 0;
541
      wpa_s->p2p_retry_limit = 0;
542
    }
543
  }
544
#endif /* CONFIG_P2P */
545
546
#ifdef CONFIG_WPS
547
  if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
548
    /*
549
     * Optimize post-provisioning scan based on channel used
550
     * during provisioning.
551
     */
552
    wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
553
      "that was used during provisioning", wpa_s->wps_freq);
554
    params->freqs = os_calloc(2, sizeof(int));
555
    if (params->freqs)
556
      params->freqs[0] = wpa_s->wps_freq;
557
    wpa_s->after_wps--;
558
  } else if (wpa_s->after_wps)
559
    wpa_s->after_wps--;
560
561
  if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
562
  {
563
    /* Optimize provisioning scan based on already known channel */
564
    wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
565
      wpa_s->wps_freq);
566
    params->freqs = os_calloc(2, sizeof(int));
567
    if (params->freqs)
568
      params->freqs[0] = wpa_s->wps_freq;
569
    wpa_s->known_wps_freq = 0; /* only do this once */
570
  }
571
#endif /* CONFIG_WPS */
572
0
}
573
574
575
#ifdef CONFIG_INTERWORKING
576
static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
577
             struct wpabuf *buf)
578
0
{
579
0
  wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
580
0
  wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
581
0
          1 + ETH_ALEN);
582
0
  wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
583
  /* No Venue Info */
584
0
  if (!is_zero_ether_addr(wpa_s->conf->hessid))
585
0
    wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
586
0
}
587
#endif /* CONFIG_INTERWORKING */
588
589
590
#ifdef CONFIG_MBO
591
static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
592
            struct wpabuf **ie)
593
{
594
  if (wpabuf_resize(ie, 5)) {
595
    wpa_printf(MSG_DEBUG,
596
         "Failed to allocate space for FILS Request Parameters element");
597
    return;
598
  }
599
600
  /* FILS Request Parameters element */
601
  wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
602
  wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
603
  wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
604
  /* Parameter control bitmap */
605
  wpabuf_put_u8(*ie, 0);
606
  /* Max Channel Time field - contains the value of MaxChannelTime
607
   * parameter of the MLME-SCAN.request primitive represented in units of
608
   * TUs, as an unsigned integer. A Max Channel Time field value of 255
609
   * is used to indicate any duration of more than 254 TUs, or an
610
   * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
611
   */
612
  wpabuf_put_u8(*ie, 255);
613
}
614
#endif /* CONFIG_MBO */
615
616
617
void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
618
0
{
619
0
  struct wpabuf *default_ies = NULL;
620
0
  u8 ext_capab[18];
621
0
  int ext_capab_len, frame_id;
622
0
  enum wpa_driver_if_type type = WPA_IF_STATION;
623
624
#ifdef CONFIG_P2P
625
  if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
626
    type = WPA_IF_P2P_CLIENT;
627
#endif /* CONFIG_P2P */
628
629
0
  wpa_drv_get_ext_capa(wpa_s, type);
630
631
0
  ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
632
0
               sizeof(ext_capab), NULL);
633
0
  if (ext_capab_len > 0 &&
634
0
      wpabuf_resize(&default_ies, ext_capab_len) == 0)
635
0
    wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
636
637
#ifdef CONFIG_MBO
638
  if (wpa_s->enable_oce & OCE_STA)
639
    wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
640
  /* Send MBO and OCE capabilities */
641
  if (wpabuf_resize(&default_ies, 12) == 0)
642
    wpas_mbo_scan_ie(wpa_s, default_ies);
643
#endif /* CONFIG_MBO */
644
645
0
  if (type == WPA_IF_P2P_CLIENT)
646
0
    frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
647
0
  else
648
0
    frame_id = VENDOR_ELEM_PROBE_REQ;
649
650
0
  if (wpa_s->vendor_elem[frame_id]) {
651
0
    size_t len;
652
653
0
    len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
654
0
    if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
655
0
      wpabuf_put_buf(default_ies,
656
0
               wpa_s->vendor_elem[frame_id]);
657
0
  }
658
659
0
  if (default_ies)
660
0
    wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
661
0
               wpabuf_len(default_ies));
662
0
  wpabuf_free(default_ies);
663
0
}
664
665
666
static struct wpabuf * wpa_supplicant_ml_probe_ie(int mld_id, u16 links)
667
0
{
668
0
  struct wpabuf *extra_ie;
669
0
  u16 control = MULTI_LINK_CONTROL_TYPE_PROBE_REQ;
670
0
  size_t len = 3 + 4 + 4 * MAX_NUM_MLD_LINKS;
671
0
  u8 link_id;
672
0
  u8 *len_pos;
673
674
0
  if (mld_id >= 0) {
675
0
    control |= EHT_ML_PRES_BM_PROBE_REQ_AP_MLD_ID;
676
0
    len++;
677
0
  }
678
679
0
  extra_ie = wpabuf_alloc(len);
680
0
  if (!extra_ie)
681
0
    return NULL;
682
683
0
  wpabuf_put_u8(extra_ie, WLAN_EID_EXTENSION);
684
0
  len_pos = wpabuf_put(extra_ie, 1);
685
0
  wpabuf_put_u8(extra_ie, WLAN_EID_EXT_MULTI_LINK);
686
687
0
  wpabuf_put_le16(extra_ie, control);
688
689
  /* common info length and MLD ID (if requested) */
690
0
  if (mld_id >= 0) {
691
0
    wpabuf_put_u8(extra_ie, 2);
692
0
    wpabuf_put_u8(extra_ie, mld_id);
693
694
0
    wpa_printf(MSG_DEBUG, "MLD: ML probe targeted at MLD ID %d",
695
0
         mld_id);
696
0
  } else {
697
0
    wpabuf_put_u8(extra_ie, 1);
698
699
0
    wpa_printf(MSG_DEBUG, "MLD: ML probe targeted at receiving AP");
700
0
  }
701
702
0
  if (!links)
703
0
    wpa_printf(MSG_DEBUG, "MLD: Probing all links");
704
0
  else
705
0
    wpa_printf(MSG_DEBUG, "MLD: Probing links 0x%04x", links);
706
707
0
  for_each_link(links, link_id) {
708
0
    wpabuf_put_u8(extra_ie, EHT_ML_SUB_ELEM_PER_STA_PROFILE);
709
710
    /* Subelement length includes only the control */
711
0
    wpabuf_put_u8(extra_ie, 2);
712
713
0
    control = link_id | EHT_PER_STA_CTRL_COMPLETE_PROFILE_MSK;
714
715
0
    wpabuf_put_le16(extra_ie, control);
716
0
  }
717
718
0
  *len_pos = (u8 *) wpabuf_put(extra_ie, 0) - len_pos - 1;
719
720
0
  return extra_ie;
721
0
}
722
723
724
static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
725
0
{
726
0
  struct wpabuf *extra_ie = NULL;
727
0
  u8 ext_capab[18];
728
0
  int ext_capab_len;
729
#ifdef CONFIG_WPS
730
  int wps = 0;
731
  enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
732
#endif /* CONFIG_WPS */
733
734
0
  if (!is_zero_ether_addr(wpa_s->ml_probe_bssid)) {
735
0
    extra_ie = wpa_supplicant_ml_probe_ie(wpa_s->ml_probe_mld_id,
736
0
                  wpa_s->ml_probe_links);
737
738
    /* No other elements should be included in the probe request */
739
0
    wpa_printf(MSG_DEBUG, "MLD: Scan including only ML element");
740
0
    return extra_ie;
741
0
  }
742
743
#ifdef CONFIG_P2P
744
  if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
745
    wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
746
  else
747
#endif /* CONFIG_P2P */
748
0
    wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
749
750
0
  ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
751
0
               sizeof(ext_capab), NULL);
752
0
  if (ext_capab_len > 0 &&
753
0
      (size_t) ext_capab_len < wpa_s->drv_max_probe_req_ie_len &&
754
0
      wpabuf_resize(&extra_ie, ext_capab_len) == 0)
755
0
    wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
756
757
0
#ifdef CONFIG_INTERWORKING
758
0
  if (wpa_s->conf->interworking &&
759
0
      wpa_s->drv_max_probe_req_ie_len >= 2 &&
760
0
      wpabuf_resize(&extra_ie, 100) == 0)
761
0
    wpas_add_interworking_elements(wpa_s, extra_ie);
762
0
#endif /* CONFIG_INTERWORKING */
763
764
#ifdef CONFIG_MBO
765
  if ((wpa_s->enable_oce & OCE_STA) &&
766
      wpa_s->drv_max_probe_req_ie_len >= 5)
767
    wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
768
#endif /* CONFIG_MBO */
769
770
#ifdef CONFIG_WPS
771
  wps = wpas_wps_in_use(wpa_s, &req_type);
772
773
  if (wps) {
774
    struct wpabuf *wps_ie;
775
    wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
776
            DEV_PW_DEFAULT,
777
            &wpa_s->wps->dev,
778
            wpa_s->wps->uuid, req_type,
779
            0, NULL);
780
    if (wps_ie &&
781
        wpabuf_len(wps_ie) <= wpa_s->drv_max_probe_req_ie_len &&
782
        wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
783
      wpabuf_put_buf(extra_ie, wps_ie);
784
    wpabuf_free(wps_ie);
785
  }
786
787
#ifdef CONFIG_P2P
788
  if (wps) {
789
    size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
790
791
    if (ielen <= wpa_s->drv_max_probe_req_ie_len &&
792
        wpabuf_resize(&extra_ie, ielen) == 0)
793
      wpas_p2p_scan_ie(wpa_s, extra_ie);
794
  }
795
#endif /* CONFIG_P2P */
796
797
  wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
798
799
#endif /* CONFIG_WPS */
800
801
0
#ifdef CONFIG_HS20
802
0
  if (wpa_s->conf->hs20 && wpa_s->drv_max_probe_req_ie_len >= 9 &&
803
0
      wpabuf_resize(&extra_ie, 9) == 0)
804
0
    wpas_hs20_add_indication(extra_ie, -1, 0);
805
0
#endif /* CONFIG_HS20 */
806
807
#ifdef CONFIG_FST
808
  if (wpa_s->fst_ies &&
809
      wpa_s->drv_max_probe_req_ie_len >= wpabuf_len(wpa_s->fst_ies) &&
810
      wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
811
    wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
812
#endif /* CONFIG_FST */
813
814
#ifdef CONFIG_MBO
815
  /* Send MBO and OCE capabilities */
816
  if (wpabuf_resize(&extra_ie, 12) == 0)
817
    wpas_mbo_scan_ie(wpa_s, extra_ie);
818
#endif /* CONFIG_MBO */
819
820
0
  if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
821
0
    struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
822
823
0
    if (wpa_s->drv_max_probe_req_ie_len >= wpabuf_len(buf) &&
824
0
        wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
825
0
      wpabuf_put_buf(extra_ie, buf);
826
0
  }
827
828
0
  return extra_ie;
829
0
}
830
831
832
#ifdef CONFIG_P2P
833
834
/*
835
 * Check whether there are any enabled networks or credentials that could be
836
 * used for a non-P2P connection.
837
 */
838
static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
839
{
840
  struct wpa_ssid *ssid;
841
842
  for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
843
    if (wpas_network_disabled(wpa_s, ssid))
844
      continue;
845
    if (!ssid->p2p_group)
846
      return 1;
847
  }
848
849
  if (wpa_s->conf->cred && wpa_s->conf->interworking &&
850
      wpa_s->conf->auto_interworking)
851
    return 1;
852
853
  return 0;
854
}
855
856
#endif /* CONFIG_P2P */
857
858
859
int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
860
          enum hostapd_hw_mode band,
861
          struct wpa_driver_scan_params *params,
862
          bool is_6ghz, bool only_6ghz_psc,
863
          bool exclude_radar)
864
0
{
865
  /* Include only supported channels for the specified band */
866
0
  struct hostapd_hw_modes *mode;
867
0
  int num_chans = 0;
868
0
  int *freqs, i;
869
870
0
  mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
871
0
  if (!mode || !mode->num_channels)
872
0
    return -1;
873
874
0
  if (params->freqs) {
875
0
    while (params->freqs[num_chans])
876
0
      num_chans++;
877
0
  }
878
879
0
  freqs = os_realloc(params->freqs,
880
0
         (num_chans + mode->num_channels + 1) * sizeof(int));
881
0
  if (!freqs)
882
0
    return -1;
883
884
0
  params->freqs = freqs;
885
0
  for (i = 0; i < mode->num_channels; i++) {
886
0
    if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
887
0
      continue;
888
0
    if (exclude_radar &&
889
0
        (mode->channels[i].flag & HOSTAPD_CHAN_RADAR))
890
0
      continue;
891
892
0
    if (is_6ghz && only_6ghz_psc &&
893
0
        !is_6ghz_psc_frequency(mode->channels[i].freq))
894
0
      continue;
895
896
0
    params->freqs[num_chans++] = mode->channels[i].freq;
897
0
  }
898
0
  params->freqs[num_chans] = 0;
899
900
0
  return 0;
901
0
}
902
903
904
static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
905
           struct wpa_driver_scan_params *params)
906
0
{
907
0
  if (wpa_s->hw.modes == NULL)
908
0
    return; /* unknown what channels the driver supports */
909
0
  if (params->freqs)
910
0
    return; /* already using a limited channel set */
911
912
0
  if (wpa_s->setband_mask & WPA_SETBAND_5G)
913
0
    wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
914
0
          false, false, false);
915
0
  if (wpa_s->setband_mask & WPA_SETBAND_2G)
916
0
    wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
917
0
          false, false, false);
918
0
  if (wpa_s->setband_mask & WPA_SETBAND_6G)
919
0
    wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
920
0
          true, false, false);
921
0
}
922
923
924
static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
925
            struct wpa_driver_scan_params *params,
926
            size_t max_ssids, const u8 *ssid, size_t ssid_len)
927
0
{
928
0
  unsigned int j;
929
930
0
  for (j = 0; j < params->num_ssids; j++) {
931
0
    if (params->ssids[j].ssid_len == ssid_len &&
932
0
        params->ssids[j].ssid &&
933
0
        os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
934
0
      return; /* already in the list */
935
0
  }
936
937
0
  if (params->num_ssids + 1 > max_ssids) {
938
0
    wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
939
0
    return;
940
0
  }
941
942
0
  wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
943
0
       wpa_ssid_txt(ssid, ssid_len));
944
945
0
  params->ssids[params->num_ssids].ssid = ssid;
946
0
  params->ssids[params->num_ssids].ssid_len = ssid_len;
947
0
  params->num_ssids++;
948
0
}
949
950
951
void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
952
         struct wpa_driver_scan_params *params,
953
         const struct wpa_ssid *ssid, size_t max_ssids)
954
0
{
955
#ifdef CONFIG_OWE
956
  struct wpa_bss *bss;
957
958
  if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
959
    return;
960
961
  wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
962
       wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
963
964
  dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
965
    const u8 *owe, *owe_bssid, *owe_ssid;
966
    size_t owe_ssid_len;
967
968
    if (bss->ssid_len != ssid->ssid_len ||
969
        os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
970
      continue;
971
972
    owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
973
    if (!owe || owe[1] < 4)
974
      continue;
975
976
    if (wpas_get_owe_trans_network(owe, &owe_bssid, &owe_ssid,
977
                 &owe_ssid_len))
978
      continue;
979
980
    wpa_printf(MSG_DEBUG,
981
         "OWE: scan_ssids: transition mode OWE ssid=%s",
982
         wpa_ssid_txt(owe_ssid, owe_ssid_len));
983
984
    wpa_add_scan_ssid(wpa_s, params, max_ssids,
985
          owe_ssid, owe_ssid_len);
986
    return;
987
  }
988
#endif /* CONFIG_OWE */
989
0
}
990
991
992
static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
993
             struct wpa_driver_scan_params *params,
994
             size_t max_ssids)
995
0
{
996
0
  unsigned int i;
997
0
  struct wpa_ssid *ssid;
998
999
  /*
1000
   * For devices with max_ssids greater than 1, leave the last slot empty
1001
   * for adding the wildcard scan entry.
1002
   */
1003
0
  max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
1004
1005
0
  for (i = 0; i < wpa_s->scan_id_count; i++) {
1006
0
    ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
1007
0
    if (!ssid)
1008
0
      continue;
1009
0
    if (ssid->scan_ssid)
1010
0
      wpa_add_scan_ssid(wpa_s, params, max_ssids,
1011
0
            ssid->ssid, ssid->ssid_len);
1012
    /*
1013
     * Also add the SSID of the OWE BSS, to allow discovery of
1014
     * transition mode APs more quickly.
1015
     */
1016
0
    wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
1017
0
  }
1018
1019
0
  wpa_s->scan_id_count = 0;
1020
0
}
1021
1022
1023
static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
1024
               struct wpa_driver_scan_params *params,
1025
               size_t max_ssids)
1026
0
{
1027
0
  unsigned int i;
1028
1029
0
  if (wpa_s->ssids_from_scan_req == NULL ||
1030
0
      wpa_s->num_ssids_from_scan_req == 0)
1031
0
    return 0;
1032
1033
0
  if (wpa_s->num_ssids_from_scan_req > max_ssids) {
1034
0
    wpa_s->num_ssids_from_scan_req = max_ssids;
1035
0
    wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
1036
0
         (unsigned int) max_ssids);
1037
0
  }
1038
1039
0
  for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
1040
0
    params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
1041
0
    params->ssids[i].ssid_len =
1042
0
      wpa_s->ssids_from_scan_req[i].ssid_len;
1043
0
    wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
1044
0
          params->ssids[i].ssid,
1045
0
          params->ssids[i].ssid_len);
1046
0
  }
1047
1048
0
  params->num_ssids = wpa_s->num_ssids_from_scan_req;
1049
0
  wpa_s->num_ssids_from_scan_req = 0;
1050
0
  return 1;
1051
0
}
1052
1053
1054
static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
1055
0
{
1056
0
  struct wpa_supplicant *wpa_s = eloop_ctx;
1057
0
  struct wpa_ssid *ssid;
1058
0
  int ret, p2p_in_prog;
1059
0
  struct wpabuf *extra_ie = NULL;
1060
0
  struct wpa_driver_scan_params params;
1061
0
  struct wpa_driver_scan_params *scan_params;
1062
0
  size_t max_ssids;
1063
0
  int connect_without_scan = 0;
1064
1065
0
  wpa_s->ignore_post_flush_scan_res = 0;
1066
1067
0
  if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1068
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
1069
0
    return;
1070
0
  }
1071
1072
0
  if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
1073
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
1074
0
    wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1075
0
    return;
1076
0
  }
1077
1078
0
  if (wpa_s->scanning) {
1079
    /*
1080
     * If we are already in scanning state, we shall reschedule the
1081
     * the incoming scan request.
1082
     */
1083
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
1084
0
    wpa_supplicant_req_scan(wpa_s, 1, 0);
1085
0
    return;
1086
0
  }
1087
1088
0
  if (!wpa_supplicant_enabled_networks(wpa_s) &&
1089
0
      wpa_s->scan_req == NORMAL_SCAN_REQ) {
1090
0
    wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
1091
0
    wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1092
0
    return;
1093
0
  }
1094
1095
0
  if (wpa_s->conf->ap_scan != 0 &&
1096
0
      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
1097
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
1098
0
      "overriding ap_scan configuration");
1099
0
    wpa_s->conf->ap_scan = 0;
1100
0
    wpas_notify_ap_scan_changed(wpa_s);
1101
0
  }
1102
1103
0
  if (wpa_s->conf->ap_scan == 0) {
1104
0
    wpa_supplicant_gen_assoc_event(wpa_s);
1105
0
    return;
1106
0
  }
1107
1108
0
  ssid = NULL;
1109
0
  if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
1110
0
      wpa_s->connect_without_scan) {
1111
0
    connect_without_scan = 1;
1112
0
    for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1113
0
      if (ssid == wpa_s->connect_without_scan)
1114
0
        break;
1115
0
    }
1116
0
  }
1117
1118
0
  p2p_in_prog = wpas_p2p_in_progress(wpa_s);
1119
0
  if (p2p_in_prog && p2p_in_prog != 2 &&
1120
0
      (!ssid ||
1121
0
       (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
1122
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
1123
0
    wpa_supplicant_req_scan(wpa_s, 5, 0);
1124
0
    return;
1125
0
  }
1126
1127
  /*
1128
   * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
1129
   * used for changing modes inside wpa_supplicant (roaming,
1130
   * auto-reconnect, etc). Discarding the scan might hurt these processes.
1131
   * The normal use case for PNO is to suspend the host immediately after
1132
   * starting PNO, so the periodic 100 ms attempts to run the scan do not
1133
   * normally happen in practice multiple times, i.e., this is simply
1134
   * restarting scanning once the host is woken up and PNO stopped.
1135
   */
1136
0
  if (wpa_s->pno || wpa_s->pno_sched_pending) {
1137
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
1138
0
    wpa_supplicant_req_scan(wpa_s, 0, 100000);
1139
0
    return;
1140
0
  }
1141
1142
0
  if (wpa_s->conf->ap_scan == 2)
1143
0
    max_ssids = 1;
1144
0
  else {
1145
0
    max_ssids = wpa_s->max_scan_ssids;
1146
0
    if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1147
0
      max_ssids = WPAS_MAX_SCAN_SSIDS;
1148
0
  }
1149
1150
0
  wpa_s->last_scan_req = wpa_s->scan_req;
1151
0
  wpa_s->scan_req = NORMAL_SCAN_REQ;
1152
1153
0
  if (connect_without_scan) {
1154
0
    wpa_s->connect_without_scan = NULL;
1155
0
    if (ssid) {
1156
0
      wpa_printf(MSG_DEBUG, "Start a pre-selected network "
1157
0
           "without scan step");
1158
0
      wpa_supplicant_associate(wpa_s, NULL, ssid);
1159
0
      return;
1160
0
    }
1161
0
  }
1162
1163
0
  os_memset(&params, 0, sizeof(params));
1164
1165
0
  wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
1166
0
  if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1167
0
      wpa_s->wpa_state == WPA_INACTIVE)
1168
0
    wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1169
1170
  /*
1171
   * If autoscan has set its own scanning parameters
1172
   */
1173
0
  if (wpa_s->autoscan_params != NULL) {
1174
0
    scan_params = wpa_s->autoscan_params;
1175
0
    goto scan;
1176
0
  }
1177
1178
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1179
0
      wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1180
0
    wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1181
0
    goto ssid_list_set;
1182
0
  }
1183
1184
#ifdef CONFIG_P2P
1185
  if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
1186
      wpa_s->go_params && !wpa_s->conf->passive_scan) {
1187
    wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1188
         wpa_s->p2p_in_provisioning,
1189
         wpa_s->show_group_started);
1190
    params.ssids[0].ssid = wpa_s->go_params->ssid;
1191
    params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1192
    params.num_ssids = 1;
1193
    params.bssid = wpa_s->go_params->peer_interface_addr;
1194
    wpa_printf(MSG_DEBUG, "P2P: Use specific BSSID " MACSTR
1195
         " (peer interface address) for scan",
1196
         MAC2STR(params.bssid));
1197
    goto ssid_list_set;
1198
  }
1199
1200
  if (wpa_s->p2p_in_invitation) {
1201
    if (wpa_s->current_ssid) {
1202
      wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1203
      params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1204
      params.ssids[0].ssid_len =
1205
        wpa_s->current_ssid->ssid_len;
1206
      params.num_ssids = 1;
1207
      if (wpa_s->current_ssid->bssid_set) {
1208
        params.bssid = wpa_s->current_ssid->bssid;
1209
        wpa_printf(MSG_DEBUG, "P2P: Use specific BSSID "
1210
             MACSTR " for scan",
1211
             MAC2STR(params.bssid));
1212
      }
1213
    } else {
1214
      wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1215
    }
1216
    goto ssid_list_set;
1217
  }
1218
#endif /* CONFIG_P2P */
1219
1220
  /* Find the starting point from which to continue scanning */
1221
0
  ssid = wpa_s->conf->ssid;
1222
0
  if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1223
0
    while (ssid) {
1224
0
      if (ssid == wpa_s->prev_scan_ssid) {
1225
0
        ssid = ssid->next;
1226
0
        break;
1227
0
      }
1228
0
      ssid = ssid->next;
1229
0
    }
1230
0
  }
1231
1232
0
  if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
1233
#ifdef CONFIG_AP
1234
      !wpa_s->ap_iface &&
1235
#endif /* CONFIG_AP */
1236
0
      wpa_s->conf->ap_scan == 2) {
1237
0
    wpa_s->connect_without_scan = NULL;
1238
0
    wpa_s->prev_scan_wildcard = 0;
1239
0
    wpa_supplicant_assoc_try(wpa_s, ssid);
1240
0
    return;
1241
0
  } else if (wpa_s->conf->ap_scan == 2) {
1242
    /*
1243
     * User-initiated scan request in ap_scan == 2; scan with
1244
     * wildcard SSID.
1245
     */
1246
0
    ssid = NULL;
1247
0
  } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1248
    /*
1249
     * Perform single-channel single-SSID scan for
1250
     * reassociate-to-same-BSS operation.
1251
     */
1252
    /* Setup SSID */
1253
0
    ssid = wpa_s->current_ssid;
1254
0
    wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1255
0
          ssid->ssid, ssid->ssid_len);
1256
0
    params.ssids[0].ssid = ssid->ssid;
1257
0
    params.ssids[0].ssid_len = ssid->ssid_len;
1258
0
    params.num_ssids = 1;
1259
1260
    /*
1261
     * Allocate memory for frequency array, allocate one extra
1262
     * slot for the zero-terminator.
1263
     */
1264
0
    params.freqs = os_malloc(sizeof(int) * 2);
1265
0
    if (params.freqs) {
1266
0
      params.freqs[0] = wpa_s->assoc_freq;
1267
0
      params.freqs[1] = 0;
1268
0
    }
1269
1270
    /*
1271
     * Reset the reattach flag so that we fall back to full scan if
1272
     * this scan fails.
1273
     */
1274
0
    wpa_s->reattach = 0;
1275
0
  } else {
1276
0
    struct wpa_ssid *start = ssid, *tssid;
1277
0
    int freqs_set = 0;
1278
0
    if (ssid == NULL && max_ssids > 1)
1279
0
      ssid = wpa_s->conf->ssid;
1280
0
    while (ssid) {
1281
0
      if (!wpas_network_disabled(wpa_s, ssid) &&
1282
0
          ssid->scan_ssid) {
1283
0
        wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1284
0
              ssid->ssid, ssid->ssid_len);
1285
0
        params.ssids[params.num_ssids].ssid =
1286
0
          ssid->ssid;
1287
0
        params.ssids[params.num_ssids].ssid_len =
1288
0
          ssid->ssid_len;
1289
0
        params.num_ssids++;
1290
0
        if (params.num_ssids + 1 >= max_ssids)
1291
0
          break;
1292
0
      }
1293
1294
0
      if (!wpas_network_disabled(wpa_s, ssid)) {
1295
        /*
1296
         * Also add the SSID of the OWE BSS, to allow
1297
         * discovery of transition mode APs more
1298
         * quickly.
1299
         */
1300
0
        wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1301
0
                  max_ssids);
1302
0
      }
1303
1304
0
      ssid = ssid->next;
1305
0
      if (ssid == start)
1306
0
        break;
1307
0
      if (ssid == NULL && max_ssids > 1 &&
1308
0
          start != wpa_s->conf->ssid)
1309
0
        ssid = wpa_s->conf->ssid;
1310
0
    }
1311
1312
0
    if (wpa_s->scan_id_count &&
1313
0
        wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1314
0
      wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1315
1316
0
    for (tssid = wpa_s->conf->ssid;
1317
0
         wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1318
0
         tssid = tssid->next) {
1319
0
      if (wpas_network_disabled(wpa_s, tssid))
1320
0
        continue;
1321
0
      if (((params.freqs || !freqs_set) &&
1322
0
           tssid->scan_freq) &&
1323
0
          int_array_len(params.freqs) < 100) {
1324
0
        int_array_concat(&params.freqs,
1325
0
             tssid->scan_freq);
1326
0
      } else {
1327
0
        os_free(params.freqs);
1328
0
        params.freqs = NULL;
1329
0
      }
1330
0
      freqs_set = 1;
1331
0
    }
1332
0
    int_array_sort_unique(params.freqs);
1333
0
  }
1334
1335
0
  if (ssid && max_ssids == 1) {
1336
    /*
1337
     * If the driver is limited to 1 SSID at a time interleave
1338
     * wildcard SSID scans with specific SSID scans to avoid
1339
     * waiting a long time for a wildcard scan.
1340
     */
1341
0
    if (!wpa_s->prev_scan_wildcard) {
1342
0
      params.ssids[0].ssid = NULL;
1343
0
      params.ssids[0].ssid_len = 0;
1344
0
      wpa_s->prev_scan_wildcard = 1;
1345
0
      wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1346
0
        "wildcard SSID (Interleave with specific)");
1347
0
    } else {
1348
0
      wpa_s->prev_scan_ssid = ssid;
1349
0
      wpa_s->prev_scan_wildcard = 0;
1350
0
      wpa_dbg(wpa_s, MSG_DEBUG,
1351
0
        "Starting AP scan for specific SSID: %s",
1352
0
        wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1353
0
    }
1354
0
  } else if (ssid) {
1355
    /* max_ssids > 1 */
1356
1357
0
    wpa_s->prev_scan_ssid = ssid;
1358
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1359
0
      "the scan request");
1360
0
    params.num_ssids++;
1361
0
  } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1362
0
       wpa_s->manual_scan_passive && params.num_ssids == 0) {
1363
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
1364
0
  } else if (wpa_s->conf->passive_scan) {
1365
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1366
0
      "Use passive scan based on configuration");
1367
0
  } else {
1368
0
    wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1369
0
    params.num_ssids++;
1370
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1371
0
      "SSID");
1372
0
  }
1373
1374
0
ssid_list_set:
1375
0
  wpa_supplicant_optimize_freqs(wpa_s, &params);
1376
0
  extra_ie = wpa_supplicant_extra_ies(wpa_s);
1377
1378
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1379
0
      wpa_s->manual_scan_only_new) {
1380
0
    wpa_printf(MSG_DEBUG,
1381
0
         "Request driver to clear scan cache due to manual only_new=1 scan");
1382
0
    params.only_new_results = 1;
1383
0
  }
1384
1385
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1386
0
      wpa_s->manual_scan_freqs) {
1387
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1388
0
    params.freqs = wpa_s->manual_scan_freqs;
1389
0
    wpa_s->manual_scan_freqs = NULL;
1390
0
  }
1391
1392
0
  if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1393
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1394
0
      "Limit select_network scan to specified channels");
1395
0
    params.freqs = wpa_s->select_network_scan_freqs;
1396
0
    wpa_s->select_network_scan_freqs = NULL;
1397
0
  }
1398
1399
0
  if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1400
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1401
0
      "generated frequency list");
1402
0
    params.freqs = wpa_s->next_scan_freqs;
1403
0
  } else
1404
0
    os_free(wpa_s->next_scan_freqs);
1405
0
  wpa_s->next_scan_freqs = NULL;
1406
0
  wpa_setband_scan_freqs(wpa_s, &params);
1407
1408
  /* See if user specified frequencies. If so, scan only those. */
1409
0
  if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1410
0
      wpa_s->conf->initial_freq_list && !params.freqs) {
1411
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1412
0
      "Optimize scan based on conf->initial_freq_list");
1413
0
    int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1414
0
  } else if (wpa_s->conf->freq_list && !params.freqs) {
1415
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1416
0
      "Optimize scan based on conf->freq_list");
1417
0
    int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1418
0
  }
1419
1420
  /* Use current associated channel? */
1421
0
  if (wpa_s->conf->scan_cur_freq && !params.freqs) {
1422
0
    unsigned int num = wpa_s->num_multichan_concurrent;
1423
1424
0
    params.freqs = os_calloc(num + 1, sizeof(int));
1425
0
    if (params.freqs) {
1426
0
      num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1427
0
                 false);
1428
0
      if (num > 0) {
1429
0
        wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1430
0
          "current operating channels since "
1431
0
          "scan_cur_freq is enabled");
1432
0
      } else {
1433
0
        os_free(params.freqs);
1434
0
        params.freqs = NULL;
1435
0
      }
1436
0
    }
1437
0
  }
1438
1439
#ifdef CONFIG_MBO
1440
  if (wpa_s->enable_oce & OCE_STA)
1441
    params.oce_scan = 1;
1442
#endif /* CONFIG_MBO */
1443
1444
0
  params.filter_ssids = wpa_supplicant_build_filter_ssids(
1445
0
    wpa_s->conf, &params.num_filter_ssids);
1446
0
  if (extra_ie) {
1447
0
    params.extra_ies = wpabuf_head(extra_ie);
1448
0
    params.extra_ies_len = wpabuf_len(extra_ie);
1449
0
  }
1450
1451
#ifdef CONFIG_P2P
1452
  if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
1453
      (wpa_s->show_group_started && wpa_s->go_params)) {
1454
    /*
1455
     * The interface may not yet be in P2P mode, so we have to
1456
     * explicitly request P2P probe to disable CCK rates.
1457
     */
1458
    params.p2p_probe = 1;
1459
  }
1460
#endif /* CONFIG_P2P */
1461
1462
0
  if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
1463
0
      wpa_s->wpa_state <= WPA_SCANNING)
1464
0
    wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
1465
1466
0
  if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1467
0
    struct wpa_bss *bss;
1468
1469
0
    params.bssid = wpa_s->next_scan_bssid;
1470
0
    bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1471
0
    if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1472
0
        bss && bss->ssid_len && params.num_ssids == 1 &&
1473
0
        params.ssids[0].ssid_len == 0) {
1474
0
      params.ssids[0].ssid = bss->ssid;
1475
0
      params.ssids[0].ssid_len = bss->ssid_len;
1476
0
      wpa_dbg(wpa_s, MSG_DEBUG,
1477
0
        "Scan a previously specified BSSID " MACSTR
1478
0
        " and SSID %s",
1479
0
        MAC2STR(params.bssid),
1480
0
        wpa_ssid_txt(bss->ssid, bss->ssid_len));
1481
0
    } else {
1482
0
      wpa_dbg(wpa_s, MSG_DEBUG,
1483
0
        "Scan a previously specified BSSID " MACSTR,
1484
0
        MAC2STR(params.bssid));
1485
0
    }
1486
0
  } else if (!is_zero_ether_addr(wpa_s->ml_probe_bssid)) {
1487
0
    wpa_printf(MSG_DEBUG, "Scanning for ML probe request");
1488
0
    params.bssid = wpa_s->ml_probe_bssid;
1489
0
    params.min_probe_req_content = true;
1490
0
  }
1491
1492
1493
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1494
0
      wpa_s->manual_non_coloc_6ghz) {
1495
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Collocated 6 GHz logic is disabled");
1496
0
    params.non_coloc_6ghz = 1;
1497
0
  }
1498
1499
0
  scan_params = &params;
1500
1501
0
scan:
1502
#ifdef CONFIG_P2P
1503
  /*
1504
   * If the driver does not support multi-channel concurrency and a
1505
   * virtual interface that shares the same radio with the wpa_s interface
1506
   * is operating there may not be need to scan other channels apart from
1507
   * the current operating channel on the other virtual interface. Filter
1508
   * out other channels in case we are trying to find a connection for a
1509
   * station interface when we are not configured to prefer station
1510
   * connection and a concurrent operation is already in process.
1511
   */
1512
  if (wpa_s->scan_for_connection &&
1513
      wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
1514
      !scan_params->freqs && !params.freqs &&
1515
      wpas_is_p2p_prioritized(wpa_s) &&
1516
      wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1517
      non_p2p_network_enabled(wpa_s)) {
1518
    unsigned int num = wpa_s->num_multichan_concurrent;
1519
1520
    params.freqs = os_calloc(num + 1, sizeof(int));
1521
    if (params.freqs) {
1522
      /*
1523
       * Exclude the operating frequency of the current
1524
       * interface since we're looking to transition off of
1525
       * it.
1526
       */
1527
      num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1528
                 true);
1529
      if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1530
        wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1531
      } else {
1532
        os_free(params.freqs);
1533
        params.freqs = NULL;
1534
      }
1535
    }
1536
  }
1537
1538
  if (!params.freqs && wpas_is_6ghz_supported(wpa_s, true) &&
1539
      (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning))
1540
    wpas_p2p_scan_freqs(wpa_s, &params, true);
1541
#endif /* CONFIG_P2P */
1542
1543
0
  ret = wpa_supplicant_trigger_scan(wpa_s, scan_params, false, false);
1544
1545
0
  if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1546
0
      !wpa_s->manual_scan_freqs) {
1547
    /* Restore manual_scan_freqs for the next attempt */
1548
0
    wpa_s->manual_scan_freqs = params.freqs;
1549
0
    params.freqs = NULL;
1550
0
  }
1551
1552
0
  wpabuf_free(extra_ie);
1553
0
  os_free(params.freqs);
1554
0
  os_free(params.filter_ssids);
1555
0
  os_free(params.mac_addr);
1556
1557
0
  if (ret) {
1558
0
    wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
1559
0
    if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1560
0
      wpa_supplicant_set_state(wpa_s,
1561
0
             wpa_s->scan_prev_wpa_state);
1562
    /* Restore scan_req since we will try to scan again */
1563
0
    wpa_s->scan_req = wpa_s->last_scan_req;
1564
0
    wpa_supplicant_req_scan(wpa_s, 1, 0);
1565
0
  } else {
1566
0
    wpa_s->scan_for_connection = 0;
1567
0
#ifdef CONFIG_INTERWORKING
1568
0
    wpa_s->interworking_fast_assoc_tried = 0;
1569
0
#endif /* CONFIG_INTERWORKING */
1570
0
    wpa_s->next_scan_bssid_wildcard_ssid = 0;
1571
0
    if (params.bssid)
1572
0
      os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
1573
0
  }
1574
1575
0
  wpa_s->ml_probe_mld_id = -1;
1576
0
  wpa_s->ml_probe_links = 0;
1577
0
  os_memset(wpa_s->ml_probe_bssid, 0, sizeof(wpa_s->ml_probe_bssid));
1578
0
}
1579
1580
1581
void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1582
0
{
1583
0
  struct os_reltime remaining, new_int;
1584
0
  int cancelled;
1585
1586
0
  cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1587
0
               &remaining);
1588
1589
0
  new_int.sec = sec;
1590
0
  new_int.usec = 0;
1591
0
  if (cancelled && os_reltime_before(&remaining, &new_int)) {
1592
0
    new_int.sec = remaining.sec;
1593
0
    new_int.usec = remaining.usec;
1594
0
  }
1595
1596
0
  if (cancelled) {
1597
0
    eloop_register_timeout(new_int.sec, new_int.usec,
1598
0
               wpa_supplicant_scan, wpa_s, NULL);
1599
0
  }
1600
0
  wpa_s->scan_interval = sec;
1601
0
}
1602
1603
1604
/**
1605
 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1606
 * @wpa_s: Pointer to wpa_supplicant data
1607
 * @sec: Number of seconds after which to scan
1608
 * @usec: Number of microseconds after which to scan
1609
 *
1610
 * This function is used to schedule a scan for neighboring access points after
1611
 * the specified time.
1612
 */
1613
void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1614
914
{
1615
914
  int res;
1616
1617
914
  if (wpa_s->p2p_mgmt) {
1618
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1619
0
      "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1620
0
      sec, usec);
1621
0
    return;
1622
0
  }
1623
1624
914
  res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1625
914
            NULL);
1626
914
  if (res == 1) {
1627
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1628
0
      sec, usec);
1629
914
  } else if (res == 0) {
1630
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1631
0
      sec, usec);
1632
914
  } else {
1633
914
    wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1634
914
      sec, usec);
1635
914
    eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1636
914
  }
1637
914
}
1638
1639
1640
/**
1641
 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1642
 * @wpa_s: Pointer to wpa_supplicant data
1643
 * @sec: Number of seconds after which to scan
1644
 * @usec: Number of microseconds after which to scan
1645
 * Returns: 0 on success or -1 otherwise
1646
 *
1647
 * This function is used to schedule periodic scans for neighboring
1648
 * access points after the specified time.
1649
 */
1650
int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1651
              int sec, int usec)
1652
0
{
1653
0
  if (!wpa_s->sched_scan_supported)
1654
0
    return -1;
1655
1656
0
  eloop_register_timeout(sec, usec,
1657
0
             wpa_supplicant_delayed_sched_scan_timeout,
1658
0
             wpa_s, NULL);
1659
1660
0
  return 0;
1661
0
}
1662
1663
1664
static void
1665
wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1666
          struct wpa_driver_scan_params *params)
1667
0
{
1668
0
  if (wpa_s->wpa_state != WPA_COMPLETED ||
1669
0
      !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1670
0
      wpa_s->srp.relative_rssi_set == 0)
1671
0
    return;
1672
1673
0
  params->relative_rssi_set = 1;
1674
0
  params->relative_rssi = wpa_s->srp.relative_rssi;
1675
1676
0
  if (wpa_s->srp.relative_adjust_rssi == 0)
1677
0
    return;
1678
1679
0
  params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1680
0
  params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1681
0
}
1682
1683
1684
/**
1685
 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1686
 * @wpa_s: Pointer to wpa_supplicant data
1687
 * Returns: 0 is sched_scan was started or -1 otherwise
1688
 *
1689
 * This function is used to schedule periodic scans for neighboring
1690
 * access points repeating the scan continuously.
1691
 */
1692
int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1693
0
{
1694
0
  struct wpa_driver_scan_params params;
1695
0
  struct wpa_driver_scan_params *scan_params;
1696
0
  enum wpa_states prev_state;
1697
0
  struct wpa_ssid *ssid = NULL;
1698
0
  struct wpabuf *extra_ie = NULL;
1699
0
  int ret;
1700
0
  unsigned int max_sched_scan_ssids;
1701
0
  int wildcard = 0;
1702
0
  int need_ssids;
1703
0
  struct sched_scan_plan scan_plan;
1704
1705
0
  if (!wpa_s->sched_scan_supported)
1706
0
    return -1;
1707
1708
0
  if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1709
0
    max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1710
0
  else
1711
0
    max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1712
0
  if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1713
0
    return -1;
1714
1715
0
  wpa_s->sched_scan_stop_req = 0;
1716
1717
0
  if (wpa_s->sched_scanning) {
1718
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1719
0
    return 0;
1720
0
  }
1721
1722
0
  need_ssids = 0;
1723
0
  for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1724
0
    if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1725
      /* Use wildcard SSID to find this network */
1726
0
      wildcard = 1;
1727
0
    } else if (!wpas_network_disabled(wpa_s, ssid) &&
1728
0
         ssid->ssid_len)
1729
0
      need_ssids++;
1730
1731
#ifdef CONFIG_WPS
1732
    if (!wpas_network_disabled(wpa_s, ssid) &&
1733
        ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1734
      /*
1735
       * Normal scan is more reliable and faster for WPS
1736
       * operations and since these are for short periods of
1737
       * time, the benefit of trying to use sched_scan would
1738
       * be limited.
1739
       */
1740
      wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1741
        "sched_scan for WPS");
1742
      return -1;
1743
    }
1744
#endif /* CONFIG_WPS */
1745
0
  }
1746
0
  if (wildcard)
1747
0
    need_ssids++;
1748
1749
0
  if (wpa_s->normal_scans < 3 &&
1750
0
      (need_ssids <= wpa_s->max_scan_ssids ||
1751
0
       wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1752
    /*
1753
     * When normal scan can speed up operations, use that for the
1754
     * first operations before starting the sched_scan to allow
1755
     * user space sleep more. We do this only if the normal scan
1756
     * has functionality that is suitable for this or if the
1757
     * sched_scan does not have better support for multiple SSIDs.
1758
     */
1759
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1760
0
      "sched_scan for initial scans (normal_scans=%d)",
1761
0
      wpa_s->normal_scans);
1762
0
    return -1;
1763
0
  }
1764
1765
0
  os_memset(&params, 0, sizeof(params));
1766
1767
  /* If we can't allocate space for the filters, we just don't filter */
1768
0
  params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1769
0
          sizeof(struct wpa_driver_scan_filter));
1770
1771
0
  prev_state = wpa_s->wpa_state;
1772
0
  if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1773
0
      wpa_s->wpa_state == WPA_INACTIVE)
1774
0
    wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1775
1776
0
  if (wpa_s->autoscan_params != NULL) {
1777
0
    scan_params = wpa_s->autoscan_params;
1778
0
    goto scan;
1779
0
  }
1780
1781
  /* Find the starting point from which to continue scanning */
1782
0
  ssid = wpa_s->conf->ssid;
1783
0
  if (wpa_s->prev_sched_ssid) {
1784
0
    while (ssid) {
1785
0
      if (ssid == wpa_s->prev_sched_ssid) {
1786
0
        ssid = ssid->next;
1787
0
        break;
1788
0
      }
1789
0
      ssid = ssid->next;
1790
0
    }
1791
0
  }
1792
1793
0
  if (!ssid || !wpa_s->prev_sched_ssid) {
1794
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1795
0
    wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1796
0
    wpa_s->first_sched_scan = 1;
1797
0
    ssid = wpa_s->conf->ssid;
1798
0
    wpa_s->prev_sched_ssid = ssid;
1799
0
  }
1800
1801
0
  if (wildcard) {
1802
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1803
0
    params.num_ssids++;
1804
0
  }
1805
1806
0
  while (ssid) {
1807
0
    if (wpas_network_disabled(wpa_s, ssid))
1808
0
      goto next;
1809
1810
0
    if (params.num_filter_ssids < wpa_s->max_match_sets &&
1811
0
        params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1812
0
      wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1813
0
        wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1814
0
      os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1815
0
          ssid->ssid, ssid->ssid_len);
1816
0
      params.filter_ssids[params.num_filter_ssids].ssid_len =
1817
0
        ssid->ssid_len;
1818
0
      params.num_filter_ssids++;
1819
0
    } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1820
0
    {
1821
0
      wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1822
0
        "filter for sched_scan - drop filter");
1823
0
      os_free(params.filter_ssids);
1824
0
      params.filter_ssids = NULL;
1825
0
      params.num_filter_ssids = 0;
1826
0
    }
1827
1828
0
    if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1829
0
      if (params.num_ssids == max_sched_scan_ssids)
1830
0
        break; /* only room for broadcast SSID */
1831
0
      wpa_dbg(wpa_s, MSG_DEBUG,
1832
0
        "add to active scan ssid: %s",
1833
0
        wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1834
0
      params.ssids[params.num_ssids].ssid =
1835
0
        ssid->ssid;
1836
0
      params.ssids[params.num_ssids].ssid_len =
1837
0
        ssid->ssid_len;
1838
0
      params.num_ssids++;
1839
0
      if (params.num_ssids >= max_sched_scan_ssids) {
1840
0
        wpa_s->prev_sched_ssid = ssid;
1841
0
        do {
1842
0
          ssid = ssid->next;
1843
0
        } while (ssid &&
1844
0
           (wpas_network_disabled(wpa_s, ssid) ||
1845
0
            !ssid->scan_ssid));
1846
0
        break;
1847
0
      }
1848
0
    }
1849
1850
0
  next:
1851
0
    wpa_s->prev_sched_ssid = ssid;
1852
0
    ssid = ssid->next;
1853
0
  }
1854
1855
0
  if (params.num_filter_ssids == 0) {
1856
0
    os_free(params.filter_ssids);
1857
0
    params.filter_ssids = NULL;
1858
0
  }
1859
1860
0
  extra_ie = wpa_supplicant_extra_ies(wpa_s);
1861
0
  if (extra_ie) {
1862
0
    params.extra_ies = wpabuf_head(extra_ie);
1863
0
    params.extra_ies_len = wpabuf_len(extra_ie);
1864
0
  }
1865
1866
0
  if (wpa_s->conf->filter_rssi)
1867
0
    params.filter_rssi = wpa_s->conf->filter_rssi;
1868
1869
  /* See if user specified frequencies. If so, scan only those. */
1870
0
  if (wpa_s->conf->freq_list && !params.freqs) {
1871
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1872
0
      "Optimize scan based on conf->freq_list");
1873
0
    int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1874
0
  }
1875
1876
#ifdef CONFIG_MBO
1877
  if (wpa_s->enable_oce & OCE_STA)
1878
    params.oce_scan = 1;
1879
#endif /* CONFIG_MBO */
1880
1881
0
  scan_params = &params;
1882
1883
0
scan:
1884
0
  wpa_s->sched_scan_timed_out = 0;
1885
1886
  /*
1887
   * We cannot support multiple scan plans if the scan request includes
1888
   * too many SSID's, so in this case use only the last scan plan and make
1889
   * it run infinitely. It will be stopped by the timeout.
1890
   */
1891
0
  if (wpa_s->sched_scan_plans_num == 1 ||
1892
0
      (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1893
0
    params.sched_scan_plans = wpa_s->sched_scan_plans;
1894
0
    params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1895
0
  } else if (wpa_s->sched_scan_plans_num > 1) {
1896
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1897
0
      "Too many SSIDs. Default to using single scheduled_scan plan");
1898
0
    params.sched_scan_plans =
1899
0
      &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1900
0
             1];
1901
0
    params.sched_scan_plans_num = 1;
1902
0
  } else {
1903
0
    if (wpa_s->conf->sched_scan_interval)
1904
0
      scan_plan.interval = wpa_s->conf->sched_scan_interval;
1905
0
    else
1906
0
      scan_plan.interval = 10;
1907
1908
0
    if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1909
0
      wpa_printf(MSG_WARNING,
1910
0
           "Scan interval too long(%u), use the maximum allowed(%u)",
1911
0
           scan_plan.interval,
1912
0
           wpa_s->max_sched_scan_plan_interval);
1913
0
      scan_plan.interval =
1914
0
        wpa_s->max_sched_scan_plan_interval;
1915
0
    }
1916
1917
0
    scan_plan.iterations = 0;
1918
0
    params.sched_scan_plans = &scan_plan;
1919
0
    params.sched_scan_plans_num = 1;
1920
0
  }
1921
1922
0
  params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1923
1924
0
  if (ssid || !wpa_s->first_sched_scan) {
1925
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1926
0
      "Starting sched scan after %u seconds: interval %u timeout %d",
1927
0
      params.sched_scan_start_delay,
1928
0
      params.sched_scan_plans[0].interval,
1929
0
      wpa_s->sched_scan_timeout);
1930
0
  } else {
1931
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1932
0
      "Starting sched scan after %u seconds (no timeout)",
1933
0
      params.sched_scan_start_delay);
1934
0
  }
1935
1936
0
  wpa_setband_scan_freqs(wpa_s, scan_params);
1937
1938
0
  if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
1939
0
      wpa_s->wpa_state <= WPA_SCANNING)
1940
0
    wpa_setup_mac_addr_rand_params(&params,
1941
0
                 wpa_s->mac_addr_sched_scan);
1942
1943
0
  wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1944
1945
0
  ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
1946
0
  wpabuf_free(extra_ie);
1947
0
  os_free(params.filter_ssids);
1948
0
  os_free(params.mac_addr);
1949
0
  if (ret) {
1950
0
    wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1951
0
    if (prev_state != wpa_s->wpa_state)
1952
0
      wpa_supplicant_set_state(wpa_s, prev_state);
1953
0
    return ret;
1954
0
  }
1955
1956
  /* If we have more SSIDs to scan, add a timeout so we scan them too */
1957
0
  if (ssid || !wpa_s->first_sched_scan) {
1958
0
    wpa_s->sched_scan_timed_out = 0;
1959
0
    eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1960
0
               wpa_supplicant_sched_scan_timeout,
1961
0
               wpa_s, NULL);
1962
0
    wpa_s->first_sched_scan = 0;
1963
0
    wpa_s->sched_scan_timeout /= 2;
1964
0
    params.sched_scan_plans[0].interval *= 2;
1965
0
    if ((unsigned int) wpa_s->sched_scan_timeout <
1966
0
        params.sched_scan_plans[0].interval ||
1967
0
        params.sched_scan_plans[0].interval >
1968
0
        wpa_s->max_sched_scan_plan_interval) {
1969
0
      params.sched_scan_plans[0].interval = 10;
1970
0
      wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1971
0
    }
1972
0
  }
1973
1974
  /* If there is no more ssids, start next time from the beginning */
1975
0
  if (!ssid)
1976
0
    wpa_s->prev_sched_ssid = NULL;
1977
1978
0
  return 0;
1979
0
}
1980
1981
1982
/**
1983
 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1984
 * @wpa_s: Pointer to wpa_supplicant data
1985
 *
1986
 * This function is used to cancel a scan request scheduled with
1987
 * wpa_supplicant_req_scan().
1988
 */
1989
void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1990
0
{
1991
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1992
0
  eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1993
0
}
1994
1995
1996
/**
1997
 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1998
 * @wpa_s: Pointer to wpa_supplicant data
1999
 *
2000
 * This function is used to stop a delayed scheduled scan.
2001
 */
2002
void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
2003
0
{
2004
0
  if (!wpa_s->sched_scan_supported)
2005
0
    return;
2006
2007
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
2008
0
  eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
2009
0
           wpa_s, NULL);
2010
0
}
2011
2012
2013
/**
2014
 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
2015
 * @wpa_s: Pointer to wpa_supplicant data
2016
 *
2017
 * This function is used to stop a periodic scheduled scan.
2018
 */
2019
void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
2020
0
{
2021
0
  if (!wpa_s->sched_scanning)
2022
0
    return;
2023
2024
0
  if (wpa_s->sched_scanning)
2025
0
    wpa_s->sched_scan_stop_req = 1;
2026
2027
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
2028
0
  eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
2029
0
  wpa_supplicant_stop_sched_scan(wpa_s);
2030
0
}
2031
2032
2033
/**
2034
 * wpa_supplicant_notify_scanning - Indicate possible scan state change
2035
 * @wpa_s: Pointer to wpa_supplicant data
2036
 * @scanning: Whether scanning is currently in progress
2037
 *
2038
 * This function is to generate scanning notifycations. It is called whenever
2039
 * there may have been a change in scanning (scan started, completed, stopped).
2040
 * wpas_notify_scanning() is called whenever the scanning state changed from the
2041
 * previously notified state.
2042
 */
2043
void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
2044
            int scanning)
2045
0
{
2046
0
  if (wpa_s->scanning != scanning) {
2047
0
    wpa_s->scanning = scanning;
2048
0
    wpas_notify_scanning(wpa_s);
2049
0
  }
2050
0
}
2051
2052
2053
static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
2054
0
{
2055
0
  int rate = 0;
2056
0
  const u8 *ie;
2057
0
  int i;
2058
2059
0
  ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
2060
0
  for (i = 0; ie && i < ie[1]; i++) {
2061
0
    if ((ie[i + 2] & 0x7f) > rate)
2062
0
      rate = ie[i + 2] & 0x7f;
2063
0
  }
2064
2065
0
  ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
2066
0
  for (i = 0; ie && i < ie[1]; i++) {
2067
0
    if ((ie[i + 2] & 0x7f) > rate)
2068
0
      rate = ie[i + 2] & 0x7f;
2069
0
  }
2070
2071
0
  return rate;
2072
0
}
2073
2074
2075
/**
2076
 * wpa_scan_get_ie - Fetch a specified information element from a scan result
2077
 * @res: Scan result entry
2078
 * @ie: Information element identitifier (WLAN_EID_*)
2079
 * Returns: Pointer to the information element (id field) or %NULL if not found
2080
 *
2081
 * This function returns the first matching information element in the scan
2082
 * result.
2083
 */
2084
const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
2085
0
{
2086
0
  size_t ie_len = res->ie_len;
2087
2088
  /* Use the Beacon frame IEs if res->ie_len is not available */
2089
0
  if (!ie_len)
2090
0
    ie_len = res->beacon_ie_len;
2091
2092
0
  return get_ie((const u8 *) (res + 1), ie_len, ie);
2093
0
}
2094
2095
2096
const u8 * wpa_scan_get_ml_ie(const struct wpa_scan_res *res, u8 type)
2097
0
{
2098
0
  size_t ie_len = res->ie_len;
2099
2100
  /* Use the Beacon frame IEs if res->ie_len is not available */
2101
0
  if (!ie_len)
2102
0
    ie_len = res->beacon_ie_len;
2103
2104
0
  return get_ml_ie((const u8 *) (res + 1), ie_len, type);
2105
0
}
2106
2107
2108
/**
2109
 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
2110
 * @res: Scan result entry
2111
 * @vendor_type: Vendor type (four octets starting the IE payload)
2112
 * Returns: Pointer to the information element (id field) or %NULL if not found
2113
 *
2114
 * This function returns the first matching information element in the scan
2115
 * result.
2116
 */
2117
const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
2118
          u32 vendor_type)
2119
0
{
2120
0
  const u8 *ies;
2121
0
  const struct element *elem;
2122
2123
0
  ies = (const u8 *) (res + 1);
2124
2125
0
  for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
2126
0
    if (elem->datalen >= 4 &&
2127
0
        vendor_type == WPA_GET_BE32(elem->data))
2128
0
      return &elem->id;
2129
0
  }
2130
2131
0
  return NULL;
2132
0
}
2133
2134
2135
/**
2136
 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
2137
 * @res: Scan result entry
2138
 * @vendor_type: Vendor type (four octets starting the IE payload)
2139
 * Returns: Pointer to the information element (id field) or %NULL if not found
2140
 *
2141
 * This function returns the first matching information element in the scan
2142
 * result.
2143
 *
2144
 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
2145
 * from Beacon frames instead of either Beacon or Probe Response frames.
2146
 */
2147
const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
2148
           u32 vendor_type)
2149
0
{
2150
0
  const u8 *ies;
2151
0
  const struct element *elem;
2152
2153
0
  if (res->beacon_ie_len == 0)
2154
0
    return NULL;
2155
2156
0
  ies = (const u8 *) (res + 1);
2157
0
  ies += res->ie_len;
2158
2159
0
  for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
2160
0
          res->beacon_ie_len) {
2161
0
    if (elem->datalen >= 4 &&
2162
0
        vendor_type == WPA_GET_BE32(elem->data))
2163
0
      return &elem->id;
2164
0
  }
2165
2166
0
  return NULL;
2167
0
}
2168
2169
2170
/**
2171
 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
2172
 * @res: Scan result entry
2173
 * @vendor_type: Vendor type (four octets starting the IE payload)
2174
 * Returns: Pointer to the information element payload or %NULL if not found
2175
 *
2176
 * This function returns concatenated payload of possibly fragmented vendor
2177
 * specific information elements in the scan result. The caller is responsible
2178
 * for freeing the returned buffer.
2179
 */
2180
struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2181
               u32 vendor_type)
2182
0
{
2183
0
  struct wpabuf *buf;
2184
0
  const u8 *end, *pos;
2185
2186
0
  buf = wpabuf_alloc(res->ie_len);
2187
0
  if (buf == NULL)
2188
0
    return NULL;
2189
2190
0
  pos = (const u8 *) (res + 1);
2191
0
  end = pos + res->ie_len;
2192
2193
0
  while (end - pos > 1) {
2194
0
    u8 ie, len;
2195
2196
0
    ie = pos[0];
2197
0
    len = pos[1];
2198
0
    if (len > end - pos - 2)
2199
0
      break;
2200
0
    pos += 2;
2201
0
    if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2202
0
        vendor_type == WPA_GET_BE32(pos))
2203
0
      wpabuf_put_data(buf, pos + 4, len - 4);
2204
0
    pos += len;
2205
0
  }
2206
2207
0
  if (wpabuf_len(buf) == 0) {
2208
0
    wpabuf_free(buf);
2209
0
    buf = NULL;
2210
0
  }
2211
2212
0
  return buf;
2213
0
}
2214
2215
2216
static int wpas_channel_width_offset(enum chan_width cw)
2217
0
{
2218
0
  switch (cw) {
2219
0
  case CHAN_WIDTH_40:
2220
0
    return 1;
2221
0
  case CHAN_WIDTH_80:
2222
0
    return 2;
2223
0
  case CHAN_WIDTH_80P80:
2224
0
  case CHAN_WIDTH_160:
2225
0
    return 3;
2226
0
  case CHAN_WIDTH_320:
2227
0
    return 4;
2228
0
  default:
2229
0
    return 0;
2230
0
  }
2231
0
}
2232
2233
2234
/**
2235
 * wpas_channel_width_tx_pwr - Calculate the max transmit power at the channel
2236
 * width
2237
 * @ies: Information elements
2238
 * @ies_len: Length of elements
2239
 * @cw: The channel width
2240
 * Returns: The max transmit power at the channel width, TX_POWER_NO_CONSTRAINT
2241
 * if it is not constrained.
2242
 *
2243
 * This function is only used to estimate the actual signal RSSI when associated
2244
 * based on the beacon RSSI at the STA. Beacon frames are transmitted on 20 MHz
2245
 * channels, while the Data frames usually use higher channel width. Therefore
2246
 * their RSSIs may be different. Assuming there is a fixed gap between the TX
2247
 * power limit of the STA defined by the Transmit Power Envelope element and the
2248
 * TX power of the AP, the difference in the TX power of X MHz and Y MHz at the
2249
 * STA equals to the difference at the AP, and the difference in the signal RSSI
2250
 * at the STA. tx_pwr is a floating point number in the standard, but the error
2251
 * of casting to int is trivial in comparing two BSSes.
2252
 */
2253
static int wpas_channel_width_tx_pwr(const u8 *ies, size_t ies_len,
2254
             enum chan_width cw)
2255
0
{
2256
0
  int offset = wpas_channel_width_offset(cw);
2257
0
  const struct element *elem;
2258
0
  int max_tx_power = TX_POWER_NO_CONSTRAINT, tx_pwr = 0;
2259
2260
0
  for_each_element_id(elem, WLAN_EID_TRANSMIT_POWER_ENVELOPE, ies,
2261
0
          ies_len) {
2262
0
    int max_tx_pwr_count;
2263
0
    enum max_tx_pwr_interpretation tx_pwr_intrpn;
2264
0
    enum reg_6g_client_type client_type;
2265
2266
0
    if (elem->datalen < 1)
2267
0
      continue;
2268
2269
    /*
2270
     * IEEE Std 802.11ax-2021, 9.4.2.161 (Transmit Power Envelope
2271
     * element) defines Maximum Transmit Power Count (B0-B2),
2272
     * Maximum Transmit Power Interpretation (B3-B5), and Maximum
2273
     * Transmit Power Category (B6-B7).
2274
     */
2275
0
    max_tx_pwr_count = elem->data[0] & 0x07;
2276
0
    tx_pwr_intrpn = (elem->data[0] >> 3) & 0x07;
2277
0
    client_type = (elem->data[0] >> 6) & 0x03;
2278
2279
0
    if (client_type != REG_DEFAULT_CLIENT)
2280
0
      continue;
2281
2282
0
    if (tx_pwr_intrpn == LOCAL_EIRP ||
2283
0
        tx_pwr_intrpn == REGULATORY_CLIENT_EIRP) {
2284
0
      int offs;
2285
2286
0
      max_tx_pwr_count = MIN(max_tx_pwr_count, 3);
2287
0
      offs = MIN(offset, max_tx_pwr_count) + 1;
2288
0
      if (elem->datalen <= offs)
2289
0
        continue;
2290
0
      tx_pwr = (signed char) elem->data[offs];
2291
      /*
2292
       * Maximum Transmit Power subfield is encoded as an
2293
       * 8-bit 2s complement signed integer in the range -64
2294
       * dBm to 63 dBm with a 0.5 dB step. 63.5 dBm means no
2295
       * local maximum transmit power constraint.
2296
       */
2297
0
      if (tx_pwr == 127)
2298
0
        continue;
2299
0
      tx_pwr /= 2;
2300
0
      max_tx_power = MIN(max_tx_power, tx_pwr);
2301
0
    } else if (tx_pwr_intrpn == LOCAL_EIRP_PSD ||
2302
0
         tx_pwr_intrpn == REGULATORY_CLIENT_EIRP_PSD) {
2303
0
      if (elem->datalen < 2)
2304
0
        continue;
2305
2306
0
      tx_pwr = (signed char) elem->data[1];
2307
      /*
2308
       * Maximum Transmit PSD subfield is encoded as an 8-bit
2309
       * 2s complement signed integer. -128 indicates that the
2310
       * corresponding 20 MHz channel cannot be used for
2311
       * transmission. +127 indicates that no maximum PSD
2312
       * limit is specified for the corresponding 20 MHz
2313
       * channel.
2314
       */
2315
0
      if (tx_pwr == 127 || tx_pwr == -128)
2316
0
        continue;
2317
2318
      /*
2319
       * The Maximum Transmit PSD subfield indicates the
2320
       * maximum transmit PSD for the 20 MHz channel. Suppose
2321
       * the PSD value is X dBm/MHz, the TX power of N MHz is
2322
       * X + 10*log10(N) = X + 10*log10(20) + 10*log10(N/20) =
2323
       * X + 13 + 3*log2(N/20)
2324
       */
2325
0
      tx_pwr = tx_pwr / 2 + 13 + offset * 3;
2326
0
      max_tx_power = MIN(max_tx_power, tx_pwr);
2327
0
    }
2328
0
  }
2329
2330
0
  return max_tx_power;
2331
0
}
2332
2333
2334
/**
2335
 * Estimate the RSSI bump of channel width |cw| with respect to 20 MHz channel.
2336
 * If the TX power has no constraint, it is unable to estimate the RSSI bump.
2337
 */
2338
int wpas_channel_width_rssi_bump(const u8 *ies, size_t ies_len,
2339
         enum chan_width cw)
2340
0
{
2341
0
  int max_20mhz_tx_pwr = wpas_channel_width_tx_pwr(ies, ies_len,
2342
0
               CHAN_WIDTH_20);
2343
0
  int max_cw_tx_pwr = wpas_channel_width_tx_pwr(ies, ies_len, cw);
2344
2345
0
  return (max_20mhz_tx_pwr == TX_POWER_NO_CONSTRAINT ||
2346
0
    max_cw_tx_pwr == TX_POWER_NO_CONSTRAINT) ?
2347
0
    0 : (max_cw_tx_pwr - max_20mhz_tx_pwr);
2348
0
}
2349
2350
2351
int wpas_adjust_snr_by_chanwidth(const u8 *ies, size_t ies_len,
2352
         enum chan_width max_cw, int snr)
2353
0
{
2354
0
  int rssi_bump = wpas_channel_width_rssi_bump(ies, ies_len, max_cw);
2355
  /*
2356
   * The noise has uniform power spectral density (PSD) across the
2357
   * frequency band, its power is proportional to the channel width.
2358
   * Suppose the PSD of noise is X dBm/MHz, the noise power of N MHz is
2359
   * X + 10*log10(N), and the noise power bump with respect to 20 MHz is
2360
   * 10*log10(N) - 10*log10(20) = 10*log10(N/20) = 3*log2(N/20)
2361
   */
2362
0
  int noise_bump = 3 * wpas_channel_width_offset(max_cw);
2363
2364
0
  return snr + rssi_bump - noise_bump;
2365
0
}
2366
2367
2368
/* Compare function for sorting scan results. Return >0 if @b is considered
2369
 * better. */
2370
static int wpa_scan_result_compar(const void *a, const void *b)
2371
0
{
2372
0
  struct wpa_scan_res **_wa = (void *) a;
2373
0
  struct wpa_scan_res **_wb = (void *) b;
2374
0
  struct wpa_scan_res *wa = *_wa;
2375
0
  struct wpa_scan_res *wb = *_wb;
2376
0
  int wpa_a, wpa_b;
2377
0
  int snr_a, snr_b, snr_a_full, snr_b_full;
2378
0
  size_t ies_len;
2379
0
#ifndef CONFIG_NO_WPA
2380
0
  const u8 *rsne_a, *rsne_b;
2381
0
#endif /* CONFIG_NO_WPA */
2382
2383
  /* WPA/WPA2 support preferred */
2384
0
  wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2385
0
    wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2386
0
  wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2387
0
    wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2388
2389
0
  if (wpa_b && !wpa_a)
2390
0
    return 1;
2391
0
  if (!wpa_b && wpa_a)
2392
0
    return -1;
2393
2394
  /* privacy support preferred */
2395
0
  if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2396
0
      (wb->caps & IEEE80211_CAP_PRIVACY))
2397
0
    return 1;
2398
0
  if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2399
0
      (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2400
0
    return -1;
2401
2402
0
  if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
2403
    /*
2404
     * The scan result estimates SNR over 20 MHz, while Data frames
2405
     * usually use wider channel width. The TX power and noise power
2406
     * are both affected by the channel width.
2407
     */
2408
0
    ies_len = wa->ie_len ? wa->ie_len : wa->beacon_ie_len;
2409
0
    snr_a_full = wpas_adjust_snr_by_chanwidth((const u8 *) (wa + 1),
2410
0
                ies_len, wa->max_cw,
2411
0
                wa->snr);
2412
0
    snr_a = MIN(snr_a_full, GREAT_SNR);
2413
0
    ies_len = wb->ie_len ? wb->ie_len : wb->beacon_ie_len;
2414
0
    snr_b_full = wpas_adjust_snr_by_chanwidth((const u8 *) (wb + 1),
2415
0
                ies_len, wb->max_cw,
2416
0
                wb->snr);
2417
0
    snr_b = MIN(snr_b_full, GREAT_SNR);
2418
0
  } else {
2419
    /* Level is not in dBm, so we can't calculate
2420
     * SNR. Just use raw level (units unknown). */
2421
0
    snr_a = snr_a_full = wa->level;
2422
0
    snr_b = snr_b_full = wb->level;
2423
0
  }
2424
2425
0
#ifndef CONFIG_NO_WPA
2426
  /* If SNR of a SAE BSS is good or at least as high as the PSK BSS,
2427
   * prefer SAE over PSK for mixed WPA3-Personal transition mode and
2428
   * WPA2-Personal deployments */
2429
0
  rsne_a = wpa_scan_get_ie(wa, WLAN_EID_RSN);
2430
0
  rsne_b = wpa_scan_get_ie(wb, WLAN_EID_RSN);
2431
0
  if (rsne_a && rsne_b) {
2432
0
    struct wpa_ie_data data;
2433
0
    bool psk_a = false, psk_b = false, sae_a = false, sae_b = false;
2434
2435
0
    if (wpa_parse_wpa_ie_rsn(rsne_a, 2 + rsne_a[1], &data) == 0) {
2436
0
      psk_a = wpa_key_mgmt_wpa_psk_no_sae(data.key_mgmt);
2437
0
      sae_a = wpa_key_mgmt_sae(data.key_mgmt);
2438
0
    }
2439
0
    if (wpa_parse_wpa_ie_rsn(rsne_b, 2 + rsne_b[1], &data) == 0) {
2440
0
      psk_b = wpa_key_mgmt_wpa_psk_no_sae(data.key_mgmt);
2441
0
      sae_b = wpa_key_mgmt_sae(data.key_mgmt);
2442
0
    }
2443
2444
0
    if (sae_a && !sae_b && psk_b &&
2445
0
        (snr_a >= GREAT_SNR || snr_a >= snr_b))
2446
0
      return -1;
2447
0
    if (sae_b && !sae_a && psk_a &&
2448
0
        (snr_b >= GREAT_SNR || snr_b >= snr_a))
2449
0
      return 1;
2450
0
  }
2451
0
#endif /* CONFIG_NO_WPA */
2452
2453
  /* If SNR is close, decide by max rate or frequency band. For cases
2454
   * involving the 6 GHz band, use the throughput estimate irrespective
2455
   * of the SNR difference since the LPI/VLP rules may result in
2456
   * significant differences in SNR for cases where the estimated
2457
   * throughput can be considerably higher with the lower SNR. */
2458
0
  if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2459
0
             is_6ghz_freq(wa->freq) ||
2460
0
             is_6ghz_freq(wb->freq))) {
2461
0
    if (wa->est_throughput != wb->est_throughput)
2462
0
      return (int) wb->est_throughput -
2463
0
        (int) wa->est_throughput;
2464
0
  }
2465
0
  if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2466
0
      (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
2467
0
    if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2468
0
      return is_6ghz_freq(wa->freq) ? -1 : 1;
2469
0
    if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2470
0
      return IS_5GHZ(wa->freq) ? -1 : 1;
2471
0
  }
2472
2473
  /* all things being equal, use SNR; if SNRs are
2474
   * identical, use quality values since some drivers may only report
2475
   * that value and leave the signal level zero */
2476
0
  if (snr_b_full == snr_a_full)
2477
0
    return wb->qual - wa->qual;
2478
0
  return snr_b_full - snr_a_full;
2479
0
}
2480
2481
2482
#ifdef CONFIG_WPS
2483
/* Compare function for sorting scan results when searching a WPS AP for
2484
 * provisioning. Return >0 if @b is considered better. */
2485
static int wpa_scan_result_wps_compar(const void *a, const void *b)
2486
{
2487
  struct wpa_scan_res **_wa = (void *) a;
2488
  struct wpa_scan_res **_wb = (void *) b;
2489
  struct wpa_scan_res *wa = *_wa;
2490
  struct wpa_scan_res *wb = *_wb;
2491
  int uses_wps_a, uses_wps_b;
2492
  struct wpabuf *wps_a, *wps_b;
2493
  int res;
2494
2495
  /* Optimization - check WPS IE existence before allocated memory and
2496
   * doing full reassembly. */
2497
  uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2498
  uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2499
  if (uses_wps_a && !uses_wps_b)
2500
    return -1;
2501
  if (!uses_wps_a && uses_wps_b)
2502
    return 1;
2503
2504
  if (uses_wps_a && uses_wps_b) {
2505
    wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2506
    wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2507
    res = wps_ap_priority_compar(wps_a, wps_b);
2508
    wpabuf_free(wps_a);
2509
    wpabuf_free(wps_b);
2510
    if (res)
2511
      return res;
2512
  }
2513
2514
  /*
2515
   * Do not use current AP security policy as a sorting criteria during
2516
   * WPS provisioning step since the AP may get reconfigured at the
2517
   * completion of provisioning.
2518
   */
2519
2520
  /* all things being equal, use signal level; if signal levels are
2521
   * identical, use quality values since some drivers may only report
2522
   * that value and leave the signal level zero */
2523
  if (wb->level == wa->level)
2524
    return wb->qual - wa->qual;
2525
  return wb->level - wa->level;
2526
}
2527
#endif /* CONFIG_WPS */
2528
2529
2530
static void dump_scan_res(struct wpa_scan_results *scan_res)
2531
0
{
2532
0
#ifndef CONFIG_NO_STDOUT_DEBUG
2533
0
  size_t i;
2534
2535
0
  if (scan_res->res == NULL || scan_res->num == 0)
2536
0
    return;
2537
2538
0
  wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2539
2540
0
  for (i = 0; i < scan_res->num; i++) {
2541
0
    struct wpa_scan_res *r = scan_res->res[i];
2542
0
    u8 *pos;
2543
0
    const u8 *ssid_ie, *ssid = NULL;
2544
0
    size_t ssid_len = 0;
2545
2546
0
    ssid_ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
2547
0
    if (ssid_ie) {
2548
0
      ssid = ssid_ie + 2;
2549
0
      ssid_len = ssid_ie[1];
2550
0
    }
2551
2552
0
    if (r->flags & WPA_SCAN_LEVEL_DBM) {
2553
0
      int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2554
2555
0
      wpa_printf(MSG_EXCESSIVE, MACSTR
2556
0
           " ssid=%s freq=%d qual=%d noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2557
0
           MAC2STR(r->bssid),
2558
0
           wpa_ssid_txt(ssid, ssid_len),
2559
0
           r->freq, r->qual,
2560
0
           r->noise, noise_valid ? "" : "~", r->level,
2561
0
           r->snr, r->snr >= GREAT_SNR ? "*" : "",
2562
0
           r->flags,
2563
0
           r->age, r->est_throughput);
2564
0
    } else {
2565
0
      wpa_printf(MSG_EXCESSIVE, MACSTR
2566
0
           " ssid=%s freq=%d qual=%d noise=%d level=%d flags=0x%x age=%u est=%u",
2567
0
           MAC2STR(r->bssid),
2568
0
           wpa_ssid_txt(ssid, ssid_len),
2569
0
           r->freq, r->qual,
2570
0
           r->noise, r->level, r->flags, r->age,
2571
0
           r->est_throughput);
2572
0
    }
2573
0
    pos = (u8 *) (r + 1);
2574
0
    if (r->ie_len)
2575
0
      wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2576
0
    pos += r->ie_len;
2577
0
    if (r->beacon_ie_len)
2578
0
      wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2579
0
            pos, r->beacon_ie_len);
2580
0
  }
2581
0
#endif /* CONFIG_NO_STDOUT_DEBUG */
2582
0
}
2583
2584
2585
/**
2586
 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2587
 * @wpa_s: Pointer to wpa_supplicant data
2588
 * @bssid: BSSID to check
2589
 * Returns: 0 if the BSSID is filtered or 1 if not
2590
 *
2591
 * This function is used to filter out specific BSSIDs from scan reslts mainly
2592
 * for testing purposes (SET bssid_filter ctrl_iface command).
2593
 */
2594
int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2595
              const u8 *bssid)
2596
0
{
2597
0
  size_t i;
2598
2599
0
  if (wpa_s->bssid_filter == NULL)
2600
0
    return 1;
2601
2602
0
  for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2603
0
    if (ether_addr_equal(wpa_s->bssid_filter + i * ETH_ALEN, bssid))
2604
0
      return 1;
2605
0
  }
2606
2607
0
  return 0;
2608
0
}
2609
2610
2611
static void filter_scan_res(struct wpa_supplicant *wpa_s,
2612
          struct wpa_scan_results *res)
2613
0
{
2614
0
  size_t i, j;
2615
2616
0
  if (wpa_s->bssid_filter == NULL)
2617
0
    return;
2618
2619
0
  for (i = 0, j = 0; i < res->num; i++) {
2620
0
    if (wpa_supplicant_filter_bssid_match(wpa_s,
2621
0
                  res->res[i]->bssid)) {
2622
0
      res->res[j++] = res->res[i];
2623
0
    } else {
2624
0
      os_free(res->res[i]);
2625
0
      res->res[i] = NULL;
2626
0
    }
2627
0
  }
2628
2629
0
  if (res->num != j) {
2630
0
    wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2631
0
         (int) (res->num - j));
2632
0
    res->num = j;
2633
0
  }
2634
0
}
2635
2636
2637
void scan_snr(struct wpa_scan_res *res)
2638
0
{
2639
0
  if (res->flags & WPA_SCAN_NOISE_INVALID) {
2640
0
    res->noise = is_6ghz_freq(res->freq) ?
2641
0
      DEFAULT_NOISE_FLOOR_6GHZ :
2642
0
      (IS_5GHZ(res->freq) ?
2643
0
       DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
2644
0
  }
2645
2646
0
  if (res->flags & WPA_SCAN_LEVEL_DBM) {
2647
0
    res->snr = res->level - res->noise;
2648
0
  } else {
2649
    /* Level is not in dBm, so we can't calculate
2650
     * SNR. Just use raw level (units unknown). */
2651
0
    res->snr = res->level;
2652
0
  }
2653
0
}
2654
2655
2656
/* Minimum SNR required to achieve a certain bitrate. */
2657
struct minsnr_bitrate_entry {
2658
  int minsnr;
2659
  unsigned int bitrate; /* in Mbps */
2660
};
2661
2662
/* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2663
static const int vht_mcs = 8;
2664
2665
static const struct minsnr_bitrate_entry vht20_table[] = {
2666
  { 0, 0 },
2667
  { 2, 6500 },   /* HT20 MCS0 */
2668
  { 5, 13000 },  /* HT20 MCS1 */
2669
  { 9, 19500 },  /* HT20 MCS2 */
2670
  { 11, 26000 }, /* HT20 MCS3 */
2671
  { 15, 39000 }, /* HT20 MCS4 */
2672
  { 18, 52000 }, /* HT20 MCS5 */
2673
  { 20, 58500 }, /* HT20 MCS6 */
2674
  { 25, 65000 }, /* HT20 MCS7 */
2675
  { 29, 78000 }, /* VHT20 MCS8 */
2676
  { -1, 78000 }  /* SNR > 29 */
2677
};
2678
2679
static const struct minsnr_bitrate_entry vht40_table[] = {
2680
  { 0, 0 },
2681
  { 5, 13500 },   /* HT40 MCS0 */
2682
  { 8, 27000 },   /* HT40 MCS1 */
2683
  { 12, 40500 },  /* HT40 MCS2 */
2684
  { 14, 54000 },  /* HT40 MCS3 */
2685
  { 18, 81000 },  /* HT40 MCS4 */
2686
  { 21, 108000 }, /* HT40 MCS5 */
2687
  { 23, 121500 }, /* HT40 MCS6 */
2688
  { 28, 135000 }, /* HT40 MCS7 */
2689
  { 32, 162000 }, /* VHT40 MCS8 */
2690
  { 34, 180000 }, /* VHT40 MCS9 */
2691
  { -1, 180000 }  /* SNR > 34 */
2692
};
2693
2694
static const struct minsnr_bitrate_entry vht80_table[] = {
2695
  { 0, 0 },
2696
  { 8, 29300 },   /* VHT80 MCS0 */
2697
  { 11, 58500 },  /* VHT80 MCS1 */
2698
  { 15, 87800 },  /* VHT80 MCS2 */
2699
  { 17, 117000 }, /* VHT80 MCS3 */
2700
  { 21, 175500 }, /* VHT80 MCS4 */
2701
  { 24, 234000 }, /* VHT80 MCS5 */
2702
  { 26, 263300 }, /* VHT80 MCS6 */
2703
  { 31, 292500 }, /* VHT80 MCS7 */
2704
  { 35, 351000 }, /* VHT80 MCS8 */
2705
  { 37, 390000 }, /* VHT80 MCS9 */
2706
  { -1, 390000 }  /* SNR > 37 */
2707
};
2708
2709
2710
static const struct minsnr_bitrate_entry vht160_table[] = {
2711
  { 0, 0 },
2712
  { 11, 58500 },  /* VHT160 MCS0 */
2713
  { 14, 117000 }, /* VHT160 MCS1 */
2714
  { 18, 175500 }, /* VHT160 MCS2 */
2715
  { 20, 234000 }, /* VHT160 MCS3 */
2716
  { 24, 351000 }, /* VHT160 MCS4 */
2717
  { 27, 468000 }, /* VHT160 MCS5 */
2718
  { 29, 526500 }, /* VHT160 MCS6 */
2719
  { 34, 585000 }, /* VHT160 MCS7 */
2720
  { 38, 702000 }, /* VHT160 MCS8 */
2721
  { 40, 780000 }, /* VHT160 MCS9 */
2722
  { -1, 780000 }  /* SNR > 37 */
2723
};
2724
2725
/* EHT needs to be enabled in order to achieve MCS12 and MCS13 rates. */
2726
0
#define EHT_MCS 12
2727
2728
static const struct minsnr_bitrate_entry he20_table[] = {
2729
  { 0, 0 },
2730
  { 2, 8600 },    /* HE20 MCS0 */
2731
  { 5, 17200 },   /* HE20 MCS1 */
2732
  { 9, 25800 },   /* HE20 MCS2 */
2733
  { 11, 34400 },  /* HE20 MCS3 */
2734
  { 15, 51600 },  /* HE20 MCS4 */
2735
  { 18, 68800 },  /* HE20 MCS5 */
2736
  { 20, 77400 },  /* HE20 MCS6 */
2737
  { 25, 86000 },  /* HE20 MCS7 */
2738
  { 29, 103200 }, /* HE20 MCS8 */
2739
  { 31, 114700 }, /* HE20 MCS9 */
2740
  { 34, 129000 }, /* HE20 MCS10 */
2741
  { 36, 143400 }, /* HE20 MCS11 */
2742
  { 39, 154900 }, /* EHT20 MCS12 */
2743
  { 42, 172100 }, /* EHT20 MCS13 */
2744
  { -1, 172100 }  /* SNR > 42 */
2745
};
2746
2747
static const struct minsnr_bitrate_entry he40_table[] = {
2748
  { 0, 0 },
2749
  { 5, 17200 },   /* HE40 MCS0 */
2750
  { 8, 34400 },   /* HE40 MCS1 */
2751
  { 12, 51600 },  /* HE40 MCS2 */
2752
  { 14, 68800 },  /* HE40 MCS3 */
2753
  { 18, 103200 }, /* HE40 MCS4 */
2754
  { 21, 137600 }, /* HE40 MCS5 */
2755
  { 23, 154900 }, /* HE40 MCS6 */
2756
  { 28, 172100 }, /* HE40 MCS7 */
2757
  { 32, 206500 }, /* HE40 MCS8 */
2758
  { 34, 229400 }, /* HE40 MCS9 */
2759
  { 37, 258100 }, /* HE40 MCS10 */
2760
  { 39, 286800 }, /* HE40 MCS11 */
2761
  { 42, 309500 }, /* EHT40 MCS12 */
2762
  { 45, 344100 }, /* EHT40 MCS13 */
2763
  { -1, 344100 }  /* SNR > 45 */
2764
};
2765
2766
static const struct minsnr_bitrate_entry he80_table[] = {
2767
  { 0, 0 },
2768
  { 8, 36000 },   /* HE80 MCS0 */
2769
  { 11, 72100 },  /* HE80 MCS1 */
2770
  { 15, 108100 }, /* HE80 MCS2 */
2771
  { 17, 144100 }, /* HE80 MCS3 */
2772
  { 21, 216200 }, /* HE80 MCS4 */
2773
  { 24, 288200 }, /* HE80 MCS5 */
2774
  { 26, 324300 }, /* HE80 MCS6 */
2775
  { 31, 360300 }, /* HE80 MCS7 */
2776
  { 35, 432400 }, /* HE80 MCS8 */
2777
  { 37, 480400 }, /* HE80 MCS9 */
2778
  { 40, 540400 }, /* HE80 MCS10 */
2779
  { 42, 600500 }, /* HE80 MCS11 */
2780
  { 45, 648500 }, /* EHT80 MCS12 */
2781
  { 48, 720600 }, /* EHT80 MCS13 */
2782
  { -1, 720600 }  /* SNR > 48 */
2783
};
2784
2785
2786
static const struct minsnr_bitrate_entry he160_table[] = {
2787
  { 0, 0 },
2788
  { 11, 72100 },   /* HE160 MCS0 */
2789
  { 14, 144100 },  /* HE160 MCS1 */
2790
  { 18, 216200 },  /* HE160 MCS2 */
2791
  { 20, 288200 },  /* HE160 MCS3 */
2792
  { 24, 432400 },  /* HE160 MCS4 */
2793
  { 27, 576500 },  /* HE160 MCS5 */
2794
  { 29, 648500 },  /* HE160 MCS6 */
2795
  { 34, 720600 },  /* HE160 MCS7 */
2796
  { 38, 864700 },  /* HE160 MCS8 */
2797
  { 40, 960800 },  /* HE160 MCS9 */
2798
  { 43, 1080900 }, /* HE160 MCS10 */
2799
  { 45, 1201000 }, /* HE160 MCS11 */
2800
  { 48, 1297100 }, /* EHT160 MCS12 */
2801
  { 51, 1441200 }, /* EHT160 MCS13 */
2802
  { -1, 1441200 }  /* SNR > 51 */
2803
};
2804
2805
/* See IEEE P802.11be/D7.0, Table 36-78 - EHT-MCSs for 484+242-tone MRU,
2806
 * NSS,u = 1
2807
 */
2808
static const struct minsnr_bitrate_entry eht60_table[] = {
2809
  { 0, 0 },
2810
  { 8, 25800 },   /* EHT80 with 20 MHz punctured MCS0 */
2811
  { 11, 51600 },  /* EHT80 with 20 MHz punctured MCS1 */
2812
  { 15, 77400 },  /* EHT80 with 20 MHz punctured MCS2 */
2813
  { 17, 103200 }, /* EHT80 with 20 MHz punctured MCS3 */
2814
  { 21, 154900 }, /* EHT80 with 20 MHz punctured MCS4 */
2815
  { 24, 206500 }, /* EHT80 with 20 MHz punctured MCS5 */
2816
  { 26, 232300 }, /* EHT80 with 20 MHz punctured MCS6 */
2817
  { 31, 258100 }, /* EHT80 with 20 MHz punctured MCS7 */
2818
  { 35, 309700 }, /* EHT80 with 20 MHz punctured MCS8 */
2819
  { 37, 344100 }, /* EHT80 with 20 MHz punctured MCS9 */
2820
  { 40, 387100 }, /* EHT80 with 20 MHz punctured MCS10 */
2821
  { 42, 430100 }, /* EHT80 with 20 MHz punctured MCS11 */
2822
  { 45, 464600 }, /* EHT80 with 20 MHz punctured MCS12 */
2823
  { 48, 516200 }, /* EHT80 with 20 MHz punctured MCS13 */
2824
  { -1, 516200 }  /* SNR > 48 */
2825
};
2826
2827
/* See IEEE P802.11be/D7.0, Table 36-80 - EHT-MCSs for 996+484-tone MRU,
2828
 * NSS,u = 1
2829
 */
2830
static const struct minsnr_bitrate_entry eht120_table[] = {
2831
  { 0, 0 },
2832
  { 11, 53200 },   /* EHT160 with 40 MHz punctured MCS0 */
2833
  { 14, 106500 },  /* EHT160 with 40 MHz punctured MCS1 */
2834
  { 18, 159700 },  /* EHT160 with 40 MHz punctured MCS2 */
2835
  { 20, 212900 },  /* EHT160 with 40 MHz punctured MCS3 */
2836
  { 24, 319400 },  /* EHT160 with 40 MHz punctured MCS4 */
2837
  { 27, 425900 },  /* EHT160 with 40 MHz punctured MCS5 */
2838
  { 29, 479100 },  /* EHT160 with 40 MHz punctured MCS6 */
2839
  { 34, 532400 },  /* EHT160 with 40 MHz punctured MCS7 */
2840
  { 38, 638800 },  /* EHT160 with 40 MHz punctured MCS8 */
2841
  { 40, 709800 },  /* EHT160 with 40 MHz punctured MCS9 */
2842
  { 43, 798500 },  /* EHT160 with 40 MHz punctured MCS10 */
2843
  { 45, 887200 },  /* EHT160 with 40 MHz punctured MCS11 */
2844
  { 48, 958200 },  /* EHT160 with 40 MHz punctured MCS12 */
2845
  { 51, 1064700 }, /* EHT160 with 40 MHz punctured MCS13 */
2846
  { -1, 1064700 }  /* SNR > 51 */
2847
};
2848
2849
/* See IEEE P802.11be/D7.0, Table 36-81 - EHT-MCSs for 996+484+242-tone MRU,
2850
 * NSS,u = 1
2851
 */
2852
static const struct minsnr_bitrate_entry eht140_table[] = {
2853
  { 0, 0 },
2854
  { 11, 61800 },   /* EHT160 with 20 MHz punctured MCS0 */
2855
  { 14, 123700 },  /* EHT160 with 20 MHz punctured MCS1 */
2856
  { 18, 185500 },  /* EHT160 with 20 MHz punctured MCS2 */
2857
  { 20, 247400 },  /* EHT160 with 20 MHz punctured MCS3 */
2858
  { 24, 371000 },  /* EHT160 with 20 MHz punctured MCS4 */
2859
  { 27, 494700 },  /* EHT160 with 20 MHz punctured MCS5 */
2860
  { 29, 556500 },  /* EHT160 with 20 MHz punctured MCS6 */
2861
  { 34, 618400 },  /* EHT160 with 20 MHz punctured MCS7 */
2862
  { 38, 742100 },  /* EHT160 with 20 MHz punctured MCS8 */
2863
  { 40, 824500 },  /* EHT160 with 20 MHz punctured MCS9 */
2864
  { 43, 927600 },  /* EHT160 with 20 MHz punctured MCS10 */
2865
  { 45, 1030600 }, /* EHT160 with 20 MHz punctured MCS11 */
2866
  { 48, 1113100 }, /* EHT160 with 20 MHz punctured MCS12 */
2867
  { 51, 1236800 }, /* EHT160 with 20 MHz punctured MCS13 */
2868
  { -1, 1236800 }  /* SNR > 51 */
2869
};
2870
2871
/* See IEEE P802.11be/D7.0, Table 36-83 - EHT-MCSs for 2x996+484-tone NRU,
2872
 * NSS,u = 1
2873
 */
2874
static const struct minsnr_bitrate_entry eht200_table[] = {
2875
  { 0, 0 },
2876
  { 14, 89300 },    /* EHT320 with 120 MHz punctured MCS0 */
2877
  { 17, 178500 },   /* EHT320 with 120 MHz punctured MCS1 */
2878
  { 21, 267800 },   /* EHT320 with 120 MHz punctured MCS2 */
2879
  { 23, 357100 },   /* EHT320 with 120 MHz punctured MCS3 */
2880
  { 27, 535600 },   /* EHT320 with 120 MHz punctured MCS4 */
2881
  { 30, 714100 },   /* EHT320 with 120 MHz punctured MCS5 */
2882
  { 32, 803400 },   /* EHT320 with 120 MHz punctured MCS6 */
2883
  { 37, 892600 },   /* EHT320 with 120 MHz punctured MCS7 */
2884
  { 41, 1071200 },  /* EHT320 with 120 MHz punctured MCS8 */
2885
  { 43, 1190100 },  /* EHT320 with 120 MHz punctured MCS9 */
2886
  { 46, 1339000 },  /* EHT320 with 120 MHz punctured MCS10 */
2887
  { 48, 1487700 },  /* EHT320 with 120 MHz punctured MCS11 */
2888
  { 51, 1606800 },  /* EHT320 with 120 MHz punctured MCS12 */
2889
  { 54, 1785300 },  /* EHT320 with 120 MHz punctured MCS13 */
2890
  { -1, 1785300 }   /* SNR > 54 */
2891
};
2892
2893
/* See IEEE P802.11be/D7.0, Table 36-84 - EHT-MCSs for 3x996-tone MRU,
2894
 * NSS,u = 1
2895
 */
2896
static const struct minsnr_bitrate_entry eht240_table[] = {
2897
  { 0, 0 },
2898
  { 14, 108100 },   /* EHT320 with 80 MHz punctured MCS0 */
2899
  { 17, 216200 },   /* EHT320 with 80 MHz punctured MCS1 */
2900
  { 21, 324300 },   /* EHT320 with 80 MHz punctured MCS2 */
2901
  { 23, 432400 },   /* EHT320 with 80 MHz punctured MCS3 */
2902
  { 27, 648500 },   /* EHT320 with 80 MHz punctured MCS4 */
2903
  { 30, 864700 },   /* EHT320 with 80 MHz punctured MCS5 */
2904
  { 32, 972800 },   /* EHT320 with 80 MHz punctured MCS6 */
2905
  { 37, 1080900 },  /* EHT320 with 80 MHz punctured MCS7 */
2906
  { 41, 1297100 },  /* EHT320 with 80 MHz punctured MCS8 */
2907
  { 43, 1441200 },  /* EHT320 with 80 MHz punctured MCS9 */
2908
  { 46, 1621300 },  /* EHT320 with 80 MHz punctured MCS10 */
2909
  { 48, 1801500 },  /* EHT320 with 80 MHz punctured MCS11 */
2910
  { 51, 1945600 },  /* EHT320 with 80 MHz punctured MCS12 */
2911
  { 54, 2161800 },  /* EHT320 with 80 MHz punctured MCS13 */
2912
  { -1, 2161800 }   /* SNR > 54 */
2913
};
2914
2915
/* See IEEE P802.11be/D7.0, Table 36-85: EHT-MCSs for 3x996+484-tone MRU,
2916
 * NSS,u = 1
2917
 */
2918
static const struct minsnr_bitrate_entry eht280_table[] = {
2919
  { 0, 0 },
2920
  { 14, 125300 },   /* EHT320 with 40 MHz punctured MCS0 */
2921
  { 17, 250600 },   /* EHT320 with 40 MHz punctured MCS1 */
2922
  { 21, 375900 },   /* EHT320 with 40 MHz punctured MCS2 */
2923
  { 23, 501200 },   /* EHT320 with 40 MHz punctured MCS3 */
2924
  { 27, 751800 },   /* EHT320 with 40 MHz punctured MCS4 */
2925
  { 30, 1002400 },  /* EHT320 with 40 MHz punctured MCS5 */
2926
  { 32, 1127600 },  /* EHT320 with 40 MHz punctured MCS6 */
2927
  { 37, 1252900 },  /* EHT320 with 40 MHz punctured MCS7 */
2928
  { 41, 1503500 },  /* EHT320 with 40 MHz punctured MCS8 */
2929
  { 43, 1670600 },  /* EHT320 with 40 MHz punctured MCS9 */
2930
  { 46, 1879400 },  /* EHT320 with 40 MHz punctured MCS10 */
2931
  { 48, 2088200 },  /* EHT320 with 40 MHz punctured MCS11 */
2932
  { 51, 2255300 },  /* EHT320 with 40 MHz punctured MCS12 */
2933
  { 54, 2505900 },  /* EHT320 with 40 MHz punctured MCS13 */
2934
  { -1, 2505900 }   /* SNR > 54 */
2935
};
2936
2937
/* See IEEE P802.11be/D2.0, Table 36-86: EHT-MCSs for 4x996-tone RU, NSS,u = 1
2938
 */
2939
static const struct minsnr_bitrate_entry eht320_table[] = {
2940
  { 0, 0 },
2941
  { 14, 144100 },   /* EHT320 MCS0 */
2942
  { 17, 288200 },   /* EHT320 MCS1 */
2943
  { 21, 432400 },   /* EHT320 MCS2 */
2944
  { 23, 576500 },   /* EHT320 MCS3 */
2945
  { 27, 864700 },   /* EHT320 MCS4 */
2946
  { 30, 1152900 },  /* EHT320 MCS5 */
2947
  { 32, 1297100 },  /* EHT320 MCS6 */
2948
  { 37, 1441200 },  /* EHT320 MCS7 */
2949
  { 41, 1729400 },  /* EHT320 MCS8 */
2950
  { 43, 1921500 },  /* EHT320 MCS9 */
2951
  { 46, 2161800 },  /* EHT320 MCS10 */
2952
  { 48, 2401900 },  /* EHT320 MCS11 */
2953
  { 51, 2594100 },  /* EHT320 MCS12 */
2954
  { 54, 2882400 },  /* EHT320 MCS13 */
2955
  { -1, 2882400 }   /* SNR > 54 */
2956
};
2957
2958
static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2959
             int rate0, int rate1)
2960
0
{
2961
0
  return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
2962
0
}
2963
2964
2965
static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2966
           int snr, bool vht)
2967
0
{
2968
0
  const struct minsnr_bitrate_entry *prev, *entry = table;
2969
2970
0
  while ((entry->minsnr != -1) &&
2971
0
         (snr >= entry->minsnr) &&
2972
0
         (vht || entry - table <= vht_mcs))
2973
0
    entry++;
2974
0
  if (entry == table)
2975
0
    return entry->bitrate;
2976
0
  prev = entry - 1;
2977
0
  if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2978
0
    return prev->bitrate;
2979
0
  return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2980
0
        entry->bitrate);
2981
0
}
2982
2983
2984
static unsigned int max_ht20_rate(int snr, bool vht)
2985
0
{
2986
0
  return max_rate(vht20_table, snr, vht);
2987
0
}
2988
2989
2990
static unsigned int max_ht40_rate(int snr, bool vht)
2991
0
{
2992
0
  return max_rate(vht40_table, snr, vht);
2993
0
}
2994
2995
2996
static unsigned int max_vht80_rate(int snr)
2997
0
{
2998
0
  return max_rate(vht80_table, snr, 1);
2999
0
}
3000
3001
3002
static unsigned int max_vht160_rate(int snr)
3003
0
{
3004
0
  return max_rate(vht160_table, snr, 1);
3005
0
}
3006
3007
3008
static unsigned int max_he_eht_rate(const struct minsnr_bitrate_entry table[],
3009
            int snr, bool eht)
3010
0
{
3011
0
  const struct minsnr_bitrate_entry *prev, *entry = table;
3012
3013
0
  while (entry->minsnr != -1 && snr >= entry->minsnr &&
3014
0
         (eht || entry - table <= EHT_MCS))
3015
0
    entry++;
3016
0
  if (entry == table)
3017
0
    return 0;
3018
0
  prev = entry - 1;
3019
0
  if (entry->minsnr == -1 || (!eht && entry - table > EHT_MCS))
3020
0
    return prev->bitrate;
3021
0
  return interpolate_rate(snr, prev->minsnr, entry->minsnr,
3022
0
        prev->bitrate, entry->bitrate);
3023
0
}
3024
3025
3026
static unsigned int get_eht_punctured_rate(enum chan_width cw,
3027
             u8 num_punct_bits, int adjusted_snr,
3028
             u8 boost)
3029
0
{
3030
0
  const struct minsnr_bitrate_entry *eht_table;
3031
3032
0
  switch (cw) {
3033
0
  case CHAN_WIDTH_80:
3034
0
    switch (num_punct_bits) {
3035
0
    case 1:
3036
      /* EHT80 with 20 MHz punctured */
3037
0
      eht_table = eht60_table;
3038
0
      break;
3039
0
    default:
3040
0
      eht_table = he80_table;
3041
0
      break;
3042
0
    }
3043
0
    break;
3044
0
  case CHAN_WIDTH_160:
3045
0
    switch (num_punct_bits) {
3046
0
    case 2:
3047
      /* EHT160 with 40 MHz punctured */
3048
0
      eht_table = eht120_table;
3049
0
      break;
3050
0
    case 1:
3051
      /* EHT160 with 20 MHz punctured */
3052
0
      eht_table = eht140_table;
3053
0
      break;
3054
0
    default:
3055
0
      eht_table = he160_table;
3056
0
      break;
3057
0
    }
3058
0
    break;
3059
0
  case CHAN_WIDTH_320:
3060
0
    switch (num_punct_bits) {
3061
0
    case 6:
3062
      /* EHT320 with 120 MHz punctured */
3063
0
      eht_table = eht200_table;
3064
0
      break;
3065
0
    case 4:
3066
      /* EHT320 with 80 MHz punctured */
3067
0
      eht_table = eht240_table;
3068
0
      break;
3069
0
    case 2:
3070
      /* EHT320 with 40 MHz punctured */
3071
0
      eht_table = eht280_table;
3072
0
      break;
3073
0
    default:
3074
0
      eht_table = eht320_table;
3075
0
      break;
3076
0
    }
3077
0
    break;
3078
0
  default:
3079
    /* Puncturing is not supported for the channel width */
3080
0
    return 0;
3081
0
  }
3082
3083
0
  return max_he_eht_rate(eht_table, adjusted_snr, true) + boost;
3084
0
}
3085
3086
3087
static u8 get_eht_num_punct_bits(const u8 *ies, size_t ies_len)
3088
0
{
3089
0
  const u8 *eht_ie;
3090
3091
0
  eht_ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_EHT_OPERATION);
3092
0
  if (eht_ie && eht_ie[1] >= 1 + IEEE80211_EHT_OP_MIN_LEN) {
3093
0
    struct ieee80211_eht_operation *eht_op;
3094
3095
0
    eht_op = (struct ieee80211_eht_operation *) &eht_ie[3];
3096
3097
0
    if (eht_op->oper_params &
3098
0
        EHT_OPER_DISABLED_SUBCHAN_BITMAP_PRESENT) {
3099
0
      u16 punct_bitmap;
3100
0
      u8 count = 0;
3101
3102
0
      punct_bitmap = le_to_host16(
3103
0
        eht_op->oper_info.disabled_chan_bitmap);
3104
0
      while (punct_bitmap) {
3105
0
        count += punct_bitmap & 1;
3106
0
        punct_bitmap >>= 1;
3107
0
      }
3108
0
      return count;
3109
0
    }
3110
0
  }
3111
3112
0
  return 0;
3113
0
}
3114
3115
3116
unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
3117
            const u8 *ies, size_t ies_len, int rate,
3118
            int snr, int freq, enum chan_width *max_cw)
3119
0
{
3120
0
  struct hostapd_hw_modes *hw_mode;
3121
0
  unsigned int est, tmp;
3122
0
  const u8 *ie;
3123
  /*
3124
   * No need to apply a bump to the noise here because the
3125
   * minsnr_bitrate_entry tables are based on MCS tables where this has
3126
   * been taken into account.
3127
   */
3128
0
  int adjusted_snr;
3129
0
  bool ht40 = false, vht80 = false, vht160 = false;
3130
3131
  /* Limit based on estimated SNR */
3132
0
  if (rate > 1 * 2 && snr < 1)
3133
0
    rate = 1 * 2;
3134
0
  else if (rate > 2 * 2 && snr < 4)
3135
0
    rate = 2 * 2;
3136
0
  else if (rate > 6 * 2 && snr < 5)
3137
0
    rate = 6 * 2;
3138
0
  else if (rate > 9 * 2 && snr < 6)
3139
0
    rate = 9 * 2;
3140
0
  else if (rate > 12 * 2 && snr < 7)
3141
0
    rate = 12 * 2;
3142
0
  else if (rate > 12 * 2 && snr < 8)
3143
0
    rate = 14 * 2;
3144
0
  else if (rate > 12 * 2 && snr < 9)
3145
0
    rate = 16 * 2;
3146
0
  else if (rate > 18 * 2 && snr < 10)
3147
0
    rate = 18 * 2;
3148
0
  else if (rate > 24 * 2 && snr < 11)
3149
0
    rate = 24 * 2;
3150
0
  else if (rate > 24 * 2 && snr < 12)
3151
0
    rate = 27 * 2;
3152
0
  else if (rate > 24 * 2 && snr < 13)
3153
0
    rate = 30 * 2;
3154
0
  else if (rate > 24 * 2 && snr < 14)
3155
0
    rate = 33 * 2;
3156
0
  else if (rate > 36 * 2 && snr < 15)
3157
0
    rate = 36 * 2;
3158
0
  else if (rate > 36 * 2 && snr < 16)
3159
0
    rate = 39 * 2;
3160
0
  else if (rate > 36 * 2 && snr < 17)
3161
0
    rate = 42 * 2;
3162
0
  else if (rate > 36 * 2 && snr < 18)
3163
0
    rate = 45 * 2;
3164
0
  else if (rate > 48 * 2 && snr < 19)
3165
0
    rate = 48 * 2;
3166
0
  else if (rate > 48 * 2 && snr < 20)
3167
0
    rate = 51 * 2;
3168
0
  else if (rate > 54 * 2 && snr < 21)
3169
0
    rate = 54 * 2;
3170
0
  est = rate * 500;
3171
3172
0
  hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
3173
0
             freq);
3174
3175
0
  if (hw_mode && hw_mode->ht_capab) {
3176
0
    ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
3177
0
    if (ie) {
3178
0
      *max_cw = CHAN_WIDTH_20;
3179
0
      tmp = max_ht20_rate(snr, false);
3180
0
      if (tmp > est)
3181
0
        est = tmp;
3182
0
    }
3183
0
  }
3184
3185
0
  ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
3186
0
  if (ie && ie[1] >= 2 &&
3187
0
      (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK))
3188
0
    ht40 = true;
3189
3190
0
  if (hw_mode &&
3191
0
      (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
3192
0
    if (ht40) {
3193
0
      *max_cw = CHAN_WIDTH_40;
3194
0
      adjusted_snr = snr +
3195
0
        wpas_channel_width_rssi_bump(ies, ies_len,
3196
0
                   CHAN_WIDTH_40);
3197
0
      tmp = max_ht40_rate(adjusted_snr, false);
3198
0
      if (tmp > est)
3199
0
        est = tmp;
3200
0
    }
3201
0
  }
3202
3203
  /* Determine VHT BSS bandwidth based on IEEE Std 802.11-2020,
3204
   * Table 11-23 (VHT BSS bandwidth) */
3205
0
  ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
3206
0
  if (ie && ie[1] >= 3) {
3207
0
    u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
3208
0
    u8 seg0 = ie[3];
3209
0
    u8 seg1 = ie[4];
3210
3211
0
    if (cw)
3212
0
      vht80 = true;
3213
0
    if (cw == 2 ||
3214
0
        (cw == 3 && (seg1 > 0 && abs(seg1 - seg0) == 16)))
3215
0
      vht160 = true;
3216
0
    if (cw == 1 &&
3217
0
        ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
3218
0
         (seg1 > 0 && abs(seg1 - seg0) == 16)))
3219
0
      vht160 = true;
3220
0
  }
3221
3222
0
  if (hw_mode && hw_mode->vht_capab) {
3223
    /* Use +1 to assume VHT is always faster than HT */
3224
0
    ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
3225
0
    if (ie) {
3226
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN)
3227
0
        *max_cw = CHAN_WIDTH_20;
3228
0
      tmp = max_ht20_rate(snr, true) + 1;
3229
0
      if (tmp > est)
3230
0
        est = tmp;
3231
3232
0
      if (ht40) {
3233
0
        *max_cw = CHAN_WIDTH_40;
3234
0
        adjusted_snr = snr +
3235
0
          wpas_channel_width_rssi_bump(
3236
0
            ies, ies_len, CHAN_WIDTH_40);
3237
0
        tmp = max_ht40_rate(adjusted_snr, true) + 1;
3238
0
        if (tmp > est)
3239
0
          est = tmp;
3240
0
      }
3241
3242
0
      if (vht80) {
3243
0
        *max_cw = CHAN_WIDTH_80;
3244
0
        adjusted_snr = snr +
3245
0
          wpas_channel_width_rssi_bump(
3246
0
            ies, ies_len, CHAN_WIDTH_80);
3247
0
        tmp = max_vht80_rate(adjusted_snr) + 1;
3248
0
        if (tmp > est)
3249
0
          est = tmp;
3250
0
      }
3251
3252
0
      if (vht160 &&
3253
0
          (hw_mode->vht_capab &
3254
0
           (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3255
0
            VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
3256
0
        *max_cw = CHAN_WIDTH_160;
3257
0
        adjusted_snr = snr +
3258
0
          wpas_channel_width_rssi_bump(
3259
0
            ies, ies_len, CHAN_WIDTH_160);
3260
0
        tmp = max_vht160_rate(adjusted_snr) + 1;
3261
0
        if (tmp > est)
3262
0
          est = tmp;
3263
0
      }
3264
0
    }
3265
0
  }
3266
3267
0
  if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
3268
    /* Use +2 to assume HE is always faster than HT/VHT */
3269
0
    struct ieee80211_he_capabilities *he;
3270
0
    struct ieee80211_eht_capabilities *eht;
3271
0
    struct he_capabilities *own_he;
3272
0
    u8 cw, boost = 2;
3273
0
    const u8 *eht_ie = NULL;
3274
0
    bool is_eht = false;
3275
0
    u8 num_punct_bits;
3276
3277
0
    ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
3278
0
    if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
3279
0
      return est;
3280
0
    he = (struct ieee80211_he_capabilities *) &ie[3];
3281
0
    own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
3282
3283
    /* Use +3 to assume EHT is always faster than HE */
3284
0
    if (hw_mode->eht_capab[IEEE80211_MODE_INFRA].eht_supported) {
3285
0
      eht_ie = get_ie_ext(ies, ies_len,
3286
0
              WLAN_EID_EXT_EHT_CAPABILITIES);
3287
0
      if (eht_ie &&
3288
0
          (eht_ie[1] >= 1 + IEEE80211_EHT_CAPAB_MIN_LEN)) {
3289
0
        is_eht = true;
3290
0
        boost = 3;
3291
0
      }
3292
0
    }
3293
3294
0
    if (*max_cw == CHAN_WIDTH_UNKNOWN)
3295
0
      *max_cw = CHAN_WIDTH_20;
3296
0
    tmp = max_he_eht_rate(he20_table, snr, is_eht) + boost;
3297
0
    if (tmp > est)
3298
0
      est = tmp;
3299
3300
0
    cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
3301
0
      own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
3302
0
    if ((cw &
3303
0
         (IS_2P4GHZ(freq) ?
3304
0
          HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
3305
0
          HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) && ht40) {
3306
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3307
0
          *max_cw < CHAN_WIDTH_40)
3308
0
        *max_cw = CHAN_WIDTH_40;
3309
0
      adjusted_snr = snr + wpas_channel_width_rssi_bump(
3310
0
        ies, ies_len, CHAN_WIDTH_40);
3311
0
      tmp = max_he_eht_rate(he40_table, adjusted_snr,
3312
0
                is_eht) + boost;
3313
0
      if (tmp > est)
3314
0
        est = tmp;
3315
0
    }
3316
3317
0
    if (!IS_2P4GHZ(freq) &&
3318
0
        (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G) &&
3319
0
        (!IS_5GHZ(freq) || vht80)) {
3320
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3321
0
          *max_cw < CHAN_WIDTH_80)
3322
0
        *max_cw = CHAN_WIDTH_80;
3323
0
      adjusted_snr = snr + wpas_channel_width_rssi_bump(
3324
0
        ies, ies_len, CHAN_WIDTH_80);
3325
3326
0
      num_punct_bits = get_eht_num_punct_bits(ies, ies_len);
3327
0
      if (is_eht && num_punct_bits)
3328
0
        tmp = get_eht_punctured_rate(CHAN_WIDTH_80,
3329
0
                   num_punct_bits,
3330
0
                   adjusted_snr,
3331
0
                   boost);
3332
0
      else
3333
0
        tmp = max_he_eht_rate(he80_table, adjusted_snr,
3334
0
                  is_eht) + boost;
3335
3336
0
      if (tmp > est)
3337
0
        est = tmp;
3338
0
    }
3339
3340
0
    if (!IS_2P4GHZ(freq) &&
3341
0
        (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3342
0
         HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) &&
3343
0
        (!IS_5GHZ(freq) || vht160)) {
3344
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3345
0
          *max_cw < CHAN_WIDTH_160)
3346
0
        *max_cw = CHAN_WIDTH_160;
3347
0
      adjusted_snr = snr + wpas_channel_width_rssi_bump(
3348
0
        ies, ies_len, CHAN_WIDTH_160);
3349
3350
0
      num_punct_bits = get_eht_num_punct_bits(ies, ies_len);
3351
0
      if (is_eht && num_punct_bits)
3352
0
        tmp = get_eht_punctured_rate(CHAN_WIDTH_160,
3353
0
                   num_punct_bits,
3354
0
                   adjusted_snr,
3355
0
                   boost);
3356
0
      else
3357
0
        tmp = max_he_eht_rate(he160_table, adjusted_snr,
3358
0
                  is_eht) + boost;
3359
0
      if (tmp > est)
3360
0
        est = tmp;
3361
0
    }
3362
3363
0
    if (!is_eht || !eht_ie)
3364
0
      return est;
3365
3366
0
    eht = (struct ieee80211_eht_capabilities *) &eht_ie[3];
3367
3368
0
    if (is_6ghz_freq(freq) &&
3369
0
        (eht->phy_cap[EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
3370
0
         EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK)) {
3371
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3372
0
          *max_cw < CHAN_WIDTH_320)
3373
0
        *max_cw = CHAN_WIDTH_320;
3374
0
      adjusted_snr = snr + wpas_channel_width_rssi_bump(
3375
0
        ies, ies_len, CHAN_WIDTH_320);
3376
3377
0
      num_punct_bits = get_eht_num_punct_bits(ies, ies_len);
3378
0
      if (num_punct_bits)
3379
0
        tmp = get_eht_punctured_rate(CHAN_WIDTH_320,
3380
0
                   num_punct_bits,
3381
0
                   adjusted_snr,
3382
0
                   0);
3383
0
      else
3384
0
        tmp = max_he_eht_rate(eht320_table, adjusted_snr,
3385
0
                  true);
3386
0
      if (tmp > est)
3387
0
        est = tmp;
3388
0
    }
3389
0
  }
3390
3391
0
  return est;
3392
0
}
3393
3394
3395
void scan_est_throughput(struct wpa_supplicant *wpa_s,
3396
       struct wpa_scan_res *res)
3397
0
{
3398
0
  int rate; /* max legacy rate in 500 kb/s units */
3399
0
  int snr = res->snr;
3400
0
  const u8 *ies = (const void *) (res + 1);
3401
0
  size_t ie_len = res->ie_len;
3402
3403
0
  if (res->est_throughput)
3404
0
    return;
3405
3406
  /* Get maximum legacy rate */
3407
0
  rate = wpa_scan_get_max_rate(res);
3408
3409
0
  if (!ie_len)
3410
0
    ie_len = res->beacon_ie_len;
3411
0
  res->est_throughput = wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr,
3412
0
                 res->freq, &res->max_cw);
3413
3414
  /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
3415
0
}
3416
3417
3418
/**
3419
 * wpa_supplicant_get_scan_results - Get scan results
3420
 * @wpa_s: Pointer to wpa_supplicant data
3421
 * @info: Information about what was scanned or %NULL if not available
3422
 * @new_scan: Whether a new scan was performed
3423
 * @bssid: Return BSS entries only for a single BSSID, %NULL for all
3424
 * Returns: Scan results, %NULL on failure
3425
 *
3426
 * This function request the current scan results from the driver and updates
3427
 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
3428
 * results with wpa_scan_results_free().
3429
 */
3430
struct wpa_scan_results *
3431
wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
3432
        struct scan_info *info, int new_scan,
3433
        const u8 *bssid)
3434
914
{
3435
914
  struct wpa_scan_results *scan_res;
3436
914
  size_t i;
3437
914
  int (*compar)(const void *, const void *) = wpa_scan_result_compar;
3438
3439
914
  scan_res = wpa_drv_get_scan_results(wpa_s, bssid);
3440
914
  if (scan_res == NULL) {
3441
914
    wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
3442
914
    return NULL;
3443
914
  }
3444
0
  if (scan_res->fetch_time.sec == 0) {
3445
    /*
3446
     * Make sure we have a valid timestamp if the driver wrapper
3447
     * does not set this.
3448
     */
3449
0
    os_get_reltime(&scan_res->fetch_time);
3450
0
  }
3451
0
  filter_scan_res(wpa_s, scan_res);
3452
3453
0
  for (i = 0; i < scan_res->num; i++) {
3454
0
    struct wpa_scan_res *scan_res_item = scan_res->res[i];
3455
3456
0
    scan_snr(scan_res_item);
3457
0
    scan_est_throughput(wpa_s, scan_res_item);
3458
0
  }
3459
3460
#ifdef CONFIG_WPS
3461
  if (wpas_wps_searching(wpa_s)) {
3462
    wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
3463
      "provisioning rules");
3464
    compar = wpa_scan_result_wps_compar;
3465
  }
3466
#endif /* CONFIG_WPS */
3467
3468
0
  if (scan_res->res) {
3469
0
    qsort(scan_res->res, scan_res->num,
3470
0
          sizeof(struct wpa_scan_res *), compar);
3471
0
  }
3472
0
  dump_scan_res(scan_res);
3473
3474
0
  if (wpa_s->ignore_post_flush_scan_res) {
3475
    /* FLUSH command aborted an ongoing scan and these are the
3476
     * results from the aborted scan. Do not process the results to
3477
     * maintain flushed state. */
3478
0
    wpa_dbg(wpa_s, MSG_DEBUG,
3479
0
      "Do not update BSS table based on pending post-FLUSH scan results");
3480
0
    wpa_s->ignore_post_flush_scan_res = 0;
3481
0
    return scan_res;
3482
0
  }
3483
3484
0
  wpa_bss_update_start(wpa_s);
3485
0
  for (i = 0; i < scan_res->num; i++)
3486
0
    wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
3487
0
          &scan_res->fetch_time);
3488
0
  wpa_bss_update_end(wpa_s, info, new_scan);
3489
3490
0
  return scan_res;
3491
0
}
3492
3493
3494
/**
3495
 * wpa_supplicant_update_scan_results - Update scan results from the driver
3496
 * @wpa_s: Pointer to wpa_supplicant data
3497
 * @bssid: Update BSS entries only for a single BSSID, %NULL for all
3498
 * Returns: 0 on success, -1 on failure
3499
 *
3500
 * This function updates the BSS table within wpa_supplicant based on the
3501
 * currently available scan results from the driver without requesting a new
3502
 * scan. This is used in cases where the driver indicates an association
3503
 * (including roaming within ESS) and wpa_supplicant does not yet have the
3504
 * needed information to complete the connection (e.g., to perform validation
3505
 * steps in 4-way handshake).
3506
 */
3507
int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s,
3508
               const u8 *bssid)
3509
914
{
3510
914
  struct wpa_scan_results *scan_res;
3511
914
  scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0, bssid);
3512
914
  if (scan_res == NULL)
3513
914
    return -1;
3514
0
  wpa_scan_results_free(scan_res);
3515
3516
0
  return 0;
3517
914
}
3518
3519
3520
/**
3521
 * scan_only_handler - Reports scan results
3522
 */
3523
void scan_only_handler(struct wpa_supplicant *wpa_s,
3524
           struct wpa_scan_results *scan_res)
3525
0
{
3526
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
3527
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3528
0
      wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
3529
0
    wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
3530
0
           wpa_s->manual_scan_id);
3531
0
    wpa_s->manual_scan_use_id = 0;
3532
0
  } else {
3533
0
    wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
3534
0
  }
3535
0
  wpas_notify_scan_results(wpa_s);
3536
0
  wpas_notify_scan_done(wpa_s, 1);
3537
0
  if (wpa_s->scan_work) {
3538
0
    struct wpa_radio_work *work = wpa_s->scan_work;
3539
0
    wpa_s->scan_work = NULL;
3540
0
    radio_work_done(work);
3541
0
  }
3542
3543
0
  if (wpa_s->wpa_state == WPA_SCANNING)
3544
0
    wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
3545
0
}
3546
3547
3548
int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
3549
0
{
3550
0
  return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
3551
0
}
3552
3553
3554
struct wpa_driver_scan_params *
3555
wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
3556
0
{
3557
0
  struct wpa_driver_scan_params *params;
3558
0
  size_t i;
3559
0
  u8 *n;
3560
3561
0
  params = os_zalloc(sizeof(*params));
3562
0
  if (params == NULL)
3563
0
    return NULL;
3564
3565
0
  for (i = 0; i < src->num_ssids; i++) {
3566
0
    if (src->ssids[i].ssid) {
3567
0
      n = os_memdup(src->ssids[i].ssid,
3568
0
              src->ssids[i].ssid_len);
3569
0
      if (n == NULL)
3570
0
        goto failed;
3571
0
      params->ssids[i].ssid = n;
3572
0
      params->ssids[i].ssid_len = src->ssids[i].ssid_len;
3573
0
    }
3574
0
  }
3575
0
  params->num_ssids = src->num_ssids;
3576
3577
0
  if (src->extra_ies) {
3578
0
    n = os_memdup(src->extra_ies, src->extra_ies_len);
3579
0
    if (n == NULL)
3580
0
      goto failed;
3581
0
    params->extra_ies = n;
3582
0
    params->extra_ies_len = src->extra_ies_len;
3583
0
  }
3584
3585
0
  if (src->freqs) {
3586
0
    int len = int_array_len(src->freqs);
3587
0
    params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
3588
0
    if (params->freqs == NULL)
3589
0
      goto failed;
3590
0
  }
3591
3592
0
  if (src->filter_ssids) {
3593
0
    params->filter_ssids = os_memdup(src->filter_ssids,
3594
0
             sizeof(*params->filter_ssids) *
3595
0
             src->num_filter_ssids);
3596
0
    if (params->filter_ssids == NULL)
3597
0
      goto failed;
3598
0
    params->num_filter_ssids = src->num_filter_ssids;
3599
0
  }
3600
3601
0
  params->filter_rssi = src->filter_rssi;
3602
0
  params->p2p_probe = src->p2p_probe;
3603
0
  params->only_new_results = src->only_new_results;
3604
0
  params->low_priority = src->low_priority;
3605
0
  params->duration = src->duration;
3606
0
  params->duration_mandatory = src->duration_mandatory;
3607
0
  params->oce_scan = src->oce_scan;
3608
0
  params->link_id = src->link_id;
3609
3610
0
  if (src->sched_scan_plans_num > 0) {
3611
0
    params->sched_scan_plans =
3612
0
      os_memdup(src->sched_scan_plans,
3613
0
          sizeof(*src->sched_scan_plans) *
3614
0
          src->sched_scan_plans_num);
3615
0
    if (!params->sched_scan_plans)
3616
0
      goto failed;
3617
3618
0
    params->sched_scan_plans_num = src->sched_scan_plans_num;
3619
0
  }
3620
3621
0
  if (src->mac_addr_rand &&
3622
0
      wpa_setup_mac_addr_rand_params(params, src->mac_addr))
3623
0
    goto failed;
3624
3625
0
  if (src->bssid) {
3626
0
    u8 *bssid;
3627
3628
0
    bssid = os_memdup(src->bssid, ETH_ALEN);
3629
0
    if (!bssid)
3630
0
      goto failed;
3631
0
    params->bssid = bssid;
3632
0
  }
3633
3634
0
  params->relative_rssi_set = src->relative_rssi_set;
3635
0
  params->relative_rssi = src->relative_rssi;
3636
0
  params->relative_adjust_band = src->relative_adjust_band;
3637
0
  params->relative_adjust_rssi = src->relative_adjust_rssi;
3638
0
  params->p2p_include_6ghz = src->p2p_include_6ghz;
3639
0
  params->non_coloc_6ghz = src->non_coloc_6ghz;
3640
0
  params->min_probe_req_content = src->min_probe_req_content;
3641
0
  return params;
3642
3643
0
failed:
3644
0
  wpa_scan_free_params(params);
3645
0
  return NULL;
3646
0
}
3647
3648
3649
void wpa_scan_free_params(struct wpa_driver_scan_params *params)
3650
0
{
3651
0
  size_t i;
3652
3653
0
  if (params == NULL)
3654
0
    return;
3655
3656
0
  for (i = 0; i < params->num_ssids; i++)
3657
0
    os_free((u8 *) params->ssids[i].ssid);
3658
0
  os_free((u8 *) params->extra_ies);
3659
0
  os_free(params->freqs);
3660
0
  os_free(params->filter_ssids);
3661
0
  os_free(params->sched_scan_plans);
3662
3663
  /*
3664
   * Note: params->mac_addr_mask points to same memory allocation and
3665
   * must not be freed separately.
3666
   */
3667
0
  os_free((u8 *) params->mac_addr);
3668
3669
0
  os_free((u8 *) params->bssid);
3670
3671
0
  os_free(params);
3672
0
}
3673
3674
3675
int wpas_start_pno(struct wpa_supplicant *wpa_s)
3676
0
{
3677
0
  int ret;
3678
0
  size_t prio, i, num_ssid, num_match_ssid;
3679
0
  struct wpa_ssid *ssid;
3680
0
  struct wpa_driver_scan_params params;
3681
0
  struct sched_scan_plan scan_plan;
3682
0
  unsigned int max_sched_scan_ssids;
3683
3684
0
  if (!wpa_s->sched_scan_supported)
3685
0
    return -1;
3686
3687
0
  if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
3688
0
    max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
3689
0
  else
3690
0
    max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
3691
0
  if (max_sched_scan_ssids < 1)
3692
0
    return -1;
3693
3694
0
  if (wpa_s->pno || wpa_s->pno_sched_pending)
3695
0
    return 0;
3696
3697
0
  if ((wpa_s->wpa_state > WPA_SCANNING) &&
3698
0
      (wpa_s->wpa_state < WPA_COMPLETED)) {
3699
0
    wpa_printf(MSG_ERROR, "PNO: In assoc process");
3700
0
    return -EAGAIN;
3701
0
  }
3702
3703
0
  if (wpa_s->wpa_state == WPA_SCANNING) {
3704
0
    wpa_supplicant_cancel_scan(wpa_s);
3705
0
    if (wpa_s->sched_scanning) {
3706
0
      wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
3707
0
           "ongoing sched scan");
3708
0
      wpa_supplicant_cancel_sched_scan(wpa_s);
3709
0
      wpa_s->pno_sched_pending = 1;
3710
0
      return 0;
3711
0
    }
3712
0
  }
3713
3714
0
  if (wpa_s->sched_scan_stop_req) {
3715
0
    wpa_printf(MSG_DEBUG,
3716
0
         "Schedule PNO after previous sched scan has stopped");
3717
0
    wpa_s->pno_sched_pending = 1;
3718
0
    return 0;
3719
0
  }
3720
3721
0
  os_memset(&params, 0, sizeof(params));
3722
3723
0
  num_ssid = num_match_ssid = 0;
3724
0
  ssid = wpa_s->conf->ssid;
3725
0
  while (ssid) {
3726
0
    if (!wpas_network_disabled(wpa_s, ssid)) {
3727
0
      num_match_ssid++;
3728
0
      if (ssid->scan_ssid)
3729
0
        num_ssid++;
3730
0
    }
3731
0
    ssid = ssid->next;
3732
0
  }
3733
3734
0
  if (num_match_ssid == 0) {
3735
0
    wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
3736
0
    return -1;
3737
0
  }
3738
3739
0
  if (num_match_ssid > num_ssid) {
3740
0
    params.num_ssids++; /* wildcard */
3741
0
    num_ssid++;
3742
0
  }
3743
3744
0
  if (num_ssid > max_sched_scan_ssids) {
3745
0
    wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
3746
0
         "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3747
0
    num_ssid = max_sched_scan_ssids;
3748
0
  }
3749
3750
0
  if (num_match_ssid > wpa_s->max_match_sets) {
3751
0
    num_match_ssid = wpa_s->max_match_sets;
3752
0
    wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
3753
0
  }
3754
0
  params.filter_ssids = os_calloc(num_match_ssid,
3755
0
          sizeof(struct wpa_driver_scan_filter));
3756
0
  if (params.filter_ssids == NULL)
3757
0
    return -1;
3758
3759
0
  i = 0;
3760
0
  prio = 0;
3761
0
  ssid = wpa_s->conf->pssid[prio];
3762
0
  while (ssid) {
3763
0
    if (!wpas_network_disabled(wpa_s, ssid)) {
3764
0
      if (ssid->scan_ssid && params.num_ssids < num_ssid) {
3765
0
        params.ssids[params.num_ssids].ssid =
3766
0
          ssid->ssid;
3767
0
        params.ssids[params.num_ssids].ssid_len =
3768
0
           ssid->ssid_len;
3769
0
        params.num_ssids++;
3770
0
      }
3771
0
      os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
3772
0
          ssid->ssid_len);
3773
0
      params.filter_ssids[i].ssid_len = ssid->ssid_len;
3774
0
      params.num_filter_ssids++;
3775
0
      i++;
3776
0
      if (i == num_match_ssid)
3777
0
        break;
3778
0
    }
3779
0
    if (ssid->pnext)
3780
0
      ssid = ssid->pnext;
3781
0
    else if (prio + 1 == wpa_s->conf->num_prio)
3782
0
      break;
3783
0
    else
3784
0
      ssid = wpa_s->conf->pssid[++prio];
3785
0
  }
3786
3787
0
  if (wpa_s->conf->filter_rssi)
3788
0
    params.filter_rssi = wpa_s->conf->filter_rssi;
3789
3790
0
  if (wpa_s->sched_scan_plans_num) {
3791
0
    params.sched_scan_plans = wpa_s->sched_scan_plans;
3792
0
    params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3793
0
  } else {
3794
    /* Set one scan plan that will run infinitely */
3795
0
    if (wpa_s->conf->sched_scan_interval)
3796
0
      scan_plan.interval = wpa_s->conf->sched_scan_interval;
3797
0
    else
3798
0
      scan_plan.interval = 10;
3799
3800
0
    scan_plan.iterations = 0;
3801
0
    params.sched_scan_plans = &scan_plan;
3802
0
    params.sched_scan_plans_num = 1;
3803
0
  }
3804
3805
0
  params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3806
3807
0
  if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3808
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3809
0
    params.freqs = wpa_s->manual_sched_scan_freqs;
3810
0
  }
3811
3812
0
  if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
3813
0
      wpa_s->wpa_state <= WPA_SCANNING)
3814
0
    wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
3815
3816
0
  wpa_scan_set_relative_rssi_params(wpa_s, &params);
3817
3818
0
  ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
3819
0
  os_free(params.filter_ssids);
3820
0
  os_free(params.mac_addr);
3821
0
  if (ret == 0)
3822
0
    wpa_s->pno = 1;
3823
0
  else
3824
0
    wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3825
0
  return ret;
3826
0
}
3827
3828
3829
int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3830
0
{
3831
0
  int ret = 0;
3832
3833
0
  if (!wpa_s->pno)
3834
0
    return 0;
3835
3836
0
  ret = wpa_supplicant_stop_sched_scan(wpa_s);
3837
0
  wpa_s->sched_scan_stop_req = 1;
3838
3839
0
  wpa_s->pno = 0;
3840
0
  wpa_s->pno_sched_pending = 0;
3841
3842
0
  if (wpa_s->wpa_state == WPA_SCANNING)
3843
0
    wpa_supplicant_req_scan(wpa_s, 0, 0);
3844
3845
0
  return ret;
3846
0
}
3847
3848
3849
void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3850
            unsigned int type)
3851
0
{
3852
0
  type &= MAC_ADDR_RAND_ALL;
3853
0
  wpa_s->mac_addr_rand_enable &= ~type;
3854
3855
0
  if (type & MAC_ADDR_RAND_SCAN) {
3856
0
    os_free(wpa_s->mac_addr_scan);
3857
0
    wpa_s->mac_addr_scan = NULL;
3858
0
  }
3859
3860
0
  if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3861
0
    os_free(wpa_s->mac_addr_sched_scan);
3862
0
    wpa_s->mac_addr_sched_scan = NULL;
3863
0
  }
3864
3865
0
  if (type & MAC_ADDR_RAND_PNO) {
3866
0
    os_free(wpa_s->mac_addr_pno);
3867
0
    wpa_s->mac_addr_pno = NULL;
3868
0
  }
3869
0
}
3870
3871
3872
int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3873
        unsigned int type, const u8 *addr,
3874
        const u8 *mask)
3875
0
{
3876
0
  u8 *tmp = NULL;
3877
3878
0
  if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3879
0
    wpa_printf(MSG_INFO,
3880
0
         "scan: MAC randomization type %u != supported=%u",
3881
0
         type, wpa_s->mac_addr_rand_supported);
3882
0
    return -1;
3883
0
  }
3884
3885
0
  wpas_mac_addr_rand_scan_clear(wpa_s, type);
3886
3887
0
  if (addr) {
3888
0
    tmp = os_malloc(2 * ETH_ALEN);
3889
0
    if (!tmp)
3890
0
      return -1;
3891
0
    os_memcpy(tmp, addr, ETH_ALEN);
3892
0
    os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3893
0
  }
3894
3895
0
  if (type == MAC_ADDR_RAND_SCAN) {
3896
0
    wpa_s->mac_addr_scan = tmp;
3897
0
  } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3898
0
    wpa_s->mac_addr_sched_scan = tmp;
3899
0
  } else if (type == MAC_ADDR_RAND_PNO) {
3900
0
    wpa_s->mac_addr_pno = tmp;
3901
0
  } else {
3902
0
    wpa_printf(MSG_INFO,
3903
0
         "scan: Invalid MAC randomization type=0x%x",
3904
0
         type);
3905
0
    os_free(tmp);
3906
0
    return -1;
3907
0
  }
3908
3909
0
  wpa_s->mac_addr_rand_enable |= type;
3910
0
  return 0;
3911
0
}
3912
3913
3914
int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3915
             unsigned int type, u8 *mask)
3916
0
{
3917
0
  const u8 *to_copy;
3918
3919
0
  if ((wpa_s->mac_addr_rand_enable & type) != type)
3920
0
    return -1;
3921
3922
0
  if (type == MAC_ADDR_RAND_SCAN) {
3923
0
    to_copy = wpa_s->mac_addr_scan;
3924
0
  } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3925
0
    to_copy = wpa_s->mac_addr_sched_scan;
3926
0
  } else if (type == MAC_ADDR_RAND_PNO) {
3927
0
    to_copy = wpa_s->mac_addr_pno;
3928
0
  } else {
3929
0
    wpa_printf(MSG_DEBUG,
3930
0
         "scan: Invalid MAC randomization type=0x%x",
3931
0
         type);
3932
0
    return -1;
3933
0
  }
3934
3935
0
  os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3936
0
  return 0;
3937
0
}
3938
3939
3940
int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3941
0
{
3942
0
  struct wpa_radio_work *work;
3943
0
  struct wpa_radio *radio = wpa_s->radio;
3944
3945
0
  dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3946
0
    if (work->wpa_s != wpa_s || !work->started ||
3947
0
        (os_strcmp(work->type, "scan") != 0 &&
3948
0
         os_strcmp(work->type, "p2p-scan") != 0))
3949
0
      continue;
3950
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
3951
0
    return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
3952
0
  }
3953
3954
0
  wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3955
0
  return -1;
3956
0
}
3957
3958
3959
int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3960
0
{
3961
0
  struct sched_scan_plan *scan_plans = NULL;
3962
0
  const char *token, *context = NULL;
3963
0
  unsigned int num = 0;
3964
3965
0
  if (!cmd)
3966
0
    return -1;
3967
3968
0
  if (!cmd[0]) {
3969
0
    wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3970
0
    os_free(wpa_s->sched_scan_plans);
3971
0
    wpa_s->sched_scan_plans = NULL;
3972
0
    wpa_s->sched_scan_plans_num = 0;
3973
0
    return 0;
3974
0
  }
3975
3976
0
  while ((token = cstr_token(cmd, " ", &context))) {
3977
0
    int ret;
3978
0
    struct sched_scan_plan *scan_plan, *n;
3979
3980
0
    n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3981
0
    if (!n)
3982
0
      goto fail;
3983
3984
0
    scan_plans = n;
3985
0
    scan_plan = &scan_plans[num];
3986
0
    num++;
3987
3988
0
    ret = sscanf(token, "%u:%u", &scan_plan->interval,
3989
0
           &scan_plan->iterations);
3990
0
    if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3991
0
      wpa_printf(MSG_ERROR,
3992
0
           "Invalid sched scan plan input: %s", token);
3993
0
      goto fail;
3994
0
    }
3995
3996
0
    if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3997
0
      wpa_printf(MSG_WARNING,
3998
0
           "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3999
0
           num, scan_plan->interval,
4000
0
           wpa_s->max_sched_scan_plan_interval);
4001
0
      scan_plan->interval =
4002
0
        wpa_s->max_sched_scan_plan_interval;
4003
0
    }
4004
4005
0
    if (ret == 1) {
4006
0
      scan_plan->iterations = 0;
4007
0
      break;
4008
0
    }
4009
4010
0
    if (!scan_plan->iterations) {
4011
0
      wpa_printf(MSG_ERROR,
4012
0
           "scan plan %u: Number of iterations cannot be zero",
4013
0
           num);
4014
0
      goto fail;
4015
0
    }
4016
4017
0
    if (scan_plan->iterations >
4018
0
        wpa_s->max_sched_scan_plan_iterations) {
4019
0
      wpa_printf(MSG_WARNING,
4020
0
           "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
4021
0
           num, scan_plan->iterations,
4022
0
           wpa_s->max_sched_scan_plan_iterations);
4023
0
      scan_plan->iterations =
4024
0
        wpa_s->max_sched_scan_plan_iterations;
4025
0
    }
4026
4027
0
    wpa_printf(MSG_DEBUG,
4028
0
         "scan plan %u: interval=%u iterations=%u",
4029
0
         num, scan_plan->interval, scan_plan->iterations);
4030
0
  }
4031
4032
0
  if (!scan_plans) {
4033
0
    wpa_printf(MSG_ERROR, "Invalid scan plans entry");
4034
0
    goto fail;
4035
0
  }
4036
4037
0
  if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
4038
0
    wpa_printf(MSG_ERROR,
4039
0
         "All scan plans but the last must specify a number of iterations");
4040
0
    goto fail;
4041
0
  }
4042
4043
0
  wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
4044
0
       num, scan_plans[num - 1].interval);
4045
4046
0
  if (num > wpa_s->max_sched_scan_plans) {
4047
0
    wpa_printf(MSG_WARNING,
4048
0
         "Too many scheduled scan plans (only %u supported)",
4049
0
         wpa_s->max_sched_scan_plans);
4050
0
    wpa_printf(MSG_WARNING,
4051
0
         "Use only the first %u scan plans, and the last one (in infinite loop)",
4052
0
         wpa_s->max_sched_scan_plans - 1);
4053
0
    os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
4054
0
        &scan_plans[num - 1], sizeof(*scan_plans));
4055
0
    num = wpa_s->max_sched_scan_plans;
4056
0
  }
4057
4058
0
  os_free(wpa_s->sched_scan_plans);
4059
0
  wpa_s->sched_scan_plans = scan_plans;
4060
0
  wpa_s->sched_scan_plans_num = num;
4061
4062
0
  return 0;
4063
4064
0
fail:
4065
0
  os_free(scan_plans);
4066
0
  wpa_printf(MSG_ERROR, "invalid scan plans list");
4067
0
  return -1;
4068
0
}
4069
4070
4071
/**
4072
 * wpas_scan_reset_sched_scan - Reset sched_scan state
4073
 * @wpa_s: Pointer to wpa_supplicant data
4074
 *
4075
 * This function is used to cancel a running scheduled scan and to reset an
4076
 * internal scan state to continue with a regular scan on the following
4077
 * wpa_supplicant_req_scan() calls.
4078
 */
4079
void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
4080
0
{
4081
0
  wpa_s->normal_scans = 0;
4082
0
  if (wpa_s->sched_scanning) {
4083
0
    wpa_s->sched_scan_timed_out = 0;
4084
0
    wpa_s->prev_sched_ssid = NULL;
4085
0
    wpa_supplicant_cancel_sched_scan(wpa_s);
4086
0
  }
4087
0
}
4088
4089
4090
void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
4091
0
{
4092
  /* simulate timeout to restart the sched scan */
4093
0
  wpa_s->sched_scan_timed_out = 1;
4094
0
  wpa_s->prev_sched_ssid = NULL;
4095
0
  wpa_supplicant_cancel_sched_scan(wpa_s);
4096
0
}