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