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