3 * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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.
16 #include "crypto/random.h"
17 #include "crypto/sha1.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "eap_peer/eap.h"
20 #include "eap_peer/eap_proxy.h"
21 #include "eap_server/eap_methods.h"
22 #include "rsn_supp/wpa.h"
25 #include "utils/ext_password.h"
26 #include "l2_packet/l2_packet.h"
27 #include "wpa_supplicant_i.h"
29 #include "ctrl_iface.h"
30 #include "pcsc_funcs.h"
31 #include "common/version.h"
32 #include "rsn_supp/preauth.h"
33 #include "rsn_supp/pmksa_cache.h"
34 #include "common/wpa_ctrl.h"
35 #include "common/ieee802_11_defs.h"
37 #include "blacklist.h"
38 #include "wpas_glue.h"
39 #include "wps_supplicant.h"
42 #include "gas_query.h"
44 #include "p2p_supplicant.h"
45 #include "wifi_display.h"
51 #include "offchannel.h"
52 #include "hs20_supplicant.h"
57 const char *wpa_supplicant_version =
58 "wpa_supplicant v" VERSION_STR "\n"
59 "Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi> and contributors";
61 const char *wpa_supplicant_license =
62 "This software may be distributed under the terms of the BSD license.\n"
63 "See README for more details.\n"
64 #ifdef EAP_TLS_OPENSSL
65 "\nThis product includes software developed by the OpenSSL Project\n"
66 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
67 #endif /* EAP_TLS_OPENSSL */
70 #ifndef CONFIG_NO_STDOUT_DEBUG
71 /* Long text divided into parts in order to fit in C89 strings size limits. */
72 const char *wpa_supplicant_full_license1 =
74 const char *wpa_supplicant_full_license2 =
75 "This software may be distributed under the terms of the BSD license.\n"
77 "Redistribution and use in source and binary forms, with or without\n"
78 "modification, are permitted provided that the following conditions are\n"
81 const char *wpa_supplicant_full_license3 =
82 "1. Redistributions of source code must retain the above copyright\n"
83 " notice, this list of conditions and the following disclaimer.\n"
85 "2. Redistributions in binary form must reproduce the above copyright\n"
86 " notice, this list of conditions and the following disclaimer in the\n"
87 " documentation and/or other materials provided with the distribution.\n"
89 const char *wpa_supplicant_full_license4 =
90 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
91 " names of its contributors may be used to endorse or promote products\n"
92 " derived from this software without specific prior written permission.\n"
94 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
95 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
96 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
97 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
98 const char *wpa_supplicant_full_license5 =
99 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
100 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
101 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
102 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
103 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
104 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
105 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
107 #endif /* CONFIG_NO_STDOUT_DEBUG */
109 /* Configure default/group WEP keys for static WEP */
110 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
114 for (i = 0; i < NUM_WEP_KEYS; i++) {
115 if (ssid->wep_key_len[i] == 0)
119 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
120 i, i == ssid->wep_tx_keyidx, NULL, 0,
121 ssid->wep_key[i], ssid->wep_key_len[i]);
128 int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
129 struct wpa_ssid *ssid)
137 /* IBSS/WPA-None uses only one key (Group) for both receiving and
138 * sending unicast and multicast packets. */
140 if (ssid->mode != WPAS_MODE_IBSS) {
141 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
142 "IBSS/ad-hoc) for WPA-None", ssid->mode);
146 if (!ssid->psk_set) {
147 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
152 switch (wpa_s->group_cipher) {
153 case WPA_CIPHER_CCMP:
154 os_memcpy(key, ssid->psk, 16);
158 case WPA_CIPHER_GCMP:
159 os_memcpy(key, ssid->psk, 16);
163 case WPA_CIPHER_TKIP:
164 /* WPA-None uses the same Michael MIC key for both TX and RX */
165 os_memcpy(key, ssid->psk, 16 + 8);
166 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
171 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
172 "WPA-None", wpa_s->group_cipher);
176 /* TODO: should actually remember the previously used seq#, both for TX
177 * and RX from each STA.. */
179 ret = wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
180 os_memset(key, 0, sizeof(key));
185 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
187 struct wpa_supplicant *wpa_s = eloop_ctx;
188 const u8 *bssid = wpa_s->bssid;
189 if (is_zero_ether_addr(bssid))
190 bssid = wpa_s->pending_bssid;
191 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
193 wpa_blacklist_add(wpa_s, bssid);
194 wpa_sm_notify_disassoc(wpa_s->wpa);
195 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
196 wpa_s->reassociate = 1;
199 * If we timed out, the AP or the local radio may be busy.
200 * So, wait a second until scanning again.
202 wpa_supplicant_req_scan(wpa_s, 1, 0);
207 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
208 * @wpa_s: Pointer to wpa_supplicant data
209 * @sec: Number of seconds after which to time out authentication
210 * @usec: Number of microseconds after which to time out authentication
212 * This function is used to schedule a timeout for the current authentication
215 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
218 if (wpa_s->conf->ap_scan == 0 &&
219 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
222 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
223 "%d usec", sec, usec);
224 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
225 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
230 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
231 * @wpa_s: Pointer to wpa_supplicant data
233 * This function is used to cancel authentication timeout scheduled with
234 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
237 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
239 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
240 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
241 wpa_blacklist_del(wpa_s, wpa_s->bssid);
246 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
247 * @wpa_s: Pointer to wpa_supplicant data
249 * This function is used to configure EAPOL state machine based on the selected
250 * authentication mode.
252 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
254 #ifdef IEEE8021X_EAPOL
255 struct eapol_config eapol_conf;
256 struct wpa_ssid *ssid = wpa_s->current_ssid;
258 #ifdef CONFIG_IBSS_RSN
259 if (ssid->mode == WPAS_MODE_IBSS &&
260 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
261 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
263 * RSN IBSS authentication is per-STA and we can disable the
264 * per-BSSID EAPOL authentication.
266 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
267 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
268 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
271 #endif /* CONFIG_IBSS_RSN */
273 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
274 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
276 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
277 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
278 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
280 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
282 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
283 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
284 eapol_conf.accept_802_1x_keys = 1;
285 eapol_conf.required_keys = 0;
286 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
287 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
289 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
290 eapol_conf.required_keys |=
291 EAPOL_REQUIRE_KEY_BROADCAST;
294 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
295 eapol_conf.required_keys = 0;
297 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
298 eapol_conf.workaround = ssid->eap_workaround;
299 eapol_conf.eap_disabled =
300 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
301 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
302 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
303 eapol_conf.external_sim = wpa_s->conf->external_sim;
306 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
307 eapol_conf.wps |= EAPOL_LOCAL_WPS_IN_USE;
308 if (wpa_s->current_bss) {
310 ie = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
315 EAPOL_PEER_IS_WPS20_AP;
320 #endif /* CONFIG_WPS */
322 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
324 ieee802_1x_alloc_kay_sm(wpa_s, ssid);
325 #endif /* IEEE8021X_EAPOL */
330 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
331 * @wpa_s: Pointer to wpa_supplicant data
332 * @ssid: Configuration data for the network
334 * This function is used to configure WPA state machine and related parameters
335 * to a mode where WPA is not enabled. This is called as part of the
336 * authentication configuration when the selected network does not use WPA.
338 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
339 struct wpa_ssid *ssid)
343 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
344 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
345 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
346 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
348 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
349 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
350 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
351 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
352 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
353 wpa_s->group_cipher = WPA_CIPHER_NONE;
354 wpa_s->mgmt_group_cipher = 0;
356 for (i = 0; i < NUM_WEP_KEYS; i++) {
357 if (ssid->wep_key_len[i] > 5) {
358 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
359 wpa_s->group_cipher = WPA_CIPHER_WEP104;
361 } else if (ssid->wep_key_len[i] > 0) {
362 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
363 wpa_s->group_cipher = WPA_CIPHER_WEP40;
368 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
369 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
370 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
371 wpa_s->pairwise_cipher);
372 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
373 #ifdef CONFIG_IEEE80211W
374 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
375 wpa_s->mgmt_group_cipher);
376 #endif /* CONFIG_IEEE80211W */
378 pmksa_cache_clear_current(wpa_s->wpa);
382 void free_hw_features(struct wpa_supplicant *wpa_s)
385 if (wpa_s->hw.modes == NULL)
388 for (i = 0; i < wpa_s->hw.num_modes; i++) {
389 os_free(wpa_s->hw.modes[i].channels);
390 os_free(wpa_s->hw.modes[i].rates);
393 os_free(wpa_s->hw.modes);
394 wpa_s->hw.modes = NULL;
398 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
402 bgscan_deinit(wpa_s);
403 autoscan_deinit(wpa_s);
404 scard_deinit(wpa_s->scard);
406 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
407 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
408 l2_packet_deinit(wpa_s->l2);
411 l2_packet_deinit(wpa_s->l2_br);
414 #ifdef CONFIG_TESTING_OPTIONS
415 l2_packet_deinit(wpa_s->l2_test);
416 wpa_s->l2_test = NULL;
417 #endif /* CONFIG_TESTING_OPTIONS */
419 if (wpa_s->conf != NULL) {
420 struct wpa_ssid *ssid;
421 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
422 wpas_notify_network_removed(wpa_s, ssid);
425 os_free(wpa_s->confname);
426 wpa_s->confname = NULL;
428 os_free(wpa_s->confanother);
429 wpa_s->confanother = NULL;
431 wpa_sm_set_eapol(wpa_s->wpa, NULL);
432 eapol_sm_deinit(wpa_s->eapol);
435 rsn_preauth_deinit(wpa_s->wpa);
438 wpa_tdls_deinit(wpa_s->wpa);
439 #endif /* CONFIG_TDLS */
441 pmksa_candidate_free(wpa_s->wpa);
442 wpa_sm_deinit(wpa_s->wpa);
444 wpa_blacklist_clear(wpa_s);
446 wpa_bss_deinit(wpa_s);
448 wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
449 wpa_supplicant_cancel_scan(wpa_s);
450 wpa_supplicant_cancel_auth_timeout(wpa_s);
451 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
452 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
453 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
455 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
457 wpas_wps_deinit(wpa_s);
459 wpabuf_free(wpa_s->pending_eapol_rx);
460 wpa_s->pending_eapol_rx = NULL;
462 #ifdef CONFIG_IBSS_RSN
463 ibss_rsn_deinit(wpa_s->ibss_rsn);
464 wpa_s->ibss_rsn = NULL;
465 #endif /* CONFIG_IBSS_RSN */
470 wpa_supplicant_ap_deinit(wpa_s);
471 #endif /* CONFIG_AP */
473 wpas_p2p_deinit(wpa_s);
475 #ifdef CONFIG_OFFCHANNEL
476 offchannel_deinit(wpa_s);
477 #endif /* CONFIG_OFFCHANNEL */
479 wpa_supplicant_cancel_sched_scan(wpa_s);
481 os_free(wpa_s->next_scan_freqs);
482 wpa_s->next_scan_freqs = NULL;
484 os_free(wpa_s->manual_scan_freqs);
485 wpa_s->manual_scan_freqs = NULL;
487 os_free(wpa_s->manual_sched_scan_freqs);
488 wpa_s->manual_sched_scan_freqs = NULL;
490 gas_query_deinit(wpa_s->gas);
493 free_hw_features(wpa_s);
495 ieee802_1x_dealloc_kay_sm(wpa_s);
497 os_free(wpa_s->bssid_filter);
498 wpa_s->bssid_filter = NULL;
500 os_free(wpa_s->disallow_aps_bssid);
501 wpa_s->disallow_aps_bssid = NULL;
502 os_free(wpa_s->disallow_aps_ssid);
503 wpa_s->disallow_aps_ssid = NULL;
505 wnm_bss_keep_alive_deinit(wpa_s);
507 wnm_deallocate_memory(wpa_s);
508 #endif /* CONFIG_WNM */
510 ext_password_deinit(wpa_s->ext_pw);
511 wpa_s->ext_pw = NULL;
513 wpabuf_free(wpa_s->last_gas_resp);
514 wpa_s->last_gas_resp = NULL;
515 wpabuf_free(wpa_s->prev_gas_resp);
516 wpa_s->prev_gas_resp = NULL;
518 os_free(wpa_s->last_scan_res);
519 wpa_s->last_scan_res = NULL;
523 #endif /* CONFIG_HS20 */
525 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
526 wpabuf_free(wpa_s->vendor_elem[i]);
527 wpa_s->vendor_elem[i] = NULL;
530 wmm_ac_notify_disassoc(wpa_s);
535 * wpa_clear_keys - Clear keys configured for the driver
536 * @wpa_s: Pointer to wpa_supplicant data
537 * @addr: Previously used BSSID or %NULL if not available
539 * This function clears the encryption keys that has been previously configured
542 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
546 #ifdef CONFIG_IEEE80211W
548 #else /* CONFIG_IEEE80211W */
550 #endif /* CONFIG_IEEE80211W */
552 /* MLME-DELETEKEYS.request */
553 for (i = 0; i < max; i++) {
554 if (wpa_s->keys_cleared & BIT(i))
556 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
559 if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
560 !is_zero_ether_addr(addr)) {
561 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
563 /* MLME-SETPROTECTION.request(None) */
564 wpa_drv_mlme_setprotection(
566 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
567 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
569 wpa_s->keys_cleared = (u32) -1;
574 * wpa_supplicant_state_txt - Get the connection state name as a text string
575 * @state: State (wpa_state; WPA_*)
576 * Returns: The state name as a printable text string
578 const char * wpa_supplicant_state_txt(enum wpa_states state)
581 case WPA_DISCONNECTED:
582 return "DISCONNECTED";
585 case WPA_INTERFACE_DISABLED:
586 return "INTERFACE_DISABLED";
589 case WPA_AUTHENTICATING:
590 return "AUTHENTICATING";
591 case WPA_ASSOCIATING:
592 return "ASSOCIATING";
595 case WPA_4WAY_HANDSHAKE:
596 return "4WAY_HANDSHAKE";
597 case WPA_GROUP_HANDSHAKE:
598 return "GROUP_HANDSHAKE";
609 static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
613 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
614 name = wpa_s->current_ssid->bgscan;
616 name = wpa_s->conf->bgscan;
617 if (name == NULL || name[0] == '\0')
619 if (wpas_driver_bss_selection(wpa_s))
621 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
624 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
626 #endif /* CONFIG_P2P */
628 bgscan_deinit(wpa_s);
629 if (wpa_s->current_ssid) {
630 if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
631 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
634 * Live without bgscan; it is only used as a roaming
635 * optimization, so the initial connection is not
639 struct wpa_scan_results *scan_res;
640 wpa_s->bgscan_ssid = wpa_s->current_ssid;
641 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
644 bgscan_notify_scan(wpa_s, scan_res);
645 wpa_scan_results_free(scan_res);
649 wpa_s->bgscan_ssid = NULL;
653 static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
655 if (wpa_s->bgscan_ssid != NULL) {
656 bgscan_deinit(wpa_s);
657 wpa_s->bgscan_ssid = NULL;
661 #endif /* CONFIG_BGSCAN */
664 static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
666 if (autoscan_init(wpa_s, 0))
667 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
671 static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
673 autoscan_deinit(wpa_s);
677 void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
679 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
680 wpa_s->wpa_state == WPA_SCANNING) {
681 autoscan_deinit(wpa_s);
682 wpa_supplicant_start_autoscan(wpa_s);
688 * wpa_supplicant_set_state - Set current connection state
689 * @wpa_s: Pointer to wpa_supplicant data
690 * @state: The new connection state
692 * This function is called whenever the connection state changes, e.g.,
693 * association is completed for WPA/WPA2 4-Way Handshake is started.
695 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
696 enum wpa_states state)
698 enum wpa_states old_state = wpa_s->wpa_state;
700 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
701 wpa_supplicant_state_txt(wpa_s->wpa_state),
702 wpa_supplicant_state_txt(state));
704 if (state == WPA_INTERFACE_DISABLED) {
705 /* Assure normal scan when interface is restored */
706 wpa_s->normal_scans = 0;
709 if (state == WPA_COMPLETED) {
710 wpas_connect_work_done(wpa_s);
711 /* Reinitialize normal_scan counter */
712 wpa_s->normal_scans = 0;
715 if (state != WPA_SCANNING)
716 wpa_supplicant_notify_scanning(wpa_s, 0);
718 if (state == WPA_COMPLETED && wpa_s->new_connection) {
719 struct wpa_ssid *ssid = wpa_s->current_ssid;
720 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
721 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
722 MACSTR " completed [id=%d id_str=%s]",
723 MAC2STR(wpa_s->bssid),
724 ssid ? ssid->id : -1,
725 ssid && ssid->id_str ? ssid->id_str : "");
726 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
727 wpas_clear_temp_disabled(wpa_s, ssid, 1);
728 wpa_s->extra_blacklist_count = 0;
729 wpa_s->new_connection = 0;
730 wpa_drv_set_operstate(wpa_s, 1);
731 #ifndef IEEE8021X_EAPOL
732 wpa_drv_set_supp_port(wpa_s, 1);
733 #endif /* IEEE8021X_EAPOL */
734 wpa_s->after_wps = 0;
735 wpa_s->known_wps_freq = 0;
736 wpas_p2p_completed(wpa_s);
738 sme_sched_obss_scan(wpa_s, 1);
739 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
740 state == WPA_ASSOCIATED) {
741 wpa_s->new_connection = 1;
742 wpa_drv_set_operstate(wpa_s, 0);
743 #ifndef IEEE8021X_EAPOL
744 wpa_drv_set_supp_port(wpa_s, 0);
745 #endif /* IEEE8021X_EAPOL */
746 sme_sched_obss_scan(wpa_s, 0);
748 wpa_s->wpa_state = state;
751 if (state == WPA_COMPLETED)
752 wpa_supplicant_start_bgscan(wpa_s);
753 else if (state < WPA_ASSOCIATED)
754 wpa_supplicant_stop_bgscan(wpa_s);
755 #endif /* CONFIG_BGSCAN */
757 if (state == WPA_AUTHENTICATING)
758 wpa_supplicant_stop_autoscan(wpa_s);
760 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
761 wpa_supplicant_start_autoscan(wpa_s);
763 if (old_state >= WPA_ASSOCIATED && wpa_s->wpa_state < WPA_ASSOCIATED)
764 wmm_ac_notify_disassoc(wpa_s);
766 if (wpa_s->wpa_state != old_state) {
767 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
770 * Notify the P2P Device interface about a state change in one
773 wpas_p2p_indicate_state_change(wpa_s);
775 if (wpa_s->wpa_state == WPA_COMPLETED ||
776 old_state == WPA_COMPLETED)
777 wpas_notify_auth_changed(wpa_s);
782 void wpa_supplicant_terminate_proc(struct wpa_global *global)
786 struct wpa_supplicant *wpa_s = global->ifaces;
788 struct wpa_supplicant *next = wpa_s->next;
789 if (wpas_wps_terminate_pending(wpa_s) == 1)
792 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
793 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
794 wpas_p2p_disconnect(wpa_s);
795 #endif /* CONFIG_P2P */
798 #endif /* CONFIG_WPS */
805 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
807 struct wpa_global *global = signal_ctx;
808 wpa_supplicant_terminate_proc(global);
812 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
814 enum wpa_states old_state = wpa_s->wpa_state;
816 wpa_s->pairwise_cipher = 0;
817 wpa_s->group_cipher = 0;
818 wpa_s->mgmt_group_cipher = 0;
820 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
821 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
823 if (wpa_s->wpa_state != old_state)
824 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
829 * wpa_supplicant_reload_configuration - Reload configuration data
830 * @wpa_s: Pointer to wpa_supplicant data
831 * Returns: 0 on success or -1 if configuration parsing failed
833 * This function can be used to request that the configuration data is reloaded
834 * (e.g., after configuration file change). This function is reloading
835 * configuration only for one interface, so this may need to be called multiple
836 * times if %wpa_supplicant is controlling multiple interfaces and all
837 * interfaces need reconfiguration.
839 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
841 struct wpa_config *conf;
845 if (wpa_s->confname == NULL)
847 conf = wpa_config_read(wpa_s->confname, NULL);
849 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
850 "file '%s' - exiting", wpa_s->confname);
853 wpa_config_read(wpa_s->confanother, conf);
855 conf->changed_parameters = (unsigned int) -1;
857 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
858 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
859 os_strcmp(conf->ctrl_interface,
860 wpa_s->conf->ctrl_interface) != 0);
862 if (reconf_ctrl && wpa_s->ctrl_iface) {
863 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
864 wpa_s->ctrl_iface = NULL;
867 eapol_sm_invalidate_cached_session(wpa_s->eapol);
868 if (wpa_s->current_ssid) {
869 wpa_supplicant_deauthenticate(wpa_s,
870 WLAN_REASON_DEAUTH_LEAVING);
874 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
875 * pkcs11_engine_path, pkcs11_module_path, openssl_ciphers.
877 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
879 * Clear forced success to clear EAP state for next
882 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
884 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
885 wpa_sm_set_config(wpa_s->wpa, NULL);
886 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
887 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
888 rsn_preauth_deinit(wpa_s->wpa);
890 old_ap_scan = wpa_s->conf->ap_scan;
891 wpa_config_free(wpa_s->conf);
893 if (old_ap_scan != wpa_s->conf->ap_scan)
894 wpas_notify_ap_scan_changed(wpa_s);
897 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
899 wpa_supplicant_update_config(wpa_s);
901 wpa_supplicant_clear_status(wpa_s);
902 if (wpa_supplicant_enabled_networks(wpa_s)) {
903 wpa_s->reassociate = 1;
904 wpa_supplicant_req_scan(wpa_s, 0, 0);
906 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
911 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
913 struct wpa_global *global = signal_ctx;
914 struct wpa_supplicant *wpa_s;
915 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
916 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
918 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
919 wpa_supplicant_terminate_proc(global);
925 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
926 struct wpa_ssid *ssid,
927 struct wpa_ie_data *ie)
929 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
932 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
933 "from association info");
938 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
940 if (!(ie->group_cipher & ssid->group_cipher)) {
941 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
942 "cipher 0x%x (mask 0x%x) - reject",
943 ie->group_cipher, ssid->group_cipher);
946 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
947 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
948 "cipher 0x%x (mask 0x%x) - reject",
949 ie->pairwise_cipher, ssid->pairwise_cipher);
952 if (!(ie->key_mgmt & ssid->key_mgmt)) {
953 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
954 "management 0x%x (mask 0x%x) - reject",
955 ie->key_mgmt, ssid->key_mgmt);
959 #ifdef CONFIG_IEEE80211W
960 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
961 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
962 wpa_s->conf->pmf : ssid->ieee80211w) ==
963 MGMT_FRAME_PROTECTION_REQUIRED) {
964 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
965 "that does not support management frame protection - "
969 #endif /* CONFIG_IEEE80211W */
976 * wpa_supplicant_set_suites - Set authentication and encryption parameters
977 * @wpa_s: Pointer to wpa_supplicant data
978 * @bss: Scan results for the selected BSS, or %NULL if not available
979 * @ssid: Configuration data for the selected network
980 * @wpa_ie: Buffer for the WPA/RSN IE
981 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
982 * used buffer length in case the functions returns success.
983 * Returns: 0 on success or -1 on failure
985 * This function is used to configure authentication and encryption parameters
986 * based on the network configuration and scan result for the selected BSS (if
989 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
990 struct wpa_bss *bss, struct wpa_ssid *ssid,
991 u8 *wpa_ie, size_t *wpa_ie_len)
993 struct wpa_ie_data ie;
995 const u8 *bss_wpa, *bss_rsn, *bss_osen;
998 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
999 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1000 bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
1002 bss_wpa = bss_rsn = bss_osen = NULL;
1004 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
1005 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
1006 (ie.group_cipher & ssid->group_cipher) &&
1007 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1008 (ie.key_mgmt & ssid->key_mgmt)) {
1009 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
1010 proto = WPA_PROTO_RSN;
1011 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
1012 wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie) == 0 &&
1013 (ie.group_cipher & ssid->group_cipher) &&
1014 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1015 (ie.key_mgmt & ssid->key_mgmt)) {
1016 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
1017 proto = WPA_PROTO_WPA;
1019 } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
1020 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
1021 /* TODO: parse OSEN element */
1022 os_memset(&ie, 0, sizeof(ie));
1023 ie.group_cipher = WPA_CIPHER_CCMP;
1024 ie.pairwise_cipher = WPA_CIPHER_CCMP;
1025 ie.key_mgmt = WPA_KEY_MGMT_OSEN;
1026 proto = WPA_PROTO_OSEN;
1027 #endif /* CONFIG_HS20 */
1029 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
1030 wpa_dbg(wpa_s, MSG_DEBUG,
1031 "WPA: ssid proto=0x%x pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1032 ssid->proto, ssid->pairwise_cipher, ssid->group_cipher,
1034 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: BSS " MACSTR " ssid='%s'%s%s%s",
1035 MAC2STR(bss->bssid),
1036 wpa_ssid_txt(bss->ssid, bss->ssid_len),
1037 bss_wpa ? " WPA" : "",
1038 bss_rsn ? " RSN" : "",
1039 bss_osen ? " OSEN" : "");
1041 wpa_hexdump(MSG_DEBUG, "RSN", bss_rsn, 2 + bss_rsn[1]);
1042 if (wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie)) {
1043 wpa_dbg(wpa_s, MSG_DEBUG,
1044 "Could not parse RSN element");
1046 wpa_dbg(wpa_s, MSG_DEBUG,
1047 "RSN: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1048 ie.pairwise_cipher, ie.group_cipher,
1053 wpa_hexdump(MSG_DEBUG, "WPA", bss_wpa, 2 + bss_wpa[1]);
1054 if (wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie)) {
1055 wpa_dbg(wpa_s, MSG_DEBUG,
1056 "Could not parse WPA element");
1058 wpa_dbg(wpa_s, MSG_DEBUG,
1059 "WPA: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1060 ie.pairwise_cipher, ie.group_cipher,
1066 if (ssid->proto & WPA_PROTO_OSEN)
1067 proto = WPA_PROTO_OSEN;
1068 else if (ssid->proto & WPA_PROTO_RSN)
1069 proto = WPA_PROTO_RSN;
1071 proto = WPA_PROTO_WPA;
1072 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1073 os_memset(&ie, 0, sizeof(ie));
1074 ie.group_cipher = ssid->group_cipher;
1075 ie.pairwise_cipher = ssid->pairwise_cipher;
1076 ie.key_mgmt = ssid->key_mgmt;
1077 #ifdef CONFIG_IEEE80211W
1078 ie.mgmt_group_cipher =
1079 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
1080 WPA_CIPHER_AES_128_CMAC : 0;
1081 #endif /* CONFIG_IEEE80211W */
1082 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1083 "based on configuration");
1088 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1089 "pairwise %d key_mgmt %d proto %d",
1090 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1091 #ifdef CONFIG_IEEE80211W
1092 if (ssid->ieee80211w) {
1093 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1094 ie.mgmt_group_cipher);
1096 #endif /* CONFIG_IEEE80211W */
1098 wpa_s->wpa_proto = proto;
1099 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1100 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
1101 !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
1103 if (bss || !wpa_s->ap_ies_from_associnfo) {
1104 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1105 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1106 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1107 bss_rsn ? 2 + bss_rsn[1] : 0))
1111 sel = ie.group_cipher & ssid->group_cipher;
1112 wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1113 if (wpa_s->group_cipher < 0) {
1114 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1118 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1119 wpa_cipher_txt(wpa_s->group_cipher));
1121 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
1122 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1123 if (wpa_s->pairwise_cipher < 0) {
1124 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1128 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1129 wpa_cipher_txt(wpa_s->pairwise_cipher));
1131 sel = ie.key_mgmt & ssid->key_mgmt;
1133 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
1134 sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
1135 #endif /* CONFIG_SAE */
1137 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1138 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
1139 wpa_dbg(wpa_s, MSG_DEBUG,
1140 "WPA: using KEY_MGMT 802.1X with Suite B");
1141 #ifdef CONFIG_IEEE80211R
1142 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1143 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1144 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
1145 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1146 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1147 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1148 #endif /* CONFIG_IEEE80211R */
1150 } else if (sel & WPA_KEY_MGMT_SAE) {
1151 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1152 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1153 } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1154 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1155 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1156 #endif /* CONFIG_SAE */
1157 #ifdef CONFIG_IEEE80211W
1158 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1159 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1160 wpa_dbg(wpa_s, MSG_DEBUG,
1161 "WPA: using KEY_MGMT 802.1X with SHA256");
1162 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1163 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1164 wpa_dbg(wpa_s, MSG_DEBUG,
1165 "WPA: using KEY_MGMT PSK with SHA256");
1166 #endif /* CONFIG_IEEE80211W */
1167 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1168 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1169 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1170 } else if (sel & WPA_KEY_MGMT_PSK) {
1171 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1172 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1173 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1174 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1175 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1177 } else if (sel & WPA_KEY_MGMT_OSEN) {
1178 wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1179 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1180 #endif /* CONFIG_HS20 */
1182 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1183 "authenticated key management type");
1187 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1188 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1189 wpa_s->pairwise_cipher);
1190 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1192 #ifdef CONFIG_IEEE80211W
1193 sel = ie.mgmt_group_cipher;
1194 if ((ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1195 wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION ||
1196 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1198 if (sel & WPA_CIPHER_AES_128_CMAC) {
1199 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1200 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1202 } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1203 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1204 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1206 } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1207 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1208 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1210 } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1211 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1212 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1215 wpa_s->mgmt_group_cipher = 0;
1216 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1218 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1219 wpa_s->mgmt_group_cipher);
1220 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1221 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1222 wpa_s->conf->pmf : ssid->ieee80211w));
1223 #endif /* CONFIG_IEEE80211W */
1225 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1226 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1230 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
1231 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN, NULL);
1232 #ifndef CONFIG_NO_PBKDF2
1233 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1236 pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1237 4096, psk, PMK_LEN);
1238 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1240 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL);
1241 os_memset(psk, 0, sizeof(psk));
1243 #endif /* CONFIG_NO_PBKDF2 */
1244 #ifdef CONFIG_EXT_PASSWORD
1245 if (ssid->ext_psk) {
1246 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1248 char pw_str[64 + 1];
1252 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
1253 "found from external storage");
1257 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1258 wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
1259 "PSK length %d in external storage",
1260 (int) wpabuf_len(pw));
1261 ext_password_free(pw);
1265 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1266 pw_str[wpabuf_len(pw)] = '\0';
1268 #ifndef CONFIG_NO_PBKDF2
1269 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1271 pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1272 4096, psk, PMK_LEN);
1273 os_memset(pw_str, 0, sizeof(pw_str));
1274 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
1275 "external passphrase)",
1277 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL);
1278 os_memset(psk, 0, sizeof(psk));
1280 #endif /* CONFIG_NO_PBKDF2 */
1281 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1282 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1283 wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
1284 "Invalid PSK hex string");
1285 os_memset(pw_str, 0, sizeof(pw_str));
1286 ext_password_free(pw);
1289 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL);
1290 os_memset(psk, 0, sizeof(psk));
1292 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
1294 os_memset(pw_str, 0, sizeof(pw_str));
1295 ext_password_free(pw);
1299 os_memset(pw_str, 0, sizeof(pw_str));
1300 ext_password_free(pw);
1302 #endif /* CONFIG_EXT_PASSWORD */
1304 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1310 static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
1315 case 0: /* Bits 0-7 */
1317 case 1: /* Bits 8-15 */
1319 case 2: /* Bits 16-23 */
1321 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
1322 *pos |= 0x08; /* Bit 19 - BSS Transition */
1323 #endif /* CONFIG_WNM */
1325 case 3: /* Bits 24-31 */
1327 *pos |= 0x02; /* Bit 25 - SSID List */
1328 #endif /* CONFIG_WNM */
1329 #ifdef CONFIG_INTERWORKING
1330 if (wpa_s->conf->interworking)
1331 *pos |= 0x80; /* Bit 31 - Interworking */
1332 #endif /* CONFIG_INTERWORKING */
1334 case 4: /* Bits 32-39 */
1335 #ifdef CONFIG_INTERWORKING
1336 if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
1337 *pos |= 0x01; /* Bit 32 - QoS Map */
1338 #endif /* CONFIG_INTERWORKING */
1340 case 5: /* Bits 40-47 */
1342 if (wpa_s->conf->hs20)
1343 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1344 #endif /* CONFIG_HS20 */
1346 case 6: /* Bits 48-55 */
1352 int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf, size_t buflen)
1357 if (len < wpa_s->extended_capa_len)
1358 len = wpa_s->extended_capa_len;
1359 if (buflen < (size_t) len + 2) {
1360 wpa_printf(MSG_INFO,
1361 "Not enough room for building extended capabilities element");
1365 *pos++ = WLAN_EID_EXT_CAPAB;
1367 for (i = 0; i < len; i++, pos++) {
1368 wpas_ext_capab_byte(wpa_s, pos, i);
1370 if (i < wpa_s->extended_capa_len) {
1371 *pos &= ~wpa_s->extended_capa_mask[i];
1372 *pos |= wpa_s->extended_capa[i];
1376 while (len > 0 && buf[1 + len] == 0) {
1387 static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
1388 struct wpa_bss *test_bss)
1390 struct wpa_bss *bss;
1392 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1393 if (bss == test_bss)
1401 static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
1402 struct wpa_ssid *test_ssid)
1404 struct wpa_ssid *ssid;
1406 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1407 if (ssid == test_ssid)
1415 int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
1416 struct wpa_ssid *test_ssid)
1418 if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
1421 return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
1425 void wpas_connect_work_free(struct wpa_connect_work *cwork)
1433 void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
1435 struct wpa_connect_work *cwork;
1436 struct wpa_radio_work *work = wpa_s->connect_work;
1441 wpa_s->connect_work = NULL;
1444 wpas_connect_work_free(cwork);
1445 radio_work_done(work);
1449 int wpas_update_random_addr(struct wpa_supplicant *wpa_s, int style)
1451 struct os_reltime now;
1454 os_get_reltime(&now);
1455 if (wpa_s->last_mac_addr_style == style &&
1456 wpa_s->last_mac_addr_change.sec != 0 &&
1457 !os_reltime_expired(&now, &wpa_s->last_mac_addr_change,
1458 wpa_s->conf->rand_addr_lifetime)) {
1459 wpa_msg(wpa_s, MSG_DEBUG,
1460 "Previously selected random MAC address has not yet expired");
1466 if (random_mac_addr(addr) < 0)
1470 os_memcpy(addr, wpa_s->perm_addr, ETH_ALEN);
1471 if (random_mac_addr_keep_oui(addr) < 0)
1478 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
1479 wpa_msg(wpa_s, MSG_INFO,
1480 "Failed to set random MAC address");
1484 os_get_reltime(&wpa_s->last_mac_addr_change);
1485 wpa_s->mac_addr_changed = 1;
1486 wpa_s->last_mac_addr_style = style;
1488 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
1489 wpa_msg(wpa_s, MSG_INFO,
1490 "Could not update MAC address information");
1494 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
1501 int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
1503 if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
1504 !wpa_s->conf->preassoc_mac_addr)
1507 return wpas_update_random_addr(wpa_s, wpa_s->conf->preassoc_mac_addr);
1511 static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
1514 * wpa_supplicant_associate - Request association
1515 * @wpa_s: Pointer to wpa_supplicant data
1516 * @bss: Scan results for the selected BSS, or %NULL if not available
1517 * @ssid: Configuration data for the selected network
1519 * This function is used to request %wpa_supplicant to associate with a BSS.
1521 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1522 struct wpa_bss *bss, struct wpa_ssid *ssid)
1524 struct wpa_connect_work *cwork;
1527 if (ssid->mac_addr == -1)
1528 rand_style = wpa_s->conf->mac_addr;
1530 rand_style = ssid->mac_addr;
1532 if (wpa_s->last_ssid == ssid) {
1533 wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
1534 } else if (rand_style > 0) {
1535 if (wpas_update_random_addr(wpa_s, rand_style) < 0)
1537 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
1538 } else if (wpa_s->mac_addr_changed) {
1539 if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
1540 wpa_msg(wpa_s, MSG_INFO,
1541 "Could not restore permanent MAC address");
1544 wpa_s->mac_addr_changed = 0;
1545 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
1546 wpa_msg(wpa_s, MSG_INFO,
1547 "Could not update MAC address information");
1550 wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
1552 wpa_s->last_ssid = ssid;
1554 #ifdef CONFIG_IBSS_RSN
1555 ibss_rsn_deinit(wpa_s->ibss_rsn);
1556 wpa_s->ibss_rsn = NULL;
1557 #endif /* CONFIG_IBSS_RSN */
1559 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1560 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1562 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1563 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1567 if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
1568 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1569 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1570 wpas_p2p_ap_setup_failed(wpa_s);
1573 wpa_s->current_bss = bss;
1574 #else /* CONFIG_AP */
1575 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1577 #endif /* CONFIG_AP */
1581 if (ssid->mode == WPAS_MODE_MESH) {
1583 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MESH)) {
1584 wpa_msg(wpa_s, MSG_INFO,
1585 "Driver does not support mesh mode");
1589 ssid->frequency = bss->freq;
1590 if (wpa_supplicant_join_mesh(wpa_s, ssid) < 0) {
1591 wpa_msg(wpa_s, MSG_ERROR, "Could not join mesh");
1594 wpa_s->current_bss = bss;
1595 wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_GROUP_STARTED
1596 "ssid=\"%s\" id=%d",
1597 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1599 #else /* CONFIG_MESH */
1600 wpa_msg(wpa_s, MSG_ERROR,
1601 "mesh mode support not included in the build");
1602 #endif /* CONFIG_MESH */
1608 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1610 #endif /* CONFIG_TDLS */
1612 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1613 ssid->mode == IEEE80211_MODE_INFRA) {
1614 sme_authenticate(wpa_s, bss, ssid);
1618 if (wpa_s->connect_work) {
1619 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
1623 if (radio_work_pending(wpa_s, "connect")) {
1624 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
1628 cwork = os_zalloc(sizeof(*cwork));
1635 if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
1636 wpas_start_assoc_cb, cwork) < 0) {
1642 static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
1644 struct wpa_connect_work *cwork = work->ctx;
1645 struct wpa_bss *bss = cwork->bss;
1646 struct wpa_ssid *ssid = cwork->ssid;
1647 struct wpa_supplicant *wpa_s = work->wpa_s;
1650 int use_crypt, ret, i, bssid_changed;
1651 int algs = WPA_AUTH_ALG_OPEN;
1652 unsigned int cipher_pairwise, cipher_group;
1653 struct wpa_driver_associate_params params;
1654 int wep_keys_set = 0;
1655 int assoc_failed = 0;
1656 struct wpa_ssid *old_ssid;
1657 #ifdef CONFIG_HT_OVERRIDES
1658 struct ieee80211_ht_capabilities htcaps;
1659 struct ieee80211_ht_capabilities htcaps_mask;
1660 #endif /* CONFIG_HT_OVERRIDES */
1661 #ifdef CONFIG_VHT_OVERRIDES
1662 struct ieee80211_vht_capabilities vhtcaps;
1663 struct ieee80211_vht_capabilities vhtcaps_mask;
1664 #endif /* CONFIG_VHT_OVERRIDES */
1667 if (work->started) {
1668 wpa_s->connect_work = NULL;
1670 /* cancel possible auth. timeout */
1671 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
1674 wpas_connect_work_free(cwork);
1678 wpa_s->connect_work = work;
1680 if (cwork->bss_removed || !wpas_valid_bss_ssid(wpa_s, bss, ssid)) {
1681 wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
1682 wpas_connect_work_done(wpa_s);
1686 os_memset(¶ms, 0, sizeof(params));
1687 wpa_s->reassociate = 0;
1688 wpa_s->eap_expected_failure = 0;
1690 (!wpas_driver_bss_selection(wpa_s) || wpas_wps_searching(wpa_s))) {
1691 #ifdef CONFIG_IEEE80211R
1692 const u8 *ie, *md = NULL;
1693 #endif /* CONFIG_IEEE80211R */
1694 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1695 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1696 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1697 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1698 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1699 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1701 wpas_notify_bssid_changed(wpa_s);
1702 #ifdef CONFIG_IEEE80211R
1703 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1704 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1706 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1708 /* Prepare for the next transition */
1709 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1711 #endif /* CONFIG_IEEE80211R */
1713 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1714 wpa_s->conf->ap_scan == 2 &&
1715 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1716 /* Use ap_scan==1 style network selection to find the network
1718 wpas_connect_work_done(wpa_s);
1719 wpa_s->scan_req = MANUAL_SCAN_REQ;
1720 wpa_s->reassociate = 1;
1721 wpa_supplicant_req_scan(wpa_s, 0, 0);
1723 #endif /* CONFIG_WPS */
1725 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1726 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1727 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1729 wpa_supplicant_cancel_sched_scan(wpa_s);
1730 wpa_supplicant_cancel_scan(wpa_s);
1732 /* Starting new association, so clear the possibly used WPA IE from the
1733 * previous association. */
1734 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1736 #ifdef IEEE8021X_EAPOL
1737 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1739 if (ssid->non_leap == 0)
1740 algs = WPA_AUTH_ALG_LEAP;
1742 algs |= WPA_AUTH_ALG_LEAP;
1745 #endif /* IEEE8021X_EAPOL */
1746 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1747 if (ssid->auth_alg) {
1748 algs = ssid->auth_alg;
1749 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1753 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1754 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1755 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
1756 int try_opportunistic;
1757 try_opportunistic = (ssid->proactive_key_caching < 0 ?
1759 ssid->proactive_key_caching) &&
1760 (ssid->proto & WPA_PROTO_RSN);
1761 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1762 ssid, try_opportunistic) == 0)
1763 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1764 wpa_ie_len = sizeof(wpa_ie);
1765 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1766 wpa_ie, &wpa_ie_len)) {
1767 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1768 "key management and encryption suites");
1769 wpas_connect_work_done(wpa_s);
1772 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
1773 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
1775 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
1776 * use non-WPA since the scan results did not indicate that the
1777 * AP is using WPA or WPA2.
1779 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1781 wpa_s->wpa_proto = 0;
1782 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
1783 wpa_ie_len = sizeof(wpa_ie);
1784 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1785 wpa_ie, &wpa_ie_len)) {
1786 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1787 "key management and encryption suites (no "
1789 wpas_connect_work_done(wpa_s);
1793 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1794 struct wpabuf *wps_ie;
1795 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1796 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1797 wpa_ie_len = wpabuf_len(wps_ie);
1798 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1801 wpabuf_free(wps_ie);
1802 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1803 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1804 params.wps = WPS_MODE_PRIVACY;
1806 params.wps = WPS_MODE_OPEN;
1807 wpa_s->wpa_proto = 0;
1808 #endif /* CONFIG_WPS */
1810 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1812 wpa_s->wpa_proto = 0;
1816 if (wpa_s->global->p2p) {
1820 pos = wpa_ie + wpa_ie_len;
1821 len = sizeof(wpa_ie) - wpa_ie_len;
1822 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1828 wpa_s->cross_connect_disallowed = 0;
1831 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1833 wpa_s->cross_connect_disallowed =
1834 p2p_get_cross_connect_disallowed(p2p);
1836 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1838 wpa_s->cross_connect_disallowed ?
1839 "disallows" : "allows");
1843 os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
1844 #endif /* CONFIG_P2P */
1847 if (is_hs20_network(wpa_s, ssid, bss)) {
1848 struct wpabuf *hs20;
1849 hs20 = wpabuf_alloc(20);
1851 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
1854 wpas_hs20_add_indication(hs20, pps_mo_id);
1855 len = sizeof(wpa_ie) - wpa_ie_len;
1856 if (wpabuf_len(hs20) <= len) {
1857 os_memcpy(wpa_ie + wpa_ie_len,
1858 wpabuf_head(hs20), wpabuf_len(hs20));
1859 wpa_ie_len += wpabuf_len(hs20);
1864 #endif /* CONFIG_HS20 */
1867 * Workaround: Add Extended Capabilities element only if the AP
1868 * included this element in Beacon/Probe Response frames. Some older
1869 * APs seem to have interoperability issues if this element is
1870 * included, so while the standard may require us to include the
1871 * element in all cases, it is justifiable to skip it to avoid
1872 * interoperability issues.
1874 if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
1877 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
1879 if (ext_capab_len > 0) {
1881 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1883 os_memmove(pos + ext_capab_len, pos,
1884 wpa_ie_len - (pos - wpa_ie));
1885 wpa_ie_len += ext_capab_len;
1886 os_memcpy(pos, ext_capab, ext_capab_len);
1890 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1892 cipher_pairwise = wpa_s->pairwise_cipher;
1893 cipher_group = wpa_s->group_cipher;
1894 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1895 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1896 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1898 if (wpa_set_wep_keys(wpa_s, ssid)) {
1903 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1906 #ifdef IEEE8021X_EAPOL
1907 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1908 if ((ssid->eapol_flags &
1909 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1910 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1914 /* Assume that dynamic WEP-104 keys will be used and
1915 * set cipher suites in order for drivers to expect
1917 cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
1920 #endif /* IEEE8021X_EAPOL */
1922 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1923 /* Set the key before (and later after) association */
1924 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1927 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1929 params.ssid = bss->ssid;
1930 params.ssid_len = bss->ssid_len;
1931 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
1932 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
1933 MACSTR " freq=%u MHz based on scan results "
1935 MAC2STR(bss->bssid), bss->freq,
1937 params.bssid = bss->bssid;
1938 params.freq.freq = bss->freq;
1940 params.bssid_hint = bss->bssid;
1941 params.freq_hint = bss->freq;
1943 params.ssid = ssid->ssid;
1944 params.ssid_len = ssid->ssid_len;
1947 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1948 wpa_s->conf->ap_scan == 2) {
1949 params.bssid = ssid->bssid;
1950 params.fixed_bssid = 1;
1953 /* Initial frequency for IBSS/mesh */
1954 if ((ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) &&
1955 ssid->frequency > 0 && params.freq.freq == 0) {
1956 enum hostapd_hw_mode hw_mode;
1959 params.freq.freq = ssid->frequency;
1961 hw_mode = ieee80211_freq_to_chan(ssid->frequency, &channel);
1962 for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
1963 if (wpa_s->hw.modes[i].mode == hw_mode) {
1964 struct hostapd_hw_modes *mode;
1966 mode = &wpa_s->hw.modes[i];
1967 params.freq.ht_enabled = ht_supported(mode);
1973 if (ssid->mode == WPAS_MODE_IBSS) {
1974 if (ssid->beacon_int)
1975 params.beacon_int = ssid->beacon_int;
1977 params.beacon_int = wpa_s->conf->beacon_int;
1980 params.wpa_ie = wpa_ie;
1981 params.wpa_ie_len = wpa_ie_len;
1982 params.pairwise_suite = cipher_pairwise;
1983 params.group_suite = cipher_group;
1984 params.key_mgmt_suite = wpa_s->key_mgmt;
1985 params.wpa_proto = wpa_s->wpa_proto;
1986 params.auth_alg = algs;
1987 params.mode = ssid->mode;
1988 params.bg_scan_period = ssid->bg_scan_period;
1989 for (i = 0; i < NUM_WEP_KEYS; i++) {
1990 if (ssid->wep_key_len[i])
1991 params.wep_key[i] = ssid->wep_key[i];
1992 params.wep_key_len[i] = ssid->wep_key_len[i];
1994 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1996 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1997 (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
1998 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
1999 params.passphrase = ssid->passphrase;
2001 params.psk = ssid->psk;
2004 if (wpa_s->conf->key_mgmt_offload) {
2005 if (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
2006 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
2007 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B)
2008 params.req_key_mgmt_offload =
2009 ssid->proactive_key_caching < 0 ?
2010 wpa_s->conf->okc : ssid->proactive_key_caching;
2012 params.req_key_mgmt_offload = 1;
2014 if ((params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
2015 params.key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
2016 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK) &&
2018 params.psk = ssid->psk;
2021 params.drop_unencrypted = use_crypt;
2023 #ifdef CONFIG_IEEE80211W
2024 params.mgmt_frame_protection =
2025 ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
2026 wpa_s->conf->pmf : ssid->ieee80211w;
2027 if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
2028 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2029 struct wpa_ie_data ie;
2030 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
2032 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
2033 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
2034 "MFP: require MFP");
2035 params.mgmt_frame_protection =
2036 MGMT_FRAME_PROTECTION_REQUIRED;
2039 #endif /* CONFIG_IEEE80211W */
2041 params.p2p = ssid->p2p_group;
2043 if (wpa_s->parent->set_sta_uapsd)
2044 params.uapsd = wpa_s->parent->sta_uapsd;
2048 #ifdef CONFIG_HT_OVERRIDES
2049 os_memset(&htcaps, 0, sizeof(htcaps));
2050 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
2051 params.htcaps = (u8 *) &htcaps;
2052 params.htcaps_mask = (u8 *) &htcaps_mask;
2053 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, ¶ms);
2054 #endif /* CONFIG_HT_OVERRIDES */
2055 #ifdef CONFIG_VHT_OVERRIDES
2056 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
2057 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
2058 params.vhtcaps = &vhtcaps;
2059 params.vhtcaps_mask = &vhtcaps_mask;
2060 wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, ¶ms);
2061 #endif /* CONFIG_VHT_OVERRIDES */
2065 * If multi-channel concurrency is not supported, check for any
2066 * frequency conflict. In case of any frequency conflict, remove the
2067 * least prioritized connection.
2069 if (wpa_s->num_multichan_concurrent < 2) {
2071 num = get_shared_radio_freqs(wpa_s, &freq, 1);
2072 if (num > 0 && freq > 0 && freq != params.freq.freq) {
2073 wpa_printf(MSG_DEBUG,
2074 "Assoc conflicting freq found (%d != %d)",
2075 freq, params.freq.freq);
2076 if (wpas_p2p_handle_frequency_conflicts(
2077 wpa_s, params.freq.freq, ssid) < 0) {
2078 wpas_connect_work_done(wpa_s);
2083 #endif /* CONFIG_P2P */
2085 ret = wpa_drv_associate(wpa_s, ¶ms);
2087 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
2089 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
2091 * The driver is known to mean what is saying, so we
2092 * can stop right here; the association will not
2095 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
2096 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2097 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
2100 /* try to continue anyway; new association will be tried again
2105 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2106 /* Set the key after the association just in case association
2107 * cleared the previously configured key. */
2108 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
2109 /* No need to timeout authentication since there is no key
2111 wpa_supplicant_cancel_auth_timeout(wpa_s);
2112 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2113 #ifdef CONFIG_IBSS_RSN
2114 } else if (ssid->mode == WPAS_MODE_IBSS &&
2115 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2116 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
2118 * RSN IBSS authentication is per-STA and we can disable the
2119 * per-BSSID authentication.
2121 wpa_supplicant_cancel_auth_timeout(wpa_s);
2122 #endif /* CONFIG_IBSS_RSN */
2124 /* Timeout for IEEE 802.11 authentication and association */
2128 /* give IBSS a bit more time */
2129 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
2130 } else if (wpa_s->conf->ap_scan == 1) {
2131 /* give IBSS a bit more time */
2132 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
2134 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
2138 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
2139 /* Set static WEP keys again */
2140 wpa_set_wep_keys(wpa_s, ssid);
2143 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
2145 * Do not allow EAP session resumption between different
2146 * network configurations.
2148 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2150 old_ssid = wpa_s->current_ssid;
2151 wpa_s->current_ssid = ssid;
2152 wpa_s->current_bss = bss;
2153 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
2154 wpa_supplicant_initiate_eapol(wpa_s);
2155 if (old_ssid != wpa_s->current_ssid)
2156 wpas_notify_network_changed(wpa_s);
2160 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
2163 struct wpa_ssid *old_ssid;
2165 wpas_connect_work_done(wpa_s);
2166 wpa_clear_keys(wpa_s, addr);
2167 old_ssid = wpa_s->current_ssid;
2168 wpa_supplicant_mark_disassoc(wpa_s);
2169 wpa_sm_set_config(wpa_s->wpa, NULL);
2170 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2171 if (old_ssid != wpa_s->current_ssid)
2172 wpas_notify_network_changed(wpa_s);
2173 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
2178 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
2179 * @wpa_s: Pointer to wpa_supplicant data
2180 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
2182 * This function is used to request %wpa_supplicant to deauthenticate from the
2185 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
2189 union wpa_event_data event;
2192 wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
2193 " pending_bssid=" MACSTR " reason=%d state=%s",
2194 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
2195 reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
2197 if (!is_zero_ether_addr(wpa_s->bssid))
2198 addr = wpa_s->bssid;
2199 else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
2200 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
2201 wpa_s->wpa_state == WPA_ASSOCIATING))
2202 addr = wpa_s->pending_bssid;
2203 else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
2205 * When using driver-based BSS selection, we may not know the
2206 * BSSID with which we are currently trying to associate. We
2207 * need to notify the driver of this disconnection even in such
2208 * a case, so use the all zeros address here.
2210 addr = wpa_s->bssid;
2215 wpa_tdls_teardown_peers(wpa_s->wpa);
2216 #endif /* CONFIG_TDLS */
2220 wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_GROUP_REMOVED "%s",
2222 wpa_supplicant_leave_mesh(wpa_s);
2224 #endif /* CONFIG_MESH */
2227 wpa_drv_deauthenticate(wpa_s, addr, reason_code);
2228 os_memset(&event, 0, sizeof(event));
2229 event.deauth_info.reason_code = (u16) reason_code;
2230 event.deauth_info.locally_generated = 1;
2231 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
2236 wpa_supplicant_clear_connection(wpa_s, addr);
2239 static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
2240 struct wpa_ssid *ssid)
2242 if (!ssid || !ssid->disabled || ssid->disabled == 2)
2246 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2247 wpas_notify_network_enabled_changed(wpa_s, ssid);
2250 * Try to reassociate since there is no current configuration and a new
2251 * network was made available.
2253 if (!wpa_s->current_ssid && !wpa_s->disconnected)
2254 wpa_s->reassociate = 1;
2259 * wpa_supplicant_enable_network - Mark a configured network as enabled
2260 * @wpa_s: wpa_supplicant structure for a network interface
2261 * @ssid: wpa_ssid structure for a configured network or %NULL
2263 * Enables the specified network or all networks if no network specified.
2265 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
2266 struct wpa_ssid *ssid)
2269 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2270 wpa_supplicant_enable_one_network(wpa_s, ssid);
2272 wpa_supplicant_enable_one_network(wpa_s, ssid);
2274 if (wpa_s->reassociate && !wpa_s->disconnected) {
2275 if (wpa_s->sched_scanning) {
2276 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
2277 "new network to scan filters");
2278 wpa_supplicant_cancel_sched_scan(wpa_s);
2281 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2282 wpa_supplicant_req_scan(wpa_s, 0, 0);
2288 * wpa_supplicant_disable_network - Mark a configured network as disabled
2289 * @wpa_s: wpa_supplicant structure for a network interface
2290 * @ssid: wpa_ssid structure for a configured network or %NULL
2292 * Disables the specified network or all networks if no network specified.
2294 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
2295 struct wpa_ssid *ssid)
2297 struct wpa_ssid *other_ssid;
2301 if (wpa_s->sched_scanning)
2302 wpa_supplicant_cancel_sched_scan(wpa_s);
2304 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2305 other_ssid = other_ssid->next) {
2306 was_disabled = other_ssid->disabled;
2307 if (was_disabled == 2)
2308 continue; /* do not change persistent P2P group
2311 other_ssid->disabled = 1;
2313 if (was_disabled != other_ssid->disabled)
2314 wpas_notify_network_enabled_changed(
2317 if (wpa_s->current_ssid)
2318 wpa_supplicant_deauthenticate(
2319 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2320 } else if (ssid->disabled != 2) {
2321 if (ssid == wpa_s->current_ssid)
2322 wpa_supplicant_deauthenticate(
2323 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2325 was_disabled = ssid->disabled;
2329 if (was_disabled != ssid->disabled) {
2330 wpas_notify_network_enabled_changed(wpa_s, ssid);
2331 if (wpa_s->sched_scanning) {
2332 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
2333 "to remove network from filters");
2334 wpa_supplicant_cancel_sched_scan(wpa_s);
2335 wpa_supplicant_req_scan(wpa_s, 0, 0);
2343 * wpa_supplicant_select_network - Attempt association with a network
2344 * @wpa_s: wpa_supplicant structure for a network interface
2345 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
2347 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
2348 struct wpa_ssid *ssid)
2351 struct wpa_ssid *other_ssid;
2352 int disconnected = 0;
2354 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
2355 wpa_supplicant_deauthenticate(
2356 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2361 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2364 * Mark all other networks disabled or mark all networks enabled if no
2365 * network specified.
2367 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2368 other_ssid = other_ssid->next) {
2369 int was_disabled = other_ssid->disabled;
2370 if (was_disabled == 2)
2371 continue; /* do not change persistent P2P group data */
2373 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
2374 if (was_disabled && !other_ssid->disabled)
2375 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
2377 if (was_disabled != other_ssid->disabled)
2378 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
2381 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
2382 /* We are already associated with the selected network */
2383 wpa_printf(MSG_DEBUG, "Already associated with the "
2384 "selected network - do nothing");
2389 wpa_s->current_ssid = ssid;
2390 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2391 wpa_s->connect_without_scan =
2392 (ssid->mode == WPAS_MODE_MESH) ? ssid : NULL;
2394 wpa_s->connect_without_scan = NULL;
2397 wpa_s->disconnected = 0;
2398 wpa_s->reassociate = 1;
2400 if (wpa_s->connect_without_scan ||
2401 wpa_supplicant_fast_associate(wpa_s) != 1)
2402 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
2405 wpas_notify_network_selected(wpa_s, ssid);
2410 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
2411 * @wpa_s: wpa_supplicant structure for a network interface
2412 * @pkcs11_engine_path: PKCS #11 engine path or NULL
2413 * @pkcs11_module_path: PKCS #11 module path or NULL
2414 * Returns: 0 on success; -1 on failure
2416 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
2417 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
2418 * module path fails the paths will be reset to the default value (NULL).
2420 int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
2421 const char *pkcs11_engine_path,
2422 const char *pkcs11_module_path)
2424 char *pkcs11_engine_path_copy = NULL;
2425 char *pkcs11_module_path_copy = NULL;
2427 if (pkcs11_engine_path != NULL) {
2428 pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
2429 if (pkcs11_engine_path_copy == NULL)
2432 if (pkcs11_module_path != NULL) {
2433 pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
2434 if (pkcs11_module_path_copy == NULL) {
2435 os_free(pkcs11_engine_path_copy);
2440 os_free(wpa_s->conf->pkcs11_engine_path);
2441 os_free(wpa_s->conf->pkcs11_module_path);
2442 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
2443 wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
2445 wpa_sm_set_eapol(wpa_s->wpa, NULL);
2446 eapol_sm_deinit(wpa_s->eapol);
2447 wpa_s->eapol = NULL;
2448 if (wpa_supplicant_init_eapol(wpa_s)) {
2449 /* Error -> Reset paths to the default value (NULL) once. */
2450 if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
2451 wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
2456 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2463 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
2464 * @wpa_s: wpa_supplicant structure for a network interface
2465 * @ap_scan: AP scan mode
2466 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
2469 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
2474 if (ap_scan < 0 || ap_scan > 2)
2478 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
2479 wpa_s->wpa_state >= WPA_ASSOCIATING &&
2480 wpa_s->wpa_state < WPA_COMPLETED) {
2481 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
2482 "associating", wpa_s->conf->ap_scan, ap_scan);
2485 #endif /* ANDROID */
2487 old_ap_scan = wpa_s->conf->ap_scan;
2488 wpa_s->conf->ap_scan = ap_scan;
2490 if (old_ap_scan != wpa_s->conf->ap_scan)
2491 wpas_notify_ap_scan_changed(wpa_s);
2498 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
2499 * @wpa_s: wpa_supplicant structure for a network interface
2500 * @expire_age: Expiration age in seconds
2501 * Returns: 0 if succeed or -1 if expire_age has an invalid value
2504 int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
2505 unsigned int bss_expire_age)
2507 if (bss_expire_age < 10) {
2508 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
2512 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
2514 wpa_s->conf->bss_expiration_age = bss_expire_age;
2521 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
2522 * @wpa_s: wpa_supplicant structure for a network interface
2523 * @expire_count: number of scans after which an unseen BSS is reclaimed
2524 * Returns: 0 if succeed or -1 if expire_count has an invalid value
2527 int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
2528 unsigned int bss_expire_count)
2530 if (bss_expire_count < 1) {
2531 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
2535 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
2537 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
2544 * wpa_supplicant_set_scan_interval - Set scan interval
2545 * @wpa_s: wpa_supplicant structure for a network interface
2546 * @scan_interval: scan interval in seconds
2547 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
2550 int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
2553 if (scan_interval < 0) {
2554 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
2558 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
2560 wpa_supplicant_update_scan_int(wpa_s, scan_interval);
2567 * wpa_supplicant_set_debug_params - Set global debug params
2568 * @global: wpa_global structure
2569 * @debug_level: debug level
2570 * @debug_timestamp: determines if show timestamp in debug data
2571 * @debug_show_keys: determines if show keys in debug data
2572 * Returns: 0 if succeed or -1 if debug_level has wrong value
2574 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
2575 int debug_timestamp, int debug_show_keys)
2578 int old_level, old_timestamp, old_show_keys;
2580 /* check for allowed debuglevels */
2581 if (debug_level != MSG_EXCESSIVE &&
2582 debug_level != MSG_MSGDUMP &&
2583 debug_level != MSG_DEBUG &&
2584 debug_level != MSG_INFO &&
2585 debug_level != MSG_WARNING &&
2586 debug_level != MSG_ERROR)
2589 old_level = wpa_debug_level;
2590 old_timestamp = wpa_debug_timestamp;
2591 old_show_keys = wpa_debug_show_keys;
2593 wpa_debug_level = debug_level;
2594 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
2595 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
2597 if (wpa_debug_level != old_level)
2598 wpas_notify_debug_level_changed(global);
2599 if (wpa_debug_timestamp != old_timestamp)
2600 wpas_notify_debug_timestamp_changed(global);
2601 if (wpa_debug_show_keys != old_show_keys)
2602 wpas_notify_debug_show_keys_changed(global);
2609 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
2610 * @wpa_s: Pointer to wpa_supplicant data
2611 * Returns: A pointer to the current network structure or %NULL on failure
2613 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
2615 struct wpa_ssid *entry;
2616 u8 ssid[MAX_SSID_LEN];
2622 res = wpa_drv_get_ssid(wpa_s, ssid);
2624 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
2630 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2631 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
2636 wired = wpa_s->conf->ap_scan == 0 &&
2637 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
2639 entry = wpa_s->conf->ssid;
2641 if (!wpas_network_disabled(wpa_s, entry) &&
2642 ((ssid_len == entry->ssid_len &&
2643 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
2644 (!entry->bssid_set ||
2645 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2648 if (!wpas_network_disabled(wpa_s, entry) &&
2649 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
2650 (entry->ssid == NULL || entry->ssid_len == 0) &&
2651 (!entry->bssid_set ||
2652 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2654 #endif /* CONFIG_WPS */
2656 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
2657 entry->ssid_len == 0 &&
2658 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
2661 entry = entry->next;
2668 static int select_driver(struct wpa_supplicant *wpa_s, int i)
2670 struct wpa_global *global = wpa_s->global;
2672 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
2673 global->drv_priv[i] = wpa_drivers[i]->global_init();
2674 if (global->drv_priv[i] == NULL) {
2675 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2676 "'%s'", wpa_drivers[i]->name);
2681 wpa_s->driver = wpa_drivers[i];
2682 wpa_s->global_drv_priv = global->drv_priv[i];
2688 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
2693 const char *pos, *driver = name;
2698 if (wpa_drivers[0] == NULL) {
2699 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
2705 /* default to first driver in the list */
2706 return select_driver(wpa_s, 0);
2710 pos = os_strchr(driver, ',');
2714 len = os_strlen(driver);
2716 for (i = 0; wpa_drivers[i]; i++) {
2717 if (os_strlen(wpa_drivers[i]->name) == len &&
2718 os_strncmp(driver, wpa_drivers[i]->name, len) ==
2720 /* First driver that succeeds wins */
2721 if (select_driver(wpa_s, i) == 0)
2729 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
2735 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
2736 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
2737 * with struct wpa_driver_ops::init()
2738 * @src_addr: Source address of the EAPOL frame
2739 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
2740 * @len: Length of the EAPOL data
2742 * This function is called for each received EAPOL frame. Most driver
2743 * interfaces rely on more generic OS mechanism for receiving frames through
2744 * l2_packet, but if such a mechanism is not available, the driver wrapper may
2745 * take care of received EAPOL frames and deliver them to the core supplicant
2746 * code by calling this function.
2748 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
2749 const u8 *buf, size_t len)
2751 struct wpa_supplicant *wpa_s = ctx;
2753 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
2754 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
2756 #ifdef CONFIG_PEERKEY
2757 if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
2758 wpa_s->current_ssid->peerkey &&
2759 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2760 wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
2761 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
2764 #endif /* CONFIG_PEERKEY */
2766 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
2767 (wpa_s->last_eapol_matches_bssid &&
2770 #endif /* CONFIG_AP */
2771 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
2773 * There is possible race condition between receiving the
2774 * association event and the EAPOL frame since they are coming
2775 * through different paths from the driver. In order to avoid
2776 * issues in trying to process the EAPOL frame before receiving
2777 * association information, lets queue it for processing until
2778 * the association event is received. This may also be needed in
2779 * driver-based roaming case, so also use src_addr != BSSID as a
2780 * trigger if we have previously confirmed that the
2781 * Authenticator uses BSSID as the src_addr (which is not the
2782 * case with wired IEEE 802.1X).
2784 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
2785 "of received EAPOL frame (state=%s bssid=" MACSTR ")",
2786 wpa_supplicant_state_txt(wpa_s->wpa_state),
2787 MAC2STR(wpa_s->bssid));
2788 wpabuf_free(wpa_s->pending_eapol_rx);
2789 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
2790 if (wpa_s->pending_eapol_rx) {
2791 os_get_reltime(&wpa_s->pending_eapol_rx_time);
2792 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
2798 wpa_s->last_eapol_matches_bssid =
2799 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
2802 if (wpa_s->ap_iface) {
2803 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2806 #endif /* CONFIG_AP */
2808 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
2809 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2810 "no key management is configured");
2814 if (wpa_s->eapol_received == 0 &&
2815 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
2816 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2817 wpa_s->wpa_state != WPA_COMPLETED) &&
2818 (wpa_s->current_ssid == NULL ||
2819 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
2820 /* Timeout for completing IEEE 802.1X and WPA authentication */
2821 wpa_supplicant_req_auth_timeout(
2823 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2824 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2825 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
2828 wpa_s->eapol_received++;
2830 if (wpa_s->countermeasures) {
2831 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2836 #ifdef CONFIG_IBSS_RSN
2837 if (wpa_s->current_ssid &&
2838 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
2839 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2842 #endif /* CONFIG_IBSS_RSN */
2844 /* Source address of the incoming EAPOL frame could be compared to the
2845 * current BSSID. However, it is possible that a centralized
2846 * Authenticator could be using another MAC address than the BSSID of
2847 * an AP, so just allow any address to be used for now. The replies are
2848 * still sent to the current BSSID (if available), though. */
2850 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2851 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
2852 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2854 wpa_drv_poll(wpa_s);
2855 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2856 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2857 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2859 * Set portValid = TRUE here since we are going to skip 4-way
2860 * handshake processing which would normally set portValid. We
2861 * need this to allow the EAPOL state machines to be completed
2862 * without going through EAPOL-Key handshake.
2864 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2869 int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
2871 if ((!wpa_s->p2p_mgmt ||
2872 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
2873 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
2874 l2_packet_deinit(wpa_s->l2);
2875 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2876 wpa_drv_get_mac_addr(wpa_s),
2878 wpa_supplicant_rx_eapol, wpa_s, 0);
2879 if (wpa_s->l2 == NULL)
2882 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2884 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2887 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2888 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2892 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2898 static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2899 const u8 *buf, size_t len)
2901 struct wpa_supplicant *wpa_s = ctx;
2902 const struct l2_ethhdr *eth;
2904 if (len < sizeof(*eth))
2906 eth = (const struct l2_ethhdr *) buf;
2908 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2909 !(eth->h_dest[0] & 0x01)) {
2910 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2911 " (bridge - not for this interface - ignore)",
2912 MAC2STR(src_addr), MAC2STR(eth->h_dest));
2916 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2917 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2918 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2919 len - sizeof(*eth));
2924 * wpa_supplicant_driver_init - Initialize driver interface parameters
2925 * @wpa_s: Pointer to wpa_supplicant data
2926 * Returns: 0 on success, -1 on failure
2928 * This function is called to initialize driver interface parameters.
2929 * wpa_drv_init() must have been called before this function to initialize the
2932 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2934 static int interface_count = 0;
2936 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2939 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2940 MAC2STR(wpa_s->own_addr));
2941 os_memcpy(wpa_s->perm_addr, wpa_s->own_addr, ETH_ALEN);
2942 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2944 if (wpa_s->bridge_ifname[0]) {
2945 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2946 "interface '%s'", wpa_s->bridge_ifname);
2947 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2950 wpa_supplicant_rx_eapol_bridge,
2952 if (wpa_s->l2_br == NULL) {
2953 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2954 "connection for the bridge interface '%s'",
2955 wpa_s->bridge_ifname);
2960 wpa_clear_keys(wpa_s, NULL);
2962 /* Make sure that TKIP countermeasures are not left enabled (could
2963 * happen if wpa_supplicant is killed during countermeasures. */
2964 wpa_drv_set_countermeasures(wpa_s, 0);
2966 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2967 wpa_drv_flush_pmkid(wpa_s);
2969 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
2970 wpa_s->prev_scan_wildcard = 0;
2972 if (wpa_supplicant_enabled_networks(wpa_s)) {
2973 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2974 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2975 interface_count = 0;
2978 if (!wpa_s->p2p_mgmt &&
2979 wpa_supplicant_delayed_sched_scan(wpa_s,
2980 interface_count % 3,
2982 wpa_supplicant_req_scan(wpa_s, interface_count % 3,
2984 #endif /* ANDROID */
2987 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2993 static int wpa_supplicant_daemon(const char *pid_file)
2995 wpa_printf(MSG_DEBUG, "Daemonize..");
2996 return os_daemonize(pid_file);
3000 static struct wpa_supplicant * wpa_supplicant_alloc(void)
3002 struct wpa_supplicant *wpa_s;
3004 wpa_s = os_zalloc(sizeof(*wpa_s));
3007 wpa_s->scan_req = INITIAL_SCAN_REQ;
3008 wpa_s->scan_interval = 5;
3009 wpa_s->new_connection = 1;
3010 wpa_s->parent = wpa_s;
3011 wpa_s->sched_scanning = 0;
3017 #ifdef CONFIG_HT_OVERRIDES
3019 static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
3020 struct ieee80211_ht_capabilities *htcaps,
3021 struct ieee80211_ht_capabilities *htcaps_mask,
3024 /* parse ht_mcs into hex array */
3026 const char *tmp = ht_mcs;
3029 /* If ht_mcs is null, do not set anything */
3033 /* This is what we are setting in the kernel */
3034 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
3036 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
3038 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
3040 long v = strtol(tmp, &end, 16);
3042 wpa_msg(wpa_s, MSG_DEBUG,
3043 "htcap value[%i]: %ld end: %p tmp: %p",
3048 htcaps->supported_mcs_set[i] = v;
3051 wpa_msg(wpa_s, MSG_ERROR,
3052 "Failed to parse ht-mcs: %s, error: %s\n",
3053 ht_mcs, strerror(errno));
3059 * If we were able to parse any values, then set mask for the MCS set.
3062 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
3063 IEEE80211_HT_MCS_MASK_LEN - 1);
3064 /* skip the 3 reserved bits */
3065 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
3073 static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
3074 struct ieee80211_ht_capabilities *htcaps,
3075 struct ieee80211_ht_capabilities *htcaps_mask,
3080 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
3085 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
3086 htcaps_mask->ht_capabilities_info |= msk;
3088 htcaps->ht_capabilities_info &= msk;
3090 htcaps->ht_capabilities_info |= msk;
3096 static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
3097 struct ieee80211_ht_capabilities *htcaps,
3098 struct ieee80211_ht_capabilities *htcaps_mask,
3101 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
3106 if (factor < 0 || factor > 3) {
3107 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
3108 "Must be 0-3 or -1", factor);
3112 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
3113 htcaps->a_mpdu_params &= ~0x3;
3114 htcaps->a_mpdu_params |= factor & 0x3;
3120 static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
3121 struct ieee80211_ht_capabilities *htcaps,
3122 struct ieee80211_ht_capabilities *htcaps_mask,
3125 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
3130 if (density < 0 || density > 7) {
3131 wpa_msg(wpa_s, MSG_ERROR,
3132 "ampdu_density: %d out of range. Must be 0-7 or -1.",
3137 htcaps_mask->a_mpdu_params |= 0x1C;
3138 htcaps->a_mpdu_params &= ~(0x1C);
3139 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
3145 static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
3146 struct ieee80211_ht_capabilities *htcaps,
3147 struct ieee80211_ht_capabilities *htcaps_mask,
3150 /* Masking these out disables HT40 */
3151 le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
3152 HT_CAP_INFO_SHORT_GI40MHZ);
3154 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
3157 htcaps->ht_capabilities_info &= ~msk;
3159 htcaps->ht_capabilities_info |= msk;
3161 htcaps_mask->ht_capabilities_info |= msk;
3167 static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
3168 struct ieee80211_ht_capabilities *htcaps,
3169 struct ieee80211_ht_capabilities *htcaps_mask,
3172 /* Masking these out disables SGI */
3173 le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
3174 HT_CAP_INFO_SHORT_GI40MHZ);
3176 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
3179 htcaps->ht_capabilities_info &= ~msk;
3181 htcaps->ht_capabilities_info |= msk;
3183 htcaps_mask->ht_capabilities_info |= msk;
3189 static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
3190 struct ieee80211_ht_capabilities *htcaps,
3191 struct ieee80211_ht_capabilities *htcaps_mask,
3194 /* Masking these out disables LDPC */
3195 le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
3197 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
3200 htcaps->ht_capabilities_info &= ~msk;
3202 htcaps->ht_capabilities_info |= msk;
3204 htcaps_mask->ht_capabilities_info |= msk;
3210 void wpa_supplicant_apply_ht_overrides(
3211 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3212 struct wpa_driver_associate_params *params)
3214 struct ieee80211_ht_capabilities *htcaps;
3215 struct ieee80211_ht_capabilities *htcaps_mask;
3220 params->disable_ht = ssid->disable_ht;
3221 if (!params->htcaps || !params->htcaps_mask)
3224 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
3225 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
3226 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
3227 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
3228 ssid->disable_max_amsdu);
3229 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
3230 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
3231 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
3232 wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
3233 wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
3235 if (ssid->ht40_intolerant) {
3236 le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
3237 htcaps->ht_capabilities_info |= bit;
3238 htcaps_mask->ht_capabilities_info |= bit;
3242 #endif /* CONFIG_HT_OVERRIDES */
3245 #ifdef CONFIG_VHT_OVERRIDES
3246 void wpa_supplicant_apply_vht_overrides(
3247 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3248 struct wpa_driver_associate_params *params)
3250 struct ieee80211_vht_capabilities *vhtcaps;
3251 struct ieee80211_vht_capabilities *vhtcaps_mask;
3256 params->disable_vht = ssid->disable_vht;
3258 vhtcaps = (void *) params->vhtcaps;
3259 vhtcaps_mask = (void *) params->vhtcaps_mask;
3261 if (!vhtcaps || !vhtcaps_mask)
3264 vhtcaps->vht_capabilities_info = ssid->vht_capa;
3265 vhtcaps_mask->vht_capabilities_info = ssid->vht_capa_mask;
3267 #ifdef CONFIG_HT_OVERRIDES
3268 /* if max ampdu is <= 3, we have to make the HT cap the same */
3269 if (ssid->vht_capa_mask & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) {
3272 max_ampdu = (ssid->vht_capa &
3273 VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
3274 VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT;
3276 max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
3277 wpa_set_ampdu_factor(wpa_s,
3278 (void *) params->htcaps,
3279 (void *) params->htcaps_mask,
3282 #endif /* CONFIG_HT_OVERRIDES */
3284 #define OVERRIDE_MCS(i) \
3285 if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
3286 vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
3288 vhtcaps->vht_supported_mcs_set.tx_map |= \
3289 ssid->vht_tx_mcs_nss_ ##i << 2 * (i - 1); \
3291 if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
3292 vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
3294 vhtcaps->vht_supported_mcs_set.rx_map |= \
3295 ssid->vht_rx_mcs_nss_ ##i << 2 * (i - 1); \
3307 #endif /* CONFIG_VHT_OVERRIDES */
3310 static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
3315 if (!wpa_s->conf->pcsc_reader)
3318 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
3322 if (wpa_s->conf->pcsc_pin &&
3323 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
3324 scard_deinit(wpa_s->scard);
3325 wpa_s->scard = NULL;
3326 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
3330 len = sizeof(wpa_s->imsi) - 1;
3331 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
3332 scard_deinit(wpa_s->scard);
3333 wpa_s->scard = NULL;
3334 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
3337 wpa_s->imsi[len] = '\0';
3339 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
3341 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
3342 wpa_s->imsi, wpa_s->mnc_len);
3344 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
3345 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
3346 #endif /* PCSC_FUNCS */
3352 int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
3356 ext_password_deinit(wpa_s->ext_pw);
3357 wpa_s->ext_pw = NULL;
3358 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
3360 if (!wpa_s->conf->ext_password_backend)
3363 val = os_strdup(wpa_s->conf->ext_password_backend);
3366 pos = os_strchr(val, ':');
3370 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
3372 wpa_s->ext_pw = ext_password_init(val, pos);
3374 if (wpa_s->ext_pw == NULL) {
3375 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
3378 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
3384 static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
3385 const struct wpa_driver_capa *capa)
3387 struct wowlan_triggers *triggers;
3390 if (!wpa_s->conf->wowlan_triggers)
3393 triggers = wpa_get_wowlan_triggers(wpa_s->conf->wowlan_triggers, capa);
3395 ret = wpa_drv_wowlan(wpa_s, triggers);
3402 static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
3405 struct wpa_supplicant *iface = wpa_s->global->ifaces;
3406 struct wpa_radio *radio;
3408 while (rn && iface) {
3409 radio = iface->radio;
3410 if (radio && os_strcmp(rn, radio->name) == 0) {
3411 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
3413 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3417 iface = iface->next;
3420 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
3421 wpa_s->ifname, rn ? rn : "N/A");
3422 radio = os_zalloc(sizeof(*radio));
3427 os_strlcpy(radio->name, rn, sizeof(radio->name));
3428 dl_list_init(&radio->ifaces);
3429 dl_list_init(&radio->work);
3430 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3436 static void radio_work_free(struct wpa_radio_work *work)
3438 if (work->wpa_s->scan_work == work) {
3439 /* This should not really happen. */
3440 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
3441 work->type, work, work->started);
3442 work->wpa_s->scan_work = NULL;
3446 if (work->wpa_s->p2p_scan_work == work) {
3447 /* This should not really happen. */
3448 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
3449 work->type, work, work->started);
3450 work->wpa_s->p2p_scan_work = NULL;
3452 #endif /* CONFIG_P2P */
3454 dl_list_del(&work->list);
3459 static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
3461 struct wpa_radio *radio = eloop_ctx;
3462 struct wpa_radio_work *work;
3463 struct os_reltime now, diff;
3464 struct wpa_supplicant *wpa_s;
3466 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
3471 return; /* already started and still in progress */
3473 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
3475 if (wpa_s && wpa_s->external_scan_running) {
3476 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
3480 os_get_reltime(&now);
3481 os_reltime_sub(&now, &work->time, &diff);
3482 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting radio work '%s'@%p after %ld.%06ld second wait",
3483 work->type, work, diff.sec, diff.usec);
3491 * This function removes both started and pending radio works running on
3492 * the provided interface's radio.
3493 * Prior to the removal of the radio work, its callback (cb) is called with
3494 * deinit set to be 1. Each work's callback is responsible for clearing its
3495 * internal data and restoring to a correct state.
3496 * @wpa_s: wpa_supplicant data
3497 * @type: type of works to be removed
3498 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
3499 * this interface's works.
3501 void radio_remove_works(struct wpa_supplicant *wpa_s,
3502 const char *type, int remove_all)
3504 struct wpa_radio_work *work, *tmp;
3505 struct wpa_radio *radio = wpa_s->radio;
3507 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
3509 if (type && os_strcmp(type, work->type) != 0)
3512 /* skip other ifaces' works */
3513 if (!remove_all && work->wpa_s != wpa_s)
3516 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
3517 work->type, work, work->started ? " (started)" : "");
3519 radio_work_free(work);
3522 /* in case we removed the started work */
3523 radio_work_check_next(wpa_s);
3527 static void radio_remove_interface(struct wpa_supplicant *wpa_s)
3529 struct wpa_radio *radio = wpa_s->radio;
3534 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
3535 wpa_s->ifname, radio->name);
3536 dl_list_del(&wpa_s->radio_list);
3537 radio_remove_works(wpa_s, NULL, 0);
3538 wpa_s->radio = NULL;
3539 if (!dl_list_empty(&radio->ifaces))
3540 return; /* Interfaces remain for this radio */
3542 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
3543 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
3548 void radio_work_check_next(struct wpa_supplicant *wpa_s)
3550 struct wpa_radio *radio = wpa_s->radio;
3552 if (dl_list_empty(&radio->work))
3554 if (wpa_s->ext_work_in_progress) {
3555 wpa_printf(MSG_DEBUG,
3556 "External radio work in progress - delay start of pending item");
3559 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
3560 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
3565 * radio_add_work - Add a radio work item
3566 * @wpa_s: Pointer to wpa_supplicant data
3567 * @freq: Frequency of the offchannel operation in MHz or 0
3568 * @type: Unique identifier for each type of work
3569 * @next: Force as the next work to be executed
3570 * @cb: Callback function for indicating when radio is available
3571 * @ctx: Context pointer for the work (work->ctx in cb())
3572 * Returns: 0 on success, -1 on failure
3574 * This function is used to request time for an operation that requires
3575 * exclusive radio control. Once the radio is available, the registered callback
3576 * function will be called. radio_work_done() must be called once the exclusive
3577 * radio operation has been completed, so that the radio is freed for other
3578 * operations. The special case of deinit=1 is used to free the context data
3579 * during interface removal. That does not allow the callback function to start
3580 * the radio operation, i.e., it must free any resources allocated for the radio
3583 * The @freq parameter can be used to indicate a single channel on which the
3584 * offchannel operation will occur. This may allow multiple radio work
3585 * operations to be performed in parallel if they apply for the same channel.
3586 * Setting this to 0 indicates that the work item may use multiple channels or
3587 * requires exclusive control of the radio.
3589 int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
3590 const char *type, int next,
3591 void (*cb)(struct wpa_radio_work *work, int deinit),
3594 struct wpa_radio_work *work;
3597 work = os_zalloc(sizeof(*work));
3600 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
3601 os_get_reltime(&work->time);
3604 work->wpa_s = wpa_s;
3608 was_empty = dl_list_empty(&wpa_s->radio->work);
3610 dl_list_add(&wpa_s->radio->work, &work->list);
3612 dl_list_add_tail(&wpa_s->radio->work, &work->list);
3614 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
3615 radio_work_check_next(wpa_s);
3623 * radio_work_done - Indicate that a radio work item has been completed
3624 * @work: Completed work
3626 * This function is called once the callback function registered with
3627 * radio_add_work() has completed its work.
3629 void radio_work_done(struct wpa_radio_work *work)
3631 struct wpa_supplicant *wpa_s = work->wpa_s;
3632 struct os_reltime now, diff;
3633 unsigned int started = work->started;
3635 os_get_reltime(&now);
3636 os_reltime_sub(&now, &work->time, &diff);
3637 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
3638 work->type, work, started ? "done" : "canceled",
3639 diff.sec, diff.usec);
3640 radio_work_free(work);
3642 radio_work_check_next(wpa_s);
3646 struct wpa_radio_work *
3647 radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
3649 struct wpa_radio_work *work;
3650 struct wpa_radio *radio = wpa_s->radio;
3652 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3653 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
3661 static int wpas_init_driver(struct wpa_supplicant *wpa_s,
3662 struct wpa_interface *iface)
3664 const char *ifname, *driver, *rn;
3666 driver = iface->driver;
3668 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
3671 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
3672 if (wpa_s->drv_priv == NULL) {
3674 pos = driver ? os_strchr(driver, ',') : NULL;
3676 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
3677 "driver interface - try next driver wrapper");
3681 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
3685 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
3686 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
3687 "driver_param '%s'", wpa_s->conf->driver_param);
3691 ifname = wpa_drv_get_ifname(wpa_s);
3692 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
3693 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
3694 "interface name with '%s'", ifname);
3695 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
3698 rn = wpa_driver_get_radio_name(wpa_s);
3699 if (rn && rn[0] == '\0')
3702 wpa_s->radio = radio_add_interface(wpa_s, rn);
3703 if (wpa_s->radio == NULL)
3710 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
3711 struct wpa_interface *iface)
3713 struct wpa_driver_capa capa;
3716 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
3717 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
3718 iface->confname ? iface->confname : "N/A",
3719 iface->driver ? iface->driver : "default",
3720 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
3721 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
3723 if (iface->confname) {
3724 #ifdef CONFIG_BACKEND_FILE
3725 wpa_s->confname = os_rel2abs_path(iface->confname);
3726 if (wpa_s->confname == NULL) {
3727 wpa_printf(MSG_ERROR, "Failed to get absolute path "
3728 "for configuration file '%s'.",
3732 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
3733 iface->confname, wpa_s->confname);
3734 #else /* CONFIG_BACKEND_FILE */
3735 wpa_s->confname = os_strdup(iface->confname);
3736 #endif /* CONFIG_BACKEND_FILE */
3737 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
3738 if (wpa_s->conf == NULL) {
3739 wpa_printf(MSG_ERROR, "Failed to read or parse "
3740 "configuration '%s'.", wpa_s->confname);
3743 wpa_s->confanother = os_rel2abs_path(iface->confanother);
3744 wpa_config_read(wpa_s->confanother, wpa_s->conf);
3747 * Override ctrl_interface and driver_param if set on command
3750 if (iface->ctrl_interface) {
3751 os_free(wpa_s->conf->ctrl_interface);
3752 wpa_s->conf->ctrl_interface =
3753 os_strdup(iface->ctrl_interface);
3756 if (iface->driver_param) {
3757 os_free(wpa_s->conf->driver_param);
3758 wpa_s->conf->driver_param =
3759 os_strdup(iface->driver_param);
3762 if (iface->p2p_mgmt && !iface->ctrl_interface) {
3763 os_free(wpa_s->conf->ctrl_interface);
3764 wpa_s->conf->ctrl_interface = NULL;
3767 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
3768 iface->driver_param);
3770 if (wpa_s->conf == NULL) {
3771 wpa_printf(MSG_ERROR, "\nNo configuration found.");
3775 if (iface->ifname == NULL) {
3776 wpa_printf(MSG_ERROR, "\nInterface name is required.");
3779 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
3780 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
3784 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
3786 if (iface->bridge_ifname) {
3787 if (os_strlen(iface->bridge_ifname) >=
3788 sizeof(wpa_s->bridge_ifname)) {
3789 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
3790 "name '%s'.", iface->bridge_ifname);
3793 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
3794 sizeof(wpa_s->bridge_ifname));
3797 /* RSNA Supplicant Key Management - INITIALIZE */
3798 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
3799 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
3801 /* Initialize driver interface and register driver event handler before
3802 * L2 receive handler so that association events are processed before
3803 * EAPOL-Key packets if both become available for the same select()
3805 if (wpas_init_driver(wpa_s, iface) < 0)
3808 if (wpa_supplicant_init_wpa(wpa_s) < 0)
3811 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
3812 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
3814 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
3816 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
3817 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
3818 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
3819 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3820 "dot11RSNAConfigPMKLifetime");
3824 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
3825 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
3826 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
3827 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3828 "dot11RSNAConfigPMKReauthThreshold");
3832 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
3833 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
3834 wpa_s->conf->dot11RSNAConfigSATimeout)) {
3835 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3836 "dot11RSNAConfigSATimeout");
3840 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
3841 &wpa_s->hw.num_modes,
3844 capa_res = wpa_drv_get_capa(wpa_s, &capa);
3845 if (capa_res == 0) {
3846 wpa_s->drv_capa_known = 1;
3847 wpa_s->drv_flags = capa.flags;
3848 wpa_s->drv_enc = capa.enc;
3849 wpa_s->drv_smps_modes = capa.smps_modes;
3850 wpa_s->drv_rrm_flags = capa.rrm_flags;
3851 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
3852 wpa_s->max_scan_ssids = capa.max_scan_ssids;
3853 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
3854 wpa_s->sched_scan_supported = capa.sched_scan_supported;
3855 wpa_s->max_match_sets = capa.max_match_sets;
3856 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
3857 wpa_s->max_stations = capa.max_stations;
3858 wpa_s->extended_capa = capa.extended_capa;
3859 wpa_s->extended_capa_mask = capa.extended_capa_mask;
3860 wpa_s->extended_capa_len = capa.extended_capa_len;
3861 wpa_s->num_multichan_concurrent =
3862 capa.num_multichan_concurrent;
3863 wpa_s->wmm_ac_supported = capa.wmm_ac_supported;
3865 if (wpa_s->max_remain_on_chan == 0)
3866 wpa_s->max_remain_on_chan = 1000;
3869 * Only take p2p_mgmt parameters when P2P Device is supported.
3870 * Doing it here as it determines whether l2_packet_init() will be done
3871 * during wpa_supplicant_driver_init().
3873 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
3874 wpa_s->p2p_mgmt = iface->p2p_mgmt;
3876 iface->p2p_mgmt = 1;
3878 if (wpa_s->num_multichan_concurrent == 0)
3879 wpa_s->num_multichan_concurrent = 1;
3881 if (wpa_supplicant_driver_init(wpa_s) < 0)
3885 if ((!iface->p2p_mgmt ||
3886 !(wpa_s->drv_flags &
3887 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
3888 wpa_tdls_init(wpa_s->wpa))
3890 #endif /* CONFIG_TDLS */
3892 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
3893 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
3894 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
3898 if (wpas_wps_init(wpa_s))
3901 if (wpa_supplicant_init_eapol(wpa_s) < 0)
3903 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
3905 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
3906 if (wpa_s->ctrl_iface == NULL) {
3907 wpa_printf(MSG_ERROR,
3908 "Failed to initialize control interface '%s'.\n"
3909 "You may have another wpa_supplicant process "
3910 "already running or the file was\n"
3911 "left by an unclean termination of wpa_supplicant "
3912 "in which case you will need\n"
3913 "to manually remove this file before starting "
3914 "wpa_supplicant again.\n",
3915 wpa_s->conf->ctrl_interface);
3919 wpa_s->gas = gas_query_init(wpa_s);
3920 if (wpa_s->gas == NULL) {
3921 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
3925 if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
3926 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
3930 if (wpa_bss_init(wpa_s) < 0)
3934 * Set Wake-on-WLAN triggers, if configured.
3935 * Note: We don't restore/remove the triggers on shutdown (it doesn't
3936 * have effect anyway when the interface is down).
3938 if (capa_res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
3941 #ifdef CONFIG_EAP_PROXY
3944 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
3946 if (wpa_s->mnc_len > 0) {
3947 wpa_s->imsi[len] = '\0';
3948 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
3949 wpa_s->imsi, wpa_s->mnc_len);
3951 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
3954 #endif /* CONFIG_EAP_PROXY */
3956 if (pcsc_reader_init(wpa_s) < 0)
3959 if (wpas_init_ext_pw(wpa_s) < 0)
3962 wpas_rrm_reset(wpa_s);
3968 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
3969 int notify, int terminate)
3971 struct wpa_global *global = wpa_s->global;
3972 struct wpa_supplicant *iface, *prev;
3974 if (wpa_s == wpa_s->parent)
3975 wpas_p2p_group_remove(wpa_s, "*");
3977 iface = global->ifaces;
3979 if (iface == wpa_s || iface->parent != wpa_s) {
3980 iface = iface->next;
3983 wpa_printf(MSG_DEBUG,
3984 "Remove remaining child interface %s from parent %s",
3985 iface->ifname, wpa_s->ifname);
3987 iface = iface->next;
3988 wpa_supplicant_remove_iface(global, prev, terminate);
3991 wpa_s->disconnected = 1;
3992 if (wpa_s->drv_priv) {
3993 wpa_supplicant_deauthenticate(wpa_s,
3994 WLAN_REASON_DEAUTH_LEAVING);
3996 wpa_drv_set_countermeasures(wpa_s, 0);
3997 wpa_clear_keys(wpa_s, NULL);
4000 wpa_supplicant_cleanup(wpa_s);
4001 wpas_p2p_deinit_iface(wpa_s);
4003 wpas_ctrl_radio_work_flush(wpa_s);
4004 radio_remove_interface(wpa_s);
4006 if (wpa_s->drv_priv)
4007 wpa_drv_deinit(wpa_s);
4010 wpas_notify_iface_removed(wpa_s);
4013 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
4015 if (wpa_s->ctrl_iface) {
4016 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
4017 wpa_s->ctrl_iface = NULL;
4022 wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
4023 wpa_s->ifmsh = NULL;
4025 #endif /* CONFIG_MESH */
4027 if (wpa_s->conf != NULL) {
4028 wpa_config_free(wpa_s->conf);
4037 * wpa_supplicant_add_iface - Add a new network interface
4038 * @global: Pointer to global data from wpa_supplicant_init()
4039 * @iface: Interface configuration options
4040 * Returns: Pointer to the created interface or %NULL on failure
4042 * This function is used to add new network interfaces for %wpa_supplicant.
4043 * This can be called before wpa_supplicant_run() to add interfaces before the
4044 * main event loop has been started. In addition, new interfaces can be added
4045 * dynamically while %wpa_supplicant is already running. This could happen,
4046 * e.g., when a hotplug network adapter is inserted.
4048 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
4049 struct wpa_interface *iface)
4051 struct wpa_supplicant *wpa_s;
4052 struct wpa_interface t_iface;
4053 struct wpa_ssid *ssid;
4055 if (global == NULL || iface == NULL)
4058 wpa_s = wpa_supplicant_alloc();
4062 wpa_s->global = global;
4065 if (global->params.override_driver) {
4066 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
4068 iface->driver, global->params.override_driver);
4069 t_iface.driver = global->params.override_driver;
4071 if (global->params.override_ctrl_interface) {
4072 wpa_printf(MSG_DEBUG, "Override interface parameter: "
4073 "ctrl_interface ('%s' -> '%s')",
4074 iface->ctrl_interface,
4075 global->params.override_ctrl_interface);
4076 t_iface.ctrl_interface =
4077 global->params.override_ctrl_interface;
4079 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
4080 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
4082 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
4086 if (iface->p2p_mgmt == 0) {
4087 /* Notify the control interfaces about new iface */
4088 if (wpas_notify_iface_added(wpa_s)) {
4089 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
4093 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
4094 wpas_notify_network_added(wpa_s, ssid);
4097 wpa_s->next = global->ifaces;
4098 global->ifaces = wpa_s;
4100 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
4101 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
4104 if (wpa_s->global->p2p == NULL &&
4105 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4106 wpas_p2p_add_p2pdev_interface(wpa_s, iface->conf_p2p_dev) < 0) {
4107 wpa_printf(MSG_INFO,
4108 "P2P: Failed to enable P2P Device interface");
4109 /* Try to continue without. P2P will be disabled. */
4111 #endif /* CONFIG_P2P */
4118 * wpa_supplicant_remove_iface - Remove a network interface
4119 * @global: Pointer to global data from wpa_supplicant_init()
4120 * @wpa_s: Pointer to the network interface to be removed
4121 * Returns: 0 if interface was removed, -1 if interface was not found
4123 * This function can be used to dynamically remove network interfaces from
4124 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
4125 * addition, this function is used to remove all remaining interfaces when
4126 * %wpa_supplicant is terminated.
4128 int wpa_supplicant_remove_iface(struct wpa_global *global,
4129 struct wpa_supplicant *wpa_s,
4132 struct wpa_supplicant *prev;
4134 unsigned int mesh_if_created = wpa_s->mesh_if_created;
4135 char *ifname = NULL;
4136 #endif /* CONFIG_MESH */
4138 /* Remove interface from the global list of interfaces */
4139 prev = global->ifaces;
4140 if (prev == wpa_s) {
4141 global->ifaces = wpa_s->next;
4143 while (prev && prev->next != wpa_s)
4147 prev->next = wpa_s->next;
4150 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
4153 if (mesh_if_created) {
4154 ifname = os_strdup(wpa_s->ifname);
4155 if (ifname == NULL) {
4156 wpa_dbg(wpa_s, MSG_ERROR,
4157 "mesh: Failed to malloc ifname");
4161 #endif /* CONFIG_MESH */
4163 if (global->p2p_group_formation == wpa_s)
4164 global->p2p_group_formation = NULL;
4165 if (global->p2p_invite_group == wpa_s)
4166 global->p2p_invite_group = NULL;
4167 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
4170 if (mesh_if_created) {
4171 wpa_drv_if_remove(global->ifaces, WPA_IF_MESH, ifname);
4174 #endif /* CONFIG_MESH */
4181 * wpa_supplicant_get_eap_mode - Get the current EAP mode
4182 * @wpa_s: Pointer to the network interface
4183 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
4185 const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
4187 const char *eapol_method;
4189 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
4190 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
4194 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
4195 if (eapol_method == NULL)
4196 return "UNKNOWN-EAP";
4198 return eapol_method;
4203 * wpa_supplicant_get_iface - Get a new network interface
4204 * @global: Pointer to global data from wpa_supplicant_init()
4205 * @ifname: Interface name
4206 * Returns: Pointer to the interface or %NULL if not found
4208 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
4211 struct wpa_supplicant *wpa_s;
4213 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4214 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4221 #ifndef CONFIG_NO_WPA_MSG
4222 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
4224 struct wpa_supplicant *wpa_s = ctx;
4227 return wpa_s->ifname;
4229 #endif /* CONFIG_NO_WPA_MSG */
4233 * wpa_supplicant_init - Initialize %wpa_supplicant
4234 * @params: Parameters for %wpa_supplicant
4235 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
4237 * This function is used to initialize %wpa_supplicant. After successful
4238 * initialization, the returned data pointer can be used to add and remove
4239 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
4241 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
4243 struct wpa_global *global;
4249 #ifdef CONFIG_DRIVER_NDIS
4251 void driver_ndis_init_ops(void);
4252 driver_ndis_init_ops();
4254 #endif /* CONFIG_DRIVER_NDIS */
4256 #ifndef CONFIG_NO_WPA_MSG
4257 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
4258 #endif /* CONFIG_NO_WPA_MSG */
4260 if (params->wpa_debug_file_path)
4261 wpa_debug_open_file(params->wpa_debug_file_path);
4263 wpa_debug_setup_stdout();
4264 if (params->wpa_debug_syslog)
4265 wpa_debug_open_syslog();
4266 if (params->wpa_debug_tracing) {
4267 ret = wpa_debug_open_linux_tracing();
4269 wpa_printf(MSG_ERROR,
4270 "Failed to enable trace logging");
4275 ret = eap_register_methods();
4277 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
4279 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
4280 "the same EAP type.");
4284 global = os_zalloc(sizeof(*global));
4287 dl_list_init(&global->p2p_srv_bonjour);
4288 dl_list_init(&global->p2p_srv_upnp);
4289 global->params.daemonize = params->daemonize;
4290 global->params.wait_for_monitor = params->wait_for_monitor;
4291 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
4292 if (params->pid_file)
4293 global->params.pid_file = os_strdup(params->pid_file);
4294 if (params->ctrl_interface)
4295 global->params.ctrl_interface =
4296 os_strdup(params->ctrl_interface);
4297 if (params->ctrl_interface_group)
4298 global->params.ctrl_interface_group =
4299 os_strdup(params->ctrl_interface_group);
4300 if (params->override_driver)
4301 global->params.override_driver =
4302 os_strdup(params->override_driver);
4303 if (params->override_ctrl_interface)
4304 global->params.override_ctrl_interface =
4305 os_strdup(params->override_ctrl_interface);
4306 wpa_debug_level = global->params.wpa_debug_level =
4307 params->wpa_debug_level;
4308 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
4309 params->wpa_debug_show_keys;
4310 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
4311 params->wpa_debug_timestamp;
4313 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
4316 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
4317 wpa_supplicant_deinit(global);
4321 random_init(params->entropy_file);
4323 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
4324 if (global->ctrl_iface == NULL) {
4325 wpa_supplicant_deinit(global);
4329 if (wpas_notify_supplicant_initialized(global)) {
4330 wpa_supplicant_deinit(global);
4334 for (i = 0; wpa_drivers[i]; i++)
4335 global->drv_count++;
4336 if (global->drv_count == 0) {
4337 wpa_printf(MSG_ERROR, "No drivers enabled");
4338 wpa_supplicant_deinit(global);
4341 global->drv_priv = os_calloc(global->drv_count, sizeof(void *));
4342 if (global->drv_priv == NULL) {
4343 wpa_supplicant_deinit(global);
4347 #ifdef CONFIG_WIFI_DISPLAY
4348 if (wifi_display_init(global) < 0) {
4349 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
4350 wpa_supplicant_deinit(global);
4353 #endif /* CONFIG_WIFI_DISPLAY */
4360 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
4361 * @global: Pointer to global data from wpa_supplicant_init()
4362 * Returns: 0 after successful event loop run, -1 on failure
4364 * This function starts the main event loop and continues running as long as
4365 * there are any remaining events. In most cases, this function is running as
4366 * long as the %wpa_supplicant process in still in use.
4368 int wpa_supplicant_run(struct wpa_global *global)
4370 struct wpa_supplicant *wpa_s;
4372 if (global->params.daemonize &&
4373 wpa_supplicant_daemon(global->params.pid_file))
4376 if (global->params.wait_for_monitor) {
4377 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
4378 if (wpa_s->ctrl_iface)
4379 wpa_supplicant_ctrl_iface_wait(
4383 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
4384 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
4393 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
4394 * @global: Pointer to global data from wpa_supplicant_init()
4396 * This function is called to deinitialize %wpa_supplicant and to free all
4397 * allocated resources. Remaining network interfaces will also be removed.
4399 void wpa_supplicant_deinit(struct wpa_global *global)
4406 #ifdef CONFIG_WIFI_DISPLAY
4407 wifi_display_deinit(global);
4408 #endif /* CONFIG_WIFI_DISPLAY */
4410 while (global->ifaces)
4411 wpa_supplicant_remove_iface(global, global->ifaces, 1);
4413 if (global->ctrl_iface)
4414 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
4416 wpas_notify_supplicant_deinitialized(global);
4418 eap_peer_unregister_methods();
4420 eap_server_unregister_methods();
4421 #endif /* CONFIG_AP */
4423 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
4424 if (!global->drv_priv[i])
4426 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
4428 os_free(global->drv_priv);
4434 if (global->params.pid_file) {
4435 os_daemonize_terminate(global->params.pid_file);
4436 os_free(global->params.pid_file);
4438 os_free(global->params.ctrl_interface);
4439 os_free(global->params.ctrl_interface_group);
4440 os_free(global->params.override_driver);
4441 os_free(global->params.override_ctrl_interface);
4443 os_free(global->p2p_disallow_freq.range);
4444 os_free(global->p2p_go_avoid_freq.range);
4445 os_free(global->add_psk);
4448 wpa_debug_close_syslog();
4449 wpa_debug_close_file();
4450 wpa_debug_close_linux_tracing();
4454 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
4456 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4457 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4459 country[0] = wpa_s->conf->country[0];
4460 country[1] = wpa_s->conf->country[1];
4462 if (wpa_drv_set_country(wpa_s, country) < 0) {
4463 wpa_printf(MSG_ERROR, "Failed to set country code "
4468 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
4469 wpas_init_ext_pw(wpa_s);
4472 wpas_wps_update_config(wpa_s);
4473 #endif /* CONFIG_WPS */
4474 wpas_p2p_update_config(wpa_s);
4475 wpa_s->conf->changed_parameters = 0;
4479 void add_freq(int *freqs, int *num_freqs, int freq)
4483 for (i = 0; i < *num_freqs; i++) {
4484 if (freqs[i] == freq)
4488 freqs[*num_freqs] = freq;
4493 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
4495 struct wpa_bss *bss, *cbss;
4496 const int max_freqs = 10;
4500 freqs = os_calloc(max_freqs + 1, sizeof(int));
4504 cbss = wpa_s->current_bss;
4506 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
4509 if (bss->ssid_len == cbss->ssid_len &&
4510 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
4511 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
4512 add_freq(freqs, &num_freqs, bss->freq);
4513 if (num_freqs == max_freqs)
4518 if (num_freqs == 0) {
4527 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
4533 wpas_connect_work_done(wpa_s);
4536 * Remove possible authentication timeout since the connection failed.
4538 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
4540 if (wpa_s->disconnected) {
4542 * There is no point in blacklisting the AP if this event is
4543 * generated based on local request to disconnect.
4545 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
4546 "indication since interface has been put into "
4547 "disconnected state");
4552 * Add the failed BSSID into the blacklist and speed up next scan
4553 * attempt if there could be other APs that could accept association.
4554 * The current blacklist count indicates how many times we have tried
4555 * connecting to this AP and multiple attempts mean that other APs are
4556 * either not available or has already been tried, so that we can start
4557 * increasing the delay here to avoid constant scanning.
4559 count = wpa_blacklist_add(wpa_s, bssid);
4560 if (count == 1 && wpa_s->current_bss) {
4562 * This BSS was not in the blacklist before. If there is
4563 * another BSS available for the same ESS, we should try that
4564 * next. Otherwise, we may as well try this one once more
4565 * before allowing other, likely worse, ESSes to be considered.
4567 freqs = get_bss_freqs_in_ess(wpa_s);
4569 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
4570 "has been seen; try it next");
4571 wpa_blacklist_add(wpa_s, bssid);
4573 * On the next scan, go through only the known channels
4574 * used in this ESS based on previous scans to speed up
4575 * common load balancing use case.
4577 os_free(wpa_s->next_scan_freqs);
4578 wpa_s->next_scan_freqs = freqs;
4583 * Add previous failure count in case the temporary blacklist was
4584 * cleared due to no other BSSes being available.
4586 count += wpa_s->extra_blacklist_count;
4588 if (count > 3 && wpa_s->current_ssid) {
4589 wpa_printf(MSG_DEBUG, "Continuous association failures - "
4590 "consider temporary network disabling");
4591 wpas_auth_failed(wpa_s, "CONN_FAILED");
4612 wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
4613 "ms", count, timeout);
4616 * TODO: if more than one possible AP is available in scan results,
4617 * could try the other ones before requesting a new scan.
4619 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
4620 1000 * (timeout % 1000));
4624 int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
4626 return wpa_s->conf->ap_scan == 2 ||
4627 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
4631 #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
4632 int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
4633 struct wpa_ssid *ssid,
4637 #ifdef IEEE8021X_EAPOL
4638 struct eap_peer_config *eap = &ssid->eap;
4640 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
4641 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
4642 (const u8 *) value, os_strlen(value));
4644 switch (wpa_supplicant_ctrl_req_from_string(field)) {
4645 case WPA_CTRL_REQ_EAP_IDENTITY:
4646 os_free(eap->identity);
4647 eap->identity = (u8 *) os_strdup(value);
4648 eap->identity_len = os_strlen(value);
4649 eap->pending_req_identity = 0;
4650 if (ssid == wpa_s->current_ssid)
4651 wpa_s->reassociate = 1;
4653 case WPA_CTRL_REQ_EAP_PASSWORD:
4654 bin_clear_free(eap->password, eap->password_len);
4655 eap->password = (u8 *) os_strdup(value);
4656 eap->password_len = os_strlen(value);
4657 eap->pending_req_password = 0;
4658 if (ssid == wpa_s->current_ssid)
4659 wpa_s->reassociate = 1;
4661 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
4662 bin_clear_free(eap->new_password, eap->new_password_len);
4663 eap->new_password = (u8 *) os_strdup(value);
4664 eap->new_password_len = os_strlen(value);
4665 eap->pending_req_new_password = 0;
4666 if (ssid == wpa_s->current_ssid)
4667 wpa_s->reassociate = 1;
4669 case WPA_CTRL_REQ_EAP_PIN:
4670 str_clear_free(eap->pin);
4671 eap->pin = os_strdup(value);
4672 eap->pending_req_pin = 0;
4673 if (ssid == wpa_s->current_ssid)
4674 wpa_s->reassociate = 1;
4676 case WPA_CTRL_REQ_EAP_OTP:
4677 bin_clear_free(eap->otp, eap->otp_len);
4678 eap->otp = (u8 *) os_strdup(value);
4679 eap->otp_len = os_strlen(value);
4680 os_free(eap->pending_req_otp);
4681 eap->pending_req_otp = NULL;
4682 eap->pending_req_otp_len = 0;
4684 case WPA_CTRL_REQ_EAP_PASSPHRASE:
4685 str_clear_free(eap->private_key_passwd);
4686 eap->private_key_passwd = os_strdup(value);
4687 eap->pending_req_passphrase = 0;
4688 if (ssid == wpa_s->current_ssid)
4689 wpa_s->reassociate = 1;
4691 case WPA_CTRL_REQ_SIM:
4692 str_clear_free(eap->external_sim_resp);
4693 eap->external_sim_resp = os_strdup(value);
4696 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
4701 #else /* IEEE8021X_EAPOL */
4702 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
4704 #endif /* IEEE8021X_EAPOL */
4706 #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
4709 int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
4712 unsigned int drv_enc;
4720 if (wpa_s && wpa_s->drv_capa_known)
4721 drv_enc = wpa_s->drv_enc;
4723 drv_enc = (unsigned int) -1;
4725 for (i = 0; i < NUM_WEP_KEYS; i++) {
4726 size_t len = ssid->wep_key_len[i];
4729 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
4731 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
4733 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
4735 return 1; /* invalid WEP key */
4738 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
4739 (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk)
4746 int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
4748 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
4750 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
4756 void wpas_auth_failed(struct wpa_supplicant *wpa_s, char *reason)
4758 struct wpa_ssid *ssid = wpa_s->current_ssid;
4760 struct os_reltime now;
4763 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
4768 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
4771 ssid->auth_failures++;
4774 if (ssid->p2p_group &&
4775 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
4777 * Skip the wait time since there is a short timeout on the
4778 * connection to a P2P group.
4782 #endif /* CONFIG_P2P */
4784 if (ssid->auth_failures > 50)
4786 else if (ssid->auth_failures > 10)
4788 else if (ssid->auth_failures > 5)
4790 else if (ssid->auth_failures > 3)
4792 else if (ssid->auth_failures > 2)
4794 else if (ssid->auth_failures > 1)
4799 if (ssid->auth_failures > 1 &&
4800 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
4801 dur += os_random() % (ssid->auth_failures * 10);
4803 os_get_reltime(&now);
4804 if (now.sec + dur <= ssid->disabled_until.sec)
4807 ssid->disabled_until.sec = now.sec + dur;
4809 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
4810 "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
4811 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
4812 ssid->auth_failures, dur, reason);
4816 void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
4817 struct wpa_ssid *ssid, int clear_failures)
4822 if (ssid->disabled_until.sec) {
4823 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
4824 "id=%d ssid=\"%s\"",
4825 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4827 ssid->disabled_until.sec = 0;
4828 ssid->disabled_until.usec = 0;
4830 ssid->auth_failures = 0;
4834 int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
4838 if (wpa_s->disallow_aps_bssid == NULL)
4841 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
4842 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
4843 bssid, ETH_ALEN) == 0)
4851 int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
4856 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
4859 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
4860 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
4861 if (ssid_len == s->ssid_len &&
4862 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4871 * wpas_request_connection - Request a new connection
4872 * @wpa_s: Pointer to the network interface
4874 * This function is used to request a new connection to be found. It will mark
4875 * the interface to allow reassociation and request a new scan to find a
4876 * suitable network to connect to.
4878 void wpas_request_connection(struct wpa_supplicant *wpa_s)
4880 wpa_s->normal_scans = 0;
4881 wpa_s->scan_req = NORMAL_SCAN_REQ;
4882 wpa_supplicant_reinit_autoscan(wpa_s);
4883 wpa_s->extra_blacklist_count = 0;
4884 wpa_s->disconnected = 0;
4885 wpa_s->reassociate = 1;
4887 if (wpa_supplicant_fast_associate(wpa_s) != 1)
4888 wpa_supplicant_req_scan(wpa_s, 0, 0);
4892 void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
4893 struct wpa_used_freq_data *freqs_data,
4898 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
4900 for (i = 0; i < len; i++) {
4901 struct wpa_used_freq_data *cur = &freqs_data[i];
4902 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
4903 i, cur->freq, cur->flags);
4909 * Find the operating frequencies of any of the virtual interfaces that
4910 * are using the same radio as the current interface, and in addition, get
4911 * information about the interface types that are using the frequency.
4913 int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
4914 struct wpa_used_freq_data *freqs_data,
4917 struct wpa_supplicant *ifs;
4920 unsigned int idx = 0, i;
4922 wpa_dbg(wpa_s, MSG_DEBUG,
4923 "Determining shared radio frequencies (max len %u)", len);
4924 os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
4926 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
4931 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
4934 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
4935 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
4936 freq = ifs->current_ssid->frequency;
4937 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
4938 freq = ifs->assoc_freq;
4942 /* Hold only distinct freqs */
4943 for (i = 0; i < idx; i++)
4944 if (freqs_data[i].freq == freq)
4948 freqs_data[idx++].freq = freq;
4950 if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
4951 freqs_data[i].flags = ifs->current_ssid->p2p_group ?
4952 WPA_FREQ_USED_BY_P2P_CLIENT :
4953 WPA_FREQ_USED_BY_INFRA_STATION;
4957 dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
4963 * Find the operating frequencies of any of the virtual interfaces that
4964 * are using the same radio as the current interface.
4966 int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
4967 int *freq_array, unsigned int len)
4969 struct wpa_used_freq_data *freqs_data;
4972 os_memset(freq_array, 0, sizeof(int) * len);
4974 freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
4978 num = get_shared_radio_freqs_data(wpa_s, freqs_data, len);
4979 for (i = 0; i < num; i++)
4980 freq_array[i] = freqs_data[i].freq;
4982 os_free(freqs_data);
4988 static void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
4990 struct rrm_data *rrm = data;
4992 if (!rrm->notify_neighbor_rep) {
4993 wpa_printf(MSG_ERROR,
4994 "RRM: Unexpected neighbor report timeout");
4998 wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
4999 rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
5001 rrm->notify_neighbor_rep = NULL;
5002 rrm->neighbor_rep_cb_ctx = NULL;
5007 * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
5008 * @wpa_s: Pointer to wpa_supplicant
5010 void wpas_rrm_reset(struct wpa_supplicant *wpa_s)
5012 wpa_s->rrm.rrm_used = 0;
5014 eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
5016 if (wpa_s->rrm.notify_neighbor_rep)
5017 wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
5018 wpa_s->rrm.next_neighbor_rep_token = 1;
5023 * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
5024 * @wpa_s: Pointer to wpa_supplicant
5025 * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
5026 * @report_len: Length of neighbor report buffer
5028 void wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
5029 const u8 *report, size_t report_len)
5031 struct wpabuf *neighbor_rep;
5033 wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
5037 if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
5038 wpa_printf(MSG_DEBUG,
5039 "RRM: Discarding neighbor report with token %d (expected %d)",
5040 report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
5044 eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
5047 if (!wpa_s->rrm.notify_neighbor_rep) {
5048 wpa_printf(MSG_ERROR, "RRM: Unexpected neighbor report");
5052 /* skipping the first byte, which is only an id (dialog token) */
5053 neighbor_rep = wpabuf_alloc(report_len - 1);
5054 if (neighbor_rep == NULL)
5056 wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
5057 wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
5059 wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
5061 wpa_s->rrm.notify_neighbor_rep = NULL;
5062 wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
5067 * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
5068 * @wpa_s: Pointer to wpa_supplicant
5069 * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
5070 * is sent in the request.
5071 * @cb: Callback function to be called once the requested report arrives, or
5072 * timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
5073 * In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
5074 * the requester's responsibility to free it.
5075 * In the latter case NULL will be sent in 'neighbor_rep'.
5076 * @cb_ctx: Context value to send the callback function
5077 * Returns: 0 in case of success, negative error code otherwise
5079 * In case there is a previous request which has not been answered yet, the
5080 * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
5081 * Request must contain a callback function.
5083 int wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
5084 const struct wpa_ssid *ssid,
5085 void (*cb)(void *ctx,
5086 struct wpabuf *neighbor_rep),
5092 if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
5093 wpa_printf(MSG_DEBUG, "RRM: No connection, no RRM.");
5097 if (!wpa_s->rrm.rrm_used) {
5098 wpa_printf(MSG_DEBUG, "RRM: No RRM in current connection.");
5102 rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
5103 WLAN_EID_RRM_ENABLED_CAPABILITIES);
5104 if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
5105 !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
5106 wpa_printf(MSG_DEBUG,
5107 "RRM: No network support for Neighbor Report.");
5112 wpa_printf(MSG_DEBUG,
5113 "RRM: Neighbor Report request must provide a callback.");
5117 /* Refuse if there's a live request */
5118 if (wpa_s->rrm.notify_neighbor_rep) {
5119 wpa_printf(MSG_DEBUG,
5120 "RRM: Currently handling previous Neighbor Report.");
5124 /* 3 = action category + action code + dialog token */
5125 buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0));
5127 wpa_printf(MSG_DEBUG,
5128 "RRM: Failed to allocate Neighbor Report Request");
5132 wpa_printf(MSG_DEBUG, "RRM: Neighbor report request (for %s), token=%d",
5133 (ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
5134 wpa_s->rrm.next_neighbor_rep_token);
5136 wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
5137 wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
5138 wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
5140 wpabuf_put_u8(buf, WLAN_EID_SSID);
5141 wpabuf_put_u8(buf, ssid->ssid_len);
5142 wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
5145 wpa_s->rrm.next_neighbor_rep_token++;
5147 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
5148 wpa_s->own_addr, wpa_s->bssid,
5149 wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
5150 wpa_printf(MSG_DEBUG,
5151 "RRM: Failed to send Neighbor Report Request");
5156 wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
5157 wpa_s->rrm.notify_neighbor_rep = cb;
5158 eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
5159 wpas_rrm_neighbor_rep_timeout_handler,
5167 void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
5169 const u8 *frame, size_t len,
5173 const struct rrm_link_measurement_request *req;
5174 struct rrm_link_measurement_report report;
5176 if (wpa_s->wpa_state != WPA_COMPLETED) {
5177 wpa_printf(MSG_INFO,
5178 "RRM: Ignoring link measurement request. Not associated");
5182 if (!wpa_s->rrm.rrm_used) {
5183 wpa_printf(MSG_INFO,
5184 "RRM: Ignoring link measurement request. Not RRM network");
5188 if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
5189 wpa_printf(MSG_INFO,
5190 "RRM: Measurement report failed. TX power insertion not supported");
5194 req = (const struct rrm_link_measurement_request *) frame;
5195 if (len < sizeof(*req)) {
5196 wpa_printf(MSG_INFO,
5197 "RRM: Link measurement report failed. Request too short");
5201 os_memset(&report, 0, sizeof(report));
5202 report.tpc.eid = WLAN_EID_TPC_REPORT;
5204 report.rsni = 255; /* 255 indicates that RSNI is not available */
5205 report.dialog_token = req->dialog_token;
5208 * It's possible to estimate RCPI based on RSSI in dBm. This
5209 * calculation will not reflect the correct value for high rates,
5210 * but it's good enough for Action frames which are transmitted
5211 * with up to 24 Mbps rates.
5214 report.rcpi = 255; /* not available */
5215 else if (rssi < -110)
5220 report.rcpi = (rssi + 110) * 2;
5222 /* action_category + action_code */
5223 buf = wpabuf_alloc(2 + sizeof(report));
5225 wpa_printf(MSG_ERROR,
5226 "RRM: Link measurement report failed. Buffer allocation failed");
5230 wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
5231 wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
5232 wpabuf_put_data(buf, &report, sizeof(report));
5233 wpa_hexdump(MSG_DEBUG, "RRM: Link measurement report:",
5234 wpabuf_head(buf), wpabuf_len(buf));
5236 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
5237 wpa_s->own_addr, wpa_s->bssid,
5238 wpabuf_head(buf), wpabuf_len(buf), 0)) {
5239 wpa_printf(MSG_ERROR,
5240 "RRM: Link measurement report failed. Send action failed");