Coverage Report

Created: 2023-03-26 06:22

/src/hostap/wpa_supplicant/wpa_supplicant.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * WPA Supplicant
3
 * Copyright (c) 2003-2022, 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
 * This file implements functions for registering and unregistering
9
 * %wpa_supplicant interfaces. In addition, this file contains number of
10
 * functions for managing network connections.
11
 */
12
13
#include "includes.h"
14
#ifdef CONFIG_MATCH_IFACE
15
#include <net/if.h>
16
#include <fnmatch.h>
17
#endif /* CONFIG_MATCH_IFACE */
18
19
#include "common.h"
20
#include "crypto/crypto.h"
21
#include "crypto/random.h"
22
#include "crypto/sha1.h"
23
#include "eapol_supp/eapol_supp_sm.h"
24
#include "eap_peer/eap.h"
25
#include "eap_peer/eap_proxy.h"
26
#include "eap_server/eap_methods.h"
27
#include "rsn_supp/wpa.h"
28
#include "eloop.h"
29
#include "config.h"
30
#include "utils/ext_password.h"
31
#include "l2_packet/l2_packet.h"
32
#include "wpa_supplicant_i.h"
33
#include "driver_i.h"
34
#include "ctrl_iface.h"
35
#include "pcsc_funcs.h"
36
#include "common/version.h"
37
#include "rsn_supp/preauth.h"
38
#include "rsn_supp/pmksa_cache.h"
39
#include "common/wpa_ctrl.h"
40
#include "common/ieee802_11_common.h"
41
#include "common/ieee802_11_defs.h"
42
#include "common/hw_features_common.h"
43
#include "common/gas_server.h"
44
#include "common/dpp.h"
45
#include "common/ptksa_cache.h"
46
#include "p2p/p2p.h"
47
#include "fst/fst.h"
48
#include "bssid_ignore.h"
49
#include "wpas_glue.h"
50
#include "wps_supplicant.h"
51
#include "ibss_rsn.h"
52
#include "sme.h"
53
#include "gas_query.h"
54
#include "ap.h"
55
#include "p2p_supplicant.h"
56
#include "wifi_display.h"
57
#include "notify.h"
58
#include "bgscan.h"
59
#include "autoscan.h"
60
#include "bss.h"
61
#include "scan.h"
62
#include "offchannel.h"
63
#include "hs20_supplicant.h"
64
#include "wnm_sta.h"
65
#include "wpas_kay.h"
66
#include "mesh.h"
67
#include "dpp_supplicant.h"
68
#ifdef CONFIG_MESH
69
#include "ap/ap_config.h"
70
#include "ap/hostapd.h"
71
#endif /* CONFIG_MESH */
72
73
const char *const wpa_supplicant_version =
74
"wpa_supplicant v" VERSION_STR "\n"
75
"Copyright (c) 2003-2022, Jouni Malinen <j@w1.fi> and contributors";
76
77
const char *const wpa_supplicant_license =
78
"This software may be distributed under the terms of the BSD license.\n"
79
"See README for more details.\n"
80
#ifdef EAP_TLS_OPENSSL
81
"\nThis product includes software developed by the OpenSSL Project\n"
82
"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
83
#endif /* EAP_TLS_OPENSSL */
84
;
85
86
#ifndef CONFIG_NO_STDOUT_DEBUG
87
/* Long text divided into parts in order to fit in C89 strings size limits. */
88
const char *const wpa_supplicant_full_license1 =
89
"";
90
const char *const wpa_supplicant_full_license2 =
91
"This software may be distributed under the terms of the BSD license.\n"
92
"\n"
93
"Redistribution and use in source and binary forms, with or without\n"
94
"modification, are permitted provided that the following conditions are\n"
95
"met:\n"
96
"\n";
97
const char *const wpa_supplicant_full_license3 =
98
"1. Redistributions of source code must retain the above copyright\n"
99
"   notice, this list of conditions and the following disclaimer.\n"
100
"\n"
101
"2. Redistributions in binary form must reproduce the above copyright\n"
102
"   notice, this list of conditions and the following disclaimer in the\n"
103
"   documentation and/or other materials provided with the distribution.\n"
104
"\n";
105
const char *const wpa_supplicant_full_license4 =
106
"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
107
"   names of its contributors may be used to endorse or promote products\n"
108
"   derived from this software without specific prior written permission.\n"
109
"\n"
110
"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
111
"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
112
"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
113
"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
114
const char *const wpa_supplicant_full_license5 =
115
"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
116
"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
117
"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
118
"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
119
"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
120
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
121
"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
122
"\n";
123
#endif /* CONFIG_NO_STDOUT_DEBUG */
124
125
126
static void wpa_bss_tmp_disallow_timeout(void *eloop_ctx, void *timeout_ctx);
127
#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
128
static void wpas_update_fils_connect_params(struct wpa_supplicant *wpa_s);
129
#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
130
#ifdef CONFIG_OWE
131
static void wpas_update_owe_connect_params(struct wpa_supplicant *wpa_s);
132
#endif /* CONFIG_OWE */
133
134
135
#ifdef CONFIG_WEP
136
/* Configure default/group WEP keys for static WEP */
137
int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
138
{
139
  int i, set = 0;
140
141
  for (i = 0; i < NUM_WEP_KEYS; i++) {
142
    if (ssid->wep_key_len[i] == 0)
143
      continue;
144
145
    set = 1;
146
    wpa_drv_set_key(wpa_s, -1, WPA_ALG_WEP, NULL,
147
        i, i == ssid->wep_tx_keyidx, NULL, 0,
148
        ssid->wep_key[i], ssid->wep_key_len[i],
149
        i == ssid->wep_tx_keyidx ?
150
        KEY_FLAG_GROUP_RX_TX_DEFAULT :
151
        KEY_FLAG_GROUP_RX_TX);
152
  }
153
154
  return set;
155
}
156
#endif /* CONFIG_WEP */
157
158
159
int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
160
            struct wpa_ssid *ssid)
161
0
{
162
0
  u8 key[32];
163
0
  size_t keylen;
164
0
  enum wpa_alg alg;
165
0
  u8 seq[6] = { 0 };
166
0
  int ret;
167
168
  /* IBSS/WPA-None uses only one key (Group) for both receiving and
169
   * sending unicast and multicast packets. */
170
171
0
  if (ssid->mode != WPAS_MODE_IBSS) {
172
0
    wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
173
0
      "IBSS/ad-hoc) for WPA-None", ssid->mode);
174
0
    return -1;
175
0
  }
176
177
0
  if (!ssid->psk_set) {
178
0
    wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
179
0
      "WPA-None");
180
0
    return -1;
181
0
  }
182
183
0
  switch (wpa_s->group_cipher) {
184
0
  case WPA_CIPHER_CCMP:
185
0
    os_memcpy(key, ssid->psk, 16);
186
0
    keylen = 16;
187
0
    alg = WPA_ALG_CCMP;
188
0
    break;
189
0
  case WPA_CIPHER_GCMP:
190
0
    os_memcpy(key, ssid->psk, 16);
191
0
    keylen = 16;
192
0
    alg = WPA_ALG_GCMP;
193
0
    break;
194
0
  case WPA_CIPHER_TKIP:
195
    /* WPA-None uses the same Michael MIC key for both TX and RX */
196
0
    os_memcpy(key, ssid->psk, 16 + 8);
197
0
    os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
198
0
    keylen = 32;
199
0
    alg = WPA_ALG_TKIP;
200
0
    break;
201
0
  default:
202
0
    wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
203
0
      "WPA-None", wpa_s->group_cipher);
204
0
    return -1;
205
0
  }
206
207
  /* TODO: should actually remember the previously used seq#, both for TX
208
   * and RX from each STA.. */
209
210
0
  ret = wpa_drv_set_key(wpa_s, -1, alg, NULL, 0, 1, seq, 6, key, keylen,
211
0
            KEY_FLAG_GROUP_RX_TX_DEFAULT);
212
0
  os_memset(key, 0, sizeof(key));
213
0
  return ret;
214
0
}
215
216
217
static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
218
0
{
219
0
  struct wpa_supplicant *wpa_s = eloop_ctx;
220
0
  const u8 *bssid = wpa_s->bssid;
221
0
  if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
222
0
      (wpa_s->wpa_state == WPA_AUTHENTICATING ||
223
0
       wpa_s->wpa_state == WPA_ASSOCIATING))
224
0
    bssid = wpa_s->pending_bssid;
225
0
  wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
226
0
    MAC2STR(bssid));
227
0
  wpa_bssid_ignore_add(wpa_s, bssid);
228
0
  wpa_sm_notify_disassoc(wpa_s->wpa);
229
0
  wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
230
0
  wpa_s->reassociate = 1;
231
232
  /*
233
   * If we timed out, the AP or the local radio may be busy.
234
   * So, wait a second until scanning again.
235
   */
236
0
  wpa_supplicant_req_scan(wpa_s, 1, 0);
237
0
}
238
239
240
/**
241
 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
242
 * @wpa_s: Pointer to wpa_supplicant data
243
 * @sec: Number of seconds after which to time out authentication
244
 * @usec: Number of microseconds after which to time out authentication
245
 *
246
 * This function is used to schedule a timeout for the current authentication
247
 * attempt.
248
 */
249
void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
250
             int sec, int usec)
251
0
{
252
0
  if (wpa_s->conf->ap_scan == 0 &&
253
0
      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
254
0
    return;
255
256
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
257
0
    "%d usec", sec, usec);
258
0
  eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
259
0
  wpa_s->last_auth_timeout_sec = sec;
260
0
  eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
261
0
}
262
263
264
/*
265
 * wpas_auth_timeout_restart - Restart and change timeout for authentication
266
 * @wpa_s: Pointer to wpa_supplicant data
267
 * @sec_diff: difference in seconds applied to original timeout value
268
 */
269
void wpas_auth_timeout_restart(struct wpa_supplicant *wpa_s, int sec_diff)
270
0
{
271
0
  int new_sec = wpa_s->last_auth_timeout_sec + sec_diff;
272
273
0
  if (eloop_is_timeout_registered(wpa_supplicant_timeout, wpa_s, NULL)) {
274
0
    wpa_dbg(wpa_s, MSG_DEBUG,
275
0
      "Authentication timeout restart: %d sec", new_sec);
276
0
    eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
277
0
    eloop_register_timeout(new_sec, 0, wpa_supplicant_timeout,
278
0
               wpa_s, NULL);
279
0
  }
280
0
}
281
282
283
/**
284
 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
285
 * @wpa_s: Pointer to wpa_supplicant data
286
 *
287
 * This function is used to cancel authentication timeout scheduled with
288
 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
289
 * been completed.
290
 */
291
void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
292
0
{
293
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
294
0
  eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
295
0
  wpa_bssid_ignore_del(wpa_s, wpa_s->bssid);
296
0
  os_free(wpa_s->last_con_fail_realm);
297
0
  wpa_s->last_con_fail_realm = NULL;
298
0
  wpa_s->last_con_fail_realm_len = 0;
299
0
}
300
301
302
/**
303
 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
304
 * @wpa_s: Pointer to wpa_supplicant data
305
 *
306
 * This function is used to configure EAPOL state machine based on the selected
307
 * authentication mode.
308
 */
309
void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
310
0
{
311
0
#ifdef IEEE8021X_EAPOL
312
0
  struct eapol_config eapol_conf;
313
0
  struct wpa_ssid *ssid = wpa_s->current_ssid;
314
315
#ifdef CONFIG_IBSS_RSN
316
  if (ssid->mode == WPAS_MODE_IBSS &&
317
      wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
318
      wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
319
    /*
320
     * RSN IBSS authentication is per-STA and we can disable the
321
     * per-BSSID EAPOL authentication.
322
     */
323
    eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
324
    eapol_sm_notify_eap_success(wpa_s->eapol, true);
325
    eapol_sm_notify_eap_fail(wpa_s->eapol, false);
326
    return;
327
  }
328
#endif /* CONFIG_IBSS_RSN */
329
330
0
  eapol_sm_notify_eap_success(wpa_s->eapol, false);
331
0
  eapol_sm_notify_eap_fail(wpa_s->eapol, false);
332
333
0
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
334
0
      wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
335
0
    eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
336
0
  else
337
0
    eapol_sm_notify_portControl(wpa_s->eapol, Auto);
338
339
0
  os_memset(&eapol_conf, 0, sizeof(eapol_conf));
340
0
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
341
0
    eapol_conf.accept_802_1x_keys = 1;
342
0
    eapol_conf.required_keys = 0;
343
0
    if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
344
0
      eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
345
0
    }
346
0
    if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
347
0
      eapol_conf.required_keys |=
348
0
        EAPOL_REQUIRE_KEY_BROADCAST;
349
0
    }
350
351
0
    if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
352
0
      eapol_conf.required_keys = 0;
353
0
  }
354
0
  eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
355
0
  eapol_conf.workaround = ssid->eap_workaround;
356
0
  eapol_conf.eap_disabled =
357
0
    !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
358
0
    wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
359
0
    wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
360
0
  eapol_conf.external_sim = wpa_s->conf->external_sim;
361
362
#ifdef CONFIG_WPS
363
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
364
    eapol_conf.wps |= EAPOL_LOCAL_WPS_IN_USE;
365
    if (wpa_s->current_bss) {
366
      struct wpabuf *ie;
367
      ie = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
368
               WPS_IE_VENDOR_TYPE);
369
      if (ie) {
370
        if (wps_is_20(ie))
371
          eapol_conf.wps |=
372
            EAPOL_PEER_IS_WPS20_AP;
373
        wpabuf_free(ie);
374
      }
375
    }
376
  }
377
#endif /* CONFIG_WPS */
378
379
0
  eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
380
381
#ifdef CONFIG_MACSEC
382
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE && ssid->mka_psk_set)
383
    ieee802_1x_create_preshared_mka(wpa_s, ssid);
384
  else
385
    ieee802_1x_alloc_kay_sm(wpa_s, ssid);
386
#endif /* CONFIG_MACSEC */
387
0
#endif /* IEEE8021X_EAPOL */
388
0
}
389
390
391
/**
392
 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
393
 * @wpa_s: Pointer to wpa_supplicant data
394
 * @ssid: Configuration data for the network
395
 *
396
 * This function is used to configure WPA state machine and related parameters
397
 * to a mode where WPA is not enabled. This is called as part of the
398
 * authentication configuration when the selected network does not use WPA.
399
 */
400
void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
401
               struct wpa_ssid *ssid)
402
0
{
403
#ifdef CONFIG_WEP
404
  int i;
405
#endif /* CONFIG_WEP */
406
0
  struct wpa_sm_mlo mlo;
407
408
0
  if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
409
0
    wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
410
0
  else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
411
0
    wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
412
0
  else
413
0
    wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
414
0
  wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
415
0
  wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
416
0
  wpa_sm_set_ap_rsnxe(wpa_s->wpa, NULL, 0);
417
0
  wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
418
0
  wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
419
0
  wpa_s->rsnxe_len = 0;
420
0
  wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
421
0
  wpa_s->group_cipher = WPA_CIPHER_NONE;
422
0
  wpa_s->mgmt_group_cipher = 0;
423
424
#ifdef CONFIG_WEP
425
  for (i = 0; i < NUM_WEP_KEYS; i++) {
426
    if (ssid->wep_key_len[i] > 5) {
427
      wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
428
      wpa_s->group_cipher = WPA_CIPHER_WEP104;
429
      break;
430
    } else if (ssid->wep_key_len[i] > 0) {
431
      wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
432
      wpa_s->group_cipher = WPA_CIPHER_WEP40;
433
      break;
434
    }
435
  }
436
#endif /* CONFIG_WEP */
437
438
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
439
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
440
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
441
0
       wpa_s->pairwise_cipher);
442
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
443
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
444
0
       wpa_s->mgmt_group_cipher);
445
446
0
  pmksa_cache_clear_current(wpa_s->wpa);
447
0
  os_memset(&mlo, 0, sizeof(mlo));
448
0
  wpa_sm_set_mlo_params(wpa_s->wpa, &mlo);
449
0
}
450
451
452
void free_hw_features(struct wpa_supplicant *wpa_s)
453
0
{
454
0
  int i;
455
0
  if (wpa_s->hw.modes == NULL)
456
0
    return;
457
458
0
  for (i = 0; i < wpa_s->hw.num_modes; i++) {
459
0
    os_free(wpa_s->hw.modes[i].channels);
460
0
    os_free(wpa_s->hw.modes[i].rates);
461
0
  }
462
463
0
  os_free(wpa_s->hw.modes);
464
0
  wpa_s->hw.modes = NULL;
465
0
}
466
467
468
static void remove_bss_tmp_disallowed_entry(struct wpa_supplicant *wpa_s,
469
              struct wpa_bss_tmp_disallowed *bss)
470
0
{
471
0
  eloop_cancel_timeout(wpa_bss_tmp_disallow_timeout, wpa_s, bss);
472
0
  dl_list_del(&bss->list);
473
0
  os_free(bss);
474
0
}
475
476
477
void free_bss_tmp_disallowed(struct wpa_supplicant *wpa_s)
478
0
{
479
0
  struct wpa_bss_tmp_disallowed *bss, *prev;
480
481
0
  dl_list_for_each_safe(bss, prev, &wpa_s->bss_tmp_disallowed,
482
0
            struct wpa_bss_tmp_disallowed, list)
483
0
    remove_bss_tmp_disallowed_entry(wpa_s, bss);
484
0
}
485
486
487
void wpas_flush_fils_hlp_req(struct wpa_supplicant *wpa_s)
488
0
{
489
0
  struct fils_hlp_req *req;
490
491
0
  while ((req = dl_list_first(&wpa_s->fils_hlp_req, struct fils_hlp_req,
492
0
            list)) != NULL) {
493
0
    dl_list_del(&req->list);
494
0
    wpabuf_free(req->pkt);
495
0
    os_free(req);
496
0
  }
497
0
}
498
499
500
void wpas_clear_disabled_interface(void *eloop_ctx, void *timeout_ctx)
501
0
{
502
0
  struct wpa_supplicant *wpa_s = eloop_ctx;
503
504
0
  if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
505
0
    return;
506
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Clear cached state on disabled interface");
507
0
  wpa_bss_flush(wpa_s);
508
0
}
509
510
511
#ifdef CONFIG_TESTING_OPTIONS
512
void wpas_clear_driver_signal_override(struct wpa_supplicant *wpa_s)
513
{
514
  struct driver_signal_override *dso;
515
516
  while ((dso = dl_list_first(&wpa_s->drv_signal_override,
517
            struct driver_signal_override, list))) {
518
    dl_list_del(&dso->list);
519
    os_free(dso);
520
  }
521
}
522
#endif /* CONFIG_TESTING_OPTIONS */
523
524
525
static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
526
0
{
527
0
  int i;
528
529
0
  bgscan_deinit(wpa_s);
530
0
  autoscan_deinit(wpa_s);
531
0
  scard_deinit(wpa_s->scard);
532
0
  wpa_s->scard = NULL;
533
0
  wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
534
0
  eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
535
0
  l2_packet_deinit(wpa_s->l2);
536
0
  wpa_s->l2 = NULL;
537
0
  if (wpa_s->l2_br) {
538
0
    l2_packet_deinit(wpa_s->l2_br);
539
0
    wpa_s->l2_br = NULL;
540
0
  }
541
#ifdef CONFIG_TESTING_OPTIONS
542
  l2_packet_deinit(wpa_s->l2_test);
543
  wpa_s->l2_test = NULL;
544
  os_free(wpa_s->get_pref_freq_list_override);
545
  wpa_s->get_pref_freq_list_override = NULL;
546
  wpabuf_free(wpa_s->last_assoc_req_wpa_ie);
547
  wpa_s->last_assoc_req_wpa_ie = NULL;
548
  os_free(wpa_s->extra_sae_rejected_groups);
549
  wpa_s->extra_sae_rejected_groups = NULL;
550
  wpabuf_free(wpa_s->rsne_override_eapol);
551
  wpa_s->rsne_override_eapol = NULL;
552
  wpabuf_free(wpa_s->rsnxe_override_assoc);
553
  wpa_s->rsnxe_override_assoc = NULL;
554
  wpabuf_free(wpa_s->rsnxe_override_eapol);
555
  wpa_s->rsnxe_override_eapol = NULL;
556
  wpas_clear_driver_signal_override(wpa_s);
557
#endif /* CONFIG_TESTING_OPTIONS */
558
559
0
  if (wpa_s->conf != NULL) {
560
0
    struct wpa_ssid *ssid;
561
0
    for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
562
0
      wpas_notify_network_removed(wpa_s, ssid);
563
0
  }
564
565
0
  os_free(wpa_s->confname);
566
0
  wpa_s->confname = NULL;
567
568
0
  os_free(wpa_s->confanother);
569
0
  wpa_s->confanother = NULL;
570
571
0
  os_free(wpa_s->last_con_fail_realm);
572
0
  wpa_s->last_con_fail_realm = NULL;
573
0
  wpa_s->last_con_fail_realm_len = 0;
574
575
0
  wpa_sm_set_eapol(wpa_s->wpa, NULL);
576
0
  eapol_sm_deinit(wpa_s->eapol);
577
0
  wpa_s->eapol = NULL;
578
579
0
  rsn_preauth_deinit(wpa_s->wpa);
580
581
#ifdef CONFIG_TDLS
582
  wpa_tdls_deinit(wpa_s->wpa);
583
#endif /* CONFIG_TDLS */
584
585
0
  wmm_ac_clear_saved_tspecs(wpa_s);
586
0
  pmksa_candidate_free(wpa_s->wpa);
587
0
  ptksa_cache_deinit(wpa_s->ptksa);
588
0
  wpa_s->ptksa = NULL;
589
0
  wpa_sm_deinit(wpa_s->wpa);
590
0
  wpa_s->wpa = NULL;
591
0
  wpa_bssid_ignore_clear(wpa_s);
592
593
#ifdef CONFIG_PASN
594
  wpas_pasn_auth_stop(wpa_s);
595
#endif /* CONFIG_PASN */
596
597
0
  wpa_bss_deinit(wpa_s);
598
599
0
  wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
600
0
  wpa_supplicant_cancel_scan(wpa_s);
601
0
  wpa_supplicant_cancel_auth_timeout(wpa_s);
602
0
  eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
603
#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
604
  eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
605
           wpa_s, NULL);
606
#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
607
608
0
  eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
609
0
  eloop_cancel_timeout(wpas_clear_disabled_interface, wpa_s, NULL);
610
611
0
  wpas_wps_deinit(wpa_s);
612
613
0
  wpabuf_free(wpa_s->pending_eapol_rx);
614
0
  wpa_s->pending_eapol_rx = NULL;
615
616
#ifdef CONFIG_IBSS_RSN
617
  ibss_rsn_deinit(wpa_s->ibss_rsn);
618
  wpa_s->ibss_rsn = NULL;
619
#endif /* CONFIG_IBSS_RSN */
620
621
0
  sme_deinit(wpa_s);
622
623
#ifdef CONFIG_AP
624
  wpa_supplicant_ap_deinit(wpa_s);
625
#endif /* CONFIG_AP */
626
627
0
  wpas_p2p_deinit(wpa_s);
628
629
#ifdef CONFIG_OFFCHANNEL
630
  offchannel_deinit(wpa_s);
631
#endif /* CONFIG_OFFCHANNEL */
632
633
0
  wpa_supplicant_cancel_sched_scan(wpa_s);
634
635
0
  os_free(wpa_s->next_scan_freqs);
636
0
  wpa_s->next_scan_freqs = NULL;
637
638
0
  os_free(wpa_s->manual_scan_freqs);
639
0
  wpa_s->manual_scan_freqs = NULL;
640
0
  os_free(wpa_s->select_network_scan_freqs);
641
0
  wpa_s->select_network_scan_freqs = NULL;
642
643
0
  os_free(wpa_s->manual_sched_scan_freqs);
644
0
  wpa_s->manual_sched_scan_freqs = NULL;
645
646
0
  wpas_mac_addr_rand_scan_clear(wpa_s, MAC_ADDR_RAND_ALL);
647
648
  /*
649
   * Need to remove any pending gas-query radio work before the
650
   * gas_query_deinit() call because gas_query::work has not yet been set
651
   * for works that have not been started. gas_query_free() will be unable
652
   * to cancel such pending radio works and once the pending gas-query
653
   * radio work eventually gets removed, the deinit notification call to
654
   * gas_query_start_cb() would result in dereferencing freed memory.
655
   */
656
0
  if (wpa_s->radio)
657
0
    radio_remove_works(wpa_s, "gas-query", 0);
658
0
  gas_query_deinit(wpa_s->gas);
659
0
  wpa_s->gas = NULL;
660
0
  gas_server_deinit(wpa_s->gas_server);
661
0
  wpa_s->gas_server = NULL;
662
663
0
  free_hw_features(wpa_s);
664
665
0
  ieee802_1x_dealloc_kay_sm(wpa_s);
666
667
0
  os_free(wpa_s->bssid_filter);
668
0
  wpa_s->bssid_filter = NULL;
669
670
0
  os_free(wpa_s->disallow_aps_bssid);
671
0
  wpa_s->disallow_aps_bssid = NULL;
672
0
  os_free(wpa_s->disallow_aps_ssid);
673
0
  wpa_s->disallow_aps_ssid = NULL;
674
675
0
  wnm_bss_keep_alive_deinit(wpa_s);
676
0
#ifdef CONFIG_WNM
677
0
  wnm_deallocate_memory(wpa_s);
678
0
#endif /* CONFIG_WNM */
679
680
0
  ext_password_deinit(wpa_s->ext_pw);
681
0
  wpa_s->ext_pw = NULL;
682
683
0
  wpabuf_free(wpa_s->last_gas_resp);
684
0
  wpa_s->last_gas_resp = NULL;
685
0
  wpabuf_free(wpa_s->prev_gas_resp);
686
0
  wpa_s->prev_gas_resp = NULL;
687
688
0
  os_free(wpa_s->last_scan_res);
689
0
  wpa_s->last_scan_res = NULL;
690
691
0
#ifdef CONFIG_HS20
692
0
  if (wpa_s->drv_priv)
693
0
    wpa_drv_configure_frame_filters(wpa_s, 0);
694
0
  hs20_deinit(wpa_s);
695
0
#endif /* CONFIG_HS20 */
696
697
0
  for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
698
0
    wpabuf_free(wpa_s->vendor_elem[i]);
699
0
    wpa_s->vendor_elem[i] = NULL;
700
0
  }
701
702
0
  wmm_ac_notify_disassoc(wpa_s);
703
704
0
  wpa_s->sched_scan_plans_num = 0;
705
0
  os_free(wpa_s->sched_scan_plans);
706
0
  wpa_s->sched_scan_plans = NULL;
707
708
#ifdef CONFIG_MBO
709
  wpa_s->non_pref_chan_num = 0;
710
  os_free(wpa_s->non_pref_chan);
711
  wpa_s->non_pref_chan = NULL;
712
#endif /* CONFIG_MBO */
713
714
0
  free_bss_tmp_disallowed(wpa_s);
715
716
0
  wpabuf_free(wpa_s->lci);
717
0
  wpa_s->lci = NULL;
718
0
  wpas_clear_beacon_rep_data(wpa_s);
719
720
#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
721
#ifdef CONFIG_MESH
722
  {
723
    struct external_pmksa_cache *entry;
724
725
    while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
726
               struct external_pmksa_cache,
727
               list)) != NULL) {
728
      dl_list_del(&entry->list);
729
      os_free(entry->pmksa_cache);
730
      os_free(entry);
731
    }
732
  }
733
#endif /* CONFIG_MESH */
734
#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
735
736
0
  wpas_flush_fils_hlp_req(wpa_s);
737
738
0
  wpabuf_free(wpa_s->ric_ies);
739
0
  wpa_s->ric_ies = NULL;
740
741
#ifdef CONFIG_DPP
742
  wpas_dpp_deinit(wpa_s);
743
  dpp_global_deinit(wpa_s->dpp);
744
  wpa_s->dpp = NULL;
745
#endif /* CONFIG_DPP */
746
747
#ifdef CONFIG_PASN
748
  wpas_pasn_auth_stop(wpa_s);
749
#endif /* CONFIG_PASN */
750
0
  wpas_scs_deinit(wpa_s);
751
0
  wpas_dscp_deinit(wpa_s);
752
0
}
753
754
755
/**
756
 * wpa_clear_keys - Clear keys configured for the driver
757
 * @wpa_s: Pointer to wpa_supplicant data
758
 * @addr: Previously used BSSID or %NULL if not available
759
 *
760
 * This function clears the encryption keys that has been previously configured
761
 * for the driver.
762
 */
763
void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
764
0
{
765
0
  int i, max = 6;
766
767
  /* MLME-DELETEKEYS.request */
768
0
  for (i = 0; i < max; i++) {
769
0
    if (wpa_s->keys_cleared & BIT(i))
770
0
      continue;
771
0
    wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
772
0
        NULL, 0, KEY_FLAG_GROUP);
773
0
  }
774
  /* Pairwise Key ID 1 for Extended Key ID is tracked in bit 15 */
775
0
  if (~wpa_s->keys_cleared & (BIT(0) | BIT(15)) && addr &&
776
0
      !is_zero_ether_addr(addr)) {
777
0
    if (!(wpa_s->keys_cleared & BIT(0)))
778
0
      wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, addr, 0, 0,
779
0
          NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
780
0
    if (!(wpa_s->keys_cleared & BIT(15)))
781
0
      wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, addr, 1, 0,
782
0
          NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
783
    /* MLME-SETPROTECTION.request(None) */
784
0
    wpa_drv_mlme_setprotection(
785
0
      wpa_s, addr,
786
0
      MLME_SETPROTECTION_PROTECT_TYPE_NONE,
787
0
      MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
788
0
  }
789
0
  wpa_s->keys_cleared = (u32) -1;
790
0
}
791
792
793
/**
794
 * wpa_supplicant_state_txt - Get the connection state name as a text string
795
 * @state: State (wpa_state; WPA_*)
796
 * Returns: The state name as a printable text string
797
 */
798
const char * wpa_supplicant_state_txt(enum wpa_states state)
799
0
{
800
0
  switch (state) {
801
0
  case WPA_DISCONNECTED:
802
0
    return "DISCONNECTED";
803
0
  case WPA_INACTIVE:
804
0
    return "INACTIVE";
805
0
  case WPA_INTERFACE_DISABLED:
806
0
    return "INTERFACE_DISABLED";
807
0
  case WPA_SCANNING:
808
0
    return "SCANNING";
809
0
  case WPA_AUTHENTICATING:
810
0
    return "AUTHENTICATING";
811
0
  case WPA_ASSOCIATING:
812
0
    return "ASSOCIATING";
813
0
  case WPA_ASSOCIATED:
814
0
    return "ASSOCIATED";
815
0
  case WPA_4WAY_HANDSHAKE:
816
0
    return "4WAY_HANDSHAKE";
817
0
  case WPA_GROUP_HANDSHAKE:
818
0
    return "GROUP_HANDSHAKE";
819
0
  case WPA_COMPLETED:
820
0
    return "COMPLETED";
821
0
  default:
822
0
    return "UNKNOWN";
823
0
  }
824
0
}
825
826
827
#ifdef CONFIG_BGSCAN
828
829
static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
830
{
831
  if (wpa_s->bgscan_ssid) {
832
    bgscan_deinit(wpa_s);
833
    wpa_s->bgscan_ssid = NULL;
834
  }
835
}
836
837
838
/**
839
 * wpa_supplicant_reset_bgscan - Reset the bgscan for the current SSID.
840
 * @wpa_s: Pointer to the wpa_supplicant data
841
 *
842
 * Stop, start, or reconfigure the scan parameters depending on the method.
843
 */
844
void wpa_supplicant_reset_bgscan(struct wpa_supplicant *wpa_s)
845
{
846
  const char *name;
847
848
  if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
849
    name = wpa_s->current_ssid->bgscan;
850
  else
851
    name = wpa_s->conf->bgscan;
852
  if (!name || name[0] == '\0') {
853
    wpa_supplicant_stop_bgscan(wpa_s);
854
    return;
855
  }
856
  if (wpas_driver_bss_selection(wpa_s))
857
    return;
858
#ifdef CONFIG_P2P
859
  if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
860
    return;
861
#endif /* CONFIG_P2P */
862
863
  bgscan_deinit(wpa_s);
864
  if (wpa_s->current_ssid) {
865
    if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
866
      wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
867
        "bgscan");
868
      /*
869
       * Live without bgscan; it is only used as a roaming
870
       * optimization, so the initial connection is not
871
       * affected.
872
       */
873
    } else {
874
      struct wpa_scan_results *scan_res;
875
      wpa_s->bgscan_ssid = wpa_s->current_ssid;
876
      scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
877
                   0);
878
      if (scan_res) {
879
        bgscan_notify_scan(wpa_s, scan_res);
880
        wpa_scan_results_free(scan_res);
881
      }
882
    }
883
  } else
884
    wpa_s->bgscan_ssid = NULL;
885
}
886
887
#endif /* CONFIG_BGSCAN */
888
889
890
static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
891
0
{
892
0
  if (autoscan_init(wpa_s, 0))
893
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
894
0
}
895
896
897
static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
898
0
{
899
0
  autoscan_deinit(wpa_s);
900
0
}
901
902
903
void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
904
0
{
905
0
  if (wpa_s->wpa_state == WPA_DISCONNECTED ||
906
0
      wpa_s->wpa_state == WPA_SCANNING) {
907
0
    autoscan_deinit(wpa_s);
908
0
    wpa_supplicant_start_autoscan(wpa_s);
909
0
  }
910
0
}
911
912
913
/**
914
 * wpa_supplicant_set_state - Set current connection state
915
 * @wpa_s: Pointer to wpa_supplicant data
916
 * @state: The new connection state
917
 *
918
 * This function is called whenever the connection state changes, e.g.,
919
 * association is completed for WPA/WPA2 4-Way Handshake is started.
920
 */
921
void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
922
            enum wpa_states state)
923
0
{
924
0
  enum wpa_states old_state = wpa_s->wpa_state;
925
#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
926
  bool update_fils_connect_params = false;
927
#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
928
929
0
  wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
930
0
    wpa_supplicant_state_txt(wpa_s->wpa_state),
931
0
    wpa_supplicant_state_txt(state));
932
933
0
  if (state == WPA_COMPLETED &&
934
0
      os_reltime_initialized(&wpa_s->roam_start)) {
935
0
    os_reltime_age(&wpa_s->roam_start, &wpa_s->roam_time);
936
0
    wpa_s->roam_start.sec = 0;
937
0
    wpa_s->roam_start.usec = 0;
938
0
    wpas_notify_auth_changed(wpa_s);
939
0
    wpas_notify_roam_time(wpa_s);
940
0
    wpas_notify_roam_complete(wpa_s);
941
0
  } else if (state == WPA_DISCONNECTED &&
942
0
       os_reltime_initialized(&wpa_s->roam_start)) {
943
0
    wpa_s->roam_start.sec = 0;
944
0
    wpa_s->roam_start.usec = 0;
945
0
    wpa_s->roam_time.sec = 0;
946
0
    wpa_s->roam_time.usec = 0;
947
0
    wpas_notify_roam_complete(wpa_s);
948
0
  }
949
950
0
  if (state == WPA_INTERFACE_DISABLED) {
951
    /* Assure normal scan when interface is restored */
952
0
    wpa_s->normal_scans = 0;
953
0
  }
954
955
0
  if (state == WPA_COMPLETED) {
956
0
    wpas_connect_work_done(wpa_s);
957
    /* Reinitialize normal_scan counter */
958
0
    wpa_s->normal_scans = 0;
959
0
  }
960
961
#ifdef CONFIG_P2P
962
  /*
963
   * P2PS client has to reply to Probe Request frames received on the
964
   * group operating channel. Enable Probe Request frame reporting for
965
   * P2P connected client in case p2p_cli_probe configuration property is
966
   * set to 1.
967
   */
968
  if (wpa_s->conf->p2p_cli_probe && wpa_s->current_ssid &&
969
      wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
970
      wpa_s->current_ssid->p2p_group) {
971
    if (state == WPA_COMPLETED && !wpa_s->p2p_cli_probe) {
972
      wpa_dbg(wpa_s, MSG_DEBUG,
973
        "P2P: Enable CLI Probe Request RX reporting");
974
      wpa_s->p2p_cli_probe =
975
        wpa_drv_probe_req_report(wpa_s, 1) >= 0;
976
    } else if (state != WPA_COMPLETED && wpa_s->p2p_cli_probe) {
977
      wpa_dbg(wpa_s, MSG_DEBUG,
978
        "P2P: Disable CLI Probe Request RX reporting");
979
      wpa_s->p2p_cli_probe = 0;
980
      wpa_drv_probe_req_report(wpa_s, 0);
981
    }
982
  }
983
#endif /* CONFIG_P2P */
984
985
0
  if (state != WPA_SCANNING)
986
0
    wpa_supplicant_notify_scanning(wpa_s, 0);
987
988
0
  if (state == WPA_COMPLETED && wpa_s->new_connection) {
989
0
    struct wpa_ssid *ssid = wpa_s->current_ssid;
990
0
    int fils_hlp_sent = 0;
991
0
    char mld_addr[50];
992
993
0
    mld_addr[0] = '\0';
994
0
    if (wpa_s->valid_links)
995
0
      os_snprintf(mld_addr, sizeof(mld_addr),
996
0
            " ap_mld_addr=" MACSTR,
997
0
            MAC2STR(wpa_s->ap_mld_addr));
998
999
#ifdef CONFIG_SME
1000
    if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1001
        wpa_auth_alg_fils(wpa_s->sme.auth_alg))
1002
      fils_hlp_sent = 1;
1003
#endif /* CONFIG_SME */
1004
0
    if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1005
0
        wpa_auth_alg_fils(wpa_s->auth_alg))
1006
0
      fils_hlp_sent = 1;
1007
1008
0
#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1009
0
    wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
1010
0
      MACSTR " completed [id=%d id_str=%s%s]%s",
1011
0
      MAC2STR(wpa_s->bssid),
1012
0
      ssid ? ssid->id : -1,
1013
0
      ssid && ssid->id_str ? ssid->id_str : "",
1014
0
      fils_hlp_sent ? " FILS_HLP_SENT" : "", mld_addr);
1015
0
#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1016
0
    wpas_clear_temp_disabled(wpa_s, ssid, 1);
1017
0
    wpa_s->consecutive_conn_failures = 0;
1018
0
    wpa_s->new_connection = 0;
1019
0
    wpa_drv_set_operstate(wpa_s, 1);
1020
#ifndef IEEE8021X_EAPOL
1021
    wpa_drv_set_supp_port(wpa_s, 1);
1022
#endif /* IEEE8021X_EAPOL */
1023
0
    wpa_s->after_wps = 0;
1024
0
    wpa_s->known_wps_freq = 0;
1025
0
    wpas_p2p_completed(wpa_s);
1026
1027
0
    sme_sched_obss_scan(wpa_s, 1);
1028
1029
#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
1030
    if (!fils_hlp_sent && ssid && ssid->eap.erp)
1031
      update_fils_connect_params = true;
1032
#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
1033
#ifdef CONFIG_OWE
1034
    if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_OWE))
1035
      wpas_update_owe_connect_params(wpa_s);
1036
#endif /* CONFIG_OWE */
1037
0
  } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
1038
0
       state == WPA_ASSOCIATED) {
1039
0
    wpa_s->new_connection = 1;
1040
0
    wpa_drv_set_operstate(wpa_s, 0);
1041
#ifndef IEEE8021X_EAPOL
1042
    wpa_drv_set_supp_port(wpa_s, 0);
1043
#endif /* IEEE8021X_EAPOL */
1044
0
    sme_sched_obss_scan(wpa_s, 0);
1045
0
  }
1046
0
  wpa_s->wpa_state = state;
1047
1048
#ifdef CONFIG_BGSCAN
1049
  if (state == WPA_COMPLETED && wpa_s->current_ssid != wpa_s->bgscan_ssid)
1050
    wpa_supplicant_reset_bgscan(wpa_s);
1051
  else if (state < WPA_ASSOCIATED)
1052
    wpa_supplicant_stop_bgscan(wpa_s);
1053
#endif /* CONFIG_BGSCAN */
1054
1055
0
  if (state > WPA_SCANNING)
1056
0
    wpa_supplicant_stop_autoscan(wpa_s);
1057
1058
0
  if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
1059
0
    wpa_supplicant_start_autoscan(wpa_s);
1060
1061
0
  if (old_state >= WPA_ASSOCIATED && wpa_s->wpa_state < WPA_ASSOCIATED)
1062
0
    wmm_ac_notify_disassoc(wpa_s);
1063
1064
0
  if (wpa_s->wpa_state != old_state) {
1065
0
    wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
1066
1067
    /*
1068
     * Notify the P2P Device interface about a state change in one
1069
     * of the interfaces.
1070
     */
1071
0
    wpas_p2p_indicate_state_change(wpa_s);
1072
1073
0
    if (wpa_s->wpa_state == WPA_COMPLETED ||
1074
0
        old_state == WPA_COMPLETED)
1075
0
      wpas_notify_auth_changed(wpa_s);
1076
#ifdef CONFIG_DPP2
1077
    if (wpa_s->wpa_state == WPA_COMPLETED)
1078
      wpas_dpp_connected(wpa_s);
1079
#endif /* CONFIG_DPP2 */
1080
0
  }
1081
#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
1082
  if (update_fils_connect_params)
1083
    wpas_update_fils_connect_params(wpa_s);
1084
#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
1085
0
}
1086
1087
1088
void wpa_supplicant_terminate_proc(struct wpa_global *global)
1089
0
{
1090
0
  int pending = 0;
1091
#ifdef CONFIG_WPS
1092
  struct wpa_supplicant *wpa_s = global->ifaces;
1093
  while (wpa_s) {
1094
    struct wpa_supplicant *next = wpa_s->next;
1095
    if (wpas_wps_terminate_pending(wpa_s) == 1)
1096
      pending = 1;
1097
#ifdef CONFIG_P2P
1098
    if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
1099
        (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
1100
      wpas_p2p_disconnect(wpa_s);
1101
#endif /* CONFIG_P2P */
1102
    wpa_s = next;
1103
  }
1104
#endif /* CONFIG_WPS */
1105
0
  if (pending)
1106
0
    return;
1107
0
  eloop_terminate();
1108
0
}
1109
1110
1111
static void wpa_supplicant_terminate(int sig, void *signal_ctx)
1112
0
{
1113
0
  struct wpa_global *global = signal_ctx;
1114
0
  wpa_supplicant_terminate_proc(global);
1115
0
}
1116
1117
1118
void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
1119
0
{
1120
0
  enum wpa_states old_state = wpa_s->wpa_state;
1121
0
  enum wpa_states new_state;
1122
1123
0
  if (old_state == WPA_SCANNING)
1124
0
    new_state = WPA_SCANNING;
1125
0
  else
1126
0
    new_state = WPA_DISCONNECTED;
1127
1128
0
  wpa_s->pairwise_cipher = 0;
1129
0
  wpa_s->group_cipher = 0;
1130
0
  wpa_s->mgmt_group_cipher = 0;
1131
0
  wpa_s->key_mgmt = 0;
1132
0
  wpa_s->allowed_key_mgmts = 0;
1133
0
  if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
1134
0
    wpa_supplicant_set_state(wpa_s, new_state);
1135
1136
0
  if (wpa_s->wpa_state != old_state)
1137
0
    wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
1138
0
}
1139
1140
1141
/**
1142
 * wpa_supplicant_reload_configuration - Reload configuration data
1143
 * @wpa_s: Pointer to wpa_supplicant data
1144
 * Returns: 0 on success or -1 if configuration parsing failed
1145
 *
1146
 * This function can be used to request that the configuration data is reloaded
1147
 * (e.g., after configuration file change). This function is reloading
1148
 * configuration only for one interface, so this may need to be called multiple
1149
 * times if %wpa_supplicant is controlling multiple interfaces and all
1150
 * interfaces need reconfiguration.
1151
 */
1152
int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
1153
0
{
1154
0
  struct wpa_config *conf;
1155
0
  int reconf_ctrl;
1156
0
  int old_ap_scan;
1157
1158
0
  if (wpa_s->confname == NULL)
1159
0
    return -1;
1160
0
  conf = wpa_config_read(wpa_s->confname, NULL, false);
1161
0
  if (conf == NULL) {
1162
0
    wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
1163
0
      "file '%s' - exiting", wpa_s->confname);
1164
0
    return -1;
1165
0
  }
1166
0
  if (wpa_s->confanother &&
1167
0
      !wpa_config_read(wpa_s->confanother, conf, true)) {
1168
0
    wpa_msg(wpa_s, MSG_ERROR,
1169
0
      "Failed to parse the configuration file '%s' - exiting",
1170
0
      wpa_s->confanother);
1171
0
    return -1;
1172
0
  }
1173
1174
0
  conf->changed_parameters = (unsigned int) -1;
1175
1176
0
  reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
1177
0
    || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
1178
0
        os_strcmp(conf->ctrl_interface,
1179
0
            wpa_s->conf->ctrl_interface) != 0);
1180
1181
0
  if (reconf_ctrl) {
1182
0
    wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
1183
0
    wpa_s->ctrl_iface = NULL;
1184
0
  }
1185
1186
0
  eapol_sm_invalidate_cached_session(wpa_s->eapol);
1187
0
  if (wpa_s->current_ssid) {
1188
0
    if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1189
0
      wpa_s->own_disconnect_req = 1;
1190
0
    wpa_supplicant_deauthenticate(wpa_s,
1191
0
                WLAN_REASON_DEAUTH_LEAVING);
1192
0
  }
1193
1194
  /*
1195
   * TODO: should notify EAPOL SM about changes in opensc_engine_path,
1196
   * pkcs11_engine_path, pkcs11_module_path, openssl_ciphers.
1197
   */
1198
0
  if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1199
0
      wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
1200
0
      wpa_s->key_mgmt == WPA_KEY_MGMT_DPP) {
1201
    /*
1202
     * Clear forced success to clear EAP state for next
1203
     * authentication.
1204
     */
1205
0
    eapol_sm_notify_eap_success(wpa_s->eapol, false);
1206
0
  }
1207
0
  eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1208
0
  wpa_sm_set_config(wpa_s->wpa, NULL);
1209
0
  wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
1210
0
  wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1211
0
  rsn_preauth_deinit(wpa_s->wpa);
1212
1213
0
  old_ap_scan = wpa_s->conf->ap_scan;
1214
0
  wpa_config_free(wpa_s->conf);
1215
0
  wpa_s->conf = conf;
1216
0
  if (old_ap_scan != wpa_s->conf->ap_scan)
1217
0
    wpas_notify_ap_scan_changed(wpa_s);
1218
1219
0
  if (reconf_ctrl)
1220
0
    wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1221
1222
0
  wpa_supplicant_update_config(wpa_s);
1223
1224
0
  wpa_supplicant_clear_status(wpa_s);
1225
0
  if (wpa_supplicant_enabled_networks(wpa_s)) {
1226
0
    wpa_s->reassociate = 1;
1227
0
    wpa_supplicant_req_scan(wpa_s, 0, 0);
1228
0
  }
1229
0
  wpa_bssid_ignore_clear(wpa_s);
1230
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
1231
0
  return 0;
1232
0
}
1233
1234
1235
static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
1236
0
{
1237
0
  struct wpa_global *global = signal_ctx;
1238
0
  struct wpa_supplicant *wpa_s;
1239
0
  for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1240
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
1241
0
      sig);
1242
0
    if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
1243
0
      wpa_supplicant_terminate_proc(global);
1244
0
    }
1245
0
  }
1246
1247
0
  if (wpa_debug_reopen_file() < 0) {
1248
    /* Ignore errors since we cannot really do much to fix this */
1249
0
    wpa_printf(MSG_DEBUG, "Could not reopen debug log file");
1250
0
  }
1251
0
}
1252
1253
1254
static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
1255
           struct wpa_ssid *ssid,
1256
           struct wpa_ie_data *ie)
1257
0
{
1258
0
  int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
1259
0
  if (ret) {
1260
0
    if (ret == -2) {
1261
0
      wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
1262
0
        "from association info");
1263
0
    }
1264
0
    return -1;
1265
0
  }
1266
1267
0
  wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
1268
0
    "cipher suites");
1269
0
  if (!(ie->group_cipher & ssid->group_cipher)) {
1270
0
    wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
1271
0
      "cipher 0x%x (mask 0x%x) - reject",
1272
0
      ie->group_cipher, ssid->group_cipher);
1273
0
    return -1;
1274
0
  }
1275
0
  if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
1276
0
    wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
1277
0
      "cipher 0x%x (mask 0x%x) - reject",
1278
0
      ie->pairwise_cipher, ssid->pairwise_cipher);
1279
0
    return -1;
1280
0
  }
1281
0
  if (!(ie->key_mgmt & ssid->key_mgmt)) {
1282
0
    wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
1283
0
      "management 0x%x (mask 0x%x) - reject",
1284
0
      ie->key_mgmt, ssid->key_mgmt);
1285
0
    return -1;
1286
0
  }
1287
1288
0
  if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
1289
0
      wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
1290
0
    wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
1291
0
      "that does not support management frame protection - "
1292
0
      "reject");
1293
0
    return -1;
1294
0
  }
1295
1296
0
  return 0;
1297
0
}
1298
1299
1300
static int matching_ciphers(struct wpa_ssid *ssid, struct wpa_ie_data *ie,
1301
          int freq)
1302
0
{
1303
0
  if (!ie->has_group)
1304
0
    ie->group_cipher = wpa_default_rsn_cipher(freq);
1305
0
  if (!ie->has_pairwise)
1306
0
    ie->pairwise_cipher = wpa_default_rsn_cipher(freq);
1307
0
  return (ie->group_cipher & ssid->group_cipher) &&
1308
0
    (ie->pairwise_cipher & ssid->pairwise_cipher);
1309
0
}
1310
1311
1312
void wpas_set_mgmt_group_cipher(struct wpa_supplicant *wpa_s,
1313
        struct wpa_ssid *ssid, struct wpa_ie_data *ie)
1314
0
{
1315
0
  int sel;
1316
1317
0
  sel = ie->mgmt_group_cipher;
1318
0
  if (ssid->group_mgmt_cipher)
1319
0
    sel &= ssid->group_mgmt_cipher;
1320
0
  if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION ||
1321
0
      !(ie->capabilities & WPA_CAPABILITY_MFPC))
1322
0
    sel = 0;
1323
0
  wpa_dbg(wpa_s, MSG_DEBUG,
1324
0
    "WPA: AP mgmt_group_cipher 0x%x network profile mgmt_group_cipher 0x%x; available mgmt_group_cipher 0x%x",
1325
0
    ie->mgmt_group_cipher, ssid->group_mgmt_cipher, sel);
1326
0
  if (sel & WPA_CIPHER_AES_128_CMAC) {
1327
0
    wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1328
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1329
0
      "WPA: using MGMT group cipher AES-128-CMAC");
1330
0
  } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1331
0
    wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1332
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1333
0
      "WPA: using MGMT group cipher BIP-GMAC-128");
1334
0
  } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1335
0
    wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1336
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1337
0
      "WPA: using MGMT group cipher BIP-GMAC-256");
1338
0
  } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1339
0
    wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1340
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1341
0
      "WPA: using MGMT group cipher BIP-CMAC-256");
1342
0
  } else {
1343
0
    wpa_s->mgmt_group_cipher = 0;
1344
0
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1345
0
  }
1346
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1347
0
       wpa_s->mgmt_group_cipher);
1348
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1349
0
       wpas_get_ssid_pmf(wpa_s, ssid));
1350
0
}
1351
1352
/**
1353
 * wpa_supplicant_get_psk - Get PSK from config or external database
1354
 * @wpa_s: Pointer to wpa_supplicant data
1355
 * @bss: Scan results for the selected BSS, or %NULL if not available
1356
 * @ssid: Configuration data for the selected network
1357
 * @psk: Buffer for the PSK
1358
 * Returns: 0 on success or -1 if configuration parsing failed
1359
 *
1360
 * This function obtains the PSK for a network, either included inline in the
1361
 * config or retrieved from an external database.
1362
 */
1363
static int wpa_supplicant_get_psk(struct wpa_supplicant *wpa_s,
1364
          struct wpa_bss *bss, struct wpa_ssid *ssid,
1365
          u8 *psk)
1366
0
{
1367
0
  if (ssid->psk_set) {
1368
0
    wpa_hexdump_key(MSG_MSGDUMP, "PSK (set in config)",
1369
0
        ssid->psk, PMK_LEN);
1370
0
    os_memcpy(psk, ssid->psk, PMK_LEN);
1371
0
    return 0;
1372
0
  }
1373
1374
0
#ifndef CONFIG_NO_PBKDF2
1375
0
  if (bss && ssid->bssid_set && ssid->ssid_len == 0 && ssid->passphrase) {
1376
0
    if (pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1377
0
        4096, psk, PMK_LEN) != 0) {
1378
0
      wpa_msg(wpa_s, MSG_WARNING, "Error in pbkdf2_sha1()");
1379
0
      return -1;
1380
0
    }
1381
0
    wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1382
0
        psk, PMK_LEN);
1383
0
    return 0;
1384
0
  }
1385
0
#endif /* CONFIG_NO_PBKDF2 */
1386
1387
#ifdef CONFIG_EXT_PASSWORD
1388
  if (ssid->ext_psk) {
1389
    struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1390
                 ssid->ext_psk);
1391
    char pw_str[64 + 1];
1392
1393
    if (!pw) {
1394
      wpa_msg(wpa_s, MSG_INFO,
1395
        "EXT PW: No PSK found from external storage");
1396
      return -1;
1397
    }
1398
1399
    if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1400
      wpa_msg(wpa_s, MSG_INFO,
1401
        "EXT PW: Unexpected PSK length %d in external storage",
1402
        (int) wpabuf_len(pw));
1403
      ext_password_free(pw);
1404
      return -1;
1405
    }
1406
1407
    os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1408
    pw_str[wpabuf_len(pw)] = '\0';
1409
1410
#ifndef CONFIG_NO_PBKDF2
1411
    if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1412
    {
1413
      if (pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1414
          4096, psk, PMK_LEN) != 0) {
1415
        wpa_msg(wpa_s, MSG_WARNING,
1416
          "Error in pbkdf2_sha1()");
1417
        forced_memzero(pw_str, sizeof(pw_str));
1418
        ext_password_free(pw);
1419
        return -1;
1420
      }
1421
      wpa_hexdump_key(MSG_MSGDUMP,
1422
          "PSK (from external passphrase)",
1423
          psk, PMK_LEN);
1424
    } else
1425
#endif /* CONFIG_NO_PBKDF2 */
1426
    if (wpabuf_len(pw) == 2 * PMK_LEN) {
1427
      if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1428
        wpa_msg(wpa_s, MSG_INFO,
1429
          "EXT PW: Invalid PSK hex string");
1430
        forced_memzero(pw_str, sizeof(pw_str));
1431
        ext_password_free(pw);
1432
        return -1;
1433
      }
1434
      wpa_hexdump_key(MSG_MSGDUMP, "PSK (from external PSK)",
1435
          psk, PMK_LEN);
1436
    } else {
1437
      wpa_msg(wpa_s, MSG_INFO,
1438
        "EXT PW: No suitable PSK available");
1439
      forced_memzero(pw_str, sizeof(pw_str));
1440
      ext_password_free(pw);
1441
      return -1;
1442
    }
1443
1444
    forced_memzero(pw_str, sizeof(pw_str));
1445
    ext_password_free(pw);
1446
1447
    return 0;
1448
  }
1449
#endif /* CONFIG_EXT_PASSWORD */
1450
1451
0
  return -1;
1452
0
}
1453
1454
1455
static void wpas_update_allowed_key_mgmt(struct wpa_supplicant *wpa_s,
1456
           struct wpa_ssid *ssid)
1457
0
{
1458
0
  int akm_count = wpa_s->max_num_akms;
1459
0
  u8 capab = 0;
1460
1461
0
  if (akm_count < 2)
1462
0
    return;
1463
1464
0
  akm_count--;
1465
0
  wpa_s->allowed_key_mgmts = 0;
1466
0
  switch (wpa_s->key_mgmt) {
1467
0
  case WPA_KEY_MGMT_PSK:
1468
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1469
0
      akm_count--;
1470
0
      wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE;
1471
0
    }
1472
0
    if (!akm_count)
1473
0
      break;
1474
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1475
0
      akm_count--;
1476
0
      wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE_EXT_KEY;
1477
0
    }
1478
0
    if (!akm_count)
1479
0
      break;
1480
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1481
0
      wpa_s->allowed_key_mgmts |=
1482
0
        WPA_KEY_MGMT_PSK_SHA256;
1483
0
    break;
1484
0
  case WPA_KEY_MGMT_PSK_SHA256:
1485
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1486
0
      akm_count--;
1487
0
      wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE;
1488
0
    }
1489
0
    if (!akm_count)
1490
0
      break;
1491
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1492
0
      akm_count--;
1493
0
      wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE_EXT_KEY;
1494
0
    }
1495
0
    if (!akm_count)
1496
0
      break;
1497
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
1498
0
      wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_PSK;
1499
0
    break;
1500
0
  case WPA_KEY_MGMT_SAE:
1501
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1502
0
      akm_count--;
1503
0
      wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_PSK;
1504
0
    }
1505
0
    if (!akm_count)
1506
0
      break;
1507
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1508
0
      akm_count--;
1509
0
      wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE_EXT_KEY;
1510
0
    }
1511
0
    if (!akm_count)
1512
0
      break;
1513
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1514
0
      wpa_s->allowed_key_mgmts |=
1515
0
        WPA_KEY_MGMT_PSK_SHA256;
1516
0
    break;
1517
0
  case WPA_KEY_MGMT_SAE_EXT_KEY:
1518
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1519
0
      akm_count--;
1520
0
      wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE;
1521
0
    }
1522
0
    if (!akm_count)
1523
0
      break;
1524
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1525
0
      akm_count--;
1526
0
      wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_PSK;
1527
0
    }
1528
0
    if (!akm_count)
1529
0
      break;
1530
0
    if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1531
0
      wpa_s->allowed_key_mgmts |=
1532
0
        WPA_KEY_MGMT_PSK_SHA256;
1533
0
    break;
1534
0
  default:
1535
0
    return;
1536
0
  }
1537
1538
0
  if (wpa_s->conf->sae_pwe != SAE_PWE_HUNT_AND_PECK &&
1539
0
      wpa_s->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
1540
0
    capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E);
1541
#ifdef CONFIG_SAE_PK
1542
  if (ssid->sae_pk)
1543
    capab |= BIT(WLAN_RSNX_CAPAB_SAE_PK);
1544
#endif /* CONFIG_SAE_PK */
1545
1546
0
  if (!((wpa_s->allowed_key_mgmts &
1547
0
         (WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_SAE_EXT_KEY)) && capab))
1548
0
    return;
1549
1550
0
  if (!wpa_s->rsnxe_len) {
1551
0
    wpa_s->rsnxe_len = 3;
1552
0
    wpa_s->rsnxe[0] = WLAN_EID_RSNX;
1553
0
    wpa_s->rsnxe[1] = 1;
1554
0
    wpa_s->rsnxe[2] = 0;
1555
0
  }
1556
1557
0
  wpa_s->rsnxe[2] |= capab;
1558
0
}
1559
1560
1561
/**
1562
 * wpa_supplicant_set_suites - Set authentication and encryption parameters
1563
 * @wpa_s: Pointer to wpa_supplicant data
1564
 * @bss: Scan results for the selected BSS, or %NULL if not available
1565
 * @ssid: Configuration data for the selected network
1566
 * @wpa_ie: Buffer for the WPA/RSN IE
1567
 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
1568
 * used buffer length in case the functions returns success.
1569
 * @skip_default_rsne: Whether to skip setting of the default RSNE/RSNXE
1570
 * Returns: 0 on success or -1 on failure
1571
 *
1572
 * This function is used to configure authentication and encryption parameters
1573
 * based on the network configuration and scan result for the selected BSS (if
1574
 * available).
1575
 */
1576
int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
1577
            struct wpa_bss *bss, struct wpa_ssid *ssid,
1578
            u8 *wpa_ie, size_t *wpa_ie_len,
1579
            bool skip_default_rsne)
1580
0
{
1581
0
  struct wpa_ie_data ie;
1582
0
  int sel, proto;
1583
0
  enum sae_pwe sae_pwe;
1584
0
  const u8 *bss_wpa, *bss_rsn, *bss_rsnx, *bss_osen;
1585
1586
0
  if (bss) {
1587
0
    bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1588
0
    bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1589
0
    bss_rsnx = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
1590
0
    bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
1591
0
  } else {
1592
0
    bss_wpa = bss_rsn = bss_rsnx = bss_osen = NULL;
1593
0
  }
1594
1595
0
  if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
1596
0
      wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
1597
0
      matching_ciphers(ssid, &ie, bss->freq) &&
1598
0
      (ie.key_mgmt & ssid->key_mgmt)) {
1599
0
    wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
1600
0
    proto = WPA_PROTO_RSN;
1601
0
  } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
1602
0
       wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie) == 0 &&
1603
0
       (ie.group_cipher & ssid->group_cipher) &&
1604
0
       (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1605
0
       (ie.key_mgmt & ssid->key_mgmt)) {
1606
0
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
1607
0
    proto = WPA_PROTO_WPA;
1608
0
#ifdef CONFIG_HS20
1609
0
  } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN) &&
1610
0
       wpa_parse_wpa_ie(bss_osen, 2 + bss_osen[1], &ie) == 0 &&
1611
0
       (ie.group_cipher & ssid->group_cipher) &&
1612
0
       (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1613
0
       (ie.key_mgmt & ssid->key_mgmt)) {
1614
0
    wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
1615
0
    proto = WPA_PROTO_OSEN;
1616
0
  } else if (bss_rsn && (ssid->proto & WPA_PROTO_OSEN) &&
1617
0
      wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
1618
0
      (ie.group_cipher & ssid->group_cipher) &&
1619
0
      (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1620
0
      (ie.key_mgmt & ssid->key_mgmt)) {
1621
0
    wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using OSEN (within RSN)");
1622
0
    proto = WPA_PROTO_RSN;
1623
0
#endif /* CONFIG_HS20 */
1624
0
  } else if (bss) {
1625
0
    wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
1626
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1627
0
      "WPA: ssid proto=0x%x pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1628
0
      ssid->proto, ssid->pairwise_cipher, ssid->group_cipher,
1629
0
      ssid->key_mgmt);
1630
0
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: BSS " MACSTR " ssid='%s'%s%s%s",
1631
0
      MAC2STR(bss->bssid),
1632
0
      wpa_ssid_txt(bss->ssid, bss->ssid_len),
1633
0
      bss_wpa ? " WPA" : "",
1634
0
      bss_rsn ? " RSN" : "",
1635
0
      bss_osen ? " OSEN" : "");
1636
0
    if (bss_rsn) {
1637
0
      wpa_hexdump(MSG_DEBUG, "RSN", bss_rsn, 2 + bss_rsn[1]);
1638
0
      if (wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie)) {
1639
0
        wpa_dbg(wpa_s, MSG_DEBUG,
1640
0
          "Could not parse RSN element");
1641
0
      } else {
1642
0
        wpa_dbg(wpa_s, MSG_DEBUG,
1643
0
          "RSN: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1644
0
          ie.pairwise_cipher, ie.group_cipher,
1645
0
          ie.key_mgmt);
1646
0
      }
1647
0
    }
1648
0
    if (bss_wpa) {
1649
0
      wpa_hexdump(MSG_DEBUG, "WPA", bss_wpa, 2 + bss_wpa[1]);
1650
0
      if (wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie)) {
1651
0
        wpa_dbg(wpa_s, MSG_DEBUG,
1652
0
          "Could not parse WPA element");
1653
0
      } else {
1654
0
        wpa_dbg(wpa_s, MSG_DEBUG,
1655
0
          "WPA: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1656
0
          ie.pairwise_cipher, ie.group_cipher,
1657
0
          ie.key_mgmt);
1658
0
      }
1659
0
    }
1660
0
    return -1;
1661
0
  } else {
1662
0
    if (ssid->proto & WPA_PROTO_OSEN)
1663
0
      proto = WPA_PROTO_OSEN;
1664
0
    else if (ssid->proto & WPA_PROTO_RSN)
1665
0
      proto = WPA_PROTO_RSN;
1666
0
    else
1667
0
      proto = WPA_PROTO_WPA;
1668
0
    if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1669
0
      os_memset(&ie, 0, sizeof(ie));
1670
0
      ie.group_cipher = ssid->group_cipher;
1671
0
      ie.pairwise_cipher = ssid->pairwise_cipher;
1672
0
      ie.key_mgmt = ssid->key_mgmt;
1673
0
      ie.mgmt_group_cipher = 0;
1674
0
      if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1675
0
        if (ssid->group_mgmt_cipher &
1676
0
            WPA_CIPHER_BIP_GMAC_256)
1677
0
          ie.mgmt_group_cipher =
1678
0
            WPA_CIPHER_BIP_GMAC_256;
1679
0
        else if (ssid->group_mgmt_cipher &
1680
0
           WPA_CIPHER_BIP_CMAC_256)
1681
0
          ie.mgmt_group_cipher =
1682
0
            WPA_CIPHER_BIP_CMAC_256;
1683
0
        else if (ssid->group_mgmt_cipher &
1684
0
           WPA_CIPHER_BIP_GMAC_128)
1685
0
          ie.mgmt_group_cipher =
1686
0
            WPA_CIPHER_BIP_GMAC_128;
1687
0
        else
1688
0
          ie.mgmt_group_cipher =
1689
0
            WPA_CIPHER_AES_128_CMAC;
1690
0
      }
1691
#ifdef CONFIG_OWE
1692
      if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
1693
          !ssid->owe_only &&
1694
          !bss_wpa && !bss_rsn && !bss_osen) {
1695
        wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1696
        wpa_s->wpa_proto = 0;
1697
        *wpa_ie_len = 0;
1698
        return 0;
1699
      }
1700
#endif /* CONFIG_OWE */
1701
0
      wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1702
0
        "based on configuration");
1703
0
    } else
1704
0
      proto = ie.proto;
1705
0
  }
1706
1707
0
  wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1708
0
    "pairwise %d key_mgmt %d proto %d",
1709
0
    ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1710
0
  if (ssid->ieee80211w) {
1711
0
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1712
0
      ie.mgmt_group_cipher);
1713
0
  }
1714
1715
0
  wpa_s->wpa_proto = proto;
1716
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1717
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
1718
0
       !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
1719
1720
0
  if (bss || !wpa_s->ap_ies_from_associnfo) {
1721
0
    if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1722
0
           bss_wpa ? 2 + bss_wpa[1] : 0) ||
1723
0
        wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1724
0
           bss_rsn ? 2 + bss_rsn[1] : 0) ||
1725
0
        wpa_sm_set_ap_rsnxe(wpa_s->wpa, bss_rsnx,
1726
0
          bss_rsnx ? 2 + bss_rsnx[1] : 0))
1727
0
      return -1;
1728
0
  }
1729
1730
#ifdef CONFIG_NO_WPA
1731
  wpa_s->group_cipher = WPA_CIPHER_NONE;
1732
  wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1733
#else /* CONFIG_NO_WPA */
1734
0
  sel = ie.group_cipher & ssid->group_cipher;
1735
0
  wpa_dbg(wpa_s, MSG_DEBUG,
1736
0
    "WPA: AP group 0x%x network profile group 0x%x; available group 0x%x",
1737
0
    ie.group_cipher, ssid->group_cipher, sel);
1738
0
  wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1739
0
  if (wpa_s->group_cipher < 0) {
1740
0
    wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1741
0
      "cipher");
1742
0
    return -1;
1743
0
  }
1744
0
  wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1745
0
    wpa_cipher_txt(wpa_s->group_cipher));
1746
1747
0
  sel = ie.pairwise_cipher & ssid->pairwise_cipher;
1748
0
  wpa_dbg(wpa_s, MSG_DEBUG,
1749
0
    "WPA: AP pairwise 0x%x network profile pairwise 0x%x; available pairwise 0x%x",
1750
0
    ie.pairwise_cipher, ssid->pairwise_cipher, sel);
1751
0
  wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1752
0
  if (wpa_s->pairwise_cipher < 0) {
1753
0
    wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1754
0
      "cipher");
1755
0
    return -1;
1756
0
  }
1757
0
  wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1758
0
    wpa_cipher_txt(wpa_s->pairwise_cipher));
1759
0
#endif /* CONFIG_NO_WPA */
1760
1761
0
  sel = ie.key_mgmt & ssid->key_mgmt;
1762
#ifdef CONFIG_SAE
1763
  if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) ||
1764
      wpas_is_sae_avoided(wpa_s, ssid, &ie))
1765
    sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_SAE_EXT_KEY |
1766
       WPA_KEY_MGMT_FT_SAE | WPA_KEY_MGMT_FT_SAE_EXT_KEY);
1767
#endif /* CONFIG_SAE */
1768
#ifdef CONFIG_IEEE80211R
1769
  if (!(wpa_s->drv_flags & (WPA_DRIVER_FLAGS_SME |
1770
          WPA_DRIVER_FLAGS_UPDATE_FT_IES)))
1771
    sel &= ~WPA_KEY_MGMT_FT;
1772
#endif /* CONFIG_IEEE80211R */
1773
0
  wpa_dbg(wpa_s, MSG_DEBUG,
1774
0
    "WPA: AP key_mgmt 0x%x network profile key_mgmt 0x%x; available key_mgmt 0x%x",
1775
0
    ie.key_mgmt, ssid->key_mgmt, sel);
1776
0
  if (0) {
1777
#ifdef CONFIG_IEEE80211R
1778
#ifdef CONFIG_SHA384
1779
  } else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) &&
1780
       os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
1781
    wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
1782
    wpa_dbg(wpa_s, MSG_DEBUG,
1783
      "WPA: using KEY_MGMT FT/802.1X-SHA384");
1784
    if (!ssid->ft_eap_pmksa_caching &&
1785
        pmksa_cache_get_current(wpa_s->wpa)) {
1786
      /* PMKSA caching with FT may have interoperability
1787
       * issues, so disable that case by default for now. */
1788
      wpa_dbg(wpa_s, MSG_DEBUG,
1789
        "WPA: Disable PMKSA caching for FT/802.1X connection");
1790
      pmksa_cache_clear_current(wpa_s->wpa);
1791
    }
1792
#endif /* CONFIG_SHA384 */
1793
#endif /* CONFIG_IEEE80211R */
1794
#ifdef CONFIG_SUITEB192
1795
  } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1796
    wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
1797
    wpa_dbg(wpa_s, MSG_DEBUG,
1798
      "WPA: using KEY_MGMT 802.1X with Suite B (192-bit)");
1799
#endif /* CONFIG_SUITEB192 */
1800
#ifdef CONFIG_SUITEB
1801
  } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1802
    wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
1803
    wpa_dbg(wpa_s, MSG_DEBUG,
1804
      "WPA: using KEY_MGMT 802.1X with Suite B");
1805
#endif /* CONFIG_SUITEB */
1806
#ifdef CONFIG_FILS
1807
#ifdef CONFIG_IEEE80211R
1808
  } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA384) {
1809
    wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
1810
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA384");
1811
#endif /* CONFIG_IEEE80211R */
1812
  } else if (sel & WPA_KEY_MGMT_FILS_SHA384) {
1813
    wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
1814
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA384");
1815
#ifdef CONFIG_IEEE80211R
1816
  } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA256) {
1817
    wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
1818
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA256");
1819
#endif /* CONFIG_IEEE80211R */
1820
  } else if (sel & WPA_KEY_MGMT_FILS_SHA256) {
1821
    wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
1822
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA256");
1823
#endif /* CONFIG_FILS */
1824
#ifdef CONFIG_IEEE80211R
1825
  } else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X) &&
1826
       os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
1827
    wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1828
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
1829
    if (!ssid->ft_eap_pmksa_caching &&
1830
        pmksa_cache_get_current(wpa_s->wpa)) {
1831
      /* PMKSA caching with FT may have interoperability
1832
       * issues, so disable that case by default for now. */
1833
      wpa_dbg(wpa_s, MSG_DEBUG,
1834
        "WPA: Disable PMKSA caching for FT/802.1X connection");
1835
      pmksa_cache_clear_current(wpa_s->wpa);
1836
    }
1837
#endif /* CONFIG_IEEE80211R */
1838
#ifdef CONFIG_DPP
1839
  } else if (sel & WPA_KEY_MGMT_DPP) {
1840
    wpa_s->key_mgmt = WPA_KEY_MGMT_DPP;
1841
    wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT DPP");
1842
#endif /* CONFIG_DPP */
1843
#ifdef CONFIG_SAE
1844
  } else if (sel & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
1845
    wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE_EXT_KEY;
1846
    wpa_dbg(wpa_s, MSG_DEBUG,
1847
      "RSN: using KEY_MGMT FT/SAE (ext key)");
1848
  } else if (sel & WPA_KEY_MGMT_SAE_EXT_KEY) {
1849
    wpa_s->key_mgmt = WPA_KEY_MGMT_SAE_EXT_KEY;
1850
    wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE (ext key)");
1851
  } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1852
    wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1853
    wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1854
  } else if (sel & WPA_KEY_MGMT_SAE) {
1855
    wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1856
    wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1857
#endif /* CONFIG_SAE */
1858
#ifdef CONFIG_IEEE80211R
1859
  } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1860
    wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1861
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1862
#endif /* CONFIG_IEEE80211R */
1863
0
  } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1864
0
    wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1865
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1866
0
      "WPA: using KEY_MGMT 802.1X with SHA256");
1867
0
  } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1868
0
    wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1869
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1870
0
      "WPA: using KEY_MGMT PSK with SHA256");
1871
0
  } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1872
0
    wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1873
0
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1874
0
  } else if (sel & WPA_KEY_MGMT_PSK) {
1875
0
    wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1876
0
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1877
0
  } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1878
0
    wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1879
0
    wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1880
0
#ifdef CONFIG_HS20
1881
0
  } else if (sel & WPA_KEY_MGMT_OSEN) {
1882
0
    wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1883
0
    wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1884
0
#endif /* CONFIG_HS20 */
1885
#ifdef CONFIG_OWE
1886
  } else if (sel & WPA_KEY_MGMT_OWE) {
1887
    wpa_s->key_mgmt = WPA_KEY_MGMT_OWE;
1888
    wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT OWE");
1889
#endif /* CONFIG_OWE */
1890
0
  } else {
1891
0
    wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1892
0
      "authenticated key management type");
1893
0
    return -1;
1894
0
  }
1895
1896
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1897
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1898
0
       wpa_s->pairwise_cipher);
1899
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1900
1901
0
  if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
1902
0
      (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED ||
1903
0
       (bss && is_6ghz_freq(bss->freq)))) {
1904
0
    wpa_msg(wpa_s, MSG_INFO,
1905
0
      "RSN: Management frame protection required but the selected AP does not enable it");
1906
0
    return -1;
1907
0
  }
1908
1909
0
  wpas_set_mgmt_group_cipher(wpa_s, ssid, &ie);
1910
#ifdef CONFIG_OCV
1911
  if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
1912
      (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV))
1913
    wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCV, ssid->ocv);
1914
#endif /* CONFIG_OCV */
1915
0
  sae_pwe = wpa_s->conf->sae_pwe;
1916
0
  if ((ssid->sae_password_id ||
1917
0
       wpa_key_mgmt_sae_ext_key(wpa_s->key_mgmt)) &&
1918
0
      sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
1919
0
    sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
1920
0
  if (bss && is_6ghz_freq(bss->freq) &&
1921
0
      sae_pwe == SAE_PWE_HUNT_AND_PECK) {
1922
0
    wpa_dbg(wpa_s, MSG_DEBUG,
1923
0
      "RSN: Enable SAE hash-to-element mode for 6 GHz BSS");
1924
0
    sae_pwe = SAE_PWE_BOTH;
1925
0
  }
1926
0
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SAE_PWE, sae_pwe);
1927
#ifdef CONFIG_SAE_PK
1928
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SAE_PK,
1929
       wpa_key_mgmt_sae(ssid->key_mgmt) &&
1930
       ssid->sae_pk != SAE_PK_MODE_DISABLED &&
1931
       ((ssid->sae_password &&
1932
         sae_pk_valid_password(ssid->sae_password)) ||
1933
        (!ssid->sae_password && ssid->passphrase &&
1934
         sae_pk_valid_password(ssid->passphrase))));
1935
#endif /* CONFIG_SAE_PK */
1936
0
  if (bss && is_6ghz_freq(bss->freq) &&
1937
0
      wpas_get_ssid_pmf(wpa_s, ssid) != MGMT_FRAME_PROTECTION_REQUIRED) {
1938
0
    wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Force MFPR=1 on 6 GHz");
1939
0
    wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1940
0
         MGMT_FRAME_PROTECTION_REQUIRED);
1941
0
  }
1942
#ifdef CONFIG_TESTING_OPTIONS
1943
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_FT_RSNXE_USED,
1944
       wpa_s->ft_rsnxe_used);
1945
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL,
1946
       wpa_s->oci_freq_override_eapol);
1947
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL_G2,
1948
       wpa_s->oci_freq_override_eapol_g2);
1949
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FT_ASSOC,
1950
       wpa_s->oci_freq_override_ft_assoc);
1951
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FILS_ASSOC,
1952
       wpa_s->oci_freq_override_fils_assoc);
1953
  wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DISABLE_EAPOL_G2_TX,
1954
       wpa_s->disable_eapol_g2_tx);
1955
#endif /* CONFIG_TESTING_OPTIONS */
1956
1957
  /* Extended Key ID is only supported in infrastructure BSS so far */
1958
0
  if (ssid->mode == WPAS_MODE_INFRA && wpa_s->conf->extended_key_id &&
1959
0
      (ssid->proto & WPA_PROTO_RSN) &&
1960
0
      ssid->pairwise_cipher & (WPA_CIPHER_CCMP | WPA_CIPHER_CCMP_256 |
1961
0
             WPA_CIPHER_GCMP | WPA_CIPHER_GCMP_256) &&
1962
0
      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_EXTENDED_KEY_ID)) {
1963
0
    int use_ext_key_id = 0;
1964
1965
0
    wpa_msg(wpa_s, MSG_DEBUG,
1966
0
      "WPA: Enable Extended Key ID support");
1967
0
    wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_EXT_KEY_ID,
1968
0
         wpa_s->conf->extended_key_id);
1969
0
    if (bss_rsn &&
1970
0
        wpa_s->conf->extended_key_id &&
1971
0
        wpa_s->pairwise_cipher != WPA_CIPHER_TKIP &&
1972
0
        (ie.capabilities & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST))
1973
0
      use_ext_key_id = 1;
1974
0
    wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_USE_EXT_KEY_ID,
1975
0
         use_ext_key_id);
1976
0
  } else {
1977
0
    wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_EXT_KEY_ID, 0);
1978
0
    wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_USE_EXT_KEY_ID, 0);
1979
0
  }
1980
1981
0
  if (!skip_default_rsne) {
1982
0
    if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie,
1983
0
                wpa_ie_len)) {
1984
0
      wpa_msg(wpa_s, MSG_WARNING,
1985
0
        "RSN: Failed to generate RSNE/WPA IE");
1986
0
      return -1;
1987
0
    }
1988
1989
0
    wpa_s->rsnxe_len = sizeof(wpa_s->rsnxe);
1990
0
    if (wpa_sm_set_assoc_rsnxe_default(wpa_s->wpa, wpa_s->rsnxe,
1991
0
               &wpa_s->rsnxe_len)) {
1992
0
      wpa_msg(wpa_s, MSG_WARNING,
1993
0
        "RSN: Failed to generate RSNXE");
1994
0
      return -1;
1995
0
    }
1996
0
  }
1997
1998
0
  if (0) {
1999
#ifdef CONFIG_DPP
2000
  } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_DPP) {
2001
    /* Use PMK from DPP network introduction (PMKSA entry) */
2002
    wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
2003
#ifdef CONFIG_DPP2
2004
    wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DPP_PFS, ssid->dpp_pfs);
2005
#endif /* CONFIG_DPP2 */
2006
#endif /* CONFIG_DPP */
2007
0
  } else if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
2008
0
    int psk_set = 0;
2009
2010
0
    if (wpa_key_mgmt_wpa_psk_no_sae(ssid->key_mgmt)) {
2011
0
      u8 psk[PMK_LEN];
2012
2013
0
      if (wpa_supplicant_get_psk(wpa_s, bss, ssid,
2014
0
               psk) == 0) {
2015
0
        wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
2016
0
                 NULL);
2017
0
        psk_set = 1;
2018
0
      }
2019
0
      forced_memzero(psk, sizeof(psk));
2020
0
    }
2021
2022
0
    if (wpa_key_mgmt_sae(ssid->key_mgmt) &&
2023
0
        (ssid->sae_password || ssid->passphrase || ssid->ext_psk))
2024
0
      psk_set = 1;
2025
2026
0
    if (!psk_set) {
2027
0
      wpa_msg(wpa_s, MSG_INFO,
2028
0
        "No PSK available for association");
2029
0
      wpas_auth_failed(wpa_s, "NO_PSK_AVAILABLE", NULL);
2030
0
      return -1;
2031
0
    }
2032
#ifdef CONFIG_OWE
2033
  } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE) {
2034
    /* OWE Diffie-Hellman exchange in (Re)Association
2035
     * Request/Response frames set the PMK, so do not override it
2036
     * here. */
2037
#endif /* CONFIG_OWE */
2038
0
  } else
2039
0
    wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
2040
2041
0
  if (ssid->mode != WPAS_MODE_IBSS &&
2042
0
      !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED) &&
2043
0
      (ssid->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_NEVER ||
2044
0
       (ssid->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_LOCAL_OK &&
2045
0
        !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAFE_PTK0_REKEYS)))) {
2046
0
    wpa_msg(wpa_s, MSG_INFO,
2047
0
      "Disable PTK0 rekey support - replaced with reconnect");
2048
0
    wpa_s->deny_ptk0_rekey = 1;
2049
0
    wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DENY_PTK0_REKEY, 1);
2050
0
  } else {
2051
0
    wpa_s->deny_ptk0_rekey = 0;
2052
0
    wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DENY_PTK0_REKEY, 0);
2053
0
  }
2054
2055
0
  if (wpa_key_mgmt_cross_akm(wpa_s->key_mgmt) &&
2056
0
      !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2057
0
    wpas_update_allowed_key_mgmt(wpa_s, ssid);
2058
2059
0
  return 0;
2060
0
}
2061
2062
2063
static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
2064
0
{
2065
0
  bool scs = true, mscs = true;
2066
2067
0
  *pos = 0x00;
2068
2069
0
  switch (idx) {
2070
0
  case 0: /* Bits 0-7 */
2071
0
    break;
2072
0
  case 1: /* Bits 8-15 */
2073
0
    if (wpa_s->conf->coloc_intf_reporting) {
2074
      /* Bit 13 - Collocated Interference Reporting */
2075
0
      *pos |= 0x20;
2076
0
    }
2077
0
    break;
2078
0
  case 2: /* Bits 16-23 */
2079
0
#ifdef CONFIG_WNM
2080
0
    *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
2081
0
    if (!wpa_s->disable_mbo_oce && !wpa_s->conf->disable_btm)
2082
0
      *pos |= 0x08; /* Bit 19 - BSS Transition */
2083
0
#endif /* CONFIG_WNM */
2084
0
    break;
2085
0
  case 3: /* Bits 24-31 */
2086
0
#ifdef CONFIG_WNM
2087
0
    *pos |= 0x02; /* Bit 25 - SSID List */
2088
0
#endif /* CONFIG_WNM */
2089
0
#ifdef CONFIG_INTERWORKING
2090
0
    if (wpa_s->conf->interworking)
2091
0
      *pos |= 0x80; /* Bit 31 - Interworking */
2092
0
#endif /* CONFIG_INTERWORKING */
2093
0
    break;
2094
0
  case 4: /* Bits 32-39 */
2095
0
#ifdef CONFIG_INTERWORKING
2096
0
    if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_QOS_MAPPING)
2097
0
      *pos |= 0x01; /* Bit 32 - QoS Map */
2098
0
#endif /* CONFIG_INTERWORKING */
2099
0
    break;
2100
0
  case 5: /* Bits 40-47 */
2101
0
#ifdef CONFIG_HS20
2102
0
    if (wpa_s->conf->hs20)
2103
0
      *pos |= 0x40; /* Bit 46 - WNM-Notification */
2104
0
#endif /* CONFIG_HS20 */
2105
#ifdef CONFIG_MBO
2106
    *pos |= 0x40; /* Bit 46 - WNM-Notification */
2107
#endif /* CONFIG_MBO */
2108
0
    break;
2109
0
  case 6: /* Bits 48-55 */
2110
#ifdef CONFIG_TESTING_OPTIONS
2111
    if (wpa_s->disable_scs_support)
2112
      scs = false;
2113
#endif /* CONFIG_TESTING_OPTIONS */
2114
0
    if (scs)
2115
0
      *pos |= 0x40; /* Bit 54 - SCS */
2116
0
    break;
2117
0
  case 7: /* Bits 56-63 */
2118
0
    break;
2119
0
  case 8: /* Bits 64-71 */
2120
0
    if (wpa_s->conf->ftm_responder)
2121
0
      *pos |= 0x40; /* Bit 70 - FTM responder */
2122
0
    if (wpa_s->conf->ftm_initiator)
2123
0
      *pos |= 0x80; /* Bit 71 - FTM initiator */
2124
0
    break;
2125
0
  case 9: /* Bits 72-79 */
2126
#ifdef CONFIG_FILS
2127
    if (!wpa_s->disable_fils)
2128
      *pos |= 0x01;
2129
#endif /* CONFIG_FILS */
2130
0
    break;
2131
0
  case 10: /* Bits 80-87 */
2132
#ifdef CONFIG_TESTING_OPTIONS
2133
    if (wpa_s->disable_mscs_support)
2134
      mscs = false;
2135
#endif /* CONFIG_TESTING_OPTIONS */
2136
0
    if (mscs)
2137
0
      *pos |= 0x20; /* Bit 85 - Mirrored SCS */
2138
0
    break;
2139
0
  }
2140
0
}
2141
2142
2143
int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf, size_t buflen)
2144
0
{
2145
0
  u8 *pos = buf;
2146
0
  u8 len = 11, i;
2147
2148
0
  if (len < wpa_s->extended_capa_len)
2149
0
    len = wpa_s->extended_capa_len;
2150
0
  if (buflen < (size_t) len + 2) {
2151
0
    wpa_printf(MSG_INFO,
2152
0
         "Not enough room for building extended capabilities element");
2153
0
    return -1;
2154
0
  }
2155
2156
0
  *pos++ = WLAN_EID_EXT_CAPAB;
2157
0
  *pos++ = len;
2158
0
  for (i = 0; i < len; i++, pos++) {
2159
0
    wpas_ext_capab_byte(wpa_s, pos, i);
2160
2161
0
    if (i < wpa_s->extended_capa_len) {
2162
0
      *pos &= ~wpa_s->extended_capa_mask[i];
2163
0
      *pos |= wpa_s->extended_capa[i];
2164
0
    }
2165
0
  }
2166
2167
0
  while (len > 0 && buf[1 + len] == 0) {
2168
0
    len--;
2169
0
    buf[1] = len;
2170
0
  }
2171
0
  if (len == 0)
2172
0
    return 0;
2173
2174
0
  return 2 + len;
2175
0
}
2176
2177
2178
static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
2179
        struct wpa_bss *test_bss)
2180
0
{
2181
0
  struct wpa_bss *bss;
2182
2183
0
  dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2184
0
    if (bss == test_bss)
2185
0
      return 1;
2186
0
  }
2187
2188
0
  return 0;
2189
0
}
2190
2191
2192
static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
2193
         struct wpa_ssid *test_ssid)
2194
0
{
2195
0
  struct wpa_ssid *ssid;
2196
2197
0
  for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2198
0
    if (ssid == test_ssid)
2199
0
      return 1;
2200
0
  }
2201
2202
0
  return 0;
2203
0
}
2204
2205
2206
int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
2207
      struct wpa_ssid *test_ssid)
2208
0
{
2209
0
  if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
2210
0
    return 0;
2211
2212
0
  return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
2213
0
}
2214
2215
2216
void wpas_connect_work_free(struct wpa_connect_work *cwork)
2217
0
{
2218
0
  if (cwork == NULL)
2219
0
    return;
2220
0
  os_free(cwork);
2221
0
}
2222
2223
2224
void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
2225
0
{
2226
0
  struct wpa_connect_work *cwork;
2227
0
  struct wpa_radio_work *work = wpa_s->connect_work;
2228
2229
0
  if (!work)
2230
0
    return;
2231
2232
0
  wpa_s->connect_work = NULL;
2233
0
  cwork = work->ctx;
2234
0
  work->ctx = NULL;
2235
0
  wpas_connect_work_free(cwork);
2236
0
  radio_work_done(work);
2237
0
}
2238
2239
2240
int wpas_update_random_addr(struct wpa_supplicant *wpa_s,
2241
          enum wpas_mac_addr_style style,
2242
          struct wpa_ssid *ssid)
2243
0
{
2244
0
  struct os_reltime now;
2245
0
  u8 addr[ETH_ALEN];
2246
2247
0
  os_get_reltime(&now);
2248
  /* Random addresses are valid within a given ESS so check
2249
   * expiration/value only when continuing to use the same ESS. */
2250
0
  if (wpa_s->last_mac_addr_style == style && wpa_s->reassoc_same_ess) {
2251
0
    if (style == WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS) {
2252
      /* Pregenerated addresses do not expire but their value
2253
       * might have changed, so let's check that. */
2254
0
      if (os_memcmp(wpa_s->own_addr, ssid->mac_value,
2255
0
              ETH_ALEN) == 0)
2256
0
        return 0;
2257
0
    } else if ((wpa_s->last_mac_addr_change.sec != 0 ||
2258
0
          wpa_s->last_mac_addr_change.usec != 0) &&
2259
0
         !os_reltime_expired(
2260
0
           &now,
2261
0
           &wpa_s->last_mac_addr_change,
2262
0
           wpa_s->conf->rand_addr_lifetime)) {
2263
0
      wpa_msg(wpa_s, MSG_DEBUG,
2264
0
        "Previously selected random MAC address has not yet expired");
2265
0
      return 0;
2266
0
    }
2267
0
  }
2268
2269
0
  switch (style) {
2270
0
  case WPAS_MAC_ADDR_STYLE_RANDOM:
2271
0
    if (random_mac_addr(addr) < 0)
2272
0
      return -1;
2273
0
    break;
2274
0
  case WPAS_MAC_ADDR_STYLE_RANDOM_SAME_OUI:
2275
0
    os_memcpy(addr, wpa_s->perm_addr, ETH_ALEN);
2276
0
    if (random_mac_addr_keep_oui(addr) < 0)
2277
0
      return -1;
2278
0
    break;
2279
0
  case WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS:
2280
0
    if (!ssid) {
2281
0
      wpa_msg(wpa_s, MSG_INFO,
2282
0
        "Invalid 'ssid' for address policy 3");
2283
0
      return -1;
2284
0
    }
2285
0
    os_memcpy(addr, ssid->mac_value, ETH_ALEN);
2286
0
    break;
2287
0
  default:
2288
0
    return -1;
2289
0
  }
2290
2291
0
  if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
2292
0
    wpa_msg(wpa_s, MSG_INFO,
2293
0
      "Failed to set random MAC address");
2294
0
    return -1;
2295
0
  }
2296
2297
0
  os_get_reltime(&wpa_s->last_mac_addr_change);
2298
0
  wpa_s->mac_addr_changed = 1;
2299
0
  wpa_s->last_mac_addr_style = style;
2300
2301
0
  if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
2302
0
    wpa_msg(wpa_s, MSG_INFO,
2303
0
      "Could not update MAC address information");
2304
0
    return -1;
2305
0
  }
2306
2307
0
  wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
2308
0
    MAC2STR(addr));
2309
2310
0
  return 1;
2311
0
}
2312
2313
2314
int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
2315
0
{
2316
0
  if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
2317
0
      !wpa_s->conf->preassoc_mac_addr)
2318
0
    return 0;
2319
2320
0
  return wpas_update_random_addr(wpa_s, wpa_s->conf->preassoc_mac_addr,
2321
0
               NULL);
2322
0
}
2323
2324
2325
void wpa_s_setup_sae_pt(struct wpa_config *conf, struct wpa_ssid *ssid,
2326
      bool force)
2327
0
{
2328
#ifdef CONFIG_SAE
2329
  int *groups = conf->sae_groups;
2330
  int default_groups[] = { 19, 20, 21, 0 };
2331
  const char *password;
2332
2333
  if (!groups || groups[0] <= 0)
2334
    groups = default_groups;
2335
2336
  password = ssid->sae_password;
2337
  if (!password)
2338
    password = ssid->passphrase;
2339
2340
  if (!password ||
2341
      (conf->sae_pwe == SAE_PWE_HUNT_AND_PECK && !ssid->sae_password_id &&
2342
       !wpa_key_mgmt_sae_ext_key(ssid->key_mgmt) &&
2343
       !force &&
2344
       !sae_pk_valid_password(password)) ||
2345
      conf->sae_pwe == SAE_PWE_FORCE_HUNT_AND_PECK) {
2346
    /* PT derivation not needed */
2347
    sae_deinit_pt(ssid->pt);
2348
    ssid->pt = NULL;
2349
    return;
2350
  }
2351
2352
  if (ssid->pt)
2353
    return; /* PT already derived */
2354
  ssid->pt = sae_derive_pt(groups, ssid->ssid, ssid->ssid_len,
2355
         (const u8 *) password, os_strlen(password),
2356
         ssid->sae_password_id);
2357
#endif /* CONFIG_SAE */
2358
0
}
2359
2360
2361
static void wpa_s_clear_sae_rejected(struct wpa_supplicant *wpa_s)
2362
0
{
2363
#if defined(CONFIG_SAE) && defined(CONFIG_SME)
2364
  os_free(wpa_s->sme.sae_rejected_groups);
2365
  wpa_s->sme.sae_rejected_groups = NULL;
2366
#ifdef CONFIG_TESTING_OPTIONS
2367
  if (wpa_s->extra_sae_rejected_groups) {
2368
    int i, *groups = wpa_s->extra_sae_rejected_groups;
2369
2370
    for (i = 0; groups[i]; i++) {
2371
      wpa_printf(MSG_DEBUG,
2372
           "TESTING: Indicate rejection of an extra SAE group %d",
2373
           groups[i]);
2374
      int_array_add_unique(&wpa_s->sme.sae_rejected_groups,
2375
               groups[i]);
2376
    }
2377
  }
2378
#endif /* CONFIG_TESTING_OPTIONS */
2379
#endif /* CONFIG_SAE && CONFIG_SME */
2380
0
}
2381
2382
2383
int wpas_restore_permanent_mac_addr(struct wpa_supplicant *wpa_s)
2384
0
{
2385
0
  if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
2386
0
    wpa_msg(wpa_s, MSG_INFO,
2387
0
      "Could not restore permanent MAC address");
2388
0
    return -1;
2389
0
  }
2390
0
  wpa_s->mac_addr_changed = 0;
2391
0
  if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
2392
0
    wpa_msg(wpa_s, MSG_INFO,
2393
0
      "Could not update MAC address information");
2394
0
    return -1;
2395
0
  }
2396
0
  wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
2397
0
  return 0;
2398
0
}
2399
2400
2401
static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
2402
2403
/**
2404
 * wpa_supplicant_associate - Request association
2405
 * @wpa_s: Pointer to wpa_supplicant data
2406
 * @bss: Scan results for the selected BSS, or %NULL if not available
2407
 * @ssid: Configuration data for the selected network
2408
 *
2409
 * This function is used to request %wpa_supplicant to associate with a BSS.
2410
 */
2411
void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
2412
            struct wpa_bss *bss, struct wpa_ssid *ssid)
2413
0
{
2414
0
  struct wpa_connect_work *cwork;
2415
0
  enum wpas_mac_addr_style rand_style;
2416
2417
0
  wpa_s->own_disconnect_req = 0;
2418
0
  wpa_s->own_reconnect_req = 0;
2419
2420
  /*
2421
   * If we are starting a new connection, any previously pending EAPOL
2422
   * RX cannot be valid anymore.
2423
   */
2424
0
  wpabuf_free(wpa_s->pending_eapol_rx);
2425
0
  wpa_s->pending_eapol_rx = NULL;
2426
2427
0
  if (ssid->mac_addr == WPAS_MAC_ADDR_STYLE_NOT_SET)
2428
0
    rand_style = wpa_s->conf->mac_addr;
2429
0
  else
2430
0
    rand_style = ssid->mac_addr;
2431
2432
0
  wpa_s->eapol_failed = 0;
2433
0
  wpa_s->multi_ap_ie = 0;
2434
0
  wmm_ac_clear_saved_tspecs(wpa_s);
2435
0
  wpa_s->reassoc_same_bss = 0;
2436
0
  wpa_s->reassoc_same_ess = 0;
2437
#ifdef CONFIG_TESTING_OPTIONS
2438
  wpa_s->testing_resend_assoc = 0;
2439
#endif /* CONFIG_TESTING_OPTIONS */
2440
2441
0
  if (wpa_s->last_ssid == ssid) {
2442
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
2443
0
    wpa_s->reassoc_same_ess = 1;
2444
0
    if (wpa_s->current_bss && wpa_s->current_bss == bss) {
2445
0
      wmm_ac_save_tspecs(wpa_s);
2446
0
      wpa_s->reassoc_same_bss = 1;
2447
0
    } else if (wpa_s->current_bss && wpa_s->current_bss != bss) {
2448
0
      os_get_reltime(&wpa_s->roam_start);
2449
0
    }
2450
0
  } else {
2451
#ifdef CONFIG_SAE
2452
    wpa_s_clear_sae_rejected(wpa_s);
2453
#endif /* CONFIG_SAE */
2454
0
  }
2455
#ifdef CONFIG_SAE
2456
  wpa_s_setup_sae_pt(wpa_s->conf, ssid, false);
2457
#endif /* CONFIG_SAE */
2458
2459
0
  if (rand_style > WPAS_MAC_ADDR_STYLE_PERMANENT) {
2460
0
    int status = wpas_update_random_addr(wpa_s, rand_style, ssid);
2461
2462
0
    if (status < 0)
2463
0
      return;
2464
0
    if (rand_style != WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS &&
2465
0
        status > 0) /* MAC changed */
2466
0
      wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2467
0
  } else if (rand_style == WPAS_MAC_ADDR_STYLE_PERMANENT &&
2468
0
       wpa_s->mac_addr_changed) {
2469
0
    if (wpas_restore_permanent_mac_addr(wpa_s) < 0)
2470
0
      return;
2471
0
  }
2472
0
  wpa_s->last_ssid = ssid;
2473
2474
#ifdef CONFIG_IBSS_RSN
2475
  ibss_rsn_deinit(wpa_s->ibss_rsn);
2476
  wpa_s->ibss_rsn = NULL;
2477
#else /* CONFIG_IBSS_RSN */
2478
0
  if (ssid->mode == WPAS_MODE_IBSS &&
2479
0
      !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPA_NONE))) {
2480
0
    wpa_msg(wpa_s, MSG_INFO,
2481
0
      "IBSS RSN not supported in the build");
2482
0
    return;
2483
0
  }
2484
0
#endif /* CONFIG_IBSS_RSN */
2485
2486
0
  if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
2487
0
      ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
2488
#ifdef CONFIG_AP
2489
    if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
2490
      wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
2491
        "mode");
2492
      return;
2493
    }
2494
    if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
2495
      wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2496
      if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
2497
        wpas_p2p_ap_setup_failed(wpa_s);
2498
      return;
2499
    }
2500
    wpa_s->current_bss = bss;
2501
#else /* CONFIG_AP */
2502
0
    wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
2503
0
      "the build");
2504
0
#endif /* CONFIG_AP */
2505
0
    return;
2506
0
  }
2507
2508
0
  if (ssid->mode == WPAS_MODE_MESH) {
2509
#ifdef CONFIG_MESH
2510
    if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MESH)) {
2511
      wpa_msg(wpa_s, MSG_INFO,
2512
        "Driver does not support mesh mode");
2513
      return;
2514
    }
2515
    if (bss)
2516
      ssid->frequency = bss->freq;
2517
    if (wpa_supplicant_join_mesh(wpa_s, ssid) < 0) {
2518
      wpa_msg(wpa_s, MSG_ERROR, "Could not join mesh");
2519
      return;
2520
    }
2521
    wpa_s->current_bss = bss;
2522
#else /* CONFIG_MESH */
2523
0
    wpa_msg(wpa_s, MSG_ERROR,
2524
0
      "mesh mode support not included in the build");
2525
0
#endif /* CONFIG_MESH */
2526
0
    return;
2527
0
  }
2528
2529
  /*
2530
   * Set WPA state machine configuration to match the selected network now
2531
   * so that the information is available before wpas_start_assoc_cb()
2532
   * gets called. This is needed at least for RSN pre-authentication where
2533
   * candidate APs are added to a list based on scan result processing
2534
   * before completion of the first association.
2535
   */
2536
0
  wpa_supplicant_rsn_supp_set_config(wpa_s, ssid);
2537
2538
#ifdef CONFIG_DPP
2539
  if (wpas_dpp_check_connect(wpa_s, ssid, bss) != 0)
2540
    return;
2541
#endif /* CONFIG_DPP */
2542
2543
#ifdef CONFIG_TDLS
2544
  if (bss)
2545
    wpa_tdls_ap_ies(wpa_s->wpa, wpa_bss_ie_ptr(bss), bss->ie_len);
2546
#endif /* CONFIG_TDLS */
2547
2548
#ifdef CONFIG_MBO
2549
  wpas_mbo_check_pmf(wpa_s, bss, ssid);
2550
#endif /* CONFIG_MBO */
2551
2552
0
  if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
2553
0
      ssid->mode == WPAS_MODE_INFRA) {
2554
0
    sme_authenticate(wpa_s, bss, ssid);
2555
0
    return;
2556
0
  }
2557
2558
0
  if (wpa_s->connect_work) {
2559
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
2560
0
    return;
2561
0
  }
2562
2563
0
  if (radio_work_pending(wpa_s, "connect")) {
2564
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
2565
0
    return;
2566
0
  }
2567
2568
#ifdef CONFIG_SME
2569
  if (ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) {
2570
    /* Clear possibly set auth_alg, if any, from last attempt. */
2571
    wpa_s->sme.auth_alg = WPA_AUTH_ALG_OPEN;
2572
  }
2573
#endif /* CONFIG_SME */
2574
2575
0
  wpas_abort_ongoing_scan(wpa_s);
2576
2577
0
  cwork = os_zalloc(sizeof(*cwork));
2578
0
  if (cwork == NULL)
2579
0
    return;
2580
2581
0
  cwork->bss = bss;
2582
0
  cwork->ssid = ssid;
2583
2584
0
  if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
2585
0
         wpas_start_assoc_cb, cwork) < 0) {
2586
0
    os_free(cwork);
2587
0
  }
2588
0
}
2589
2590
2591
static int bss_is_ibss(struct wpa_bss *bss)
2592
0
{
2593
0
  return (bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
2594
0
    IEEE80211_CAP_IBSS;
2595
0
}
2596
2597
2598
static int drv_supports_vht(struct wpa_supplicant *wpa_s,
2599
          const struct wpa_ssid *ssid)
2600
0
{
2601
0
  enum hostapd_hw_mode hw_mode;
2602
0
  struct hostapd_hw_modes *mode = NULL;
2603
0
  u8 channel;
2604
0
  int i;
2605
2606
0
  hw_mode = ieee80211_freq_to_chan(ssid->frequency, &channel);
2607
0
  if (hw_mode == NUM_HOSTAPD_MODES)
2608
0
    return 0;
2609
0
  for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
2610
0
    if (wpa_s->hw.modes[i].mode == hw_mode) {
2611
0
      mode = &wpa_s->hw.modes[i];
2612
0
      break;
2613
0
    }
2614
0
  }
2615
2616
0
  if (!mode)
2617
0
    return 0;
2618
2619
0
  return mode->vht_capab != 0;
2620
0
}
2621
2622
2623
static bool ibss_mesh_is_80mhz_avail(int channel, struct hostapd_hw_modes *mode)
2624
0
{
2625
0
  int i;
2626
2627
0
  for (i = channel; i < channel + 16; i += 4) {
2628
0
    struct hostapd_channel_data *chan;
2629
2630
0
    chan = hw_get_channel_chan(mode, i, NULL);
2631
0
    if (!chan ||
2632
0
        chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2633
0
      return false;
2634
0
  }
2635
2636
0
  return true;
2637
0
}
2638
2639
2640
static struct wpa_bss * ibss_find_existing_bss(struct wpa_supplicant *wpa_s,
2641
                 const struct wpa_ssid *ssid)
2642
0
{
2643
0
  unsigned int j;
2644
2645
0
  for (j = 0; j < wpa_s->last_scan_res_used; j++) {
2646
0
    struct wpa_bss *bss = wpa_s->last_scan_res[j];
2647
2648
0
    if (!bss_is_ibss(bss))
2649
0
      continue;
2650
2651
0
    if (ssid->ssid_len == bss->ssid_len &&
2652
0
        os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) == 0)
2653
0
      return bss;
2654
0
  }
2655
0
  return NULL;
2656
0
}
2657
2658
2659
static bool ibss_mesh_can_use_ht(struct wpa_supplicant *wpa_s,
2660
         const struct wpa_ssid *ssid,
2661
         struct hostapd_hw_modes *mode)
2662
0
{
2663
  /* For IBSS check HT_IBSS flag */
2664
0
  if (ssid->mode == WPAS_MODE_IBSS &&
2665
0
      !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_HT_IBSS))
2666
0
    return false;
2667
2668
0
  if (wpa_s->group_cipher == WPA_CIPHER_WEP40 ||
2669
0
      wpa_s->group_cipher == WPA_CIPHER_WEP104 ||
2670
0
      wpa_s->pairwise_cipher == WPA_CIPHER_TKIP) {
2671
0
    wpa_printf(MSG_DEBUG,
2672
0
         "IBSS: WEP/TKIP detected, do not try to enable HT");
2673
0
    return false;
2674
0
  }
2675
2676
0
  if (!ht_supported(mode))
2677
0
    return false;
2678
2679
#ifdef CONFIG_HT_OVERRIDES
2680
  if (ssid->disable_ht)
2681
    return false;
2682
#endif /* CONFIG_HT_OVERRIDES */
2683
2684
0
  return true;
2685
0
}
2686
2687
2688
static bool ibss_mesh_can_use_vht(struct wpa_supplicant *wpa_s,
2689
          const struct wpa_ssid *ssid,
2690
          struct hostapd_hw_modes *mode)
2691
0
{
2692
0
  if (mode->mode != HOSTAPD_MODE_IEEE80211A)
2693
0
    return false;
2694
2695
0
  if (!drv_supports_vht(wpa_s, ssid))
2696
0
    return false;
2697
2698
  /* For IBSS check VHT_IBSS flag */
2699
0
  if (ssid->mode == WPAS_MODE_IBSS &&
2700
0
      !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_VHT_IBSS))
2701
0
    return false;
2702
2703
0
  if (!vht_supported(mode))
2704
0
    return false;
2705
2706
#ifdef CONFIG_VHT_OVERRIDES
2707
  if (ssid->disable_vht)
2708
    return false;
2709
#endif /* CONFIG_VHT_OVERRIDES */
2710
2711
0
  return true;
2712
0
}
2713
2714
2715
static bool ibss_mesh_can_use_he(struct wpa_supplicant *wpa_s,
2716
         const struct wpa_ssid *ssid,
2717
         const struct hostapd_hw_modes *mode,
2718
         int ieee80211_mode)
2719
0
{
2720
#ifdef CONFIG_HE_OVERRIDES
2721
  if (ssid->disable_he)
2722
    return false;
2723
#endif /* CONFIG_HE_OVERRIDES */
2724
2725
0
  switch (mode->mode) {
2726
0
  case HOSTAPD_MODE_IEEE80211G:
2727
0
  case HOSTAPD_MODE_IEEE80211B:
2728
0
  case HOSTAPD_MODE_IEEE80211A:
2729
0
    return mode->he_capab[ieee80211_mode].he_supported;
2730
0
  default:
2731
0
    return false;
2732
0
  }
2733
0
}
2734
2735
2736
static bool ibss_mesh_can_use_eht(struct wpa_supplicant *wpa_s,
2737
          const struct wpa_ssid *ssid,
2738
          const struct hostapd_hw_modes *mode,
2739
          int ieee80211_mode)
2740
0
{
2741
0
  if (ssid->disable_eht)
2742
0
    return false;
2743
2744
0
  switch(mode->mode) {
2745
0
  case HOSTAPD_MODE_IEEE80211G:
2746
0
  case HOSTAPD_MODE_IEEE80211B:
2747
0
  case HOSTAPD_MODE_IEEE80211A:
2748
0
    return mode->eht_capab[ieee80211_mode].eht_supported;
2749
0
  default:
2750
0
    return false;
2751
0
  }
2752
0
}
2753
2754
2755
static void ibss_mesh_select_40mhz(struct wpa_supplicant *wpa_s,
2756
           const struct wpa_ssid *ssid,
2757
           struct hostapd_hw_modes *mode,
2758
           struct hostapd_freq_params *freq,
2759
0
           int obss_scan) {
2760
0
  int chan_idx;
2761
0
  struct hostapd_channel_data *pri_chan = NULL, *sec_chan = NULL;
2762
0
  int i, res;
2763
0
  unsigned int j;
2764
0
  static const int ht40plus[] = {
2765
0
    36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157, 184, 192
2766
0
  };
2767
0
  int ht40 = -1;
2768
2769
0
  if (!freq->ht_enabled)
2770
0
    return;
2771
2772
0
  for (chan_idx = 0; chan_idx < mode->num_channels; chan_idx++) {
2773
0
    pri_chan = &mode->channels[chan_idx];
2774
0
    if (pri_chan->chan == freq->channel)
2775
0
      break;
2776
0
    pri_chan = NULL;
2777
0
  }
2778
0
  if (!pri_chan)
2779
0
    return;
2780
2781
  /* Check primary channel flags */
2782
0
  if (pri_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2783
0
    return;
2784
2785
#ifdef CONFIG_HT_OVERRIDES
2786
  if (ssid->disable_ht40)
2787
    return;
2788
#endif
2789
2790
  /* Check/setup HT40+/HT40- */
2791
0
  for (j = 0; j < ARRAY_SIZE(ht40plus); j++) {
2792
0
    if (ht40plus[j] == freq->channel) {
2793
0
      ht40 = 1;
2794
0
      break;
2795
0
    }
2796
0
  }
2797
2798
  /* Find secondary channel */
2799
0
  for (i = 0; i < mode->num_channels; i++) {
2800
0
    sec_chan = &mode->channels[i];
2801
0
    if (sec_chan->chan == freq->channel + ht40 * 4)
2802
0
      break;
2803
0
    sec_chan = NULL;
2804
0
  }
2805
0
  if (!sec_chan)
2806
0
    return;
2807
2808
  /* Check secondary channel flags */
2809
0
  if (sec_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2810
0
    return;
2811
2812
0
  if (ht40 == -1) {
2813
0
    if (!(pri_chan->flag & HOSTAPD_CHAN_HT40MINUS))
2814
0
      return;
2815
0
  } else {
2816
0
    if (!(pri_chan->flag & HOSTAPD_CHAN_HT40PLUS))
2817
0
      return;
2818
0
  }
2819
0
  freq->sec_channel_offset = ht40;
2820
2821
0
  if (obss_scan) {
2822
0
    struct wpa_scan_results *scan_res;
2823
2824
0
    scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2825
0
    if (scan_res == NULL) {
2826
      /* Back to HT20 */
2827
0
      freq->sec_channel_offset = 0;
2828
0
      return;
2829
0
    }
2830
2831
0
    res = check_40mhz_5g(scan_res, pri_chan, sec_chan);
2832
0
    switch (res) {
2833
0
    case 0:
2834
      /* Back to HT20 */
2835
0
      freq->sec_channel_offset = 0;
2836
0
      break;
2837
0
    case 1:
2838
      /* Configuration allowed */
2839
0
      break;
2840
0
    case 2:
2841
      /* Switch pri/sec channels */
2842
0
      freq->freq = hw_get_freq(mode, sec_chan->chan);
2843
0
      freq->sec_channel_offset = -freq->sec_channel_offset;
2844
0
      freq->channel = sec_chan->chan;
2845
0
      break;
2846
0
    default:
2847
0
      freq->sec_channel_offset = 0;
2848
0
      break;
2849
0
    }
2850
2851
0
    wpa_scan_results_free(scan_res);
2852
0
  }
2853
2854
0
  wpa_printf(MSG_DEBUG,
2855
0
       "IBSS/mesh: setup freq channel %d, sec_channel_offset %d",
2856
0
       freq->channel, freq->sec_channel_offset);
2857
0
}
2858
2859
2860
static bool ibss_mesh_select_80_160mhz(struct wpa_supplicant *wpa_s,
2861
               const struct wpa_ssid *ssid,
2862
               struct hostapd_hw_modes *mode,
2863
               struct hostapd_freq_params *freq,
2864
0
               int ieee80211_mode, bool is_6ghz) {
2865
0
  static const int bw80[] = {
2866
0
    5180, 5260, 5500, 5580, 5660, 5745, 5955,
2867
0
    6035, 6115, 6195, 6275, 6355, 6435, 6515,
2868
0
    6595, 6675, 6755, 6835, 6915, 6995
2869
0
  };
2870
0
  static const int bw160[] = {
2871
0
    5955, 6115, 6275, 6435, 6595, 6755, 6915
2872
0
  };
2873
0
  struct hostapd_freq_params vht_freq;
2874
0
  int i;
2875
0
  unsigned int j, k;
2876
0
  int chwidth, seg0, seg1;
2877
0
  u32 vht_caps = 0;
2878
0
  u8 channel = freq->channel;
2879
2880
0
  if (!freq->vht_enabled && !freq->he_enabled)
2881
0
    return true;
2882
2883
0
  vht_freq = *freq;
2884
2885
0
  chwidth = CONF_OPER_CHWIDTH_USE_HT;
2886
0
  seg0 = freq->channel + 2 * freq->sec_channel_offset;
2887
0
  seg1 = 0;
2888
0
  if (freq->sec_channel_offset == 0) {
2889
0
    seg0 = 0;
2890
    /* Don't try 80 MHz if 40 MHz failed, except in 6 GHz */
2891
0
    if (freq->ht_enabled && !is_6ghz)
2892
0
      goto skip_80mhz;
2893
0
  }
2894
0
  if (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_USE_HT)
2895
0
    goto skip_80mhz;
2896
2897
  /* setup center_freq1, bandwidth */
2898
0
  for (j = 0; j < ARRAY_SIZE(bw80); j++) {
2899
0
    if (freq->freq >= bw80[j] &&
2900
0
        freq->freq < bw80[j] + 80)
2901
0
      break;
2902
0
  }
2903
2904
0
  if (j == ARRAY_SIZE(bw80) ||
2905
0
      ieee80211_freq_to_chan(bw80[j], &channel) == NUM_HOSTAPD_MODES)
2906
0
    goto skip_80mhz;
2907
2908
  /* Use 40 MHz if channel not usable */
2909
0
  if (!ibss_mesh_is_80mhz_avail(channel, mode))
2910
0
    goto skip_80mhz;
2911
2912
0
  chwidth = CONF_OPER_CHWIDTH_80MHZ;
2913
0
  seg0 = channel + 6;
2914
0
  seg1 = 0;
2915
2916
  /* In 160 MHz, the initial four 20 MHz channels were validated
2917
   * above. If 160 MHz is supported, check the remaining four 20 MHz
2918
   * channels for the total of 160 MHz bandwidth for 6 GHz.
2919
   */
2920
0
  if ((mode->he_capab[ieee80211_mode].phy_cap[
2921
0
         HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
2922
0
       HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G) && is_6ghz &&
2923
0
      ibss_mesh_is_80mhz_avail(channel + 16, mode)) {
2924
0
    for (j = 0; j < ARRAY_SIZE(bw160); j++) {
2925
0
      if (freq->freq == bw160[j]) {
2926
0
        chwidth = CONF_OPER_CHWIDTH_160MHZ;
2927
0
        seg0 = channel + 14;
2928
0
        break;
2929
0
      }
2930
0
    }
2931
0
  }
2932
2933
0
  if (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_80P80MHZ) {
2934
    /* setup center_freq2, bandwidth */
2935
0
    for (k = 0; k < ARRAY_SIZE(bw80); k++) {
2936
      /* Only accept 80 MHz segments separated by a gap */
2937
0
      if (j == k || abs(bw80[j] - bw80[k]) == 80)
2938
0
        continue;
2939
2940
0
      if (ieee80211_freq_to_chan(bw80[k], &channel) ==
2941
0
          NUM_HOSTAPD_MODES)
2942
0
        break;
2943
2944
0
      for (i = channel; i < channel + 16; i += 4) {
2945
0
        struct hostapd_channel_data *chan;
2946
2947
0
        chan = hw_get_channel_chan(mode, i, NULL);
2948
0
        if (!chan)
2949
0
          continue;
2950
2951
0
        if (chan->flag & (HOSTAPD_CHAN_DISABLED |
2952
0
              HOSTAPD_CHAN_NO_IR |
2953
0
              HOSTAPD_CHAN_RADAR))
2954
0
          continue;
2955
2956
        /* Found a suitable second segment for 80+80 */
2957
0
        chwidth = CONF_OPER_CHWIDTH_80P80MHZ;
2958
0
        if (!is_6ghz)
2959
0
          vht_caps |=
2960
0
            VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
2961
0
        seg1 = channel + 6;
2962
0
      }
2963
2964
0
      if (chwidth == CONF_OPER_CHWIDTH_80P80MHZ)
2965
0
        break;
2966
0
    }
2967
0
  } else if (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_160MHZ) {
2968
0
    if (freq->freq == 5180) {
2969
0
      chwidth = CONF_OPER_CHWIDTH_160MHZ;
2970
0
      vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
2971
0
      seg0 = 50;
2972
0
    } else if (freq->freq == 5520) {
2973
0
      chwidth = CONF_OPER_CHWIDTH_160MHZ;
2974
0
      vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
2975
0
      seg0 = 114;
2976
0
    }
2977
0
  }
2978
2979
0
skip_80mhz:
2980
0
  if (hostapd_set_freq_params(&vht_freq, mode->mode, freq->freq,
2981
0
            freq->channel, ssid->enable_edmg,
2982
0
            ssid->edmg_channel, freq->ht_enabled,
2983
0
            freq->vht_enabled, freq->he_enabled,
2984
0
            freq->eht_enabled,
2985
0
            freq->sec_channel_offset,
2986
0
            chwidth, seg0, seg1, vht_caps,
2987
0
            &mode->he_capab[ieee80211_mode],
2988
0
            &mode->eht_capab[ieee80211_mode]) != 0)
2989
0
    return false;
2990
2991
0
  *freq = vht_freq;
2992
2993
0
  wpa_printf(MSG_DEBUG, "IBSS: VHT setup freq cf1 %d, cf2 %d, bw %d",
2994
0
       freq->center_freq1, freq->center_freq2, freq->bandwidth);
2995
0
  return true;
2996
0
}
2997
2998
2999
void ibss_mesh_setup_freq(struct wpa_supplicant *wpa_s,
3000
        const struct wpa_ssid *ssid,
3001
        struct hostapd_freq_params *freq)
3002
0
{
3003
0
  int ieee80211_mode = wpas_mode_to_ieee80211_mode(ssid->mode);
3004
0
  enum hostapd_hw_mode hw_mode;
3005
0
  struct hostapd_hw_modes *mode = NULL;
3006
0
  int i, obss_scan = 1;
3007
0
  u8 channel;
3008
0
  bool is_6ghz;
3009
3010
0
  freq->freq = ssid->frequency;
3011
3012
0
  if (ssid->mode == WPAS_MODE_IBSS && !ssid->fixed_freq) {
3013
0
    struct wpa_bss *bss = ibss_find_existing_bss(wpa_s, ssid);
3014
3015
0
    if (bss) {
3016
0
      wpa_printf(MSG_DEBUG,
3017
0
           "IBSS already found in scan results, adjust control freq: %d",
3018
0
           bss->freq);
3019
0
      freq->freq = bss->freq;
3020
0
      obss_scan = 0;
3021
0
    }
3022
0
  }
3023
3024
0
  hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
3025
0
  for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
3026
0
    if (wpa_s->hw.modes[i].mode == hw_mode &&
3027
0
        hw_mode_get_channel(&wpa_s->hw.modes[i], freq->freq,
3028
0
          NULL) != NULL) {
3029
0
      mode = &wpa_s->hw.modes[i];
3030
0
      break;
3031
0
    }
3032
0
  }
3033
3034
0
  if (!mode)
3035
0
    return;
3036
3037
0
  is_6ghz = is_6ghz_freq(freq->freq);
3038
3039
0
  freq->ht_enabled = 0;
3040
0
  freq->vht_enabled = 0;
3041
0
  freq->he_enabled = 0;
3042
0
  freq->eht_enabled = 0;
3043
3044
0
  if (!is_6ghz)
3045
0
    freq->ht_enabled = ibss_mesh_can_use_ht(wpa_s, ssid, mode);
3046
0
  if (freq->ht_enabled)
3047
0
    freq->vht_enabled = ibss_mesh_can_use_vht(wpa_s, ssid, mode);
3048
0
  if (freq->vht_enabled || is_6ghz)
3049
0
    freq->he_enabled = ibss_mesh_can_use_he(wpa_s, ssid, mode,
3050
0
              ieee80211_mode);
3051
0
  freq->channel = channel;
3052
  /* Setup higher BW only for 5 GHz */
3053
0
  if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
3054
0
    ibss_mesh_select_40mhz(wpa_s, ssid, mode, freq, obss_scan);
3055
0
    if (!ibss_mesh_select_80_160mhz(wpa_s, ssid, mode, freq,
3056
0
            ieee80211_mode, is_6ghz))
3057
0
      freq->he_enabled = freq->vht_enabled = false;
3058
0
  }
3059
3060
0
  if (freq->he_enabled)
3061
0
    freq->eht_enabled = ibss_mesh_can_use_eht(wpa_s, ssid, mode,
3062
0
                ieee80211_mode);
3063
0
}
3064
3065
3066
#ifdef CONFIG_FILS
3067
static size_t wpas_add_fils_hlp_req(struct wpa_supplicant *wpa_s, u8 *ie_buf,
3068
            size_t ie_buf_len)
3069
{
3070
  struct fils_hlp_req *req;
3071
  size_t rem_len, hdr_len, hlp_len, len, ie_len = 0;
3072
  const u8 *pos;
3073
  u8 *buf = ie_buf;
3074
3075
  dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
3076
       list) {
3077
    rem_len = ie_buf_len - ie_len;
3078
    pos = wpabuf_head(req->pkt);
3079
    hdr_len = 1 + 2 * ETH_ALEN + 6;
3080
    hlp_len = wpabuf_len(req->pkt);
3081
3082
    if (rem_len < 2 + hdr_len + hlp_len) {
3083
      wpa_printf(MSG_ERROR,
3084
           "FILS: Cannot fit HLP - rem_len=%lu to_fill=%lu",
3085
           (unsigned long) rem_len,
3086
           (unsigned long) (2 + hdr_len + hlp_len));
3087
      break;
3088
    }
3089
3090
    len = (hdr_len + hlp_len) > 255 ? 255 : hdr_len + hlp_len;
3091
    /* Element ID */
3092
    *buf++ = WLAN_EID_EXTENSION;
3093
    /* Length */
3094
    *buf++ = len;
3095
    /* Element ID Extension */
3096
    *buf++ = WLAN_EID_EXT_FILS_HLP_CONTAINER;
3097
    /* Destination MAC address */
3098
    os_memcpy(buf, req->dst, ETH_ALEN);
3099
    buf += ETH_ALEN;
3100
    /* Source MAC address */
3101
    os_memcpy(buf, wpa_s->own_addr, ETH_ALEN);
3102
    buf += ETH_ALEN;
3103
    /* LLC/SNAP Header */
3104
    os_memcpy(buf, "\xaa\xaa\x03\x00\x00\x00", 6);
3105
    buf += 6;
3106
    /* HLP Packet */
3107
    os_memcpy(buf, pos, len - hdr_len);
3108
    buf += len - hdr_len;
3109
    pos += len - hdr_len;
3110
3111
    hlp_len -= len - hdr_len;
3112
    ie_len += 2 + len;
3113
    rem_len -= 2 + len;
3114
3115
    while (hlp_len) {
3116
      len = (hlp_len > 255) ? 255 : hlp_len;
3117
      if (rem_len < 2 + len)
3118
        break;
3119
      *buf++ = WLAN_EID_FRAGMENT;
3120
      *buf++ = len;
3121
      os_memcpy(buf, pos, len);
3122
      buf += len;
3123
      pos += len;
3124
3125
      hlp_len -= len;
3126
      ie_len += 2 + len;
3127
      rem_len -= 2 + len;
3128
    }
3129
  }
3130
3131
  return ie_len;
3132
}
3133
3134
3135
int wpa_is_fils_supported(struct wpa_supplicant *wpa_s)
3136
{
3137
  return (((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3138
     (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS)) ||
3139
    (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3140
     (wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD)));
3141
}
3142
3143
3144
int wpa_is_fils_sk_pfs_supported(struct wpa_supplicant *wpa_s)
3145
{
3146
#ifdef CONFIG_FILS_SK_PFS
3147
  return (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3148
    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS);
3149
#else /* CONFIG_FILS_SK_PFS */
3150
  return 0;
3151
#endif /* CONFIG_FILS_SK_PFS */
3152
}
3153
3154
#endif /* CONFIG_FILS */
3155
3156
3157
static int wpas_populate_wfa_capa(struct wpa_supplicant *wpa_s,
3158
          struct wpa_bss *bss,
3159
          u8 *wpa_ie, size_t wpa_ie_len,
3160
          size_t max_wpa_ie_len)
3161
0
{
3162
0
  struct wpabuf *wfa_ie = NULL;
3163
0
  u8 wfa_capa[1];
3164
0
  size_t wfa_ie_len, buf_len;
3165
3166
0
  os_memset(wfa_capa, 0, sizeof(wfa_capa));
3167
0
  if (wpa_s->enable_dscp_policy_capa)
3168
0
    wfa_capa[0] |= WFA_CAPA_QM_DSCP_POLICY;
3169
3170
0
  if (!wfa_capa[0])
3171
0
    return wpa_ie_len;
3172
3173
  /* Wi-Fi Alliance element */
3174
0
  buf_len = 1 + /* Element ID */
3175
0
      1 + /* Length */
3176
0
      3 + /* OUI */
3177
0
      1 + /* OUI Type */
3178
0
      1 + /* Capabilities Length */
3179
0
      sizeof(wfa_capa); /* Capabilities */
3180
0
  wfa_ie = wpabuf_alloc(buf_len);
3181
0
  if (!wfa_ie)
3182
0
    return wpa_ie_len;
3183
3184
0
  wpabuf_put_u8(wfa_ie, WLAN_EID_VENDOR_SPECIFIC);
3185
0
  wpabuf_put_u8(wfa_ie, buf_len - 2);
3186
0
  wpabuf_put_be24(wfa_ie, OUI_WFA);
3187
0
  wpabuf_put_u8(wfa_ie, WFA_CAPA_OUI_TYPE);
3188
0
  wpabuf_put_u8(wfa_ie, sizeof(wfa_capa));
3189
0
  wpabuf_put_data(wfa_ie, wfa_capa, sizeof(wfa_capa));
3190
3191
0
  wfa_ie_len = wpabuf_len(wfa_ie);
3192
0
  if (wpa_ie_len + wfa_ie_len <= max_wpa_ie_len) {
3193
0
    wpa_hexdump_buf(MSG_MSGDUMP, "WFA Capabilities element",
3194
0
        wfa_ie);
3195
0
    os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(wfa_ie),
3196
0
        wfa_ie_len);
3197
0
    wpa_ie_len += wfa_ie_len;
3198
0
  }
3199
3200
0
  wpabuf_free(wfa_ie);
3201
0
  return wpa_ie_len;
3202
0
}
3203
3204
3205
static u8 * wpas_populate_assoc_ies(
3206
  struct wpa_supplicant *wpa_s,
3207
  struct wpa_bss *bss, struct wpa_ssid *ssid,
3208
  struct wpa_driver_associate_params *params,
3209
  enum wpa_drv_update_connect_params_mask *mask)
3210
0
{
3211
0
  u8 *wpa_ie;
3212
0
  size_t max_wpa_ie_len = 500;
3213
0
  size_t wpa_ie_len;
3214
0
  int algs = WPA_AUTH_ALG_OPEN;
3215
#ifdef CONFIG_MBO
3216
  const u8 *mbo_ie;
3217
#endif
3218
#if defined(CONFIG_SAE) || defined(CONFIG_FILS)
3219
  int pmksa_cached = 0;
3220
#endif /* CONFIG_SAE || CONFIG_FILS */
3221
#ifdef CONFIG_FILS
3222
  const u8 *realm, *username, *rrk;
3223
  size_t realm_len, username_len, rrk_len;
3224
  u16 next_seq_num;
3225
  struct fils_hlp_req *req;
3226
3227
  dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
3228
       list) {
3229
    max_wpa_ie_len += 3 + 2 * ETH_ALEN + 6 + wpabuf_len(req->pkt) +
3230
          2 + 2 * wpabuf_len(req->pkt) / 255;
3231
  }
3232
#endif /* CONFIG_FILS */
3233
3234
0
  wpa_ie = os_malloc(max_wpa_ie_len);
3235
0
  if (!wpa_ie) {
3236
0
    wpa_printf(MSG_ERROR,
3237
0
         "Failed to allocate connect IE buffer for %lu bytes",
3238
0
         (unsigned long) max_wpa_ie_len);
3239
0
    return NULL;
3240
0
  }
3241
3242
0
  if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
3243
0
        wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
3244
0
      wpa_key_mgmt_wpa(ssid->key_mgmt)) {
3245
0
    int try_opportunistic;
3246
0
    const u8 *cache_id = NULL;
3247
0
    const u8 *addr = bss->bssid;
3248
3249
0
    if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3250
0
        (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_MLO) &&
3251
0
        !is_zero_ether_addr(bss->mld_addr))
3252
0
      addr = bss->mld_addr;
3253
3254
0
    if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3255
0
        wpa_s->valid_links)
3256
0
      addr = wpa_s->ap_mld_addr;
3257
3258
0
    try_opportunistic = (ssid->proactive_key_caching < 0 ?
3259
0
             wpa_s->conf->okc :
3260
0
             ssid->proactive_key_caching) &&
3261
0
      (ssid->proto & WPA_PROTO_RSN);
3262
#ifdef CONFIG_FILS
3263
    if (wpa_key_mgmt_fils(ssid->key_mgmt))
3264
      cache_id = wpa_bss_get_fils_cache_id(bss);
3265
#endif /* CONFIG_FILS */
3266
0
    if (pmksa_cache_set_current(wpa_s->wpa, NULL, addr,
3267
0
              ssid, try_opportunistic,
3268
0
              cache_id, 0) == 0) {
3269
0
      eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
3270
#if defined(CONFIG_SAE) || defined(CONFIG_FILS)
3271
      pmksa_cached = 1;
3272
#endif /* CONFIG_SAE || CONFIG_FILS */
3273
0
    }
3274
0
    wpa_ie_len = max_wpa_ie_len;
3275
0
    if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
3276
0
                wpa_ie, &wpa_ie_len, false)) {
3277
0
      wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
3278
0
        "key management and encryption suites");
3279
0
      os_free(wpa_ie);
3280
0
      return NULL;
3281
0
    }
3282
0
#ifdef CONFIG_HS20
3283
0
  } else if (bss && wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE) &&
3284
0
       (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)) {
3285
    /* No PMKSA caching, but otherwise similar to RSN/WPA */
3286
0
    wpa_ie_len = max_wpa_ie_len;
3287
0
    if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
3288
0
                wpa_ie, &wpa_ie_len, false)) {
3289
0
      wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
3290
0
        "key management and encryption suites");
3291
0
      os_free(wpa_ie);
3292
0
      return NULL;
3293
0
    }
3294
0
#endif /* CONFIG_HS20 */
3295
0
  } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
3296
0
       wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
3297
    /*
3298
     * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
3299
     * use non-WPA since the scan results did not indicate that the
3300
     * AP is using WPA or WPA2.
3301
     */
3302
0
    wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
3303
0
    wpa_ie_len = 0;
3304
0
    wpa_s->wpa_proto = 0;
3305
0
  } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
3306
0
    wpa_ie_len = max_wpa_ie_len;
3307
0
    if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
3308
0
                wpa_ie, &wpa_ie_len, false)) {
3309
0
      wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
3310
0
        "key management and encryption suites (no "
3311
0
        "scan results)");
3312
0
      os_free(wpa_ie);
3313
0
      return NULL;
3314
0
    }
3315
#ifdef CONFIG_WPS
3316
  } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
3317
    struct wpabuf *wps_ie;
3318
    wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
3319
    if (wps_ie && wpabuf_len(wps_ie) <= max_wpa_ie_len) {
3320
      wpa_ie_len = wpabuf_len(wps_ie);
3321
      os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
3322
    } else
3323
      wpa_ie_len = 0;
3324
    wpabuf_free(wps_ie);
3325
    wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
3326
    if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
3327
      params->wps = WPS_MODE_PRIVACY;
3328
    else
3329
      params->wps = WPS_MODE_OPEN;
3330
    wpa_s->wpa_proto = 0;
3331
#endif /* CONFIG_WPS */
3332
0
  } else {
3333
0
    wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
3334
0
    wpa_ie_len = 0;
3335
0
    wpa_s->wpa_proto = 0;
3336
0
  }
3337
3338
0
#ifdef IEEE8021X_EAPOL
3339
0
  if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3340
0
    if (ssid->leap) {
3341
0
      if (ssid->non_leap == 0)
3342
0
        algs = WPA_AUTH_ALG_LEAP;
3343
0
      else
3344
0
        algs |= WPA_AUTH_ALG_LEAP;
3345
0
    }
3346
0
  }
3347
3348
#ifdef CONFIG_FILS
3349
  /* Clear FILS association */
3350
  wpa_sm_set_reset_fils_completed(wpa_s->wpa, 0);
3351
3352
  if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD) &&
3353
      ssid->eap.erp && wpa_key_mgmt_fils(wpa_s->key_mgmt) &&
3354
      eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap, &username,
3355
          &username_len, &realm, &realm_len,
3356
          &next_seq_num, &rrk, &rrk_len) == 0 &&
3357
      (!wpa_s->last_con_fail_realm ||
3358
       wpa_s->last_con_fail_realm_len != realm_len ||
3359
       os_memcmp(wpa_s->last_con_fail_realm, realm, realm_len) != 0)) {
3360
    algs = WPA_AUTH_ALG_FILS;
3361
    params->fils_erp_username = username;
3362
    params->fils_erp_username_len = username_len;
3363
    params->fils_erp_realm = realm;
3364
    params->fils_erp_realm_len = realm_len;
3365
    params->fils_erp_next_seq_num = next_seq_num;
3366
    params->fils_erp_rrk = rrk;
3367
    params->fils_erp_rrk_len = rrk_len;
3368
3369
    if (mask)
3370
      *mask |= WPA_DRV_UPDATE_FILS_ERP_INFO;
3371
  } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD) &&
3372
       ssid->eap.erp && wpa_key_mgmt_fils(wpa_s->key_mgmt) &&
3373
       pmksa_cached) {
3374
    algs = WPA_AUTH_ALG_FILS;
3375
  }
3376
#endif /* CONFIG_FILS */
3377
0
#endif /* IEEE8021X_EAPOL */
3378
#ifdef CONFIG_SAE
3379
  if (wpa_key_mgmt_sae(wpa_s->key_mgmt))
3380
    algs = WPA_AUTH_ALG_SAE;
3381
#endif /* CONFIG_SAE */
3382
3383
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
3384
0
  if (ssid->auth_alg) {
3385
0
    algs = ssid->auth_alg;
3386
0
    wpa_dbg(wpa_s, MSG_DEBUG,
3387
0
      "Overriding auth_alg selection: 0x%x", algs);
3388
0
  }
3389
3390
#ifdef CONFIG_SAE
3391
  if (pmksa_cached && algs == WPA_AUTH_ALG_SAE) {
3392
    wpa_dbg(wpa_s, MSG_DEBUG,
3393
      "SAE: Use WPA_AUTH_ALG_OPEN for PMKSA caching attempt");
3394
    algs = WPA_AUTH_ALG_OPEN;
3395
  }
3396
#endif /* CONFIG_SAE */
3397
3398
#ifdef CONFIG_P2P
3399
  if (wpa_s->global->p2p) {
3400
    u8 *pos;
3401
    size_t len;
3402
    int res;
3403
    pos = wpa_ie + wpa_ie_len;
3404
    len = max_wpa_ie_len - wpa_ie_len;
3405
    res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
3406
              ssid->p2p_group);
3407
    if (res >= 0)
3408
      wpa_ie_len += res;
3409
  }
3410
3411
  wpa_s->cross_connect_disallowed = 0;
3412
  if (bss) {
3413
    struct wpabuf *p2p;
3414
    p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3415
    if (p2p) {
3416
      wpa_s->cross_connect_disallowed =
3417
        p2p_get_cross_connect_disallowed(p2p);
3418
      wpabuf_free(p2p);
3419
      wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
3420
        "connection",
3421
        wpa_s->cross_connect_disallowed ?
3422
        "disallows" : "allows");
3423
    }
3424
  }
3425
3426
  os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
3427
#endif /* CONFIG_P2P */
3428
3429
0
  if (bss) {
3430
0
    wpa_ie_len += wpas_supp_op_class_ie(wpa_s, ssid, bss,
3431
0
                wpa_ie + wpa_ie_len,
3432
0
                max_wpa_ie_len -
3433
0
                wpa_ie_len);
3434
0
  }
3435
3436
  /*
3437
   * Workaround: Add Extended Capabilities element only if the AP
3438
   * included this element in Beacon/Probe Response frames. Some older
3439
   * APs seem to have interoperability issues if this element is
3440
   * included, so while the standard may require us to include the
3441
   * element in all cases, it is justifiable to skip it to avoid
3442
   * interoperability issues.
3443
   */
3444
0
  if (ssid->p2p_group)
3445
0
    wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
3446
0
  else
3447
0
    wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
3448
3449
0
  if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
3450
0
    u8 ext_capab[18];
3451
0
    int ext_capab_len;
3452
0
    ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
3453
0
                 sizeof(ext_capab));
3454
0
    if (ext_capab_len > 0 &&
3455
0
        wpa_ie_len + ext_capab_len <= max_wpa_ie_len) {
3456
0
      u8 *pos = wpa_ie;
3457
0
      if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
3458
0
        pos += 2 + pos[1];
3459
0
      os_memmove(pos + ext_capab_len, pos,
3460
0
           wpa_ie_len - (pos - wpa_ie));
3461
0
      wpa_ie_len += ext_capab_len;
3462
0
      os_memcpy(pos, ext_capab, ext_capab_len);
3463
0
    }
3464
0
  }
3465
3466
0
#ifdef CONFIG_HS20
3467
0
  if (is_hs20_network(wpa_s, ssid, bss)) {
3468
0
    struct wpabuf *hs20;
3469
3470
0
    hs20 = wpabuf_alloc(20 + MAX_ROAMING_CONS_OI_LEN);
3471
0
    if (hs20) {
3472
0
      int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
3473
0
      size_t len;
3474
3475
0
      wpas_hs20_add_indication(hs20, pps_mo_id,
3476
0
             get_hs20_version(bss));
3477
0
      wpas_hs20_add_roam_cons_sel(hs20, ssid);
3478
0
      len = max_wpa_ie_len - wpa_ie_len;
3479
0
      if (wpabuf_len(hs20) <= len) {
3480
0
        os_memcpy(wpa_ie + wpa_ie_len,
3481
0
            wpabuf_head(hs20), wpabuf_len(hs20));
3482
0
        wpa_ie_len += wpabuf_len(hs20);
3483
0
      }
3484
0
      wpabuf_free(hs20);
3485
3486
0
      hs20_configure_frame_filters(wpa_s);
3487
0
    }
3488
0
  }
3489
0
#endif /* CONFIG_HS20 */
3490
3491
0
  if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
3492
0
    struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
3493
0
    size_t len;
3494
3495
0
    len = max_wpa_ie_len - wpa_ie_len;
3496
0
    if (wpabuf_len(buf) <= len) {
3497
0
      os_memcpy(wpa_ie + wpa_ie_len,
3498
0
          wpabuf_head(buf), wpabuf_len(buf));
3499
0
      wpa_ie_len += wpabuf_len(buf);
3500
0
    }
3501
0
  }
3502
3503
#ifdef CONFIG_FST
3504
  if (wpa_s->fst_ies) {
3505
    int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
3506
3507
    if (wpa_ie_len + fst_ies_len <= max_wpa_ie_len) {
3508
      os_memcpy(wpa_ie + wpa_ie_len,
3509
          wpabuf_head(wpa_s->fst_ies), fst_ies_len);
3510
      wpa_ie_len += fst_ies_len;
3511
    }
3512
  }
3513
#endif /* CONFIG_FST */
3514
3515
#ifdef CONFIG_MBO
3516
  mbo_ie = bss ? wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE) : NULL;
3517
  if (!wpa_s->disable_mbo_oce && mbo_ie) {
3518
    int len;
3519
3520
    len = wpas_mbo_ie(wpa_s, wpa_ie + wpa_ie_len,
3521
          max_wpa_ie_len - wpa_ie_len,
3522
          !!mbo_attr_from_mbo_ie(mbo_ie,
3523
               OCE_ATTR_ID_CAPA_IND));
3524
    if (len >= 0)
3525
      wpa_ie_len += len;
3526
  }
3527
#endif /* CONFIG_MBO */
3528
3529
#ifdef CONFIG_FILS
3530
  if (algs == WPA_AUTH_ALG_FILS) {
3531
    size_t len;
3532
3533
    len = wpas_add_fils_hlp_req(wpa_s, wpa_ie + wpa_ie_len,
3534
              max_wpa_ie_len - wpa_ie_len);
3535
    wpa_ie_len += len;
3536
  }
3537
#endif /* CONFIG_FILS */
3538
3539
#ifdef CONFIG_OWE
3540
#ifdef CONFIG_TESTING_OPTIONS
3541
  if (get_ie_ext(wpa_ie, wpa_ie_len, WLAN_EID_EXT_OWE_DH_PARAM)) {
3542
    wpa_printf(MSG_INFO, "TESTING: Override OWE DH element");
3543
  } else
3544
#endif /* CONFIG_TESTING_OPTIONS */
3545
  if (algs == WPA_AUTH_ALG_OPEN &&
3546
      ssid->key_mgmt == WPA_KEY_MGMT_OWE) {
3547
    struct wpabuf *owe_ie;
3548
    u16 group;
3549
3550
    if (ssid->owe_group) {
3551
      group = ssid->owe_group;
3552
    } else if (wpa_s->assoc_status_code ==
3553
         WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) {
3554
      if (wpa_s->last_owe_group == 19)
3555
        group = 20;
3556
      else if (wpa_s->last_owe_group == 20)
3557
        group = 21;
3558
      else
3559
        group = OWE_DH_GROUP;
3560
    } else {
3561
      group = OWE_DH_GROUP;
3562
    }
3563
3564
    wpa_s->last_owe_group = group;
3565
    wpa_printf(MSG_DEBUG, "OWE: Try to use group %u", group);
3566
    owe_ie = owe_build_assoc_req(wpa_s->wpa, group);
3567
    if (owe_ie &&
3568
        wpabuf_len(owe_ie) <= max_wpa_ie_len - wpa_ie_len) {
3569
      os_memcpy(wpa_ie + wpa_ie_len,
3570
          wpabuf_head(owe_ie), wpabuf_len(owe_ie));
3571
      wpa_ie_len += wpabuf_len(owe_ie);
3572
    }
3573
    wpabuf_free(owe_ie);
3574
  }
3575
#endif /* CONFIG_OWE */
3576
3577
#ifdef CONFIG_DPP2
3578
  if (DPP_VERSION > 1 &&
3579
      wpa_sm_get_key_mgmt(wpa_s->wpa) == WPA_KEY_MGMT_DPP &&
3580
      ssid->dpp_netaccesskey &&
3581
      ssid->dpp_pfs != 2 && !ssid->dpp_pfs_fallback) {
3582
    struct rsn_pmksa_cache_entry *pmksa;
3583
3584
    pmksa = pmksa_cache_get_current(wpa_s->wpa);
3585
    if (!pmksa || !pmksa->dpp_pfs)
3586
      goto pfs_fail;
3587
3588
    dpp_pfs_free(wpa_s->dpp_pfs);
3589
    wpa_s->dpp_pfs = dpp_pfs_init(ssid->dpp_netaccesskey,
3590
                ssid->dpp_netaccesskey_len);
3591
    if (!wpa_s->dpp_pfs) {
3592
      wpa_printf(MSG_DEBUG, "DPP: Could not initialize PFS");
3593
      /* Try to continue without PFS */
3594
      goto pfs_fail;
3595
    }
3596
    if (wpabuf_len(wpa_s->dpp_pfs->ie) <=
3597
        max_wpa_ie_len - wpa_ie_len) {
3598
      os_memcpy(wpa_ie + wpa_ie_len,
3599
          wpabuf_head(wpa_s->dpp_pfs->ie),
3600
          wpabuf_len(wpa_s->dpp_pfs->ie));
3601
      wpa_ie_len += wpabuf_len(wpa_s->dpp_pfs->ie);
3602
    }
3603
  }
3604
pfs_fail:
3605
#endif /* CONFIG_DPP2 */
3606
3607
#ifdef CONFIG_IEEE80211R
3608
  /*
3609
   * Add MDIE under these conditions: the network profile allows FT,
3610
   * the AP supports FT, and the mobility domain ID matches.
3611
   */
3612
  if (bss && wpa_key_mgmt_ft(wpa_sm_get_key_mgmt(wpa_s->wpa))) {
3613
    const u8 *mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
3614
3615
    if (mdie && mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
3616
      size_t len = 0;
3617
      const u8 *md = mdie + 2;
3618
      const u8 *wpa_md = wpa_sm_get_ft_md(wpa_s->wpa);
3619
3620
      if (os_memcmp(md, wpa_md,
3621
              MOBILITY_DOMAIN_ID_LEN) == 0) {
3622
        /* Add mobility domain IE */
3623
        len = wpa_ft_add_mdie(
3624
          wpa_s->wpa, wpa_ie + wpa_ie_len,
3625
          max_wpa_ie_len - wpa_ie_len, mdie);
3626
        wpa_ie_len += len;
3627
      }
3628
#ifdef CONFIG_SME
3629
      if (len > 0 && wpa_s->sme.ft_used &&
3630
          wpa_sm_has_ft_keys(wpa_s->wpa, md)) {
3631
        wpa_dbg(wpa_s, MSG_DEBUG,
3632
          "SME: Trying to use FT over-the-air");
3633
        algs |= WPA_AUTH_ALG_FT;
3634
      }
3635
#endif /* CONFIG_SME */
3636
    }
3637
  }
3638
#endif /* CONFIG_IEEE80211R */
3639
3640
#ifdef CONFIG_TESTING_OPTIONS
3641
  if (wpa_s->rsnxe_override_assoc &&
3642
      wpabuf_len(wpa_s->rsnxe_override_assoc) <=
3643
      max_wpa_ie_len - wpa_ie_len) {
3644
    wpa_printf(MSG_DEBUG, "TESTING: RSNXE AssocReq override");
3645
    os_memcpy(wpa_ie + wpa_ie_len,
3646
        wpabuf_head(wpa_s->rsnxe_override_assoc),
3647
        wpabuf_len(wpa_s->rsnxe_override_assoc));
3648
    wpa_ie_len += wpabuf_len(wpa_s->rsnxe_override_assoc);
3649
  } else
3650
#endif /* CONFIG_TESTING_OPTIONS */
3651
0
  if (wpa_s->rsnxe_len > 0 &&
3652
0
      wpa_s->rsnxe_len <= max_wpa_ie_len - wpa_ie_len) {
3653
0
    os_memcpy(wpa_ie + wpa_ie_len, wpa_s->rsnxe, wpa_s->rsnxe_len);
3654
0
    wpa_ie_len += wpa_s->rsnxe_len;
3655
0
  }
3656
3657
#ifdef CONFIG_TESTING_OPTIONS
3658
  if (wpa_s->disable_mscs_support)
3659
    goto mscs_end;
3660
#endif /* CONFIG_TESTING_OPTIONS */
3661
0
  if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_MSCS) &&
3662
0
      wpa_s->robust_av.valid_config) {
3663
0
    struct wpabuf *mscs_ie;
3664
0
    size_t mscs_ie_len, buf_len;
3665
3666
0
    buf_len = 3 + /* MSCS descriptor IE header */
3667
0
        1 + /* Request type */
3668
0
        2 + /* User priority control */
3669
0
        4 + /* Stream timeout */
3670
0
        3 + /* TCLAS Mask IE header */
3671
0
        wpa_s->robust_av.frame_classifier_len;
3672
0
    mscs_ie = wpabuf_alloc(buf_len);
3673
0
    if (!mscs_ie) {
3674
0
      wpa_printf(MSG_INFO,
3675
0
           "MSCS: Failed to allocate MSCS IE");
3676
0
      goto mscs_end;
3677
0
    }
3678
3679
0
    wpas_populate_mscs_descriptor_ie(&wpa_s->robust_av, mscs_ie);
3680
0
    if ((wpa_ie_len + wpabuf_len(mscs_ie)) <= max_wpa_ie_len) {
3681
0
      wpa_hexdump_buf(MSG_MSGDUMP, "MSCS IE", mscs_ie);
3682
0
      mscs_ie_len = wpabuf_len(mscs_ie);
3683
0
      os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(mscs_ie),
3684
0
          mscs_ie_len);
3685
0
      wpa_ie_len += mscs_ie_len;
3686
0
    }
3687
3688
0
    wpabuf_free(mscs_ie);
3689
0
  }
3690
0
mscs_end:
3691
3692
0
  wpa_ie_len = wpas_populate_wfa_capa(wpa_s, bss, wpa_ie, wpa_ie_len,
3693
0
              max_wpa_ie_len);
3694
3695
0
  if (ssid->multi_ap_backhaul_sta) {
3696
0
    size_t multi_ap_ie_len;
3697
3698
0
    multi_ap_ie_len = add_multi_ap_ie(wpa_ie + wpa_ie_len,
3699
0
              max_wpa_ie_len - wpa_ie_len,
3700
0
              MULTI_AP_BACKHAUL_STA);
3701
0
    if (multi_ap_ie_len == 0) {
3702
0
      wpa_printf(MSG_ERROR,
3703
0
           "Multi-AP: Failed to build Multi-AP IE");
3704
0
      os_free(wpa_ie);
3705
0
      return NULL;
3706
0
    }
3707
0
    wpa_ie_len += multi_ap_ie_len;
3708
0
  }
3709
3710
0
  params->wpa_ie = wpa_ie;
3711
0
  params->wpa_ie_len = wpa_ie_len;
3712
0
  params->auth_alg = algs;
3713
0
  if (mask)
3714
0
    *mask |= WPA_DRV_UPDATE_ASSOC_IES | WPA_DRV_UPDATE_AUTH_TYPE;
3715
3716
0
  return wpa_ie;
3717
0
}
3718
3719
3720
#ifdef CONFIG_OWE
3721
static void wpas_update_owe_connect_params(struct wpa_supplicant *wpa_s)
3722
{
3723
  struct wpa_driver_associate_params params;
3724
  u8 *wpa_ie;
3725
3726
  os_memset(&params, 0, sizeof(params));
3727
  wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
3728
           wpa_s->current_ssid, &params, NULL);
3729
  if (!wpa_ie)
3730
    return;
3731
3732
  wpa_drv_update_connect_params(wpa_s, &params, WPA_DRV_UPDATE_ASSOC_IES);
3733
  os_free(wpa_ie);
3734
}
3735
#endif /* CONFIG_OWE */
3736
3737
3738
#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
3739
static void wpas_update_fils_connect_params(struct wpa_supplicant *wpa_s)
3740
{
3741
  struct wpa_driver_associate_params params;
3742
  enum wpa_drv_update_connect_params_mask mask = 0;
3743
  u8 *wpa_ie;
3744
3745
  if (wpa_s->auth_alg != WPA_AUTH_ALG_OPEN)
3746
    return; /* nothing to do */
3747
3748
  os_memset(&params, 0, sizeof(params));
3749
  wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
3750
           wpa_s->current_ssid, &params, &mask);
3751
  if (!wpa_ie)
3752
    return;
3753
3754
  if (params.auth_alg == WPA_AUTH_ALG_FILS) {
3755
    wpa_s->auth_alg = params.auth_alg;
3756
    wpa_drv_update_connect_params(wpa_s, &params, mask);
3757
  }
3758
3759
  os_free(wpa_ie);
3760
}
3761
#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
3762
3763
3764
static u8 wpa_ie_get_edmg_oper_chans(const u8 *edmg_ie)
3765
0
{
3766
0
  if (!edmg_ie || edmg_ie[1] < 6)
3767
0
    return 0;
3768
0
  return edmg_ie[EDMG_BSS_OPERATING_CHANNELS_OFFSET];
3769
0
}
3770
3771
3772
static u8 wpa_ie_get_edmg_oper_chan_width(const u8 *edmg_ie)
3773
0
{
3774
0
  if (!edmg_ie || edmg_ie[1] < 6)
3775
0
    return 0;
3776
0
  return edmg_ie[EDMG_OPERATING_CHANNEL_WIDTH_OFFSET];
3777
0
}
3778
3779
3780
/* Returns the intersection of two EDMG configurations.
3781
 * Note: The current implementation is limited to CB2 only (CB1 included),
3782
 * i.e., the implementation supports up to 2 contiguous channels.
3783
 * For supporting non-contiguous (aggregated) channels and for supporting
3784
 * CB3 and above, this function will need to be extended.
3785
 */
3786
static struct ieee80211_edmg_config
3787
get_edmg_intersection(struct ieee80211_edmg_config a,
3788
          struct ieee80211_edmg_config b,
3789
          u8 primary_channel)
3790
0
{
3791
0
  struct ieee80211_edmg_config result;
3792
0
  int i, contiguous = 0;
3793
0
  int max_contiguous = 0;
3794
3795
0
  result.channels = b.channels & a.channels;
3796
0
  if (!result.channels) {
3797
0
    wpa_printf(MSG_DEBUG,
3798
0
         "EDMG not possible: cannot intersect channels 0x%x and 0x%x",
3799
0
         a.channels, b.channels);
3800
0
    goto fail;
3801
0
  }
3802
3803
0
  if (!(result.channels & BIT(primary_channel - 1))) {
3804
0
    wpa_printf(MSG_DEBUG,
3805
0
         "EDMG not possible: the primary channel %d is not one of the intersected channels 0x%x",
3806
0
         primary_channel, result.channels);
3807
0
    goto fail;
3808
0
  }
3809
3810
  /* Find max contiguous channels */
3811
0
  for (i = 0; i < 6; i++) {
3812
0
    if (result.channels & BIT(i))
3813
0
      contiguous++;
3814
0
    else
3815
0
      contiguous = 0;
3816
3817
0
    if (contiguous > max_contiguous)
3818
0
      max_contiguous = contiguous;
3819
0
  }
3820
3821
  /* Assuming AP and STA supports ONLY contiguous channels,
3822
   * bw configuration can have value between 4-7.
3823
   */
3824
0
  if ((b.bw_config < a.bw_config))
3825
0
    result.bw_config = b.bw_config;
3826
0
  else
3827
0
    result.bw_config = a.bw_config;
3828
3829
0
  if ((max_contiguous >= 2 && result.bw_config < EDMG_BW_CONFIG_5) ||
3830
0
      (max_contiguous >= 1 && result.bw_config < EDMG_BW_CONFIG_4)) {
3831
0
    wpa_printf(MSG_DEBUG,
3832
0
         "EDMG not possible: not enough contiguous channels %d for supporting CB1 or CB2",
3833
0
         max_contiguous);
3834
0
    goto fail;
3835
0
  }
3836
3837
0
  return result;
3838
3839
0
fail:
3840
0
  result.channels = 0;
3841
0
  result.bw_config = 0;
3842
0
  return result;
3843
0
}
3844
3845
3846
static struct ieee80211_edmg_config
3847
get_supported_edmg(struct wpa_supplicant *wpa_s,
3848
       struct hostapd_freq_params *freq,
3849
       struct ieee80211_edmg_config request_edmg)
3850
0
{
3851
0
  enum hostapd_hw_mode hw_mode;
3852
0
  struct hostapd_hw_modes *mode = NULL;
3853
0
  u8 primary_channel;
3854
3855
0
  if (!wpa_s->hw.modes)
3856
0
    goto fail;
3857
3858
0
  hw_mode = ieee80211_freq_to_chan(freq->freq, &primary_channel);
3859
0
  if (hw_mode == NUM_HOSTAPD_MODES)
3860
0
    goto fail;
3861
3862
0
  mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, hw_mode, false);
3863
0
  if (!mode)
3864
0
    goto fail;
3865
3866
0
  return get_edmg_intersection(mode->edmg, request_edmg, primary_channel);
3867
3868
0
fail:
3869
0
  request_edmg.channels = 0;
3870
0
  request_edmg.bw_config = 0;
3871
0
  return request_edmg;
3872
0
}
3873
3874
3875
#ifdef CONFIG_MBO
3876
void wpas_update_mbo_connect_params(struct wpa_supplicant *wpa_s)
3877
{
3878
  struct wpa_driver_associate_params params;
3879
  u8 *wpa_ie;
3880
3881
  /*
3882
   * Update MBO connect params only in case of change of MBO attributes
3883
   * when connected, if the AP support MBO.
3884
   */
3885
3886
  if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_ssid ||
3887
      !wpa_s->current_bss ||
3888
      !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
3889
    return;
3890
3891
  os_memset(&params, 0, sizeof(params));
3892
  wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
3893
           wpa_s->current_ssid, &params, NULL);
3894
  if (!wpa_ie)
3895
    return;
3896
3897
  wpa_drv_update_connect_params(wpa_s, &params, WPA_DRV_UPDATE_ASSOC_IES);
3898
  os_free(wpa_ie);
3899
}
3900
#endif /* CONFIG_MBO */
3901
3902
3903
static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
3904
0
{
3905
0
  struct wpa_connect_work *cwork = work->ctx;
3906
0
  struct wpa_bss *bss = cwork->bss;
3907
0
  struct wpa_ssid *ssid = cwork->ssid;
3908
0
  struct wpa_supplicant *wpa_s = work->wpa_s;
3909
0
  u8 *wpa_ie;
3910
0
  const u8 *edmg_ie_oper;
3911
0
  int use_crypt, ret, bssid_changed;
3912
0
  unsigned int cipher_pairwise, cipher_group, cipher_group_mgmt;
3913
0
  struct wpa_driver_associate_params params;
3914
0
  u8 psk[PMK_LEN];
3915
0
#if defined(CONFIG_WEP) || defined(IEEE8021X_EAPOL)
3916
0
  int wep_keys_set = 0;
3917
0
#endif /* CONFIG_WEP || IEEE8021X_EAPOL */
3918
0
  int assoc_failed = 0;
3919
0
  struct wpa_ssid *old_ssid;
3920
0
  u8 prev_bssid[ETH_ALEN];
3921
#ifdef CONFIG_HT_OVERRIDES
3922
  struct ieee80211_ht_capabilities htcaps;
3923
  struct ieee80211_ht_capabilities htcaps_mask;
3924
#endif /* CONFIG_HT_OVERRIDES */
3925
#ifdef CONFIG_VHT_OVERRIDES
3926
       struct ieee80211_vht_capabilities vhtcaps;
3927
       struct ieee80211_vht_capabilities vhtcaps_mask;
3928
#endif /* CONFIG_VHT_OVERRIDES */
3929
3930
0
  wpa_s->roam_in_progress = false;
3931
0
#ifdef CONFIG_WNM
3932
0
  wpa_s->bss_trans_mgmt_in_progress = false;
3933
0
#endif /* CONFIG_WNM */
3934
3935
0
  if (deinit) {
3936
0
    if (work->started) {
3937
0
      wpa_s->connect_work = NULL;
3938
3939
      /* cancel possible auth. timeout */
3940
0
      eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
3941
0
               NULL);
3942
0
    }
3943
0
    wpas_connect_work_free(cwork);
3944
0
    return;
3945
0
  }
3946
3947
0
  wpa_s->connect_work = work;
3948
3949
0
  if (cwork->bss_removed || !wpas_valid_bss_ssid(wpa_s, bss, ssid) ||
3950
0
      wpas_network_disabled(wpa_s, ssid)) {
3951
0
    wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
3952
0
    wpas_connect_work_done(wpa_s);
3953
0
    return;
3954
0
  }
3955
3956
0
  os_memcpy(prev_bssid, wpa_s->bssid, ETH_ALEN);
3957
0
  os_memset(&params, 0, sizeof(params));
3958
0
  wpa_s->reassociate = 0;
3959
0
  wpa_s->eap_expected_failure = 0;
3960
3961
  /* Starting new association, so clear the possibly used WPA IE from the
3962
   * previous association. */
3963
0
  wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
3964
0
  wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
3965
0
  wpa_s->rsnxe_len = 0;
3966
0
  wpa_s->mscs_setup_done = false;
3967
3968
0
  wpa_ie = wpas_populate_assoc_ies(wpa_s, bss, ssid, &params, NULL);
3969
0
  if (!wpa_ie) {
3970
0
    wpas_connect_work_done(wpa_s);
3971
0
    return;
3972
0
  }
3973
3974
0
  if (bss &&
3975
0
      (!wpas_driver_bss_selection(wpa_s) || wpas_wps_searching(wpa_s))) {
3976
#ifdef CONFIG_IEEE80211R
3977
    const u8 *ie, *md = NULL;
3978
#endif /* CONFIG_IEEE80211R */
3979
0
    wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
3980
0
      " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
3981
0
      wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
3982
0
    bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
3983
0
    os_memset(wpa_s->bssid, 0, ETH_ALEN);
3984
0
    os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
3985
0
    if (bssid_changed)
3986
0
      wpas_notify_bssid_changed(wpa_s);
3987
#ifdef CONFIG_IEEE80211R
3988
    ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
3989
    if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
3990
      md = ie + 2;
3991
    wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
3992
    if (md) {
3993
      /* Prepare for the next transition */
3994
      wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
3995
    }
3996
#endif /* CONFIG_IEEE80211R */
3997
#ifdef CONFIG_WPS
3998
  } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
3999
       wpa_s->conf->ap_scan == 2 &&
4000
       (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
4001
    /* Use ap_scan==1 style network selection to find the network
4002
     */
4003
    wpas_connect_work_done(wpa_s);
4004
    wpa_s->scan_req = MANUAL_SCAN_REQ;
4005
    wpa_s->reassociate = 1;
4006
    wpa_supplicant_req_scan(wpa_s, 0, 0);
4007
    os_free(wpa_ie);
4008
    return;
4009
#endif /* CONFIG_WPS */
4010
0
  } else {
4011
0
    wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
4012
0
      wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4013
0
    if (bss)
4014
0
      os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
4015
0
    else
4016
0
      os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
4017
0
  }
4018
0
  if (!wpa_s->pno)
4019
0
    wpa_supplicant_cancel_sched_scan(wpa_s);
4020
4021
0
  wpa_supplicant_cancel_scan(wpa_s);
4022
4023
0
  wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
4024
0
  use_crypt = 1;
4025
0
  cipher_pairwise = wpa_s->pairwise_cipher;
4026
0
  cipher_group = wpa_s->group_cipher;
4027
0
  cipher_group_mgmt = wpa_s->mgmt_group_cipher;
4028
0
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
4029
0
      wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
4030
0
    if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
4031
0
      use_crypt = 0;
4032
#ifdef CONFIG_WEP
4033
    if (wpa_set_wep_keys(wpa_s, ssid)) {
4034
      use_crypt = 1;
4035
      wep_keys_set = 1;
4036
    }
4037
#endif /* CONFIG_WEP */
4038
0
  }
4039
0
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
4040
0
    use_crypt = 0;
4041
4042
0
#ifdef IEEE8021X_EAPOL
4043
0
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
4044
0
    if ((ssid->eapol_flags &
4045
0
         (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
4046
0
          EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
4047
0
        !wep_keys_set) {
4048
0
      use_crypt = 0;
4049
0
    } else {
4050
      /* Assume that dynamic WEP-104 keys will be used and
4051
       * set cipher suites in order for drivers to expect
4052
       * encryption. */
4053
0
      cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
4054
0
    }
4055
0
  }
4056
0
#endif /* IEEE8021X_EAPOL */
4057
4058
0
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
4059
    /* Set the key before (and later after) association */
4060
0
    wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
4061
0
  }
4062
4063
  /* Set current_ssid before changing state to ASSOCIATING, so that the
4064
   * selected SSID is available to wpas_notify_state_changed(). */
4065
0
  old_ssid = wpa_s->current_ssid;
4066
0
  wpa_s->current_ssid = ssid;
4067
4068
0
  wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
4069
0
  if (bss) {
4070
0
    params.ssid = bss->ssid;
4071
0
    params.ssid_len = bss->ssid_len;
4072
0
    if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set ||
4073
0
        wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
4074
0
      wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
4075
0
           MACSTR " freq=%u MHz based on scan results "
4076
0
           "(bssid_set=%d wps=%d)",
4077
0
           MAC2STR(bss->bssid), bss->freq,
4078
0
           ssid->bssid_set,
4079
0
           wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
4080
0
      params.bssid = bss->bssid;
4081
0
      params.freq.freq = bss->freq;
4082
0
    }
4083
0
    params.bssid_hint = bss->bssid;
4084
0
    params.freq_hint = bss->freq;
4085
0
    params.pbss = bss_is_pbss(bss);
4086
0
  } else {
4087
0
    if (ssid->bssid_hint_set)
4088
0
      params.bssid_hint = ssid->bssid_hint;
4089
4090
0
    params.ssid = ssid->ssid;
4091
0
    params.ssid_len = ssid->ssid_len;
4092
0
    params.pbss = (ssid->pbss != 2) ? ssid->pbss : 0;
4093
0
  }
4094
4095
0
  if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
4096
0
      wpa_s->conf->ap_scan == 2) {
4097
0
    params.bssid = ssid->bssid;
4098
0
    params.fixed_bssid = 1;
4099
0
  }
4100
4101
  /* Initial frequency for IBSS/mesh */
4102
0
  if ((ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) &&
4103
0
      ssid->frequency > 0 && params.freq.freq == 0)
4104
0
    ibss_mesh_setup_freq(wpa_s, ssid, &params.freq);
4105
4106
0
  if (ssid->mode == WPAS_MODE_IBSS) {
4107
0
    params.fixed_freq = ssid->fixed_freq;
4108
0
    if (ssid->beacon_int)
4109
0
      params.beacon_int = ssid->beacon_int;
4110
0
    else
4111
0
      params.beacon_int = wpa_s->conf->beacon_int;
4112
0
  }
4113
4114
0
  if (bss && ssid->enable_edmg)
4115
0
    edmg_ie_oper = wpa_bss_get_ie_ext(bss,
4116
0
              WLAN_EID_EXT_EDMG_OPERATION);
4117
0
  else
4118
0
    edmg_ie_oper = NULL;
4119
4120
0
  if (edmg_ie_oper) {
4121
0
    params.freq.edmg.channels =
4122
0
      wpa_ie_get_edmg_oper_chans(edmg_ie_oper);
4123
0
    params.freq.edmg.bw_config =
4124
0
      wpa_ie_get_edmg_oper_chan_width(edmg_ie_oper);
4125
0
    wpa_printf(MSG_DEBUG,
4126
0
         "AP supports EDMG channels 0x%x, bw_config %d",
4127
0
         params.freq.edmg.channels,
4128
0
         params.freq.edmg.bw_config);
4129
4130
    /* User may ask for specific EDMG channel for EDMG connection
4131
     * (must be supported by AP)
4132
     */
4133
0
    if (ssid->edmg_channel) {
4134
0
      struct ieee80211_edmg_config configured_edmg;
4135
0
      enum hostapd_hw_mode hw_mode;
4136
0
      u8 primary_channel;
4137
4138
0
      hw_mode = ieee80211_freq_to_chan(bss->freq,
4139
0
               &primary_channel);
4140
0
      if (hw_mode == NUM_HOSTAPD_MODES)
4141
0
        goto edmg_fail;
4142
4143
0
      hostapd_encode_edmg_chan(ssid->enable_edmg,
4144
0
             ssid->edmg_channel,
4145
0
             primary_channel,
4146
0
             &configured_edmg);
4147
4148
0
      if (ieee802_edmg_is_allowed(params.freq.edmg,
4149
0
                configured_edmg)) {
4150
0
        params.freq.edmg = configured_edmg;
4151
0
        wpa_printf(MSG_DEBUG,
4152
0
             "Use EDMG channel %d for connection",
4153
0
             ssid->edmg_channel);
4154
0
      } else {
4155
0
      edmg_fail:
4156
0
        params.freq.edmg.channels = 0;
4157
0
        params.freq.edmg.bw_config = 0;
4158
0
        wpa_printf(MSG_WARNING,
4159
0
             "EDMG channel %d not supported by AP, fallback to DMG",
4160
0
             ssid->edmg_channel);
4161
0
      }
4162
0
    }
4163
4164
0
    if (params.freq.edmg.channels) {
4165
0
      wpa_printf(MSG_DEBUG,
4166
0
           "EDMG before: channels 0x%x, bw_config %d",
4167
0
           params.freq.edmg.channels,
4168
0
           params.freq.edmg.bw_config);
4169
0
      params.freq.edmg = get_supported_edmg(wpa_s,
4170
0
                    &params.freq,
4171
0
                    params.freq.edmg);
4172
0
      wpa_printf(MSG_DEBUG,
4173
0
           "EDMG after: channels 0x%x, bw_config %d",
4174
0
           params.freq.edmg.channels,
4175
0
           params.freq.edmg.bw_config);
4176
0
    }
4177
0
  }
4178
4179
0
  params.pairwise_suite = cipher_pairwise;
4180
0
  params.group_suite = cipher_group;
4181
0
  params.mgmt_group_suite = cipher_group_mgmt;
4182
0
  params.key_mgmt_suite = wpa_s->key_mgmt;
4183
0
  params.allowed_key_mgmts = wpa_s->allowed_key_mgmts;
4184
0
  params.wpa_proto = wpa_s->wpa_proto;
4185
0
  wpa_s->auth_alg = params.auth_alg;
4186
0
  params.mode = ssid->mode;
4187
0
  params.bg_scan_period = ssid->bg_scan_period;
4188
#ifdef CONFIG_WEP
4189
  {
4190
    int i;
4191
4192
    for (i = 0; i < NUM_WEP_KEYS; i++) {
4193
      if (ssid->wep_key_len[i])
4194
        params.wep_key[i] = ssid->wep_key[i];
4195
      params.wep_key_len[i] = ssid->wep_key_len[i];
4196
    }
4197
    params.wep_tx_keyidx = ssid->wep_tx_keyidx;
4198
  }
4199
#endif /* CONFIG_WEP */
4200
4201
0
  if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) &&
4202
0
      (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4203
0
       params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
4204
0
       (params.allowed_key_mgmts &
4205
0
        (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK)))) {
4206
0
    params.passphrase = ssid->passphrase;
4207
0
    if (wpa_supplicant_get_psk(wpa_s, bss, ssid, psk) == 0)
4208
0
      params.psk = psk;
4209
0
  }
4210
4211
0
  if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X) &&
4212
0
      (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4213
0
       params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4214
0
       params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
4215
0
       params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192))
4216
0
    params.req_handshake_offload = 1;
4217
4218
0
  if (wpa_s->conf->key_mgmt_offload) {
4219
0
    if (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4220
0
        params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4221
0
        params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
4222
0
        params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
4223
0
      params.req_key_mgmt_offload =
4224
0
        ssid->proactive_key_caching < 0 ?
4225
0
        wpa_s->conf->okc : ssid->proactive_key_caching;
4226
0
    else
4227
0
      params.req_key_mgmt_offload = 1;
4228
4229
0
    if ((wpa_key_mgmt_wpa_psk_no_sae(params.key_mgmt_suite) ||
4230
0
         wpa_key_mgmt_wpa_psk_no_sae(params.allowed_key_mgmts)) &&
4231
0
        wpa_supplicant_get_psk(wpa_s, bss, ssid, psk) == 0)
4232
0
      params.psk = psk;
4233
0
  }
4234
4235
0
  params.drop_unencrypted = use_crypt;
4236
4237
0
  params.mgmt_frame_protection = wpas_get_ssid_pmf(wpa_s, ssid);
4238
0
  if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
4239
0
    const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
4240
0
    struct wpa_ie_data ie;
4241
0
    if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
4242
0
        ie.capabilities &
4243
0
        (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
4244
0
      wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
4245
0
        "MFP: require MFP");
4246
0
      params.mgmt_frame_protection =
4247
0
        MGMT_FRAME_PROTECTION_REQUIRED;
4248
#ifdef CONFIG_OWE
4249
    } else if (!rsn && (ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
4250
         !ssid->owe_only) {
4251
      params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
4252
#endif /* CONFIG_OWE */
4253
0
    }
4254
0
  }
4255
4256
0
  params.p2p = ssid->p2p_group;
4257
4258
0
  if (wpa_s->p2pdev->set_sta_uapsd)
4259
0
    params.uapsd = wpa_s->p2pdev->sta_uapsd;
4260
0
  else
4261
0
    params.uapsd = -1;
4262
4263
#ifdef CONFIG_HT_OVERRIDES
4264
  os_memset(&htcaps, 0, sizeof(htcaps));
4265
  os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
4266
  params.htcaps = (u8 *) &htcaps;
4267
  params.htcaps_mask = (u8 *) &htcaps_mask;
4268
  wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
4269
#endif /* CONFIG_HT_OVERRIDES */
4270
#ifdef CONFIG_VHT_OVERRIDES
4271
  os_memset(&vhtcaps, 0, sizeof(vhtcaps));
4272
  os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
4273
  params.vhtcaps = &vhtcaps;
4274
  params.vhtcaps_mask = &vhtcaps_mask;
4275
  wpa_supplicant_apply_vht_overrides(wpa_s, ssid, &params);
4276
#endif /* CONFIG_VHT_OVERRIDES */
4277
#ifdef CONFIG_HE_OVERRIDES
4278
  wpa_supplicant_apply_he_overrides(wpa_s, ssid, &params);
4279
#endif /* CONFIG_HE_OVERRIDES */
4280
0
  wpa_supplicant_apply_eht_overrides(wpa_s, ssid, &params);
4281
4282
#ifdef CONFIG_P2P
4283
  /*
4284
   * If multi-channel concurrency is not supported, check for any
4285
   * frequency conflict. In case of any frequency conflict, remove the
4286
   * least prioritized connection.
4287
   */
4288
  if (wpa_s->num_multichan_concurrent < 2) {
4289
    int freq, num;
4290
    num = get_shared_radio_freqs(wpa_s, &freq, 1, false);
4291
    if (num > 0 && freq > 0 && freq != params.freq.freq) {
4292
      wpa_printf(MSG_DEBUG,
4293
           "Assoc conflicting freq found (%d != %d)",
4294
           freq, params.freq.freq);
4295
      if (wpas_p2p_handle_frequency_conflicts(
4296
            wpa_s, params.freq.freq, ssid) < 0) {
4297
        wpas_connect_work_done(wpa_s);
4298
        os_free(wpa_ie);
4299
        return;
4300
      }
4301
    }
4302
  }
4303
#endif /* CONFIG_P2P */
4304
4305
0
  if (wpa_s->reassoc_same_ess && !is_zero_ether_addr(prev_bssid) &&
4306
0
      old_ssid)
4307
0
    params.prev_bssid = prev_bssid;
4308
4309
#ifdef CONFIG_SAE
4310
  params.sae_pwe = wpa_s->conf->sae_pwe;
4311
#endif /* CONFIG_SAE */
4312
4313
0
  ret = wpa_drv_associate(wpa_s, &params);
4314
0
  forced_memzero(psk, sizeof(psk));
4315
0
  os_free(wpa_ie);
4316
0
  if (ret < 0) {
4317
0
    wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
4318
0
      "failed");
4319
0
    if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_VALID_ERROR_CODES) {
4320
      /*
4321
       * The driver is known to mean what is saying, so we
4322
       * can stop right here; the association will not
4323
       * succeed.
4324
       */
4325
0
      wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
4326
0
      wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
4327
0
      os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
4328
0
      return;
4329
0
    }
4330
    /* try to continue anyway; new association will be tried again
4331
     * after timeout */
4332
0
    assoc_failed = 1;
4333
0
  }
4334
4335
0
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
4336
    /* Set the key after the association just in case association
4337
     * cleared the previously configured key. */
4338
0
    wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
4339
    /* No need to timeout authentication since there is no key
4340
     * management. */
4341
0
    wpa_supplicant_cancel_auth_timeout(wpa_s);
4342
0
    wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
4343
#ifdef CONFIG_IBSS_RSN
4344
  } else if (ssid->mode == WPAS_MODE_IBSS &&
4345
       wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
4346
       wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
4347
    /*
4348
     * RSN IBSS authentication is per-STA and we can disable the
4349
     * per-BSSID authentication.
4350
     */
4351
    wpa_supplicant_cancel_auth_timeout(wpa_s);
4352
#endif /* CONFIG_IBSS_RSN */
4353
0
  } else {
4354
    /* Timeout for IEEE 802.11 authentication and association */
4355
0
    int timeout = 60;
4356
4357
0
    if (assoc_failed) {
4358
      /* give IBSS a bit more time */
4359
0
      timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
4360
0
    } else if (wpa_s->conf->ap_scan == 1) {
4361
      /* give IBSS a bit more time */
4362
0
      timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
4363
0
    }
4364
0
    wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
4365
0
  }
4366
4367
#ifdef CONFIG_WEP
4368
  if (wep_keys_set &&
4369
      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
4370
    /* Set static WEP keys again */
4371
    wpa_set_wep_keys(wpa_s, ssid);
4372
  }
4373
#endif /* CONFIG_WEP */
4374
4375
0
  if (old_ssid && old_ssid != ssid) {
4376
    /*
4377
     * Do not allow EAP session resumption between different
4378
     * network configurations.
4379
     */
4380
0
    eapol_sm_invalidate_cached_session(wpa_s->eapol);
4381
0
  }
4382
4383
0
  if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
4384
0
    wpa_s->current_bss = bss;
4385
0
#ifdef CONFIG_HS20
4386
0
    hs20_configure_frame_filters(wpa_s);
4387
0
#endif /* CONFIG_HS20 */
4388
0
  }
4389
4390
0
  wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
4391
0
  wpa_supplicant_initiate_eapol(wpa_s);
4392
0
  if (old_ssid != wpa_s->current_ssid)
4393
0
    wpas_notify_network_changed(wpa_s);
4394
0
  if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
4395
0
    wpas_notify_auth_changed(wpa_s);
4396
0
}
4397
4398
4399
static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
4400
              const u8 *addr)
4401
0
{
4402
0
  struct wpa_ssid *old_ssid;
4403
4404
0
  wpas_connect_work_done(wpa_s);
4405
0
  wpa_clear_keys(wpa_s, addr);
4406
0
  old_ssid = wpa_s->current_ssid;
4407
0
  wpa_supplicant_mark_disassoc(wpa_s);
4408
0
  wpa_sm_set_config(wpa_s->wpa, NULL);
4409
0
  eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
4410
0
  if (old_ssid != wpa_s->current_ssid)
4411
0
    wpas_notify_network_changed(wpa_s);
4412
4413
0
  wpas_scs_deinit(wpa_s);
4414
0
  wpas_dscp_deinit(wpa_s);
4415
0
  eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
4416
0
}
4417
4418
4419
/**
4420
 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
4421
 * @wpa_s: Pointer to wpa_supplicant data
4422
 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
4423
 *
4424
 * This function is used to request %wpa_supplicant to deauthenticate from the
4425
 * current AP.
4426
 */
4427
void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
4428
           u16 reason_code)
4429
0
{
4430
0
  u8 *addr = NULL;
4431
0
  union wpa_event_data event;
4432
0
  int zero_addr = 0;
4433
4434
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
4435
0
    " pending_bssid=" MACSTR " reason=%d (%s) state=%s",
4436
0
    MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
4437
0
    reason_code, reason2str(reason_code),
4438
0
    wpa_supplicant_state_txt(wpa_s->wpa_state));
4439
4440
0
  if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
4441
0
      (wpa_s->wpa_state == WPA_AUTHENTICATING ||
4442
0
       wpa_s->wpa_state == WPA_ASSOCIATING))
4443
0
    addr = wpa_s->pending_bssid;
4444
0
  else if (!is_zero_ether_addr(wpa_s->bssid))
4445
0
    addr = wpa_s->bssid;
4446
0
  else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
4447
    /*
4448
     * When using driver-based BSS selection, we may not know the
4449
     * BSSID with which we are currently trying to associate. We
4450
     * need to notify the driver of this disconnection even in such
4451
     * a case, so use the all zeros address here.
4452
     */
4453
0
    addr = wpa_s->bssid;
4454
0
    zero_addr = 1;
4455
0
  }
4456
4457
0
  if (wpa_s->enabled_4addr_mode && wpa_drv_set_4addr_mode(wpa_s, 0) == 0)
4458
0
    wpa_s->enabled_4addr_mode = 0;
4459
4460
#ifdef CONFIG_TDLS
4461
  wpa_tdls_teardown_peers(wpa_s->wpa);
4462
#endif /* CONFIG_TDLS */
4463
4464
#ifdef CONFIG_MESH
4465
  if (wpa_s->ifmsh) {
4466
    struct mesh_conf *mconf;
4467
4468
    mconf = wpa_s->ifmsh->mconf;
4469
    wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_REMOVED "%s",
4470
      wpa_s->ifname);
4471
    wpas_notify_mesh_group_removed(wpa_s, mconf->meshid,
4472
                 mconf->meshid_len, reason_code);
4473
    wpa_supplicant_leave_mesh(wpa_s, true);
4474
  }
4475
#endif /* CONFIG_MESH */
4476
4477
0
  if (addr) {
4478
0
    wpa_drv_deauthenticate(wpa_s, addr, reason_code);
4479
0
    os_memset(&event, 0, sizeof(event));
4480
0
    event.deauth_info.reason_code = reason_code;
4481
0
    event.deauth_info.locally_generated = 1;
4482
0
    wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
4483
0
    if (zero_addr)
4484
0
      addr = NULL;
4485
0
  }
4486
4487
0
  wpa_supplicant_clear_connection(wpa_s, addr);
4488
0
}
4489
4490
4491
void wpa_supplicant_reconnect(struct wpa_supplicant *wpa_s)
4492
0
{
4493
0
  wpa_s->own_reconnect_req = 1;
4494
0
  wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
4495
4496
0
}
4497
4498
4499
static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
4500
                struct wpa_ssid *ssid)
4501
0
{
4502
0
  if (!ssid || !ssid->disabled || ssid->disabled == 2)
4503
0
    return;
4504
4505
0
  ssid->disabled = 0;
4506
0
  ssid->owe_transition_bss_select_count = 0;
4507
0
  wpas_clear_temp_disabled(wpa_s, ssid, 1);
4508
0
  wpas_notify_network_enabled_changed(wpa_s, ssid);
4509
4510
  /*
4511
   * Try to reassociate since there is no current configuration and a new
4512
   * network was made available.
4513
   */
4514
0
  if (!wpa_s->current_ssid && !wpa_s->disconnected)
4515
0
    wpa_s->reassociate = 1;
4516
0
}
4517
4518
4519
/**
4520
 * wpa_supplicant_add_network - Add a new network
4521
 * @wpa_s: wpa_supplicant structure for a network interface
4522
 * Returns: The new network configuration or %NULL if operation failed
4523
 *
4524
 * This function performs the following operations:
4525
 * 1. Adds a new network.
4526
 * 2. Send network addition notification.
4527
 * 3. Marks the network disabled.
4528
 * 4. Set network default parameters.
4529
 */
4530
struct wpa_ssid * wpa_supplicant_add_network(struct wpa_supplicant *wpa_s)
4531
0
{
4532
0
  struct wpa_ssid *ssid;
4533
4534
0
  ssid = wpa_config_add_network(wpa_s->conf);
4535
0
  if (!ssid)
4536
0
    return NULL;
4537
0
  wpas_notify_network_added(wpa_s, ssid);
4538
0
  ssid->disabled = 1;
4539
0
  wpa_config_set_network_defaults(ssid);
4540
4541
0
  return ssid;
4542
0
}
4543
4544
4545
/**
4546
 * wpa_supplicant_remove_network - Remove a configured network based on id
4547
 * @wpa_s: wpa_supplicant structure for a network interface
4548
 * @id: Unique network id to search for
4549
 * Returns: 0 on success, or -1 if the network was not found, -2 if the network
4550
 * could not be removed
4551
 *
4552
 * This function performs the following operations:
4553
 * 1. Removes the network.
4554
 * 2. Send network removal notification.
4555
 * 3. Update internal state machines.
4556
 * 4. Stop any running sched scans.
4557
 */
4558
int wpa_supplicant_remove_network(struct wpa_supplicant *wpa_s, int id)
4559
0
{
4560
0
  struct wpa_ssid *ssid, *prev = wpa_s->current_ssid;
4561
0
  int was_disabled;
4562
4563
0
  ssid = wpa_config_get_network(wpa_s->conf, id);
4564
0
  if (!ssid)
4565
0
    return -1;
4566
0
  wpas_notify_network_removed(wpa_s, ssid);
4567
4568
0
  if (ssid == prev || !prev) {
4569
#ifdef CONFIG_SME
4570
    wpa_s->sme.prev_bssid_set = 0;
4571
#endif /* CONFIG_SME */
4572
    /*
4573
     * Invalidate the EAP session cache if the current or
4574
     * previously used network is removed.
4575
     */
4576
0
    eapol_sm_invalidate_cached_session(wpa_s->eapol);
4577
0
  }
4578
4579
0
  if (ssid == prev) {
4580
0
    wpa_sm_set_config(wpa_s->wpa, NULL);
4581
0
    eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
4582
4583
0
    if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4584
0
      wpa_s->own_disconnect_req = 1;
4585
0
    wpa_supplicant_deauthenticate(wpa_s,
4586
0
                WLAN_REASON_DEAUTH_LEAVING);
4587
0
  }
4588
4589
0
  was_disabled = ssid->disabled;
4590
4591
0
  if (wpa_config_remove_network(wpa_s->conf, id) < 0)
4592
0
    return -2;
4593
4594
0
  if (!was_disabled && wpa_s->sched_scanning) {
4595
0
    wpa_printf(MSG_DEBUG,
4596
0
         "Stop ongoing sched_scan to remove network from filters");
4597
0
    wpa_supplicant_cancel_sched_scan(wpa_s);
4598
0
    wpa_supplicant_req_scan(wpa_s, 0, 0);
4599
0
  }
4600
4601
0
  return 0;
4602
0
}
4603
4604
4605
/**
4606
 * wpa_supplicant_remove_all_networks - Remove all configured networks
4607
 * @wpa_s: wpa_supplicant structure for a network interface
4608
 * Returns: 0 on success (errors are currently ignored)
4609
 *
4610
 * This function performs the following operations:
4611
 * 1. Remove all networks.
4612
 * 2. Send network removal notifications.
4613
 * 3. Update internal state machines.
4614
 * 4. Stop any running sched scans.
4615
 */
4616
int wpa_supplicant_remove_all_networks(struct wpa_supplicant *wpa_s)
4617
0
{
4618
0
  struct wpa_ssid *ssid;
4619
4620
0
  if (wpa_s->sched_scanning)
4621
0
    wpa_supplicant_cancel_sched_scan(wpa_s);
4622
4623
0
  eapol_sm_invalidate_cached_session(wpa_s->eapol);
4624
0
  if (wpa_s->current_ssid) {
4625
#ifdef CONFIG_SME
4626
    wpa_s->sme.prev_bssid_set = 0;
4627
#endif /* CONFIG_SME */
4628
0
    wpa_sm_set_config(wpa_s->wpa, NULL);
4629
0
    eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
4630
0
    if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4631
0
      wpa_s->own_disconnect_req = 1;
4632
0
    wpa_supplicant_deauthenticate(
4633
0
      wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4634
0
  }
4635
0
  ssid = wpa_s->conf->ssid;
4636
0
  while (ssid) {
4637
0
    struct wpa_ssid *remove_ssid = ssid;
4638
0
    int id;
4639
4640
0
    id = ssid->id;
4641
0
    ssid = ssid->next;
4642
0
    wpas_notify_network_removed(wpa_s, remove_ssid);
4643
0
    wpa_config_remove_network(wpa_s->conf, id);
4644
0
  }
4645
0
  return 0;
4646
0
}
4647
4648
4649
/**
4650
 * wpa_supplicant_enable_network - Mark a configured network as enabled
4651
 * @wpa_s: wpa_supplicant structure for a network interface
4652
 * @ssid: wpa_ssid structure for a configured network or %NULL
4653
 *
4654
 * Enables the specified network or all networks if no network specified.
4655
 */
4656
void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
4657
           struct wpa_ssid *ssid)
4658
0
{
4659
0
  if (ssid == NULL) {
4660
0
    for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
4661
0
      wpa_supplicant_enable_one_network(wpa_s, ssid);
4662
0
  } else
4663
0
    wpa_supplicant_enable_one_network(wpa_s, ssid);
4664
4665
0
  if (wpa_s->reassociate && !wpa_s->disconnected &&
4666
0
      (!wpa_s->current_ssid ||
4667
0
       wpa_s->wpa_state == WPA_DISCONNECTED ||
4668
0
       wpa_s->wpa_state == WPA_SCANNING)) {
4669
0
    if (wpa_s->sched_scanning) {
4670
0
      wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
4671
0
           "new network to scan filters");
4672
0
      wpa_supplicant_cancel_sched_scan(wpa_s);
4673
0
    }
4674
4675
0
    if (wpa_supplicant_fast_associate(wpa_s) != 1) {
4676
0
      wpa_s->scan_req = NORMAL_SCAN_REQ;
4677
0
      wpa_supplicant_req_scan(wpa_s, 0, 0);
4678
0
    }
4679
0
  }
4680
0
}
4681
4682
4683
/**
4684
 * wpa_supplicant_disable_network - Mark a configured network as disabled
4685
 * @wpa_s: wpa_supplicant structure for a network interface
4686
 * @ssid: wpa_ssid structure for a configured network or %NULL
4687
 *
4688
 * Disables the specified network or all networks if no network specified.
4689
 */
4690
void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
4691
            struct wpa_ssid *ssid)
4692
0
{
4693
0
  struct wpa_ssid *other_ssid;
4694
0
  int was_disabled;
4695
4696
0
  if (ssid == NULL) {
4697
0
    if (wpa_s->sched_scanning)
4698
0
      wpa_supplicant_cancel_sched_scan(wpa_s);
4699
4700
0
    for (other_ssid = wpa_s->conf->ssid; other_ssid;
4701
0
         other_ssid = other_ssid->next) {
4702
0
      was_disabled = other_ssid->disabled;
4703
0
      if (was_disabled == 2)
4704
0
        continue; /* do not change persistent P2P group
4705
             * data */
4706
4707
0
      other_ssid->disabled = 1;
4708
4709
0
      if (was_disabled != other_ssid->disabled)
4710
0
        wpas_notify_network_enabled_changed(
4711
0
          wpa_s, other_ssid);
4712
0
    }
4713
0
    if (wpa_s->current_ssid) {
4714
0
      if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4715
0
        wpa_s->own_disconnect_req = 1;
4716
0
      wpa_supplicant_deauthenticate(
4717
0
        wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4718
0
    }
4719
0
  } else if (ssid->disabled != 2) {
4720
0
    if (ssid == wpa_s->current_ssid) {
4721
0
      if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4722
0
        wpa_s->own_disconnect_req = 1;
4723
0
      wpa_supplicant_deauthenticate(
4724
0
        wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4725
0
    }
4726
4727
0
    was_disabled = ssid->disabled;
4728
4729
0
    ssid->disabled = 1;
4730
4731
0
    if (was_disabled != ssid->disabled) {
4732
0
      wpas_notify_network_enabled_changed(wpa_s, ssid);
4733
0
      if (wpa_s->sched_scanning) {
4734
0
        wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
4735
0
             "to remove network from filters");
4736
0
        wpa_supplicant_cancel_sched_scan(wpa_s);
4737
0
        wpa_supplicant_req_scan(wpa_s, 0, 0);
4738
0
      }
4739
0
    }
4740
0
  }
4741
0
}
4742
4743
4744
/**
4745
 * wpa_supplicant_select_network - Attempt association with a network
4746
 * @wpa_s: wpa_supplicant structure for a network interface
4747
 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
4748
 */
4749
void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
4750
           struct wpa_ssid *ssid)
4751
0
{
4752
4753
0
  struct wpa_ssid *other_ssid;
4754
0
  int disconnected = 0;
4755
4756
0
  if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
4757
0
    if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4758
0
      wpa_s->own_disconnect_req = 1;
4759
0
    wpa_supplicant_deauthenticate(
4760
0
      wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4761
0
    disconnected = 1;
4762
0
  }
4763
4764
0
  if (ssid)
4765
0
    wpas_clear_temp_disabled(wpa_s, ssid, 1);
4766
4767
  /*
4768
   * Mark all other networks disabled or mark all networks enabled if no
4769
   * network specified.
4770
   */
4771
0
  for (other_ssid = wpa_s->conf->ssid; other_ssid;
4772
0
       other_ssid = other_ssid->next) {
4773
0
    int was_disabled = other_ssid->disabled;
4774
0
    if (was_disabled == 2)
4775
0
      continue; /* do not change persistent P2P group data */
4776
4777
0
    other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
4778
0
    if (was_disabled && !other_ssid->disabled)
4779
0
      wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
4780
4781
0
    if (was_disabled != other_ssid->disabled)
4782
0
      wpas_notify_network_enabled_changed(wpa_s, other_ssid);
4783
0
  }
4784
4785
0
  if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid &&
4786
0
      wpa_s->wpa_state >= WPA_AUTHENTICATING) {
4787
    /* We are already associated with the selected network */
4788
0
    wpa_printf(MSG_DEBUG, "Already associated with the "
4789
0
         "selected network - do nothing");
4790
0
    return;
4791
0
  }
4792
4793
0
  if (ssid) {
4794
0
    wpa_s->current_ssid = ssid;
4795
0
    eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
4796
0
    wpa_s->connect_without_scan =
4797
0
      (ssid->mode == WPAS_MODE_MESH) ? ssid : NULL;
4798
4799
    /*
4800
     * Don't optimize next scan freqs since a new ESS has been
4801
     * selected.
4802
     */
4803
0
    os_free(wpa_s->next_scan_freqs);
4804
0
    wpa_s->next_scan_freqs = NULL;
4805
0
  } else {
4806
0
    wpa_s->connect_without_scan = NULL;
4807
0
  }
4808
4809
0
  wpa_s->disconnected = 0;
4810
0
  wpa_s->reassociate = 1;
4811
0
  wpa_s_clear_sae_rejected(wpa_s);
4812
0
  wpa_s->last_owe_group = 0;
4813
0
  if (ssid) {
4814
0
    ssid->owe_transition_bss_select_count = 0;
4815
0
    wpa_s_setup_sae_pt(wpa_s->conf, ssid, false);
4816
0
  }
4817
4818
0
  if (wpa_s->connect_without_scan ||
4819
0
      wpa_supplicant_fast_associate(wpa_s) != 1) {
4820
0
    wpa_s->scan_req = NORMAL_SCAN_REQ;
4821
0
    wpas_scan_reset_sched_scan(wpa_s);
4822
0
    wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
4823
0
  }
4824
4825
0
  if (ssid)
4826
0
    wpas_notify_network_selected(wpa_s, ssid);
4827
0
}
4828
4829
4830
/**
4831
 * wpas_remove_cred - Remove the specified credential and all the network
4832
 * entries created based on the removed credential
4833
 * @wpa_s: wpa_supplicant structure for a network interface
4834
 * @cred: The credential to remove
4835
 * Returns: 0 on success, -1 on failure
4836
 */
4837
int wpas_remove_cred(struct wpa_supplicant *wpa_s, struct wpa_cred *cred)
4838
0
{
4839
0
  struct wpa_ssid *ssid, *next;
4840
0
  int id;
4841
4842
0
  if (!cred) {
4843
0
    wpa_printf(MSG_DEBUG, "Could not find cred");
4844
0
    return -1;
4845
0
  }
4846
4847
0
  id = cred->id;
4848
0
  if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
4849
0
    wpa_printf(MSG_DEBUG, "Could not find cred %d", id);
4850
0
    return -1;
4851
0
  }
4852
4853
0
  wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
4854
4855
  /* Remove any network entry created based on the removed credential */
4856
0
  ssid = wpa_s->conf->ssid;
4857
0
  while (ssid) {
4858
0
    next = ssid->next;
4859
4860
0
    if (ssid->parent_cred == cred) {
4861
0
      wpa_printf(MSG_DEBUG,
4862
0
           "Remove network id %d since it used the removed credential",
4863
0
           ssid->id);
4864
0
      if (wpa_supplicant_remove_network(wpa_s, ssid->id) ==
4865
0
          -1) {
4866
0
        wpa_printf(MSG_DEBUG,
4867
0
             "Could not find network id=%d",
4868
0
             ssid->id);
4869
0
      }
4870
0
    }
4871
4872
0
    ssid = next;
4873
0
  }
4874
4875
0
  return 0;
4876
0
}
4877
4878
4879
/**
4880
 * wpas_remove_cred - Remove all the Interworking credentials
4881
 * @wpa_s: wpa_supplicant structure for a network interface
4882
 * Returns: 0 on success, -1 on failure
4883
 */
4884
int wpas_remove_all_creds(struct wpa_supplicant *wpa_s)
4885
0
{
4886
0
  int res, ret = 0;
4887
0
  struct wpa_cred *cred, *prev;
4888
4889
0
  cred = wpa_s->conf->cred;
4890
0
  while (cred) {
4891
0
    prev = cred;
4892
0
    cred = cred->next;
4893
0
    res = wpas_remove_cred(wpa_s, prev);
4894
0
    if (res < 0) {
4895
0
      wpa_printf(MSG_DEBUG,
4896
0
           "Removal of all credentials failed - failed to remove credential id=%d",
4897
0
           prev->id);
4898
0
      ret = -1;
4899
0
    }
4900
0
  }
4901
4902
0
  return ret;
4903
0
}
4904
4905
4906
/**
4907
 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
4908
 * @wpa_s: wpa_supplicant structure for a network interface
4909
 * @pkcs11_engine_path: PKCS #11 engine path or NULL
4910
 * @pkcs11_module_path: PKCS #11 module path or NULL
4911
 * Returns: 0 on success; -1 on failure
4912
 *
4913
 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
4914
 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
4915
 * module path fails the paths will be reset to the default value (NULL).
4916
 */
4917
int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
4918
             const char *pkcs11_engine_path,
4919
             const char *pkcs11_module_path)
4920
0
{
4921
0
  char *pkcs11_engine_path_copy = NULL;
4922
0
  char *pkcs11_module_path_copy = NULL;
4923
4924
0
  if (pkcs11_engine_path != NULL) {
4925
0
    pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
4926
0
    if (pkcs11_engine_path_copy == NULL)
4927
0
      return -1;
4928
0
  }
4929
0
  if (pkcs11_module_path != NULL) {
4930
0
    pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
4931
0
    if (pkcs11_module_path_copy == NULL) {
4932
0
      os_free(pkcs11_engine_path_copy);
4933
0
      return -1;
4934
0
    }
4935
0
  }
4936
4937
0
  os_free(wpa_s->conf->pkcs11_engine_path);
4938
0
  os_free(wpa_s->conf->pkcs11_module_path);
4939
0
  wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
4940
0
  wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
4941
4942
0
  wpa_sm_set_eapol(wpa_s->wpa, NULL);
4943
0
  eapol_sm_deinit(wpa_s->eapol);
4944
0
  wpa_s->eapol = NULL;
4945
0
  if (wpa_supplicant_init_eapol(wpa_s)) {
4946
    /* Error -> Reset paths to the default value (NULL) once. */
4947
0
    if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
4948
0
      wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
4949
0
                     NULL);
4950
4951
0
    return -1;
4952
0
  }
4953
0
  wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
4954
4955
0
  return 0;
4956
0
}
4957
4958
4959
/**
4960
 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
4961
 * @wpa_s: wpa_supplicant structure for a network interface
4962
 * @ap_scan: AP scan mode
4963
 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
4964
 *
4965
 */
4966
int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
4967
0
{
4968
4969
0
  int old_ap_scan;
4970
4971
0
  if (ap_scan < 0 || ap_scan > 2)
4972
0
    return -1;
4973
4974
0
  if (ap_scan == 2 && os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
4975
0
    wpa_printf(MSG_INFO,
4976
0
         "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
4977
0
  }
4978
4979
#ifdef ANDROID
4980
  if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
4981
      wpa_s->wpa_state >= WPA_ASSOCIATING &&
4982
      wpa_s->wpa_state < WPA_COMPLETED) {
4983
    wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
4984
         "associating", wpa_s->conf->ap_scan, ap_scan);
4985
    return 0;
4986
  }
4987
#endif /* ANDROID */
4988
4989
0
  old_ap_scan = wpa_s->conf->ap_scan;
4990
0
  wpa_s->conf->ap_scan = ap_scan;
4991
4992
0
  if (old_ap_scan != wpa_s->conf->ap_scan)
4993
0
    wpas_notify_ap_scan_changed(wpa_s);
4994
4995
0
  return 0;
4996
0
}
4997
4998
4999
/**
5000
 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
5001
 * @wpa_s: wpa_supplicant structure for a network interface
5002
 * @expire_age: Expiration age in seconds
5003
 * Returns: 0 if succeed or -1 if expire_age has an invalid value
5004
 *
5005
 */
5006
int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
5007
            unsigned int bss_expire_age)
5008
0
{
5009
0
  if (bss_expire_age < 10) {
5010
0
    wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
5011
0
      bss_expire_age);
5012
0
    return -1;
5013
0
  }
5014
0
  wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
5015
0
    bss_expire_age);
5016
0
  wpa_s->conf->bss_expiration_age = bss_expire_age;
5017
5018
0
  return 0;
5019
0
}
5020
5021
5022
/**
5023
 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
5024
 * @wpa_s: wpa_supplicant structure for a network interface
5025
 * @expire_count: number of scans after which an unseen BSS is reclaimed
5026
 * Returns: 0 if succeed or -1 if expire_count has an invalid value
5027
 *
5028
 */
5029
int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
5030
              unsigned int bss_expire_count)
5031
0
{
5032
0
  if (bss_expire_count < 1) {
5033
0
    wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
5034
0
      bss_expire_count);
5035
0
    return -1;
5036
0
  }
5037
0
  wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
5038
0
    bss_expire_count);
5039
0
  wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
5040
5041
0
  return 0;
5042
0
}
5043
5044
5045
/**
5046
 * wpa_supplicant_set_scan_interval - Set scan interval
5047
 * @wpa_s: wpa_supplicant structure for a network interface
5048
 * @scan_interval: scan interval in seconds
5049
 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
5050
 *
5051
 */
5052
int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
5053
             int scan_interval)
5054
0
{
5055
0
  if (scan_interval < 0) {
5056
0
    wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
5057
0
      scan_interval);
5058
0
    return -1;
5059
0
  }
5060
0
  wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
5061
0
    scan_interval);
5062
0
  wpa_supplicant_update_scan_int(wpa_s, scan_interval);
5063
5064
0
  return 0;
5065
0
}
5066
5067
5068
/**
5069
 * wpa_supplicant_set_debug_params - Set global debug params
5070
 * @global: wpa_global structure
5071
 * @debug_level: debug level
5072
 * @debug_timestamp: determines if show timestamp in debug data
5073
 * @debug_show_keys: determines if show keys in debug data
5074
 * Returns: 0 if succeed or -1 if debug_level has wrong value
5075
 */
5076
int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
5077
            int debug_timestamp, int debug_show_keys)
5078
0
{
5079
5080
0
  int old_level, old_timestamp, old_show_keys;
5081
5082
  /* check for allowed debuglevels */
5083
0
  if (debug_level != MSG_EXCESSIVE &&
5084
0
      debug_level != MSG_MSGDUMP &&
5085
0
      debug_level != MSG_DEBUG &&
5086
0
      debug_level != MSG_INFO &&
5087
0
      debug_level != MSG_WARNING &&
5088
0
      debug_level != MSG_ERROR)
5089
0
    return -1;
5090
5091
0
  old_level = wpa_debug_level;
5092
0
  old_timestamp = wpa_debug_timestamp;
5093
0
  old_show_keys = wpa_debug_show_keys;
5094
5095
0
  wpa_debug_level = debug_level;
5096
0
  wpa_debug_timestamp = debug_timestamp ? 1 : 0;
5097
0
  wpa_debug_show_keys = debug_show_keys ? 1 : 0;
5098
5099
0
  if (wpa_debug_level != old_level)
5100
0
    wpas_notify_debug_level_changed(global);
5101
0
  if (wpa_debug_timestamp != old_timestamp)
5102
0
    wpas_notify_debug_timestamp_changed(global);
5103
0
  if (wpa_debug_show_keys != old_show_keys)
5104
0
    wpas_notify_debug_show_keys_changed(global);
5105
5106
0
  return 0;
5107
0
}
5108
5109
5110
#ifdef CONFIG_OWE
5111
static int owe_trans_ssid_match(struct wpa_supplicant *wpa_s, const u8 *bssid,
5112
        const u8 *entry_ssid, size_t entry_ssid_len)
5113
{
5114
  const u8 *owe, *pos, *end;
5115
  u8 ssid_len;
5116
  struct wpa_bss *bss;
5117
5118
  /* Check network profile SSID aganst the SSID in the
5119
   * OWE Transition Mode element. */
5120
5121
  bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
5122
  if (!bss)
5123
    return 0;
5124
5125
  owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
5126
  if (!owe)
5127
    return 0;
5128
5129
  pos = owe + 6;
5130
  end = owe + 2 + owe[1];
5131
5132
  if (end - pos < ETH_ALEN + 1)
5133
    return 0;
5134
  pos += ETH_ALEN;
5135
  ssid_len = *pos++;
5136
  if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
5137
    return 0;
5138
5139
  return entry_ssid_len == ssid_len &&
5140
    os_memcmp(pos, entry_ssid, ssid_len) == 0;
5141
}
5142
#endif /* CONFIG_OWE */
5143
5144
5145
/**
5146
 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
5147
 * @wpa_s: Pointer to wpa_supplicant data
5148
 * Returns: A pointer to the current network structure or %NULL on failure
5149
 */
5150
struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
5151
0
{
5152
0
  struct wpa_ssid *entry;
5153
0
  u8 ssid[SSID_MAX_LEN];
5154
0
  int res;
5155
0
  size_t ssid_len;
5156
0
  u8 bssid[ETH_ALEN];
5157
0
  int wired;
5158
5159
0
  res = wpa_drv_get_ssid(wpa_s, ssid);
5160
0
  if (res < 0) {
5161
0
    wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
5162
0
      "driver");
5163
0
    return NULL;
5164
0
  }
5165
0
  ssid_len = res;
5166
5167
0
  if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
5168
0
    wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
5169
0
      "driver");
5170
0
    return NULL;
5171
0
  }
5172
5173
0
  wired = wpa_s->conf->ap_scan == 0 &&
5174
0
    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
5175
5176
0
  entry = wpa_s->conf->ssid;
5177
0
  while (entry) {
5178
0
    if (!wpas_network_disabled(wpa_s, entry) &&
5179
0
        ((ssid_len == entry->ssid_len &&
5180
0
          (!entry->ssid ||
5181
0
           os_memcmp(ssid, entry->ssid, ssid_len) == 0)) ||
5182
0
         wired) &&
5183
0
        (!entry->bssid_set ||
5184
0
         os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
5185
0
      return entry;
5186
#ifdef CONFIG_WPS
5187
    if (!wpas_network_disabled(wpa_s, entry) &&
5188
        (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
5189
        (entry->ssid == NULL || entry->ssid_len == 0) &&
5190
        (!entry->bssid_set ||
5191
         os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
5192
      return entry;
5193
#endif /* CONFIG_WPS */
5194
5195
#ifdef CONFIG_OWE
5196
    if (!wpas_network_disabled(wpa_s, entry) &&
5197
        owe_trans_ssid_match(wpa_s, bssid, entry->ssid,
5198
        entry->ssid_len) &&
5199
        (!entry->bssid_set ||
5200
         os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
5201
      return entry;
5202
#endif /* CONFIG_OWE */
5203
5204
0
    if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
5205
0
        entry->ssid_len == 0 &&
5206
0
        os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
5207
0
      return entry;
5208
5209
0
    entry = entry->next;
5210
0
  }
5211
5212
0
  return NULL;
5213
0
}
5214
5215
5216
static int select_driver(struct wpa_supplicant *wpa_s, int i)
5217
0
{
5218
0
  struct wpa_global *global = wpa_s->global;
5219
5220
0
  if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
5221
0
    global->drv_priv[i] = wpa_drivers[i]->global_init(global);
5222
0
    if (global->drv_priv[i] == NULL) {
5223
0
      wpa_printf(MSG_ERROR, "Failed to initialize driver "
5224
0
           "'%s'", wpa_drivers[i]->name);
5225
0
      return -1;
5226
0
    }
5227
0
  }
5228
5229
0
  wpa_s->driver = wpa_drivers[i];
5230
0
  wpa_s->global_drv_priv = global->drv_priv[i];
5231
5232
0
  return 0;
5233
0
}
5234
5235
5236
static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
5237
             const char *name)
5238
0
{
5239
0
  int i;
5240
0
  size_t len;
5241
0
  const char *pos, *driver = name;
5242
5243
0
  if (wpa_s == NULL)
5244
0
    return -1;
5245
5246
0
  if (wpa_drivers[0] == NULL) {
5247
0
    wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
5248
0
      "wpa_supplicant");
5249
0
    return -1;
5250
0
  }
5251
5252
0
  if (name == NULL) {
5253
    /* Default to first successful driver in the list */
5254
0
    for (i = 0; wpa_drivers[i]; i++) {
5255
0
      if (select_driver(wpa_s, i) == 0)
5256
0
        return 0;
5257
0
    }
5258
    /* Drivers have each reported failure, so no wpa_msg() here. */
5259
0
    return -1;
5260
0
  }
5261
5262
0
  do {
5263
0
    pos = os_strchr(driver, ',');
5264
0
    if (pos)
5265
0
      len = pos - driver;
5266
0
    else
5267
0
      len = os_strlen(driver);
5268
5269
0
    for (i = 0; wpa_drivers[i]; i++) {
5270
0
      if (os_strlen(wpa_drivers[i]->name) == len &&
5271
0
          os_strncmp(driver, wpa_drivers[i]->name, len) ==
5272
0
          0) {
5273
        /* First driver that succeeds wins */
5274
0
        if (select_driver(wpa_s, i) == 0)
5275
0
          return 0;
5276
0
      }
5277
0
    }
5278
5279
0
    driver = pos + 1;
5280
0
  } while (pos);
5281
5282
0
  wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
5283
0
  return -1;
5284
0
}
5285
5286
5287
/**
5288
 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
5289
 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
5290
 *  with struct wpa_driver_ops::init()
5291
 * @src_addr: Source address of the EAPOL frame
5292
 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
5293
 * @len: Length of the EAPOL data
5294
 * @encrypted: Whether the frame was encrypted
5295
 *
5296
 * This function is called for each received EAPOL frame. Most driver
5297
 * interfaces rely on more generic OS mechanism for receiving frames through
5298
 * l2_packet, but if such a mechanism is not available, the driver wrapper may
5299
 * take care of received EAPOL frames and deliver them to the core supplicant
5300
 * code by calling this function.
5301
 */
5302
void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
5303
           const u8 *buf, size_t len,
5304
           enum frame_encryption encrypted)
5305
0
{
5306
0
  struct wpa_supplicant *wpa_s = ctx;
5307
0
  const u8 *connected_addr = wpa_s->valid_links ?
5308
0
    wpa_s->ap_mld_addr : wpa_s->bssid;
5309
5310
0
  wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " (encrypted=%d)",
5311
0
    MAC2STR(src_addr), encrypted);
5312
0
  wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
5313
5314
0
  if (wpa_s->own_disconnect_req) {
5315
0
    wpa_printf(MSG_DEBUG,
5316
0
         "Drop received EAPOL frame as we are disconnecting");
5317
0
    return;
5318
0
  }
5319
5320
#ifdef CONFIG_TESTING_OPTIONS
5321
  wpa_msg_ctrl(wpa_s, MSG_INFO, "EAPOL-RX " MACSTR " %zu",
5322
         MAC2STR(src_addr), len);
5323
  if (wpa_s->ignore_auth_resp) {
5324
    wpa_printf(MSG_INFO, "RX EAPOL - ignore_auth_resp active!");
5325
    return;
5326
  }
5327
#endif /* CONFIG_TESTING_OPTIONS */
5328
5329
0
  if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5330
0
      (wpa_s->last_eapol_matches_bssid &&
5331
#ifdef CONFIG_AP
5332
       !wpa_s->ap_iface &&
5333
#endif /* CONFIG_AP */
5334
0
       os_memcmp(src_addr, connected_addr, ETH_ALEN) != 0)) {
5335
    /*
5336
     * There is possible race condition between receiving the
5337
     * association event and the EAPOL frame since they are coming
5338
     * through different paths from the driver. In order to avoid
5339
     * issues in trying to process the EAPOL frame before receiving
5340
     * association information, lets queue it for processing until
5341
     * the association event is received. This may also be needed in
5342
     * driver-based roaming case, so also use src_addr != BSSID as a
5343
     * trigger if we have previously confirmed that the
5344
     * Authenticator uses BSSID as the src_addr (which is not the
5345
     * case with wired IEEE 802.1X).
5346
     */
5347
0
    wpa_dbg(wpa_s, MSG_DEBUG,
5348
0
      "Not associated - Delay processing of received EAPOL frame (state=%s connected_addr="
5349
0
      MACSTR ")",
5350
0
      wpa_supplicant_state_txt(wpa_s->wpa_state),
5351
0
      MAC2STR(connected_addr));
5352
0
    wpabuf_free(wpa_s->pending_eapol_rx);
5353
0
    wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
5354
0
    if (wpa_s->pending_eapol_rx) {
5355
0
      os_get_reltime(&wpa_s->pending_eapol_rx_time);
5356
0
      os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
5357
0
          ETH_ALEN);
5358
0
      wpa_s->pending_eapol_encrypted = encrypted;
5359
0
    }
5360
0
    return;
5361
0
  }
5362
5363
0
  wpa_s->last_eapol_matches_bssid =
5364
0
    os_memcmp(src_addr, connected_addr, ETH_ALEN) == 0;
5365
5366
#ifdef CONFIG_AP
5367
  if (wpa_s->ap_iface) {
5368
    wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len,
5369
             encrypted);
5370
    return;
5371
  }
5372
#endif /* CONFIG_AP */
5373
5374
0
  if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
5375
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
5376
0
      "no key management is configured");
5377
0
    return;
5378
0
  }
5379
5380
0
  if (wpa_s->eapol_received == 0 &&
5381
0
      (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) ||
5382
0
       !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
5383
0
       wpa_s->wpa_state != WPA_COMPLETED) &&
5384
0
      (wpa_s->current_ssid == NULL ||
5385
0
       wpa_s->current_ssid->mode != WPAS_MODE_IBSS)) {
5386
    /* Timeout for completing IEEE 802.1X and WPA authentication */
5387
0
    int timeout = 10;
5388
5389
0
    if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
5390
0
        wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
5391
0
        wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
5392
      /* Use longer timeout for IEEE 802.1X/EAP */
5393
0
      timeout = 70;
5394
0
    }
5395
5396
#ifdef CONFIG_WPS
5397
    if (wpa_s->current_ssid && wpa_s->current_bss &&
5398
        (wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
5399
        eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
5400
      /*
5401
       * Use shorter timeout if going through WPS AP iteration
5402
       * for PIN config method with an AP that does not
5403
       * advertise Selected Registrar.
5404
       */
5405
      struct wpabuf *wps_ie;
5406
5407
      wps_ie = wpa_bss_get_vendor_ie_multi(
5408
        wpa_s->current_bss, WPS_IE_VENDOR_TYPE);
5409
      if (wps_ie &&
5410
          !wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1))
5411
        timeout = 10;
5412
      wpabuf_free(wps_ie);
5413
    }
5414
#endif /* CONFIG_WPS */
5415
5416
0
    wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
5417
0
  }
5418
0
  wpa_s->eapol_received++;
5419
5420
0
  if (wpa_s->countermeasures) {
5421
0
    wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
5422
0
      "EAPOL packet");
5423
0
    return;
5424
0
  }
5425
5426
#ifdef CONFIG_IBSS_RSN
5427
  if (wpa_s->current_ssid &&
5428
      wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
5429
    ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len,
5430
          encrypted);
5431
    return;
5432
  }
5433
#endif /* CONFIG_IBSS_RSN */
5434
5435
  /* Source address of the incoming EAPOL frame could be compared to the
5436
   * current BSSID. However, it is possible that a centralized
5437
   * Authenticator could be using another MAC address than the BSSID of
5438
   * an AP, so just allow any address to be used for now. The replies are
5439
   * still sent to the current BSSID (if available), though. */
5440
5441
0
  os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
5442
0
  if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
5443
0
      wpa_s->key_mgmt != WPA_KEY_MGMT_OWE &&
5444
0
      wpa_s->key_mgmt != WPA_KEY_MGMT_DPP &&
5445
0
      eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len,
5446
0
            encrypted) > 0)
5447
0
    return;
5448
0
  wpa_drv_poll(wpa_s);
5449
0
  if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK))
5450
0
    wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len, encrypted);
5451
0
  else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
5452
    /*
5453
     * Set portValid = true here since we are going to skip 4-way
5454
     * handshake processing which would normally set portValid. We
5455
     * need this to allow the EAPOL state machines to be completed
5456
     * without going through EAPOL-Key handshake.
5457
     */
5458
0
    eapol_sm_notify_portValid(wpa_s->eapol, true);
5459
0
  }
5460
0
}
5461
5462
5463
static void wpa_supplicant_rx_eapol_cb(void *ctx, const u8 *src_addr,
5464
               const u8 *buf, size_t len)
5465
0
{
5466
0
  wpa_supplicant_rx_eapol(ctx, src_addr, buf, len,
5467
0
        FRAME_ENCRYPTION_UNKNOWN);
5468
0
}
5469
5470
5471
static int wpas_eapol_needs_l2_packet(struct wpa_supplicant *wpa_s)
5472
0
{
5473
0
  return !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_CONTROL_PORT) ||
5474
0
    !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX);
5475
0
}
5476
5477
5478
int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
5479
0
{
5480
0
  u8 prev_mac_addr[ETH_ALEN];
5481
5482
0
  os_memcpy(prev_mac_addr, wpa_s->own_addr, ETH_ALEN);
5483
5484
0
  if ((!wpa_s->p2p_mgmt ||
5485
0
       !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
5486
0
      !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
5487
0
    l2_packet_deinit(wpa_s->l2);
5488
0
    wpa_s->l2 = l2_packet_init(wpa_s->ifname,
5489
0
             wpa_drv_get_mac_addr(wpa_s),
5490
0
             ETH_P_EAPOL,
5491
0
             wpas_eapol_needs_l2_packet(wpa_s) ?
5492
0
             wpa_supplicant_rx_eapol_cb : NULL,
5493
0
             wpa_s, 0);
5494
0
    if (wpa_s->l2 == NULL)
5495
0
      return -1;
5496
5497
0
    if (l2_packet_set_packet_filter(wpa_s->l2,
5498
0
            L2_PACKET_FILTER_PKTTYPE))
5499
0
      wpa_dbg(wpa_s, MSG_DEBUG,
5500
0
        "Failed to attach pkt_type filter");
5501
5502
0
    if (l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
5503
0
      wpa_msg(wpa_s, MSG_ERROR,
5504
0
        "Failed to get own L2 address");
5505
0
      return -1;
5506
0
    }
5507
0
  } else {
5508
0
    const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
5509
0
    if (addr)
5510
0
      os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
5511
0
  }
5512
5513
0
  wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
5514
0
  wpas_wps_update_mac_addr(wpa_s);
5515
5516
#ifdef CONFIG_FST
5517
  if (wpa_s->fst)
5518
    fst_update_mac_addr(wpa_s->fst, wpa_s->own_addr);
5519
#endif /* CONFIG_FST */
5520
5521
0
  if (os_memcmp(prev_mac_addr, wpa_s->own_addr, ETH_ALEN) != 0)
5522
0
    wpas_notify_mac_address_changed(wpa_s);
5523
5524
0
  return 0;
5525
0
}
5526
5527
5528
static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
5529
             const u8 *buf, size_t len)
5530
0
{
5531
0
  struct wpa_supplicant *wpa_s = ctx;
5532
0
  const struct l2_ethhdr *eth;
5533
5534
0
  if (len < sizeof(*eth))
5535
0
    return;
5536
0
  eth = (const struct l2_ethhdr *) buf;
5537
5538
0
  if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
5539
0
      !(eth->h_dest[0] & 0x01)) {
5540
0
    wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
5541
0
      " (bridge - not for this interface - ignore)",
5542
0
      MAC2STR(src_addr), MAC2STR(eth->h_dest));
5543
0
    return;
5544
0
  }
5545
5546
0
  wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
5547
0
    " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
5548
0
  wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
5549
0
        len - sizeof(*eth), FRAME_ENCRYPTION_UNKNOWN);
5550
0
}
5551
5552
5553
int wpa_supplicant_update_bridge_ifname(struct wpa_supplicant *wpa_s,
5554
          const char *bridge_ifname)
5555
0
{
5556
0
  if (wpa_s->wpa_state > WPA_SCANNING)
5557
0
    return -EBUSY;
5558
5559
0
  if (bridge_ifname &&
5560
0
      os_strlen(bridge_ifname) >= sizeof(wpa_s->bridge_ifname))
5561
0
    return -EINVAL;
5562
5563
0
  if (!bridge_ifname)
5564
0
    bridge_ifname = "";
5565
5566
0
  if (os_strcmp(wpa_s->bridge_ifname, bridge_ifname) == 0)
5567
0
    return 0;
5568
5569
0
  if (wpa_s->l2_br) {
5570
0
    l2_packet_deinit(wpa_s->l2_br);
5571
0
    wpa_s->l2_br = NULL;
5572
0
  }
5573
5574
0
  os_strlcpy(wpa_s->bridge_ifname, bridge_ifname,
5575
0
       sizeof(wpa_s->bridge_ifname));
5576
5577
0
  if (wpa_s->bridge_ifname[0]) {
5578
0
    wpa_dbg(wpa_s, MSG_DEBUG,
5579
0
      "Receiving packets from bridge interface '%s'",
5580
0
      wpa_s->bridge_ifname);
5581
0
    wpa_s->l2_br = l2_packet_init_bridge(
5582
0
      wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
5583
0
      ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
5584
0
    if (!wpa_s->l2_br) {
5585
0
      wpa_msg(wpa_s, MSG_ERROR,
5586
0
        "Failed to open l2_packet connection for the bridge interface '%s'",
5587
0
        wpa_s->bridge_ifname);
5588
0
      goto fail;
5589
0
    }
5590
0
  }
5591
5592
#ifdef CONFIG_TDLS
5593
  if (!wpa_s->p2p_mgmt && wpa_tdls_init(wpa_s->wpa))
5594
    goto fail;
5595
#endif /* CONFIG_TDLS */
5596
5597
0
  return 0;
5598
0
fail:
5599
0
  wpa_s->bridge_ifname[0] = 0;
5600
0
  if (wpa_s->l2_br) {
5601
0
    l2_packet_deinit(wpa_s->l2_br);
5602
0
    wpa_s->l2_br = NULL;
5603
0
  }
5604
#ifdef CONFIG_TDLS
5605
  if (!wpa_s->p2p_mgmt)
5606
    wpa_tdls_init(wpa_s->wpa);
5607
#endif /* CONFIG_TDLS */
5608
0
  return -EIO;
5609
0
}
5610
5611
5612
/**
5613
 * wpa_supplicant_driver_init - Initialize driver interface parameters
5614
 * @wpa_s: Pointer to wpa_supplicant data
5615
 * Returns: 0 on success, -1 on failure
5616
 *
5617
 * This function is called to initialize driver interface parameters.
5618
 * wpa_drv_init() must have been called before this function to initialize the
5619
 * driver interface.
5620
 */
5621
int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
5622
0
{
5623
0
  static int interface_count = 0;
5624
5625
0
  if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
5626
0
    return -1;
5627
5628
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
5629
0
    MAC2STR(wpa_s->own_addr));
5630
0
  os_memcpy(wpa_s->perm_addr, wpa_s->own_addr, ETH_ALEN);
5631
0
  wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
5632
5633
0
  if (wpa_s->bridge_ifname[0] && wpas_eapol_needs_l2_packet(wpa_s)) {
5634
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
5635
0
      "interface '%s'", wpa_s->bridge_ifname);
5636
0
    wpa_s->l2_br = l2_packet_init_bridge(
5637
0
      wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
5638
0
      ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
5639
0
    if (wpa_s->l2_br == NULL) {
5640
0
      wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
5641
0
        "connection for the bridge interface '%s'",
5642
0
        wpa_s->bridge_ifname);
5643
0
      return -1;
5644
0
    }
5645
0
  }
5646
5647
0
  if (wpa_s->conf->ap_scan == 2 &&
5648
0
      os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
5649
0
    wpa_printf(MSG_INFO,
5650
0
         "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
5651
0
  }
5652
5653
0
  wpa_clear_keys(wpa_s, NULL);
5654
5655
  /* Make sure that TKIP countermeasures are not left enabled (could
5656
   * happen if wpa_supplicant is killed during countermeasures. */
5657
0
  wpa_drv_set_countermeasures(wpa_s, 0);
5658
5659
0
  wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
5660
0
  wpa_drv_flush_pmkid(wpa_s);
5661
5662
0
  wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
5663
0
  wpa_s->prev_scan_wildcard = 0;
5664
5665
0
  if (wpa_supplicant_enabled_networks(wpa_s)) {
5666
0
    if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5667
0
      wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
5668
0
      interface_count = 0;
5669
0
    }
5670
0
#ifndef ANDROID
5671
0
    if (!wpa_s->p2p_mgmt &&
5672
0
        wpa_supplicant_delayed_sched_scan(wpa_s,
5673
0
                  interface_count % 3,
5674
0
                  100000))
5675
0
      wpa_supplicant_req_scan(wpa_s, interface_count % 3,
5676
0
            100000);
5677
0
#endif /* ANDROID */
5678
0
    interface_count++;
5679
0
  } else
5680
0
    wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
5681
5682
0
  return 0;
5683
0
}
5684
5685
5686
static int wpa_supplicant_daemon(const char *pid_file)
5687
0
{
5688
0
  wpa_printf(MSG_DEBUG, "Daemonize..");
5689
0
  return os_daemonize(pid_file);
5690
0
}
5691
5692
5693
static struct wpa_supplicant *
5694
wpa_supplicant_alloc(struct wpa_supplicant *parent)
5695
0
{
5696
0
  struct wpa_supplicant *wpa_s;
5697
5698
0
  wpa_s = os_zalloc(sizeof(*wpa_s));
5699
0
  if (wpa_s == NULL)
5700
0
    return NULL;
5701
0
  wpa_s->scan_req = INITIAL_SCAN_REQ;
5702
0
  wpa_s->scan_interval = 5;
5703
0
  wpa_s->new_connection = 1;
5704
0
  wpa_s->parent = parent ? parent : wpa_s;
5705
0
  wpa_s->p2pdev = wpa_s->parent;
5706
0
  wpa_s->sched_scanning = 0;
5707
0
  wpa_s->setband_mask = WPA_SETBAND_AUTO;
5708
5709
0
  dl_list_init(&wpa_s->bss_tmp_disallowed);
5710
0
  dl_list_init(&wpa_s->fils_hlp_req);
5711
#ifdef CONFIG_TESTING_OPTIONS
5712
  dl_list_init(&wpa_s->drv_signal_override);
5713
#endif /* CONFIG_TESTING_OPTIONS */
5714
0
  dl_list_init(&wpa_s->active_scs_ids);
5715
5716
0
  return wpa_s;
5717
0
}
5718
5719
5720
#ifdef CONFIG_HT_OVERRIDES
5721
5722
static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
5723
           struct ieee80211_ht_capabilities *htcaps,
5724
           struct ieee80211_ht_capabilities *htcaps_mask,
5725
           const char *ht_mcs)
5726
{
5727
  /* parse ht_mcs into hex array */
5728
  int i;
5729
  const char *tmp = ht_mcs;
5730
  char *end = NULL;
5731
5732
  /* If ht_mcs is null, do not set anything */
5733
  if (!ht_mcs)
5734
    return 0;
5735
5736
  /* This is what we are setting in the kernel */
5737
  os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
5738
5739
  wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
5740
5741
  for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5742
    long v;
5743
5744
    errno = 0;
5745
    v = strtol(tmp, &end, 16);
5746
5747
    if (errno == 0) {
5748
      wpa_msg(wpa_s, MSG_DEBUG,
5749
        "htcap value[%i]: %ld end: %p  tmp: %p",
5750
        i, v, end, tmp);
5751
      if (end == tmp)
5752
        break;
5753
5754
      htcaps->supported_mcs_set[i] = v;
5755
      tmp = end;
5756
    } else {
5757
      wpa_msg(wpa_s, MSG_ERROR,
5758
        "Failed to parse ht-mcs: %s, error: %s\n",
5759
        ht_mcs, strerror(errno));
5760
      return -1;
5761
    }
5762
  }
5763
5764
  /*
5765
   * If we were able to parse any values, then set mask for the MCS set.
5766
   */
5767
  if (i) {
5768
    os_memset(&htcaps_mask->supported_mcs_set, 0xff,
5769
        IEEE80211_HT_MCS_MASK_LEN - 1);
5770
    /* skip the 3 reserved bits */
5771
    htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
5772
      0x1f;
5773
  }
5774
5775
  return 0;
5776
}
5777
5778
5779
static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
5780
         struct ieee80211_ht_capabilities *htcaps,
5781
         struct ieee80211_ht_capabilities *htcaps_mask,
5782
         int disabled)
5783
{
5784
  le16 msk;
5785
5786
  if (disabled == -1)
5787
    return 0;
5788
5789
  wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
5790
5791
  msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
5792
  htcaps_mask->ht_capabilities_info |= msk;
5793
  if (disabled)
5794
    htcaps->ht_capabilities_info &= msk;
5795
  else
5796
    htcaps->ht_capabilities_info |= msk;
5797
5798
  return 0;
5799
}
5800
5801
5802
static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
5803
        struct ieee80211_ht_capabilities *htcaps,
5804
        struct ieee80211_ht_capabilities *htcaps_mask,
5805
        int factor)
5806
{
5807
  if (factor == -1)
5808
    return 0;
5809
5810
  wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
5811
5812
  if (factor < 0 || factor > 3) {
5813
    wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
5814
      "Must be 0-3 or -1", factor);
5815
    return -EINVAL;
5816
  }
5817
5818
  htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
5819
  htcaps->a_mpdu_params &= ~0x3;
5820
  htcaps->a_mpdu_params |= factor & 0x3;
5821
5822
  return 0;
5823
}
5824
5825
5826
static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
5827
         struct ieee80211_ht_capabilities *htcaps,
5828
         struct ieee80211_ht_capabilities *htcaps_mask,
5829
         int density)
5830
{
5831
  if (density == -1)
5832
    return 0;
5833
5834
  wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
5835
5836
  if (density < 0 || density > 7) {
5837
    wpa_msg(wpa_s, MSG_ERROR,
5838
      "ampdu_density: %d out of range. Must be 0-7 or -1.",
5839
      density);
5840
    return -EINVAL;
5841
  }
5842
5843
  htcaps_mask->a_mpdu_params |= 0x1C;
5844
  htcaps->a_mpdu_params &= ~(0x1C);
5845
  htcaps->a_mpdu_params |= (density << 2) & 0x1C;
5846
5847
  return 0;
5848
}
5849
5850
5851
static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
5852
        struct ieee80211_ht_capabilities *htcaps,
5853
        struct ieee80211_ht_capabilities *htcaps_mask,
5854
        int disabled)
5855
{
5856
  if (disabled)
5857
    wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
5858
5859
  set_disable_ht40(htcaps, disabled);
5860
  set_disable_ht40(htcaps_mask, 0);
5861
5862
  return 0;
5863
}
5864
5865
5866
static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
5867
             struct ieee80211_ht_capabilities *htcaps,
5868
             struct ieee80211_ht_capabilities *htcaps_mask,
5869
             int disabled)
5870
{
5871
  /* Masking these out disables SGI */
5872
  le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
5873
        HT_CAP_INFO_SHORT_GI40MHZ);
5874
5875
  if (disabled)
5876
    wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
5877
5878
  if (disabled)
5879
    htcaps->ht_capabilities_info &= ~msk;
5880
  else
5881
    htcaps->ht_capabilities_info |= msk;
5882
5883
  htcaps_mask->ht_capabilities_info |= msk;
5884
5885
  return 0;
5886
}
5887
5888
5889
static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
5890
             struct ieee80211_ht_capabilities *htcaps,
5891
             struct ieee80211_ht_capabilities *htcaps_mask,
5892
             int disabled)
5893
{
5894
  /* Masking these out disables LDPC */
5895
  le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
5896
5897
  if (disabled)
5898
    wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
5899
5900
  if (disabled)
5901
    htcaps->ht_capabilities_info &= ~msk;
5902
  else
5903
    htcaps->ht_capabilities_info |= msk;
5904
5905
  htcaps_mask->ht_capabilities_info |= msk;
5906
5907
  return 0;
5908
}
5909
5910
5911
static int wpa_set_tx_stbc(struct wpa_supplicant *wpa_s,
5912
         struct ieee80211_ht_capabilities *htcaps,
5913
         struct ieee80211_ht_capabilities *htcaps_mask,
5914
         int tx_stbc)
5915
{
5916
  le16 msk = host_to_le16(HT_CAP_INFO_TX_STBC);
5917
5918
  if (tx_stbc == -1)
5919
    return 0;
5920
5921
  wpa_msg(wpa_s, MSG_DEBUG, "set_tx_stbc: %d", tx_stbc);
5922
5923
  if (tx_stbc < 0 || tx_stbc > 1) {
5924
    wpa_msg(wpa_s, MSG_ERROR,
5925
      "tx_stbc: %d out of range. Must be 0-1 or -1", tx_stbc);
5926
    return -EINVAL;
5927
  }
5928
5929
  htcaps_mask->ht_capabilities_info |= msk;
5930
  htcaps->ht_capabilities_info &= ~msk;
5931
  htcaps->ht_capabilities_info |= (tx_stbc << 7) & msk;
5932
5933
  return 0;
5934
}
5935
5936
5937
static int wpa_set_rx_stbc(struct wpa_supplicant *wpa_s,
5938
         struct ieee80211_ht_capabilities *htcaps,
5939
         struct ieee80211_ht_capabilities *htcaps_mask,
5940
         int rx_stbc)
5941
{
5942
  le16 msk = host_to_le16(HT_CAP_INFO_RX_STBC_MASK);
5943
5944
  if (rx_stbc == -1)
5945
    return 0;
5946
5947
  wpa_msg(wpa_s, MSG_DEBUG, "set_rx_stbc: %d", rx_stbc);
5948
5949
  if (rx_stbc < 0 || rx_stbc > 3) {
5950
    wpa_msg(wpa_s, MSG_ERROR,
5951
      "rx_stbc: %d out of range. Must be 0-3 or -1", rx_stbc);
5952
    return -EINVAL;
5953
  }
5954
5955
  htcaps_mask->ht_capabilities_info |= msk;
5956
  htcaps->ht_capabilities_info &= ~msk;
5957
  htcaps->ht_capabilities_info |= (rx_stbc << 8) & msk;
5958
5959
  return 0;
5960
}
5961
5962
5963
void wpa_supplicant_apply_ht_overrides(
5964
  struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
5965
  struct wpa_driver_associate_params *params)
5966
{
5967
  struct ieee80211_ht_capabilities *htcaps;
5968
  struct ieee80211_ht_capabilities *htcaps_mask;
5969
5970
  if (!ssid)
5971
    return;
5972
5973
  params->disable_ht = ssid->disable_ht;
5974
  if (!params->htcaps || !params->htcaps_mask)
5975
    return;
5976
5977
  htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
5978
  htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
5979
  wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
5980
  wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
5981
            ssid->disable_max_amsdu);
5982
  wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
5983
  wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
5984
  wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
5985
  wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
5986
  wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
5987
  wpa_set_rx_stbc(wpa_s, htcaps, htcaps_mask, ssid->rx_stbc);
5988
  wpa_set_tx_stbc(wpa_s, htcaps, htcaps_mask, ssid->tx_stbc);
5989
5990
  if (ssid->ht40_intolerant) {
5991
    le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
5992
    htcaps->ht_capabilities_info |= bit;
5993
    htcaps_mask->ht_capabilities_info |= bit;
5994
  }
5995
}
5996
5997
#endif /* CONFIG_HT_OVERRIDES */
5998
5999
6000
#ifdef CONFIG_VHT_OVERRIDES
6001
void wpa_supplicant_apply_vht_overrides(
6002
  struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
6003
  struct wpa_driver_associate_params *params)
6004
{
6005
  struct ieee80211_vht_capabilities *vhtcaps;
6006
  struct ieee80211_vht_capabilities *vhtcaps_mask;
6007
6008
  if (!ssid)
6009
    return;
6010
6011
  params->disable_vht = ssid->disable_vht;
6012
6013
  vhtcaps = (void *) params->vhtcaps;
6014
  vhtcaps_mask = (void *) params->vhtcaps_mask;
6015
6016
  if (!vhtcaps || !vhtcaps_mask)
6017
    return;
6018
6019
  vhtcaps->vht_capabilities_info = host_to_le32(ssid->vht_capa);
6020
  vhtcaps_mask->vht_capabilities_info = host_to_le32(ssid->vht_capa_mask);
6021
6022
#ifdef CONFIG_HT_OVERRIDES
6023
  if (ssid->disable_sgi) {
6024
    vhtcaps_mask->vht_capabilities_info |= (VHT_CAP_SHORT_GI_80 |
6025
              VHT_CAP_SHORT_GI_160);
6026
    vhtcaps->vht_capabilities_info &= ~(VHT_CAP_SHORT_GI_80 |
6027
                VHT_CAP_SHORT_GI_160);
6028
    wpa_msg(wpa_s, MSG_DEBUG,
6029
      "disable-sgi override specified, vht-caps: 0x%x",
6030
      vhtcaps->vht_capabilities_info);
6031
  }
6032
6033
  /* if max ampdu is <= 3, we have to make the HT cap the same */
6034
  if (ssid->vht_capa_mask & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) {
6035
    int max_ampdu;
6036
6037
    max_ampdu = (ssid->vht_capa &
6038
           VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
6039
      VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT;
6040
6041
    max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
6042
    wpa_set_ampdu_factor(wpa_s,
6043
             (void *) params->htcaps,
6044
             (void *) params->htcaps_mask,
6045
             max_ampdu);
6046
  }
6047
#endif /* CONFIG_HT_OVERRIDES */
6048
6049
#define OVERRIDE_MCS(i)             \
6050
  if (ssid->vht_tx_mcs_nss_ ##i >= 0) {       \
6051
    vhtcaps_mask->vht_supported_mcs_set.tx_map |=   \
6052
      host_to_le16(3 << 2 * (i - 1));     \
6053
    vhtcaps->vht_supported_mcs_set.tx_map |=    \
6054
      host_to_le16(ssid->vht_tx_mcs_nss_ ##i << \
6055
             2 * (i - 1));      \
6056
  }               \
6057
  if (ssid->vht_rx_mcs_nss_ ##i >= 0) {       \
6058
    vhtcaps_mask->vht_supported_mcs_set.rx_map |=   \
6059
      host_to_le16(3 << 2 * (i - 1));     \
6060
    vhtcaps->vht_supported_mcs_set.rx_map |=    \
6061
      host_to_le16(ssid->vht_rx_mcs_nss_ ##i << \
6062
             2 * (i - 1));      \
6063
  }
6064
6065
  OVERRIDE_MCS(1);
6066
  OVERRIDE_MCS(2);
6067
  OVERRIDE_MCS(3);
6068
  OVERRIDE_MCS(4);
6069
  OVERRIDE_MCS(5);
6070
  OVERRIDE_MCS(6);
6071
  OVERRIDE_MCS(7);
6072
  OVERRIDE_MCS(8);
6073
}
6074
#endif /* CONFIG_VHT_OVERRIDES */
6075
6076
6077
#ifdef CONFIG_HE_OVERRIDES
6078
void wpa_supplicant_apply_he_overrides(
6079
  struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
6080
  struct wpa_driver_associate_params *params)
6081
{
6082
  if (!ssid)
6083
    return;
6084
6085
  params->disable_he = ssid->disable_he;
6086
}
6087
#endif /* CONFIG_HE_OVERRIDES */
6088
6089
6090
void wpa_supplicant_apply_eht_overrides(
6091
  struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
6092
  struct wpa_driver_associate_params *params)
6093
0
{
6094
0
  if (!ssid)
6095
0
    return;
6096
6097
0
  params->disable_eht = ssid->disable_eht;
6098
0
}
6099
6100
6101
static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
6102
0
{
6103
#ifdef PCSC_FUNCS
6104
  size_t len;
6105
6106
  if (!wpa_s->conf->pcsc_reader)
6107
    return 0;
6108
6109
  wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
6110
  if (!wpa_s->scard)
6111
    return 1;
6112
6113
  if (wpa_s->conf->pcsc_pin &&
6114
      scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
6115
    scard_deinit(wpa_s->scard);
6116
    wpa_s->scard = NULL;
6117
    wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
6118
    return -1;
6119
  }
6120
6121
  len = sizeof(wpa_s->imsi) - 1;
6122
  if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
6123
    scard_deinit(wpa_s->scard);
6124
    wpa_s->scard = NULL;
6125
    wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
6126
    return -1;
6127
  }
6128
  wpa_s->imsi[len] = '\0';
6129
6130
  wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
6131
6132
  wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
6133
       wpa_s->imsi, wpa_s->mnc_len);
6134
6135
  wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
6136
  eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
6137
#endif /* PCSC_FUNCS */
6138
6139
0
  return 0;
6140
0
}
6141
6142
6143
int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
6144
0
{
6145
0
  char *val, *pos;
6146
6147
0
  ext_password_deinit(wpa_s->ext_pw);
6148
0
  wpa_s->ext_pw = NULL;
6149
0
  eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
6150
6151
0
  if (!wpa_s->conf->ext_password_backend)
6152
0
    return 0;
6153
6154
0
  val = os_strdup(wpa_s->conf->ext_password_backend);
6155
0
  if (val == NULL)
6156
0
    return -1;
6157
0
  pos = os_strchr(val, ':');
6158
0
  if (pos)
6159
0
    *pos++ = '\0';
6160
6161
0
  wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
6162
6163
0
  wpa_s->ext_pw = ext_password_init(val, pos);
6164
0
  os_free(val);
6165
0
  if (wpa_s->ext_pw == NULL) {
6166
0
    wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
6167
0
    return -1;
6168
0
  }
6169
0
  eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
6170
6171
0
  return 0;
6172
0
}
6173
6174
6175
#ifdef CONFIG_FST
6176
6177
static const u8 * wpas_fst_get_bssid_cb(void *ctx)
6178
{
6179
  struct wpa_supplicant *wpa_s = ctx;
6180
6181
  return (is_zero_ether_addr(wpa_s->bssid) ||
6182
    wpa_s->wpa_state != WPA_COMPLETED) ? NULL : wpa_s->bssid;
6183
}
6184
6185
6186
static void wpas_fst_get_channel_info_cb(void *ctx,
6187
           enum hostapd_hw_mode *hw_mode,
6188
           u8 *channel)
6189
{
6190
  struct wpa_supplicant *wpa_s = ctx;
6191
6192
  if (wpa_s->current_bss) {
6193
    *hw_mode = ieee80211_freq_to_chan(wpa_s->current_bss->freq,
6194
              channel);
6195
  } else if (wpa_s->hw.num_modes) {
6196
    *hw_mode = wpa_s->hw.modes[0].mode;
6197
  } else {
6198
    WPA_ASSERT(0);
6199
    *hw_mode = 0;
6200
  }
6201
}
6202
6203
6204
static int wpas_fst_get_hw_modes(void *ctx, struct hostapd_hw_modes **modes)
6205
{
6206
  struct wpa_supplicant *wpa_s = ctx;
6207
6208
  *modes = wpa_s->hw.modes;
6209
  return wpa_s->hw.num_modes;
6210
}
6211
6212
6213
static void wpas_fst_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
6214
{
6215
  struct wpa_supplicant *wpa_s = ctx;
6216
6217
  wpa_hexdump_buf(MSG_DEBUG, "FST: Set IEs", fst_ies);
6218
  wpa_s->fst_ies = fst_ies;
6219
}
6220
6221
6222
static int wpas_fst_send_action_cb(void *ctx, const u8 *da, struct wpabuf *data)
6223
{
6224
  struct wpa_supplicant *wpa_s = ctx;
6225
6226
  if (os_memcmp(wpa_s->bssid, da, ETH_ALEN) != 0) {
6227
    wpa_printf(MSG_INFO, "FST:%s:bssid=" MACSTR " != da=" MACSTR,
6228
         __func__, MAC2STR(wpa_s->bssid), MAC2STR(da));
6229
    return -1;
6230
  }
6231
  return wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
6232
           wpa_s->own_addr, wpa_s->bssid,
6233
           wpabuf_head(data), wpabuf_len(data),
6234
           0);
6235
}
6236
6237
6238
static const struct wpabuf * wpas_fst_get_mb_ie_cb(void *ctx, const u8 *addr)
6239
{
6240
  struct wpa_supplicant *wpa_s = ctx;
6241
6242
  WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
6243
  return wpa_s->received_mb_ies;
6244
}
6245
6246
6247
static void wpas_fst_update_mb_ie_cb(void *ctx, const u8 *addr,
6248
             const u8 *buf, size_t size)
6249
{
6250
  struct wpa_supplicant *wpa_s = ctx;
6251
  struct mb_ies_info info;
6252
6253
  WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
6254
6255
  if (!mb_ies_info_by_ies(&info, buf, size)) {
6256
    wpabuf_free(wpa_s->received_mb_ies);
6257
    wpa_s->received_mb_ies = mb_ies_by_info(&info);
6258
  }
6259
}
6260
6261
6262
static const u8 * wpas_fst_get_peer_first(void *ctx,
6263
            struct fst_get_peer_ctx **get_ctx,
6264
            bool mb_only)
6265
{
6266
  struct wpa_supplicant *wpa_s = ctx;
6267
6268
  *get_ctx = NULL;
6269
  if (!is_zero_ether_addr(wpa_s->bssid))
6270
    return (wpa_s->received_mb_ies || !mb_only) ?
6271
      wpa_s->bssid : NULL;
6272
  return NULL;
6273
}
6274
6275
6276
static const u8 * wpas_fst_get_peer_next(void *ctx,
6277
           struct fst_get_peer_ctx **get_ctx,
6278
           bool mb_only)
6279
{
6280
  return NULL;
6281
}
6282
6283
void fst_wpa_supplicant_fill_iface_obj(struct wpa_supplicant *wpa_s,
6284
               struct fst_wpa_obj *iface_obj)
6285
{
6286
  os_memset(iface_obj, 0, sizeof(*iface_obj));
6287
  iface_obj->ctx              = wpa_s;
6288
  iface_obj->get_bssid        = wpas_fst_get_bssid_cb;
6289
  iface_obj->get_channel_info = wpas_fst_get_channel_info_cb;
6290
  iface_obj->get_hw_modes     = wpas_fst_get_hw_modes;
6291
  iface_obj->set_ies          = wpas_fst_set_ies_cb;
6292
  iface_obj->send_action      = wpas_fst_send_action_cb;
6293
  iface_obj->get_mb_ie        = wpas_fst_get_mb_ie_cb;
6294
  iface_obj->update_mb_ie     = wpas_fst_update_mb_ie_cb;
6295
  iface_obj->get_peer_first   = wpas_fst_get_peer_first;
6296
  iface_obj->get_peer_next    = wpas_fst_get_peer_next;
6297
}
6298
#endif /* CONFIG_FST */
6299
6300
static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
6301
            const struct wpa_driver_capa *capa)
6302
0
{
6303
0
  struct wowlan_triggers *triggers;
6304
0
  int ret = 0;
6305
6306
0
  if (!wpa_s->conf->wowlan_triggers)
6307
0
    return 0;
6308
6309
0
  triggers = wpa_get_wowlan_triggers(wpa_s->conf->wowlan_triggers, capa);
6310
0
  if (triggers) {
6311
0
    ret = wpa_drv_wowlan(wpa_s, triggers);
6312
0
    os_free(triggers);
6313
0
  }
6314
0
  return ret;
6315
0
}
6316
6317
6318
enum wpa_radio_work_band wpas_freq_to_band(int freq)
6319
0
{
6320
0
  if (freq < 3000)
6321
0
    return BAND_2_4_GHZ;
6322
0
  if (freq > 50000)
6323
0
    return BAND_60_GHZ;
6324
0
  return BAND_5_GHZ;
6325
0
}
6326
6327
6328
unsigned int wpas_get_bands(struct wpa_supplicant *wpa_s, const int *freqs)
6329
0
{
6330
0
  int i;
6331
0
  unsigned int band = 0;
6332
6333
0
  if (freqs) {
6334
    /* freqs are specified for the radio work */
6335
0
    for (i = 0; freqs[i]; i++)
6336
0
      band |= wpas_freq_to_band(freqs[i]);
6337
0
  } else {
6338
    /*
6339
     * freqs are not specified, implies all
6340
     * the supported freqs by HW
6341
     */
6342
0
    for (i = 0; i < wpa_s->hw.num_modes; i++) {
6343
0
      if (wpa_s->hw.modes[i].num_channels != 0) {
6344
0
        if (wpa_s->hw.modes[i].mode ==
6345
0
            HOSTAPD_MODE_IEEE80211B ||
6346
0
            wpa_s->hw.modes[i].mode ==
6347
0
            HOSTAPD_MODE_IEEE80211G)
6348
0
          band |= BAND_2_4_GHZ;
6349
0
        else if (wpa_s->hw.modes[i].mode ==
6350
0
           HOSTAPD_MODE_IEEE80211A)
6351
0
          band |= BAND_5_GHZ;
6352
0
        else if (wpa_s->hw.modes[i].mode ==
6353
0
           HOSTAPD_MODE_IEEE80211AD)
6354
0
          band |= BAND_60_GHZ;
6355
0
        else if (wpa_s->hw.modes[i].mode ==
6356
0
           HOSTAPD_MODE_IEEE80211ANY)
6357
0
          band = BAND_2_4_GHZ | BAND_5_GHZ |
6358
0
            BAND_60_GHZ;
6359
0
      }
6360
0
    }
6361
0
  }
6362
6363
0
  return band;
6364
0
}
6365
6366
6367
static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
6368
                const char *rn)
6369
0
{
6370
0
  struct wpa_supplicant *iface = wpa_s->global->ifaces;
6371
0
  struct wpa_radio *radio;
6372
6373
0
  while (rn && iface) {
6374
0
    radio = iface->radio;
6375
0
    if (radio && os_strcmp(rn, radio->name) == 0) {
6376
0
      wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
6377
0
           wpa_s->ifname, rn);
6378
0
      dl_list_add(&radio->ifaces, &wpa_s->radio_list);
6379
0
      return radio;
6380
0
    }
6381
6382
0
    iface = iface->next;
6383
0
  }
6384
6385
0
  wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
6386
0
       wpa_s->ifname, rn ? rn : "N/A");
6387
0
  radio = os_zalloc(sizeof(*radio));
6388
0
  if (radio == NULL)
6389
0
    return NULL;
6390
6391
0
  if (rn)
6392
0
    os_strlcpy(radio->name, rn, sizeof(radio->name));
6393
0
  dl_list_init(&radio->ifaces);
6394
0
  dl_list_init(&radio->work);
6395
0
  dl_list_add(&radio->ifaces, &wpa_s->radio_list);
6396
6397
0
  return radio;
6398
0
}
6399
6400
6401
static void radio_work_free(struct wpa_radio_work *work)
6402
0
{
6403
0
  if (work->wpa_s->scan_work == work) {
6404
    /* This should not really happen. */
6405
0
    wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
6406
0
      work->type, work, work->started);
6407
0
    work->wpa_s->scan_work = NULL;
6408
0
  }
6409
6410
#ifdef CONFIG_P2P
6411
  if (work->wpa_s->p2p_scan_work == work) {
6412
    /* This should not really happen. */
6413
    wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
6414
      work->type, work, work->started);
6415
    work->wpa_s->p2p_scan_work = NULL;
6416
  }
6417
#endif /* CONFIG_P2P */
6418
6419
0
  if (work->started) {
6420
0
    work->wpa_s->radio->num_active_works--;
6421
0
    wpa_dbg(work->wpa_s, MSG_DEBUG,
6422
0
      "radio_work_free('%s'@%p): num_active_works --> %u",
6423
0
      work->type, work,
6424
0
      work->wpa_s->radio->num_active_works);
6425
0
  }
6426
6427
0
  dl_list_del(&work->list);
6428
0
  os_free(work);
6429
0
}
6430
6431
6432
static int radio_work_is_connect(struct wpa_radio_work *work)
6433
0
{
6434
0
  return os_strcmp(work->type, "sme-connect") == 0 ||
6435
0
    os_strcmp(work->type, "connect") == 0;
6436
0
}
6437
6438
6439
static int radio_work_is_scan(struct wpa_radio_work *work)
6440
0
{
6441
0
  return os_strcmp(work->type, "scan") == 0 ||
6442
0
    os_strcmp(work->type, "p2p-scan") == 0;
6443
0
}
6444
6445
6446
static struct wpa_radio_work * radio_work_get_next_work(struct wpa_radio *radio)
6447
0
{
6448
0
  struct wpa_radio_work *active_work = NULL;
6449
0
  struct wpa_radio_work *tmp;
6450
6451
  /* Get the active work to know the type and band. */
6452
0
  dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
6453
0
    if (tmp->started) {
6454
0
      active_work = tmp;
6455
0
      break;
6456
0
    }
6457
0
  }
6458
6459
0
  if (!active_work) {
6460
    /* No active work, start one */
6461
0
    radio->num_active_works = 0;
6462
0
    dl_list_for_each(tmp, &radio->work, struct wpa_radio_work,
6463
0
         list) {
6464
0
      if (os_strcmp(tmp->type, "scan") == 0 &&
6465
0
          external_scan_running(radio) &&
6466
0
          (((struct wpa_driver_scan_params *)
6467
0
            tmp->ctx)->only_new_results ||
6468
0
           tmp->wpa_s->clear_driver_scan_cache))
6469
0
        continue;
6470
0
      return tmp;
6471
0
    }
6472
0
    return NULL;
6473
0
  }
6474
6475
0
  if (radio_work_is_connect(active_work)) {
6476
    /*
6477
     * If the active work is either connect or sme-connect,
6478
     * do not parallelize them with other radio works.
6479
     */
6480
0
    wpa_dbg(active_work->wpa_s, MSG_DEBUG,
6481
0
      "Do not parallelize radio work with %s",
6482
0
      active_work->type);
6483
0
    return NULL;
6484
0
  }
6485
6486
0
  dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
6487
0
    if (tmp->started)
6488
0
      continue;
6489
6490
    /*
6491
     * If connect or sme-connect are enqueued, parallelize only
6492
     * those operations ahead of them in the queue.
6493
     */
6494
0
    if (radio_work_is_connect(tmp))
6495
0
      break;
6496
6497
    /* Serialize parallel scan and p2p_scan operations on the same
6498
     * interface since the driver_nl80211 mechanism for tracking
6499
     * scan cookies does not yet have support for this. */
6500
0
    if (active_work->wpa_s == tmp->wpa_s &&
6501
0
        radio_work_is_scan(active_work) &&
6502
0
        radio_work_is_scan(tmp)) {
6503
0
      wpa_dbg(active_work->wpa_s, MSG_DEBUG,
6504
0
        "Do not start work '%s' when another work '%s' is already scheduled",
6505
0
        tmp->type, active_work->type);
6506
0
      continue;
6507
0
    }
6508
    /*
6509
     * Check that the radio works are distinct and
6510
     * on different bands.
6511
     */
6512
0
    if (os_strcmp(active_work->type, tmp->type) != 0 &&
6513
0
        (active_work->bands != tmp->bands)) {
6514
      /*
6515
       * If a scan has to be scheduled through nl80211 scan
6516
       * interface and if an external scan is already running,
6517
       * do not schedule the scan since it is likely to get
6518
       * rejected by kernel.
6519
       */
6520
0
      if (os_strcmp(tmp->type, "scan") == 0 &&
6521
0
          external_scan_running(radio) &&
6522
0
          (((struct wpa_driver_scan_params *)
6523
0
            tmp->ctx)->only_new_results ||
6524
0
           tmp->wpa_s->clear_driver_scan_cache))
6525
0
        continue;
6526
6527
0
      wpa_dbg(active_work->wpa_s, MSG_DEBUG,
6528
0
        "active_work:%s new_work:%s",
6529
0
        active_work->type, tmp->type);
6530
0
      return tmp;
6531
0
    }
6532
0
  }
6533
6534
  /* Did not find a radio work to schedule in parallel. */
6535
0
  return NULL;
6536
0
}
6537
6538
6539
static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
6540
0
{
6541
0
  struct wpa_radio *radio = eloop_ctx;
6542
0
  struct wpa_radio_work *work;
6543
0
  struct os_reltime now, diff;
6544
0
  struct wpa_supplicant *wpa_s;
6545
6546
0
  work = dl_list_first(&radio->work, struct wpa_radio_work, list);
6547
0
  if (work == NULL) {
6548
0
    radio->num_active_works = 0;
6549
0
    return;
6550
0
  }
6551
6552
0
  wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
6553
0
            radio_list);
6554
6555
0
  if (!(wpa_s &&
6556
0
        wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)) {
6557
0
    if (work->started)
6558
0
      return; /* already started and still in progress */
6559
6560
0
    if (wpa_s && external_scan_running(wpa_s->radio)) {
6561
0
      wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
6562
0
      return;
6563
0
    }
6564
0
  } else {
6565
0
    work = NULL;
6566
0
    if (radio->num_active_works < MAX_ACTIVE_WORKS) {
6567
      /* get the work to schedule next */
6568
0
      work = radio_work_get_next_work(radio);
6569
0
    }
6570
0
    if (!work)
6571
0
      return;
6572
0
  }
6573
6574
0
  wpa_s = work->wpa_s;
6575
0
  os_get_reltime(&now);
6576
0
  os_reltime_sub(&now, &work->time, &diff);
6577
0
  wpa_dbg(wpa_s, MSG_DEBUG,
6578
0
    "Starting radio work '%s'@%p after %ld.%06ld second wait",
6579
0
    work->type, work, diff.sec, diff.usec);
6580
0
  work->started = 1;
6581
0
  work->time = now;
6582
0
  radio->num_active_works++;
6583
6584
0
  work->cb(work, 0);
6585
6586
0
  if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS) &&
6587
0
      radio->num_active_works < MAX_ACTIVE_WORKS)
6588
0
    radio_work_check_next(wpa_s);
6589
0
}
6590
6591
6592
/*
6593
 * This function removes both started and pending radio works running on
6594
 * the provided interface's radio.
6595
 * Prior to the removal of the radio work, its callback (cb) is called with
6596
 * deinit set to be 1. Each work's callback is responsible for clearing its
6597
 * internal data and restoring to a correct state.
6598
 * @wpa_s: wpa_supplicant data
6599
 * @type: type of works to be removed
6600
 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
6601
 * this interface's works.
6602
 */
6603
void radio_remove_works(struct wpa_supplicant *wpa_s,
6604
      const char *type, int remove_all)
6605
0
{
6606
0
  struct wpa_radio_work *work, *tmp;
6607
0
  struct wpa_radio *radio = wpa_s->radio;
6608
6609
0
  dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
6610
0
            list) {
6611
0
    if (type && os_strcmp(type, work->type) != 0)
6612
0
      continue;
6613
6614
    /* skip other ifaces' works */
6615
0
    if (!remove_all && work->wpa_s != wpa_s)
6616
0
      continue;
6617
6618
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
6619
0
      work->type, work, work->started ? " (started)" : "");
6620
0
    work->cb(work, 1);
6621
0
    radio_work_free(work);
6622
0
  }
6623
6624
  /* in case we removed the started work */
6625
0
  radio_work_check_next(wpa_s);
6626
0
}
6627
6628
6629
void radio_remove_pending_work(struct wpa_supplicant *wpa_s, void *ctx)
6630
0
{
6631
0
  struct wpa_radio_work *work;
6632
0
  struct wpa_radio *radio = wpa_s->radio;
6633
6634
0
  dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
6635
0
    if (work->ctx != ctx)
6636
0
      continue;
6637
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Free pending radio work '%s'@%p%s",
6638
0
      work->type, work, work->started ? " (started)" : "");
6639
0
    radio_work_free(work);
6640
0
    break;
6641
0
  }
6642
0
}
6643
6644
6645
static void radio_remove_interface(struct wpa_supplicant *wpa_s)
6646
0
{
6647
0
  struct wpa_radio *radio = wpa_s->radio;
6648
6649
0
  if (!radio)
6650
0
    return;
6651
6652
0
  wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
6653
0
       wpa_s->ifname, radio->name);
6654
0
  dl_list_del(&wpa_s->radio_list);
6655
0
  radio_remove_works(wpa_s, NULL, 0);
6656
  /* If the interface that triggered the external scan was removed, the
6657
   * external scan is no longer running. */
6658
0
  if (wpa_s == radio->external_scan_req_interface)
6659
0
    radio->external_scan_req_interface = NULL;
6660
0
  wpa_s->radio = NULL;
6661
0
  if (!dl_list_empty(&radio->ifaces))
6662
0
    return; /* Interfaces remain for this radio */
6663
6664
0
  wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
6665
0
  eloop_cancel_timeout(radio_start_next_work, radio, NULL);
6666
0
  os_free(radio);
6667
0
}
6668
6669
6670
void radio_work_check_next(struct wpa_supplicant *wpa_s)
6671
0
{
6672
0
  struct wpa_radio *radio = wpa_s->radio;
6673
6674
0
  if (dl_list_empty(&radio->work))
6675
0
    return;
6676
0
  if (wpa_s->ext_work_in_progress) {
6677
0
    wpa_printf(MSG_DEBUG,
6678
0
         "External radio work in progress - delay start of pending item");
6679
0
    return;
6680
0
  }
6681
0
  eloop_cancel_timeout(radio_start_next_work, radio, NULL);
6682
0
  eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
6683
0
}
6684
6685
6686
/**
6687
 * radio_add_work - Add a radio work item
6688
 * @wpa_s: Pointer to wpa_supplicant data
6689
 * @freq: Frequency of the offchannel operation in MHz or 0
6690
 * @type: Unique identifier for each type of work
6691
 * @next: Force as the next work to be executed
6692
 * @cb: Callback function for indicating when radio is available
6693
 * @ctx: Context pointer for the work (work->ctx in cb())
6694
 * Returns: 0 on success, -1 on failure
6695
 *
6696
 * This function is used to request time for an operation that requires
6697
 * exclusive radio control. Once the radio is available, the registered callback
6698
 * function will be called. radio_work_done() must be called once the exclusive
6699
 * radio operation has been completed, so that the radio is freed for other
6700
 * operations. The special case of deinit=1 is used to free the context data
6701
 * during interface removal. That does not allow the callback function to start
6702
 * the radio operation, i.e., it must free any resources allocated for the radio
6703
 * work and return.
6704
 *
6705
 * The @freq parameter can be used to indicate a single channel on which the
6706
 * offchannel operation will occur. This may allow multiple radio work
6707
 * operations to be performed in parallel if they apply for the same channel.
6708
 * Setting this to 0 indicates that the work item may use multiple channels or
6709
 * requires exclusive control of the radio.
6710
 */
6711
int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
6712
       const char *type, int next,
6713
       void (*cb)(struct wpa_radio_work *work, int deinit),
6714
       void *ctx)
6715
0
{
6716
0
  struct wpa_radio *radio = wpa_s->radio;
6717
0
  struct wpa_radio_work *work;
6718
0
  int was_empty;
6719
6720
0
  work = os_zalloc(sizeof(*work));
6721
0
  if (work == NULL)
6722
0
    return -1;
6723
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
6724
0
  os_get_reltime(&work->time);
6725
0
  work->freq = freq;
6726
0
  work->type = type;
6727
0
  work->wpa_s = wpa_s;
6728
0
  work->cb = cb;
6729
0
  work->ctx = ctx;
6730
6731
0
  if (freq)
6732
0
    work->bands = wpas_freq_to_band(freq);
6733
0
  else if (os_strcmp(type, "scan") == 0 ||
6734
0
     os_strcmp(type, "p2p-scan") == 0)
6735
0
    work->bands = wpas_get_bands(wpa_s,
6736
0
               ((struct wpa_driver_scan_params *)
6737
0
                ctx)->freqs);
6738
0
  else
6739
0
    work->bands = wpas_get_bands(wpa_s, NULL);
6740
6741
0
  was_empty = dl_list_empty(&wpa_s->radio->work);
6742
0
  if (next)
6743
0
    dl_list_add(&wpa_s->radio->work, &work->list);
6744
0
  else
6745
0
    dl_list_add_tail(&wpa_s->radio->work, &work->list);
6746
0
  if (was_empty) {
6747
0
    wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
6748
0
    radio_work_check_next(wpa_s);
6749
0
  } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)
6750
0
       && radio->num_active_works < MAX_ACTIVE_WORKS) {
6751
0
    wpa_dbg(wpa_s, MSG_DEBUG,
6752
0
      "Try to schedule a radio work (num_active_works=%u)",
6753
0
      radio->num_active_works);
6754
0
    radio_work_check_next(wpa_s);
6755
0
  }
6756
6757
0
  return 0;
6758
0
}
6759
6760
6761
/**
6762
 * radio_work_done - Indicate that a radio work item has been completed
6763
 * @work: Completed work
6764
 *
6765
 * This function is called once the callback function registered with
6766
 * radio_add_work() has completed its work.
6767
 */
6768
void radio_work_done(struct wpa_radio_work *work)
6769
0
{
6770
0
  struct wpa_supplicant *wpa_s = work->wpa_s;
6771
0
  struct os_reltime now, diff;
6772
0
  unsigned int started = work->started;
6773
6774
0
  os_get_reltime(&now);
6775
0
  os_reltime_sub(&now, &work->time, &diff);
6776
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
6777
0
    work->type, work, started ? "done" : "canceled",
6778
0
    diff.sec, diff.usec);
6779
0
  radio_work_free(work);
6780
0
  if (started)
6781
0
    radio_work_check_next(wpa_s);
6782
0
}
6783
6784
6785
struct wpa_radio_work *
6786
radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
6787
0
{
6788
0
  struct wpa_radio_work *work;
6789
0
  struct wpa_radio *radio = wpa_s->radio;
6790
6791
0
  dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
6792
0
    if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
6793
0
      return work;
6794
0
  }
6795
6796
0
  return NULL;
6797
0
}
6798
6799
6800
static int wpas_init_driver(struct wpa_supplicant *wpa_s,
6801
          const struct wpa_interface *iface)
6802
0
{
6803
0
  const char *ifname, *driver, *rn;
6804
6805
0
  driver = iface->driver;
6806
0
next_driver:
6807
0
  if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
6808
0
    return -1;
6809
6810
0
  wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
6811
0
  if (wpa_s->drv_priv == NULL) {
6812
0
    const char *pos;
6813
0
    int level = MSG_ERROR;
6814
6815
0
    pos = driver ? os_strchr(driver, ',') : NULL;
6816
0
    if (pos) {
6817
0
      wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
6818
0
        "driver interface - try next driver wrapper");
6819
0
      driver = pos + 1;
6820
0
      goto next_driver;
6821
0
    }
6822
6823
#ifdef CONFIG_MATCH_IFACE
6824
    if (wpa_s->matched == WPA_IFACE_MATCHED_NULL)
6825
      level = MSG_DEBUG;
6826
#endif /* CONFIG_MATCH_IFACE */
6827
0
    wpa_msg(wpa_s, level, "Failed to initialize driver interface");
6828
0
    return -1;
6829
0
  }
6830
0
  if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
6831
0
    wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
6832
0
      "driver_param '%s'", wpa_s->conf->driver_param);
6833
0
    return -1;
6834
0
  }
6835
6836
0
  ifname = wpa_drv_get_ifname(wpa_s);
6837
0
  if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
6838
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
6839
0
      "interface name with '%s'", ifname);
6840
0
    os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
6841
0
  }
6842
6843
0
  rn = wpa_driver_get_radio_name(wpa_s);
6844
0
  if (rn && rn[0] == '\0')
6845
0
    rn = NULL;
6846
6847
0
  wpa_s->radio = radio_add_interface(wpa_s, rn);
6848
0
  if (wpa_s->radio == NULL)
6849
0
    return -1;
6850
6851
0
  return 0;
6852
0
}
6853
6854
6855
#ifdef CONFIG_GAS_SERVER
6856
6857
static void wpas_gas_server_tx_status(struct wpa_supplicant *wpa_s,
6858
              unsigned int freq, const u8 *dst,
6859
              const u8 *src, const u8 *bssid,
6860
              const u8 *data, size_t data_len,
6861
              enum offchannel_send_action_result result)
6862
{
6863
  wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
6864
       " result=%s",
6865
       freq, MAC2STR(dst),
6866
       result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
6867
       (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
6868
        "FAILED"));
6869
  gas_server_tx_status(wpa_s->gas_server, dst, data, data_len,
6870
           result == OFFCHANNEL_SEND_ACTION_SUCCESS);
6871
}
6872
6873
6874
static void wpas_gas_server_tx(void *ctx, int freq, const u8 *da,
6875
             struct wpabuf *buf, unsigned int wait_time)
6876
{
6877
  struct wpa_supplicant *wpa_s = ctx;
6878
  const u8 broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6879
6880
  if (wait_time > wpa_s->max_remain_on_chan)
6881
    wait_time = wpa_s->max_remain_on_chan;
6882
6883
  offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, broadcast,
6884
             wpabuf_head(buf), wpabuf_len(buf),
6885
             wait_time, wpas_gas_server_tx_status, 0);
6886
}
6887
6888
#endif /* CONFIG_GAS_SERVER */
6889
6890
static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
6891
             const struct wpa_interface *iface)
6892
0
{
6893
0
  struct wpa_driver_capa capa;
6894
0
  int capa_res;
6895
0
  u8 dfs_domain;
6896
6897
0
  wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
6898
0
       "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
6899
0
       iface->confname ? iface->confname : "N/A",
6900
0
       iface->driver ? iface->driver : "default",
6901
0
       iface->ctrl_interface ? iface->ctrl_interface : "N/A",
6902
0
       iface->bridge_ifname ? iface->bridge_ifname : "N/A");
6903
6904
0
  if (iface->confname) {
6905
#ifdef CONFIG_BACKEND_FILE
6906
    wpa_s->confname = os_rel2abs_path(iface->confname);
6907
    if (wpa_s->confname == NULL) {
6908
      wpa_printf(MSG_ERROR, "Failed to get absolute path "
6909
           "for configuration file '%s'.",
6910
           iface->confname);
6911
      return -1;
6912
    }
6913
    wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
6914
         iface->confname, wpa_s->confname);
6915
#else /* CONFIG_BACKEND_FILE */
6916
0
    wpa_s->confname = os_strdup(iface->confname);
6917
0
#endif /* CONFIG_BACKEND_FILE */
6918
0
    wpa_s->conf = wpa_config_read(wpa_s->confname, NULL, false);
6919
0
    if (wpa_s->conf == NULL) {
6920
0
      wpa_printf(MSG_ERROR, "Failed to read or parse "
6921
0
           "configuration '%s'.", wpa_s->confname);
6922
0
      return -1;
6923
0
    }
6924
0
    wpa_s->confanother = os_rel2abs_path(iface->confanother);
6925
0
    if (wpa_s->confanother &&
6926
0
        !wpa_config_read(wpa_s->confanother, wpa_s->conf, true)) {
6927
0
      wpa_printf(MSG_ERROR,
6928
0
           "Failed to read or parse configuration '%s'.",
6929
0
           wpa_s->confanother);
6930
0
      return -1;
6931
0
    }
6932
6933
    /*
6934
     * Override ctrl_interface and driver_param if set on command
6935
     * line.
6936
     */
6937
0
    if (iface->ctrl_interface) {
6938
0
      os_free(wpa_s->conf->ctrl_interface);
6939
0
      wpa_s->conf->ctrl_interface =
6940
0
        os_strdup(iface->ctrl_interface);
6941
0
      if (!wpa_s->conf->ctrl_interface) {
6942
0
        wpa_printf(MSG_ERROR,
6943
0
             "Failed to duplicate control interface '%s'.",
6944
0
             iface->ctrl_interface);
6945
0
        return -1;
6946
0
      }
6947
0
    }
6948
6949
0
    if (iface->driver_param) {
6950
0
      os_free(wpa_s->conf->driver_param);
6951
0
      wpa_s->conf->driver_param =
6952
0
        os_strdup(iface->driver_param);
6953
0
      if (!wpa_s->conf->driver_param) {
6954
0
        wpa_printf(MSG_ERROR,
6955
0
             "Failed to duplicate driver param '%s'.",
6956
0
             iface->driver_param);
6957
0
        return -1;
6958
0
      }
6959
0
    }
6960
6961
0
    if (iface->p2p_mgmt && !iface->ctrl_interface) {
6962
0
      os_free(wpa_s->conf->ctrl_interface);
6963
0
      wpa_s->conf->ctrl_interface = NULL;
6964
0
    }
6965
0
  } else
6966
0
    wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
6967
0
                 iface->driver_param);
6968
6969
0
  if (wpa_s->conf == NULL) {
6970
0
    wpa_printf(MSG_ERROR, "\nNo configuration found.");
6971
0
    return -1;
6972
0
  }
6973
6974
0
  if (iface->ifname == NULL) {
6975
0
    wpa_printf(MSG_ERROR, "\nInterface name is required.");
6976
0
    return -1;
6977
0
  }
6978
0
  if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
6979
0
    wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
6980
0
         iface->ifname);
6981
0
    return -1;
6982
0
  }
6983
0
  os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
6984
#ifdef CONFIG_MATCH_IFACE
6985
  wpa_s->matched = iface->matched;
6986
#endif /* CONFIG_MATCH_IFACE */
6987
6988
0
  if (iface->bridge_ifname) {
6989
0
    if (os_strlen(iface->bridge_ifname) >=
6990
0
        sizeof(wpa_s->bridge_ifname)) {
6991
0
      wpa_printf(MSG_ERROR, "\nToo long bridge interface "
6992
0
           "name '%s'.", iface->bridge_ifname);
6993
0
      return -1;
6994
0
    }
6995
0
    os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
6996
0
         sizeof(wpa_s->bridge_ifname));
6997
0
  }
6998
6999
  /* RSNA Supplicant Key Management - INITIALIZE */
7000
0
  eapol_sm_notify_portEnabled(wpa_s->eapol, false);
7001
0
  eapol_sm_notify_portValid(wpa_s->eapol, false);
7002
7003
  /* Initialize driver interface and register driver event handler before
7004
   * L2 receive handler so that association events are processed before
7005
   * EAPOL-Key packets if both become available for the same select()
7006
   * call. */
7007
0
  if (wpas_init_driver(wpa_s, iface) < 0)
7008
0
    return -1;
7009
7010
0
  if (wpa_supplicant_init_wpa(wpa_s) < 0)
7011
0
    return -1;
7012
7013
0
  wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
7014
0
        wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
7015
0
        NULL);
7016
0
  wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
7017
7018
0
  if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
7019
0
      wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
7020
0
           wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
7021
0
    wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
7022
0
      "dot11RSNAConfigPMKLifetime");
7023
0
    return -1;
7024
0
  }
7025
7026
0
  if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
7027
0
      wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
7028
0
           wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
7029
0
    wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
7030
0
      "dot11RSNAConfigPMKReauthThreshold");
7031
0
    return -1;
7032
0
  }
7033
7034
0
  if (wpa_s->conf->dot11RSNAConfigSATimeout &&
7035
0
      wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
7036
0
           wpa_s->conf->dot11RSNAConfigSATimeout)) {
7037
0
    wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
7038
0
      "dot11RSNAConfigSATimeout");
7039
0
    return -1;
7040
0
  }
7041
7042
0
  wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
7043
0
                  &wpa_s->hw.num_modes,
7044
0
                  &wpa_s->hw.flags,
7045
0
                  &dfs_domain);
7046
0
  if (wpa_s->hw.modes) {
7047
0
    u16 i;
7048
7049
0
    for (i = 0; i < wpa_s->hw.num_modes; i++) {
7050
0
      if (wpa_s->hw.modes[i].vht_capab) {
7051
0
        wpa_s->hw_capab = CAPAB_VHT;
7052
0
        break;
7053
0
      }
7054
7055
0
      if (wpa_s->hw.modes[i].ht_capab &
7056
0
          HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
7057
0
        wpa_s->hw_capab = CAPAB_HT40;
7058
0
      else if (wpa_s->hw.modes[i].ht_capab &&
7059
0
         wpa_s->hw_capab == CAPAB_NO_HT_VHT)
7060
0
        wpa_s->hw_capab = CAPAB_HT;
7061
0
    }
7062
0
  }
7063
7064
0
  capa_res = wpa_drv_get_capa(wpa_s, &capa);
7065
0
  if (capa_res == 0) {
7066
0
    wpa_s->drv_capa_known = 1;
7067
0
    wpa_s->drv_flags = capa.flags;
7068
0
    wpa_s->drv_flags2 = capa.flags2;
7069
0
    wpa_s->drv_enc = capa.enc;
7070
0
    wpa_s->drv_rrm_flags = capa.rrm_flags;
7071
0
    wpa_s->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
7072
0
    wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
7073
0
    wpa_s->max_scan_ssids = capa.max_scan_ssids;
7074
0
    wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
7075
0
    wpa_s->max_sched_scan_plans = capa.max_sched_scan_plans;
7076
0
    wpa_s->max_sched_scan_plan_interval =
7077
0
      capa.max_sched_scan_plan_interval;
7078
0
    wpa_s->max_sched_scan_plan_iterations =
7079
0
      capa.max_sched_scan_plan_iterations;
7080
0
    wpa_s->sched_scan_supported = capa.sched_scan_supported;
7081
0
    wpa_s->max_match_sets = capa.max_match_sets;
7082
0
    wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
7083
0
    wpa_s->max_stations = capa.max_stations;
7084
0
    wpa_s->extended_capa = capa.extended_capa;
7085
0
    wpa_s->extended_capa_mask = capa.extended_capa_mask;
7086
0
    wpa_s->extended_capa_len = capa.extended_capa_len;
7087
0
    wpa_s->num_multichan_concurrent =
7088
0
      capa.num_multichan_concurrent;
7089
0
    wpa_s->wmm_ac_supported = capa.wmm_ac_supported;
7090
0
    wpa_s->max_num_akms = capa.max_num_akms;
7091
7092
0
    if (capa.mac_addr_rand_scan_supported)
7093
0
      wpa_s->mac_addr_rand_supported |= MAC_ADDR_RAND_SCAN;
7094
0
    if (wpa_s->sched_scan_supported &&
7095
0
        capa.mac_addr_rand_sched_scan_supported)
7096
0
      wpa_s->mac_addr_rand_supported |=
7097
0
        (MAC_ADDR_RAND_SCHED_SCAN | MAC_ADDR_RAND_PNO);
7098
7099
0
    wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
7100
0
    if (wpa_s->extended_capa &&
7101
0
        wpa_s->extended_capa_len >= 3 &&
7102
0
        wpa_s->extended_capa[2] & 0x40)
7103
0
      wpa_s->multi_bss_support = 1;
7104
0
  }
7105
#ifdef CONFIG_PASN
7106
  wpa_pasn_sm_set_caps(wpa_s->wpa, wpa_s->drv_flags2);
7107
#endif /* CONFIG_PASN */
7108
0
  if (wpa_s->max_remain_on_chan == 0)
7109
0
    wpa_s->max_remain_on_chan = 1000;
7110
7111
  /*
7112
   * Only take p2p_mgmt parameters when P2P Device is supported.
7113
   * Doing it here as it determines whether l2_packet_init() will be done
7114
   * during wpa_supplicant_driver_init().
7115
   */
7116
0
  if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
7117
0
    wpa_s->p2p_mgmt = iface->p2p_mgmt;
7118
7119
0
  if (wpa_s->num_multichan_concurrent == 0)
7120
0
    wpa_s->num_multichan_concurrent = 1;
7121
7122
0
  if (wpa_supplicant_driver_init(wpa_s) < 0)
7123
0
    return -1;
7124
7125
#ifdef CONFIG_TDLS
7126
  if (!iface->p2p_mgmt && wpa_tdls_init(wpa_s->wpa))
7127
    return -1;
7128
#endif /* CONFIG_TDLS */
7129
7130
0
  if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
7131
0
      wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
7132
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
7133
0
    return -1;
7134
0
  }
7135
7136
#ifdef CONFIG_FST
7137
  if (wpa_s->conf->fst_group_id) {
7138
    struct fst_iface_cfg cfg;
7139
    struct fst_wpa_obj iface_obj;
7140
7141
    fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
7142
    os_strlcpy(cfg.group_id, wpa_s->conf->fst_group_id,
7143
         sizeof(cfg.group_id));
7144
    cfg.priority = wpa_s->conf->fst_priority;
7145
    cfg.llt = wpa_s->conf->fst_llt;
7146
7147
    wpa_s->fst = fst_attach(wpa_s->ifname, wpa_s->own_addr,
7148
          &iface_obj, &cfg);
7149
    if (!wpa_s->fst) {
7150
      wpa_msg(wpa_s, MSG_ERROR,
7151
        "FST: Cannot attach iface %s to group %s",
7152
        wpa_s->ifname, cfg.group_id);
7153
      return -1;
7154
    }
7155
  }
7156
#endif /* CONFIG_FST */
7157
7158
0
  if (wpas_wps_init(wpa_s))
7159
0
    return -1;
7160
7161
#ifdef CONFIG_GAS_SERVER
7162
  wpa_s->gas_server = gas_server_init(wpa_s, wpas_gas_server_tx);
7163
  if (!wpa_s->gas_server) {
7164
    wpa_printf(MSG_ERROR, "Failed to initialize GAS server");
7165
    return -1;
7166
  }
7167
#endif /* CONFIG_GAS_SERVER */
7168
7169
#ifdef CONFIG_DPP
7170
  if (wpas_dpp_init(wpa_s) < 0)
7171
    return -1;
7172
#endif /* CONFIG_DPP */
7173
7174
0
  if (wpa_supplicant_init_eapol(wpa_s) < 0)
7175
0
    return -1;
7176
0
  wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
7177
7178
0
  wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
7179
0
  if (wpa_s->ctrl_iface == NULL) {
7180
0
    wpa_printf(MSG_ERROR,
7181
0
         "Failed to initialize control interface '%s'.\n"
7182
0
         "You may have another wpa_supplicant process "
7183
0
         "already running or the file was\n"
7184
0
         "left by an unclean termination of wpa_supplicant "
7185
0
         "in which case you will need\n"
7186
0
         "to manually remove this file before starting "
7187
0
         "wpa_supplicant again.\n",
7188
0
         wpa_s->conf->ctrl_interface);
7189
0
    return -1;
7190
0
  }
7191
7192
0
  wpa_s->gas = gas_query_init(wpa_s);
7193
0
  if (wpa_s->gas == NULL) {
7194
0
    wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
7195
0
    return -1;
7196
0
  }
7197
7198
0
  if ((!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) ||
7199
0
       wpa_s->p2p_mgmt) &&
7200
0
      wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
7201
0
    wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
7202
0
    return -1;
7203
0
  }
7204
7205
0
  if (wpa_bss_init(wpa_s) < 0)
7206
0
    return -1;
7207
7208
#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
7209
#ifdef CONFIG_MESH
7210
  dl_list_init(&wpa_s->mesh_external_pmksa_cache);
7211
#endif /* CONFIG_MESH */
7212
#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
7213
7214
  /*
7215
   * Set Wake-on-WLAN triggers, if configured.
7216
   * Note: We don't restore/remove the triggers on shutdown (it doesn't
7217
   * have effect anyway when the interface is down).
7218
   */
7219
0
  if (capa_res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
7220
0
    return -1;
7221
7222
#ifdef CONFIG_EAP_PROXY
7223
{
7224
  size_t len;
7225
  wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
7226
                 wpa_s->imsi, &len);
7227
  if (wpa_s->mnc_len > 0) {
7228
    wpa_s->imsi[len] = '\0';
7229
    wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
7230
         wpa_s->imsi, wpa_s->mnc_len);
7231
  } else {
7232
    wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
7233
  }
7234
}
7235
#endif /* CONFIG_EAP_PROXY */
7236
7237
0
  if (pcsc_reader_init(wpa_s) < 0)
7238
0
    return -1;
7239
7240
0
  if (wpas_init_ext_pw(wpa_s) < 0)
7241
0
    return -1;
7242
7243
0
  wpas_rrm_reset(wpa_s);
7244
7245
0
  wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
7246
7247
0
#ifdef CONFIG_HS20
7248
0
  hs20_init(wpa_s);
7249
0
#endif /* CONFIG_HS20 */
7250
#ifdef CONFIG_MBO
7251
  if (!wpa_s->disable_mbo_oce && wpa_s->conf->oce) {
7252
    if ((wpa_s->conf->oce & OCE_STA) &&
7253
        (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
7254
      wpa_s->enable_oce = OCE_STA;
7255
    if ((wpa_s->conf->oce & OCE_STA_CFON) &&
7256
        (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
7257
      /* TODO: Need to add STA-CFON support */
7258
      wpa_printf(MSG_ERROR,
7259
           "OCE STA-CFON feature is not yet supported");
7260
    }
7261
  }
7262
  wpas_mbo_update_non_pref_chan(wpa_s, wpa_s->conf->non_pref_chan);
7263
#endif /* CONFIG_MBO */
7264
7265
0
  wpa_supplicant_set_default_scan_ies(wpa_s);
7266
7267
0
  return 0;
7268
0
}
7269
7270
7271
static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
7272
          int notify, int terminate)
7273
0
{
7274
0
  struct wpa_global *global = wpa_s->global;
7275
0
  struct wpa_supplicant *iface, *prev;
7276
7277
0
  if (wpa_s == wpa_s->parent)
7278
0
    wpas_p2p_group_remove(wpa_s, "*");
7279
7280
0
  iface = global->ifaces;
7281
0
  while (iface) {
7282
0
    if (iface->p2pdev == wpa_s)
7283
0
      iface->p2pdev = iface->parent;
7284
0
    if (iface == wpa_s || iface->parent != wpa_s) {
7285
0
      iface = iface->next;
7286
0
      continue;
7287
0
    }
7288
0
    wpa_printf(MSG_DEBUG,
7289
0
         "Remove remaining child interface %s from parent %s",
7290
0
         iface->ifname, wpa_s->ifname);
7291
0
    prev = iface;
7292
0
    iface = iface->next;
7293
0
    wpa_supplicant_remove_iface(global, prev, terminate);
7294
0
  }
7295
7296
0
  wpa_s->disconnected = 1;
7297
0
  if (wpa_s->drv_priv) {
7298
    /*
7299
     * Don't deauthenticate if WoWLAN is enable and not explicitly
7300
     * been configured to disconnect.
7301
     */
7302
0
    if (!wpa_drv_get_wowlan(wpa_s) ||
7303
0
        wpa_s->conf->wowlan_disconnect_on_deinit) {
7304
0
      wpa_supplicant_deauthenticate(
7305
0
        wpa_s, WLAN_REASON_DEAUTH_LEAVING);
7306
7307
0
      wpa_drv_set_countermeasures(wpa_s, 0);
7308
0
      wpa_clear_keys(wpa_s, NULL);
7309
0
    } else {
7310
0
      wpa_msg(wpa_s, MSG_INFO,
7311
0
        "Do not deauthenticate as part of interface deinit since WoWLAN is enabled");
7312
0
    }
7313
0
  }
7314
7315
0
  wpa_supplicant_cleanup(wpa_s);
7316
0
  wpas_p2p_deinit_iface(wpa_s);
7317
7318
0
  wpas_ctrl_radio_work_flush(wpa_s);
7319
0
  radio_remove_interface(wpa_s);
7320
7321
#ifdef CONFIG_FST
7322
  if (wpa_s->fst) {
7323
    fst_detach(wpa_s->fst);
7324
    wpa_s->fst = NULL;
7325
  }
7326
  if (wpa_s->received_mb_ies) {
7327
    wpabuf_free(wpa_s->received_mb_ies);
7328
    wpa_s->received_mb_ies = NULL;
7329
  }
7330
#endif /* CONFIG_FST */
7331
7332
0
  if (wpa_s->drv_priv)
7333
0
    wpa_drv_deinit(wpa_s);
7334
7335
0
  if (notify)
7336
0
    wpas_notify_iface_removed(wpa_s);
7337
7338
0
  if (terminate)
7339
0
    wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
7340
7341
0
  wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
7342
0
  wpa_s->ctrl_iface = NULL;
7343
7344
#ifdef CONFIG_MESH
7345
  if (wpa_s->ifmsh) {
7346
    wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh, true);
7347
    wpa_s->ifmsh = NULL;
7348
  }
7349
#endif /* CONFIG_MESH */
7350
7351
0
  if (wpa_s->conf != NULL) {
7352
0
    wpa_config_free(wpa_s->conf);
7353
0
    wpa_s->conf = NULL;
7354
0
  }
7355
7356
0
  os_free(wpa_s->ssids_from_scan_req);
7357
0
  os_free(wpa_s->last_scan_freqs);
7358
7359
0
  os_free(wpa_s);
7360
0
}
7361
7362
7363
#ifdef CONFIG_MATCH_IFACE
7364
7365
/**
7366
 * wpa_supplicant_match_iface - Match an interface description to a name
7367
 * @global: Pointer to global data from wpa_supplicant_init()
7368
 * @ifname: Name of the interface to match
7369
 * Returns: Pointer to the created interface description or %NULL on failure
7370
 */
7371
struct wpa_interface * wpa_supplicant_match_iface(struct wpa_global *global,
7372
              const char *ifname)
7373
{
7374
  int i;
7375
  struct wpa_interface *iface, *miface;
7376
7377
  for (i = 0; i < global->params.match_iface_count; i++) {
7378
    miface = &global->params.match_ifaces[i];
7379
    if (!miface->ifname ||
7380
        fnmatch(miface->ifname, ifname, 0) == 0) {
7381
      iface = os_zalloc(sizeof(*iface));
7382
      if (!iface)
7383
        return NULL;
7384
      *iface = *miface;
7385
      if (!miface->ifname)
7386
        iface->matched = WPA_IFACE_MATCHED_NULL;
7387
      else
7388
        iface->matched = WPA_IFACE_MATCHED;
7389
      iface->ifname = ifname;
7390
      return iface;
7391
    }
7392
  }
7393
7394
  return NULL;
7395
}
7396
7397
7398
/**
7399
 * wpa_supplicant_match_existing - Match existing interfaces
7400
 * @global: Pointer to global data from wpa_supplicant_init()
7401
 * Returns: 0 on success, -1 on failure
7402
 */
7403
static int wpa_supplicant_match_existing(struct wpa_global *global)
7404
{
7405
  struct if_nameindex *ifi, *ifp;
7406
  struct wpa_supplicant *wpa_s;
7407
  struct wpa_interface *iface;
7408
7409
  ifp = if_nameindex();
7410
  if (!ifp) {
7411
    wpa_printf(MSG_ERROR, "if_nameindex: %s", strerror(errno));
7412
    return -1;
7413
  }
7414
7415
  for (ifi = ifp; ifi->if_name; ifi++) {
7416
    wpa_s = wpa_supplicant_get_iface(global, ifi->if_name);
7417
    if (wpa_s)
7418
      continue;
7419
    iface = wpa_supplicant_match_iface(global, ifi->if_name);
7420
    if (iface) {
7421
      wpa_supplicant_add_iface(global, iface, NULL);
7422
      os_free(iface);
7423
    }
7424
  }
7425
7426
  if_freenameindex(ifp);
7427
  return 0;
7428
}
7429
7430
#endif /* CONFIG_MATCH_IFACE */
7431
7432
7433
/**
7434
 * wpa_supplicant_add_iface - Add a new network interface
7435
 * @global: Pointer to global data from wpa_supplicant_init()
7436
 * @iface: Interface configuration options
7437
 * @parent: Parent interface or %NULL to assign new interface as parent
7438
 * Returns: Pointer to the created interface or %NULL on failure
7439
 *
7440
 * This function is used to add new network interfaces for %wpa_supplicant.
7441
 * This can be called before wpa_supplicant_run() to add interfaces before the
7442
 * main event loop has been started. In addition, new interfaces can be added
7443
 * dynamically while %wpa_supplicant is already running. This could happen,
7444
 * e.g., when a hotplug network adapter is inserted.
7445
 */
7446
struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
7447
             struct wpa_interface *iface,
7448
             struct wpa_supplicant *parent)
7449
0
{
7450
0
  struct wpa_supplicant *wpa_s;
7451
0
  struct wpa_interface t_iface;
7452
0
  struct wpa_ssid *ssid;
7453
7454
0
  if (global == NULL || iface == NULL)
7455
0
    return NULL;
7456
7457
0
  wpa_s = wpa_supplicant_alloc(parent);
7458
0
  if (wpa_s == NULL)
7459
0
    return NULL;
7460
7461
0
  wpa_s->global = global;
7462
7463
0
  t_iface = *iface;
7464
0
  if (global->params.override_driver) {
7465
0
    wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
7466
0
         "('%s' -> '%s')",
7467
0
         iface->driver, global->params.override_driver);
7468
0
    t_iface.driver = global->params.override_driver;
7469
0
  }
7470
0
  if (global->params.override_ctrl_interface) {
7471
0
    wpa_printf(MSG_DEBUG, "Override interface parameter: "
7472
0
         "ctrl_interface ('%s' -> '%s')",
7473
0
         iface->ctrl_interface,
7474
0
         global->params.override_ctrl_interface);
7475
0
    t_iface.ctrl_interface =
7476
0
      global->params.override_ctrl_interface;
7477
0
  }
7478
0
  if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
7479
0
    wpa_printf(MSG_DEBUG, "Failed to add interface %s",
7480
0
         iface->ifname);
7481
0
    wpa_supplicant_deinit_iface(wpa_s, 0, 0);
7482
0
    return NULL;
7483
0
  }
7484
7485
0
  if (iface->p2p_mgmt == 0) {
7486
    /* Notify the control interfaces about new iface */
7487
0
    if (wpas_notify_iface_added(wpa_s)) {
7488
0
      wpa_supplicant_deinit_iface(wpa_s, 1, 0);
7489
0
      return NULL;
7490
0
    }
7491
7492
0
    for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
7493
0
      wpas_notify_network_added(wpa_s, ssid);
7494
0
  }
7495
7496
0
  wpa_s->next = global->ifaces;
7497
0
  global->ifaces = wpa_s;
7498
7499
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
7500
0
  wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
7501
7502
#ifdef CONFIG_P2P
7503
  if (wpa_s->global->p2p == NULL &&
7504
      !wpa_s->global->p2p_disabled && !wpa_s->conf->p2p_disabled &&
7505
      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
7506
      wpas_p2p_add_p2pdev_interface(
7507
        wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
7508
    wpa_printf(MSG_INFO,
7509
         "P2P: Failed to enable P2P Device interface");
7510
    /* Try to continue without. P2P will be disabled. */
7511
  }
7512
#endif /* CONFIG_P2P */
7513
7514
0
  return wpa_s;
7515
0
}
7516
7517
7518
/**
7519
 * wpa_supplicant_remove_iface - Remove a network interface
7520
 * @global: Pointer to global data from wpa_supplicant_init()
7521
 * @wpa_s: Pointer to the network interface to be removed
7522
 * Returns: 0 if interface was removed, -1 if interface was not found
7523
 *
7524
 * This function can be used to dynamically remove network interfaces from
7525
 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
7526
 * addition, this function is used to remove all remaining interfaces when
7527
 * %wpa_supplicant is terminated.
7528
 */
7529
int wpa_supplicant_remove_iface(struct wpa_global *global,
7530
        struct wpa_supplicant *wpa_s,
7531
        int terminate)
7532
0
{
7533
0
  struct wpa_supplicant *prev;
7534
#ifdef CONFIG_MESH
7535
  unsigned int mesh_if_created = wpa_s->mesh_if_created;
7536
  char *ifname = NULL;
7537
  struct wpa_supplicant *parent = wpa_s->parent;
7538
#endif /* CONFIG_MESH */
7539
7540
  /* Remove interface from the global list of interfaces */
7541
0
  prev = global->ifaces;
7542
0
  if (prev == wpa_s) {
7543
0
    global->ifaces = wpa_s->next;
7544
0
  } else {
7545
0
    while (prev && prev->next != wpa_s)
7546
0
      prev = prev->next;
7547
0
    if (prev == NULL)
7548
0
      return -1;
7549
0
    prev->next = wpa_s->next;
7550
0
  }
7551
7552
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
7553
7554
#ifdef CONFIG_MESH
7555
  if (mesh_if_created) {
7556
    ifname = os_strdup(wpa_s->ifname);
7557
    if (ifname == NULL) {
7558
      wpa_dbg(wpa_s, MSG_ERROR,
7559
        "mesh: Failed to malloc ifname");
7560
      return -1;
7561
    }
7562
  }
7563
#endif /* CONFIG_MESH */
7564
7565
0
  if (global->p2p_group_formation == wpa_s)
7566
0
    global->p2p_group_formation = NULL;
7567
0
  if (global->p2p_invite_group == wpa_s)
7568
0
    global->p2p_invite_group = NULL;
7569
0
  wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
7570
7571
#ifdef CONFIG_MESH
7572
  if (mesh_if_created) {
7573
    wpa_drv_if_remove(parent, WPA_IF_MESH, ifname);
7574
    os_free(ifname);
7575
  }
7576
#endif /* CONFIG_MESH */
7577
7578
0
  return 0;
7579
0
}
7580
7581
7582
/**
7583
 * wpa_supplicant_get_eap_mode - Get the current EAP mode
7584
 * @wpa_s: Pointer to the network interface
7585
 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
7586
 */
7587
const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
7588
0
{
7589
0
  const char *eapol_method;
7590
7591
0
        if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
7592
0
            wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
7593
0
    return "NO-EAP";
7594
0
  }
7595
7596
0
  eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
7597
0
  if (eapol_method == NULL)
7598
0
    return "UNKNOWN-EAP";
7599
7600
0
  return eapol_method;
7601
0
}
7602
7603
7604
/**
7605
 * wpa_supplicant_get_iface - Get a new network interface
7606
 * @global: Pointer to global data from wpa_supplicant_init()
7607
 * @ifname: Interface name
7608
 * Returns: Pointer to the interface or %NULL if not found
7609
 */
7610
struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
7611
             const char *ifname)
7612
0
{
7613
0
  struct wpa_supplicant *wpa_s;
7614
7615
0
  for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7616
0
    if (os_strcmp(wpa_s->ifname, ifname) == 0)
7617
0
      return wpa_s;
7618
0
  }
7619
0
  return NULL;
7620
0
}
7621
7622
7623
#ifndef CONFIG_NO_WPA_MSG
7624
static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
7625
0
{
7626
0
  struct wpa_supplicant *wpa_s = ctx;
7627
0
  if (wpa_s == NULL)
7628
0
    return NULL;
7629
0
  return wpa_s->ifname;
7630
0
}
7631
#endif /* CONFIG_NO_WPA_MSG */
7632
7633
7634
#ifndef WPA_SUPPLICANT_CLEANUP_INTERVAL
7635
0
#define WPA_SUPPLICANT_CLEANUP_INTERVAL 10
7636
#endif /* WPA_SUPPLICANT_CLEANUP_INTERVAL */
7637
7638
/* Periodic cleanup tasks */
7639
static void wpas_periodic(void *eloop_ctx, void *timeout_ctx)
7640
0
{
7641
0
  struct wpa_global *global = eloop_ctx;
7642
0
  struct wpa_supplicant *wpa_s;
7643
7644
0
  eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
7645
0
             wpas_periodic, global, NULL);
7646
7647
#ifdef CONFIG_P2P
7648
  if (global->p2p)
7649
    p2p_expire_peers(global->p2p);
7650
#endif /* CONFIG_P2P */
7651
7652
0
  for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7653
0
    wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
7654
#ifdef CONFIG_AP
7655
    ap_periodic(wpa_s);
7656
#endif /* CONFIG_AP */
7657
0
  }
7658
0
}
7659
7660
7661
/**
7662
 * wpa_supplicant_init - Initialize %wpa_supplicant
7663
 * @params: Parameters for %wpa_supplicant
7664
 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
7665
 *
7666
 * This function is used to initialize %wpa_supplicant. After successful
7667
 * initialization, the returned data pointer can be used to add and remove
7668
 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
7669
 */
7670
struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
7671
0
{
7672
0
  struct wpa_global *global;
7673
0
  int ret, i;
7674
7675
0
  if (params == NULL)
7676
0
    return NULL;
7677
7678
#ifdef CONFIG_DRIVER_NDIS
7679
  {
7680
    void driver_ndis_init_ops(void);
7681
    driver_ndis_init_ops();
7682
  }
7683
#endif /* CONFIG_DRIVER_NDIS */
7684
7685
0
#ifndef CONFIG_NO_WPA_MSG
7686
0
  wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
7687
0
#endif /* CONFIG_NO_WPA_MSG */
7688
7689
0
  if (params->wpa_debug_file_path)
7690
0
    wpa_debug_open_file(params->wpa_debug_file_path);
7691
0
  if (!params->wpa_debug_file_path && !params->wpa_debug_syslog)
7692
0
    wpa_debug_setup_stdout();
7693
0
  if (params->wpa_debug_syslog)
7694
0
    wpa_debug_open_syslog();
7695
0
  if (params->wpa_debug_tracing) {
7696
0
    ret = wpa_debug_open_linux_tracing();
7697
0
    if (ret) {
7698
0
      wpa_printf(MSG_ERROR,
7699
0
           "Failed to enable trace logging");
7700
0
      return NULL;
7701
0
    }
7702
0
  }
7703
7704
0
  ret = eap_register_methods();
7705
0
  if (ret) {
7706
0
    wpa_printf(MSG_ERROR, "Failed to register EAP methods");
7707
0
    if (ret == -2)
7708
0
      wpa_printf(MSG_ERROR, "Two or more EAP methods used "
7709
0
           "the same EAP type.");
7710
0
    return NULL;
7711
0
  }
7712
7713
0
  global = os_zalloc(sizeof(*global));
7714
0
  if (global == NULL)
7715
0
    return NULL;
7716
0
  dl_list_init(&global->p2p_srv_bonjour);
7717
0
  dl_list_init(&global->p2p_srv_upnp);
7718
0
  global->params.daemonize = params->daemonize;
7719
0
  global->params.wait_for_monitor = params->wait_for_monitor;
7720
0
  global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
7721
7722
0
  if (params->pid_file) {
7723
0
    global->params.pid_file = os_strdup(params->pid_file);
7724
0
    if (!global->params.pid_file) {
7725
0
      wpa_supplicant_deinit(global);
7726
0
      return NULL;
7727
0
    }
7728
0
  }
7729
7730
0
  if (params->ctrl_interface) {
7731
0
    global->params.ctrl_interface =
7732
0
      os_strdup(params->ctrl_interface);
7733
0
    if (!global->params.ctrl_interface) {
7734
0
      wpa_supplicant_deinit(global);
7735
0
      return NULL;
7736
0
    }
7737
0
  }
7738
7739
0
  if (params->ctrl_interface_group) {
7740
0
    global->params.ctrl_interface_group =
7741
0
      os_strdup(params->ctrl_interface_group);
7742
0
    if (!global->params.ctrl_interface_group) {
7743
0
      wpa_supplicant_deinit(global);
7744
0
      return NULL;
7745
0
    }
7746
0
  }
7747
7748
0
  if (params->override_driver) {
7749
0
    global->params.override_driver =
7750
0
      os_strdup(params->override_driver);
7751
0
    if (!global->params.override_driver) {
7752
0
      wpa_supplicant_deinit(global);
7753
0
      return NULL;
7754
0
    }
7755
0
  }
7756
7757
0
  if (params->override_ctrl_interface) {
7758
0
    global->params.override_ctrl_interface =
7759
0
      os_strdup(params->override_ctrl_interface);
7760
0
    if (!global->params.override_ctrl_interface) {
7761
0
      wpa_supplicant_deinit(global);
7762
0
      return NULL;
7763
0
    }
7764
0
  }
7765
7766
#ifdef CONFIG_MATCH_IFACE
7767
  global->params.match_iface_count = params->match_iface_count;
7768
  if (params->match_iface_count) {
7769
    global->params.match_ifaces =
7770
      os_calloc(params->match_iface_count,
7771
          sizeof(struct wpa_interface));
7772
    if (!global->params.match_ifaces) {
7773
      wpa_printf(MSG_ERROR,
7774
           "Failed to allocate match interfaces");
7775
      wpa_supplicant_deinit(global);
7776
      return NULL;
7777
    }
7778
    os_memcpy(global->params.match_ifaces,
7779
        params->match_ifaces,
7780
        params->match_iface_count *
7781
        sizeof(struct wpa_interface));
7782
  }
7783
#endif /* CONFIG_MATCH_IFACE */
7784
#ifdef CONFIG_P2P
7785
  if (params->conf_p2p_dev) {
7786
    global->params.conf_p2p_dev =
7787
      os_strdup(params->conf_p2p_dev);
7788
    if (!global->params.conf_p2p_dev) {
7789
      wpa_printf(MSG_ERROR, "Failed to allocate conf p2p");
7790
      wpa_supplicant_deinit(global);
7791
      return NULL;
7792
    }
7793
  }
7794
#endif /* CONFIG_P2P */
7795
0
  wpa_debug_level = global->params.wpa_debug_level =
7796
0
    params->wpa_debug_level;
7797
0
  wpa_debug_show_keys = global->params.wpa_debug_show_keys =
7798
0
    params->wpa_debug_show_keys;
7799
0
  wpa_debug_timestamp = global->params.wpa_debug_timestamp =
7800
0
    params->wpa_debug_timestamp;
7801
7802
0
  wpa_printf(MSG_DEBUG, "wpa_supplicant v%s", VERSION_STR);
7803
7804
0
  if (eloop_init()) {
7805
0
    wpa_printf(MSG_ERROR, "Failed to initialize event loop");
7806
0
    wpa_supplicant_deinit(global);
7807
0
    return NULL;
7808
0
  }
7809
7810
0
  random_init(params->entropy_file);
7811
7812
0
  global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
7813
0
  if (global->ctrl_iface == NULL) {
7814
0
    wpa_supplicant_deinit(global);
7815
0
    return NULL;
7816
0
  }
7817
7818
0
  if (wpas_notify_supplicant_initialized(global)) {
7819
0
    wpa_supplicant_deinit(global);
7820
0
    return NULL;
7821
0
  }
7822
7823
0
  for (i = 0; wpa_drivers[i]; i++)
7824
0
    global->drv_count++;
7825
0
  if (global->drv_count == 0) {
7826
0
    wpa_printf(MSG_ERROR, "No drivers enabled");
7827
0
    wpa_supplicant_deinit(global);
7828
0
    return NULL;
7829
0
  }
7830
0
  global->drv_priv = os_calloc(global->drv_count, sizeof(void *));
7831
0
  if (global->drv_priv == NULL) {
7832
0
    wpa_supplicant_deinit(global);
7833
0
    return NULL;
7834
0
  }
7835
7836
#ifdef CONFIG_WIFI_DISPLAY
7837
  if (wifi_display_init(global) < 0) {
7838
    wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
7839
    wpa_supplicant_deinit(global);
7840
    return NULL;
7841
  }
7842
#endif /* CONFIG_WIFI_DISPLAY */
7843
7844
0
  eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
7845
0
             wpas_periodic, global, NULL);
7846
7847
0
  return global;
7848
0
}
7849
7850
7851
/**
7852
 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
7853
 * @global: Pointer to global data from wpa_supplicant_init()
7854
 * Returns: 0 after successful event loop run, -1 on failure
7855
 *
7856
 * This function starts the main event loop and continues running as long as
7857
 * there are any remaining events. In most cases, this function is running as
7858
 * long as the %wpa_supplicant process in still in use.
7859
 */
7860
int wpa_supplicant_run(struct wpa_global *global)
7861
0
{
7862
0
  struct wpa_supplicant *wpa_s;
7863
7864
0
  if (global->params.daemonize &&
7865
0
      (wpa_supplicant_daemon(global->params.pid_file) ||
7866
0
       eloop_sock_requeue()))
7867
0
    return -1;
7868
7869
#ifdef CONFIG_MATCH_IFACE
7870
  if (wpa_supplicant_match_existing(global))
7871
    return -1;
7872
#endif
7873
7874
0
  if (global->params.wait_for_monitor) {
7875
0
    for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
7876
0
      if (wpa_s->ctrl_iface && !wpa_s->p2p_mgmt)
7877
0
        wpa_supplicant_ctrl_iface_wait(
7878
0
          wpa_s->ctrl_iface);
7879
0
  }
7880
7881
0
  eloop_register_signal_terminate(wpa_supplicant_terminate, global);
7882
0
  eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
7883
7884
0
  eloop_run();
7885
7886
0
  return 0;
7887
0
}
7888
7889
7890
/**
7891
 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
7892
 * @global: Pointer to global data from wpa_supplicant_init()
7893
 *
7894
 * This function is called to deinitialize %wpa_supplicant and to free all
7895
 * allocated resources. Remaining network interfaces will also be removed.
7896
 */
7897
void wpa_supplicant_deinit(struct wpa_global *global)
7898
0
{
7899
0
  int i;
7900
7901
0
  if (global == NULL)
7902
0
    return;
7903
7904
0
  eloop_cancel_timeout(wpas_periodic, global, NULL);
7905
7906
#ifdef CONFIG_WIFI_DISPLAY
7907
  wifi_display_deinit(global);
7908
#endif /* CONFIG_WIFI_DISPLAY */
7909
7910
0
  while (global->ifaces)
7911
0
    wpa_supplicant_remove_iface(global, global->ifaces, 1);
7912
7913
0
  if (global->ctrl_iface)
7914
0
    wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
7915
7916
0
  wpas_notify_supplicant_deinitialized(global);
7917
7918
0
  eap_peer_unregister_methods();
7919
#ifdef CONFIG_AP
7920
  eap_server_unregister_methods();
7921
#endif /* CONFIG_AP */
7922
7923
0
  for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
7924
0
    if (!global->drv_priv[i])
7925
0
      continue;
7926
0
    wpa_drivers[i]->global_deinit(global->drv_priv[i]);
7927
0
  }
7928
0
  os_free(global->drv_priv);
7929
7930
0
  random_deinit();
7931
7932
0
  eloop_destroy();
7933
7934
0
  if (global->params.pid_file) {
7935
0
    os_daemonize_terminate(global->params.pid_file);
7936
0
    os_free(global->params.pid_file);
7937
0
  }
7938
0
  os_free(global->params.ctrl_interface);
7939
0
  os_free(global->params.ctrl_interface_group);
7940
0
  os_free(global->params.override_driver);
7941
0
  os_free(global->params.override_ctrl_interface);
7942
#ifdef CONFIG_MATCH_IFACE
7943
  os_free(global->params.match_ifaces);
7944
#endif /* CONFIG_MATCH_IFACE */
7945
#ifdef CONFIG_P2P
7946
  os_free(global->params.conf_p2p_dev);
7947
#endif /* CONFIG_P2P */
7948
7949
0
  os_free(global->p2p_disallow_freq.range);
7950
0
  os_free(global->p2p_go_avoid_freq.range);
7951
0
  os_free(global->add_psk);
7952
7953
0
  os_free(global);
7954
0
  wpa_debug_close_syslog();
7955
0
  wpa_debug_close_file();
7956
0
  wpa_debug_close_linux_tracing();
7957
0
}
7958
7959
7960
void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
7961
0
{
7962
0
  if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7963
0
      wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7964
0
    char country[3];
7965
0
    country[0] = wpa_s->conf->country[0];
7966
0
    country[1] = wpa_s->conf->country[1];
7967
0
    country[2] = '\0';
7968
0
    if (wpa_drv_set_country(wpa_s, country) < 0) {
7969
0
      wpa_printf(MSG_ERROR, "Failed to set country code "
7970
0
           "'%s'", country);
7971
0
    }
7972
0
  }
7973
7974
0
  if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
7975
0
    wpas_init_ext_pw(wpa_s);
7976
7977
0
  if (wpa_s->conf->changed_parameters & CFG_CHANGED_SCHED_SCAN_PLANS)
7978
0
    wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
7979
7980
0
  if (wpa_s->conf->changed_parameters & CFG_CHANGED_WOWLAN_TRIGGERS) {
7981
0
    struct wpa_driver_capa capa;
7982
0
    int res = wpa_drv_get_capa(wpa_s, &capa);
7983
7984
0
    if (res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
7985
0
      wpa_printf(MSG_ERROR,
7986
0
           "Failed to update wowlan_triggers to '%s'",
7987
0
           wpa_s->conf->wowlan_triggers);
7988
0
  }
7989
7990
0
  if (wpa_s->conf->changed_parameters & CFG_CHANGED_DISABLE_BTM)
7991
0
    wpa_supplicant_set_default_scan_ies(wpa_s);
7992
7993
#ifdef CONFIG_BGSCAN
7994
  /*
7995
   * We default to global bgscan parameters only when per-network bgscan
7996
   * parameters aren't set. Only bother resetting bgscan parameters if
7997
   * this is the case.
7998
   */
7999
  if ((wpa_s->conf->changed_parameters & CFG_CHANGED_BGSCAN) &&
8000
      wpa_s->current_ssid && !wpa_s->current_ssid->bgscan &&
8001
      wpa_s->wpa_state == WPA_COMPLETED)
8002
    wpa_supplicant_reset_bgscan(wpa_s);
8003
#endif /* CONFIG_BGSCAN */
8004
8005
#ifdef CONFIG_WPS
8006
  wpas_wps_update_config(wpa_s);
8007
#endif /* CONFIG_WPS */
8008
0
  wpas_p2p_update_config(wpa_s);
8009
0
  wpa_s->conf->changed_parameters = 0;
8010
0
}
8011
8012
8013
void add_freq(int *freqs, int *num_freqs, int freq)
8014
0
{
8015
0
  int i;
8016
8017
0
  for (i = 0; i < *num_freqs; i++) {
8018
0
    if (freqs[i] == freq)
8019
0
      return;
8020
0
  }
8021
8022
0
  freqs[*num_freqs] = freq;
8023
0
  (*num_freqs)++;
8024
0
}
8025
8026
8027
static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
8028
0
{
8029
0
  struct wpa_bss *bss, *cbss;
8030
0
  const int max_freqs = 10;
8031
0
  int *freqs;
8032
0
  int num_freqs = 0;
8033
8034
0
  freqs = os_calloc(max_freqs + 1, sizeof(int));
8035
0
  if (freqs == NULL)
8036
0
    return NULL;
8037
8038
0
  cbss = wpa_s->current_bss;
8039
8040
0
  dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
8041
0
    if (bss == cbss)
8042
0
      continue;
8043
0
    if (bss->ssid_len == cbss->ssid_len &&
8044
0
        os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
8045
0
        !wpa_bssid_ignore_is_listed(wpa_s, bss->bssid)) {
8046
0
      add_freq(freqs, &num_freqs, bss->freq);
8047
0
      if (num_freqs == max_freqs)
8048
0
        break;
8049
0
    }
8050
0
  }
8051
8052
0
  if (num_freqs == 0) {
8053
0
    os_free(freqs);
8054
0
    freqs = NULL;
8055
0
  }
8056
8057
0
  return freqs;
8058
0
}
8059
8060
8061
void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
8062
0
{
8063
0
  int timeout;
8064
0
  int count;
8065
0
  int *freqs = NULL;
8066
8067
0
  wpas_connect_work_done(wpa_s);
8068
8069
  /*
8070
   * Remove possible authentication timeout since the connection failed.
8071
   */
8072
0
  eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
8073
8074
  /*
8075
   * There is no point in ignoring the AP temporarily if this event is
8076
   * generated based on local request to disconnect.
8077
   */
8078
0
  if (wpa_s->own_disconnect_req || wpa_s->own_reconnect_req) {
8079
0
    wpa_s->own_disconnect_req = 0;
8080
0
    wpa_dbg(wpa_s, MSG_DEBUG,
8081
0
      "Ignore connection failure due to local request to disconnect");
8082
0
    return;
8083
0
  }
8084
0
  if (wpa_s->disconnected) {
8085
0
    wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
8086
0
      "indication since interface has been put into "
8087
0
      "disconnected state");
8088
0
    return;
8089
0
  }
8090
8091
  /*
8092
   * Add the failed BSSID into the ignore list and speed up next scan
8093
   * attempt if there could be other APs that could accept association.
8094
   */
8095
0
  count = wpa_bssid_ignore_add(wpa_s, bssid);
8096
0
  if (count == 1 && wpa_s->current_bss) {
8097
    /*
8098
     * This BSS was not in the ignore list before. If there is
8099
     * another BSS available for the same ESS, we should try that
8100
     * next. Otherwise, we may as well try this one once more
8101
     * before allowing other, likely worse, ESSes to be considered.
8102
     */
8103
0
    freqs = get_bss_freqs_in_ess(wpa_s);
8104
0
    if (freqs) {
8105
0
      wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
8106
0
        "has been seen; try it next");
8107
0
      wpa_bssid_ignore_add(wpa_s, bssid);
8108
      /*
8109
       * On the next scan, go through only the known channels
8110
       * used in this ESS based on previous scans to speed up
8111
       * common load balancing use case.
8112
       */
8113
0
      os_free(wpa_s->next_scan_freqs);
8114
0
      wpa_s->next_scan_freqs = freqs;
8115
0
    }
8116
0
  }
8117
8118
0
  wpa_s->consecutive_conn_failures++;
8119
8120
0
  if (wpa_s->consecutive_conn_failures > 3 && wpa_s->current_ssid) {
8121
0
    wpa_printf(MSG_DEBUG, "Continuous association failures - "
8122
0
         "consider temporary network disabling");
8123
0
    wpas_auth_failed(wpa_s, "CONN_FAILED", bssid);
8124
0
  }
8125
  /*
8126
   * Multiple consecutive connection failures mean that other APs are
8127
   * either not available or have already been tried, so we can start
8128
   * increasing the delay here to avoid constant scanning.
8129
   */
8130
0
  switch (wpa_s->consecutive_conn_failures) {
8131
0
  case 1:
8132
0
    timeout = 100;
8133
0
    break;
8134
0
  case 2:
8135
0
    timeout = 500;
8136
0
    break;
8137
0
  case 3:
8138
0
    timeout = 1000;
8139
0
    break;
8140
0
  case 4:
8141
0
    timeout = 5000;
8142
0
    break;
8143
0
  default:
8144
0
    timeout = 10000;
8145
0
    break;
8146
0
  }
8147
8148
0
  wpa_dbg(wpa_s, MSG_DEBUG,
8149
0
    "Consecutive connection failures: %d --> request scan in %d ms",
8150
0
    wpa_s->consecutive_conn_failures, timeout);
8151
8152
  /*
8153
   * TODO: if more than one possible AP is available in scan results,
8154
   * could try the other ones before requesting a new scan.
8155
   */
8156
8157
  /* speed up the connection attempt with normal scan */
8158
0
  wpa_s->normal_scans = 0;
8159
0
  wpa_supplicant_req_scan(wpa_s, timeout / 1000,
8160
0
        1000 * (timeout % 1000));
8161
0
}
8162
8163
8164
#ifdef CONFIG_FILS
8165
8166
void fils_pmksa_cache_flush(struct wpa_supplicant *wpa_s)
8167
{
8168
  struct wpa_ssid *ssid = wpa_s->current_ssid;
8169
  const u8 *realm, *username, *rrk;
8170
  size_t realm_len, username_len, rrk_len;
8171
  u16 next_seq_num;
8172
8173
  /* Clear the PMKSA cache entry if FILS authentication was rejected.
8174
   * Check for ERP keys existing to limit when this can be done since
8175
   * the rejection response is not protected and such triggers should
8176
   * really not allow internal state to be modified unless required to
8177
   * avoid significant issues in functionality. In addition, drop
8178
   * externally configure PMKSA entries even without ERP keys since it
8179
   * is possible for an external component to add PMKSA entries for FILS
8180
   * authentication without restoring previously generated ERP keys.
8181
   *
8182
   * In this case, this is needed to allow recovery from cases where the
8183
   * AP or authentication server has dropped PMKSAs and ERP keys. */
8184
  if (!ssid || !ssid->eap.erp || !wpa_key_mgmt_fils(ssid->key_mgmt))
8185
    return;
8186
8187
  if (eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap,
8188
          &username, &username_len,
8189
          &realm, &realm_len, &next_seq_num,
8190
          &rrk, &rrk_len) != 0 ||
8191
      !realm) {
8192
    wpa_dbg(wpa_s, MSG_DEBUG,
8193
      "FILS: Drop external PMKSA cache entry");
8194
    wpa_sm_aborted_external_cached(wpa_s->wpa);
8195
    wpa_sm_external_pmksa_cache_flush(wpa_s->wpa, ssid);
8196
    return;
8197
  }
8198
8199
  wpa_dbg(wpa_s, MSG_DEBUG, "FILS: Drop PMKSA cache entry");
8200
  wpa_sm_aborted_cached(wpa_s->wpa);
8201
  wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
8202
}
8203
8204
8205
void fils_connection_failure(struct wpa_supplicant *wpa_s)
8206
{
8207
  struct wpa_ssid *ssid = wpa_s->current_ssid;
8208
  const u8 *realm, *username, *rrk;
8209
  size_t realm_len, username_len, rrk_len;
8210
  u16 next_seq_num;
8211
8212
  if (!ssid || !ssid->eap.erp || !wpa_key_mgmt_fils(ssid->key_mgmt) ||
8213
      eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap,
8214
          &username, &username_len,
8215
          &realm, &realm_len, &next_seq_num,
8216
          &rrk, &rrk_len) != 0 ||
8217
      !realm)
8218
    return;
8219
8220
  wpa_hexdump_ascii(MSG_DEBUG,
8221
        "FILS: Store last connection failure realm",
8222
        realm, realm_len);
8223
  os_free(wpa_s->last_con_fail_realm);
8224
  wpa_s->last_con_fail_realm = os_malloc(realm_len);
8225
  if (wpa_s->last_con_fail_realm) {
8226
    wpa_s->last_con_fail_realm_len = realm_len;
8227
    os_memcpy(wpa_s->last_con_fail_realm, realm, realm_len);
8228
  }
8229
}
8230
#endif /* CONFIG_FILS */
8231
8232
8233
int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
8234
0
{
8235
0
  return wpa_s->conf->ap_scan == 2 ||
8236
0
    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
8237
0
}
8238
8239
8240
#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
8241
int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
8242
                struct wpa_ssid *ssid,
8243
                const char *field,
8244
                const char *value)
8245
{
8246
#ifdef IEEE8021X_EAPOL
8247
  struct eap_peer_config *eap = &ssid->eap;
8248
8249
  wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
8250
  wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
8251
            (const u8 *) value, os_strlen(value));
8252
8253
  switch (wpa_supplicant_ctrl_req_from_string(field)) {
8254
  case WPA_CTRL_REQ_EAP_IDENTITY:
8255
    os_free(eap->identity);
8256
    eap->identity = (u8 *) os_strdup(value);
8257
    if (!eap->identity)
8258
      return -1;
8259
    eap->identity_len = os_strlen(value);
8260
    eap->pending_req_identity = 0;
8261
    if (ssid == wpa_s->current_ssid)
8262
      wpa_s->reassociate = 1;
8263
    break;
8264
  case WPA_CTRL_REQ_EAP_PASSWORD:
8265
    bin_clear_free(eap->password, eap->password_len);
8266
    eap->password = (u8 *) os_strdup(value);
8267
    if (!eap->password)
8268
      return -1;
8269
    eap->password_len = os_strlen(value);
8270
    eap->pending_req_password = 0;
8271
    if (ssid == wpa_s->current_ssid)
8272
      wpa_s->reassociate = 1;
8273
    break;
8274
  case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
8275
    bin_clear_free(eap->new_password, eap->new_password_len);
8276
    eap->new_password = (u8 *) os_strdup(value);
8277
    if (!eap->new_password)
8278
      return -1;
8279
    eap->new_password_len = os_strlen(value);
8280
    eap->pending_req_new_password = 0;
8281
    if (ssid == wpa_s->current_ssid)
8282
      wpa_s->reassociate = 1;
8283
    break;
8284
  case WPA_CTRL_REQ_EAP_PIN:
8285
    str_clear_free(eap->cert.pin);
8286
    eap->cert.pin = os_strdup(value);
8287
    if (!eap->cert.pin)
8288
      return -1;
8289
    eap->pending_req_pin = 0;
8290
    if (ssid == wpa_s->current_ssid)
8291
      wpa_s->reassociate = 1;
8292
    break;
8293
  case WPA_CTRL_REQ_EAP_OTP:
8294
    bin_clear_free(eap->otp, eap->otp_len);
8295
    eap->otp = (u8 *) os_strdup(value);
8296
    if (!eap->otp)
8297
      return -1;
8298
    eap->otp_len = os_strlen(value);
8299
    os_free(eap->pending_req_otp);
8300
    eap->pending_req_otp = NULL;
8301
    eap->pending_req_otp_len = 0;
8302
    break;
8303
  case WPA_CTRL_REQ_EAP_PASSPHRASE:
8304
    str_clear_free(eap->cert.private_key_passwd);
8305
    eap->cert.private_key_passwd = os_strdup(value);
8306
    if (!eap->cert.private_key_passwd)
8307
      return -1;
8308
    eap->pending_req_passphrase = 0;
8309
    if (ssid == wpa_s->current_ssid)
8310
      wpa_s->reassociate = 1;
8311
    break;
8312
  case WPA_CTRL_REQ_SIM:
8313
    str_clear_free(eap->external_sim_resp);
8314
    eap->external_sim_resp = os_strdup(value);
8315
    if (!eap->external_sim_resp)
8316
      return -1;
8317
    eap->pending_req_sim = 0;
8318
    break;
8319
  case WPA_CTRL_REQ_PSK_PASSPHRASE:
8320
    if (wpa_config_set(ssid, "psk", value, 0) < 0)
8321
      return -1;
8322
    ssid->mem_only_psk = 1;
8323
    if (ssid->passphrase)
8324
      wpa_config_update_psk(ssid);
8325
    if (wpa_s->wpa_state == WPA_SCANNING && !wpa_s->scanning)
8326
      wpa_supplicant_req_scan(wpa_s, 0, 0);
8327
    break;
8328
  case WPA_CTRL_REQ_EXT_CERT_CHECK:
8329
    if (eap->pending_ext_cert_check != PENDING_CHECK)
8330
      return -1;
8331
    if (os_strcmp(value, "good") == 0)
8332
      eap->pending_ext_cert_check = EXT_CERT_CHECK_GOOD;
8333
    else if (os_strcmp(value, "bad") == 0)
8334
      eap->pending_ext_cert_check = EXT_CERT_CHECK_BAD;
8335
    else
8336
      return -1;
8337
    break;
8338
  default:
8339
    wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
8340
    return -1;
8341
  }
8342
8343
  return 0;
8344
#else /* IEEE8021X_EAPOL */
8345
  wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
8346
  return -1;
8347
#endif /* IEEE8021X_EAPOL */
8348
}
8349
#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
8350
8351
8352
int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
8353
0
{
8354
#ifdef CONFIG_WEP
8355
  int i;
8356
  unsigned int drv_enc;
8357
#endif /* CONFIG_WEP */
8358
8359
0
  if (wpa_s->p2p_mgmt)
8360
0
    return 1; /* no normal network profiles on p2p_mgmt interface */
8361
8362
0
  if (ssid == NULL)
8363
0
    return 1;
8364
8365
0
  if (ssid->disabled)
8366
0
    return 1;
8367
8368
#ifdef CONFIG_WEP
8369
  if (wpa_s->drv_capa_known)
8370
    drv_enc = wpa_s->drv_enc;
8371
  else
8372
    drv_enc = (unsigned int) -1;
8373
8374
  for (i = 0; i < NUM_WEP_KEYS; i++) {
8375
    size_t len = ssid->wep_key_len[i];
8376
    if (len == 0)
8377
      continue;
8378
    if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
8379
      continue;
8380
    if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
8381
      continue;
8382
    if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
8383
      continue;
8384
    return 1; /* invalid WEP key */
8385
  }
8386
#endif /* CONFIG_WEP */
8387
8388
0
  if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
8389
0
      (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk &&
8390
0
      !(wpa_key_mgmt_sae(ssid->key_mgmt) && ssid->sae_password) &&
8391
0
      !ssid->mem_only_psk)
8392
0
    return 1;
8393
8394
0
#ifdef IEEE8021X_EAPOL
8395
#ifdef CRYPTO_RSA_OAEP_SHA256
8396
  if (ssid->eap.imsi_privacy_cert) {
8397
    struct crypto_rsa_key *key;
8398
    bool failed = false;
8399
8400
    key = crypto_rsa_key_read(ssid->eap.imsi_privacy_cert, false);
8401
    if (!key)
8402
      failed = true;
8403
    crypto_rsa_key_free(key);
8404
    if (failed) {
8405
      wpa_printf(MSG_DEBUG,
8406
           "Invalid imsi_privacy_cert (%s) - disable network",
8407
           ssid->eap.imsi_privacy_cert);
8408
      return 1;
8409
    }
8410
  }
8411
#endif /* CRYPTO_RSA_OAEP_SHA256 */
8412
0
#endif /* IEEE8021X_EAPOL */
8413
8414
0
  return 0;
8415
0
}
8416
8417
8418
int wpas_get_ssid_pmf(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
8419
0
{
8420
0
  if (ssid == NULL || ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
8421
0
    if (wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_OPTIONAL &&
8422
0
        !(wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)) {
8423
      /*
8424
       * Driver does not support BIP -- ignore pmf=1 default
8425
       * since the connection with PMF would fail and the
8426
       * configuration does not require PMF to be enabled.
8427
       */
8428
0
      return NO_MGMT_FRAME_PROTECTION;
8429
0
    }
8430
8431
0
    if (ssid &&
8432
0
        (ssid->key_mgmt &
8433
0
         ~(WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPS |
8434
0
           WPA_KEY_MGMT_IEEE8021X_NO_WPA)) == 0) {
8435
      /*
8436
       * Do not use the default PMF value for non-RSN networks
8437
       * since PMF is available only with RSN and pmf=2
8438
       * configuration would otherwise prevent connections to
8439
       * all open networks.
8440
       */
8441
0
      return NO_MGMT_FRAME_PROTECTION;
8442
0
    }
8443
8444
#ifdef CONFIG_OCV
8445
    /* Enable PMF if OCV is being enabled */
8446
    if (wpa_s->conf->pmf == NO_MGMT_FRAME_PROTECTION &&
8447
        ssid && ssid->ocv)
8448
      return MGMT_FRAME_PROTECTION_OPTIONAL;
8449
#endif /* CONFIG_OCV */
8450
8451
0
    return wpa_s->conf->pmf;
8452
0
  }
8453
8454
0
  return ssid->ieee80211w;
8455
0
}
8456
8457
8458
#ifdef CONFIG_SAE
8459
bool wpas_is_sae_avoided(struct wpa_supplicant *wpa_s,
8460
       struct wpa_ssid *ssid,
8461
       const struct wpa_ie_data *ie)
8462
{
8463
  return wpa_s->conf->sae_check_mfp &&
8464
    (!(ie->capabilities &
8465
       (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) ||
8466
     wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION);
8467
}
8468
#endif /* CONFIG_SAE */
8469
8470
8471
int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
8472
0
{
8473
0
  if (wpa_s->current_ssid == NULL ||
8474
0
      wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
8475
0
      os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
8476
0
    return 0;
8477
0
  return wpa_sm_pmf_enabled(wpa_s->wpa);
8478
0
}
8479
8480
8481
int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
8482
0
{
8483
0
  if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
8484
0
    return 1;
8485
0
  if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
8486
0
    return 0;
8487
0
  return -1;
8488
0
}
8489
8490
8491
void wpas_auth_failed(struct wpa_supplicant *wpa_s, const char *reason,
8492
          const u8 *bssid)
8493
0
{
8494
0
  struct wpa_ssid *ssid = wpa_s->current_ssid;
8495
0
  int dur;
8496
0
  struct os_reltime now;
8497
8498
0
  if (ssid == NULL) {
8499
0
    wpa_printf(MSG_DEBUG, "Authentication failure but no known "
8500
0
         "SSID block");
8501
0
    return;
8502
0
  }
8503
8504
0
  if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
8505
0
    return;
8506
8507
0
  ssid->auth_failures++;
8508
8509
#ifdef CONFIG_P2P
8510
  if (ssid->p2p_group &&
8511
      (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
8512
    /*
8513
     * Skip the wait time since there is a short timeout on the
8514
     * connection to a P2P group.
8515
     */
8516
    return;
8517
  }
8518
#endif /* CONFIG_P2P */
8519
8520
0
  if (ssid->auth_failures > 50)
8521
0
    dur = 300;
8522
0
  else if (ssid->auth_failures > 10)
8523
0
    dur = 120;
8524
0
  else if (ssid->auth_failures > 5)
8525
0
    dur = 90;
8526
0
  else if (ssid->auth_failures > 3)
8527
0
    dur = 60;
8528
0
  else if (ssid->auth_failures > 2)
8529
0
    dur = 30;
8530
0
  else if (ssid->auth_failures > 1)
8531
0
    dur = 20;
8532
0
  else
8533
0
    dur = 10;
8534
8535
0
  if (ssid->auth_failures > 1 &&
8536
0
      wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
8537
0
    dur += os_random() % (ssid->auth_failures * 10);
8538
8539
0
  os_get_reltime(&now);
8540
0
  if (now.sec + dur <= ssid->disabled_until.sec)
8541
0
    return;
8542
8543
0
  ssid->disabled_until.sec = now.sec + dur;
8544
8545
0
  wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
8546
0
    "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
8547
0
    ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
8548
0
    ssid->auth_failures, dur, reason);
8549
8550
0
  if (bssid)
8551
0
    os_memcpy(ssid->disabled_due_to, bssid, ETH_ALEN);
8552
0
}
8553
8554
8555
void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
8556
            struct wpa_ssid *ssid, int clear_failures)
8557
0
{
8558
0
  if (ssid == NULL)
8559
0
    return;
8560
8561
0
  if (ssid->disabled_until.sec) {
8562
0
    wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
8563
0
      "id=%d ssid=\"%s\"",
8564
0
      ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
8565
0
  }
8566
0
  ssid->disabled_until.sec = 0;
8567
0
  ssid->disabled_until.usec = 0;
8568
0
  if (clear_failures) {
8569
0
    ssid->auth_failures = 0;
8570
0
  } else if (!is_zero_ether_addr(ssid->disabled_due_to)) {
8571
0
    wpa_printf(MSG_DEBUG, "Mark BSSID " MACSTR
8572
0
         " ignored to allow a lower priority BSS, if any, to be tried next",
8573
0
         MAC2STR(ssid->disabled_due_to));
8574
0
    wpa_bssid_ignore_add(wpa_s, ssid->disabled_due_to);
8575
0
    os_memset(ssid->disabled_due_to, 0, ETH_ALEN);
8576
0
  }
8577
0
}
8578
8579
8580
int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
8581
0
{
8582
0
  size_t i;
8583
8584
0
  if (wpa_s->disallow_aps_bssid == NULL)
8585
0
    return 0;
8586
8587
0
  for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
8588
0
    if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
8589
0
            bssid, ETH_ALEN) == 0)
8590
0
      return 1;
8591
0
  }
8592
8593
0
  return 0;
8594
0
}
8595
8596
8597
int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
8598
        size_t ssid_len)
8599
0
{
8600
0
  size_t i;
8601
8602
0
  if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
8603
0
    return 0;
8604
8605
0
  for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
8606
0
    struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
8607
0
    if (ssid_len == s->ssid_len &&
8608
0
        os_memcmp(ssid, s->ssid, ssid_len) == 0)
8609
0
      return 1;
8610
0
  }
8611
8612
0
  return 0;
8613
0
}
8614
8615
8616
/**
8617
 * wpas_request_connection - Request a new connection
8618
 * @wpa_s: Pointer to the network interface
8619
 *
8620
 * This function is used to request a new connection to be found. It will mark
8621
 * the interface to allow reassociation and request a new scan to find a
8622
 * suitable network to connect to.
8623
 */
8624
void wpas_request_connection(struct wpa_supplicant *wpa_s)
8625
0
{
8626
0
  wpa_s->normal_scans = 0;
8627
0
  wpa_s->scan_req = NORMAL_SCAN_REQ;
8628
0
  wpa_supplicant_reinit_autoscan(wpa_s);
8629
0
  wpa_s->disconnected = 0;
8630
0
  wpa_s->reassociate = 1;
8631
0
  wpa_s->last_owe_group = 0;
8632
8633
0
  if (wpa_supplicant_fast_associate(wpa_s) != 1)
8634
0
    wpa_supplicant_req_scan(wpa_s, 0, 0);
8635
0
  else
8636
0
    wpa_s->reattach = 0;
8637
0
}
8638
8639
8640
/**
8641
 * wpas_request_disconnection - Request disconnection
8642
 * @wpa_s: Pointer to the network interface
8643
 *
8644
 * This function is used to request disconnection from the currently connected
8645
 * network. This will stop any ongoing scans and initiate deauthentication.
8646
 */
8647
void wpas_request_disconnection(struct wpa_supplicant *wpa_s)
8648
0
{
8649
#ifdef CONFIG_SME
8650
  wpa_s->sme.prev_bssid_set = 0;
8651
#endif /* CONFIG_SME */
8652
0
  wpa_s->reassociate = 0;
8653
0
  wpa_s->disconnected = 1;
8654
0
  wpa_supplicant_cancel_sched_scan(wpa_s);
8655
0
  wpa_supplicant_cancel_scan(wpa_s);
8656
0
  wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
8657
0
  eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
8658
0
  radio_remove_works(wpa_s, "connect", 0);
8659
0
  radio_remove_works(wpa_s, "sme-connect", 0);
8660
0
  wpa_s->roam_in_progress = false;
8661
0
#ifdef CONFIG_WNM
8662
0
  wpa_s->bss_trans_mgmt_in_progress = false;
8663
0
#endif /* CONFIG_WNM */
8664
0
}
8665
8666
8667
void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
8668
        struct wpa_used_freq_data *freqs_data,
8669
        unsigned int len)
8670
0
{
8671
0
  unsigned int i;
8672
8673
0
  wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
8674
0
    len, title);
8675
0
  for (i = 0; i < len; i++) {
8676
0
    struct wpa_used_freq_data *cur = &freqs_data[i];
8677
0
    wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
8678
0
      i, cur->freq, cur->flags);
8679
0
  }
8680
0
}
8681
8682
8683
/*
8684
 * Find the operating frequencies of any of the virtual interfaces that
8685
 * are using the same radio as the current interface, and in addition, get
8686
 * information about the interface types that are using the frequency.
8687
 */
8688
int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
8689
        struct wpa_used_freq_data *freqs_data,
8690
        unsigned int len, bool exclude_current)
8691
0
{
8692
0
  struct wpa_supplicant *ifs;
8693
0
  u8 bssid[ETH_ALEN];
8694
0
  int freq;
8695
0
  unsigned int idx = 0, i;
8696
8697
0
  wpa_dbg(wpa_s, MSG_DEBUG,
8698
0
    "Determining shared radio frequencies (max len %u)", len);
8699
0
  os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
8700
8701
0
  dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8702
0
       radio_list) {
8703
0
    if (idx == len)
8704
0
      break;
8705
8706
0
    if (exclude_current && ifs == wpa_s)
8707
0
      continue;
8708
8709
0
    if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
8710
0
      continue;
8711
8712
0
    if (ifs->current_ssid->mode == WPAS_MODE_AP ||
8713
0
        ifs->current_ssid->mode == WPAS_MODE_P2P_GO ||
8714
0
        ifs->current_ssid->mode == WPAS_MODE_MESH)
8715
0
      freq = ifs->current_ssid->frequency;
8716
0
    else if (wpa_drv_get_bssid(ifs, bssid) == 0)
8717
0
      freq = ifs->assoc_freq;
8718
0
    else
8719
0
      continue;
8720
8721
    /* Hold only distinct freqs */
8722
0
    for (i = 0; i < idx; i++)
8723
0
      if (freqs_data[i].freq == freq)
8724
0
        break;
8725
8726
0
    if (i == idx)
8727
0
      freqs_data[idx++].freq = freq;
8728
8729
0
    if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
8730
0
      freqs_data[i].flags |= ifs->current_ssid->p2p_group ?
8731
0
        WPA_FREQ_USED_BY_P2P_CLIENT :
8732
0
        WPA_FREQ_USED_BY_INFRA_STATION;
8733
0
    }
8734
0
  }
8735
8736
0
  dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
8737
0
  return idx;
8738
0
}
8739
8740
8741
/*
8742
 * Find the operating frequencies of any of the virtual interfaces that
8743
 * are using the same radio as the current interface.
8744
 */
8745
int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
8746
         int *freq_array, unsigned int len,
8747
         bool exclude_current)
8748
0
{
8749
0
  struct wpa_used_freq_data *freqs_data;
8750
0
  int num, i;
8751
8752
0
  os_memset(freq_array, 0, sizeof(int) * len);
8753
8754
0
  freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
8755
0
  if (!freqs_data)
8756
0
    return -1;
8757
8758
0
  num = get_shared_radio_freqs_data(wpa_s, freqs_data, len,
8759
0
            exclude_current);
8760
0
  for (i = 0; i < num; i++)
8761
0
    freq_array[i] = freqs_data[i].freq;
8762
8763
0
  os_free(freqs_data);
8764
8765
0
  return num;
8766
0
}
8767
8768
8769
struct wpa_supplicant *
8770
wpas_vendor_elem(struct wpa_supplicant *wpa_s, enum wpa_vendor_elem_frame frame)
8771
0
{
8772
0
  switch (frame) {
8773
#ifdef CONFIG_P2P
8774
  case VENDOR_ELEM_PROBE_REQ_P2P:
8775
  case VENDOR_ELEM_PROBE_RESP_P2P:
8776
  case VENDOR_ELEM_PROBE_RESP_P2P_GO:
8777
  case VENDOR_ELEM_BEACON_P2P_GO:
8778
  case VENDOR_ELEM_P2P_PD_REQ:
8779
  case VENDOR_ELEM_P2P_PD_RESP:
8780
  case VENDOR_ELEM_P2P_GO_NEG_REQ:
8781
  case VENDOR_ELEM_P2P_GO_NEG_RESP:
8782
  case VENDOR_ELEM_P2P_GO_NEG_CONF:
8783
  case VENDOR_ELEM_P2P_INV_REQ:
8784
  case VENDOR_ELEM_P2P_INV_RESP:
8785
  case VENDOR_ELEM_P2P_ASSOC_REQ:
8786
  case VENDOR_ELEM_P2P_ASSOC_RESP:
8787
    return wpa_s->p2pdev;
8788
#endif /* CONFIG_P2P */
8789
0
  default:
8790
0
    return wpa_s;
8791
0
  }
8792
0
}
8793
8794
8795
void wpas_vendor_elem_update(struct wpa_supplicant *wpa_s)
8796
0
{
8797
0
  unsigned int i;
8798
0
  char buf[30];
8799
8800
0
  wpa_printf(MSG_DEBUG, "Update vendor elements");
8801
8802
0
  for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
8803
0
    if (wpa_s->vendor_elem[i]) {
8804
0
      int res;
8805
8806
0
      res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
8807
0
      if (!os_snprintf_error(sizeof(buf), res)) {
8808
0
        wpa_hexdump_buf(MSG_DEBUG, buf,
8809
0
            wpa_s->vendor_elem[i]);
8810
0
      }
8811
0
    }
8812
0
  }
8813
8814
#ifdef CONFIG_P2P
8815
  if (wpa_s->parent == wpa_s &&
8816
      wpa_s->global->p2p &&
8817
      !wpa_s->global->p2p_disabled)
8818
    p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
8819
#endif /* CONFIG_P2P */
8820
0
}
8821
8822
8823
int wpas_vendor_elem_remove(struct wpa_supplicant *wpa_s, int frame,
8824
          const u8 *elem, size_t len)
8825
0
{
8826
0
  u8 *ie, *end;
8827
8828
0
  ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
8829
0
  end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
8830
8831
0
  for (; ie + 1 < end; ie += 2 + ie[1]) {
8832
0
    if (ie + len > end)
8833
0
      break;
8834
0
    if (os_memcmp(ie, elem, len) != 0)
8835
0
      continue;
8836
8837
0
    if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
8838
0
      wpabuf_free(wpa_s->vendor_elem[frame]);
8839
0
      wpa_s->vendor_elem[frame] = NULL;
8840
0
    } else {
8841
0
      os_memmove(ie, ie + len, end - (ie + len));
8842
0
      wpa_s->vendor_elem[frame]->used -= len;
8843
0
    }
8844
0
    wpas_vendor_elem_update(wpa_s);
8845
0
    return 0;
8846
0
  }
8847
8848
0
  return -1;
8849
0
}
8850
8851
8852
struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
8853
           u16 num_modes, enum hostapd_hw_mode mode,
8854
           bool is_6ghz)
8855
0
{
8856
0
  u16 i;
8857
8858
0
  if (!modes)
8859
0
    return NULL;
8860
8861
0
  for (i = 0; i < num_modes; i++) {
8862
0
    if (modes[i].mode != mode ||
8863
0
        !modes[i].num_channels || !modes[i].channels)
8864
0
      continue;
8865
0
    if ((!is_6ghz && !is_6ghz_freq(modes[i].channels[0].freq)) ||
8866
0
        (is_6ghz && is_6ghz_freq(modes[i].channels[0].freq)))
8867
0
      return &modes[i];
8868
0
  }
8869
8870
0
  return NULL;
8871
0
}
8872
8873
8874
struct hostapd_hw_modes * get_mode_with_freq(struct hostapd_hw_modes *modes,
8875
               u16 num_modes, int freq)
8876
0
{
8877
0
  int i, j;
8878
8879
0
  for (i = 0; i < num_modes; i++) {
8880
0
    for (j = 0; j < modes[i].num_channels; j++) {
8881
0
      if (freq == modes[i].channels[j].freq)
8882
0
        return &modes[i];
8883
0
    }
8884
0
  }
8885
8886
0
  return NULL;
8887
0
}
8888
8889
8890
static struct
8891
wpa_bss_tmp_disallowed * wpas_get_disallowed_bss(struct wpa_supplicant *wpa_s,
8892
             const u8 *bssid)
8893
0
{
8894
0
  struct wpa_bss_tmp_disallowed *bss;
8895
8896
0
  dl_list_for_each(bss, &wpa_s->bss_tmp_disallowed,
8897
0
       struct wpa_bss_tmp_disallowed, list) {
8898
0
    if (os_memcmp(bssid, bss->bssid, ETH_ALEN) == 0)
8899
0
      return bss;
8900
0
  }
8901
8902
0
  return NULL;
8903
0
}
8904
8905
8906
static int wpa_set_driver_tmp_disallow_list(struct wpa_supplicant *wpa_s)
8907
0
{
8908
0
  struct wpa_bss_tmp_disallowed *tmp;
8909
0
  unsigned int num_bssid = 0;
8910
0
  u8 *bssids;
8911
0
  int ret;
8912
8913
0
  bssids = os_malloc(dl_list_len(&wpa_s->bss_tmp_disallowed) * ETH_ALEN);
8914
0
  if (!bssids)
8915
0
    return -1;
8916
0
  dl_list_for_each(tmp, &wpa_s->bss_tmp_disallowed,
8917
0
       struct wpa_bss_tmp_disallowed, list) {
8918
0
    os_memcpy(&bssids[num_bssid * ETH_ALEN], tmp->bssid,
8919
0
        ETH_ALEN);
8920
0
    num_bssid++;
8921
0
  }
8922
0
  ret = wpa_drv_set_bssid_tmp_disallow(wpa_s, num_bssid, bssids);
8923
0
  os_free(bssids);
8924
0
  return ret;
8925
0
}
8926
8927
8928
static void wpa_bss_tmp_disallow_timeout(void *eloop_ctx, void *timeout_ctx)
8929
0
{
8930
0
  struct wpa_supplicant *wpa_s = eloop_ctx;
8931
0
  struct wpa_bss_tmp_disallowed *tmp, *bss = timeout_ctx;
8932
8933
  /* Make sure the bss is not already freed */
8934
0
  dl_list_for_each(tmp, &wpa_s->bss_tmp_disallowed,
8935
0
       struct wpa_bss_tmp_disallowed, list) {
8936
0
    if (bss == tmp) {
8937
0
      remove_bss_tmp_disallowed_entry(wpa_s, tmp);
8938
0
      wpa_set_driver_tmp_disallow_list(wpa_s);
8939
0
      break;
8940
0
    }
8941
0
  }
8942
0
}
8943
8944
8945
void wpa_bss_tmp_disallow(struct wpa_supplicant *wpa_s, const u8 *bssid,
8946
        unsigned int sec, int rssi_threshold)
8947
0
{
8948
0
  struct wpa_bss_tmp_disallowed *bss;
8949
8950
0
  bss = wpas_get_disallowed_bss(wpa_s, bssid);
8951
0
  if (bss) {
8952
0
    eloop_cancel_timeout(wpa_bss_tmp_disallow_timeout, wpa_s, bss);
8953
0
    goto finish;
8954
0
  }
8955
8956
0
  bss = os_malloc(sizeof(*bss));
8957
0
  if (!bss) {
8958
0
    wpa_printf(MSG_DEBUG,
8959
0
         "Failed to allocate memory for temp disallow BSS");
8960
0
    return;
8961
0
  }
8962
8963
0
  os_memcpy(bss->bssid, bssid, ETH_ALEN);
8964
0
  dl_list_add(&wpa_s->bss_tmp_disallowed, &bss->list);
8965
0
  wpa_set_driver_tmp_disallow_list(wpa_s);
8966
8967
0
finish:
8968
0
  bss->rssi_threshold = rssi_threshold;
8969
0
  eloop_register_timeout(sec, 0, wpa_bss_tmp_disallow_timeout,
8970
0
             wpa_s, bss);
8971
0
}
8972
8973
8974
int wpa_is_bss_tmp_disallowed(struct wpa_supplicant *wpa_s,
8975
            struct wpa_bss *bss)
8976
0
{
8977
0
  struct wpa_bss_tmp_disallowed *disallowed = NULL, *tmp, *prev;
8978
8979
0
  dl_list_for_each_safe(tmp, prev, &wpa_s->bss_tmp_disallowed,
8980
0
       struct wpa_bss_tmp_disallowed, list) {
8981
0
    if (os_memcmp(bss->bssid, tmp->bssid, ETH_ALEN) == 0) {
8982
0
      disallowed = tmp;
8983
0
      break;
8984
0
    }
8985
0
  }
8986
0
  if (!disallowed)
8987
0
    return 0;
8988
8989
0
  if (disallowed->rssi_threshold != 0 &&
8990
0
      bss->level > disallowed->rssi_threshold) {
8991
0
    remove_bss_tmp_disallowed_entry(wpa_s, disallowed);
8992
0
    wpa_set_driver_tmp_disallow_list(wpa_s);
8993
0
    return 0;
8994
0
  }
8995
8996
0
  return 1;
8997
0
}
8998
8999
9000
int wpas_enable_mac_addr_randomization(struct wpa_supplicant *wpa_s,
9001
               unsigned int type, const u8 *addr,
9002
               const u8 *mask)
9003
0
{
9004
0
  if ((addr && !mask) || (!addr && mask)) {
9005
0
    wpa_printf(MSG_INFO,
9006
0
         "MAC_ADDR_RAND_SCAN invalid addr/mask combination");
9007
0
    return -1;
9008
0
  }
9009
9010
0
  if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
9011
0
    wpa_printf(MSG_INFO,
9012
0
         "MAC_ADDR_RAND_SCAN cannot allow multicast address");
9013
0
    return -1;
9014
0
  }
9015
9016
0
  if (type & MAC_ADDR_RAND_SCAN) {
9017
0
    if (wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
9018
0
            addr, mask))
9019
0
      return -1;
9020
0
  }
9021
9022
0
  if (type & MAC_ADDR_RAND_SCHED_SCAN) {
9023
0
    if (wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
9024
0
            addr, mask))
9025
0
      return -1;
9026
9027
0
    if (wpa_s->sched_scanning && !wpa_s->pno)
9028
0
      wpas_scan_restart_sched_scan(wpa_s);
9029
0
  }
9030
9031
0
  if (type & MAC_ADDR_RAND_PNO) {
9032
0
    if (wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
9033
0
            addr, mask))
9034
0
      return -1;
9035
9036
0
    if (wpa_s->pno) {
9037
0
      wpas_stop_pno(wpa_s);
9038
0
      wpas_start_pno(wpa_s);
9039
0
    }
9040
0
  }
9041
9042
0
  return 0;
9043
0
}
9044
9045
9046
int wpas_disable_mac_addr_randomization(struct wpa_supplicant *wpa_s,
9047
          unsigned int type)
9048
0
{
9049
0
  wpas_mac_addr_rand_scan_clear(wpa_s, type);
9050
0
  if (wpa_s->pno) {
9051
0
    if (type & MAC_ADDR_RAND_PNO) {
9052
0
      wpas_stop_pno(wpa_s);
9053
0
      wpas_start_pno(wpa_s);
9054
0
    }
9055
0
  } else if (wpa_s->sched_scanning && (type & MAC_ADDR_RAND_SCHED_SCAN)) {
9056
0
    wpas_scan_restart_sched_scan(wpa_s);
9057
0
  }
9058
9059
0
  return 0;
9060
0
}
9061
9062
9063
int wpa_drv_signal_poll(struct wpa_supplicant *wpa_s,
9064
      struct wpa_signal_info *si)
9065
0
{
9066
0
  int res;
9067
9068
0
  if (!wpa_s->driver->signal_poll)
9069
0
    return -1;
9070
9071
0
  res = wpa_s->driver->signal_poll(wpa_s->drv_priv, si);
9072
9073
#ifdef CONFIG_TESTING_OPTIONS
9074
  if (res == 0) {
9075
    struct driver_signal_override *dso;
9076
9077
    dl_list_for_each(dso, &wpa_s->drv_signal_override,
9078
         struct driver_signal_override, list) {
9079
      if (os_memcmp(wpa_s->bssid, dso->bssid,
9080
              ETH_ALEN) != 0)
9081
        continue;
9082
      wpa_printf(MSG_DEBUG,
9083
           "Override driver signal_poll information: current_signal: %d->%d avg_signal: %d->%d avg_beacon_signal: %d->%d current_noise: %d->%d",
9084
           si->data.signal,
9085
           dso->si_current_signal,
9086
           si->data.avg_signal,
9087
           dso->si_avg_signal,
9088
           si->data.avg_beacon_signal,
9089
           dso->si_avg_beacon_signal,
9090
           si->current_noise,
9091
           dso->si_current_noise);
9092
      si->data.signal = dso->si_current_signal;
9093
      si->data.avg_signal = dso->si_avg_signal;
9094
      si->data.avg_beacon_signal = dso->si_avg_beacon_signal;
9095
      si->current_noise = dso->si_current_noise;
9096
      break;
9097
    }
9098
  }
9099
#endif /* CONFIG_TESTING_OPTIONS */
9100
9101
0
  return res;
9102
0
}
9103
9104
9105
struct wpa_scan_results *
9106
wpa_drv_get_scan_results2(struct wpa_supplicant *wpa_s)
9107
887
{
9108
887
  struct wpa_scan_results *scan_res;
9109
#ifdef CONFIG_TESTING_OPTIONS
9110
  size_t idx;
9111
#endif /* CONFIG_TESTING_OPTIONS */
9112
9113
887
  if (!wpa_s->driver->get_scan_results2)
9114
887
    return NULL;
9115
9116
0
  scan_res = wpa_s->driver->get_scan_results2(wpa_s->drv_priv);
9117
9118
#ifdef CONFIG_TESTING_OPTIONS
9119
  for (idx = 0; scan_res && idx < scan_res->num; idx++) {
9120
    struct driver_signal_override *dso;
9121
    struct wpa_scan_res *res = scan_res->res[idx];
9122
9123
    dl_list_for_each(dso, &wpa_s->drv_signal_override,
9124
         struct driver_signal_override, list) {
9125
      if (os_memcmp(res->bssid, dso->bssid, ETH_ALEN) != 0)
9126
        continue;
9127
      wpa_printf(MSG_DEBUG,
9128
           "Override driver scan signal level %d->%d for "
9129
           MACSTR,
9130
           res->level, dso->scan_level,
9131
           MAC2STR(res->bssid));
9132
      res->flags |= WPA_SCAN_QUAL_INVALID;
9133
      if (dso->scan_level < 0)
9134
        res->flags |= WPA_SCAN_LEVEL_DBM;
9135
      else
9136
        res->flags &= ~WPA_SCAN_LEVEL_DBM;
9137
      res->level = dso->scan_level;
9138
      break;
9139
    }
9140
  }
9141
#endif /* CONFIG_TESTING_OPTIONS */
9142
9143
0
  return scan_res;
9144
887
}
9145
9146
9147
static bool wpas_ap_link_address(struct wpa_supplicant *wpa_s, const u8 *addr)
9148
0
{
9149
0
  int i;
9150
9151
0
  if (!wpa_s->valid_links)
9152
0
    return false;
9153
9154
0
  for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
9155
0
    if (!(wpa_s->valid_links & BIT(i)))
9156
0
      continue;
9157
9158
0
    if (os_memcmp(wpa_s->links[i].bssid, addr, ETH_ALEN) == 0)
9159
0
      return true;
9160
0
  }
9161
9162
0
  return false;
9163
0
}
9164
9165
9166
int wpa_drv_send_action(struct wpa_supplicant *wpa_s, unsigned int freq,
9167
      unsigned int wait, const u8 *dst, const u8 *src,
9168
      const u8 *bssid, const u8 *data, size_t data_len,
9169
      int no_cck)
9170
51
{
9171
51
  if (!wpa_s->driver->send_action)
9172
51
    return -1;
9173
9174
0
  if (data_len > 0 && data[0] != WLAN_ACTION_PUBLIC) {
9175
0
    if (wpas_ap_link_address(wpa_s, dst))
9176
0
      dst = wpa_s->ap_mld_addr;
9177
9178
0
    if (wpas_ap_link_address(wpa_s, bssid))
9179
0
      bssid = wpa_s->ap_mld_addr;
9180
0
  }
9181
9182
0
  return wpa_s->driver->send_action(wpa_s->drv_priv, freq, wait, dst, src,
9183
0
            bssid, data, data_len, no_cck);
9184
51
}