Coverage Report

Created: 2026-09-14 06:11

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