Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * WPA Supplicant
      3  * Copyright (c) 2003-2009, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This program is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License version 2 as
      7  * published by the Free Software Foundation.
      8  *
      9  * Alternatively, this software may be distributed under the terms of BSD
     10  * license.
     11  *
     12  * See README and COPYING for more details.
     13  *
     14  * This file implements functions for registering and unregistering
     15  * %wpa_supplicant interfaces. In addition, this file contains number of
     16  * functions for managing network connections.
     17  */
     18 
     19 #include "includes.h"
     20 
     21 #include "common.h"
     22 #include "eapol_supp/eapol_supp_sm.h"
     23 #include "eap_peer/eap.h"
     24 #include "wpa.h"
     25 #include "eloop.h"
     26 #include "drivers/driver.h"
     27 #include "config.h"
     28 #include "l2_packet/l2_packet.h"
     29 #include "wpa_supplicant_i.h"
     30 #include "ctrl_iface.h"
     31 #include "ctrl_iface_dbus.h"
     32 #include "pcsc_funcs.h"
     33 #include "version.h"
     34 #include "preauth.h"
     35 #include "pmksa_cache.h"
     36 #include "wpa_ctrl.h"
     37 #include "mlme.h"
     38 #include "ieee802_11_defs.h"
     39 #include "blacklist.h"
     40 #include "wpas_glue.h"
     41 #include "wps_supplicant.h"
     42 #ifdef ANDROID
     43 #include <cutils/properties.h>
     44 #endif
     45 
     46 const char *wpa_supplicant_version =
     47 "wpa_supplicant v" VERSION_STR "\n"
     48 "Copyright (c) 2003-2009, Jouni Malinen <j (at) w1.fi> and contributors";
     49 
     50 const char *wpa_supplicant_license =
     51 "This program is free software. You can distribute it and/or modify it\n"
     52 "under the terms of the GNU General Public License version 2.\n"
     53 "\n"
     54 "Alternatively, this software may be distributed under the terms of the\n"
     55 "BSD license. See README and COPYING for more details.\n"
     56 #ifdef EAP_TLS_OPENSSL
     57 "\nThis product includes software developed by the OpenSSL Project\n"
     58 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
     59 #endif /* EAP_TLS_OPENSSL */
     60 ;
     61 
     62 #ifndef CONFIG_NO_STDOUT_DEBUG
     63 /* Long text divided into parts in order to fit in C89 strings size limits. */
     64 const char *wpa_supplicant_full_license1 =
     65 "This program is free software; you can redistribute it and/or modify\n"
     66 "it under the terms of the GNU General Public License version 2 as\n"
     67 "published by the Free Software Foundation.\n"
     68 "\n"
     69 "This program is distributed in the hope that it will be useful,\n"
     70 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
     71 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
     72 "GNU General Public License for more details.\n"
     73 "\n";
     74 const char *wpa_supplicant_full_license2 =
     75 "You should have received a copy of the GNU General Public License\n"
     76 "along with this program; if not, write to the Free Software\n"
     77 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n"
     78 "\n"
     79 "Alternatively, this software may be distributed under the terms of the\n"
     80 "BSD license.\n"
     81 "\n"
     82 "Redistribution and use in source and binary forms, with or without\n"
     83 "modification, are permitted provided that the following conditions are\n"
     84 "met:\n"
     85 "\n";
     86 const char *wpa_supplicant_full_license3 =
     87 "1. Redistributions of source code must retain the above copyright\n"
     88 "   notice, this list of conditions and the following disclaimer.\n"
     89 "\n"
     90 "2. Redistributions in binary form must reproduce the above copyright\n"
     91 "   notice, this list of conditions and the following disclaimer in the\n"
     92 "   documentation and/or other materials provided with the distribution.\n"
     93 "\n";
     94 const char *wpa_supplicant_full_license4 =
     95 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
     96 "   names of its contributors may be used to endorse or promote products\n"
     97 "   derived from this software without specific prior written permission.\n"
     98 "\n"
     99 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
    100 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
    101 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
    102 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
    103 const char *wpa_supplicant_full_license5 =
    104 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
    105 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
    106 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
    107 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
    108 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
    109 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
    110 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
    111 "\n";
    112 #endif /* CONFIG_NO_STDOUT_DEBUG */
    113 
    114 extern int wpa_debug_level;
    115 extern int wpa_debug_show_keys;
    116 extern int wpa_debug_timestamp;
    117 
    118 /* Configure default/group WEP keys for static WEP */
    119 static int wpa_set_wep_keys(struct wpa_supplicant *wpa_s,
    120 			    struct wpa_ssid *ssid)
    121 {
    122 	int i, set = 0;
    123 
    124 	for (i = 0; i < NUM_WEP_KEYS; i++) {
    125 		if (ssid->wep_key_len[i] == 0)
    126 			continue;
    127 
    128 		set = 1;
    129 		wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
    130 				(u8 *) "\xff\xff\xff\xff\xff\xff",
    131 				i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
    132 				ssid->wep_key[i], ssid->wep_key_len[i]);
    133 	}
    134 
    135 	return set;
    136 }
    137 
    138 
    139 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
    140 					   struct wpa_ssid *ssid)
    141 {
    142 	u8 key[32];
    143 	size_t keylen;
    144 	wpa_alg alg;
    145 	u8 seq[6] = { 0 };
    146 
    147 	/* IBSS/WPA-None uses only one key (Group) for both receiving and
    148 	 * sending unicast and multicast packets. */
    149 
    150 	if (ssid->mode != IEEE80211_MODE_IBSS) {
    151 		wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
    152 			   "for WPA-None", ssid->mode);
    153 		return -1;
    154 	}
    155 
    156 	if (!ssid->psk_set) {
    157 		wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
    158 		return -1;
    159 	}
    160 
    161 	switch (wpa_s->group_cipher) {
    162 	case WPA_CIPHER_CCMP:
    163 		os_memcpy(key, ssid->psk, 16);
    164 		keylen = 16;
    165 		alg = WPA_ALG_CCMP;
    166 		break;
    167 	case WPA_CIPHER_TKIP:
    168 		/* WPA-None uses the same Michael MIC key for both TX and RX */
    169 		os_memcpy(key, ssid->psk, 16 + 8);
    170 		os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
    171 		keylen = 32;
    172 		alg = WPA_ALG_TKIP;
    173 		break;
    174 	default:
    175 		wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
    176 			   "WPA-None", wpa_s->group_cipher);
    177 		return -1;
    178 	}
    179 
    180 	/* TODO: should actually remember the previously used seq#, both for TX
    181 	 * and RX from each STA.. */
    182 
    183 	return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
    184 			       0, 1, seq, 6, key, keylen);
    185 }
    186 
    187 
    188 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
    189 {
    190 	struct wpa_supplicant *wpa_s = eloop_ctx;
    191 	const u8 *bssid = wpa_s->bssid;
    192 	if (is_zero_ether_addr(bssid))
    193 		bssid = wpa_s->pending_bssid;
    194 	wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
    195 		MAC2STR(bssid));
    196 	wpa_blacklist_add(wpa_s, bssid);
    197 	wpa_sm_notify_disassoc(wpa_s->wpa);
    198 	wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
    199 	wpa_s->reassociate = 1;
    200 	wpa_supplicant_req_scan(wpa_s, 0, 0);
    201 }
    202 
    203 
    204 /**
    205  * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
    206  * @wpa_s: Pointer to wpa_supplicant data
    207  * @sec: Number of seconds after which to time out authentication
    208  * @usec: Number of microseconds after which to time out authentication
    209  *
    210  * This function is used to schedule a timeout for the current authentication
    211  * attempt.
    212  */
    213 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
    214 				     int sec, int usec)
    215 {
    216 	if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
    217 	    wpa_s->driver && IS_WIRED(wpa_s->driver))
    218 		return;
    219 
    220 	wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
    221 		"%d usec", sec, usec);
    222 	eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
    223 	eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
    224 }
    225 
    226 
    227 /**
    228  * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
    229  * @wpa_s: Pointer to wpa_supplicant data
    230  *
    231  * This function is used to cancel authentication timeout scheduled with
    232  * wpa_supplicant_req_auth_timeout() and it is called when authentication has
    233  * been completed.
    234  */
    235 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
    236 {
    237 	wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
    238 	eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
    239 	wpa_blacklist_del(wpa_s, wpa_s->bssid);
    240 }
    241 
    242 
    243 /**
    244  * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
    245  * @wpa_s: Pointer to wpa_supplicant data
    246  *
    247  * This function is used to configure EAPOL state machine based on the selected
    248  * authentication mode.
    249  */
    250 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
    251 {
    252 #ifdef IEEE8021X_EAPOL
    253 	struct eapol_config eapol_conf;
    254 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    255 
    256 	eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
    257 	eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
    258 
    259 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
    260 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
    261 		eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
    262 	else
    263 		eapol_sm_notify_portControl(wpa_s->eapol, Auto);
    264 
    265 	os_memset(&eapol_conf, 0, sizeof(eapol_conf));
    266 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
    267 		eapol_conf.accept_802_1x_keys = 1;
    268 		eapol_conf.required_keys = 0;
    269 		if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
    270 			eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
    271 		}
    272 		if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
    273 			eapol_conf.required_keys |=
    274 				EAPOL_REQUIRE_KEY_BROADCAST;
    275 		}
    276 
    277 		if (wpa_s->conf && wpa_s->driver && IS_WIRED(wpa_s->driver)) {
    278 			eapol_conf.required_keys = 0;
    279 		}
    280 	}
    281 	if (wpa_s->conf)
    282 		eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
    283 	eapol_conf.workaround = ssid->eap_workaround;
    284 	eapol_conf.eap_disabled =
    285 		!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
    286 		wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
    287 		wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
    288 	eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
    289 #endif /* IEEE8021X_EAPOL */
    290 }
    291 
    292 
    293 /**
    294  * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
    295  * @wpa_s: Pointer to wpa_supplicant data
    296  * @ssid: Configuration data for the network
    297  *
    298  * This function is used to configure WPA state machine and related parameters
    299  * to a mode where WPA is not enabled. This is called as part of the
    300  * authentication configuration when the selected network does not use WPA.
    301  */
    302 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
    303 				       struct wpa_ssid *ssid)
    304 {
    305 	int i;
    306 
    307 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
    308 		wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
    309 	else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
    310 		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
    311 	else
    312 		wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
    313 	wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
    314 	wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
    315 	wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
    316 	wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
    317 	wpa_s->group_cipher = WPA_CIPHER_NONE;
    318 	wpa_s->mgmt_group_cipher = 0;
    319 
    320 	for (i = 0; i < NUM_WEP_KEYS; i++) {
    321 		if (ssid->wep_key_len[i] > 5) {
    322 			wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
    323 			wpa_s->group_cipher = WPA_CIPHER_WEP104;
    324 			break;
    325 		} else if (ssid->wep_key_len[i] > 0) {
    326 			wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
    327 			wpa_s->group_cipher = WPA_CIPHER_WEP40;
    328 			break;
    329 		}
    330 	}
    331 
    332 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
    333 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
    334 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
    335 			 wpa_s->pairwise_cipher);
    336 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
    337 #ifdef CONFIG_IEEE80211W
    338 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
    339 			 wpa_s->mgmt_group_cipher);
    340 #endif /* CONFIG_IEEE80211W */
    341 
    342 	pmksa_cache_clear_current(wpa_s->wpa);
    343 }
    344 
    345 
    346 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
    347 {
    348 	scard_deinit(wpa_s->scard);
    349 	wpa_s->scard = NULL;
    350 	wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
    351 	eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
    352 	l2_packet_deinit(wpa_s->l2);
    353 	wpa_s->l2 = NULL;
    354 	if (wpa_s->l2_br) {
    355 		l2_packet_deinit(wpa_s->l2_br);
    356 		wpa_s->l2_br = NULL;
    357 	}
    358 
    359 	if (wpa_s->ctrl_iface) {
    360 		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
    361 		wpa_s->ctrl_iface = NULL;
    362 	}
    363 	if (wpa_s->conf != NULL) {
    364 		wpa_config_free(wpa_s->conf);
    365 		wpa_s->conf = NULL;
    366 	}
    367 
    368 	os_free(wpa_s->confname);
    369 	wpa_s->confname = NULL;
    370 
    371 	wpa_sm_set_eapol(wpa_s->wpa, NULL);
    372 	eapol_sm_deinit(wpa_s->eapol);
    373 	wpa_s->eapol = NULL;
    374 
    375 	rsn_preauth_deinit(wpa_s->wpa);
    376 
    377 	pmksa_candidate_free(wpa_s->wpa);
    378 	wpa_sm_deinit(wpa_s->wpa);
    379 	wpa_s->wpa = NULL;
    380 	wpa_blacklist_clear(wpa_s);
    381 
    382 	wpa_scan_results_free(wpa_s->scan_res);
    383 	wpa_s->scan_res = NULL;
    384 
    385 	wpa_supplicant_cancel_scan(wpa_s);
    386 	wpa_supplicant_cancel_auth_timeout(wpa_s);
    387 
    388 	ieee80211_sta_deinit(wpa_s);
    389 
    390 	wpas_wps_deinit(wpa_s);
    391 
    392 	wpabuf_free(wpa_s->pending_eapol_rx);
    393 	wpa_s->pending_eapol_rx = NULL;
    394 }
    395 
    396 
    397 /**
    398  * wpa_clear_keys - Clear keys configured for the driver
    399  * @wpa_s: Pointer to wpa_supplicant data
    400  * @addr: Previously used BSSID or %NULL if not available
    401  *
    402  * This function clears the encryption keys that has been previously configured
    403  * for the driver.
    404  */
    405 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
    406 {
    407 	u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
    408 
    409 	if (wpa_s->keys_cleared) {
    410 		/* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
    411 		 * timing issues with keys being cleared just before new keys
    412 		 * are set or just after association or something similar. This
    413 		 * shows up in group key handshake failing often because of the
    414 		 * client not receiving the first encrypted packets correctly.
    415 		 * Skipping some of the extra key clearing steps seems to help
    416 		 * in completing group key handshake more reliably. */
    417 		wpa_printf(MSG_DEBUG, "No keys have been configured - "
    418 			   "skip key clearing");
    419 		return;
    420 	}
    421 
    422 	/* MLME-DELETEKEYS.request */
    423 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
    424 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
    425 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
    426 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
    427 #ifdef CONFIG_IEEE80211W
    428 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
    429 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
    430 #endif /* CONFIG_IEEE80211W */
    431 	if (addr) {
    432 		wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
    433 				0);
    434 		/* MLME-SETPROTECTION.request(None) */
    435 		wpa_drv_mlme_setprotection(
    436 			wpa_s, addr,
    437 			MLME_SETPROTECTION_PROTECT_TYPE_NONE,
    438 			MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
    439 	}
    440 	wpa_s->keys_cleared = 1;
    441 }
    442 
    443 
    444 /**
    445  * wpa_supplicant_state_txt - Get the connection state name as a text string
    446  * @state: State (wpa_state; WPA_*)
    447  * Returns: The state name as a printable text string
    448  */
    449 const char * wpa_supplicant_state_txt(int state)
    450 {
    451 	switch (state) {
    452 	case WPA_DISCONNECTED:
    453 		return "DISCONNECTED";
    454 	case WPA_INACTIVE:
    455 		return "INACTIVE";
    456 	case WPA_SCANNING:
    457 		return "SCANNING";
    458 	case WPA_ASSOCIATING:
    459 		return "ASSOCIATING";
    460 	case WPA_ASSOCIATED:
    461 		return "ASSOCIATED";
    462 	case WPA_4WAY_HANDSHAKE:
    463 		return "4WAY_HANDSHAKE";
    464 	case WPA_GROUP_HANDSHAKE:
    465 		return "GROUP_HANDSHAKE";
    466 	case WPA_COMPLETED:
    467 		return "COMPLETED";
    468 	default:
    469 		return "UNKNOWN";
    470 	}
    471 }
    472 
    473 
    474 /**
    475  * wpa_supplicant_set_state - Set current connection state
    476  * @wpa_s: Pointer to wpa_supplicant data
    477  * @state: The new connection state
    478  *
    479  * This function is called whenever the connection state changes, e.g.,
    480  * association is completed for WPA/WPA2 4-Way Handshake is started.
    481  */
    482 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
    483 {
    484 #ifdef ANDROID
    485 	int network_id = -1;
    486 	if (wpa_s && wpa_s->current_ssid) {
    487 		network_id = wpa_s->current_ssid->id;
    488 	}
    489 	wpa_states reported_state = state;
    490 	if (state == WPA_DISCONNECTED && wpa_s->disconnected) {
    491 		reported_state = WPA_IDLE;
    492 	}
    493 #endif
    494 	wpa_printf(MSG_DEBUG, "State: %s -> %s",
    495 		   wpa_supplicant_state_txt(wpa_s->wpa_state),
    496 		   wpa_supplicant_state_txt(state));
    497 
    498 	if (state != WPA_SCANNING)
    499 		wpa_supplicant_notify_scanning(wpa_s, 0);
    500 
    501 	wpa_supplicant_dbus_notify_state_change(wpa_s, state,
    502 						wpa_s->wpa_state);
    503 #ifdef ANDROID
    504 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE "id=%d state=%d BSSID=" MACSTR,
    505 		network_id, reported_state, MAC2STR(wpa_s->pending_bssid));
    506 #endif
    507 
    508 	if (state == WPA_COMPLETED && wpa_s->new_connection) {
    509 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
    510 		struct wpa_ssid *ssid = wpa_s->current_ssid;
    511 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
    512 			MACSTR " completed %s [id=%d id_str=%s]",
    513 			MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
    514 			"(reauth)" : "(auth)",
    515 			ssid ? ssid->id : -1,
    516 			ssid && ssid->id_str ? ssid->id_str : "");
    517 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
    518 		wpa_s->new_connection = 0;
    519 		wpa_s->reassociated_connection = 1;
    520 		wpa_drv_set_operstate(wpa_s, 1);
    521 	} else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
    522 		   state == WPA_ASSOCIATED) {
    523 		wpa_s->new_connection = 1;
    524 		wpa_drv_set_operstate(wpa_s, 0);
    525 	}
    526 	wpa_s->wpa_state = state;
    527 }
    528 
    529 
    530 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
    531 				     void *signal_ctx)
    532 {
    533 	struct wpa_global *global = eloop_ctx;
    534 	struct wpa_supplicant *wpa_s;
    535 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    536 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
    537 			"received", sig);
    538 	}
    539 	eloop_terminate();
    540 }
    541 
    542 
    543 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
    544 {
    545 	wpa_s->pairwise_cipher = 0;
    546 	wpa_s->group_cipher = 0;
    547 	wpa_s->mgmt_group_cipher = 0;
    548 	wpa_s->key_mgmt = 0;
    549 	wpa_s->wpa_state = WPA_DISCONNECTED;
    550 }
    551 
    552 
    553 /**
    554  * wpa_supplicant_reload_configuration - Reload configuration data
    555  * @wpa_s: Pointer to wpa_supplicant data
    556  * Returns: 0 on success or -1 if configuration parsing failed
    557  *
    558  * This function can be used to request that the configuration data is reloaded
    559  * (e.g., after configuration file change). This function is reloading
    560  * configuration only for one interface, so this may need to be called multiple
    561  * times if %wpa_supplicant is controlling multiple interfaces and all
    562  * interfaces need reconfiguration.
    563  */
    564 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
    565 {
    566 	struct wpa_config *conf;
    567 	int reconf_ctrl;
    568 	if (wpa_s->confname == NULL)
    569 		return -1;
    570 	conf = wpa_config_read(wpa_s->confname);
    571 	if (conf == NULL) {
    572 		wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
    573 			"file '%s' - exiting", wpa_s->confname);
    574 		return -1;
    575 	}
    576 
    577 	reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
    578 		|| (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
    579 		    os_strcmp(conf->ctrl_interface,
    580 			      wpa_s->conf->ctrl_interface) != 0);
    581 
    582 	if (reconf_ctrl && wpa_s->ctrl_iface) {
    583 		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
    584 		wpa_s->ctrl_iface = NULL;
    585 	}
    586 
    587 	eapol_sm_invalidate_cached_session(wpa_s->eapol);
    588 	wpa_s->current_ssid = NULL;
    589 	/*
    590 	 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
    591 	 * pkcs11_engine_path, pkcs11_module_path.
    592 	 */
    593 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
    594 		/*
    595 		 * Clear forced success to clear EAP state for next
    596 		 * authentication.
    597 		 */
    598 		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
    599 	}
    600 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
    601 	wpa_sm_set_config(wpa_s->wpa, NULL);
    602 	wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
    603 	rsn_preauth_deinit(wpa_s->wpa);
    604 	wpa_config_free(wpa_s->conf);
    605 	wpa_s->conf = conf;
    606 	if (reconf_ctrl)
    607 		wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
    608 
    609 	wpa_supplicant_clear_status(wpa_s);
    610 	wpa_s->reassociate = 1;
    611 #ifdef ANDROID
    612 	wpa_supplicant_req_scan(wpa_s, 2, 0);
    613 #else
    614 	wpa_supplicant_req_scan(wpa_s, 0, 0);
    615 #endif
    616 	wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
    617 	return 0;
    618 }
    619 
    620 
    621 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
    622 				    void *signal_ctx)
    623 {
    624 	struct wpa_global *global = eloop_ctx;
    625 	struct wpa_supplicant *wpa_s;
    626 	wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
    627 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    628 		if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
    629 			eloop_terminate();
    630 		}
    631 	}
    632 }
    633 
    634 
    635 static wpa_cipher cipher_suite2driver(int cipher)
    636 {
    637 	switch (cipher) {
    638 	case WPA_CIPHER_NONE:
    639 		return CIPHER_NONE;
    640 	case WPA_CIPHER_WEP40:
    641 		return CIPHER_WEP40;
    642 	case WPA_CIPHER_WEP104:
    643 		return CIPHER_WEP104;
    644 	case WPA_CIPHER_CCMP:
    645 		return CIPHER_CCMP;
    646 	case WPA_CIPHER_TKIP:
    647 	default:
    648 		return CIPHER_TKIP;
    649 	}
    650 }
    651 
    652 
    653 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
    654 {
    655 	switch (key_mgmt) {
    656 	case WPA_KEY_MGMT_NONE:
    657 		return KEY_MGMT_NONE;
    658 	case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
    659 		return KEY_MGMT_802_1X_NO_WPA;
    660 	case WPA_KEY_MGMT_IEEE8021X:
    661 		return KEY_MGMT_802_1X;
    662 	case WPA_KEY_MGMT_WPA_NONE:
    663 		return KEY_MGMT_WPA_NONE;
    664 	case WPA_KEY_MGMT_FT_IEEE8021X:
    665 		return KEY_MGMT_FT_802_1X;
    666 	case WPA_KEY_MGMT_FT_PSK:
    667 		return KEY_MGMT_FT_PSK;
    668 	case WPA_KEY_MGMT_IEEE8021X_SHA256:
    669 		return KEY_MGMT_802_1X_SHA256;
    670 	case WPA_KEY_MGMT_PSK_SHA256:
    671 		return KEY_MGMT_PSK_SHA256;
    672 	case WPA_KEY_MGMT_WPS:
    673 		return KEY_MGMT_WPS;
    674 	case WPA_KEY_MGMT_PSK:
    675 	default:
    676 		return KEY_MGMT_PSK;
    677 	}
    678 }
    679 
    680 
    681 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
    682 					 struct wpa_ssid *ssid,
    683 					 struct wpa_ie_data *ie)
    684 {
    685 	int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
    686 	if (ret) {
    687 		if (ret == -2) {
    688 			wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
    689 				"from association info");
    690 		}
    691 		return -1;
    692 	}
    693 
    694 	wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
    695 		   "suites");
    696 	if (!(ie->group_cipher & ssid->group_cipher)) {
    697 		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
    698 			"cipher 0x%x (mask 0x%x) - reject",
    699 			ie->group_cipher, ssid->group_cipher);
    700 		return -1;
    701 	}
    702 	if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
    703 		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
    704 			"cipher 0x%x (mask 0x%x) - reject",
    705 			ie->pairwise_cipher, ssid->pairwise_cipher);
    706 		return -1;
    707 	}
    708 	if (!(ie->key_mgmt & ssid->key_mgmt)) {
    709 		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
    710 			"management 0x%x (mask 0x%x) - reject",
    711 			ie->key_mgmt, ssid->key_mgmt);
    712 		return -1;
    713 	}
    714 
    715 #ifdef CONFIG_IEEE80211W
    716 	if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
    717 	    ssid->ieee80211w == IEEE80211W_REQUIRED) {
    718 		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
    719 			"that does not support management frame protection - "
    720 			"reject");
    721 		return -1;
    722 	}
    723 #endif /* CONFIG_IEEE80211W */
    724 
    725 	return 0;
    726 }
    727 
    728 
    729 /**
    730  * wpa_supplicant_set_suites - Set authentication and encryption parameters
    731  * @wpa_s: Pointer to wpa_supplicant data
    732  * @bss: Scan results for the selected BSS, or %NULL if not available
    733  * @ssid: Configuration data for the selected network
    734  * @wpa_ie: Buffer for the WPA/RSN IE
    735  * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
    736  * used buffer length in case the functions returns success.
    737  * Returns: 0 on success or -1 on failure
    738  *
    739  * This function is used to configure authentication and encryption parameters
    740  * based on the network configuration and scan result for the selected BSS (if
    741  * available).
    742  */
    743 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
    744 			      struct wpa_scan_res *bss,
    745 			      struct wpa_ssid *ssid,
    746 			      u8 *wpa_ie, size_t *wpa_ie_len)
    747 {
    748 	struct wpa_ie_data ie;
    749 	int sel, proto;
    750 	const u8 *bss_wpa, *bss_rsn;
    751 
    752 	if (bss) {
    753 		bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
    754 		bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
    755 	} else
    756 		bss_wpa = bss_rsn = NULL;
    757 
    758 	if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
    759 	    wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
    760 	    (ie.group_cipher & ssid->group_cipher) &&
    761 	    (ie.pairwise_cipher & ssid->pairwise_cipher) &&
    762 	    (ie.key_mgmt & ssid->key_mgmt)) {
    763 		wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
    764 		proto = WPA_PROTO_RSN;
    765 	} else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
    766 		   wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
    767 		   (ie.group_cipher & ssid->group_cipher) &&
    768 		   (ie.pairwise_cipher & ssid->pairwise_cipher) &&
    769 		   (ie.key_mgmt & ssid->key_mgmt)) {
    770 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
    771 		proto = WPA_PROTO_WPA;
    772 	} else if (bss) {
    773 		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
    774 		return -1;
    775 	} else {
    776 		if (ssid->proto & WPA_PROTO_RSN)
    777 			proto = WPA_PROTO_RSN;
    778 		else
    779 			proto = WPA_PROTO_WPA;
    780 		if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
    781 			os_memset(&ie, 0, sizeof(ie));
    782 			ie.group_cipher = ssid->group_cipher;
    783 			ie.pairwise_cipher = ssid->pairwise_cipher;
    784 			ie.key_mgmt = ssid->key_mgmt;
    785 #ifdef CONFIG_IEEE80211W
    786 			ie.mgmt_group_cipher =
    787 				ssid->ieee80211w != NO_IEEE80211W ?
    788 				WPA_CIPHER_AES_128_CMAC : 0;
    789 #endif /* CONFIG_IEEE80211W */
    790 			wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
    791 				   "on configuration");
    792 		} else
    793 			proto = ie.proto;
    794 	}
    795 
    796 	wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
    797 		   "pairwise %d key_mgmt %d proto %d",
    798 		   ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
    799 #ifdef CONFIG_IEEE80211W
    800 	if (ssid->ieee80211w) {
    801 		wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
    802 			   ie.mgmt_group_cipher);
    803 	}
    804 #endif /* CONFIG_IEEE80211W */
    805 
    806 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
    807 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
    808 			 !!(ssid->proto & WPA_PROTO_RSN));
    809 
    810 	if (bss || !wpa_s->ap_ies_from_associnfo) {
    811 		if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
    812 					 bss_wpa ? 2 + bss_wpa[1] : 0) ||
    813 		    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
    814 					 bss_rsn ? 2 + bss_rsn[1] : 0))
    815 			return -1;
    816 	}
    817 
    818 	sel = ie.group_cipher & ssid->group_cipher;
    819 	if (sel & WPA_CIPHER_CCMP) {
    820 		wpa_s->group_cipher = WPA_CIPHER_CCMP;
    821 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
    822 	} else if (sel & WPA_CIPHER_TKIP) {
    823 		wpa_s->group_cipher = WPA_CIPHER_TKIP;
    824 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
    825 	} else if (sel & WPA_CIPHER_WEP104) {
    826 		wpa_s->group_cipher = WPA_CIPHER_WEP104;
    827 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
    828 	} else if (sel & WPA_CIPHER_WEP40) {
    829 		wpa_s->group_cipher = WPA_CIPHER_WEP40;
    830 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
    831 	} else {
    832 		wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
    833 		return -1;
    834 	}
    835 
    836 	sel = ie.pairwise_cipher & ssid->pairwise_cipher;
    837 	if (sel & WPA_CIPHER_CCMP) {
    838 		wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
    839 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
    840 	} else if (sel & WPA_CIPHER_TKIP) {
    841 		wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
    842 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
    843 	} else if (sel & WPA_CIPHER_NONE) {
    844 		wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
    845 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
    846 	} else {
    847 		wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
    848 			   "cipher.");
    849 		return -1;
    850 	}
    851 
    852 	sel = ie.key_mgmt & ssid->key_mgmt;
    853 	if (0) {
    854 #ifdef CONFIG_IEEE80211R
    855 	} else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
    856 		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
    857 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
    858 	} else if (sel & WPA_KEY_MGMT_FT_PSK) {
    859 		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
    860 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
    861 #endif /* CONFIG_IEEE80211R */
    862 #ifdef CONFIG_IEEE80211W
    863 	} else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
    864 		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
    865 		wpa_msg(wpa_s, MSG_DEBUG,
    866 			"WPA: using KEY_MGMT 802.1X with SHA256");
    867 	} else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
    868 		wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
    869 		wpa_msg(wpa_s, MSG_DEBUG,
    870 			"WPA: using KEY_MGMT PSK with SHA256");
    871 #endif /* CONFIG_IEEE80211W */
    872 	} else if (sel & WPA_KEY_MGMT_IEEE8021X) {
    873 		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
    874 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
    875 	} else if (sel & WPA_KEY_MGMT_PSK) {
    876 		wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
    877 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
    878 	} else if (sel & WPA_KEY_MGMT_WPA_NONE) {
    879 		wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
    880 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
    881 	} else {
    882 		wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
    883 			   "key management type.");
    884 		return -1;
    885 	}
    886 
    887 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
    888 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
    889 			 wpa_s->pairwise_cipher);
    890 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
    891 
    892 #ifdef CONFIG_IEEE80211W
    893 	sel = ie.mgmt_group_cipher;
    894 	if (ssid->ieee80211w == NO_IEEE80211W ||
    895 	    !(ie.capabilities & WPA_CAPABILITY_MFPC))
    896 		sel = 0;
    897 	if (sel & WPA_CIPHER_AES_128_CMAC) {
    898 		wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
    899 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
    900 			"AES-128-CMAC");
    901 	} else {
    902 		wpa_s->mgmt_group_cipher = 0;
    903 		wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
    904 	}
    905 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
    906 			 wpa_s->mgmt_group_cipher);
    907 #endif /* CONFIG_IEEE80211W */
    908 
    909 	if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
    910 		wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
    911 		return -1;
    912 	}
    913 
    914 	if (ssid->key_mgmt &
    915 	    (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
    916 		wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
    917 	else
    918 		wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
    919 
    920 	return 0;
    921 }
    922 
    923 
    924 /**
    925  * wpa_supplicant_associate - Request association
    926  * @wpa_s: Pointer to wpa_supplicant data
    927  * @bss: Scan results for the selected BSS, or %NULL if not available
    928  * @ssid: Configuration data for the selected network
    929  *
    930  * This function is used to request %wpa_supplicant to associate with a BSS.
    931  */
    932 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
    933 			      struct wpa_scan_res *bss, struct wpa_ssid *ssid)
    934 {
    935 	u8 wpa_ie[80];
    936 	size_t wpa_ie_len;
    937 	int use_crypt, ret, i;
    938 	int algs = AUTH_ALG_OPEN_SYSTEM;
    939 	wpa_cipher cipher_pairwise, cipher_group;
    940 	struct wpa_driver_associate_params params;
    941 	int wep_keys_set = 0;
    942 	struct wpa_driver_capa capa;
    943 	int assoc_failed = 0;
    944 
    945 	wpa_s->reassociate = 0;
    946 	if (bss) {
    947 #ifdef CONFIG_IEEE80211R
    948 		const u8 *md = NULL;
    949 #endif /* CONFIG_IEEE80211R */
    950 		const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
    951 		wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
    952 			" (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
    953 			ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
    954 		os_memset(wpa_s->bssid, 0, ETH_ALEN);
    955 		os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
    956 		wpa_s->link_speed = wpa_scan_get_max_rate(bss) * 500000;
    957 		wpa_s->rssi = bss->level;
    958 #ifdef CONFIG_IEEE80211R
    959 		ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
    960 		if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
    961 			md = ie + 2;
    962 		wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
    963 		if (md) {
    964 			/* Prepare for the next transition */
    965 			wpa_ft_prepare_auth_request(wpa_s->wpa);
    966 		}
    967 #endif /* CONFIG_IEEE80211R */
    968 #ifdef CONFIG_WPS
    969 	} else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
    970 		   wpa_s->conf->ap_scan == 2 &&
    971 		   (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
    972 		/* Use ap_scan==1 style network selection to find the network
    973 		 */
    974 		wpa_s->scan_req = 2;
    975 		wpa_s->reassociate = 1;
    976 		wpa_supplicant_req_scan(wpa_s, 0, 0);
    977 		return;
    978 #endif /* CONFIG_WPS */
    979 	} else {
    980 		wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
    981 			wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
    982 		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
    983 	}
    984 	wpa_supplicant_cancel_scan(wpa_s);
    985 
    986 	/* Starting new association, so clear the possibly used WPA IE from the
    987 	 * previous association. */
    988 	wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
    989 
    990 	if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
    991 		wpa_printf(MSG_WARNING, "Failed to set operating mode");
    992 		assoc_failed = 1;
    993 	}
    994 
    995 #ifdef IEEE8021X_EAPOL
    996 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
    997 		if (ssid->leap) {
    998 			if (ssid->non_leap == 0)
    999 				algs = AUTH_ALG_LEAP;
   1000 			else
   1001 				algs |= AUTH_ALG_LEAP;
   1002 		}
   1003 	}
   1004 #endif /* IEEE8021X_EAPOL */
   1005 	wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
   1006 	if (ssid->auth_alg) {
   1007 		algs = 0;
   1008 		if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
   1009 			algs |= AUTH_ALG_OPEN_SYSTEM;
   1010 		if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
   1011 			algs |= AUTH_ALG_SHARED_KEY;
   1012 		if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
   1013 			algs |= AUTH_ALG_LEAP;
   1014 		wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
   1015 			   algs);
   1016 	}
   1017 	wpa_drv_set_auth_alg(wpa_s, algs);
   1018 
   1019 	if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
   1020 		    wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
   1021 	    (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
   1022 			       WPA_KEY_MGMT_FT_IEEE8021X |
   1023 			       WPA_KEY_MGMT_FT_PSK |
   1024 			       WPA_KEY_MGMT_IEEE8021X_SHA256 |
   1025 			       WPA_KEY_MGMT_PSK_SHA256))) {
   1026 		int try_opportunistic;
   1027 		try_opportunistic = ssid->proactive_key_caching &&
   1028 			(ssid->proto & WPA_PROTO_RSN);
   1029 		if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
   1030 					    wpa_s->current_ssid,
   1031 					    try_opportunistic) == 0)
   1032 			eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
   1033 		wpa_ie_len = sizeof(wpa_ie);
   1034 		if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
   1035 					      wpa_ie, &wpa_ie_len)) {
   1036 			wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
   1037 				   "management and encryption suites");
   1038 			return;
   1039 		}
   1040 	} else if (ssid->key_mgmt &
   1041 		   (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
   1042 		    WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
   1043 		    WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
   1044 		    WPA_KEY_MGMT_IEEE8021X_SHA256)) {
   1045 		wpa_ie_len = sizeof(wpa_ie);
   1046 		if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
   1047 					      wpa_ie, &wpa_ie_len)) {
   1048 			wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
   1049 				   "management and encryption suites (no scan "
   1050 				   "results)");
   1051 			return;
   1052 		}
   1053 #ifdef CONFIG_WPS
   1054 	} else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
   1055 		struct wpabuf *wps_ie;
   1056 		wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
   1057 		if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
   1058 			wpa_ie_len = wpabuf_len(wps_ie);
   1059 			os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
   1060 		} else
   1061 			wpa_ie_len = 0;
   1062 		wpabuf_free(wps_ie);
   1063 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
   1064 #endif /* CONFIG_WPS */
   1065 	} else {
   1066 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
   1067 		wpa_ie_len = 0;
   1068 	}
   1069 
   1070 	wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
   1071 	use_crypt = 1;
   1072 	cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
   1073 	cipher_group = cipher_suite2driver(wpa_s->group_cipher);
   1074 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
   1075 	    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
   1076 		if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
   1077 			use_crypt = 0;
   1078 		if (wpa_set_wep_keys(wpa_s, ssid)) {
   1079 			use_crypt = 1;
   1080 			wep_keys_set = 1;
   1081 		}
   1082 	}
   1083 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
   1084 		use_crypt = 0;
   1085 
   1086 #ifdef IEEE8021X_EAPOL
   1087 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
   1088 		if ((ssid->eapol_flags &
   1089 		     (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
   1090 		      EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
   1091 		    !wep_keys_set) {
   1092 			use_crypt = 0;
   1093 		} else {
   1094 			/* Assume that dynamic WEP-104 keys will be used and
   1095 			 * set cipher suites in order for drivers to expect
   1096 			 * encryption. */
   1097 			cipher_pairwise = cipher_group = CIPHER_WEP104;
   1098 		}
   1099 	}
   1100 #endif /* IEEE8021X_EAPOL */
   1101 
   1102 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
   1103 		/* Set the key before (and later after) association */
   1104 		wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
   1105 	}
   1106 
   1107 	wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
   1108 	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
   1109 	os_memset(&params, 0, sizeof(params));
   1110 	if (bss) {
   1111 		const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
   1112 		params.bssid = bss->bssid;
   1113 		params.ssid = ie ? ie + 2 : (u8 *) "";
   1114 		params.ssid_len = ie ? ie[1] : 0;
   1115 		params.freq = bss->freq;
   1116 	} else {
   1117 		params.ssid = ssid->ssid;
   1118 		params.ssid_len = ssid->ssid_len;
   1119 	}
   1120 	if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
   1121 		params.freq = ssid->frequency; /* Initial channel for IBSS */
   1122 	params.wpa_ie = wpa_ie;
   1123 	params.wpa_ie_len = wpa_ie_len;
   1124 	params.pairwise_suite = cipher_pairwise;
   1125 	params.group_suite = cipher_group;
   1126 	params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
   1127 	params.auth_alg = algs;
   1128 	params.mode = ssid->mode;
   1129 	for (i = 0; i < NUM_WEP_KEYS; i++) {
   1130 		if (ssid->wep_key_len[i])
   1131 			params.wep_key[i] = ssid->wep_key[i];
   1132 		params.wep_key_len[i] = ssid->wep_key_len[i];
   1133 	}
   1134 	params.wep_tx_keyidx = ssid->wep_tx_keyidx;
   1135 
   1136 	if (wpa_s->driver_4way_handshake &&
   1137 	    (params.key_mgmt_suite == KEY_MGMT_PSK ||
   1138 	     params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
   1139 		params.passphrase = ssid->passphrase;
   1140 		if (ssid->psk_set)
   1141 			params.psk = ssid->psk;
   1142 	}
   1143 
   1144 #ifdef CONFIG_IEEE80211W
   1145 	switch (ssid->ieee80211w) {
   1146 	case NO_IEEE80211W:
   1147 		params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
   1148 		break;
   1149 	case IEEE80211W_OPTIONAL:
   1150 		params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
   1151 		break;
   1152 	case IEEE80211W_REQUIRED:
   1153 		params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
   1154 		break;
   1155 	}
   1156 	if (ssid->ieee80211w != NO_IEEE80211W && bss) {
   1157 		const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
   1158 		struct wpa_ie_data ie;
   1159 		if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
   1160 		    ie.capabilities &
   1161 		    (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
   1162 			wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
   1163 				   "require MFP");
   1164 			params.mgmt_frame_protection =
   1165 				MGMT_FRAME_PROTECTION_REQUIRED;
   1166 		}
   1167 	}
   1168 #endif /* CONFIG_IEEE80211W */
   1169 
   1170 	if (wpa_s->use_client_mlme)
   1171 		ret = ieee80211_sta_associate(wpa_s, &params);
   1172 	else
   1173 		ret = wpa_drv_associate(wpa_s, &params);
   1174 	if (ret < 0) {
   1175 		wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
   1176 			"failed");
   1177 		/* try to continue anyway; new association will be tried again
   1178 		 * after timeout */
   1179 		assoc_failed = 1;
   1180 	}
   1181 
   1182 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
   1183 		/* Set the key after the association just in case association
   1184 		 * cleared the previously configured key. */
   1185 		wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
   1186 		/* No need to timeout authentication since there is no key
   1187 		 * management. */
   1188 		wpa_supplicant_cancel_auth_timeout(wpa_s);
   1189 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
   1190 	} else {
   1191 		/* Timeout for IEEE 802.11 authentication and association */
   1192 		int timeout = 60;
   1193 
   1194 		if (assoc_failed) {
   1195 			/* give IBSS a bit more time */
   1196  			timeout = ssid->mode ? 10 : 5;
   1197 		} else if (wpa_s->conf->ap_scan == 1) {
   1198 			/* give IBSS a bit more time */
   1199  			timeout = ssid->mode ? 20 : 10;
   1200 		}
   1201 		wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
   1202 	}
   1203 
   1204 	if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
   1205 	    capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
   1206 		/* Set static WEP keys again */
   1207 		wpa_set_wep_keys(wpa_s, ssid);
   1208 	}
   1209 
   1210 	if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
   1211 		/*
   1212 		 * Do not allow EAP session resumption between different
   1213 		 * network configurations.
   1214 		 */
   1215 		eapol_sm_invalidate_cached_session(wpa_s->eapol);
   1216 	}
   1217 	wpa_s->current_ssid = ssid;
   1218 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
   1219 	wpa_supplicant_initiate_eapol(wpa_s);
   1220 }
   1221 
   1222 
   1223 /**
   1224  * wpa_supplicant_disassociate - Disassociate the current connection
   1225  * @wpa_s: Pointer to wpa_supplicant data
   1226  * @reason_code: IEEE 802.11 reason code for the disassociate frame
   1227  *
   1228  * This function is used to request %wpa_supplicant to disassociate with the
   1229  * current AP.
   1230  */
   1231 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
   1232 				 int reason_code)
   1233 {
   1234 	u8 *addr = NULL;
   1235 	if (!is_zero_ether_addr(wpa_s->bssid)) {
   1236 		if (wpa_s->use_client_mlme)
   1237 			ieee80211_sta_disassociate(wpa_s, reason_code);
   1238 		else
   1239 			wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
   1240 		addr = wpa_s->bssid;
   1241 	}
   1242 	wpa_clear_keys(wpa_s, addr);
   1243 	wpa_supplicant_mark_disassoc(wpa_s);
   1244 	wpa_s->current_ssid = NULL;
   1245 	wpa_sm_set_config(wpa_s->wpa, NULL);
   1246 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
   1247 }
   1248 
   1249 
   1250 /**
   1251  * wpa_supplicant_deauthenticate - Deauthenticate the current connection
   1252  * @wpa_s: Pointer to wpa_supplicant data
   1253  * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
   1254  *
   1255  * This function is used to request %wpa_supplicant to deauthenticate from the
   1256  * current AP.
   1257  */
   1258 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
   1259 				   int reason_code)
   1260 {
   1261 	u8 *addr = NULL;
   1262 	if (!is_zero_ether_addr(wpa_s->bssid)) {
   1263 		if (wpa_s->use_client_mlme)
   1264 			ieee80211_sta_deauthenticate(wpa_s, reason_code);
   1265 		else
   1266 			wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
   1267 					       reason_code);
   1268 		addr = wpa_s->bssid;
   1269 	}
   1270 	wpa_clear_keys(wpa_s, addr);
   1271 	wpa_supplicant_mark_disassoc(wpa_s);
   1272 	wpa_s->current_ssid = NULL;
   1273 	wpa_sm_set_config(wpa_s->wpa, NULL);
   1274 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
   1275 }
   1276 
   1277 
   1278 static int wpa_supplicant_get_scan_results_old(struct wpa_supplicant *wpa_s)
   1279 {
   1280 #define SCAN_AP_LIMIT 128
   1281 	struct wpa_scan_result *results;
   1282 	int num, i;
   1283 	struct wpa_scan_results *res;
   1284 
   1285 	results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
   1286 	if (results == NULL) {
   1287 		wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
   1288 			   "results");
   1289 		return -1;
   1290 	}
   1291 
   1292 	num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
   1293 	wpa_printf(MSG_DEBUG, "Scan results: %d", num);
   1294 	if (num < 0) {
   1295 		wpa_printf(MSG_DEBUG, "Failed to get scan results");
   1296 		os_free(results);
   1297 		return -1;
   1298 	}
   1299 	if (num > SCAN_AP_LIMIT) {
   1300 		wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
   1301 			   num, SCAN_AP_LIMIT);
   1302 		num = SCAN_AP_LIMIT;
   1303 	}
   1304 
   1305 	wpa_scan_results_free(wpa_s->scan_res);
   1306 	wpa_s->scan_res = NULL;
   1307 
   1308 	/* Convert old scan result data structure to the new one */
   1309 	res = os_zalloc(sizeof(*res));
   1310 	if (res == NULL) {
   1311 		os_free(results);
   1312 		return -1;
   1313 	}
   1314 	res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
   1315 	if (res->res == NULL) {
   1316 		os_free(results);
   1317 		os_free(res);
   1318 		return -1;
   1319 	}
   1320 
   1321 	for (i = 0; i < num; i++) {
   1322 		struct wpa_scan_result *bss = &results[i];
   1323 		struct wpa_scan_res *r;
   1324 		size_t ie_len;
   1325 		u8 *pos;
   1326 
   1327 		ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
   1328 		if (bss->maxrate)
   1329 			ie_len += 3;
   1330 		if (bss->mdie_present)
   1331 			ie_len += 5;
   1332 
   1333 		r = os_zalloc(sizeof(*r) + ie_len);
   1334 		if (r == NULL)
   1335 			break;
   1336 
   1337 		os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
   1338 		r->freq = bss->freq;
   1339 		r->caps = bss->caps;
   1340 		r->qual = bss->qual;
   1341 		r->noise = bss->noise;
   1342 		r->level = bss->level;
   1343 		r->tsf = bss->tsf;
   1344 		r->ie_len = ie_len;
   1345 
   1346 		pos = (u8 *) (r + 1);
   1347 
   1348 		/* SSID IE */
   1349 		*pos++ = WLAN_EID_SSID;
   1350 		*pos++ = bss->ssid_len;
   1351 		os_memcpy(pos, bss->ssid, bss->ssid_len);
   1352 		pos += bss->ssid_len;
   1353 
   1354 		if (bss->maxrate) {
   1355 			/* Fake Supported Rate IE to include max rate */
   1356 			*pos++ = WLAN_EID_SUPP_RATES;
   1357 			*pos++ = 1;
   1358 			*pos++ = bss->maxrate;
   1359 		}
   1360 
   1361 		if (bss->rsn_ie_len) {
   1362 			os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
   1363 			pos += bss->rsn_ie_len;
   1364 		}
   1365 
   1366 		if (bss->mdie_present) {
   1367 			os_memcpy(pos, bss->mdie, 5);
   1368 			pos += 5;
   1369 		}
   1370 
   1371 		if (bss->wpa_ie_len) {
   1372 			os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
   1373 			pos += bss->wpa_ie_len;
   1374 		}
   1375 
   1376 		res->res[res->num++] = r;
   1377 	}
   1378 
   1379 	os_free(results);
   1380 	wpa_s->scan_res = res;
   1381 
   1382 	return 0;
   1383 }
   1384 
   1385 
   1386 /**
   1387  * wpa_supplicant_get_scan_results - Get scan results
   1388  * @wpa_s: Pointer to wpa_supplicant data
   1389  * Returns: 0 on success, -1 on failure
   1390  *
   1391  * This function is request the current scan results from the driver and stores
   1392  * a local copy of the results in wpa_s->scan_res.
   1393  */
   1394 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
   1395 {
   1396 	int ret;
   1397 
   1398 	if (wpa_s->use_client_mlme) {
   1399 		wpa_scan_results_free(wpa_s->scan_res);
   1400 		wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
   1401 		if (wpa_s->scan_res == NULL) {
   1402 			wpa_printf(MSG_DEBUG, "Failed to get scan results");
   1403 			ret = -1;
   1404 		} else
   1405 			ret = 0;
   1406 	} else if (wpa_s->driver->get_scan_results2 == NULL)
   1407 		ret = wpa_supplicant_get_scan_results_old(wpa_s);
   1408 	else {
   1409 		wpa_scan_results_free(wpa_s->scan_res);
   1410 		wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
   1411 		if (wpa_s->scan_res == NULL) {
   1412 			wpa_printf(MSG_DEBUG, "Failed to get scan results");
   1413 			ret = -1;
   1414 		} else
   1415 			ret = 0;
   1416 	}
   1417 
   1418 	if (wpa_s->scan_res)
   1419 		wpa_scan_sort_results(wpa_s->scan_res);
   1420 
   1421 	return ret;
   1422 }
   1423 
   1424 
   1425 /**
   1426  * wpa_supplicant_get_ssid - Get a pointer to the current network structure
   1427  * @wpa_s: Pointer to wpa_supplicant data
   1428  * Returns: A pointer to the current network structure or %NULL on failure
   1429  */
   1430 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
   1431 {
   1432 	struct wpa_ssid *entry;
   1433 	u8 ssid[MAX_SSID_LEN];
   1434 	int res;
   1435 	size_t ssid_len;
   1436 	u8 bssid[ETH_ALEN];
   1437 	int wired;
   1438 
   1439 	if (wpa_s->use_client_mlme) {
   1440 		if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
   1441 			wpa_printf(MSG_WARNING, "Could not read SSID from "
   1442 				   "MLME.");
   1443 			return NULL;
   1444 		}
   1445 	} else {
   1446 		res = wpa_drv_get_ssid(wpa_s, ssid);
   1447 		if (res < 0) {
   1448 			wpa_printf(MSG_WARNING, "Could not read SSID from "
   1449 				   "driver.");
   1450 			return NULL;
   1451 		}
   1452 		ssid_len = res;
   1453 	}
   1454 
   1455 	if (wpa_s->use_client_mlme)
   1456 		os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
   1457 	else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
   1458 		wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
   1459 		return NULL;
   1460 	}
   1461 
   1462 	wired = wpa_s->conf->ap_scan == 0 && wpa_s->driver &&
   1463 		IS_WIRED(wpa_s->driver);
   1464 
   1465 	entry = wpa_s->conf->ssid;
   1466 	while (entry) {
   1467 		if (!entry->disabled &&
   1468 		    ((ssid_len == entry->ssid_len &&
   1469 		      os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
   1470 		    (!entry->bssid_set ||
   1471 		     os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
   1472 			return entry;
   1473 #ifdef CONFIG_WPS
   1474 		if (!entry->disabled &&
   1475 		    (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
   1476 		    (entry->ssid == NULL || entry->ssid_len == 0) &&
   1477 		    (!entry->bssid_set ||
   1478 		     os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
   1479 			return entry;
   1480 #endif /* CONFIG_WPS */
   1481 		entry = entry->next;
   1482 	}
   1483 
   1484 	return NULL;
   1485 }
   1486 
   1487 int wpa_drv_scan(struct wpa_supplicant *wpa_s, struct wpa_ssid **ssid_ptr)
   1488 {
   1489 	u8 *ssid_nm = NULL;
   1490 	size_t ssid_len = 0;
   1491 	int ret = -1;
   1492 
   1493 	if (wpa_s->driver->combo_scan) {
   1494 		ret = wpa_s->driver->combo_scan(wpa_s->drv_priv, ssid_ptr,
   1495 						wpa_s->conf->ssid);
   1496 		if (!ret) {
   1497 			wpa_s->prev_scan_ssid = (*ssid_ptr) ?
   1498 					(*ssid_ptr) : BROADCAST_SSID_SCAN;
   1499 		}
   1500 	}
   1501 	else if (wpa_s->driver->scan) {
   1502 		if (*ssid_ptr) {
   1503 			ssid_nm = (*ssid_ptr)->ssid;
   1504 			ssid_len = (*ssid_ptr)->ssid_len;
   1505 		}
   1506 
   1507 		ret = wpa_s->driver->scan(wpa_s->drv_priv, ssid_nm, ssid_len);
   1508 	}
   1509 	return ret;
   1510 }
   1511 
   1512 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
   1513 				     const char *name)
   1514 {
   1515 	int i;
   1516 
   1517 	if (wpa_s == NULL)
   1518 		return -1;
   1519 
   1520 	if (wpa_supplicant_drivers[0] == NULL) {
   1521 		wpa_printf(MSG_ERROR, "No driver interfaces build into "
   1522 			   "wpa_supplicant.");
   1523 		return -1;
   1524 	}
   1525 
   1526 	if (name == NULL) {
   1527 		/* default to first driver in the list */
   1528 		wpa_s->driver = wpa_supplicant_drivers[0];
   1529 		wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
   1530 		return 0;
   1531 	}
   1532 
   1533 	for (i = 0; wpa_supplicant_drivers[i]; i++) {
   1534 		if (os_strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {
   1535 			wpa_s->driver = wpa_supplicant_drivers[i];
   1536 			wpa_s->global_drv_priv = wpa_s->global->drv_priv[i];
   1537 			return 0;
   1538 		}
   1539 	}
   1540 
   1541 	wpa_printf(MSG_ERROR, "Unsupported driver '%s'.\n", name);
   1542 	return -1;
   1543 }
   1544 
   1545 
   1546 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
   1547 			     const u8 *buf, size_t len)
   1548 {
   1549 	struct wpa_supplicant *wpa_s = ctx;
   1550 
   1551 	wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
   1552 	wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
   1553 
   1554 	if (wpa_s->wpa_state < WPA_ASSOCIATED) {
   1555 		/*
   1556 		 * There is possible race condition between receiving the
   1557 		 * association event and the EAPOL frame since they are coming
   1558 		 * through different paths from the driver. In order to avoid
   1559 		 * issues in trying to process the EAPOL frame before receiving
   1560 		 * association information, lets queue it for processing until
   1561 		 * the association event is received.
   1562 		 */
   1563 		wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
   1564 			   "received EAPOL frame");
   1565 		wpabuf_free(wpa_s->pending_eapol_rx);
   1566 		wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
   1567 		if (wpa_s->pending_eapol_rx) {
   1568 			os_get_time(&wpa_s->pending_eapol_rx_time);
   1569 			os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
   1570 				  ETH_ALEN);
   1571 		}
   1572 		return;
   1573 	}
   1574 
   1575 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
   1576 		wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
   1577 			   "no key management is configured");
   1578 		return;
   1579 	}
   1580 
   1581 	if (wpa_s->eapol_received == 0 &&
   1582 	    (!wpa_s->driver_4way_handshake ||
   1583 	     !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
   1584 	     wpa_s->wpa_state != WPA_COMPLETED)) {
   1585 		/* Timeout for completing IEEE 802.1X and WPA authentication */
   1586 		wpa_supplicant_req_auth_timeout(
   1587 			wpa_s,
   1588 			(wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
   1589 			 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
   1590 			 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
   1591 			70 : 10, 0);
   1592 	}
   1593 	wpa_s->eapol_received++;
   1594 
   1595 	if (wpa_s->countermeasures) {
   1596 		wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
   1597 			   "packet");
   1598 		return;
   1599 	}
   1600 
   1601 	/* Source address of the incoming EAPOL frame could be compared to the
   1602 	 * current BSSID. However, it is possible that a centralized
   1603 	 * Authenticator could be using another MAC address than the BSSID of
   1604 	 * an AP, so just allow any address to be used for now. The replies are
   1605 	 * still sent to the current BSSID (if available), though. */
   1606 
   1607 	os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
   1608 	if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
   1609 	    eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
   1610 		return;
   1611 	wpa_drv_poll(wpa_s);
   1612 	if (!wpa_s->driver_4way_handshake)
   1613 		wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
   1614 	else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
   1615 		/*
   1616 		 * Set portValid = TRUE here since we are going to skip 4-way
   1617 		 * handshake processing which would normally set portValid. We
   1618 		 * need this to allow the EAPOL state machines to be completed
   1619 		 * without going through EAPOL-Key handshake.
   1620 		 */
   1621 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
   1622 	}
   1623 }
   1624 
   1625 
   1626 void wpa_supplicant_sta_free_hw_features(struct wpa_hw_modes *hw_features,
   1627 					 size_t num_hw_features)
   1628 {
   1629 	ieee80211_sta_free_hw_features(hw_features, num_hw_features);
   1630 }
   1631 
   1632 
   1633 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
   1634 			   struct ieee80211_rx_status *rx_status)
   1635 {
   1636 	struct wpa_supplicant *wpa_s = ctx;
   1637 	ieee80211_sta_rx(wpa_s, buf, len, rx_status);
   1638 }
   1639 
   1640 
   1641 /**
   1642  * wpa_supplicant_driver_init - Initialize driver interface parameters
   1643  * @wpa_s: Pointer to wpa_supplicant data
   1644  * Returns: 0 on success, -1 on failure
   1645  *
   1646  * This function is called to initialize driver interface parameters.
   1647  * wpa_drv_init() must have been called before this function to initialize the
   1648  * driver interface.
   1649  */
   1650 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
   1651 {
   1652 	static int interface_count = 0;
   1653 
   1654 	if (wpa_s->driver->send_eapol) {
   1655 		const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
   1656 		if (addr)
   1657 			os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
   1658 	} else {
   1659 		wpa_s->l2 = l2_packet_init(wpa_s->ifname,
   1660 					   wpa_drv_get_mac_addr(wpa_s),
   1661 					   ETH_P_EAPOL,
   1662 					   wpa_supplicant_rx_eapol, wpa_s, 0);
   1663 		if (wpa_s->l2 == NULL)
   1664 			return -1;
   1665 	}
   1666 
   1667 	if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
   1668 		wpa_printf(MSG_ERROR, "Failed to get own L2 address");
   1669 		return -1;
   1670 	}
   1671 
   1672 	wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
   1673 		   MAC2STR(wpa_s->own_addr));
   1674 
   1675 	if (wpa_s->bridge_ifname[0]) {
   1676 		wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
   1677 			   " '%s'", wpa_s->bridge_ifname);
   1678 		wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
   1679 					      wpa_s->own_addr,
   1680 					      ETH_P_EAPOL,
   1681 					      wpa_supplicant_rx_eapol, wpa_s,
   1682 					      0);
   1683 		if (wpa_s->l2_br == NULL) {
   1684 			wpa_printf(MSG_ERROR, "Failed to open l2_packet "
   1685 				   "connection for the bridge interface '%s'",
   1686 				   wpa_s->bridge_ifname);
   1687 			return -1;
   1688 		}
   1689 	}
   1690 
   1691 	/* Backwards compatibility call to set_wpa() handler. This is called
   1692 	 * only just after init and just before deinit, so these handler can be
   1693 	 * used to implement same functionality. */
   1694 	if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
   1695 		struct wpa_driver_capa capa;
   1696 		if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
   1697 		    !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
   1698 				    WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
   1699 			wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
   1700 			/* Continue to allow non-WPA modes to be used. */
   1701 		} else {
   1702 			wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
   1703 				"driver.");
   1704 			return -1;
   1705 		}
   1706 	}
   1707 
   1708 	wpa_clear_keys(wpa_s, NULL);
   1709 
   1710 	/* Make sure that TKIP countermeasures are not left enabled (could
   1711 	 * happen if wpa_supplicant is killed during countermeasures. */
   1712 	wpa_drv_set_countermeasures(wpa_s, 0);
   1713 
   1714 	wpa_drv_set_drop_unencrypted(wpa_s, 1);
   1715 
   1716 	wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
   1717 	wpa_drv_flush_pmkid(wpa_s);
   1718 
   1719 	wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
   1720 	wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
   1721 	interface_count++;
   1722 
   1723 	return 0;
   1724 }
   1725 
   1726 
   1727 static int wpa_supplicant_daemon(const char *pid_file)
   1728 {
   1729 	wpa_printf(MSG_DEBUG, "Daemonize..");
   1730 	return os_daemonize(pid_file);
   1731 }
   1732 
   1733 
   1734 static struct wpa_supplicant * wpa_supplicant_alloc(void)
   1735 {
   1736 	struct wpa_supplicant *wpa_s;
   1737 
   1738 	wpa_s = os_zalloc(sizeof(*wpa_s));
   1739 	if (wpa_s == NULL)
   1740 		return NULL;
   1741 	wpa_s->scan_req = 1;
   1742 
   1743 	return wpa_s;
   1744 }
   1745 
   1746 
   1747 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
   1748 				     struct wpa_interface *iface)
   1749 {
   1750 	wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
   1751 		   "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
   1752 		   iface->confname ? iface->confname : "N/A",
   1753 		   iface->driver ? iface->driver : "default",
   1754 		   iface->ctrl_interface ? iface->ctrl_interface : "N/A",
   1755 		   iface->bridge_ifname ? iface->bridge_ifname : "N/A");
   1756 
   1757 	if (wpa_supplicant_set_driver(wpa_s, iface->driver) < 0) {
   1758 		return -1;
   1759 	}
   1760 
   1761 	if (iface->confname) {
   1762 #ifdef CONFIG_BACKEND_FILE
   1763 		wpa_s->confname = os_rel2abs_path(iface->confname);
   1764 		if (wpa_s->confname == NULL) {
   1765 			wpa_printf(MSG_ERROR, "Failed to get absolute path "
   1766 				   "for configuration file '%s'.",
   1767 				   iface->confname);
   1768 			return -1;
   1769 		}
   1770 		wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
   1771 			   iface->confname, wpa_s->confname);
   1772 #else /* CONFIG_BACKEND_FILE */
   1773 		wpa_s->confname = os_strdup(iface->confname);
   1774 #endif /* CONFIG_BACKEND_FILE */
   1775 		wpa_s->conf = wpa_config_read(wpa_s->confname);
   1776 		if (wpa_s->conf == NULL) {
   1777 			wpa_printf(MSG_ERROR, "Failed to read or parse "
   1778 				   "configuration '%s'.", wpa_s->confname);
   1779 			return -1;
   1780 		}
   1781 
   1782 		/*
   1783 		 * Override ctrl_interface and driver_param if set on command
   1784 		 * line.
   1785 		 */
   1786 		if (iface->ctrl_interface) {
   1787 			os_free(wpa_s->conf->ctrl_interface);
   1788 			wpa_s->conf->ctrl_interface =
   1789 				os_strdup(iface->ctrl_interface);
   1790 		}
   1791 
   1792 		if (iface->driver_param) {
   1793 			os_free(wpa_s->conf->driver_param);
   1794 			wpa_s->conf->driver_param =
   1795 				os_strdup(iface->driver_param);
   1796 		}
   1797 	} else
   1798 		wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
   1799 						     iface->driver_param);
   1800 
   1801 	if (wpa_s->conf == NULL) {
   1802 		wpa_printf(MSG_ERROR, "\nNo configuration found.");
   1803 		return -1;
   1804 	}
   1805 
   1806 	if (iface->ifname == NULL) {
   1807 		wpa_printf(MSG_ERROR, "\nInterface name is required.");
   1808 		return -1;
   1809 	}
   1810 	if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
   1811 		wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
   1812 			   iface->ifname);
   1813 		return -1;
   1814 	}
   1815 	os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
   1816 
   1817 	if (iface->bridge_ifname) {
   1818 		if (os_strlen(iface->bridge_ifname) >=
   1819 		    sizeof(wpa_s->bridge_ifname)) {
   1820 			wpa_printf(MSG_ERROR, "\nToo long bridge interface "
   1821 				   "name '%s'.", iface->bridge_ifname);
   1822 			return -1;
   1823 		}
   1824 		os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
   1825 			   sizeof(wpa_s->bridge_ifname));
   1826 	}
   1827 
   1828 	return 0;
   1829 }
   1830 
   1831 
   1832 static int wpa_supplicant_init_iface2(struct wpa_supplicant *wpa_s)
   1833 {
   1834 	const char *ifname;
   1835 	struct wpa_driver_capa capa;
   1836 
   1837 	wpa_printf(MSG_DEBUG, "Initializing interface (2) '%s'",
   1838 		   wpa_s->ifname);
   1839 
   1840 	/* RSNA Supplicant Key Management - INITIALIZE */
   1841 	eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
   1842 	eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
   1843 
   1844 	/* Initialize driver interface and register driver event handler before
   1845 	 * L2 receive handler so that association events are processed before
   1846 	 * EAPOL-Key packets if both become available for the same select()
   1847 	 * call. */
   1848 	wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
   1849 	if (wpa_s->drv_priv == NULL) {
   1850 		wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
   1851 		return -1;
   1852 	}
   1853 	if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
   1854 		wpa_printf(MSG_ERROR, "Driver interface rejected "
   1855 			   "driver_param '%s'", wpa_s->conf->driver_param);
   1856 		return -1;
   1857 	}
   1858 
   1859 	ifname = wpa_drv_get_ifname(wpa_s);
   1860 	if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
   1861 		wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
   1862 			   "name with '%s'", ifname);
   1863 		os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
   1864 	}
   1865 
   1866 	if (wpa_supplicant_init_wpa(wpa_s) < 0)
   1867 		return -1;
   1868 
   1869 	wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
   1870 			  wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
   1871 			  NULL);
   1872 	wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
   1873 
   1874 	if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
   1875 	    wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
   1876 			     wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
   1877 		wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
   1878 			   "dot11RSNAConfigPMKLifetime");
   1879 		return -1;
   1880 	}
   1881 
   1882 	if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
   1883 	    wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
   1884 			     wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
   1885 		wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
   1886 			"dot11RSNAConfigPMKReauthThreshold");
   1887 		return -1;
   1888 	}
   1889 
   1890 	if (wpa_s->conf->dot11RSNAConfigSATimeout &&
   1891 	    wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
   1892 			     wpa_s->conf->dot11RSNAConfigSATimeout)) {
   1893 		wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
   1894 			   "dot11RSNAConfigSATimeout");
   1895 		return -1;
   1896 	}
   1897 
   1898 	if (wpa_supplicant_driver_init(wpa_s) < 0)
   1899 		return -1;
   1900 
   1901 	if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
   1902 	    wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
   1903 		wpa_printf(MSG_DEBUG, "Failed to set country");
   1904 		return -1;
   1905 	}
   1906 
   1907 	wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
   1908 
   1909 	if (wpas_wps_init(wpa_s))
   1910 		return -1;
   1911 
   1912 	if (wpa_supplicant_init_eapol(wpa_s) < 0)
   1913 		return -1;
   1914 	wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
   1915 
   1916 	wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
   1917 	if (wpa_s->ctrl_iface == NULL) {
   1918 		wpa_printf(MSG_ERROR,
   1919 			   "Failed to initialize control interface '%s'.\n"
   1920 			   "You may have another wpa_supplicant process "
   1921 			   "already running or the file was\n"
   1922 			   "left by an unclean termination of wpa_supplicant "
   1923 			   "in which case you will need\n"
   1924 			   "to manually remove this file before starting "
   1925 			   "wpa_supplicant again.\n",
   1926 			   wpa_s->conf->ctrl_interface);
   1927 		return -1;
   1928 	}
   1929 
   1930 	if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
   1931 		if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
   1932 			wpa_s->use_client_mlme = 1;
   1933 			if (ieee80211_sta_init(wpa_s))
   1934 				return -1;
   1935 		}
   1936 		if (capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)
   1937 			wpa_s->driver_4way_handshake = 1;
   1938 	}
   1939 
   1940 	return 0;
   1941 }
   1942 
   1943 
   1944 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
   1945 {
   1946 	if (wpa_s->drv_priv) {
   1947 		wpa_supplicant_deauthenticate(wpa_s,
   1948 					      WLAN_REASON_DEAUTH_LEAVING);
   1949 
   1950 		/* Backwards compatibility call to set_wpa() handler. This is
   1951 		 * called only just after init and just before deinit, so these
   1952 		 * handler can be used to implement same functionality. */
   1953 		if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
   1954 			wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
   1955 				   "driver.");
   1956 		}
   1957 
   1958 		wpa_drv_set_drop_unencrypted(wpa_s, 0);
   1959 		wpa_drv_set_countermeasures(wpa_s, 0);
   1960 		wpa_clear_keys(wpa_s, NULL);
   1961 	}
   1962 
   1963 	wpas_dbus_unregister_iface(wpa_s);
   1964 
   1965 	wpa_supplicant_cleanup(wpa_s);
   1966 
   1967 	if (wpa_s->drv_priv)
   1968 		wpa_drv_deinit(wpa_s);
   1969 }
   1970 
   1971 
   1972 /**
   1973  * wpa_supplicant_add_iface - Add a new network interface
   1974  * @global: Pointer to global data from wpa_supplicant_init()
   1975  * @iface: Interface configuration options
   1976  * Returns: Pointer to the created interface or %NULL on failure
   1977  *
   1978  * This function is used to add new network interfaces for %wpa_supplicant.
   1979  * This can be called before wpa_supplicant_run() to add interfaces before the
   1980  * main event loop has been started. In addition, new interfaces can be added
   1981  * dynamically while %wpa_supplicant is already running. This could happen,
   1982  * e.g., when a hotplug network adapter is inserted.
   1983  */
   1984 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
   1985 						 struct wpa_interface *iface)
   1986 {
   1987 	struct wpa_supplicant *wpa_s;
   1988 
   1989 	if (global == NULL || iface == NULL)
   1990 		return NULL;
   1991 
   1992 	wpa_s = wpa_supplicant_alloc();
   1993 	if (wpa_s == NULL)
   1994 		return NULL;
   1995 
   1996 	wpa_s->global = global;
   1997 
   1998 	if (wpa_supplicant_init_iface(wpa_s, iface) ||
   1999 	    wpa_supplicant_init_iface2(wpa_s)) {
   2000 		wpa_printf(MSG_DEBUG, "Failed to add interface %s",
   2001 			   iface->ifname);
   2002 		wpa_supplicant_deinit_iface(wpa_s);
   2003 		os_free(wpa_s);
   2004 		return NULL;
   2005 	}
   2006 
   2007 	/* Register the interface with the dbus control interface */
   2008 	if (wpas_dbus_register_iface(wpa_s)) {
   2009 		wpa_supplicant_deinit_iface(wpa_s);
   2010 		os_free(wpa_s);
   2011 		return NULL;
   2012 	}
   2013 
   2014 #ifdef ANDROID
   2015     char scan_prop[PROPERTY_VALUE_MAX];
   2016     char *endp;
   2017     if (property_get("wifi.supplicant_scan_interval", scan_prop, "6") != 0) {
   2018         wpa_s->scan_interval = (int)strtol(scan_prop, &endp, 0);
   2019         if (endp == scan_prop) {
   2020             wpa_s->scan_interval = 6;
   2021         }
   2022     }
   2023 #endif
   2024 	wpa_s->next = global->ifaces;
   2025 	global->ifaces = wpa_s;
   2026 
   2027 	wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
   2028 
   2029 	return wpa_s;
   2030 }
   2031 
   2032 
   2033 /**
   2034  * wpa_supplicant_remove_iface - Remove a network interface
   2035  * @global: Pointer to global data from wpa_supplicant_init()
   2036  * @wpa_s: Pointer to the network interface to be removed
   2037  * Returns: 0 if interface was removed, -1 if interface was not found
   2038  *
   2039  * This function can be used to dynamically remove network interfaces from
   2040  * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
   2041  * addition, this function is used to remove all remaining interfaces when
   2042  * %wpa_supplicant is terminated.
   2043  */
   2044 int wpa_supplicant_remove_iface(struct wpa_global *global,
   2045 				struct wpa_supplicant *wpa_s)
   2046 {
   2047 	struct wpa_supplicant *prev;
   2048 
   2049 	/* Remove interface from the global list of interfaces */
   2050 	prev = global->ifaces;
   2051 	if (prev == wpa_s) {
   2052 		global->ifaces = wpa_s->next;
   2053 	} else {
   2054 		while (prev && prev->next != wpa_s)
   2055 			prev = prev->next;
   2056 		if (prev == NULL)
   2057 			return -1;
   2058 		prev->next = wpa_s->next;
   2059 	}
   2060 
   2061 	wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
   2062 
   2063 	wpa_supplicant_deinit_iface(wpa_s);
   2064 	os_free(wpa_s);
   2065 
   2066 	return 0;
   2067 }
   2068 
   2069 
   2070 /**
   2071  * wpa_supplicant_get_iface - Get a new network interface
   2072  * @global: Pointer to global data from wpa_supplicant_init()
   2073  * @ifname: Interface name
   2074  * Returns: Pointer to the interface or %NULL if not found
   2075  */
   2076 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
   2077 						 const char *ifname)
   2078 {
   2079 	struct wpa_supplicant *wpa_s;
   2080 
   2081 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   2082 		if (os_strcmp(wpa_s->ifname, ifname) == 0)
   2083 			return wpa_s;
   2084 	}
   2085 	return NULL;
   2086 }
   2087 
   2088 
   2089 /**
   2090  * wpa_supplicant_init - Initialize %wpa_supplicant
   2091  * @params: Parameters for %wpa_supplicant
   2092  * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
   2093  *
   2094  * This function is used to initialize %wpa_supplicant. After successful
   2095  * initialization, the returned data pointer can be used to add and remove
   2096  * network interfaces, and eventually, to deinitialize %wpa_supplicant.
   2097  */
   2098 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
   2099 {
   2100 	struct wpa_global *global;
   2101 	int ret, i;
   2102 
   2103 	if (params == NULL)
   2104 		return NULL;
   2105 
   2106 	wpa_debug_open_file(params->wpa_debug_file_path);
   2107 
   2108 	ret = eap_peer_register_methods();
   2109 	if (ret) {
   2110 		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
   2111 		if (ret == -2)
   2112 			wpa_printf(MSG_ERROR, "Two or more EAP methods used "
   2113 				   "the same EAP type.");
   2114 		return NULL;
   2115 	}
   2116 
   2117 	global = os_zalloc(sizeof(*global));
   2118 	if (global == NULL)
   2119 		return NULL;
   2120 	global->params.daemonize = params->daemonize;
   2121 	global->params.wait_for_monitor = params->wait_for_monitor;
   2122 	global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
   2123 	if (params->pid_file)
   2124 		global->params.pid_file = os_strdup(params->pid_file);
   2125 	if (params->ctrl_interface)
   2126 		global->params.ctrl_interface =
   2127 			os_strdup(params->ctrl_interface);
   2128 	wpa_debug_level = global->params.wpa_debug_level =
   2129 		params->wpa_debug_level;
   2130 	wpa_debug_show_keys = global->params.wpa_debug_show_keys =
   2131 		params->wpa_debug_show_keys;
   2132 	wpa_debug_timestamp = global->params.wpa_debug_timestamp =
   2133 		params->wpa_debug_timestamp;
   2134 
   2135 	if (eloop_init(global)) {
   2136 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
   2137 		wpa_supplicant_deinit(global);
   2138 		return NULL;
   2139 	}
   2140 
   2141 	global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
   2142 	if (global->ctrl_iface == NULL) {
   2143 		wpa_supplicant_deinit(global);
   2144 		return NULL;
   2145 	}
   2146 
   2147 	if (global->params.dbus_ctrl_interface) {
   2148 		global->dbus_ctrl_iface =
   2149 			wpa_supplicant_dbus_ctrl_iface_init(global);
   2150 		if (global->dbus_ctrl_iface == NULL) {
   2151 			wpa_supplicant_deinit(global);
   2152 			return NULL;
   2153 		}
   2154 	}
   2155 
   2156 	for (i = 0; wpa_supplicant_drivers[i]; i++)
   2157 		global->drv_count++;
   2158 	if (global->drv_count == 0) {
   2159 		wpa_printf(MSG_ERROR, "No drivers enabled");
   2160 		wpa_supplicant_deinit(global);
   2161 		return NULL;
   2162 	}
   2163 	global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
   2164 	if (global->drv_priv == NULL) {
   2165 		wpa_supplicant_deinit(global);
   2166 		return NULL;
   2167 	}
   2168 	for (i = 0; wpa_supplicant_drivers[i]; i++) {
   2169 		if (!wpa_supplicant_drivers[i]->global_init)
   2170 			continue;
   2171 		global->drv_priv[i] = wpa_supplicant_drivers[i]->global_init();
   2172 		if (global->drv_priv[i] == NULL) {
   2173 			wpa_printf(MSG_ERROR, "Failed to initialize driver "
   2174 				   "'%s'", wpa_supplicant_drivers[i]->name);
   2175 			wpa_supplicant_deinit(global);
   2176 			return NULL;
   2177 		}
   2178 	}
   2179 
   2180 	return global;
   2181 }
   2182 
   2183 
   2184 /**
   2185  * wpa_supplicant_run - Run the %wpa_supplicant main event loop
   2186  * @global: Pointer to global data from wpa_supplicant_init()
   2187  * Returns: 0 after successful event loop run, -1 on failure
   2188  *
   2189  * This function starts the main event loop and continues running as long as
   2190  * there are any remaining events. In most cases, this function is running as
   2191  * long as the %wpa_supplicant process in still in use.
   2192  */
   2193 int wpa_supplicant_run(struct wpa_global *global)
   2194 {
   2195 	struct wpa_supplicant *wpa_s;
   2196 
   2197 	if (global->params.daemonize &&
   2198 	    wpa_supplicant_daemon(global->params.pid_file))
   2199 		return -1;
   2200 
   2201 	if (global->params.wait_for_monitor) {
   2202 		for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
   2203 			if (wpa_s->ctrl_iface)
   2204 				wpa_supplicant_ctrl_iface_wait(
   2205 					wpa_s->ctrl_iface);
   2206 	}
   2207 
   2208 	eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
   2209 	eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
   2210 
   2211 	eloop_run();
   2212 
   2213 	return 0;
   2214 }
   2215 
   2216 
   2217 /**
   2218  * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
   2219  * @global: Pointer to global data from wpa_supplicant_init()
   2220  *
   2221  * This function is called to deinitialize %wpa_supplicant and to free all
   2222  * allocated resources. Remaining network interfaces will also be removed.
   2223  */
   2224 void wpa_supplicant_deinit(struct wpa_global *global)
   2225 {
   2226 	int i;
   2227 
   2228 	if (global == NULL)
   2229 		return;
   2230 
   2231 	wpa_supplicant_terminate(0, global, NULL);
   2232 
   2233 	while (global->ifaces)
   2234 		wpa_supplicant_remove_iface(global, global->ifaces);
   2235 
   2236 	if (global->ctrl_iface)
   2237 		wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
   2238 	if (global->dbus_ctrl_iface)
   2239 		wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
   2240 
   2241 	eap_peer_unregister_methods();
   2242 
   2243 	for (i = 0; wpa_supplicant_drivers[i] && global->drv_priv; i++) {
   2244 		if (!global->drv_priv[i])
   2245 			continue;
   2246 		wpa_supplicant_drivers[i]->global_deinit(global->drv_priv[i]);
   2247 	}
   2248 	os_free(global->drv_priv);
   2249 
   2250 	eloop_destroy();
   2251 
   2252 	if (global->params.pid_file) {
   2253 		os_daemonize_terminate(global->params.pid_file);
   2254 		os_free(global->params.pid_file);
   2255 	}
   2256 	os_free(global->params.ctrl_interface);
   2257 
   2258 	os_free(global);
   2259 	wpa_debug_close_file();
   2260 }
   2261