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