Coverage Report

Created: 2024-07-23 06:36

/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
      wpabuf_resize(&extra_ie, ext_capab_len) == 0)
754
0
    wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
755
756
0
#ifdef CONFIG_INTERWORKING
757
0
  if (wpa_s->conf->interworking &&
758
0
      wpabuf_resize(&extra_ie, 100) == 0)
759
0
    wpas_add_interworking_elements(wpa_s, extra_ie);
760
0
#endif /* CONFIG_INTERWORKING */
761
762
#ifdef CONFIG_MBO
763
  if (wpa_s->enable_oce & OCE_STA)
764
    wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
765
#endif /* CONFIG_MBO */
766
767
#ifdef CONFIG_WPS
768
  wps = wpas_wps_in_use(wpa_s, &req_type);
769
770
  if (wps) {
771
    struct wpabuf *wps_ie;
772
    wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
773
            DEV_PW_DEFAULT,
774
            &wpa_s->wps->dev,
775
            wpa_s->wps->uuid, req_type,
776
            0, NULL);
777
    if (wps_ie) {
778
      if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
779
        wpabuf_put_buf(extra_ie, wps_ie);
780
      wpabuf_free(wps_ie);
781
    }
782
  }
783
784
#ifdef CONFIG_P2P
785
  if (wps) {
786
    size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
787
    if (wpabuf_resize(&extra_ie, ielen) == 0)
788
      wpas_p2p_scan_ie(wpa_s, extra_ie);
789
  }
790
#endif /* CONFIG_P2P */
791
792
  wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
793
794
#endif /* CONFIG_WPS */
795
796
0
#ifdef CONFIG_HS20
797
0
  if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
798
0
    wpas_hs20_add_indication(extra_ie, -1, 0);
799
0
#endif /* CONFIG_HS20 */
800
801
#ifdef CONFIG_FST
802
  if (wpa_s->fst_ies &&
803
      wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
804
    wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
805
#endif /* CONFIG_FST */
806
807
#ifdef CONFIG_MBO
808
  /* Send MBO and OCE capabilities */
809
  if (wpabuf_resize(&extra_ie, 12) == 0)
810
    wpas_mbo_scan_ie(wpa_s, extra_ie);
811
#endif /* CONFIG_MBO */
812
813
0
  if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
814
0
    struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
815
816
0
    if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
817
0
      wpabuf_put_buf(extra_ie, buf);
818
0
  }
819
820
0
  return extra_ie;
821
0
}
822
823
824
#ifdef CONFIG_P2P
825
826
/*
827
 * Check whether there are any enabled networks or credentials that could be
828
 * used for a non-P2P connection.
829
 */
830
static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
831
{
832
  struct wpa_ssid *ssid;
833
834
  for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
835
    if (wpas_network_disabled(wpa_s, ssid))
836
      continue;
837
    if (!ssid->p2p_group)
838
      return 1;
839
  }
840
841
  if (wpa_s->conf->cred && wpa_s->conf->interworking &&
842
      wpa_s->conf->auto_interworking)
843
    return 1;
844
845
  return 0;
846
}
847
848
#endif /* CONFIG_P2P */
849
850
851
int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
852
          enum hostapd_hw_mode band,
853
          struct wpa_driver_scan_params *params,
854
          bool is_6ghz, bool only_6ghz_psc,
855
          bool exclude_radar)
856
0
{
857
  /* Include only supported channels for the specified band */
858
0
  struct hostapd_hw_modes *mode;
859
0
  int num_chans = 0;
860
0
  int *freqs, i;
861
862
0
  mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
863
0
  if (!mode || !mode->num_channels)
864
0
    return -1;
865
866
0
  if (params->freqs) {
867
0
    while (params->freqs[num_chans])
868
0
      num_chans++;
869
0
  }
870
871
0
  freqs = os_realloc(params->freqs,
872
0
         (num_chans + mode->num_channels + 1) * sizeof(int));
873
0
  if (!freqs)
874
0
    return -1;
875
876
0
  params->freqs = freqs;
877
0
  for (i = 0; i < mode->num_channels; i++) {
878
0
    if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
879
0
      continue;
880
0
    if (exclude_radar &&
881
0
        (mode->channels[i].flag & HOSTAPD_CHAN_RADAR))
882
0
      continue;
883
884
0
    if (is_6ghz && only_6ghz_psc &&
885
0
        !is_6ghz_psc_frequency(mode->channels[i].freq))
886
0
      continue;
887
888
0
    params->freqs[num_chans++] = mode->channels[i].freq;
889
0
  }
890
0
  params->freqs[num_chans] = 0;
891
892
0
  return 0;
893
0
}
894
895
896
static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
897
           struct wpa_driver_scan_params *params)
898
0
{
899
0
  if (wpa_s->hw.modes == NULL)
900
0
    return; /* unknown what channels the driver supports */
901
0
  if (params->freqs)
902
0
    return; /* already using a limited channel set */
903
904
0
  if (wpa_s->setband_mask & WPA_SETBAND_5G)
905
0
    wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
906
0
          false, false, false);
907
0
  if (wpa_s->setband_mask & WPA_SETBAND_2G)
908
0
    wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
909
0
          false, false, false);
910
0
  if (wpa_s->setband_mask & WPA_SETBAND_6G)
911
0
    wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
912
0
          true, false, false);
913
0
}
914
915
916
static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
917
            struct wpa_driver_scan_params *params,
918
            size_t max_ssids, const u8 *ssid, size_t ssid_len)
919
0
{
920
0
  unsigned int j;
921
922
0
  for (j = 0; j < params->num_ssids; j++) {
923
0
    if (params->ssids[j].ssid_len == ssid_len &&
924
0
        params->ssids[j].ssid &&
925
0
        os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
926
0
      return; /* already in the list */
927
0
  }
928
929
0
  if (params->num_ssids + 1 > max_ssids) {
930
0
    wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
931
0
    return;
932
0
  }
933
934
0
  wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
935
0
       wpa_ssid_txt(ssid, ssid_len));
936
937
0
  params->ssids[params->num_ssids].ssid = ssid;
938
0
  params->ssids[params->num_ssids].ssid_len = ssid_len;
939
0
  params->num_ssids++;
940
0
}
941
942
943
static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
944
          struct wpa_driver_scan_params *params,
945
          struct wpa_ssid *ssid, size_t max_ssids)
946
0
{
947
#ifdef CONFIG_OWE
948
  struct wpa_bss *bss;
949
950
  if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
951
    return;
952
953
  wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
954
       wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
955
956
  dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
957
    const u8 *owe, *pos, *end;
958
    const u8 *owe_ssid;
959
    size_t owe_ssid_len;
960
961
    if (bss->ssid_len != ssid->ssid_len ||
962
        os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
963
      continue;
964
965
    owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
966
    if (!owe || owe[1] < 4)
967
      continue;
968
969
    pos = owe + 6;
970
    end = owe + 2 + owe[1];
971
972
    /* Must include BSSID and ssid_len */
973
    if (end - pos < ETH_ALEN + 1)
974
      return;
975
976
    /* Skip BSSID */
977
    pos += ETH_ALEN;
978
    owe_ssid_len = *pos++;
979
    owe_ssid = pos;
980
981
    if ((size_t) (end - pos) < owe_ssid_len ||
982
        owe_ssid_len > SSID_MAX_LEN)
983
      return;
984
985
    wpa_printf(MSG_DEBUG,
986
         "OWE: scan_ssids: transition mode OWE ssid=%s",
987
         wpa_ssid_txt(owe_ssid, owe_ssid_len));
988
989
    wpa_add_scan_ssid(wpa_s, params, max_ssids,
990
          owe_ssid, owe_ssid_len);
991
    return;
992
  }
993
#endif /* CONFIG_OWE */
994
0
}
995
996
997
static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
998
             struct wpa_driver_scan_params *params,
999
             size_t max_ssids)
1000
0
{
1001
0
  unsigned int i;
1002
0
  struct wpa_ssid *ssid;
1003
1004
  /*
1005
   * For devices with max_ssids greater than 1, leave the last slot empty
1006
   * for adding the wildcard scan entry.
1007
   */
1008
0
  max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
1009
1010
0
  for (i = 0; i < wpa_s->scan_id_count; i++) {
1011
0
    ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
1012
0
    if (!ssid)
1013
0
      continue;
1014
0
    if (ssid->scan_ssid)
1015
0
      wpa_add_scan_ssid(wpa_s, params, max_ssids,
1016
0
            ssid->ssid, ssid->ssid_len);
1017
    /*
1018
     * Also add the SSID of the OWE BSS, to allow discovery of
1019
     * transition mode APs more quickly.
1020
     */
1021
0
    wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
1022
0
  }
1023
1024
0
  wpa_s->scan_id_count = 0;
1025
0
}
1026
1027
1028
static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
1029
               struct wpa_driver_scan_params *params,
1030
               size_t max_ssids)
1031
0
{
1032
0
  unsigned int i;
1033
1034
0
  if (wpa_s->ssids_from_scan_req == NULL ||
1035
0
      wpa_s->num_ssids_from_scan_req == 0)
1036
0
    return 0;
1037
1038
0
  if (wpa_s->num_ssids_from_scan_req > max_ssids) {
1039
0
    wpa_s->num_ssids_from_scan_req = max_ssids;
1040
0
    wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
1041
0
         (unsigned int) max_ssids);
1042
0
  }
1043
1044
0
  for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
1045
0
    params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
1046
0
    params->ssids[i].ssid_len =
1047
0
      wpa_s->ssids_from_scan_req[i].ssid_len;
1048
0
    wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
1049
0
          params->ssids[i].ssid,
1050
0
          params->ssids[i].ssid_len);
1051
0
  }
1052
1053
0
  params->num_ssids = wpa_s->num_ssids_from_scan_req;
1054
0
  wpa_s->num_ssids_from_scan_req = 0;
1055
0
  return 1;
1056
0
}
1057
1058
1059
static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
1060
0
{
1061
0
  struct wpa_supplicant *wpa_s = eloop_ctx;
1062
0
  struct wpa_ssid *ssid;
1063
0
  int ret, p2p_in_prog;
1064
0
  struct wpabuf *extra_ie = NULL;
1065
0
  struct wpa_driver_scan_params params;
1066
0
  struct wpa_driver_scan_params *scan_params;
1067
0
  size_t max_ssids;
1068
0
  int connect_without_scan = 0;
1069
1070
0
  wpa_s->ignore_post_flush_scan_res = 0;
1071
1072
0
  if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1073
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
1074
0
    return;
1075
0
  }
1076
1077
0
  if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
1078
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
1079
0
    wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1080
0
    return;
1081
0
  }
1082
1083
0
  if (wpa_s->scanning) {
1084
    /*
1085
     * If we are already in scanning state, we shall reschedule the
1086
     * the incoming scan request.
1087
     */
1088
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
1089
0
    wpa_supplicant_req_scan(wpa_s, 1, 0);
1090
0
    return;
1091
0
  }
1092
1093
0
  if (!wpa_supplicant_enabled_networks(wpa_s) &&
1094
0
      wpa_s->scan_req == NORMAL_SCAN_REQ) {
1095
0
    wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
1096
0
    wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1097
0
    return;
1098
0
  }
1099
1100
0
  if (wpa_s->conf->ap_scan != 0 &&
1101
0
      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
1102
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
1103
0
      "overriding ap_scan configuration");
1104
0
    wpa_s->conf->ap_scan = 0;
1105
0
    wpas_notify_ap_scan_changed(wpa_s);
1106
0
  }
1107
1108
0
  if (wpa_s->conf->ap_scan == 0) {
1109
0
    wpa_supplicant_gen_assoc_event(wpa_s);
1110
0
    return;
1111
0
  }
1112
1113
0
  ssid = NULL;
1114
0
  if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
1115
0
      wpa_s->connect_without_scan) {
1116
0
    connect_without_scan = 1;
1117
0
    for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1118
0
      if (ssid == wpa_s->connect_without_scan)
1119
0
        break;
1120
0
    }
1121
0
  }
1122
1123
0
  p2p_in_prog = wpas_p2p_in_progress(wpa_s);
1124
0
  if (p2p_in_prog && p2p_in_prog != 2 &&
1125
0
      (!ssid ||
1126
0
       (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
1127
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
1128
0
    wpa_supplicant_req_scan(wpa_s, 5, 0);
1129
0
    return;
1130
0
  }
1131
1132
  /*
1133
   * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
1134
   * used for changing modes inside wpa_supplicant (roaming,
1135
   * auto-reconnect, etc). Discarding the scan might hurt these processes.
1136
   * The normal use case for PNO is to suspend the host immediately after
1137
   * starting PNO, so the periodic 100 ms attempts to run the scan do not
1138
   * normally happen in practice multiple times, i.e., this is simply
1139
   * restarting scanning once the host is woken up and PNO stopped.
1140
   */
1141
0
  if (wpa_s->pno || wpa_s->pno_sched_pending) {
1142
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
1143
0
    wpa_supplicant_req_scan(wpa_s, 0, 100000);
1144
0
    return;
1145
0
  }
1146
1147
0
  if (wpa_s->conf->ap_scan == 2)
1148
0
    max_ssids = 1;
1149
0
  else {
1150
0
    max_ssids = wpa_s->max_scan_ssids;
1151
0
    if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1152
0
      max_ssids = WPAS_MAX_SCAN_SSIDS;
1153
0
  }
1154
1155
0
  wpa_s->last_scan_req = wpa_s->scan_req;
1156
0
  wpa_s->scan_req = NORMAL_SCAN_REQ;
1157
1158
0
  if (connect_without_scan) {
1159
0
    wpa_s->connect_without_scan = NULL;
1160
0
    if (ssid) {
1161
0
      wpa_printf(MSG_DEBUG, "Start a pre-selected network "
1162
0
           "without scan step");
1163
0
      wpa_supplicant_associate(wpa_s, NULL, ssid);
1164
0
      return;
1165
0
    }
1166
0
  }
1167
1168
0
  os_memset(&params, 0, sizeof(params));
1169
1170
0
  wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
1171
0
  if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1172
0
      wpa_s->wpa_state == WPA_INACTIVE)
1173
0
    wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1174
1175
  /*
1176
   * If autoscan has set its own scanning parameters
1177
   */
1178
0
  if (wpa_s->autoscan_params != NULL) {
1179
0
    scan_params = wpa_s->autoscan_params;
1180
0
    goto scan;
1181
0
  }
1182
1183
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1184
0
      wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1185
0
    wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1186
0
    goto ssid_list_set;
1187
0
  }
1188
1189
#ifdef CONFIG_P2P
1190
  if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
1191
      wpa_s->go_params && !wpa_s->conf->passive_scan) {
1192
    wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1193
         wpa_s->p2p_in_provisioning,
1194
         wpa_s->show_group_started);
1195
    params.ssids[0].ssid = wpa_s->go_params->ssid;
1196
    params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1197
    params.num_ssids = 1;
1198
    params.bssid = wpa_s->go_params->peer_interface_addr;
1199
    wpa_printf(MSG_DEBUG, "P2P: Use specific BSSID " MACSTR
1200
         " (peer interface address) for scan",
1201
         MAC2STR(params.bssid));
1202
    goto ssid_list_set;
1203
  }
1204
1205
  if (wpa_s->p2p_in_invitation) {
1206
    if (wpa_s->current_ssid) {
1207
      wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1208
      params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1209
      params.ssids[0].ssid_len =
1210
        wpa_s->current_ssid->ssid_len;
1211
      params.num_ssids = 1;
1212
      if (wpa_s->current_ssid->bssid_set) {
1213
        params.bssid = wpa_s->current_ssid->bssid;
1214
        wpa_printf(MSG_DEBUG, "P2P: Use specific BSSID "
1215
             MACSTR " for scan",
1216
             MAC2STR(params.bssid));
1217
      }
1218
    } else {
1219
      wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1220
    }
1221
    goto ssid_list_set;
1222
  }
1223
#endif /* CONFIG_P2P */
1224
1225
  /* Find the starting point from which to continue scanning */
1226
0
  ssid = wpa_s->conf->ssid;
1227
0
  if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1228
0
    while (ssid) {
1229
0
      if (ssid == wpa_s->prev_scan_ssid) {
1230
0
        ssid = ssid->next;
1231
0
        break;
1232
0
      }
1233
0
      ssid = ssid->next;
1234
0
    }
1235
0
  }
1236
1237
0
  if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
1238
#ifdef CONFIG_AP
1239
      !wpa_s->ap_iface &&
1240
#endif /* CONFIG_AP */
1241
0
      wpa_s->conf->ap_scan == 2) {
1242
0
    wpa_s->connect_without_scan = NULL;
1243
0
    wpa_s->prev_scan_wildcard = 0;
1244
0
    wpa_supplicant_assoc_try(wpa_s, ssid);
1245
0
    return;
1246
0
  } else if (wpa_s->conf->ap_scan == 2) {
1247
    /*
1248
     * User-initiated scan request in ap_scan == 2; scan with
1249
     * wildcard SSID.
1250
     */
1251
0
    ssid = NULL;
1252
0
  } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1253
    /*
1254
     * Perform single-channel single-SSID scan for
1255
     * reassociate-to-same-BSS operation.
1256
     */
1257
    /* Setup SSID */
1258
0
    ssid = wpa_s->current_ssid;
1259
0
    wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1260
0
          ssid->ssid, ssid->ssid_len);
1261
0
    params.ssids[0].ssid = ssid->ssid;
1262
0
    params.ssids[0].ssid_len = ssid->ssid_len;
1263
0
    params.num_ssids = 1;
1264
1265
    /*
1266
     * Allocate memory for frequency array, allocate one extra
1267
     * slot for the zero-terminator.
1268
     */
1269
0
    params.freqs = os_malloc(sizeof(int) * 2);
1270
0
    if (params.freqs) {
1271
0
      params.freqs[0] = wpa_s->assoc_freq;
1272
0
      params.freqs[1] = 0;
1273
0
    }
1274
1275
    /*
1276
     * Reset the reattach flag so that we fall back to full scan if
1277
     * this scan fails.
1278
     */
1279
0
    wpa_s->reattach = 0;
1280
0
  } else {
1281
0
    struct wpa_ssid *start = ssid, *tssid;
1282
0
    int freqs_set = 0;
1283
0
    if (ssid == NULL && max_ssids > 1)
1284
0
      ssid = wpa_s->conf->ssid;
1285
0
    while (ssid) {
1286
0
      if (!wpas_network_disabled(wpa_s, ssid) &&
1287
0
          ssid->scan_ssid) {
1288
0
        wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1289
0
              ssid->ssid, ssid->ssid_len);
1290
0
        params.ssids[params.num_ssids].ssid =
1291
0
          ssid->ssid;
1292
0
        params.ssids[params.num_ssids].ssid_len =
1293
0
          ssid->ssid_len;
1294
0
        params.num_ssids++;
1295
0
        if (params.num_ssids + 1 >= max_ssids)
1296
0
          break;
1297
0
      }
1298
1299
0
      if (!wpas_network_disabled(wpa_s, ssid)) {
1300
        /*
1301
         * Also add the SSID of the OWE BSS, to allow
1302
         * discovery of transition mode APs more
1303
         * quickly.
1304
         */
1305
0
        wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1306
0
                  max_ssids);
1307
0
      }
1308
1309
0
      ssid = ssid->next;
1310
0
      if (ssid == start)
1311
0
        break;
1312
0
      if (ssid == NULL && max_ssids > 1 &&
1313
0
          start != wpa_s->conf->ssid)
1314
0
        ssid = wpa_s->conf->ssid;
1315
0
    }
1316
1317
0
    if (wpa_s->scan_id_count &&
1318
0
        wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1319
0
      wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1320
1321
0
    for (tssid = wpa_s->conf->ssid;
1322
0
         wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1323
0
         tssid = tssid->next) {
1324
0
      if (wpas_network_disabled(wpa_s, tssid))
1325
0
        continue;
1326
0
      if (((params.freqs || !freqs_set) &&
1327
0
           tssid->scan_freq) &&
1328
0
          int_array_len(params.freqs) < 100) {
1329
0
        int_array_concat(&params.freqs,
1330
0
             tssid->scan_freq);
1331
0
      } else {
1332
0
        os_free(params.freqs);
1333
0
        params.freqs = NULL;
1334
0
      }
1335
0
      freqs_set = 1;
1336
0
    }
1337
0
    int_array_sort_unique(params.freqs);
1338
0
  }
1339
1340
0
  if (ssid && max_ssids == 1) {
1341
    /*
1342
     * If the driver is limited to 1 SSID at a time interleave
1343
     * wildcard SSID scans with specific SSID scans to avoid
1344
     * waiting a long time for a wildcard scan.
1345
     */
1346
0
    if (!wpa_s->prev_scan_wildcard) {
1347
0
      params.ssids[0].ssid = NULL;
1348
0
      params.ssids[0].ssid_len = 0;
1349
0
      wpa_s->prev_scan_wildcard = 1;
1350
0
      wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1351
0
        "wildcard SSID (Interleave with specific)");
1352
0
    } else {
1353
0
      wpa_s->prev_scan_ssid = ssid;
1354
0
      wpa_s->prev_scan_wildcard = 0;
1355
0
      wpa_dbg(wpa_s, MSG_DEBUG,
1356
0
        "Starting AP scan for specific SSID: %s",
1357
0
        wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1358
0
    }
1359
0
  } else if (ssid) {
1360
    /* max_ssids > 1 */
1361
1362
0
    wpa_s->prev_scan_ssid = ssid;
1363
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1364
0
      "the scan request");
1365
0
    params.num_ssids++;
1366
0
  } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1367
0
       wpa_s->manual_scan_passive && params.num_ssids == 0) {
1368
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
1369
0
  } else if (wpa_s->conf->passive_scan) {
1370
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1371
0
      "Use passive scan based on configuration");
1372
0
  } else {
1373
0
    wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1374
0
    params.num_ssids++;
1375
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1376
0
      "SSID");
1377
0
  }
1378
1379
0
ssid_list_set:
1380
0
  wpa_supplicant_optimize_freqs(wpa_s, &params);
1381
0
  extra_ie = wpa_supplicant_extra_ies(wpa_s);
1382
1383
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1384
0
      wpa_s->manual_scan_only_new) {
1385
0
    wpa_printf(MSG_DEBUG,
1386
0
         "Request driver to clear scan cache due to manual only_new=1 scan");
1387
0
    params.only_new_results = 1;
1388
0
  }
1389
1390
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1391
0
      wpa_s->manual_scan_freqs) {
1392
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1393
0
    params.freqs = wpa_s->manual_scan_freqs;
1394
0
    wpa_s->manual_scan_freqs = NULL;
1395
0
  }
1396
1397
0
  if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1398
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1399
0
      "Limit select_network scan to specified channels");
1400
0
    params.freqs = wpa_s->select_network_scan_freqs;
1401
0
    wpa_s->select_network_scan_freqs = NULL;
1402
0
  }
1403
1404
0
  if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1405
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1406
0
      "generated frequency list");
1407
0
    params.freqs = wpa_s->next_scan_freqs;
1408
0
  } else
1409
0
    os_free(wpa_s->next_scan_freqs);
1410
0
  wpa_s->next_scan_freqs = NULL;
1411
0
  wpa_setband_scan_freqs(wpa_s, &params);
1412
1413
  /* See if user specified frequencies. If so, scan only those. */
1414
0
  if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1415
0
      wpa_s->conf->initial_freq_list && !params.freqs) {
1416
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1417
0
      "Optimize scan based on conf->initial_freq_list");
1418
0
    int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1419
0
  } else if (wpa_s->conf->freq_list && !params.freqs) {
1420
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1421
0
      "Optimize scan based on conf->freq_list");
1422
0
    int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1423
0
  }
1424
1425
  /* Use current associated channel? */
1426
0
  if (wpa_s->conf->scan_cur_freq && !params.freqs) {
1427
0
    unsigned int num = wpa_s->num_multichan_concurrent;
1428
1429
0
    params.freqs = os_calloc(num + 1, sizeof(int));
1430
0
    if (params.freqs) {
1431
0
      num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1432
0
                 false);
1433
0
      if (num > 0) {
1434
0
        wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1435
0
          "current operating channels since "
1436
0
          "scan_cur_freq is enabled");
1437
0
      } else {
1438
0
        os_free(params.freqs);
1439
0
        params.freqs = NULL;
1440
0
      }
1441
0
    }
1442
0
  }
1443
1444
#ifdef CONFIG_MBO
1445
  if (wpa_s->enable_oce & OCE_STA)
1446
    params.oce_scan = 1;
1447
#endif /* CONFIG_MBO */
1448
1449
0
  params.filter_ssids = wpa_supplicant_build_filter_ssids(
1450
0
    wpa_s->conf, &params.num_filter_ssids);
1451
0
  if (extra_ie) {
1452
0
    params.extra_ies = wpabuf_head(extra_ie);
1453
0
    params.extra_ies_len = wpabuf_len(extra_ie);
1454
0
  }
1455
1456
#ifdef CONFIG_P2P
1457
  if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
1458
      (wpa_s->show_group_started && wpa_s->go_params)) {
1459
    /*
1460
     * The interface may not yet be in P2P mode, so we have to
1461
     * explicitly request P2P probe to disable CCK rates.
1462
     */
1463
    params.p2p_probe = 1;
1464
  }
1465
#endif /* CONFIG_P2P */
1466
1467
0
  if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
1468
0
      wpa_s->wpa_state <= WPA_SCANNING)
1469
0
    wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
1470
1471
0
  if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1472
0
    struct wpa_bss *bss;
1473
1474
0
    params.bssid = wpa_s->next_scan_bssid;
1475
0
    bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1476
0
    if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1477
0
        bss && bss->ssid_len && params.num_ssids == 1 &&
1478
0
        params.ssids[0].ssid_len == 0) {
1479
0
      params.ssids[0].ssid = bss->ssid;
1480
0
      params.ssids[0].ssid_len = bss->ssid_len;
1481
0
      wpa_dbg(wpa_s, MSG_DEBUG,
1482
0
        "Scan a previously specified BSSID " MACSTR
1483
0
        " and SSID %s",
1484
0
        MAC2STR(params.bssid),
1485
0
        wpa_ssid_txt(bss->ssid, bss->ssid_len));
1486
0
    } else {
1487
0
      wpa_dbg(wpa_s, MSG_DEBUG,
1488
0
        "Scan a previously specified BSSID " MACSTR,
1489
0
        MAC2STR(params.bssid));
1490
0
    }
1491
0
  } else if (!is_zero_ether_addr(wpa_s->ml_probe_bssid)) {
1492
0
    wpa_printf(MSG_DEBUG, "Scanning for ML probe request");
1493
0
    params.bssid = wpa_s->ml_probe_bssid;
1494
0
    params.min_probe_req_content = true;
1495
0
  }
1496
1497
1498
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1499
0
      wpa_s->manual_non_coloc_6ghz) {
1500
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Collocated 6 GHz logic is disabled");
1501
0
    params.non_coloc_6ghz = 1;
1502
0
  }
1503
1504
0
  scan_params = &params;
1505
1506
0
scan:
1507
#ifdef CONFIG_P2P
1508
  /*
1509
   * If the driver does not support multi-channel concurrency and a
1510
   * virtual interface that shares the same radio with the wpa_s interface
1511
   * is operating there may not be need to scan other channels apart from
1512
   * the current operating channel on the other virtual interface. Filter
1513
   * out other channels in case we are trying to find a connection for a
1514
   * station interface when we are not configured to prefer station
1515
   * connection and a concurrent operation is already in process.
1516
   */
1517
  if (wpa_s->scan_for_connection &&
1518
      wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
1519
      !scan_params->freqs && !params.freqs &&
1520
      wpas_is_p2p_prioritized(wpa_s) &&
1521
      wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1522
      non_p2p_network_enabled(wpa_s)) {
1523
    unsigned int num = wpa_s->num_multichan_concurrent;
1524
1525
    params.freqs = os_calloc(num + 1, sizeof(int));
1526
    if (params.freqs) {
1527
      /*
1528
       * Exclude the operating frequency of the current
1529
       * interface since we're looking to transition off of
1530
       * it.
1531
       */
1532
      num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1533
                 true);
1534
      if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1535
        wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1536
      } else {
1537
        os_free(params.freqs);
1538
        params.freqs = NULL;
1539
      }
1540
    }
1541
  }
1542
1543
  if (!params.freqs && wpas_is_6ghz_supported(wpa_s, true) &&
1544
      (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning))
1545
    wpas_p2p_scan_freqs(wpa_s, &params, true);
1546
#endif /* CONFIG_P2P */
1547
1548
0
  ret = wpa_supplicant_trigger_scan(wpa_s, scan_params, false, false);
1549
1550
0
  if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1551
0
      !wpa_s->manual_scan_freqs) {
1552
    /* Restore manual_scan_freqs for the next attempt */
1553
0
    wpa_s->manual_scan_freqs = params.freqs;
1554
0
    params.freqs = NULL;
1555
0
  }
1556
1557
0
  wpabuf_free(extra_ie);
1558
0
  os_free(params.freqs);
1559
0
  os_free(params.filter_ssids);
1560
0
  os_free(params.mac_addr);
1561
1562
0
  if (ret) {
1563
0
    wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
1564
0
    if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1565
0
      wpa_supplicant_set_state(wpa_s,
1566
0
             wpa_s->scan_prev_wpa_state);
1567
    /* Restore scan_req since we will try to scan again */
1568
0
    wpa_s->scan_req = wpa_s->last_scan_req;
1569
0
    wpa_supplicant_req_scan(wpa_s, 1, 0);
1570
0
  } else {
1571
0
    wpa_s->scan_for_connection = 0;
1572
0
#ifdef CONFIG_INTERWORKING
1573
0
    wpa_s->interworking_fast_assoc_tried = 0;
1574
0
#endif /* CONFIG_INTERWORKING */
1575
0
    wpa_s->next_scan_bssid_wildcard_ssid = 0;
1576
0
    if (params.bssid)
1577
0
      os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
1578
0
  }
1579
1580
0
  wpa_s->ml_probe_mld_id = -1;
1581
0
  wpa_s->ml_probe_links = 0;
1582
0
  os_memset(wpa_s->ml_probe_bssid, 0, sizeof(wpa_s->ml_probe_bssid));
1583
0
}
1584
1585
1586
void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1587
0
{
1588
0
  struct os_reltime remaining, new_int;
1589
0
  int cancelled;
1590
1591
0
  cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1592
0
               &remaining);
1593
1594
0
  new_int.sec = sec;
1595
0
  new_int.usec = 0;
1596
0
  if (cancelled && os_reltime_before(&remaining, &new_int)) {
1597
0
    new_int.sec = remaining.sec;
1598
0
    new_int.usec = remaining.usec;
1599
0
  }
1600
1601
0
  if (cancelled) {
1602
0
    eloop_register_timeout(new_int.sec, new_int.usec,
1603
0
               wpa_supplicant_scan, wpa_s, NULL);
1604
0
  }
1605
0
  wpa_s->scan_interval = sec;
1606
0
}
1607
1608
1609
/**
1610
 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1611
 * @wpa_s: Pointer to wpa_supplicant data
1612
 * @sec: Number of seconds after which to scan
1613
 * @usec: Number of microseconds after which to scan
1614
 *
1615
 * This function is used to schedule a scan for neighboring access points after
1616
 * the specified time.
1617
 */
1618
void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1619
1.51k
{
1620
1.51k
  int res;
1621
1622
1.51k
  if (wpa_s->p2p_mgmt) {
1623
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1624
0
      "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1625
0
      sec, usec);
1626
0
    return;
1627
0
  }
1628
1629
1.51k
  res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1630
1.51k
            NULL);
1631
1.51k
  if (res == 1) {
1632
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1633
0
      sec, usec);
1634
1.51k
  } else if (res == 0) {
1635
498
    wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1636
498
      sec, usec);
1637
1.01k
  } else {
1638
1.01k
    wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1639
1.01k
      sec, usec);
1640
1.01k
    eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1641
1.01k
  }
1642
1.51k
}
1643
1644
1645
/**
1646
 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1647
 * @wpa_s: Pointer to wpa_supplicant data
1648
 * @sec: Number of seconds after which to scan
1649
 * @usec: Number of microseconds after which to scan
1650
 * Returns: 0 on success or -1 otherwise
1651
 *
1652
 * This function is used to schedule periodic scans for neighboring
1653
 * access points after the specified time.
1654
 */
1655
int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1656
              int sec, int usec)
1657
0
{
1658
0
  if (!wpa_s->sched_scan_supported)
1659
0
    return -1;
1660
1661
0
  eloop_register_timeout(sec, usec,
1662
0
             wpa_supplicant_delayed_sched_scan_timeout,
1663
0
             wpa_s, NULL);
1664
1665
0
  return 0;
1666
0
}
1667
1668
1669
static void
1670
wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1671
          struct wpa_driver_scan_params *params)
1672
0
{
1673
0
  if (wpa_s->wpa_state != WPA_COMPLETED ||
1674
0
      !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1675
0
      wpa_s->srp.relative_rssi_set == 0)
1676
0
    return;
1677
1678
0
  params->relative_rssi_set = 1;
1679
0
  params->relative_rssi = wpa_s->srp.relative_rssi;
1680
1681
0
  if (wpa_s->srp.relative_adjust_rssi == 0)
1682
0
    return;
1683
1684
0
  params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1685
0
  params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1686
0
}
1687
1688
1689
/**
1690
 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1691
 * @wpa_s: Pointer to wpa_supplicant data
1692
 * Returns: 0 is sched_scan was started or -1 otherwise
1693
 *
1694
 * This function is used to schedule periodic scans for neighboring
1695
 * access points repeating the scan continuously.
1696
 */
1697
int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1698
0
{
1699
0
  struct wpa_driver_scan_params params;
1700
0
  struct wpa_driver_scan_params *scan_params;
1701
0
  enum wpa_states prev_state;
1702
0
  struct wpa_ssid *ssid = NULL;
1703
0
  struct wpabuf *extra_ie = NULL;
1704
0
  int ret;
1705
0
  unsigned int max_sched_scan_ssids;
1706
0
  int wildcard = 0;
1707
0
  int need_ssids;
1708
0
  struct sched_scan_plan scan_plan;
1709
1710
0
  if (!wpa_s->sched_scan_supported)
1711
0
    return -1;
1712
1713
0
  if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1714
0
    max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1715
0
  else
1716
0
    max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1717
0
  if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1718
0
    return -1;
1719
1720
0
  wpa_s->sched_scan_stop_req = 0;
1721
1722
0
  if (wpa_s->sched_scanning) {
1723
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1724
0
    return 0;
1725
0
  }
1726
1727
0
  need_ssids = 0;
1728
0
  for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1729
0
    if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1730
      /* Use wildcard SSID to find this network */
1731
0
      wildcard = 1;
1732
0
    } else if (!wpas_network_disabled(wpa_s, ssid) &&
1733
0
         ssid->ssid_len)
1734
0
      need_ssids++;
1735
1736
#ifdef CONFIG_WPS
1737
    if (!wpas_network_disabled(wpa_s, ssid) &&
1738
        ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1739
      /*
1740
       * Normal scan is more reliable and faster for WPS
1741
       * operations and since these are for short periods of
1742
       * time, the benefit of trying to use sched_scan would
1743
       * be limited.
1744
       */
1745
      wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1746
        "sched_scan for WPS");
1747
      return -1;
1748
    }
1749
#endif /* CONFIG_WPS */
1750
0
  }
1751
0
  if (wildcard)
1752
0
    need_ssids++;
1753
1754
0
  if (wpa_s->normal_scans < 3 &&
1755
0
      (need_ssids <= wpa_s->max_scan_ssids ||
1756
0
       wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1757
    /*
1758
     * When normal scan can speed up operations, use that for the
1759
     * first operations before starting the sched_scan to allow
1760
     * user space sleep more. We do this only if the normal scan
1761
     * has functionality that is suitable for this or if the
1762
     * sched_scan does not have better support for multiple SSIDs.
1763
     */
1764
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1765
0
      "sched_scan for initial scans (normal_scans=%d)",
1766
0
      wpa_s->normal_scans);
1767
0
    return -1;
1768
0
  }
1769
1770
0
  os_memset(&params, 0, sizeof(params));
1771
1772
  /* If we can't allocate space for the filters, we just don't filter */
1773
0
  params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1774
0
          sizeof(struct wpa_driver_scan_filter));
1775
1776
0
  prev_state = wpa_s->wpa_state;
1777
0
  if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1778
0
      wpa_s->wpa_state == WPA_INACTIVE)
1779
0
    wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1780
1781
0
  if (wpa_s->autoscan_params != NULL) {
1782
0
    scan_params = wpa_s->autoscan_params;
1783
0
    goto scan;
1784
0
  }
1785
1786
  /* Find the starting point from which to continue scanning */
1787
0
  ssid = wpa_s->conf->ssid;
1788
0
  if (wpa_s->prev_sched_ssid) {
1789
0
    while (ssid) {
1790
0
      if (ssid == wpa_s->prev_sched_ssid) {
1791
0
        ssid = ssid->next;
1792
0
        break;
1793
0
      }
1794
0
      ssid = ssid->next;
1795
0
    }
1796
0
  }
1797
1798
0
  if (!ssid || !wpa_s->prev_sched_ssid) {
1799
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1800
0
    wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1801
0
    wpa_s->first_sched_scan = 1;
1802
0
    ssid = wpa_s->conf->ssid;
1803
0
    wpa_s->prev_sched_ssid = ssid;
1804
0
  }
1805
1806
0
  if (wildcard) {
1807
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1808
0
    params.num_ssids++;
1809
0
  }
1810
1811
0
  while (ssid) {
1812
0
    if (wpas_network_disabled(wpa_s, ssid))
1813
0
      goto next;
1814
1815
0
    if (params.num_filter_ssids < wpa_s->max_match_sets &&
1816
0
        params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1817
0
      wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1818
0
        wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1819
0
      os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1820
0
          ssid->ssid, ssid->ssid_len);
1821
0
      params.filter_ssids[params.num_filter_ssids].ssid_len =
1822
0
        ssid->ssid_len;
1823
0
      params.num_filter_ssids++;
1824
0
    } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1825
0
    {
1826
0
      wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1827
0
        "filter for sched_scan - drop filter");
1828
0
      os_free(params.filter_ssids);
1829
0
      params.filter_ssids = NULL;
1830
0
      params.num_filter_ssids = 0;
1831
0
    }
1832
1833
0
    if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1834
0
      if (params.num_ssids == max_sched_scan_ssids)
1835
0
        break; /* only room for broadcast SSID */
1836
0
      wpa_dbg(wpa_s, MSG_DEBUG,
1837
0
        "add to active scan ssid: %s",
1838
0
        wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1839
0
      params.ssids[params.num_ssids].ssid =
1840
0
        ssid->ssid;
1841
0
      params.ssids[params.num_ssids].ssid_len =
1842
0
        ssid->ssid_len;
1843
0
      params.num_ssids++;
1844
0
      if (params.num_ssids >= max_sched_scan_ssids) {
1845
0
        wpa_s->prev_sched_ssid = ssid;
1846
0
        do {
1847
0
          ssid = ssid->next;
1848
0
        } while (ssid &&
1849
0
           (wpas_network_disabled(wpa_s, ssid) ||
1850
0
            !ssid->scan_ssid));
1851
0
        break;
1852
0
      }
1853
0
    }
1854
1855
0
  next:
1856
0
    wpa_s->prev_sched_ssid = ssid;
1857
0
    ssid = ssid->next;
1858
0
  }
1859
1860
0
  if (params.num_filter_ssids == 0) {
1861
0
    os_free(params.filter_ssids);
1862
0
    params.filter_ssids = NULL;
1863
0
  }
1864
1865
0
  extra_ie = wpa_supplicant_extra_ies(wpa_s);
1866
0
  if (extra_ie) {
1867
0
    params.extra_ies = wpabuf_head(extra_ie);
1868
0
    params.extra_ies_len = wpabuf_len(extra_ie);
1869
0
  }
1870
1871
0
  if (wpa_s->conf->filter_rssi)
1872
0
    params.filter_rssi = wpa_s->conf->filter_rssi;
1873
1874
  /* See if user specified frequencies. If so, scan only those. */
1875
0
  if (wpa_s->conf->freq_list && !params.freqs) {
1876
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1877
0
      "Optimize scan based on conf->freq_list");
1878
0
    int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1879
0
  }
1880
1881
#ifdef CONFIG_MBO
1882
  if (wpa_s->enable_oce & OCE_STA)
1883
    params.oce_scan = 1;
1884
#endif /* CONFIG_MBO */
1885
1886
0
  scan_params = &params;
1887
1888
0
scan:
1889
0
  wpa_s->sched_scan_timed_out = 0;
1890
1891
  /*
1892
   * We cannot support multiple scan plans if the scan request includes
1893
   * too many SSID's, so in this case use only the last scan plan and make
1894
   * it run infinitely. It will be stopped by the timeout.
1895
   */
1896
0
  if (wpa_s->sched_scan_plans_num == 1 ||
1897
0
      (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1898
0
    params.sched_scan_plans = wpa_s->sched_scan_plans;
1899
0
    params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1900
0
  } else if (wpa_s->sched_scan_plans_num > 1) {
1901
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1902
0
      "Too many SSIDs. Default to using single scheduled_scan plan");
1903
0
    params.sched_scan_plans =
1904
0
      &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1905
0
             1];
1906
0
    params.sched_scan_plans_num = 1;
1907
0
  } else {
1908
0
    if (wpa_s->conf->sched_scan_interval)
1909
0
      scan_plan.interval = wpa_s->conf->sched_scan_interval;
1910
0
    else
1911
0
      scan_plan.interval = 10;
1912
1913
0
    if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1914
0
      wpa_printf(MSG_WARNING,
1915
0
           "Scan interval too long(%u), use the maximum allowed(%u)",
1916
0
           scan_plan.interval,
1917
0
           wpa_s->max_sched_scan_plan_interval);
1918
0
      scan_plan.interval =
1919
0
        wpa_s->max_sched_scan_plan_interval;
1920
0
    }
1921
1922
0
    scan_plan.iterations = 0;
1923
0
    params.sched_scan_plans = &scan_plan;
1924
0
    params.sched_scan_plans_num = 1;
1925
0
  }
1926
1927
0
  params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1928
1929
0
  if (ssid || !wpa_s->first_sched_scan) {
1930
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1931
0
      "Starting sched scan after %u seconds: interval %u timeout %d",
1932
0
      params.sched_scan_start_delay,
1933
0
      params.sched_scan_plans[0].interval,
1934
0
      wpa_s->sched_scan_timeout);
1935
0
  } else {
1936
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1937
0
      "Starting sched scan after %u seconds (no timeout)",
1938
0
      params.sched_scan_start_delay);
1939
0
  }
1940
1941
0
  wpa_setband_scan_freqs(wpa_s, scan_params);
1942
1943
0
  if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
1944
0
      wpa_s->wpa_state <= WPA_SCANNING)
1945
0
    wpa_setup_mac_addr_rand_params(&params,
1946
0
                 wpa_s->mac_addr_sched_scan);
1947
1948
0
  wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1949
1950
0
  ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
1951
0
  wpabuf_free(extra_ie);
1952
0
  os_free(params.filter_ssids);
1953
0
  os_free(params.mac_addr);
1954
0
  if (ret) {
1955
0
    wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1956
0
    if (prev_state != wpa_s->wpa_state)
1957
0
      wpa_supplicant_set_state(wpa_s, prev_state);
1958
0
    return ret;
1959
0
  }
1960
1961
  /* If we have more SSIDs to scan, add a timeout so we scan them too */
1962
0
  if (ssid || !wpa_s->first_sched_scan) {
1963
0
    wpa_s->sched_scan_timed_out = 0;
1964
0
    eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1965
0
               wpa_supplicant_sched_scan_timeout,
1966
0
               wpa_s, NULL);
1967
0
    wpa_s->first_sched_scan = 0;
1968
0
    wpa_s->sched_scan_timeout /= 2;
1969
0
    params.sched_scan_plans[0].interval *= 2;
1970
0
    if ((unsigned int) wpa_s->sched_scan_timeout <
1971
0
        params.sched_scan_plans[0].interval ||
1972
0
        params.sched_scan_plans[0].interval >
1973
0
        wpa_s->max_sched_scan_plan_interval) {
1974
0
      params.sched_scan_plans[0].interval = 10;
1975
0
      wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1976
0
    }
1977
0
  }
1978
1979
  /* If there is no more ssids, start next time from the beginning */
1980
0
  if (!ssid)
1981
0
    wpa_s->prev_sched_ssid = NULL;
1982
1983
0
  return 0;
1984
0
}
1985
1986
1987
/**
1988
 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1989
 * @wpa_s: Pointer to wpa_supplicant data
1990
 *
1991
 * This function is used to cancel a scan request scheduled with
1992
 * wpa_supplicant_req_scan().
1993
 */
1994
void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1995
0
{
1996
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1997
0
  eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1998
0
}
1999
2000
2001
/**
2002
 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
2003
 * @wpa_s: Pointer to wpa_supplicant data
2004
 *
2005
 * This function is used to stop a delayed scheduled scan.
2006
 */
2007
void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
2008
0
{
2009
0
  if (!wpa_s->sched_scan_supported)
2010
0
    return;
2011
2012
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
2013
0
  eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
2014
0
           wpa_s, NULL);
2015
0
}
2016
2017
2018
/**
2019
 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
2020
 * @wpa_s: Pointer to wpa_supplicant data
2021
 *
2022
 * This function is used to stop a periodic scheduled scan.
2023
 */
2024
void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
2025
0
{
2026
0
  if (!wpa_s->sched_scanning)
2027
0
    return;
2028
2029
0
  if (wpa_s->sched_scanning)
2030
0
    wpa_s->sched_scan_stop_req = 1;
2031
2032
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
2033
0
  eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
2034
0
  wpa_supplicant_stop_sched_scan(wpa_s);
2035
0
}
2036
2037
2038
/**
2039
 * wpa_supplicant_notify_scanning - Indicate possible scan state change
2040
 * @wpa_s: Pointer to wpa_supplicant data
2041
 * @scanning: Whether scanning is currently in progress
2042
 *
2043
 * This function is to generate scanning notifycations. It is called whenever
2044
 * there may have been a change in scanning (scan started, completed, stopped).
2045
 * wpas_notify_scanning() is called whenever the scanning state changed from the
2046
 * previously notified state.
2047
 */
2048
void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
2049
            int scanning)
2050
0
{
2051
0
  if (wpa_s->scanning != scanning) {
2052
0
    wpa_s->scanning = scanning;
2053
0
    wpas_notify_scanning(wpa_s);
2054
0
  }
2055
0
}
2056
2057
2058
static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
2059
0
{
2060
0
  int rate = 0;
2061
0
  const u8 *ie;
2062
0
  int i;
2063
2064
0
  ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
2065
0
  for (i = 0; ie && i < ie[1]; i++) {
2066
0
    if ((ie[i + 2] & 0x7f) > rate)
2067
0
      rate = ie[i + 2] & 0x7f;
2068
0
  }
2069
2070
0
  ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
2071
0
  for (i = 0; ie && i < ie[1]; i++) {
2072
0
    if ((ie[i + 2] & 0x7f) > rate)
2073
0
      rate = ie[i + 2] & 0x7f;
2074
0
  }
2075
2076
0
  return rate;
2077
0
}
2078
2079
2080
/**
2081
 * wpa_scan_get_ie - Fetch a specified information element from a scan result
2082
 * @res: Scan result entry
2083
 * @ie: Information element identitifier (WLAN_EID_*)
2084
 * Returns: Pointer to the information element (id field) or %NULL if not found
2085
 *
2086
 * This function returns the first matching information element in the scan
2087
 * result.
2088
 */
2089
const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
2090
0
{
2091
0
  size_t ie_len = res->ie_len;
2092
2093
  /* Use the Beacon frame IEs if res->ie_len is not available */
2094
0
  if (!ie_len)
2095
0
    ie_len = res->beacon_ie_len;
2096
2097
0
  return get_ie((const u8 *) (res + 1), ie_len, ie);
2098
0
}
2099
2100
2101
const u8 * wpa_scan_get_ml_ie(const struct wpa_scan_res *res, u8 type)
2102
0
{
2103
0
  size_t ie_len = res->ie_len;
2104
2105
  /* Use the Beacon frame IEs if res->ie_len is not available */
2106
0
  if (!ie_len)
2107
0
    ie_len = res->beacon_ie_len;
2108
2109
0
  return get_ml_ie((const u8 *) (res + 1), ie_len, type);
2110
0
}
2111
2112
2113
/**
2114
 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
2115
 * @res: Scan result entry
2116
 * @vendor_type: Vendor type (four octets starting the IE payload)
2117
 * Returns: Pointer to the information element (id field) or %NULL if not found
2118
 *
2119
 * This function returns the first matching information element in the scan
2120
 * result.
2121
 */
2122
const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
2123
          u32 vendor_type)
2124
0
{
2125
0
  const u8 *ies;
2126
0
  const struct element *elem;
2127
2128
0
  ies = (const u8 *) (res + 1);
2129
2130
0
  for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
2131
0
    if (elem->datalen >= 4 &&
2132
0
        vendor_type == WPA_GET_BE32(elem->data))
2133
0
      return &elem->id;
2134
0
  }
2135
2136
0
  return NULL;
2137
0
}
2138
2139
2140
/**
2141
 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
2142
 * @res: Scan result entry
2143
 * @vendor_type: Vendor type (four octets starting the IE payload)
2144
 * Returns: Pointer to the information element (id field) or %NULL if not found
2145
 *
2146
 * This function returns the first matching information element in the scan
2147
 * result.
2148
 *
2149
 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
2150
 * from Beacon frames instead of either Beacon or Probe Response frames.
2151
 */
2152
const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
2153
           u32 vendor_type)
2154
0
{
2155
0
  const u8 *ies;
2156
0
  const struct element *elem;
2157
2158
0
  if (res->beacon_ie_len == 0)
2159
0
    return NULL;
2160
2161
0
  ies = (const u8 *) (res + 1);
2162
0
  ies += res->ie_len;
2163
2164
0
  for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
2165
0
          res->beacon_ie_len) {
2166
0
    if (elem->datalen >= 4 &&
2167
0
        vendor_type == WPA_GET_BE32(elem->data))
2168
0
      return &elem->id;
2169
0
  }
2170
2171
0
  return NULL;
2172
0
}
2173
2174
2175
/**
2176
 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
2177
 * @res: Scan result entry
2178
 * @vendor_type: Vendor type (four octets starting the IE payload)
2179
 * Returns: Pointer to the information element payload or %NULL if not found
2180
 *
2181
 * This function returns concatenated payload of possibly fragmented vendor
2182
 * specific information elements in the scan result. The caller is responsible
2183
 * for freeing the returned buffer.
2184
 */
2185
struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2186
               u32 vendor_type)
2187
0
{
2188
0
  struct wpabuf *buf;
2189
0
  const u8 *end, *pos;
2190
2191
0
  buf = wpabuf_alloc(res->ie_len);
2192
0
  if (buf == NULL)
2193
0
    return NULL;
2194
2195
0
  pos = (const u8 *) (res + 1);
2196
0
  end = pos + res->ie_len;
2197
2198
0
  while (end - pos > 1) {
2199
0
    u8 ie, len;
2200
2201
0
    ie = pos[0];
2202
0
    len = pos[1];
2203
0
    if (len > end - pos - 2)
2204
0
      break;
2205
0
    pos += 2;
2206
0
    if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2207
0
        vendor_type == WPA_GET_BE32(pos))
2208
0
      wpabuf_put_data(buf, pos + 4, len - 4);
2209
0
    pos += len;
2210
0
  }
2211
2212
0
  if (wpabuf_len(buf) == 0) {
2213
0
    wpabuf_free(buf);
2214
0
    buf = NULL;
2215
0
  }
2216
2217
0
  return buf;
2218
0
}
2219
2220
2221
static int wpas_channel_width_offset(enum chan_width cw)
2222
0
{
2223
0
  switch (cw) {
2224
0
  case CHAN_WIDTH_40:
2225
0
    return 1;
2226
0
  case CHAN_WIDTH_80:
2227
0
    return 2;
2228
0
  case CHAN_WIDTH_80P80:
2229
0
  case CHAN_WIDTH_160:
2230
0
    return 3;
2231
0
  case CHAN_WIDTH_320:
2232
0
    return 4;
2233
0
  default:
2234
0
    return 0;
2235
0
  }
2236
0
}
2237
2238
2239
/**
2240
 * wpas_channel_width_tx_pwr - Calculate the max transmit power at the channel
2241
 * width
2242
 * @ies: Information elements
2243
 * @ies_len: Length of elements
2244
 * @cw: The channel width
2245
 * Returns: The max transmit power at the channel width, TX_POWER_NO_CONSTRAINT
2246
 * if it is not constrained.
2247
 *
2248
 * This function is only used to estimate the actual signal RSSI when associated
2249
 * based on the beacon RSSI at the STA. Beacon frames are transmitted on 20 MHz
2250
 * channels, while the Data frames usually use higher channel width. Therefore
2251
 * their RSSIs may be different. Assuming there is a fixed gap between the TX
2252
 * power limit of the STA defined by the Transmit Power Envelope element and the
2253
 * TX power of the AP, the difference in the TX power of X MHz and Y MHz at the
2254
 * STA equals to the difference at the AP, and the difference in the signal RSSI
2255
 * at the STA. tx_pwr is a floating point number in the standard, but the error
2256
 * of casting to int is trivial in comparing two BSSes.
2257
 */
2258
static int wpas_channel_width_tx_pwr(const u8 *ies, size_t ies_len,
2259
             enum chan_width cw)
2260
0
{
2261
0
  int offset = wpas_channel_width_offset(cw);
2262
0
  const struct element *elem;
2263
0
  int max_tx_power = TX_POWER_NO_CONSTRAINT, tx_pwr = 0;
2264
2265
0
  for_each_element_id(elem, WLAN_EID_TRANSMIT_POWER_ENVELOPE, ies,
2266
0
          ies_len) {
2267
0
    int max_tx_pwr_count;
2268
0
    enum max_tx_pwr_interpretation tx_pwr_intrpn;
2269
0
    enum reg_6g_client_type client_type;
2270
2271
0
    if (elem->datalen < 1)
2272
0
      continue;
2273
2274
    /*
2275
     * IEEE Std 802.11ax-2021, 9.4.2.161 (Transmit Power Envelope
2276
     * element) defines Maximum Transmit Power Count (B0-B2),
2277
     * Maximum Transmit Power Interpretation (B3-B5), and Maximum
2278
     * Transmit Power Category (B6-B7).
2279
     */
2280
0
    max_tx_pwr_count = elem->data[0] & 0x07;
2281
0
    tx_pwr_intrpn = (elem->data[0] >> 3) & 0x07;
2282
0
    client_type = (elem->data[0] >> 6) & 0x03;
2283
2284
0
    if (client_type != REG_DEFAULT_CLIENT)
2285
0
      continue;
2286
2287
0
    if (tx_pwr_intrpn == LOCAL_EIRP ||
2288
0
        tx_pwr_intrpn == REGULATORY_CLIENT_EIRP) {
2289
0
      int offs;
2290
2291
0
      max_tx_pwr_count = MIN(max_tx_pwr_count, 3);
2292
0
      offs = MIN(offset, max_tx_pwr_count) + 1;
2293
0
      if (elem->datalen <= offs)
2294
0
        continue;
2295
0
      tx_pwr = (signed char) elem->data[offs];
2296
      /*
2297
       * Maximum Transmit Power subfield is encoded as an
2298
       * 8-bit 2s complement signed integer in the range -64
2299
       * dBm to 63 dBm with a 0.5 dB step. 63.5 dBm means no
2300
       * local maximum transmit power constraint.
2301
       */
2302
0
      if (tx_pwr == 127)
2303
0
        continue;
2304
0
      tx_pwr /= 2;
2305
0
      max_tx_power = MIN(max_tx_power, tx_pwr);
2306
0
    } else if (tx_pwr_intrpn == LOCAL_EIRP_PSD ||
2307
0
         tx_pwr_intrpn == REGULATORY_CLIENT_EIRP_PSD) {
2308
0
      if (elem->datalen < 2)
2309
0
        continue;
2310
2311
0
      tx_pwr = (signed char) elem->data[1];
2312
      /*
2313
       * Maximum Transmit PSD subfield is encoded as an 8-bit
2314
       * 2s complement signed integer. -128 indicates that the
2315
       * corresponding 20 MHz channel cannot be used for
2316
       * transmission. +127 indicates that no maximum PSD
2317
       * limit is specified for the corresponding 20 MHz
2318
       * channel.
2319
       */
2320
0
      if (tx_pwr == 127 || tx_pwr == -128)
2321
0
        continue;
2322
2323
      /*
2324
       * The Maximum Transmit PSD subfield indicates the
2325
       * maximum transmit PSD for the 20 MHz channel. Suppose
2326
       * the PSD value is X dBm/MHz, the TX power of N MHz is
2327
       * X + 10*log10(N) = X + 10*log10(20) + 10*log10(N/20) =
2328
       * X + 13 + 3*log2(N/20)
2329
       */
2330
0
      tx_pwr = tx_pwr / 2 + 13 + offset * 3;
2331
0
      max_tx_power = MIN(max_tx_power, tx_pwr);
2332
0
    }
2333
0
  }
2334
2335
0
  return max_tx_power;
2336
0
}
2337
2338
2339
/**
2340
 * Estimate the RSSI bump of channel width |cw| with respect to 20 MHz channel.
2341
 * If the TX power has no constraint, it is unable to estimate the RSSI bump.
2342
 */
2343
int wpas_channel_width_rssi_bump(const u8 *ies, size_t ies_len,
2344
         enum chan_width cw)
2345
0
{
2346
0
  int max_20mhz_tx_pwr = wpas_channel_width_tx_pwr(ies, ies_len,
2347
0
               CHAN_WIDTH_20);
2348
0
  int max_cw_tx_pwr = wpas_channel_width_tx_pwr(ies, ies_len, cw);
2349
2350
0
  return (max_20mhz_tx_pwr == TX_POWER_NO_CONSTRAINT ||
2351
0
    max_cw_tx_pwr == TX_POWER_NO_CONSTRAINT) ?
2352
0
    0 : (max_cw_tx_pwr - max_20mhz_tx_pwr);
2353
0
}
2354
2355
2356
int wpas_adjust_snr_by_chanwidth(const u8 *ies, size_t ies_len,
2357
         enum chan_width max_cw, int snr)
2358
0
{
2359
0
  int rssi_bump = wpas_channel_width_rssi_bump(ies, ies_len, max_cw);
2360
  /*
2361
   * The noise has uniform power spectral density (PSD) across the
2362
   * frequency band, its power is proportional to the channel width.
2363
   * Suppose the PSD of noise is X dBm/MHz, the noise power of N MHz is
2364
   * X + 10*log10(N), and the noise power bump with respect to 20 MHz is
2365
   * 10*log10(N) - 10*log10(20) = 10*log10(N/20) = 3*log2(N/20)
2366
   */
2367
0
  int noise_bump = 3 * wpas_channel_width_offset(max_cw);
2368
2369
0
  return snr + rssi_bump - noise_bump;
2370
0
}
2371
2372
2373
/* Compare function for sorting scan results. Return >0 if @b is considered
2374
 * better. */
2375
static int wpa_scan_result_compar(const void *a, const void *b)
2376
0
{
2377
0
  struct wpa_scan_res **_wa = (void *) a;
2378
0
  struct wpa_scan_res **_wb = (void *) b;
2379
0
  struct wpa_scan_res *wa = *_wa;
2380
0
  struct wpa_scan_res *wb = *_wb;
2381
0
  int wpa_a, wpa_b;
2382
0
  int snr_a, snr_b, snr_a_full, snr_b_full;
2383
0
  size_t ies_len;
2384
0
  const u8 *rsne_a, *rsne_b;
2385
2386
  /* WPA/WPA2 support preferred */
2387
0
  wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2388
0
    wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2389
0
  wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2390
0
    wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2391
2392
0
  if (wpa_b && !wpa_a)
2393
0
    return 1;
2394
0
  if (!wpa_b && wpa_a)
2395
0
    return -1;
2396
2397
  /* privacy support preferred */
2398
0
  if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2399
0
      (wb->caps & IEEE80211_CAP_PRIVACY))
2400
0
    return 1;
2401
0
  if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2402
0
      (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2403
0
    return -1;
2404
2405
0
  if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
2406
    /*
2407
     * The scan result estimates SNR over 20 MHz, while Data frames
2408
     * usually use wider channel width. The TX power and noise power
2409
     * are both affected by the channel width.
2410
     */
2411
0
    ies_len = wa->ie_len ? wa->ie_len : wa->beacon_ie_len;
2412
0
    snr_a_full = wpas_adjust_snr_by_chanwidth((const u8 *) (wa + 1),
2413
0
                ies_len, wa->max_cw,
2414
0
                wa->snr);
2415
0
    snr_a = MIN(snr_a_full, GREAT_SNR);
2416
0
    ies_len = wb->ie_len ? wb->ie_len : wb->beacon_ie_len;
2417
0
    snr_b_full = wpas_adjust_snr_by_chanwidth((const u8 *) (wb + 1),
2418
0
                ies_len, wb->max_cw,
2419
0
                wb->snr);
2420
0
    snr_b = MIN(snr_b_full, GREAT_SNR);
2421
0
  } else {
2422
    /* Level is not in dBm, so we can't calculate
2423
     * SNR. Just use raw level (units unknown). */
2424
0
    snr_a = snr_a_full = wa->level;
2425
0
    snr_b = snr_b_full = wb->level;
2426
0
  }
2427
2428
  /* If SNR of a SAE BSS is good or at least as high as the PSK BSS,
2429
   * prefer SAE over PSK for mixed WPA3-Personal transition mode and
2430
   * WPA2-Personal deployments */
2431
0
  rsne_a = wpa_scan_get_ie(wa, WLAN_EID_RSN);
2432
0
  rsne_b = wpa_scan_get_ie(wb, WLAN_EID_RSN);
2433
0
  if (rsne_a && rsne_b) {
2434
0
    struct wpa_ie_data data;
2435
0
    bool psk_a = false, psk_b = false, sae_a = false, sae_b = false;
2436
2437
0
    if (wpa_parse_wpa_ie_rsn(rsne_a, 2 + rsne_a[1], &data) == 0) {
2438
0
      psk_a = wpa_key_mgmt_wpa_psk_no_sae(data.key_mgmt);
2439
0
      sae_a = wpa_key_mgmt_sae(data.key_mgmt);
2440
0
    }
2441
0
    if (wpa_parse_wpa_ie_rsn(rsne_b, 2 + rsne_b[1], &data) == 0) {
2442
0
      psk_b = wpa_key_mgmt_wpa_psk_no_sae(data.key_mgmt);
2443
0
      sae_b = wpa_key_mgmt_sae(data.key_mgmt);
2444
0
    }
2445
2446
0
    if (sae_a && !sae_b && psk_b &&
2447
0
        (snr_a >= GREAT_SNR || snr_a >= snr_b))
2448
0
      return -1;
2449
0
    if (sae_b && !sae_a && psk_a &&
2450
0
        (snr_b >= GREAT_SNR || snr_b >= snr_a))
2451
0
      return 1;
2452
0
  }
2453
2454
  /* If SNR is close, decide by max rate or frequency band. For cases
2455
   * involving the 6 GHz band, use the throughput estimate irrespective
2456
   * of the SNR difference since the LPI/VLP rules may result in
2457
   * significant differences in SNR for cases where the estimated
2458
   * throughput can be considerably higher with the lower SNR. */
2459
0
  if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2460
0
             is_6ghz_freq(wa->freq) ||
2461
0
             is_6ghz_freq(wb->freq))) {
2462
0
    if (wa->est_throughput != wb->est_throughput)
2463
0
      return (int) wb->est_throughput -
2464
0
        (int) wa->est_throughput;
2465
0
  }
2466
0
  if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2467
0
      (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
2468
0
    if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2469
0
      return is_6ghz_freq(wa->freq) ? -1 : 1;
2470
0
    if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2471
0
      return IS_5GHZ(wa->freq) ? -1 : 1;
2472
0
  }
2473
2474
  /* all things being equal, use SNR; if SNRs are
2475
   * identical, use quality values since some drivers may only report
2476
   * that value and leave the signal level zero */
2477
0
  if (snr_b_full == snr_a_full)
2478
0
    return wb->qual - wa->qual;
2479
0
  return snr_b_full - snr_a_full;
2480
0
}
2481
2482
2483
#ifdef CONFIG_WPS
2484
/* Compare function for sorting scan results when searching a WPS AP for
2485
 * provisioning. Return >0 if @b is considered better. */
2486
static int wpa_scan_result_wps_compar(const void *a, const void *b)
2487
{
2488
  struct wpa_scan_res **_wa = (void *) a;
2489
  struct wpa_scan_res **_wb = (void *) b;
2490
  struct wpa_scan_res *wa = *_wa;
2491
  struct wpa_scan_res *wb = *_wb;
2492
  int uses_wps_a, uses_wps_b;
2493
  struct wpabuf *wps_a, *wps_b;
2494
  int res;
2495
2496
  /* Optimization - check WPS IE existence before allocated memory and
2497
   * doing full reassembly. */
2498
  uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2499
  uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2500
  if (uses_wps_a && !uses_wps_b)
2501
    return -1;
2502
  if (!uses_wps_a && uses_wps_b)
2503
    return 1;
2504
2505
  if (uses_wps_a && uses_wps_b) {
2506
    wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2507
    wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2508
    res = wps_ap_priority_compar(wps_a, wps_b);
2509
    wpabuf_free(wps_a);
2510
    wpabuf_free(wps_b);
2511
    if (res)
2512
      return res;
2513
  }
2514
2515
  /*
2516
   * Do not use current AP security policy as a sorting criteria during
2517
   * WPS provisioning step since the AP may get reconfigured at the
2518
   * completion of provisioning.
2519
   */
2520
2521
  /* all things being equal, use signal level; if signal levels are
2522
   * identical, use quality values since some drivers may only report
2523
   * that value and leave the signal level zero */
2524
  if (wb->level == wa->level)
2525
    return wb->qual - wa->qual;
2526
  return wb->level - wa->level;
2527
}
2528
#endif /* CONFIG_WPS */
2529
2530
2531
static void dump_scan_res(struct wpa_scan_results *scan_res)
2532
0
{
2533
0
#ifndef CONFIG_NO_STDOUT_DEBUG
2534
0
  size_t i;
2535
2536
0
  if (scan_res->res == NULL || scan_res->num == 0)
2537
0
    return;
2538
2539
0
  wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2540
2541
0
  for (i = 0; i < scan_res->num; i++) {
2542
0
    struct wpa_scan_res *r = scan_res->res[i];
2543
0
    u8 *pos;
2544
0
    const u8 *ssid_ie, *ssid = NULL;
2545
0
    size_t ssid_len = 0;
2546
2547
0
    ssid_ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
2548
0
    if (ssid_ie) {
2549
0
      ssid = ssid_ie + 2;
2550
0
      ssid_len = ssid_ie[1];
2551
0
    }
2552
2553
0
    if (r->flags & WPA_SCAN_LEVEL_DBM) {
2554
0
      int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2555
2556
0
      wpa_printf(MSG_EXCESSIVE, MACSTR
2557
0
           " ssid=%s freq=%d qual=%d noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2558
0
           MAC2STR(r->bssid),
2559
0
           wpa_ssid_txt(ssid, ssid_len),
2560
0
           r->freq, r->qual,
2561
0
           r->noise, noise_valid ? "" : "~", r->level,
2562
0
           r->snr, r->snr >= GREAT_SNR ? "*" : "",
2563
0
           r->flags,
2564
0
           r->age, r->est_throughput);
2565
0
    } else {
2566
0
      wpa_printf(MSG_EXCESSIVE, MACSTR
2567
0
           " ssid=%s freq=%d qual=%d noise=%d level=%d flags=0x%x age=%u est=%u",
2568
0
           MAC2STR(r->bssid),
2569
0
           wpa_ssid_txt(ssid, ssid_len),
2570
0
           r->freq, r->qual,
2571
0
           r->noise, r->level, r->flags, r->age,
2572
0
           r->est_throughput);
2573
0
    }
2574
0
    pos = (u8 *) (r + 1);
2575
0
    if (r->ie_len)
2576
0
      wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2577
0
    pos += r->ie_len;
2578
0
    if (r->beacon_ie_len)
2579
0
      wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2580
0
            pos, r->beacon_ie_len);
2581
0
  }
2582
0
#endif /* CONFIG_NO_STDOUT_DEBUG */
2583
0
}
2584
2585
2586
/**
2587
 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2588
 * @wpa_s: Pointer to wpa_supplicant data
2589
 * @bssid: BSSID to check
2590
 * Returns: 0 if the BSSID is filtered or 1 if not
2591
 *
2592
 * This function is used to filter out specific BSSIDs from scan reslts mainly
2593
 * for testing purposes (SET bssid_filter ctrl_iface command).
2594
 */
2595
int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2596
              const u8 *bssid)
2597
2.41k
{
2598
2.41k
  size_t i;
2599
2600
2.41k
  if (wpa_s->bssid_filter == NULL)
2601
2.41k
    return 1;
2602
2603
0
  for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2604
0
    if (ether_addr_equal(wpa_s->bssid_filter + i * ETH_ALEN, bssid))
2605
0
      return 1;
2606
0
  }
2607
2608
0
  return 0;
2609
0
}
2610
2611
2612
static void filter_scan_res(struct wpa_supplicant *wpa_s,
2613
          struct wpa_scan_results *res)
2614
0
{
2615
0
  size_t i, j;
2616
2617
0
  if (wpa_s->bssid_filter == NULL)
2618
0
    return;
2619
2620
0
  for (i = 0, j = 0; i < res->num; i++) {
2621
0
    if (wpa_supplicant_filter_bssid_match(wpa_s,
2622
0
                  res->res[i]->bssid)) {
2623
0
      res->res[j++] = res->res[i];
2624
0
    } else {
2625
0
      os_free(res->res[i]);
2626
0
      res->res[i] = NULL;
2627
0
    }
2628
0
  }
2629
2630
0
  if (res->num != j) {
2631
0
    wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2632
0
         (int) (res->num - j));
2633
0
    res->num = j;
2634
0
  }
2635
0
}
2636
2637
2638
void scan_snr(struct wpa_scan_res *res)
2639
0
{
2640
0
  if (res->flags & WPA_SCAN_NOISE_INVALID) {
2641
0
    res->noise = is_6ghz_freq(res->freq) ?
2642
0
      DEFAULT_NOISE_FLOOR_6GHZ :
2643
0
      (IS_5GHZ(res->freq) ?
2644
0
       DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
2645
0
  }
2646
2647
0
  if (res->flags & WPA_SCAN_LEVEL_DBM) {
2648
0
    res->snr = res->level - res->noise;
2649
0
  } else {
2650
    /* Level is not in dBm, so we can't calculate
2651
     * SNR. Just use raw level (units unknown). */
2652
0
    res->snr = res->level;
2653
0
  }
2654
0
}
2655
2656
2657
/* Minimum SNR required to achieve a certain bitrate. */
2658
struct minsnr_bitrate_entry {
2659
  int minsnr;
2660
  unsigned int bitrate; /* in Mbps */
2661
};
2662
2663
/* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2664
static const int vht_mcs = 8;
2665
2666
static const struct minsnr_bitrate_entry vht20_table[] = {
2667
  { 0, 0 },
2668
  { 2, 6500 },   /* HT20 MCS0 */
2669
  { 5, 13000 },  /* HT20 MCS1 */
2670
  { 9, 19500 },  /* HT20 MCS2 */
2671
  { 11, 26000 }, /* HT20 MCS3 */
2672
  { 15, 39000 }, /* HT20 MCS4 */
2673
  { 18, 52000 }, /* HT20 MCS5 */
2674
  { 20, 58500 }, /* HT20 MCS6 */
2675
  { 25, 65000 }, /* HT20 MCS7 */
2676
  { 29, 78000 }, /* VHT20 MCS8 */
2677
  { -1, 78000 }  /* SNR > 29 */
2678
};
2679
2680
static const struct minsnr_bitrate_entry vht40_table[] = {
2681
  { 0, 0 },
2682
  { 5, 13500 },   /* HT40 MCS0 */
2683
  { 8, 27000 },   /* HT40 MCS1 */
2684
  { 12, 40500 },  /* HT40 MCS2 */
2685
  { 14, 54000 },  /* HT40 MCS3 */
2686
  { 18, 81000 },  /* HT40 MCS4 */
2687
  { 21, 108000 }, /* HT40 MCS5 */
2688
  { 23, 121500 }, /* HT40 MCS6 */
2689
  { 28, 135000 }, /* HT40 MCS7 */
2690
  { 32, 162000 }, /* VHT40 MCS8 */
2691
  { 34, 180000 }, /* VHT40 MCS9 */
2692
  { -1, 180000 }  /* SNR > 34 */
2693
};
2694
2695
static const struct minsnr_bitrate_entry vht80_table[] = {
2696
  { 0, 0 },
2697
  { 8, 29300 },   /* VHT80 MCS0 */
2698
  { 11, 58500 },  /* VHT80 MCS1 */
2699
  { 15, 87800 },  /* VHT80 MCS2 */
2700
  { 17, 117000 }, /* VHT80 MCS3 */
2701
  { 21, 175500 }, /* VHT80 MCS4 */
2702
  { 24, 234000 }, /* VHT80 MCS5 */
2703
  { 26, 263300 }, /* VHT80 MCS6 */
2704
  { 31, 292500 }, /* VHT80 MCS7 */
2705
  { 35, 351000 }, /* VHT80 MCS8 */
2706
  { 37, 390000 }, /* VHT80 MCS9 */
2707
  { -1, 390000 }  /* SNR > 37 */
2708
};
2709
2710
2711
static const struct minsnr_bitrate_entry vht160_table[] = {
2712
  { 0, 0 },
2713
  { 11, 58500 },  /* VHT160 MCS0 */
2714
  { 14, 117000 }, /* VHT160 MCS1 */
2715
  { 18, 175500 }, /* VHT160 MCS2 */
2716
  { 20, 234000 }, /* VHT160 MCS3 */
2717
  { 24, 351000 }, /* VHT160 MCS4 */
2718
  { 27, 468000 }, /* VHT160 MCS5 */
2719
  { 29, 526500 }, /* VHT160 MCS6 */
2720
  { 34, 585000 }, /* VHT160 MCS7 */
2721
  { 38, 702000 }, /* VHT160 MCS8 */
2722
  { 40, 780000 }, /* VHT160 MCS9 */
2723
  { -1, 780000 }  /* SNR > 37 */
2724
};
2725
2726
/* EHT needs to be enabled in order to achieve MCS12 and MCS13 rates. */
2727
0
#define EHT_MCS 12
2728
2729
static const struct minsnr_bitrate_entry he20_table[] = {
2730
  { 0, 0 },
2731
  { 2, 8600 },    /* HE20 MCS0 */
2732
  { 5, 17200 },   /* HE20 MCS1 */
2733
  { 9, 25800 },   /* HE20 MCS2 */
2734
  { 11, 34400 },  /* HE20 MCS3 */
2735
  { 15, 51600 },  /* HE20 MCS4 */
2736
  { 18, 68800 },  /* HE20 MCS5 */
2737
  { 20, 77400 },  /* HE20 MCS6 */
2738
  { 25, 86000 },  /* HE20 MCS7 */
2739
  { 29, 103200 }, /* HE20 MCS8 */
2740
  { 31, 114700 }, /* HE20 MCS9 */
2741
  { 34, 129000 }, /* HE20 MCS10 */
2742
  { 36, 143400 }, /* HE20 MCS11 */
2743
  { 39, 154900 }, /* EHT20 MCS12 */
2744
  { 42, 172100 }, /* EHT20 MCS13 */
2745
  { -1, 172100 }  /* SNR > 42 */
2746
};
2747
2748
static const struct minsnr_bitrate_entry he40_table[] = {
2749
  { 0, 0 },
2750
  { 5, 17200 },   /* HE40 MCS0 */
2751
  { 8, 34400 },   /* HE40 MCS1 */
2752
  { 12, 51600 },  /* HE40 MCS2 */
2753
  { 14, 68800 },  /* HE40 MCS3 */
2754
  { 18, 103200 }, /* HE40 MCS4 */
2755
  { 21, 137600 }, /* HE40 MCS5 */
2756
  { 23, 154900 }, /* HE40 MCS6 */
2757
  { 28, 172100 }, /* HE40 MCS7 */
2758
  { 32, 206500 }, /* HE40 MCS8 */
2759
  { 34, 229400 }, /* HE40 MCS9 */
2760
  { 37, 258100 }, /* HE40 MCS10 */
2761
  { 39, 286800 }, /* HE40 MCS11 */
2762
  { 42, 309500 }, /* EHT40 MCS12 */
2763
  { 45, 344100 }, /* EHT40 MCS13 */
2764
  { -1, 344100 }  /* SNR > 45 */
2765
};
2766
2767
static const struct minsnr_bitrate_entry he80_table[] = {
2768
  { 0, 0 },
2769
  { 8, 36000 },   /* HE80 MCS0 */
2770
  { 11, 72100 },  /* HE80 MCS1 */
2771
  { 15, 108100 }, /* HE80 MCS2 */
2772
  { 17, 144100 }, /* HE80 MCS3 */
2773
  { 21, 216200 }, /* HE80 MCS4 */
2774
  { 24, 288200 }, /* HE80 MCS5 */
2775
  { 26, 324300 }, /* HE80 MCS6 */
2776
  { 31, 360300 }, /* HE80 MCS7 */
2777
  { 35, 432400 }, /* HE80 MCS8 */
2778
  { 37, 480400 }, /* HE80 MCS9 */
2779
  { 40, 540400 }, /* HE80 MCS10 */
2780
  { 42, 600500 }, /* HE80 MCS11 */
2781
  { 45, 648500 }, /* EHT80 MCS12 */
2782
  { 48, 720600 }, /* EHT80 MCS13 */
2783
  { -1, 720600 }  /* SNR > 48 */
2784
};
2785
2786
2787
static const struct minsnr_bitrate_entry he160_table[] = {
2788
  { 0, 0 },
2789
  { 11, 72100 },   /* HE160 MCS0 */
2790
  { 14, 144100 },  /* HE160 MCS1 */
2791
  { 18, 216200 },  /* HE160 MCS2 */
2792
  { 20, 288200 },  /* HE160 MCS3 */
2793
  { 24, 432400 },  /* HE160 MCS4 */
2794
  { 27, 576500 },  /* HE160 MCS5 */
2795
  { 29, 648500 },  /* HE160 MCS6 */
2796
  { 34, 720600 },  /* HE160 MCS7 */
2797
  { 38, 864700 },  /* HE160 MCS8 */
2798
  { 40, 960800 },  /* HE160 MCS9 */
2799
  { 43, 1080900 }, /* HE160 MCS10 */
2800
  { 45, 1201000 }, /* HE160 MCS11 */
2801
  { 48, 1297100 }, /* EHT160 MCS12 */
2802
  { 51, 1441200 }, /* EHT160 MCS13 */
2803
  { -1, 1441200 }  /* SNR > 51 */
2804
};
2805
2806
/* See IEEE P802.11be/D2.0, Table 36-86: EHT-MCSs for 4x996-tone RU, NSS,u = 1
2807
 */
2808
static const struct minsnr_bitrate_entry eht320_table[] = {
2809
  { 0, 0 },
2810
  { 14, 144100 },   /* EHT320 MCS0 */
2811
  { 17, 288200 },   /* EHT320 MCS1 */
2812
  { 21, 432400 },   /* EHT320 MCS2 */
2813
  { 23, 576500 },   /* EHT320 MCS3 */
2814
  { 27, 864700 },   /* EHT320 MCS4 */
2815
  { 30, 1152900 },  /* EHT320 MCS5 */
2816
  { 32, 1297100 },  /* EHT320 MCS6 */
2817
  { 37, 1441200 },  /* EHT320 MCS7 */
2818
  { 41, 1729400 },  /* EHT320 MCS8 */
2819
  { 43, 1921500 },  /* EHT320 MCS9 */
2820
  { 46, 2161800 },  /* EHT320 MCS10 */
2821
  { 48, 2401900 },  /* EHT320 MCS11 */
2822
  { 51, 2594100 },  /* EHT320 MCS12 */
2823
  { 54, 2882400 },  /* EHT320 MCS13 */
2824
  { -1, 2882400 }   /* SNR > 54 */
2825
};
2826
2827
static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2828
             int rate0, int rate1)
2829
0
{
2830
0
  return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
2831
0
}
2832
2833
2834
static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2835
           int snr, bool vht)
2836
0
{
2837
0
  const struct minsnr_bitrate_entry *prev, *entry = table;
2838
2839
0
  while ((entry->minsnr != -1) &&
2840
0
         (snr >= entry->minsnr) &&
2841
0
         (vht || entry - table <= vht_mcs))
2842
0
    entry++;
2843
0
  if (entry == table)
2844
0
    return entry->bitrate;
2845
0
  prev = entry - 1;
2846
0
  if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2847
0
    return prev->bitrate;
2848
0
  return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2849
0
        entry->bitrate);
2850
0
}
2851
2852
2853
static unsigned int max_ht20_rate(int snr, bool vht)
2854
0
{
2855
0
  return max_rate(vht20_table, snr, vht);
2856
0
}
2857
2858
2859
static unsigned int max_ht40_rate(int snr, bool vht)
2860
0
{
2861
0
  return max_rate(vht40_table, snr, vht);
2862
0
}
2863
2864
2865
static unsigned int max_vht80_rate(int snr)
2866
0
{
2867
0
  return max_rate(vht80_table, snr, 1);
2868
0
}
2869
2870
2871
static unsigned int max_vht160_rate(int snr)
2872
0
{
2873
0
  return max_rate(vht160_table, snr, 1);
2874
0
}
2875
2876
2877
static unsigned int max_he_eht_rate(const struct minsnr_bitrate_entry table[],
2878
            int snr, bool eht)
2879
0
{
2880
0
  const struct minsnr_bitrate_entry *prev, *entry = table;
2881
2882
0
  while (entry->minsnr != -1 && snr >= entry->minsnr &&
2883
0
         (eht || entry - table <= EHT_MCS))
2884
0
    entry++;
2885
0
  if (entry == table)
2886
0
    return 0;
2887
0
  prev = entry - 1;
2888
0
  if (entry->minsnr == -1 || (!eht && entry - table > EHT_MCS))
2889
0
    return prev->bitrate;
2890
0
  return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2891
0
        prev->bitrate, entry->bitrate);
2892
0
}
2893
2894
2895
unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2896
            const u8 *ies, size_t ies_len, int rate,
2897
            int snr, int freq, enum chan_width *max_cw)
2898
0
{
2899
0
  struct hostapd_hw_modes *hw_mode;
2900
0
  unsigned int est, tmp;
2901
0
  const u8 *ie;
2902
  /*
2903
   * No need to apply a bump to the noise here because the
2904
   * minsnr_bitrate_entry tables are based on MCS tables where this has
2905
   * been taken into account.
2906
   */
2907
0
  int adjusted_snr;
2908
0
  bool ht40 = false, vht80 = false, vht160 = false;
2909
2910
  /* Limit based on estimated SNR */
2911
0
  if (rate > 1 * 2 && snr < 1)
2912
0
    rate = 1 * 2;
2913
0
  else if (rate > 2 * 2 && snr < 4)
2914
0
    rate = 2 * 2;
2915
0
  else if (rate > 6 * 2 && snr < 5)
2916
0
    rate = 6 * 2;
2917
0
  else if (rate > 9 * 2 && snr < 6)
2918
0
    rate = 9 * 2;
2919
0
  else if (rate > 12 * 2 && snr < 7)
2920
0
    rate = 12 * 2;
2921
0
  else if (rate > 12 * 2 && snr < 8)
2922
0
    rate = 14 * 2;
2923
0
  else if (rate > 12 * 2 && snr < 9)
2924
0
    rate = 16 * 2;
2925
0
  else if (rate > 18 * 2 && snr < 10)
2926
0
    rate = 18 * 2;
2927
0
  else if (rate > 24 * 2 && snr < 11)
2928
0
    rate = 24 * 2;
2929
0
  else if (rate > 24 * 2 && snr < 12)
2930
0
    rate = 27 * 2;
2931
0
  else if (rate > 24 * 2 && snr < 13)
2932
0
    rate = 30 * 2;
2933
0
  else if (rate > 24 * 2 && snr < 14)
2934
0
    rate = 33 * 2;
2935
0
  else if (rate > 36 * 2 && snr < 15)
2936
0
    rate = 36 * 2;
2937
0
  else if (rate > 36 * 2 && snr < 16)
2938
0
    rate = 39 * 2;
2939
0
  else if (rate > 36 * 2 && snr < 17)
2940
0
    rate = 42 * 2;
2941
0
  else if (rate > 36 * 2 && snr < 18)
2942
0
    rate = 45 * 2;
2943
0
  else if (rate > 48 * 2 && snr < 19)
2944
0
    rate = 48 * 2;
2945
0
  else if (rate > 48 * 2 && snr < 20)
2946
0
    rate = 51 * 2;
2947
0
  else if (rate > 54 * 2 && snr < 21)
2948
0
    rate = 54 * 2;
2949
0
  est = rate * 500;
2950
2951
0
  hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2952
0
             freq);
2953
2954
0
  if (hw_mode && hw_mode->ht_capab) {
2955
0
    ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
2956
0
    if (ie) {
2957
0
      *max_cw = CHAN_WIDTH_20;
2958
0
      tmp = max_ht20_rate(snr, false);
2959
0
      if (tmp > est)
2960
0
        est = tmp;
2961
0
    }
2962
0
  }
2963
2964
0
  ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
2965
0
  if (ie && ie[1] >= 2 &&
2966
0
      (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK))
2967
0
    ht40 = true;
2968
2969
0
  if (hw_mode &&
2970
0
      (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
2971
0
    if (ht40) {
2972
0
      *max_cw = CHAN_WIDTH_40;
2973
0
      adjusted_snr = snr +
2974
0
        wpas_channel_width_rssi_bump(ies, ies_len,
2975
0
                   CHAN_WIDTH_40);
2976
0
      tmp = max_ht40_rate(adjusted_snr, false);
2977
0
      if (tmp > est)
2978
0
        est = tmp;
2979
0
    }
2980
0
  }
2981
2982
  /* Determine VHT BSS bandwidth based on IEEE Std 802.11-2020,
2983
   * Table 11-23 (VHT BSS bandwidth) */
2984
0
  ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
2985
0
  if (ie && ie[1] >= 3) {
2986
0
    u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2987
0
    u8 seg0 = ie[3];
2988
0
    u8 seg1 = ie[4];
2989
2990
0
    if (cw)
2991
0
      vht80 = true;
2992
0
    if (cw == 2 ||
2993
0
        (cw == 3 && (seg1 > 0 && abs(seg1 - seg0) == 16)))
2994
0
      vht160 = true;
2995
0
    if (cw == 1 &&
2996
0
        ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2997
0
         (seg1 > 0 && abs(seg1 - seg0) == 16)))
2998
0
      vht160 = true;
2999
0
  }
3000
3001
0
  if (hw_mode && hw_mode->vht_capab) {
3002
    /* Use +1 to assume VHT is always faster than HT */
3003
0
    ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
3004
0
    if (ie) {
3005
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN)
3006
0
        *max_cw = CHAN_WIDTH_20;
3007
0
      tmp = max_ht20_rate(snr, true) + 1;
3008
0
      if (tmp > est)
3009
0
        est = tmp;
3010
3011
0
      if (ht40) {
3012
0
        *max_cw = CHAN_WIDTH_40;
3013
0
        adjusted_snr = snr +
3014
0
          wpas_channel_width_rssi_bump(
3015
0
            ies, ies_len, CHAN_WIDTH_40);
3016
0
        tmp = max_ht40_rate(adjusted_snr, true) + 1;
3017
0
        if (tmp > est)
3018
0
          est = tmp;
3019
0
      }
3020
3021
0
      if (vht80) {
3022
0
        *max_cw = CHAN_WIDTH_80;
3023
0
        adjusted_snr = snr +
3024
0
          wpas_channel_width_rssi_bump(
3025
0
            ies, ies_len, CHAN_WIDTH_80);
3026
0
        tmp = max_vht80_rate(adjusted_snr) + 1;
3027
0
        if (tmp > est)
3028
0
          est = tmp;
3029
0
      }
3030
3031
0
      if (vht160 &&
3032
0
          (hw_mode->vht_capab &
3033
0
           (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3034
0
            VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
3035
0
        *max_cw = CHAN_WIDTH_160;
3036
0
        adjusted_snr = snr +
3037
0
          wpas_channel_width_rssi_bump(
3038
0
            ies, ies_len, CHAN_WIDTH_160);
3039
0
        tmp = max_vht160_rate(adjusted_snr) + 1;
3040
0
        if (tmp > est)
3041
0
          est = tmp;
3042
0
      }
3043
0
    }
3044
0
  }
3045
3046
0
  if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
3047
    /* Use +2 to assume HE is always faster than HT/VHT */
3048
0
    struct ieee80211_he_capabilities *he;
3049
0
    struct ieee80211_eht_capabilities *eht;
3050
0
    struct he_capabilities *own_he;
3051
0
    u8 cw, boost = 2;
3052
0
    const u8 *eht_ie;
3053
0
    bool is_eht = false;
3054
3055
0
    ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
3056
0
    if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
3057
0
      return est;
3058
0
    he = (struct ieee80211_he_capabilities *) &ie[3];
3059
0
    own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
3060
3061
    /* Use +3 to assume EHT is always faster than HE */
3062
0
    if (hw_mode->eht_capab[IEEE80211_MODE_INFRA].eht_supported) {
3063
0
      eht_ie = get_ie_ext(ies, ies_len,
3064
0
              WLAN_EID_EXT_EHT_CAPABILITIES);
3065
0
      if (eht_ie &&
3066
0
          (eht_ie[1] >= 1 + IEEE80211_EHT_CAPAB_MIN_LEN)) {
3067
0
        is_eht = true;
3068
0
        boost = 3;
3069
0
      }
3070
0
    }
3071
3072
0
    if (*max_cw == CHAN_WIDTH_UNKNOWN)
3073
0
      *max_cw = CHAN_WIDTH_20;
3074
0
    tmp = max_he_eht_rate(he20_table, snr, is_eht) + boost;
3075
0
    if (tmp > est)
3076
0
      est = tmp;
3077
3078
0
    cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
3079
0
      own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
3080
0
    if ((cw &
3081
0
         (IS_2P4GHZ(freq) ?
3082
0
          HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
3083
0
          HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) && ht40) {
3084
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3085
0
          *max_cw < CHAN_WIDTH_40)
3086
0
        *max_cw = CHAN_WIDTH_40;
3087
0
      adjusted_snr = snr + wpas_channel_width_rssi_bump(
3088
0
        ies, ies_len, CHAN_WIDTH_40);
3089
0
      tmp = max_he_eht_rate(he40_table, adjusted_snr,
3090
0
                is_eht) + boost;
3091
0
      if (tmp > est)
3092
0
        est = tmp;
3093
0
    }
3094
3095
0
    if (!IS_2P4GHZ(freq) &&
3096
0
        (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G) &&
3097
0
        (!IS_5GHZ(freq) || vht80)) {
3098
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3099
0
          *max_cw < CHAN_WIDTH_80)
3100
0
        *max_cw = CHAN_WIDTH_80;
3101
0
      adjusted_snr = snr + wpas_channel_width_rssi_bump(
3102
0
        ies, ies_len, CHAN_WIDTH_80);
3103
0
      tmp = max_he_eht_rate(he80_table, adjusted_snr,
3104
0
                is_eht) + boost;
3105
0
      if (tmp > est)
3106
0
        est = tmp;
3107
0
    }
3108
3109
0
    if (!IS_2P4GHZ(freq) &&
3110
0
        (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3111
0
         HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) &&
3112
0
        (!IS_5GHZ(freq) || vht160)) {
3113
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3114
0
          *max_cw < CHAN_WIDTH_160)
3115
0
        *max_cw = CHAN_WIDTH_160;
3116
0
      adjusted_snr = snr + wpas_channel_width_rssi_bump(
3117
0
        ies, ies_len, CHAN_WIDTH_160);
3118
0
      tmp = max_he_eht_rate(he160_table, adjusted_snr,
3119
0
                is_eht) + boost;
3120
0
      if (tmp > est)
3121
0
        est = tmp;
3122
0
    }
3123
3124
0
    if (!is_eht)
3125
0
      return est;
3126
3127
0
    eht = (struct ieee80211_eht_capabilities *) &eht_ie[3];
3128
3129
0
    if (is_6ghz_freq(freq) &&
3130
0
        (eht->phy_cap[EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
3131
0
         EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK)) {
3132
0
      if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3133
0
          *max_cw < CHAN_WIDTH_320)
3134
0
        *max_cw = CHAN_WIDTH_320;
3135
0
      adjusted_snr = snr + wpas_channel_width_rssi_bump(
3136
0
        ies, ies_len, CHAN_WIDTH_320);
3137
0
      tmp = max_he_eht_rate(eht320_table, adjusted_snr, true);
3138
0
      if (tmp > est)
3139
0
        est = tmp;
3140
0
    }
3141
0
  }
3142
3143
0
  return est;
3144
0
}
3145
3146
3147
void scan_est_throughput(struct wpa_supplicant *wpa_s,
3148
       struct wpa_scan_res *res)
3149
0
{
3150
0
  int rate; /* max legacy rate in 500 kb/s units */
3151
0
  int snr = res->snr;
3152
0
  const u8 *ies = (const void *) (res + 1);
3153
0
  size_t ie_len = res->ie_len;
3154
3155
0
  if (res->est_throughput)
3156
0
    return;
3157
3158
  /* Get maximum legacy rate */
3159
0
  rate = wpa_scan_get_max_rate(res);
3160
3161
0
  if (!ie_len)
3162
0
    ie_len = res->beacon_ie_len;
3163
0
  res->est_throughput = wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr,
3164
0
                 res->freq, &res->max_cw);
3165
3166
  /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
3167
0
}
3168
3169
3170
/**
3171
 * wpa_supplicant_get_scan_results - Get scan results
3172
 * @wpa_s: Pointer to wpa_supplicant data
3173
 * @info: Information about what was scanned or %NULL if not available
3174
 * @new_scan: Whether a new scan was performed
3175
 * @bssid: Return BSS entries only for a single BSSID, %NULL for all
3176
 * Returns: Scan results, %NULL on failure
3177
 *
3178
 * This function request the current scan results from the driver and updates
3179
 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
3180
 * results with wpa_scan_results_free().
3181
 */
3182
struct wpa_scan_results *
3183
wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
3184
        struct scan_info *info, int new_scan,
3185
        const u8 *bssid)
3186
948
{
3187
948
  struct wpa_scan_results *scan_res;
3188
948
  size_t i;
3189
948
  int (*compar)(const void *, const void *) = wpa_scan_result_compar;
3190
3191
948
  scan_res = wpa_drv_get_scan_results(wpa_s, bssid);
3192
948
  if (scan_res == NULL) {
3193
948
    wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
3194
948
    return NULL;
3195
948
  }
3196
0
  if (scan_res->fetch_time.sec == 0) {
3197
    /*
3198
     * Make sure we have a valid timestamp if the driver wrapper
3199
     * does not set this.
3200
     */
3201
0
    os_get_reltime(&scan_res->fetch_time);
3202
0
  }
3203
0
  filter_scan_res(wpa_s, scan_res);
3204
3205
0
  for (i = 0; i < scan_res->num; i++) {
3206
0
    struct wpa_scan_res *scan_res_item = scan_res->res[i];
3207
3208
0
    scan_snr(scan_res_item);
3209
0
    scan_est_throughput(wpa_s, scan_res_item);
3210
0
  }
3211
3212
#ifdef CONFIG_WPS
3213
  if (wpas_wps_searching(wpa_s)) {
3214
    wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
3215
      "provisioning rules");
3216
    compar = wpa_scan_result_wps_compar;
3217
  }
3218
#endif /* CONFIG_WPS */
3219
3220
0
  if (scan_res->res) {
3221
0
    qsort(scan_res->res, scan_res->num,
3222
0
          sizeof(struct wpa_scan_res *), compar);
3223
0
  }
3224
0
  dump_scan_res(scan_res);
3225
3226
0
  if (wpa_s->ignore_post_flush_scan_res) {
3227
    /* FLUSH command aborted an ongoing scan and these are the
3228
     * results from the aborted scan. Do not process the results to
3229
     * maintain flushed state. */
3230
0
    wpa_dbg(wpa_s, MSG_DEBUG,
3231
0
      "Do not update BSS table based on pending post-FLUSH scan results");
3232
0
    wpa_s->ignore_post_flush_scan_res = 0;
3233
0
    return scan_res;
3234
0
  }
3235
3236
0
  wpa_bss_update_start(wpa_s);
3237
0
  for (i = 0; i < scan_res->num; i++)
3238
0
    wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
3239
0
          &scan_res->fetch_time);
3240
0
  wpa_bss_update_end(wpa_s, info, new_scan);
3241
3242
0
  return scan_res;
3243
0
}
3244
3245
3246
/**
3247
 * wpa_supplicant_update_scan_results - Update scan results from the driver
3248
 * @wpa_s: Pointer to wpa_supplicant data
3249
 * @bssid: Update BSS entries only for a single BSSID, %NULL for all
3250
 * Returns: 0 on success, -1 on failure
3251
 *
3252
 * This function updates the BSS table within wpa_supplicant based on the
3253
 * currently available scan results from the driver without requesting a new
3254
 * scan. This is used in cases where the driver indicates an association
3255
 * (including roaming within ESS) and wpa_supplicant does not yet have the
3256
 * needed information to complete the connection (e.g., to perform validation
3257
 * steps in 4-way handshake).
3258
 */
3259
int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s,
3260
               const u8 *bssid)
3261
948
{
3262
948
  struct wpa_scan_results *scan_res;
3263
948
  scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0, bssid);
3264
948
  if (scan_res == NULL)
3265
948
    return -1;
3266
0
  wpa_scan_results_free(scan_res);
3267
3268
0
  return 0;
3269
948
}
3270
3271
3272
/**
3273
 * scan_only_handler - Reports scan results
3274
 */
3275
void scan_only_handler(struct wpa_supplicant *wpa_s,
3276
           struct wpa_scan_results *scan_res)
3277
0
{
3278
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
3279
0
  if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3280
0
      wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
3281
0
    wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
3282
0
           wpa_s->manual_scan_id);
3283
0
    wpa_s->manual_scan_use_id = 0;
3284
0
  } else {
3285
0
    wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
3286
0
  }
3287
0
  wpas_notify_scan_results(wpa_s);
3288
0
  wpas_notify_scan_done(wpa_s, 1);
3289
0
  if (wpa_s->scan_work) {
3290
0
    struct wpa_radio_work *work = wpa_s->scan_work;
3291
0
    wpa_s->scan_work = NULL;
3292
0
    radio_work_done(work);
3293
0
  }
3294
3295
0
  if (wpa_s->wpa_state == WPA_SCANNING)
3296
0
    wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
3297
0
}
3298
3299
3300
int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
3301
0
{
3302
0
  return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
3303
0
}
3304
3305
3306
struct wpa_driver_scan_params *
3307
wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
3308
0
{
3309
0
  struct wpa_driver_scan_params *params;
3310
0
  size_t i;
3311
0
  u8 *n;
3312
3313
0
  params = os_zalloc(sizeof(*params));
3314
0
  if (params == NULL)
3315
0
    return NULL;
3316
3317
0
  for (i = 0; i < src->num_ssids; i++) {
3318
0
    if (src->ssids[i].ssid) {
3319
0
      n = os_memdup(src->ssids[i].ssid,
3320
0
              src->ssids[i].ssid_len);
3321
0
      if (n == NULL)
3322
0
        goto failed;
3323
0
      params->ssids[i].ssid = n;
3324
0
      params->ssids[i].ssid_len = src->ssids[i].ssid_len;
3325
0
    }
3326
0
  }
3327
0
  params->num_ssids = src->num_ssids;
3328
3329
0
  if (src->extra_ies) {
3330
0
    n = os_memdup(src->extra_ies, src->extra_ies_len);
3331
0
    if (n == NULL)
3332
0
      goto failed;
3333
0
    params->extra_ies = n;
3334
0
    params->extra_ies_len = src->extra_ies_len;
3335
0
  }
3336
3337
0
  if (src->freqs) {
3338
0
    int len = int_array_len(src->freqs);
3339
0
    params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
3340
0
    if (params->freqs == NULL)
3341
0
      goto failed;
3342
0
  }
3343
3344
0
  if (src->filter_ssids) {
3345
0
    params->filter_ssids = os_memdup(src->filter_ssids,
3346
0
             sizeof(*params->filter_ssids) *
3347
0
             src->num_filter_ssids);
3348
0
    if (params->filter_ssids == NULL)
3349
0
      goto failed;
3350
0
    params->num_filter_ssids = src->num_filter_ssids;
3351
0
  }
3352
3353
0
  params->filter_rssi = src->filter_rssi;
3354
0
  params->p2p_probe = src->p2p_probe;
3355
0
  params->only_new_results = src->only_new_results;
3356
0
  params->low_priority = src->low_priority;
3357
0
  params->duration = src->duration;
3358
0
  params->duration_mandatory = src->duration_mandatory;
3359
0
  params->oce_scan = src->oce_scan;
3360
0
  params->link_id = src->link_id;
3361
3362
0
  if (src->sched_scan_plans_num > 0) {
3363
0
    params->sched_scan_plans =
3364
0
      os_memdup(src->sched_scan_plans,
3365
0
          sizeof(*src->sched_scan_plans) *
3366
0
          src->sched_scan_plans_num);
3367
0
    if (!params->sched_scan_plans)
3368
0
      goto failed;
3369
3370
0
    params->sched_scan_plans_num = src->sched_scan_plans_num;
3371
0
  }
3372
3373
0
  if (src->mac_addr_rand &&
3374
0
      wpa_setup_mac_addr_rand_params(params, src->mac_addr))
3375
0
    goto failed;
3376
3377
0
  if (src->bssid) {
3378
0
    u8 *bssid;
3379
3380
0
    bssid = os_memdup(src->bssid, ETH_ALEN);
3381
0
    if (!bssid)
3382
0
      goto failed;
3383
0
    params->bssid = bssid;
3384
0
  }
3385
3386
0
  params->relative_rssi_set = src->relative_rssi_set;
3387
0
  params->relative_rssi = src->relative_rssi;
3388
0
  params->relative_adjust_band = src->relative_adjust_band;
3389
0
  params->relative_adjust_rssi = src->relative_adjust_rssi;
3390
0
  params->p2p_include_6ghz = src->p2p_include_6ghz;
3391
0
  params->non_coloc_6ghz = src->non_coloc_6ghz;
3392
0
  params->min_probe_req_content = src->min_probe_req_content;
3393
0
  return params;
3394
3395
0
failed:
3396
0
  wpa_scan_free_params(params);
3397
0
  return NULL;
3398
0
}
3399
3400
3401
void wpa_scan_free_params(struct wpa_driver_scan_params *params)
3402
0
{
3403
0
  size_t i;
3404
3405
0
  if (params == NULL)
3406
0
    return;
3407
3408
0
  for (i = 0; i < params->num_ssids; i++)
3409
0
    os_free((u8 *) params->ssids[i].ssid);
3410
0
  os_free((u8 *) params->extra_ies);
3411
0
  os_free(params->freqs);
3412
0
  os_free(params->filter_ssids);
3413
0
  os_free(params->sched_scan_plans);
3414
3415
  /*
3416
   * Note: params->mac_addr_mask points to same memory allocation and
3417
   * must not be freed separately.
3418
   */
3419
0
  os_free((u8 *) params->mac_addr);
3420
3421
0
  os_free((u8 *) params->bssid);
3422
3423
0
  os_free(params);
3424
0
}
3425
3426
3427
int wpas_start_pno(struct wpa_supplicant *wpa_s)
3428
0
{
3429
0
  int ret;
3430
0
  size_t prio, i, num_ssid, num_match_ssid;
3431
0
  struct wpa_ssid *ssid;
3432
0
  struct wpa_driver_scan_params params;
3433
0
  struct sched_scan_plan scan_plan;
3434
0
  unsigned int max_sched_scan_ssids;
3435
3436
0
  if (!wpa_s->sched_scan_supported)
3437
0
    return -1;
3438
3439
0
  if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
3440
0
    max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
3441
0
  else
3442
0
    max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
3443
0
  if (max_sched_scan_ssids < 1)
3444
0
    return -1;
3445
3446
0
  if (wpa_s->pno || wpa_s->pno_sched_pending)
3447
0
    return 0;
3448
3449
0
  if ((wpa_s->wpa_state > WPA_SCANNING) &&
3450
0
      (wpa_s->wpa_state < WPA_COMPLETED)) {
3451
0
    wpa_printf(MSG_ERROR, "PNO: In assoc process");
3452
0
    return -EAGAIN;
3453
0
  }
3454
3455
0
  if (wpa_s->wpa_state == WPA_SCANNING) {
3456
0
    wpa_supplicant_cancel_scan(wpa_s);
3457
0
    if (wpa_s->sched_scanning) {
3458
0
      wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
3459
0
           "ongoing sched scan");
3460
0
      wpa_supplicant_cancel_sched_scan(wpa_s);
3461
0
      wpa_s->pno_sched_pending = 1;
3462
0
      return 0;
3463
0
    }
3464
0
  }
3465
3466
0
  if (wpa_s->sched_scan_stop_req) {
3467
0
    wpa_printf(MSG_DEBUG,
3468
0
         "Schedule PNO after previous sched scan has stopped");
3469
0
    wpa_s->pno_sched_pending = 1;
3470
0
    return 0;
3471
0
  }
3472
3473
0
  os_memset(&params, 0, sizeof(params));
3474
3475
0
  num_ssid = num_match_ssid = 0;
3476
0
  ssid = wpa_s->conf->ssid;
3477
0
  while (ssid) {
3478
0
    if (!wpas_network_disabled(wpa_s, ssid)) {
3479
0
      num_match_ssid++;
3480
0
      if (ssid->scan_ssid)
3481
0
        num_ssid++;
3482
0
    }
3483
0
    ssid = ssid->next;
3484
0
  }
3485
3486
0
  if (num_match_ssid == 0) {
3487
0
    wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
3488
0
    return -1;
3489
0
  }
3490
3491
0
  if (num_match_ssid > num_ssid) {
3492
0
    params.num_ssids++; /* wildcard */
3493
0
    num_ssid++;
3494
0
  }
3495
3496
0
  if (num_ssid > max_sched_scan_ssids) {
3497
0
    wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
3498
0
         "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3499
0
    num_ssid = max_sched_scan_ssids;
3500
0
  }
3501
3502
0
  if (num_match_ssid > wpa_s->max_match_sets) {
3503
0
    num_match_ssid = wpa_s->max_match_sets;
3504
0
    wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
3505
0
  }
3506
0
  params.filter_ssids = os_calloc(num_match_ssid,
3507
0
          sizeof(struct wpa_driver_scan_filter));
3508
0
  if (params.filter_ssids == NULL)
3509
0
    return -1;
3510
3511
0
  i = 0;
3512
0
  prio = 0;
3513
0
  ssid = wpa_s->conf->pssid[prio];
3514
0
  while (ssid) {
3515
0
    if (!wpas_network_disabled(wpa_s, ssid)) {
3516
0
      if (ssid->scan_ssid && params.num_ssids < num_ssid) {
3517
0
        params.ssids[params.num_ssids].ssid =
3518
0
          ssid->ssid;
3519
0
        params.ssids[params.num_ssids].ssid_len =
3520
0
           ssid->ssid_len;
3521
0
        params.num_ssids++;
3522
0
      }
3523
0
      os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
3524
0
          ssid->ssid_len);
3525
0
      params.filter_ssids[i].ssid_len = ssid->ssid_len;
3526
0
      params.num_filter_ssids++;
3527
0
      i++;
3528
0
      if (i == num_match_ssid)
3529
0
        break;
3530
0
    }
3531
0
    if (ssid->pnext)
3532
0
      ssid = ssid->pnext;
3533
0
    else if (prio + 1 == wpa_s->conf->num_prio)
3534
0
      break;
3535
0
    else
3536
0
      ssid = wpa_s->conf->pssid[++prio];
3537
0
  }
3538
3539
0
  if (wpa_s->conf->filter_rssi)
3540
0
    params.filter_rssi = wpa_s->conf->filter_rssi;
3541
3542
0
  if (wpa_s->sched_scan_plans_num) {
3543
0
    params.sched_scan_plans = wpa_s->sched_scan_plans;
3544
0
    params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3545
0
  } else {
3546
    /* Set one scan plan that will run infinitely */
3547
0
    if (wpa_s->conf->sched_scan_interval)
3548
0
      scan_plan.interval = wpa_s->conf->sched_scan_interval;
3549
0
    else
3550
0
      scan_plan.interval = 10;
3551
3552
0
    scan_plan.iterations = 0;
3553
0
    params.sched_scan_plans = &scan_plan;
3554
0
    params.sched_scan_plans_num = 1;
3555
0
  }
3556
3557
0
  params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3558
3559
0
  if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3560
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3561
0
    params.freqs = wpa_s->manual_sched_scan_freqs;
3562
0
  }
3563
3564
0
  if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
3565
0
      wpa_s->wpa_state <= WPA_SCANNING)
3566
0
    wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
3567
3568
0
  wpa_scan_set_relative_rssi_params(wpa_s, &params);
3569
3570
0
  ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
3571
0
  os_free(params.filter_ssids);
3572
0
  os_free(params.mac_addr);
3573
0
  if (ret == 0)
3574
0
    wpa_s->pno = 1;
3575
0
  else
3576
0
    wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3577
0
  return ret;
3578
0
}
3579
3580
3581
int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3582
0
{
3583
0
  int ret = 0;
3584
3585
0
  if (!wpa_s->pno)
3586
0
    return 0;
3587
3588
0
  ret = wpa_supplicant_stop_sched_scan(wpa_s);
3589
0
  wpa_s->sched_scan_stop_req = 1;
3590
3591
0
  wpa_s->pno = 0;
3592
0
  wpa_s->pno_sched_pending = 0;
3593
3594
0
  if (wpa_s->wpa_state == WPA_SCANNING)
3595
0
    wpa_supplicant_req_scan(wpa_s, 0, 0);
3596
3597
0
  return ret;
3598
0
}
3599
3600
3601
void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3602
            unsigned int type)
3603
0
{
3604
0
  type &= MAC_ADDR_RAND_ALL;
3605
0
  wpa_s->mac_addr_rand_enable &= ~type;
3606
3607
0
  if (type & MAC_ADDR_RAND_SCAN) {
3608
0
    os_free(wpa_s->mac_addr_scan);
3609
0
    wpa_s->mac_addr_scan = NULL;
3610
0
  }
3611
3612
0
  if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3613
0
    os_free(wpa_s->mac_addr_sched_scan);
3614
0
    wpa_s->mac_addr_sched_scan = NULL;
3615
0
  }
3616
3617
0
  if (type & MAC_ADDR_RAND_PNO) {
3618
0
    os_free(wpa_s->mac_addr_pno);
3619
0
    wpa_s->mac_addr_pno = NULL;
3620
0
  }
3621
0
}
3622
3623
3624
int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3625
        unsigned int type, const u8 *addr,
3626
        const u8 *mask)
3627
0
{
3628
0
  u8 *tmp = NULL;
3629
3630
0
  if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3631
0
    wpa_printf(MSG_INFO,
3632
0
         "scan: MAC randomization type %u != supported=%u",
3633
0
         type, wpa_s->mac_addr_rand_supported);
3634
0
    return -1;
3635
0
  }
3636
3637
0
  wpas_mac_addr_rand_scan_clear(wpa_s, type);
3638
3639
0
  if (addr) {
3640
0
    tmp = os_malloc(2 * ETH_ALEN);
3641
0
    if (!tmp)
3642
0
      return -1;
3643
0
    os_memcpy(tmp, addr, ETH_ALEN);
3644
0
    os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3645
0
  }
3646
3647
0
  if (type == MAC_ADDR_RAND_SCAN) {
3648
0
    wpa_s->mac_addr_scan = tmp;
3649
0
  } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3650
0
    wpa_s->mac_addr_sched_scan = tmp;
3651
0
  } else if (type == MAC_ADDR_RAND_PNO) {
3652
0
    wpa_s->mac_addr_pno = tmp;
3653
0
  } else {
3654
0
    wpa_printf(MSG_INFO,
3655
0
         "scan: Invalid MAC randomization type=0x%x",
3656
0
         type);
3657
0
    os_free(tmp);
3658
0
    return -1;
3659
0
  }
3660
3661
0
  wpa_s->mac_addr_rand_enable |= type;
3662
0
  return 0;
3663
0
}
3664
3665
3666
int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3667
             unsigned int type, u8 *mask)
3668
0
{
3669
0
  const u8 *to_copy;
3670
3671
0
  if ((wpa_s->mac_addr_rand_enable & type) != type)
3672
0
    return -1;
3673
3674
0
  if (type == MAC_ADDR_RAND_SCAN) {
3675
0
    to_copy = wpa_s->mac_addr_scan;
3676
0
  } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3677
0
    to_copy = wpa_s->mac_addr_sched_scan;
3678
0
  } else if (type == MAC_ADDR_RAND_PNO) {
3679
0
    to_copy = wpa_s->mac_addr_pno;
3680
0
  } else {
3681
0
    wpa_printf(MSG_DEBUG,
3682
0
         "scan: Invalid MAC randomization type=0x%x",
3683
0
         type);
3684
0
    return -1;
3685
0
  }
3686
3687
0
  os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3688
0
  return 0;
3689
0
}
3690
3691
3692
int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3693
0
{
3694
0
  struct wpa_radio_work *work;
3695
0
  struct wpa_radio *radio = wpa_s->radio;
3696
3697
0
  dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3698
0
    if (work->wpa_s != wpa_s || !work->started ||
3699
0
        (os_strcmp(work->type, "scan") != 0 &&
3700
0
         os_strcmp(work->type, "p2p-scan") != 0))
3701
0
      continue;
3702
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
3703
0
    return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
3704
0
  }
3705
3706
0
  wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3707
0
  return -1;
3708
0
}
3709
3710
3711
int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3712
0
{
3713
0
  struct sched_scan_plan *scan_plans = NULL;
3714
0
  const char *token, *context = NULL;
3715
0
  unsigned int num = 0;
3716
3717
0
  if (!cmd)
3718
0
    return -1;
3719
3720
0
  if (!cmd[0]) {
3721
0
    wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3722
0
    os_free(wpa_s->sched_scan_plans);
3723
0
    wpa_s->sched_scan_plans = NULL;
3724
0
    wpa_s->sched_scan_plans_num = 0;
3725
0
    return 0;
3726
0
  }
3727
3728
0
  while ((token = cstr_token(cmd, " ", &context))) {
3729
0
    int ret;
3730
0
    struct sched_scan_plan *scan_plan, *n;
3731
3732
0
    n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3733
0
    if (!n)
3734
0
      goto fail;
3735
3736
0
    scan_plans = n;
3737
0
    scan_plan = &scan_plans[num];
3738
0
    num++;
3739
3740
0
    ret = sscanf(token, "%u:%u", &scan_plan->interval,
3741
0
           &scan_plan->iterations);
3742
0
    if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3743
0
      wpa_printf(MSG_ERROR,
3744
0
           "Invalid sched scan plan input: %s", token);
3745
0
      goto fail;
3746
0
    }
3747
3748
0
    if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3749
0
      wpa_printf(MSG_WARNING,
3750
0
           "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3751
0
           num, scan_plan->interval,
3752
0
           wpa_s->max_sched_scan_plan_interval);
3753
0
      scan_plan->interval =
3754
0
        wpa_s->max_sched_scan_plan_interval;
3755
0
    }
3756
3757
0
    if (ret == 1) {
3758
0
      scan_plan->iterations = 0;
3759
0
      break;
3760
0
    }
3761
3762
0
    if (!scan_plan->iterations) {
3763
0
      wpa_printf(MSG_ERROR,
3764
0
           "scan plan %u: Number of iterations cannot be zero",
3765
0
           num);
3766
0
      goto fail;
3767
0
    }
3768
3769
0
    if (scan_plan->iterations >
3770
0
        wpa_s->max_sched_scan_plan_iterations) {
3771
0
      wpa_printf(MSG_WARNING,
3772
0
           "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3773
0
           num, scan_plan->iterations,
3774
0
           wpa_s->max_sched_scan_plan_iterations);
3775
0
      scan_plan->iterations =
3776
0
        wpa_s->max_sched_scan_plan_iterations;
3777
0
    }
3778
3779
0
    wpa_printf(MSG_DEBUG,
3780
0
         "scan plan %u: interval=%u iterations=%u",
3781
0
         num, scan_plan->interval, scan_plan->iterations);
3782
0
  }
3783
3784
0
  if (!scan_plans) {
3785
0
    wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3786
0
    goto fail;
3787
0
  }
3788
3789
0
  if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3790
0
    wpa_printf(MSG_ERROR,
3791
0
         "All scan plans but the last must specify a number of iterations");
3792
0
    goto fail;
3793
0
  }
3794
3795
0
  wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3796
0
       num, scan_plans[num - 1].interval);
3797
3798
0
  if (num > wpa_s->max_sched_scan_plans) {
3799
0
    wpa_printf(MSG_WARNING,
3800
0
         "Too many scheduled scan plans (only %u supported)",
3801
0
         wpa_s->max_sched_scan_plans);
3802
0
    wpa_printf(MSG_WARNING,
3803
0
         "Use only the first %u scan plans, and the last one (in infinite loop)",
3804
0
         wpa_s->max_sched_scan_plans - 1);
3805
0
    os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3806
0
        &scan_plans[num - 1], sizeof(*scan_plans));
3807
0
    num = wpa_s->max_sched_scan_plans;
3808
0
  }
3809
3810
0
  os_free(wpa_s->sched_scan_plans);
3811
0
  wpa_s->sched_scan_plans = scan_plans;
3812
0
  wpa_s->sched_scan_plans_num = num;
3813
3814
0
  return 0;
3815
3816
0
fail:
3817
0
  os_free(scan_plans);
3818
0
  wpa_printf(MSG_ERROR, "invalid scan plans list");
3819
0
  return -1;
3820
0
}
3821
3822
3823
/**
3824
 * wpas_scan_reset_sched_scan - Reset sched_scan state
3825
 * @wpa_s: Pointer to wpa_supplicant data
3826
 *
3827
 * This function is used to cancel a running scheduled scan and to reset an
3828
 * internal scan state to continue with a regular scan on the following
3829
 * wpa_supplicant_req_scan() calls.
3830
 */
3831
void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3832
0
{
3833
0
  wpa_s->normal_scans = 0;
3834
0
  if (wpa_s->sched_scanning) {
3835
0
    wpa_s->sched_scan_timed_out = 0;
3836
0
    wpa_s->prev_sched_ssid = NULL;
3837
0
    wpa_supplicant_cancel_sched_scan(wpa_s);
3838
0
  }
3839
0
}
3840
3841
3842
void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3843
0
{
3844
  /* simulate timeout to restart the sched scan */
3845
0
  wpa_s->sched_scan_timed_out = 1;
3846
0
  wpa_s->prev_sched_ssid = NULL;
3847
0
  wpa_supplicant_cancel_sched_scan(wpa_s);
3848
0
}