wpa_supplicant.c revision 622b66d6efd0cccfeb8623184fadf2f76e7e8206
1/*
2 * WPA Supplicant
3 * Copyright (c) 2003-2011, Jouni Malinen <j@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 "crypto/random.h"
23#include "eapol_supp/eapol_supp_sm.h"
24#include "eap_peer/eap.h"
25#include "eap_server/eap_methods.h"
26#include "rsn_supp/wpa.h"
27#include "eloop.h"
28#include "config.h"
29#include "l2_packet/l2_packet.h"
30#include "wpa_supplicant_i.h"
31#include "driver_i.h"
32#include "ctrl_iface.h"
33#include "pcsc_funcs.h"
34#include "common/version.h"
35#include "rsn_supp/preauth.h"
36#include "rsn_supp/pmksa_cache.h"
37#include "common/wpa_ctrl.h"
38#include "mlme.h"
39#include "common/ieee802_11_defs.h"
40#include "p2p/p2p.h"
41#include "blacklist.h"
42#include "wpas_glue.h"
43#include "wps_supplicant.h"
44#include "ibss_rsn.h"
45#include "sme.h"
46#include "ap.h"
47#include "p2p_supplicant.h"
48#include "notify.h"
49#include "bgscan.h"
50#include "bss.h"
51#include "scan.h"
52
53const char *wpa_supplicant_version =
54"wpa_supplicant v" VERSION_STR "\n"
55"Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi> and contributors";
56
57const char *wpa_supplicant_license =
58"This program is free software. You can distribute it and/or modify it\n"
59"under the terms of the GNU General Public License version 2.\n"
60"\n"
61"Alternatively, this software may be distributed under the terms of the\n"
62"BSD license. See README and COPYING for more details.\n"
63#ifdef EAP_TLS_OPENSSL
64"\nThis product includes software developed by the OpenSSL Project\n"
65"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
66#endif /* EAP_TLS_OPENSSL */
67;
68
69#ifndef CONFIG_NO_STDOUT_DEBUG
70/* Long text divided into parts in order to fit in C89 strings size limits. */
71const char *wpa_supplicant_full_license1 =
72"This program is free software; you can redistribute it and/or modify\n"
73"it under the terms of the GNU General Public License version 2 as\n"
74"published by the Free Software Foundation.\n"
75"\n"
76"This program is distributed in the hope that it will be useful,\n"
77"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
78"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
79"GNU General Public License for more details.\n"
80"\n";
81const char *wpa_supplicant_full_license2 =
82"You should have received a copy of the GNU General Public License\n"
83"along with this program; if not, write to the Free Software\n"
84"Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n"
85"\n"
86"Alternatively, this software may be distributed under the terms of the\n"
87"BSD license.\n"
88"\n"
89"Redistribution and use in source and binary forms, with or without\n"
90"modification, are permitted provided that the following conditions are\n"
91"met:\n"
92"\n";
93const char *wpa_supplicant_full_license3 =
94"1. Redistributions of source code must retain the above copyright\n"
95"   notice, this list of conditions and the following disclaimer.\n"
96"\n"
97"2. Redistributions in binary form must reproduce the above copyright\n"
98"   notice, this list of conditions and the following disclaimer in the\n"
99"   documentation and/or other materials provided with the distribution.\n"
100"\n";
101const char *wpa_supplicant_full_license4 =
102"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
103"   names of its contributors may be used to endorse or promote products\n"
104"   derived from this software without specific prior written permission.\n"
105"\n"
106"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
107"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
108"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
109"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
110const char *wpa_supplicant_full_license5 =
111"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
112"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
113"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
114"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
115"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
116"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
117"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
118"\n";
119#endif /* CONFIG_NO_STDOUT_DEBUG */
120
121extern int wpa_debug_level;
122extern int wpa_debug_show_keys;
123extern int wpa_debug_timestamp;
124extern struct wpa_driver_ops *wpa_drivers[];
125
126/* Configure default/group WEP keys for static WEP */
127int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
128{
129	int i, set = 0;
130
131	for (i = 0; i < NUM_WEP_KEYS; i++) {
132		if (ssid->wep_key_len[i] == 0)
133			continue;
134
135		set = 1;
136		wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
137				i, i == ssid->wep_tx_keyidx, NULL, 0,
138				ssid->wep_key[i], ssid->wep_key_len[i]);
139	}
140
141	return set;
142}
143
144
145static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
146					   struct wpa_ssid *ssid)
147{
148	u8 key[32];
149	size_t keylen;
150	enum wpa_alg alg;
151	u8 seq[6] = { 0 };
152
153	/* IBSS/WPA-None uses only one key (Group) for both receiving and
154	 * sending unicast and multicast packets. */
155
156	if (ssid->mode != WPAS_MODE_IBSS) {
157		wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
158			"IBSS/ad-hoc) for WPA-None", ssid->mode);
159		return -1;
160	}
161
162	if (!ssid->psk_set) {
163		wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
164			"WPA-None");
165		return -1;
166	}
167
168	switch (wpa_s->group_cipher) {
169	case WPA_CIPHER_CCMP:
170		os_memcpy(key, ssid->psk, 16);
171		keylen = 16;
172		alg = WPA_ALG_CCMP;
173		break;
174	case WPA_CIPHER_TKIP:
175		/* WPA-None uses the same Michael MIC key for both TX and RX */
176		os_memcpy(key, ssid->psk, 16 + 8);
177		os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
178		keylen = 32;
179		alg = WPA_ALG_TKIP;
180		break;
181	default:
182		wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
183			"WPA-None", wpa_s->group_cipher);
184		return -1;
185	}
186
187	/* TODO: should actually remember the previously used seq#, both for TX
188	 * and RX from each STA.. */
189
190	return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
191}
192
193
194static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
195{
196	struct wpa_supplicant *wpa_s = eloop_ctx;
197	const u8 *bssid = wpa_s->bssid;
198	if (is_zero_ether_addr(bssid))
199		bssid = wpa_s->pending_bssid;
200	wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
201		MAC2STR(bssid));
202	wpa_blacklist_add(wpa_s, bssid);
203	wpa_sm_notify_disassoc(wpa_s->wpa);
204	wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
205	wpa_s->reassociate = 1;
206
207	/*
208	 * If we timed out, the AP or the local radio may be busy.
209	 * So, wait a second until scanning again.
210	 */
211	wpa_supplicant_req_scan(wpa_s, 1, 0);
212}
213
214
215/**
216 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
217 * @wpa_s: Pointer to wpa_supplicant data
218 * @sec: Number of seconds after which to time out authentication
219 * @usec: Number of microseconds after which to time out authentication
220 *
221 * This function is used to schedule a timeout for the current authentication
222 * attempt.
223 */
224void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
225				     int sec, int usec)
226{
227	if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
228	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
229		return;
230
231	wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
232		"%d usec", sec, usec);
233	eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
234	eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
235}
236
237
238/**
239 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
240 * @wpa_s: Pointer to wpa_supplicant data
241 *
242 * This function is used to cancel authentication timeout scheduled with
243 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
244 * been completed.
245 */
246void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
247{
248	wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
249	eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
250	wpa_blacklist_del(wpa_s, wpa_s->bssid);
251}
252
253
254/**
255 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
256 * @wpa_s: Pointer to wpa_supplicant data
257 *
258 * This function is used to configure EAPOL state machine based on the selected
259 * authentication mode.
260 */
261void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
262{
263#ifdef IEEE8021X_EAPOL
264	struct eapol_config eapol_conf;
265	struct wpa_ssid *ssid = wpa_s->current_ssid;
266
267#ifdef CONFIG_IBSS_RSN
268	if (ssid->mode == WPAS_MODE_IBSS &&
269	    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
270	    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
271		/*
272		 * RSN IBSS authentication is per-STA and we can disable the
273		 * per-BSSID EAPOL authentication.
274		 */
275		eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
276		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
277		eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
278		return;
279	}
280#endif /* CONFIG_IBSS_RSN */
281
282	eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
283	eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
284
285	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
286	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
287		eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
288	else
289		eapol_sm_notify_portControl(wpa_s->eapol, Auto);
290
291	os_memset(&eapol_conf, 0, sizeof(eapol_conf));
292	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
293		eapol_conf.accept_802_1x_keys = 1;
294		eapol_conf.required_keys = 0;
295		if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
296			eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
297		}
298		if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
299			eapol_conf.required_keys |=
300				EAPOL_REQUIRE_KEY_BROADCAST;
301		}
302
303		if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
304			eapol_conf.required_keys = 0;
305	}
306	if (wpa_s->conf)
307		eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
308	eapol_conf.workaround = ssid->eap_workaround;
309	eapol_conf.eap_disabled =
310		!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
311		wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
312		wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
313	eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
314#endif /* IEEE8021X_EAPOL */
315}
316
317
318/**
319 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
320 * @wpa_s: Pointer to wpa_supplicant data
321 * @ssid: Configuration data for the network
322 *
323 * This function is used to configure WPA state machine and related parameters
324 * to a mode where WPA is not enabled. This is called as part of the
325 * authentication configuration when the selected network does not use WPA.
326 */
327void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
328				       struct wpa_ssid *ssid)
329{
330	int i;
331
332	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
333		wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
334	else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
335		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
336	else
337		wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
338	wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
339	wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
340	wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
341	wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
342	wpa_s->group_cipher = WPA_CIPHER_NONE;
343	wpa_s->mgmt_group_cipher = 0;
344
345	for (i = 0; i < NUM_WEP_KEYS; i++) {
346		if (ssid->wep_key_len[i] > 5) {
347			wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
348			wpa_s->group_cipher = WPA_CIPHER_WEP104;
349			break;
350		} else if (ssid->wep_key_len[i] > 0) {
351			wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
352			wpa_s->group_cipher = WPA_CIPHER_WEP40;
353			break;
354		}
355	}
356
357	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
358	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
359	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
360			 wpa_s->pairwise_cipher);
361	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
362#ifdef CONFIG_IEEE80211W
363	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
364			 wpa_s->mgmt_group_cipher);
365#endif /* CONFIG_IEEE80211W */
366
367	pmksa_cache_clear_current(wpa_s->wpa);
368}
369
370
371static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
372{
373	bgscan_deinit(wpa_s);
374	scard_deinit(wpa_s->scard);
375	wpa_s->scard = NULL;
376	wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
377	eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
378	l2_packet_deinit(wpa_s->l2);
379	wpa_s->l2 = NULL;
380	if (wpa_s->l2_br) {
381		l2_packet_deinit(wpa_s->l2_br);
382		wpa_s->l2_br = NULL;
383	}
384
385/* This has been moved to wpa_supplicant_deinit_iface to avoid a race with hostapd */
386#ifndef ANDROID
387	if (wpa_s->ctrl_iface) {
388		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
389		wpa_s->ctrl_iface = NULL;
390	}
391	if (wpa_s->conf != NULL) {
392		struct wpa_ssid *ssid;
393		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
394			wpas_notify_network_removed(wpa_s, ssid);
395		wpa_config_free(wpa_s->conf);
396		wpa_s->conf = NULL;
397	}
398#endif
399
400	os_free(wpa_s->confname);
401	wpa_s->confname = NULL;
402
403	wpa_sm_set_eapol(wpa_s->wpa, NULL);
404	eapol_sm_deinit(wpa_s->eapol);
405	wpa_s->eapol = NULL;
406
407	rsn_preauth_deinit(wpa_s->wpa);
408
409#ifdef CONFIG_TDLS
410	wpa_tdls_deinit(wpa_s->wpa);
411#endif /* CONFIG_TDLS */
412
413	pmksa_candidate_free(wpa_s->wpa);
414	wpa_sm_deinit(wpa_s->wpa);
415	wpa_s->wpa = NULL;
416	wpa_blacklist_clear(wpa_s);
417
418	wpa_bss_deinit(wpa_s);
419
420	wpa_supplicant_cancel_scan(wpa_s);
421	wpa_supplicant_cancel_auth_timeout(wpa_s);
422
423	ieee80211_sta_deinit(wpa_s);
424
425	wpas_wps_deinit(wpa_s);
426
427	wpabuf_free(wpa_s->pending_eapol_rx);
428	wpa_s->pending_eapol_rx = NULL;
429
430#ifdef CONFIG_IBSS_RSN
431	ibss_rsn_deinit(wpa_s->ibss_rsn);
432	wpa_s->ibss_rsn = NULL;
433#endif /* CONFIG_IBSS_RSN */
434
435	sme_deinit(wpa_s);
436
437#ifdef CONFIG_AP
438	wpa_supplicant_ap_deinit(wpa_s);
439#endif /* CONFIG_AP */
440
441#ifdef CONFIG_P2P
442	wpas_p2p_deinit(wpa_s);
443#endif /* CONFIG_P2P */
444
445	os_free(wpa_s->next_scan_freqs);
446	wpa_s->next_scan_freqs = NULL;
447}
448
449
450/**
451 * wpa_clear_keys - Clear keys configured for the driver
452 * @wpa_s: Pointer to wpa_supplicant data
453 * @addr: Previously used BSSID or %NULL if not available
454 *
455 * This function clears the encryption keys that has been previously configured
456 * for the driver.
457 */
458void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
459{
460	if (wpa_s->keys_cleared) {
461		/* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
462		 * timing issues with keys being cleared just before new keys
463		 * are set or just after association or something similar. This
464		 * shows up in group key handshake failing often because of the
465		 * client not receiving the first encrypted packets correctly.
466		 * Skipping some of the extra key clearing steps seems to help
467		 * in completing group key handshake more reliably. */
468		wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
469			"skip key clearing");
470		return;
471	}
472
473	/* MLME-DELETEKEYS.request */
474	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
475	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
476	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
477	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
478#ifdef CONFIG_IEEE80211W
479	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
480	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
481#endif /* CONFIG_IEEE80211W */
482	if (addr) {
483		wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
484				0);
485		/* MLME-SETPROTECTION.request(None) */
486		wpa_drv_mlme_setprotection(
487			wpa_s, addr,
488			MLME_SETPROTECTION_PROTECT_TYPE_NONE,
489			MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
490	}
491	wpa_s->keys_cleared = 1;
492}
493
494
495/**
496 * wpa_supplicant_state_txt - Get the connection state name as a text string
497 * @state: State (wpa_state; WPA_*)
498 * Returns: The state name as a printable text string
499 */
500const char * wpa_supplicant_state_txt(enum wpa_states state)
501{
502	switch (state) {
503	case WPA_DISCONNECTED:
504		return "DISCONNECTED";
505	case WPA_INACTIVE:
506		return "INACTIVE";
507	case WPA_INTERFACE_DISABLED:
508		return "INTERFACE_DISABLED";
509	case WPA_SCANNING:
510		return "SCANNING";
511	case WPA_AUTHENTICATING:
512		return "AUTHENTICATING";
513	case WPA_ASSOCIATING:
514		return "ASSOCIATING";
515	case WPA_ASSOCIATED:
516		return "ASSOCIATED";
517	case WPA_4WAY_HANDSHAKE:
518		return "4WAY_HANDSHAKE";
519	case WPA_GROUP_HANDSHAKE:
520		return "GROUP_HANDSHAKE";
521	case WPA_COMPLETED:
522		return "COMPLETED";
523	default:
524		return "UNKNOWN";
525	}
526}
527
528
529#ifdef CONFIG_BGSCAN
530
531static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
532{
533	if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
534		return;
535
536	bgscan_deinit(wpa_s);
537	if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
538		if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
539			wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
540				"bgscan");
541			/*
542			 * Live without bgscan; it is only used as a roaming
543			 * optimization, so the initial connection is not
544			 * affected.
545			 */
546		} else
547			wpa_s->bgscan_ssid = wpa_s->current_ssid;
548	} else
549		wpa_s->bgscan_ssid = NULL;
550}
551
552
553static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
554{
555	if (wpa_s->bgscan_ssid != NULL) {
556		bgscan_deinit(wpa_s);
557		wpa_s->bgscan_ssid = NULL;
558	}
559}
560
561#endif /* CONFIG_BGSCAN */
562
563
564/**
565 * wpa_supplicant_set_state - Set current connection state
566 * @wpa_s: Pointer to wpa_supplicant data
567 * @state: The new connection state
568 *
569 * This function is called whenever the connection state changes, e.g.,
570 * association is completed for WPA/WPA2 4-Way Handshake is started.
571 */
572void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
573			      enum wpa_states state)
574{
575	enum wpa_states old_state = wpa_s->wpa_state;
576
577	wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
578		wpa_supplicant_state_txt(wpa_s->wpa_state),
579		wpa_supplicant_state_txt(state));
580
581	if (state != WPA_SCANNING)
582		wpa_supplicant_notify_scanning(wpa_s, 0);
583
584	if (state == WPA_COMPLETED && wpa_s->new_connection) {
585#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
586		struct wpa_ssid *ssid = wpa_s->current_ssid;
587		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
588			MACSTR " completed %s [id=%d id_str=%s]",
589			MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
590			"(reauth)" : "(auth)",
591			ssid ? ssid->id : -1,
592			ssid && ssid->id_str ? ssid->id_str : "");
593#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
594		wpa_s->new_connection = 0;
595		wpa_s->reassociated_connection = 1;
596		wpa_drv_set_operstate(wpa_s, 1);
597#ifndef IEEE8021X_EAPOL
598		wpa_drv_set_supp_port(wpa_s, 1);
599#endif /* IEEE8021X_EAPOL */
600		wpa_s->after_wps = 0;
601#ifdef CONFIG_P2P
602		wpas_p2p_completed(wpa_s);
603#endif /* CONFIG_P2P */
604	} else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
605		   state == WPA_ASSOCIATED) {
606		wpa_s->new_connection = 1;
607		wpa_drv_set_operstate(wpa_s, 0);
608#ifndef IEEE8021X_EAPOL
609		wpa_drv_set_supp_port(wpa_s, 0);
610#endif /* IEEE8021X_EAPOL */
611	}
612	wpa_s->wpa_state = state;
613
614#ifdef CONFIG_BGSCAN
615	if (state == WPA_COMPLETED)
616		wpa_supplicant_start_bgscan(wpa_s);
617	else
618		wpa_supplicant_stop_bgscan(wpa_s);
619#endif /* CONFIG_BGSCAN */
620
621	if (wpa_s->wpa_state != old_state) {
622		wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
623
624		if (wpa_s->wpa_state == WPA_COMPLETED ||
625		    old_state == WPA_COMPLETED)
626			wpas_notify_auth_changed(wpa_s);
627	}
628}
629
630
631void wpa_supplicant_terminate_proc(struct wpa_global *global)
632{
633	int pending = 0;
634#ifdef CONFIG_WPS
635	struct wpa_supplicant *wpa_s = global->ifaces;
636	while (wpa_s) {
637		if (wpas_wps_terminate_pending(wpa_s) == 1)
638			pending = 1;
639		wpa_s = wpa_s->next;
640	}
641#endif /* CONFIG_WPS */
642	if (pending)
643		return;
644	eloop_terminate();
645}
646
647
648static void wpa_supplicant_terminate(int sig, void *signal_ctx)
649{
650	struct wpa_global *global = signal_ctx;
651	struct wpa_supplicant *wpa_s;
652	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
653		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
654			"received", sig);
655	}
656	wpa_supplicant_terminate_proc(global);
657}
658
659
660void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
661{
662	enum wpa_states old_state = wpa_s->wpa_state;
663
664	wpa_s->pairwise_cipher = 0;
665	wpa_s->group_cipher = 0;
666	wpa_s->mgmt_group_cipher = 0;
667	wpa_s->key_mgmt = 0;
668	if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
669		wpa_s->wpa_state = WPA_DISCONNECTED;
670
671	if (wpa_s->wpa_state != old_state)
672		wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
673}
674
675
676/**
677 * wpa_supplicant_reload_configuration - Reload configuration data
678 * @wpa_s: Pointer to wpa_supplicant data
679 * Returns: 0 on success or -1 if configuration parsing failed
680 *
681 * This function can be used to request that the configuration data is reloaded
682 * (e.g., after configuration file change). This function is reloading
683 * configuration only for one interface, so this may need to be called multiple
684 * times if %wpa_supplicant is controlling multiple interfaces and all
685 * interfaces need reconfiguration.
686 */
687int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
688{
689	struct wpa_config *conf;
690	struct wpa_ssid *old_ssid;
691	int reconf_ctrl;
692	int old_ap_scan;
693
694	if (wpa_s->confname == NULL)
695		return -1;
696	conf = wpa_config_read(wpa_s->confname);
697	if (conf == NULL) {
698		wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
699			"file '%s' - exiting", wpa_s->confname);
700		return -1;
701	}
702	conf->changed_parameters = (unsigned int) -1;
703
704	reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
705		|| (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
706		    os_strcmp(conf->ctrl_interface,
707			      wpa_s->conf->ctrl_interface) != 0);
708
709	if (reconf_ctrl && wpa_s->ctrl_iface) {
710		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
711		wpa_s->ctrl_iface = NULL;
712	}
713
714	eapol_sm_invalidate_cached_session(wpa_s->eapol);
715	old_ssid = wpa_s->current_ssid;
716	wpa_s->current_ssid = NULL;
717	if (old_ssid != wpa_s->current_ssid)
718		wpas_notify_network_changed(wpa_s);
719
720	/*
721	 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
722	 * pkcs11_engine_path, pkcs11_module_path.
723	 */
724	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
725		/*
726		 * Clear forced success to clear EAP state for next
727		 * authentication.
728		 */
729		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
730	}
731	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
732	wpa_sm_set_config(wpa_s->wpa, NULL);
733	wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
734	rsn_preauth_deinit(wpa_s->wpa);
735
736	old_ap_scan = wpa_s->conf->ap_scan;
737	wpa_config_free(wpa_s->conf);
738	wpa_s->conf = conf;
739	if (old_ap_scan != wpa_s->conf->ap_scan)
740		wpas_notify_ap_scan_changed(wpa_s);
741
742	if (reconf_ctrl)
743		wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
744
745	wpa_supplicant_update_config(wpa_s);
746
747	wpa_supplicant_clear_status(wpa_s);
748	if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
749		wpa_s->reassociate = 1;
750		wpa_supplicant_req_scan(wpa_s, 0, 0);
751	}
752	wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
753	return 0;
754}
755
756
757static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
758{
759	struct wpa_global *global = signal_ctx;
760	struct wpa_supplicant *wpa_s;
761	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
762		wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
763			sig);
764		if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
765			wpa_supplicant_terminate_proc(global);
766		}
767	}
768}
769
770
771enum wpa_cipher cipher_suite2driver(int cipher)
772{
773	switch (cipher) {
774	case WPA_CIPHER_NONE:
775		return CIPHER_NONE;
776	case WPA_CIPHER_WEP40:
777		return CIPHER_WEP40;
778	case WPA_CIPHER_WEP104:
779		return CIPHER_WEP104;
780	case WPA_CIPHER_CCMP:
781		return CIPHER_CCMP;
782	case WPA_CIPHER_TKIP:
783	default:
784		return CIPHER_TKIP;
785	}
786}
787
788
789enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
790{
791	switch (key_mgmt) {
792	case WPA_KEY_MGMT_NONE:
793		return KEY_MGMT_NONE;
794	case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
795		return KEY_MGMT_802_1X_NO_WPA;
796	case WPA_KEY_MGMT_IEEE8021X:
797		return KEY_MGMT_802_1X;
798	case WPA_KEY_MGMT_WPA_NONE:
799		return KEY_MGMT_WPA_NONE;
800	case WPA_KEY_MGMT_FT_IEEE8021X:
801		return KEY_MGMT_FT_802_1X;
802	case WPA_KEY_MGMT_FT_PSK:
803		return KEY_MGMT_FT_PSK;
804	case WPA_KEY_MGMT_IEEE8021X_SHA256:
805		return KEY_MGMT_802_1X_SHA256;
806	case WPA_KEY_MGMT_PSK_SHA256:
807		return KEY_MGMT_PSK_SHA256;
808	case WPA_KEY_MGMT_WPS:
809		return KEY_MGMT_WPS;
810	case WPA_KEY_MGMT_PSK:
811	default:
812		return KEY_MGMT_PSK;
813	}
814}
815
816
817static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
818					 struct wpa_ssid *ssid,
819					 struct wpa_ie_data *ie)
820{
821	int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
822	if (ret) {
823		if (ret == -2) {
824			wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
825				"from association info");
826		}
827		return -1;
828	}
829
830	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
831		"cipher suites");
832	if (!(ie->group_cipher & ssid->group_cipher)) {
833		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
834			"cipher 0x%x (mask 0x%x) - reject",
835			ie->group_cipher, ssid->group_cipher);
836		return -1;
837	}
838	if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
839		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
840			"cipher 0x%x (mask 0x%x) - reject",
841			ie->pairwise_cipher, ssid->pairwise_cipher);
842		return -1;
843	}
844	if (!(ie->key_mgmt & ssid->key_mgmt)) {
845		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
846			"management 0x%x (mask 0x%x) - reject",
847			ie->key_mgmt, ssid->key_mgmt);
848		return -1;
849	}
850
851#ifdef CONFIG_IEEE80211W
852	if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
853	    ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
854		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
855			"that does not support management frame protection - "
856			"reject");
857		return -1;
858	}
859#endif /* CONFIG_IEEE80211W */
860
861	return 0;
862}
863
864
865/**
866 * wpa_supplicant_set_suites - Set authentication and encryption parameters
867 * @wpa_s: Pointer to wpa_supplicant data
868 * @bss: Scan results for the selected BSS, or %NULL if not available
869 * @ssid: Configuration data for the selected network
870 * @wpa_ie: Buffer for the WPA/RSN IE
871 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
872 * used buffer length in case the functions returns success.
873 * Returns: 0 on success or -1 on failure
874 *
875 * This function is used to configure authentication and encryption parameters
876 * based on the network configuration and scan result for the selected BSS (if
877 * available).
878 */
879int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
880			      struct wpa_bss *bss, struct wpa_ssid *ssid,
881			      u8 *wpa_ie, size_t *wpa_ie_len)
882{
883	struct wpa_ie_data ie;
884	int sel, proto;
885	const u8 *bss_wpa, *bss_rsn;
886
887	if (bss) {
888		bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
889		bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
890	} else
891		bss_wpa = bss_rsn = NULL;
892
893	if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
894	    wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
895	    (ie.group_cipher & ssid->group_cipher) &&
896	    (ie.pairwise_cipher & ssid->pairwise_cipher) &&
897	    (ie.key_mgmt & ssid->key_mgmt)) {
898		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
899		proto = WPA_PROTO_RSN;
900	} else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
901		   wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
902		   (ie.group_cipher & ssid->group_cipher) &&
903		   (ie.pairwise_cipher & ssid->pairwise_cipher) &&
904		   (ie.key_mgmt & ssid->key_mgmt)) {
905		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
906		proto = WPA_PROTO_WPA;
907	} else if (bss) {
908		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
909		return -1;
910	} else {
911		if (ssid->proto & WPA_PROTO_RSN)
912			proto = WPA_PROTO_RSN;
913		else
914			proto = WPA_PROTO_WPA;
915		if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
916			os_memset(&ie, 0, sizeof(ie));
917			ie.group_cipher = ssid->group_cipher;
918			ie.pairwise_cipher = ssid->pairwise_cipher;
919			ie.key_mgmt = ssid->key_mgmt;
920#ifdef CONFIG_IEEE80211W
921			ie.mgmt_group_cipher =
922				ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
923				WPA_CIPHER_AES_128_CMAC : 0;
924#endif /* CONFIG_IEEE80211W */
925			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
926				"based on configuration");
927		} else
928			proto = ie.proto;
929	}
930
931	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
932		"pairwise %d key_mgmt %d proto %d",
933		ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
934#ifdef CONFIG_IEEE80211W
935	if (ssid->ieee80211w) {
936		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
937			ie.mgmt_group_cipher);
938	}
939#endif /* CONFIG_IEEE80211W */
940
941	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
942	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
943			 !!(ssid->proto & WPA_PROTO_RSN));
944
945	if (bss || !wpa_s->ap_ies_from_associnfo) {
946		if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
947					 bss_wpa ? 2 + bss_wpa[1] : 0) ||
948		    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
949					 bss_rsn ? 2 + bss_rsn[1] : 0))
950			return -1;
951	}
952
953	sel = ie.group_cipher & ssid->group_cipher;
954	if (sel & WPA_CIPHER_CCMP) {
955		wpa_s->group_cipher = WPA_CIPHER_CCMP;
956		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
957	} else if (sel & WPA_CIPHER_TKIP) {
958		wpa_s->group_cipher = WPA_CIPHER_TKIP;
959		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
960	} else if (sel & WPA_CIPHER_WEP104) {
961		wpa_s->group_cipher = WPA_CIPHER_WEP104;
962		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
963	} else if (sel & WPA_CIPHER_WEP40) {
964		wpa_s->group_cipher = WPA_CIPHER_WEP40;
965		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
966	} else {
967		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
968			"cipher");
969		return -1;
970	}
971
972	sel = ie.pairwise_cipher & ssid->pairwise_cipher;
973	if (sel & WPA_CIPHER_CCMP) {
974		wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
975		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
976	} else if (sel & WPA_CIPHER_TKIP) {
977		wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
978		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
979	} else if (sel & WPA_CIPHER_NONE) {
980		wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
981		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
982	} else {
983		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
984			"cipher");
985		return -1;
986	}
987
988	sel = ie.key_mgmt & ssid->key_mgmt;
989	if (0) {
990#ifdef CONFIG_IEEE80211R
991	} else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
992		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
993		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
994	} else if (sel & WPA_KEY_MGMT_FT_PSK) {
995		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
996		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
997#endif /* CONFIG_IEEE80211R */
998#ifdef CONFIG_IEEE80211W
999	} else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1000		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1001		wpa_dbg(wpa_s, MSG_DEBUG,
1002			"WPA: using KEY_MGMT 802.1X with SHA256");
1003	} else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1004		wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1005		wpa_dbg(wpa_s, MSG_DEBUG,
1006			"WPA: using KEY_MGMT PSK with SHA256");
1007#endif /* CONFIG_IEEE80211W */
1008	} else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1009		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1010		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1011	} else if (sel & WPA_KEY_MGMT_PSK) {
1012		wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1013		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1014	} else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1015		wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1016		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1017	} else {
1018		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1019			"authenticated key management type");
1020		return -1;
1021	}
1022
1023	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1024	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1025			 wpa_s->pairwise_cipher);
1026	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1027
1028#ifdef CONFIG_IEEE80211W
1029	sel = ie.mgmt_group_cipher;
1030	if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
1031	    !(ie.capabilities & WPA_CAPABILITY_MFPC))
1032		sel = 0;
1033	if (sel & WPA_CIPHER_AES_128_CMAC) {
1034		wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1035		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1036			"AES-128-CMAC");
1037	} else {
1038		wpa_s->mgmt_group_cipher = 0;
1039		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1040	}
1041	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1042			 wpa_s->mgmt_group_cipher);
1043	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
1044#endif /* CONFIG_IEEE80211W */
1045
1046	if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1047		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1048		return -1;
1049	}
1050
1051	if (ssid->key_mgmt &
1052	    (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
1053		wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1054	else
1055		wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1056
1057	return 0;
1058}
1059
1060
1061/**
1062 * wpa_supplicant_associate - Request association
1063 * @wpa_s: Pointer to wpa_supplicant data
1064 * @bss: Scan results for the selected BSS, or %NULL if not available
1065 * @ssid: Configuration data for the selected network
1066 *
1067 * This function is used to request %wpa_supplicant to associate with a BSS.
1068 */
1069void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1070			      struct wpa_bss *bss, struct wpa_ssid *ssid)
1071{
1072	u8 wpa_ie[200];
1073	size_t wpa_ie_len;
1074	int use_crypt, ret, i, bssid_changed;
1075	int algs = WPA_AUTH_ALG_OPEN;
1076	enum wpa_cipher cipher_pairwise, cipher_group;
1077	struct wpa_driver_associate_params params;
1078	int wep_keys_set = 0;
1079	struct wpa_driver_capa capa;
1080	int assoc_failed = 0;
1081	struct wpa_ssid *old_ssid;
1082
1083#ifdef CONFIG_IBSS_RSN
1084	ibss_rsn_deinit(wpa_s->ibss_rsn);
1085	wpa_s->ibss_rsn = NULL;
1086#endif /* CONFIG_IBSS_RSN */
1087
1088	if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1089	    ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1090#ifdef CONFIG_AP
1091		if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1092			wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1093				"mode");
1094			return;
1095		}
1096		wpa_supplicant_create_ap(wpa_s, ssid);
1097		wpa_s->current_bss = bss;
1098#else /* CONFIG_AP */
1099		wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1100			"the build");
1101#endif /* CONFIG_AP */
1102		return;
1103	}
1104
1105#ifdef CONFIG_TDLS
1106	if (bss)
1107		wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1108				bss->ie_len);
1109#endif /* CONFIG_TDLS */
1110
1111	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1112	    ssid->mode == IEEE80211_MODE_INFRA) {
1113		sme_authenticate(wpa_s, bss, ssid);
1114		return;
1115	}
1116
1117	os_memset(&params, 0, sizeof(params));
1118	wpa_s->reassociate = 0;
1119	if (bss) {
1120#ifdef CONFIG_IEEE80211R
1121		const u8 *ie, *md = NULL;
1122#endif /* CONFIG_IEEE80211R */
1123		wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1124			" (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1125			wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1126		bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1127		os_memset(wpa_s->bssid, 0, ETH_ALEN);
1128		os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1129		if (bssid_changed)
1130			wpas_notify_bssid_changed(wpa_s);
1131#ifdef CONFIG_IEEE80211R
1132		ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1133		if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1134			md = ie + 2;
1135		wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1136		if (md) {
1137			/* Prepare for the next transition */
1138			wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1139		}
1140#endif /* CONFIG_IEEE80211R */
1141#ifdef CONFIG_WPS
1142	} else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1143		   wpa_s->conf->ap_scan == 2 &&
1144		   (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1145		/* Use ap_scan==1 style network selection to find the network
1146		 */
1147		wpa_s->scan_req = 2;
1148		wpa_s->reassociate = 1;
1149		wpa_supplicant_req_scan(wpa_s, 0, 0);
1150		return;
1151#endif /* CONFIG_WPS */
1152	} else {
1153		wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1154			wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1155		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1156	}
1157	wpa_supplicant_cancel_scan(wpa_s);
1158
1159	/* Starting new association, so clear the possibly used WPA IE from the
1160	 * previous association. */
1161	wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1162
1163#ifdef IEEE8021X_EAPOL
1164	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1165		if (ssid->leap) {
1166			if (ssid->non_leap == 0)
1167				algs = WPA_AUTH_ALG_LEAP;
1168			else
1169				algs |= WPA_AUTH_ALG_LEAP;
1170		}
1171	}
1172#endif /* IEEE8021X_EAPOL */
1173	wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1174	if (ssid->auth_alg) {
1175		algs = ssid->auth_alg;
1176		wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1177			"0x%x", algs);
1178	}
1179
1180	if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1181		    wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1182	    (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1183			       WPA_KEY_MGMT_FT_IEEE8021X |
1184			       WPA_KEY_MGMT_FT_PSK |
1185			       WPA_KEY_MGMT_IEEE8021X_SHA256 |
1186			       WPA_KEY_MGMT_PSK_SHA256))) {
1187		int try_opportunistic;
1188		try_opportunistic = ssid->proactive_key_caching &&
1189			(ssid->proto & WPA_PROTO_RSN);
1190		if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1191					    wpa_s->current_ssid,
1192					    try_opportunistic) == 0)
1193			eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1194		wpa_ie_len = sizeof(wpa_ie);
1195		if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1196					      wpa_ie, &wpa_ie_len)) {
1197			wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1198				"key management and encryption suites");
1199			return;
1200		}
1201	} else if (ssid->key_mgmt &
1202		   (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1203		    WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1204		    WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1205		    WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1206		wpa_ie_len = sizeof(wpa_ie);
1207		if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1208					      wpa_ie, &wpa_ie_len)) {
1209			wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1210				"key management and encryption suites (no "
1211				"scan results)");
1212			return;
1213		}
1214#ifdef CONFIG_WPS
1215	} else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1216		struct wpabuf *wps_ie;
1217		wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1218		if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1219			wpa_ie_len = wpabuf_len(wps_ie);
1220			os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1221		} else
1222			wpa_ie_len = 0;
1223		wpabuf_free(wps_ie);
1224		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1225		if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1226			params.wps = WPS_MODE_PRIVACY;
1227		else
1228			params.wps = WPS_MODE_OPEN;
1229#endif /* CONFIG_WPS */
1230	} else {
1231		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1232		wpa_ie_len = 0;
1233	}
1234
1235#ifdef CONFIG_P2P
1236	if (wpa_s->global->p2p) {
1237		u8 *pos;
1238		size_t len;
1239		int res;
1240		int p2p_group;
1241		p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
1242		pos = wpa_ie + wpa_ie_len;
1243		len = sizeof(wpa_ie) - wpa_ie_len;
1244		res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
1245		if (res >= 0)
1246			wpa_ie_len += res;
1247	}
1248
1249	wpa_s->cross_connect_disallowed = 0;
1250	if (bss) {
1251		struct wpabuf *p2p;
1252		p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1253		if (p2p) {
1254			wpa_s->cross_connect_disallowed =
1255				p2p_get_cross_connect_disallowed(p2p);
1256			wpabuf_free(p2p);
1257			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1258				"connection",
1259				wpa_s->cross_connect_disallowed ?
1260				"disallows" : "allows");
1261		}
1262	}
1263#endif /* CONFIG_P2P */
1264
1265	wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1266	use_crypt = 1;
1267	cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1268	cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1269	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1270	    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1271		if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1272			use_crypt = 0;
1273		if (wpa_set_wep_keys(wpa_s, ssid)) {
1274			use_crypt = 1;
1275			wep_keys_set = 1;
1276		}
1277	}
1278	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1279		use_crypt = 0;
1280
1281#ifdef IEEE8021X_EAPOL
1282	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1283		if ((ssid->eapol_flags &
1284		     (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1285		      EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1286		    !wep_keys_set) {
1287			use_crypt = 0;
1288		} else {
1289			/* Assume that dynamic WEP-104 keys will be used and
1290			 * set cipher suites in order for drivers to expect
1291			 * encryption. */
1292			cipher_pairwise = cipher_group = CIPHER_WEP104;
1293		}
1294	}
1295#endif /* IEEE8021X_EAPOL */
1296
1297	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1298		/* Set the key before (and later after) association */
1299		wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1300	}
1301
1302	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1303	if (bss) {
1304		params.bssid = bss->bssid;
1305		params.ssid = bss->ssid;
1306		params.ssid_len = bss->ssid_len;
1307		params.freq = bss->freq;
1308	} else {
1309		params.ssid = ssid->ssid;
1310		params.ssid_len = ssid->ssid_len;
1311	}
1312	if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1313	    params.freq == 0)
1314		params.freq = ssid->frequency; /* Initial channel for IBSS */
1315	params.wpa_ie = wpa_ie;
1316	params.wpa_ie_len = wpa_ie_len;
1317	params.pairwise_suite = cipher_pairwise;
1318	params.group_suite = cipher_group;
1319	params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1320	params.auth_alg = algs;
1321	params.mode = ssid->mode;
1322	for (i = 0; i < NUM_WEP_KEYS; i++) {
1323		if (ssid->wep_key_len[i])
1324			params.wep_key[i] = ssid->wep_key[i];
1325		params.wep_key_len[i] = ssid->wep_key_len[i];
1326	}
1327	params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1328
1329	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1330	    (params.key_mgmt_suite == KEY_MGMT_PSK ||
1331	     params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1332		params.passphrase = ssid->passphrase;
1333		if (ssid->psk_set)
1334			params.psk = ssid->psk;
1335	}
1336
1337	params.drop_unencrypted = use_crypt;
1338
1339#ifdef CONFIG_IEEE80211W
1340	params.mgmt_frame_protection = ssid->ieee80211w;
1341	if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1342		const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1343		struct wpa_ie_data ie;
1344		if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1345		    ie.capabilities &
1346		    (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1347			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1348				"MFP: require MFP");
1349			params.mgmt_frame_protection =
1350				MGMT_FRAME_PROTECTION_REQUIRED;
1351		}
1352	}
1353#endif /* CONFIG_IEEE80211W */
1354
1355#ifdef CONFIG_P2P
1356	if (wpa_s->global->p2p &&
1357	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
1358		params.p2p = 1;
1359#endif /* CONFIG_P2P */
1360
1361	if (wpa_s->parent->set_sta_uapsd)
1362		params.uapsd = wpa_s->parent->sta_uapsd;
1363	else
1364		params.uapsd = -1;
1365
1366	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1367		ret = ieee80211_sta_associate(wpa_s, &params);
1368	else
1369		ret = wpa_drv_associate(wpa_s, &params);
1370	if (ret < 0) {
1371		wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1372			"failed");
1373		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1374			/*
1375			 * The driver is known to mean what is saying, so we
1376			 * can stop right here; the association will not
1377			 * succeed.
1378			 */
1379			wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1380			os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1381			return;
1382		}
1383		/* try to continue anyway; new association will be tried again
1384		 * after timeout */
1385		assoc_failed = 1;
1386	}
1387
1388	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1389		/* Set the key after the association just in case association
1390		 * cleared the previously configured key. */
1391		wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1392		/* No need to timeout authentication since there is no key
1393		 * management. */
1394		wpa_supplicant_cancel_auth_timeout(wpa_s);
1395		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1396#ifdef CONFIG_IBSS_RSN
1397	} else if (ssid->mode == WPAS_MODE_IBSS &&
1398		   wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1399		   wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1400		/*
1401		 * RSN IBSS authentication is per-STA and we can disable the
1402		 * per-BSSID authentication.
1403		 */
1404		wpa_supplicant_cancel_auth_timeout(wpa_s);
1405#endif /* CONFIG_IBSS_RSN */
1406	} else {
1407		/* Timeout for IEEE 802.11 authentication and association */
1408		int timeout = 60;
1409
1410		if (assoc_failed) {
1411			/* give IBSS a bit more time */
1412			timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1413		} else if (wpa_s->conf->ap_scan == 1) {
1414			/* give IBSS a bit more time */
1415			timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1416		}
1417		wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1418	}
1419
1420	if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1421	    capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1422		/* Set static WEP keys again */
1423		wpa_set_wep_keys(wpa_s, ssid);
1424	}
1425
1426	if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1427		/*
1428		 * Do not allow EAP session resumption between different
1429		 * network configurations.
1430		 */
1431		eapol_sm_invalidate_cached_session(wpa_s->eapol);
1432	}
1433	old_ssid = wpa_s->current_ssid;
1434	wpa_s->current_ssid = ssid;
1435	wpa_s->current_bss = bss;
1436	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1437	wpa_supplicant_initiate_eapol(wpa_s);
1438	if (old_ssid != wpa_s->current_ssid)
1439		wpas_notify_network_changed(wpa_s);
1440}
1441
1442
1443static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1444					    const u8 *addr)
1445{
1446	struct wpa_ssid *old_ssid;
1447
1448	wpa_clear_keys(wpa_s, addr);
1449	wpa_supplicant_mark_disassoc(wpa_s);
1450	old_ssid = wpa_s->current_ssid;
1451	wpa_s->current_ssid = NULL;
1452	wpa_s->current_bss = NULL;
1453	wpa_sm_set_config(wpa_s->wpa, NULL);
1454	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1455	if (old_ssid != wpa_s->current_ssid)
1456		wpas_notify_network_changed(wpa_s);
1457	eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1458}
1459
1460
1461/**
1462 * wpa_supplicant_disassociate - Disassociate the current connection
1463 * @wpa_s: Pointer to wpa_supplicant data
1464 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1465 *
1466 * This function is used to request %wpa_supplicant to disassociate with the
1467 * current AP.
1468 */
1469void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1470				 int reason_code)
1471{
1472	u8 *addr = NULL;
1473
1474	if (!is_zero_ether_addr(wpa_s->bssid)) {
1475		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1476			ieee80211_sta_disassociate(wpa_s, reason_code);
1477		else
1478			wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1479		addr = wpa_s->bssid;
1480	}
1481
1482	wpa_supplicant_clear_connection(wpa_s, addr);
1483}
1484
1485
1486/**
1487 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1488 * @wpa_s: Pointer to wpa_supplicant data
1489 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1490 *
1491 * This function is used to request %wpa_supplicant to deauthenticate from the
1492 * current AP.
1493 */
1494void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1495				   int reason_code)
1496{
1497	u8 *addr = NULL;
1498
1499	if (!is_zero_ether_addr(wpa_s->bssid)) {
1500		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1501			ieee80211_sta_deauthenticate(wpa_s, reason_code);
1502		else
1503			wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1504					       reason_code);
1505		addr = wpa_s->bssid;
1506	}
1507
1508	wpa_supplicant_clear_connection(wpa_s, addr);
1509}
1510
1511
1512/**
1513 * wpa_supplicant_enable_network - Mark a configured network as enabled
1514 * @wpa_s: wpa_supplicant structure for a network interface
1515 * @ssid: wpa_ssid structure for a configured network or %NULL
1516 *
1517 * Enables the specified network or all networks if no network specified.
1518 */
1519void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1520				   struct wpa_ssid *ssid)
1521{
1522	struct wpa_ssid *other_ssid;
1523	int was_disabled;
1524
1525	if (ssid == NULL) {
1526		for (other_ssid = wpa_s->conf->ssid; other_ssid;
1527		     other_ssid = other_ssid->next) {
1528			if (other_ssid->disabled == 2)
1529				continue; /* do not change persistent P2P group
1530					   * data */
1531			if (other_ssid == wpa_s->current_ssid &&
1532			    other_ssid->disabled)
1533				wpa_s->reassociate = 1;
1534
1535			was_disabled = other_ssid->disabled;
1536
1537			other_ssid->disabled = 0;
1538
1539			if (was_disabled != other_ssid->disabled)
1540				wpas_notify_network_enabled_changed(
1541					wpa_s, other_ssid);
1542		}
1543		if (wpa_s->reassociate)
1544			wpa_supplicant_req_scan(wpa_s, 0, 0);
1545	} else if (ssid->disabled && ssid->disabled != 2) {
1546		if (wpa_s->current_ssid == NULL) {
1547			/*
1548			 * Try to reassociate since there is no current
1549			 * configuration and a new network was made available.
1550			 */
1551			wpa_s->reassociate = 1;
1552			wpa_supplicant_req_scan(wpa_s, 0, 0);
1553		}
1554
1555		was_disabled = ssid->disabled;
1556
1557		ssid->disabled = 0;
1558
1559		if (was_disabled != ssid->disabled)
1560			wpas_notify_network_enabled_changed(wpa_s, ssid);
1561	}
1562}
1563
1564
1565/**
1566 * wpa_supplicant_disable_network - Mark a configured network as disabled
1567 * @wpa_s: wpa_supplicant structure for a network interface
1568 * @ssid: wpa_ssid structure for a configured network or %NULL
1569 *
1570 * Disables the specified network or all networks if no network specified.
1571 */
1572void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1573				    struct wpa_ssid *ssid)
1574{
1575	struct wpa_ssid *other_ssid;
1576	int was_disabled;
1577
1578	if (ssid == NULL) {
1579		for (other_ssid = wpa_s->conf->ssid; other_ssid;
1580		     other_ssid = other_ssid->next) {
1581			was_disabled = other_ssid->disabled;
1582			if (was_disabled == 2)
1583				continue; /* do not change persistent P2P group
1584					   * data */
1585
1586			other_ssid->disabled = 1;
1587
1588			if (was_disabled != other_ssid->disabled)
1589				wpas_notify_network_enabled_changed(
1590					wpa_s, other_ssid);
1591		}
1592		if (wpa_s->current_ssid)
1593			wpa_supplicant_disassociate(
1594				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1595	} else if (ssid->disabled != 2) {
1596		if (ssid == wpa_s->current_ssid)
1597			wpa_supplicant_disassociate(
1598				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1599
1600		was_disabled = ssid->disabled;
1601
1602		ssid->disabled = 1;
1603
1604		if (was_disabled != ssid->disabled)
1605			wpas_notify_network_enabled_changed(wpa_s, ssid);
1606	}
1607}
1608
1609
1610/**
1611 * wpa_supplicant_select_network - Attempt association with a network
1612 * @wpa_s: wpa_supplicant structure for a network interface
1613 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1614 */
1615void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1616				   struct wpa_ssid *ssid)
1617{
1618
1619	struct wpa_ssid *other_ssid;
1620
1621	if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1622		wpa_supplicant_disassociate(
1623			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1624
1625	/*
1626	 * Mark all other networks disabled or mark all networks enabled if no
1627	 * network specified.
1628	 */
1629	for (other_ssid = wpa_s->conf->ssid; other_ssid;
1630	     other_ssid = other_ssid->next) {
1631		int was_disabled = other_ssid->disabled;
1632		if (was_disabled == 2)
1633			continue; /* do not change persistent P2P group data */
1634
1635		other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1636
1637		if (was_disabled != other_ssid->disabled)
1638			wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1639	}
1640	wpa_s->connect_without_scan = NULL;
1641	wpa_s->disconnected = 0;
1642	wpa_s->reassociate = 1;
1643	wpa_supplicant_req_scan(wpa_s, 0, 0);
1644
1645	if (ssid)
1646		wpas_notify_network_selected(wpa_s, ssid);
1647}
1648
1649
1650/**
1651 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1652 * @wpa_s: wpa_supplicant structure for a network interface
1653 * @ap_scan: AP scan mode
1654 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1655 *
1656 */
1657int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1658{
1659
1660	int old_ap_scan;
1661
1662	if (ap_scan < 0 || ap_scan > 2)
1663		return -1;
1664
1665	old_ap_scan = wpa_s->conf->ap_scan;
1666	wpa_s->conf->ap_scan = ap_scan;
1667
1668	if (old_ap_scan != wpa_s->conf->ap_scan)
1669		wpas_notify_ap_scan_changed(wpa_s);
1670
1671	return 0;
1672}
1673
1674
1675/**
1676 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
1677 * @wpa_s: wpa_supplicant structure for a network interface
1678 * @expire_age: Expiration age in seconds
1679 * Returns: 0 if succeed or -1 if expire_age has an invalid value
1680 *
1681 */
1682int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
1683					  unsigned int bss_expire_age)
1684{
1685	if (bss_expire_age < 10) {
1686		wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
1687			bss_expire_age);
1688		return -1;
1689	}
1690	wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
1691		bss_expire_age);
1692	wpa_s->conf->bss_expiration_age = bss_expire_age;
1693
1694	return 0;
1695}
1696
1697
1698/**
1699 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
1700 * @wpa_s: wpa_supplicant structure for a network interface
1701 * @expire_count: number of scans after which an unseen BSS is reclaimed
1702 * Returns: 0 if succeed or -1 if expire_count has an invalid value
1703 *
1704 */
1705int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
1706					    unsigned int bss_expire_count)
1707{
1708	if (bss_expire_count < 1) {
1709		wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
1710			bss_expire_count);
1711		return -1;
1712	}
1713	wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
1714		bss_expire_count);
1715	wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
1716
1717	return 0;
1718}
1719
1720
1721/**
1722 * wpa_supplicant_set_debug_params - Set global debug params
1723 * @global: wpa_global structure
1724 * @debug_level: debug level
1725 * @debug_timestamp: determines if show timestamp in debug data
1726 * @debug_show_keys: determines if show keys in debug data
1727 * Returns: 0 if succeed or -1 if debug_level has wrong value
1728 */
1729int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1730				    int debug_timestamp, int debug_show_keys)
1731{
1732
1733	int old_level, old_timestamp, old_show_keys;
1734
1735	/* check for allowed debuglevels */
1736	if (debug_level != MSG_EXCESSIVE &&
1737	    debug_level != MSG_MSGDUMP &&
1738	    debug_level != MSG_DEBUG &&
1739	    debug_level != MSG_INFO &&
1740	    debug_level != MSG_WARNING &&
1741	    debug_level != MSG_ERROR)
1742		return -1;
1743
1744	old_level = wpa_debug_level;
1745	old_timestamp = wpa_debug_timestamp;
1746	old_show_keys = wpa_debug_show_keys;
1747
1748	wpa_debug_level = debug_level;
1749	wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1750	wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1751
1752	if (wpa_debug_level != old_level)
1753		wpas_notify_debug_level_changed(global);
1754	if (wpa_debug_timestamp != old_timestamp)
1755		wpas_notify_debug_timestamp_changed(global);
1756	if (wpa_debug_show_keys != old_show_keys)
1757		wpas_notify_debug_show_keys_changed(global);
1758
1759	return 0;
1760}
1761
1762
1763/**
1764 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1765 * @wpa_s: Pointer to wpa_supplicant data
1766 * Returns: A pointer to the current network structure or %NULL on failure
1767 */
1768struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1769{
1770	struct wpa_ssid *entry;
1771	u8 ssid[MAX_SSID_LEN];
1772	int res;
1773	size_t ssid_len;
1774	u8 bssid[ETH_ALEN];
1775	int wired;
1776
1777	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1778		if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1779			wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1780				"MLME");
1781			return NULL;
1782		}
1783	} else {
1784		res = wpa_drv_get_ssid(wpa_s, ssid);
1785		if (res < 0) {
1786			wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1787				"driver");
1788			return NULL;
1789		}
1790		ssid_len = res;
1791	}
1792
1793	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1794		os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1795	else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1796		wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
1797			"driver");
1798		return NULL;
1799	}
1800
1801	wired = wpa_s->conf->ap_scan == 0 &&
1802		(wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1803
1804	entry = wpa_s->conf->ssid;
1805	while (entry) {
1806		if (!entry->disabled &&
1807		    ((ssid_len == entry->ssid_len &&
1808		      os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1809		    (!entry->bssid_set ||
1810		     os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1811			return entry;
1812#ifdef CONFIG_WPS
1813		if (!entry->disabled &&
1814		    (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1815		    (entry->ssid == NULL || entry->ssid_len == 0) &&
1816		    (!entry->bssid_set ||
1817		     os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1818			return entry;
1819#endif /* CONFIG_WPS */
1820		entry = entry->next;
1821	}
1822
1823	return NULL;
1824}
1825
1826
1827static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1828				     const char *name)
1829{
1830	int i;
1831	size_t len;
1832	const char *pos, *driver = name;
1833
1834	if (wpa_s == NULL)
1835		return -1;
1836
1837	if (wpa_drivers[0] == NULL) {
1838		wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
1839			"wpa_supplicant");
1840		return -1;
1841	}
1842
1843	if (name == NULL) {
1844		/* default to first driver in the list */
1845		wpa_s->driver = wpa_drivers[0];
1846		wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
1847		return 0;
1848	}
1849
1850	do {
1851		pos = os_strchr(driver, ',');
1852		if (pos)
1853			len = pos - driver;
1854		else
1855			len = os_strlen(driver);
1856
1857		for (i = 0; wpa_drivers[i]; i++) {
1858			if (os_strlen(wpa_drivers[i]->name) == len &&
1859			    os_strncmp(driver, wpa_drivers[i]->name, len) ==
1860			    0) {
1861				wpa_s->driver = wpa_drivers[i];
1862				wpa_s->global_drv_priv =
1863					wpa_s->global->drv_priv[i];
1864				return 0;
1865			}
1866		}
1867
1868		driver = pos + 1;
1869	} while (pos);
1870
1871	wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
1872	return -1;
1873}
1874
1875
1876/**
1877 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1878 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1879 *	with struct wpa_driver_ops::init()
1880 * @src_addr: Source address of the EAPOL frame
1881 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1882 * @len: Length of the EAPOL data
1883 *
1884 * This function is called for each received EAPOL frame. Most driver
1885 * interfaces rely on more generic OS mechanism for receiving frames through
1886 * l2_packet, but if such a mechanism is not available, the driver wrapper may
1887 * take care of received EAPOL frames and deliver them to the core supplicant
1888 * code by calling this function.
1889 */
1890void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1891			     const u8 *buf, size_t len)
1892{
1893	struct wpa_supplicant *wpa_s = ctx;
1894
1895	wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1896	wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1897
1898	if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1899		/*
1900		 * There is possible race condition between receiving the
1901		 * association event and the EAPOL frame since they are coming
1902		 * through different paths from the driver. In order to avoid
1903		 * issues in trying to process the EAPOL frame before receiving
1904		 * association information, lets queue it for processing until
1905		 * the association event is received.
1906		 */
1907		wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
1908			"of received EAPOL frame");
1909		wpabuf_free(wpa_s->pending_eapol_rx);
1910		wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1911		if (wpa_s->pending_eapol_rx) {
1912			os_get_time(&wpa_s->pending_eapol_rx_time);
1913			os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1914				  ETH_ALEN);
1915		}
1916		return;
1917	}
1918
1919#ifdef CONFIG_AP
1920	if (wpa_s->ap_iface) {
1921		wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1922		return;
1923	}
1924#endif /* CONFIG_AP */
1925
1926	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1927		wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
1928			"no key management is configured");
1929		return;
1930	}
1931
1932	if (wpa_s->eapol_received == 0 &&
1933	    (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1934	     !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1935	     wpa_s->wpa_state != WPA_COMPLETED) &&
1936	    (wpa_s->current_ssid == NULL ||
1937	     wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
1938		/* Timeout for completing IEEE 802.1X and WPA authentication */
1939		wpa_supplicant_req_auth_timeout(
1940			wpa_s,
1941			(wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1942			 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1943			 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1944			70 : 10, 0);
1945	}
1946	wpa_s->eapol_received++;
1947
1948	if (wpa_s->countermeasures) {
1949		wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
1950			"EAPOL packet");
1951		return;
1952	}
1953
1954#ifdef CONFIG_IBSS_RSN
1955	if (wpa_s->current_ssid &&
1956	    wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
1957		ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1958		return;
1959	}
1960#endif /* CONFIG_IBSS_RSN */
1961
1962	/* Source address of the incoming EAPOL frame could be compared to the
1963	 * current BSSID. However, it is possible that a centralized
1964	 * Authenticator could be using another MAC address than the BSSID of
1965	 * an AP, so just allow any address to be used for now. The replies are
1966	 * still sent to the current BSSID (if available), though. */
1967
1968	os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1969	if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1970	    eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1971		return;
1972	wpa_drv_poll(wpa_s);
1973	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1974		wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1975	else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1976		/*
1977		 * Set portValid = TRUE here since we are going to skip 4-way
1978		 * handshake processing which would normally set portValid. We
1979		 * need this to allow the EAPOL state machines to be completed
1980		 * without going through EAPOL-Key handshake.
1981		 */
1982		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1983	}
1984}
1985
1986
1987/**
1988 * wpa_supplicant_driver_init - Initialize driver interface parameters
1989 * @wpa_s: Pointer to wpa_supplicant data
1990 * Returns: 0 on success, -1 on failure
1991 *
1992 * This function is called to initialize driver interface parameters.
1993 * wpa_drv_init() must have been called before this function to initialize the
1994 * driver interface.
1995 */
1996int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1997{
1998	static int interface_count = 0;
1999
2000	if (wpa_s->driver->send_eapol) {
2001		const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2002		if (addr)
2003			os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2004	} else if (!(wpa_s->drv_flags &
2005		     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
2006		wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2007					   wpa_drv_get_mac_addr(wpa_s),
2008					   ETH_P_EAPOL,
2009					   wpa_supplicant_rx_eapol, wpa_s, 0);
2010		if (wpa_s->l2 == NULL)
2011			return -1;
2012	} else {
2013		const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2014		if (addr)
2015			os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2016	}
2017
2018	if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2019		wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2020		return -1;
2021	}
2022
2023	wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2024		MAC2STR(wpa_s->own_addr));
2025
2026	if (wpa_s->bridge_ifname[0]) {
2027		wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2028			"interface '%s'", wpa_s->bridge_ifname);
2029		wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2030					      wpa_s->own_addr,
2031					      ETH_P_EAPOL,
2032					      wpa_supplicant_rx_eapol, wpa_s,
2033					      0);
2034		if (wpa_s->l2_br == NULL) {
2035			wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2036				"connection for the bridge interface '%s'",
2037				wpa_s->bridge_ifname);
2038			return -1;
2039		}
2040	}
2041
2042	wpa_clear_keys(wpa_s, NULL);
2043
2044	/* Make sure that TKIP countermeasures are not left enabled (could
2045	 * happen if wpa_supplicant is killed during countermeasures. */
2046	wpa_drv_set_countermeasures(wpa_s, 0);
2047
2048	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2049	wpa_drv_flush_pmkid(wpa_s);
2050
2051	wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
2052	if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
2053		wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
2054		interface_count++;
2055	} else
2056		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2057
2058	return 0;
2059}
2060
2061
2062static int wpa_supplicant_daemon(const char *pid_file)
2063{
2064	wpa_printf(MSG_DEBUG, "Daemonize..");
2065	return os_daemonize(pid_file);
2066}
2067
2068
2069static struct wpa_supplicant * wpa_supplicant_alloc(void)
2070{
2071	struct wpa_supplicant *wpa_s;
2072
2073	wpa_s = os_zalloc(sizeof(*wpa_s));
2074	if (wpa_s == NULL)
2075		return NULL;
2076	wpa_s->scan_req = 1;
2077	wpa_s->scan_interval = 5;
2078	wpa_s->new_connection = 1;
2079	wpa_s->parent = wpa_s;
2080
2081	return wpa_s;
2082}
2083
2084
2085static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2086				     struct wpa_interface *iface)
2087{
2088	const char *ifname, *driver;
2089	struct wpa_driver_capa capa;
2090
2091	wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2092		   "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2093		   iface->confname ? iface->confname : "N/A",
2094		   iface->driver ? iface->driver : "default",
2095		   iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2096		   iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2097
2098	if (iface->confname) {
2099#ifdef CONFIG_BACKEND_FILE
2100		wpa_s->confname = os_rel2abs_path(iface->confname);
2101		if (wpa_s->confname == NULL) {
2102			wpa_printf(MSG_ERROR, "Failed to get absolute path "
2103				   "for configuration file '%s'.",
2104				   iface->confname);
2105			return -1;
2106		}
2107		wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2108			   iface->confname, wpa_s->confname);
2109#else /* CONFIG_BACKEND_FILE */
2110		wpa_s->confname = os_strdup(iface->confname);
2111#endif /* CONFIG_BACKEND_FILE */
2112		wpa_s->conf = wpa_config_read(wpa_s->confname);
2113		if (wpa_s->conf == NULL) {
2114			wpa_printf(MSG_ERROR, "Failed to read or parse "
2115				   "configuration '%s'.", wpa_s->confname);
2116			return -1;
2117		}
2118
2119		/*
2120		 * Override ctrl_interface and driver_param if set on command
2121		 * line.
2122		 */
2123		if (iface->ctrl_interface) {
2124			os_free(wpa_s->conf->ctrl_interface);
2125			wpa_s->conf->ctrl_interface =
2126				os_strdup(iface->ctrl_interface);
2127		}
2128
2129		if (iface->driver_param) {
2130			os_free(wpa_s->conf->driver_param);
2131			wpa_s->conf->driver_param =
2132				os_strdup(iface->driver_param);
2133		}
2134	} else
2135		wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2136						     iface->driver_param);
2137
2138	if (wpa_s->conf == NULL) {
2139		wpa_printf(MSG_ERROR, "\nNo configuration found.");
2140		return -1;
2141	}
2142
2143	if (iface->ifname == NULL) {
2144		wpa_printf(MSG_ERROR, "\nInterface name is required.");
2145		return -1;
2146	}
2147	if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2148		wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2149			   iface->ifname);
2150		return -1;
2151	}
2152	os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2153
2154	if (iface->bridge_ifname) {
2155		if (os_strlen(iface->bridge_ifname) >=
2156		    sizeof(wpa_s->bridge_ifname)) {
2157			wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2158				   "name '%s'.", iface->bridge_ifname);
2159			return -1;
2160		}
2161		os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2162			   sizeof(wpa_s->bridge_ifname));
2163	}
2164
2165	/* RSNA Supplicant Key Management - INITIALIZE */
2166	eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2167	eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2168
2169	/* Initialize driver interface and register driver event handler before
2170	 * L2 receive handler so that association events are processed before
2171	 * EAPOL-Key packets if both become available for the same select()
2172	 * call. */
2173	driver = iface->driver;
2174next_driver:
2175	if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2176		return -1;
2177
2178	wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2179	if (wpa_s->drv_priv == NULL) {
2180		const char *pos;
2181		pos = driver ? os_strchr(driver, ',') : NULL;
2182		if (pos) {
2183			wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2184				"driver interface - try next driver wrapper");
2185			driver = pos + 1;
2186			goto next_driver;
2187		}
2188		wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2189			"interface");
2190		return -1;
2191	}
2192	if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2193		wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2194			"driver_param '%s'", wpa_s->conf->driver_param);
2195		return -1;
2196	}
2197
2198	ifname = wpa_drv_get_ifname(wpa_s);
2199	if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2200		wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2201			"interface name with '%s'", ifname);
2202		os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2203	}
2204
2205	if (wpa_supplicant_init_wpa(wpa_s) < 0)
2206		return -1;
2207
2208	wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2209			  wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2210			  NULL);
2211	wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2212
2213	if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2214	    wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2215			     wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2216		wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2217			"dot11RSNAConfigPMKLifetime");
2218		return -1;
2219	}
2220
2221	if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2222	    wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2223			     wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2224		wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2225			"dot11RSNAConfigPMKReauthThreshold");
2226		return -1;
2227	}
2228
2229	if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2230	    wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2231			     wpa_s->conf->dot11RSNAConfigSATimeout)) {
2232		wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2233			"dot11RSNAConfigSATimeout");
2234		return -1;
2235	}
2236
2237	if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2238		wpa_s->drv_flags = capa.flags;
2239		if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2240			if (ieee80211_sta_init(wpa_s))
2241				return -1;
2242		}
2243		wpa_s->max_scan_ssids = capa.max_scan_ssids;
2244		wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2245		wpa_s->max_stations = capa.max_stations;
2246	}
2247	if (wpa_s->max_remain_on_chan == 0)
2248		wpa_s->max_remain_on_chan = 1000;
2249
2250	if (wpa_supplicant_driver_init(wpa_s) < 0)
2251		return -1;
2252
2253#ifdef CONFIG_TDLS
2254	if (wpa_tdls_init(wpa_s->wpa))
2255		return -1;
2256#endif /* CONFIG_TDLS */
2257
2258	if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2259	    wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2260		wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
2261		return -1;
2262	}
2263
2264	wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2265
2266	if (wpas_wps_init(wpa_s))
2267		return -1;
2268
2269	if (wpa_supplicant_init_eapol(wpa_s) < 0)
2270		return -1;
2271	wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2272
2273	wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2274	if (wpa_s->ctrl_iface == NULL) {
2275		wpa_printf(MSG_ERROR,
2276			   "Failed to initialize control interface '%s'.\n"
2277			   "You may have another wpa_supplicant process "
2278			   "already running or the file was\n"
2279			   "left by an unclean termination of wpa_supplicant "
2280			   "in which case you will need\n"
2281			   "to manually remove this file before starting "
2282			   "wpa_supplicant again.\n",
2283			   wpa_s->conf->ctrl_interface);
2284		return -1;
2285	}
2286
2287#ifdef CONFIG_P2P
2288	if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2289		wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
2290		return -1;
2291	}
2292#endif /* CONFIG_P2P */
2293
2294	if (wpa_bss_init(wpa_s) < 0)
2295		return -1;
2296
2297	return 0;
2298}
2299
2300
2301static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2302					int notify)
2303{
2304	if (wpa_s->drv_priv) {
2305		wpa_supplicant_deauthenticate(wpa_s,
2306					      WLAN_REASON_DEAUTH_LEAVING);
2307
2308		wpa_drv_set_countermeasures(wpa_s, 0);
2309		wpa_clear_keys(wpa_s, NULL);
2310	}
2311
2312	wpa_supplicant_cleanup(wpa_s);
2313
2314	if (notify)
2315		wpas_notify_iface_removed(wpa_s);
2316
2317	if (wpa_s->drv_priv)
2318		wpa_drv_deinit(wpa_s);
2319
2320/**
2321 * The wpa_drv_deinit call after sending TERMINATING to the framework causes
2322 * race condition with the start of hostapd.
2323 * This has been moved out of wpa_supplicant_cleanup(). Send the control
2324 * message and free config after the deinit.
2325 */
2326#ifdef ANDROID
2327	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
2328
2329	if (wpa_s->ctrl_iface) {
2330		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
2331		wpa_s->ctrl_iface = NULL;
2332	}
2333
2334	if (wpa_s->conf != NULL) {
2335		struct wpa_ssid *ssid;
2336		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2337			wpas_notify_network_removed(wpa_s, ssid);
2338		wpa_config_free(wpa_s->conf);
2339		wpa_s->conf = NULL;
2340	}
2341#endif
2342}
2343
2344
2345/**
2346 * wpa_supplicant_add_iface - Add a new network interface
2347 * @global: Pointer to global data from wpa_supplicant_init()
2348 * @iface: Interface configuration options
2349 * Returns: Pointer to the created interface or %NULL on failure
2350 *
2351 * This function is used to add new network interfaces for %wpa_supplicant.
2352 * This can be called before wpa_supplicant_run() to add interfaces before the
2353 * main event loop has been started. In addition, new interfaces can be added
2354 * dynamically while %wpa_supplicant is already running. This could happen,
2355 * e.g., when a hotplug network adapter is inserted.
2356 */
2357struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2358						 struct wpa_interface *iface)
2359{
2360	struct wpa_supplicant *wpa_s;
2361	struct wpa_interface t_iface;
2362	struct wpa_ssid *ssid;
2363
2364	if (global == NULL || iface == NULL)
2365		return NULL;
2366
2367	wpa_s = wpa_supplicant_alloc();
2368	if (wpa_s == NULL)
2369		return NULL;
2370
2371	wpa_s->global = global;
2372
2373	t_iface = *iface;
2374	if (global->params.override_driver) {
2375		wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2376			   "('%s' -> '%s')",
2377			   iface->driver, global->params.override_driver);
2378		t_iface.driver = global->params.override_driver;
2379	}
2380	if (global->params.override_ctrl_interface) {
2381		wpa_printf(MSG_DEBUG, "Override interface parameter: "
2382			   "ctrl_interface ('%s' -> '%s')",
2383			   iface->ctrl_interface,
2384			   global->params.override_ctrl_interface);
2385		t_iface.ctrl_interface =
2386			global->params.override_ctrl_interface;
2387	}
2388	if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2389		wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2390			   iface->ifname);
2391		wpa_supplicant_deinit_iface(wpa_s, 0);
2392		os_free(wpa_s);
2393		return NULL;
2394	}
2395
2396	/* Notify the control interfaces about new iface */
2397	if (wpas_notify_iface_added(wpa_s)) {
2398		wpa_supplicant_deinit_iface(wpa_s, 1);
2399		os_free(wpa_s);
2400		return NULL;
2401	}
2402
2403	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2404		wpas_notify_network_added(wpa_s, ssid);
2405
2406	wpa_s->next = global->ifaces;
2407	global->ifaces = wpa_s;
2408
2409	wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2410
2411	return wpa_s;
2412}
2413
2414
2415/**
2416 * wpa_supplicant_remove_iface - Remove a network interface
2417 * @global: Pointer to global data from wpa_supplicant_init()
2418 * @wpa_s: Pointer to the network interface to be removed
2419 * Returns: 0 if interface was removed, -1 if interface was not found
2420 *
2421 * This function can be used to dynamically remove network interfaces from
2422 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2423 * addition, this function is used to remove all remaining interfaces when
2424 * %wpa_supplicant is terminated.
2425 */
2426int wpa_supplicant_remove_iface(struct wpa_global *global,
2427				struct wpa_supplicant *wpa_s)
2428{
2429	struct wpa_supplicant *prev;
2430
2431	/* Remove interface from the global list of interfaces */
2432	prev = global->ifaces;
2433	if (prev == wpa_s) {
2434		global->ifaces = wpa_s->next;
2435	} else {
2436		while (prev && prev->next != wpa_s)
2437			prev = prev->next;
2438		if (prev == NULL)
2439			return -1;
2440		prev->next = wpa_s->next;
2441	}
2442
2443	wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2444
2445	if (global->p2p_group_formation == wpa_s)
2446		global->p2p_group_formation = NULL;
2447	wpa_supplicant_deinit_iface(wpa_s, 1);
2448	os_free(wpa_s);
2449
2450	return 0;
2451}
2452
2453
2454/**
2455 * wpa_supplicant_get_eap_mode - Get the current EAP mode
2456 * @wpa_s: Pointer to the network interface
2457 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
2458 */
2459const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
2460{
2461	const char *eapol_method;
2462
2463        if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
2464            wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2465		return "NO-EAP";
2466	}
2467
2468	eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
2469	if (eapol_method == NULL)
2470		return "UNKNOWN-EAP";
2471
2472	return eapol_method;
2473}
2474
2475
2476/**
2477 * wpa_supplicant_get_iface - Get a new network interface
2478 * @global: Pointer to global data from wpa_supplicant_init()
2479 * @ifname: Interface name
2480 * Returns: Pointer to the interface or %NULL if not found
2481 */
2482struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2483						 const char *ifname)
2484{
2485	struct wpa_supplicant *wpa_s;
2486
2487	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2488		if (os_strcmp(wpa_s->ifname, ifname) == 0)
2489			return wpa_s;
2490	}
2491	return NULL;
2492}
2493
2494
2495#ifndef CONFIG_NO_WPA_MSG
2496static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
2497{
2498	struct wpa_supplicant *wpa_s = ctx;
2499	if (wpa_s == NULL)
2500		return NULL;
2501	return wpa_s->ifname;
2502}
2503#endif /* CONFIG_NO_WPA_MSG */
2504
2505
2506/**
2507 * wpa_supplicant_init - Initialize %wpa_supplicant
2508 * @params: Parameters for %wpa_supplicant
2509 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2510 *
2511 * This function is used to initialize %wpa_supplicant. After successful
2512 * initialization, the returned data pointer can be used to add and remove
2513 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2514 */
2515struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2516{
2517	struct wpa_global *global;
2518	int ret, i;
2519
2520	if (params == NULL)
2521		return NULL;
2522
2523#ifndef CONFIG_NO_WPA_MSG
2524	wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
2525#endif /* CONFIG_NO_WPA_MSG */
2526
2527	wpa_debug_open_file(params->wpa_debug_file_path);
2528	if (params->wpa_debug_syslog)
2529		wpa_debug_open_syslog();
2530
2531	ret = eap_register_methods();
2532	if (ret) {
2533		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2534		if (ret == -2)
2535			wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2536				   "the same EAP type.");
2537		return NULL;
2538	}
2539
2540	global = os_zalloc(sizeof(*global));
2541	if (global == NULL)
2542		return NULL;
2543	dl_list_init(&global->p2p_srv_bonjour);
2544	dl_list_init(&global->p2p_srv_upnp);
2545	global->params.daemonize = params->daemonize;
2546	global->params.wait_for_monitor = params->wait_for_monitor;
2547	global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2548	if (params->pid_file)
2549		global->params.pid_file = os_strdup(params->pid_file);
2550	if (params->ctrl_interface)
2551		global->params.ctrl_interface =
2552			os_strdup(params->ctrl_interface);
2553	if (params->override_driver)
2554		global->params.override_driver =
2555			os_strdup(params->override_driver);
2556	if (params->override_ctrl_interface)
2557		global->params.override_ctrl_interface =
2558			os_strdup(params->override_ctrl_interface);
2559	wpa_debug_level = global->params.wpa_debug_level =
2560		params->wpa_debug_level;
2561	wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2562		params->wpa_debug_show_keys;
2563	wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2564		params->wpa_debug_timestamp;
2565
2566	wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2567
2568	if (eloop_init()) {
2569		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2570		wpa_supplicant_deinit(global);
2571		return NULL;
2572	}
2573
2574	random_init(params->entropy_file);
2575
2576	global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2577	if (global->ctrl_iface == NULL) {
2578		wpa_supplicant_deinit(global);
2579		return NULL;
2580	}
2581
2582	if (wpas_notify_supplicant_initialized(global)) {
2583		wpa_supplicant_deinit(global);
2584		return NULL;
2585	}
2586
2587	for (i = 0; wpa_drivers[i]; i++)
2588		global->drv_count++;
2589	if (global->drv_count == 0) {
2590		wpa_printf(MSG_ERROR, "No drivers enabled");
2591		wpa_supplicant_deinit(global);
2592		return NULL;
2593	}
2594	global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2595	if (global->drv_priv == NULL) {
2596		wpa_supplicant_deinit(global);
2597		return NULL;
2598	}
2599	for (i = 0; wpa_drivers[i]; i++) {
2600		if (!wpa_drivers[i]->global_init)
2601			continue;
2602		global->drv_priv[i] = wpa_drivers[i]->global_init();
2603		if (global->drv_priv[i] == NULL) {
2604			wpa_printf(MSG_ERROR, "Failed to initialize driver "
2605				   "'%s'", wpa_drivers[i]->name);
2606			wpa_supplicant_deinit(global);
2607			return NULL;
2608		}
2609	}
2610
2611	return global;
2612}
2613
2614
2615/**
2616 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2617 * @global: Pointer to global data from wpa_supplicant_init()
2618 * Returns: 0 after successful event loop run, -1 on failure
2619 *
2620 * This function starts the main event loop and continues running as long as
2621 * there are any remaining events. In most cases, this function is running as
2622 * long as the %wpa_supplicant process in still in use.
2623 */
2624int wpa_supplicant_run(struct wpa_global *global)
2625{
2626	struct wpa_supplicant *wpa_s;
2627
2628	if (global->params.daemonize &&
2629	    wpa_supplicant_daemon(global->params.pid_file))
2630		return -1;
2631
2632	if (global->params.wait_for_monitor) {
2633		for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2634			if (wpa_s->ctrl_iface)
2635				wpa_supplicant_ctrl_iface_wait(
2636					wpa_s->ctrl_iface);
2637	}
2638
2639	eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2640	eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2641
2642	eloop_run();
2643
2644	return 0;
2645}
2646
2647
2648/**
2649 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2650 * @global: Pointer to global data from wpa_supplicant_init()
2651 *
2652 * This function is called to deinitialize %wpa_supplicant and to free all
2653 * allocated resources. Remaining network interfaces will also be removed.
2654 */
2655void wpa_supplicant_deinit(struct wpa_global *global)
2656{
2657	int i;
2658
2659	if (global == NULL)
2660		return;
2661#ifdef CONFIG_P2P
2662	wpas_p2p_deinit_global(global);
2663#endif /* CONFIG_P2P */
2664
2665	while (global->ifaces)
2666		wpa_supplicant_remove_iface(global, global->ifaces);
2667
2668	if (global->ctrl_iface)
2669		wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2670
2671	wpas_notify_supplicant_deinitialized(global);
2672
2673	eap_peer_unregister_methods();
2674#ifdef CONFIG_AP
2675	eap_server_unregister_methods();
2676#endif /* CONFIG_AP */
2677
2678	for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2679		if (!global->drv_priv[i])
2680			continue;
2681		wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2682	}
2683	os_free(global->drv_priv);
2684
2685	random_deinit();
2686
2687	eloop_destroy();
2688
2689	if (global->params.pid_file) {
2690		os_daemonize_terminate(global->params.pid_file);
2691		os_free(global->params.pid_file);
2692	}
2693	os_free(global->params.ctrl_interface);
2694	os_free(global->params.override_driver);
2695	os_free(global->params.override_ctrl_interface);
2696
2697	os_free(global);
2698	wpa_debug_close_syslog();
2699	wpa_debug_close_file();
2700}
2701
2702
2703void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
2704{
2705	if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
2706	    wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2707		char country[3];
2708		country[0] = wpa_s->conf->country[0];
2709		country[1] = wpa_s->conf->country[1];
2710		country[2] = '\0';
2711		if (wpa_drv_set_country(wpa_s, country) < 0) {
2712			wpa_printf(MSG_ERROR, "Failed to set country code "
2713				   "'%s'", country);
2714		}
2715	}
2716
2717#ifdef CONFIG_WPS
2718	wpas_wps_update_config(wpa_s);
2719#endif /* CONFIG_WPS */
2720
2721#ifdef CONFIG_P2P
2722	wpas_p2p_update_config(wpa_s);
2723#endif /* CONFIG_P2P */
2724
2725	wpa_s->conf->changed_parameters = 0;
2726}
2727
2728
2729void ieee80211_sta_free_hw_features(struct hostapd_hw_modes *hw_features,
2730				    size_t num_hw_features)
2731{
2732	size_t i;
2733
2734	if (hw_features == NULL)
2735		return;
2736
2737	for (i = 0; i < num_hw_features; i++) {
2738		os_free(hw_features[i].channels);
2739		os_free(hw_features[i].rates);
2740	}
2741
2742	os_free(hw_features);
2743}
2744
2745
2746static void add_freq(int *freqs, int *num_freqs, int freq)
2747{
2748	int i;
2749
2750	for (i = 0; i < *num_freqs; i++) {
2751		if (freqs[i] == freq)
2752			return;
2753	}
2754
2755	freqs[*num_freqs] = freq;
2756	(*num_freqs)++;
2757}
2758
2759
2760static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
2761{
2762	struct wpa_bss *bss, *cbss;
2763	const int max_freqs = 10;
2764	int *freqs;
2765	int num_freqs = 0;
2766
2767	freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
2768	if (freqs == NULL)
2769		return NULL;
2770
2771	cbss = wpa_s->current_bss;
2772
2773	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2774		if (bss == cbss)
2775			continue;
2776		if (bss->ssid_len == cbss->ssid_len &&
2777		    os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
2778		    wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
2779			add_freq(freqs, &num_freqs, bss->freq);
2780			if (num_freqs == max_freqs)
2781				break;
2782		}
2783	}
2784
2785	if (num_freqs == 0) {
2786		os_free(freqs);
2787		freqs = NULL;
2788	}
2789
2790	return freqs;
2791}
2792
2793
2794void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
2795{
2796	int timeout;
2797	int count;
2798	int *freqs = NULL;
2799
2800	/*
2801	 * Add the failed BSSID into the blacklist and speed up next scan
2802	 * attempt if there could be other APs that could accept association.
2803	 * The current blacklist count indicates how many times we have tried
2804	 * connecting to this AP and multiple attempts mean that other APs are
2805	 * either not available or has already been tried, so that we can start
2806	 * increasing the delay here to avoid constant scanning.
2807	 */
2808	count = wpa_blacklist_add(wpa_s, bssid);
2809	if (count == 1 && wpa_s->current_bss) {
2810		/*
2811		 * This BSS was not in the blacklist before. If there is
2812		 * another BSS available for the same ESS, we should try that
2813		 * next. Otherwise, we may as well try this one once more
2814		 * before allowing other, likely worse, ESSes to be considered.
2815		 */
2816		freqs = get_bss_freqs_in_ess(wpa_s);
2817		if (freqs) {
2818			wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
2819				"has been seen; try it next");
2820			wpa_blacklist_add(wpa_s, bssid);
2821			/*
2822			 * On the next scan, go through only the known channels
2823			 * used in this ESS based on previous scans to speed up
2824			 * common load balancing use case.
2825			 */
2826			os_free(wpa_s->next_scan_freqs);
2827			wpa_s->next_scan_freqs = freqs;
2828		}
2829	}
2830
2831	switch (count) {
2832	case 1:
2833		timeout = 100;
2834		break;
2835	case 2:
2836		timeout = 500;
2837		break;
2838	case 3:
2839		timeout = 1000;
2840		break;
2841	default:
2842		timeout = 5000;
2843	}
2844
2845	/*
2846	 * TODO: if more than one possible AP is available in scan results,
2847	 * could try the other ones before requesting a new scan.
2848	 */
2849	wpa_supplicant_req_scan(wpa_s, timeout / 1000,
2850				1000 * (timeout % 1000));
2851}
2852