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