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