wpa_supplicant.c
Go to the documentation of this file.
00001 /*
00002  * WPA Supplicant
00003  * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  *
00014  * This file implements functions for registering and unregistering
00015  * %wpa_supplicant interfaces. In addition, this file contains number of
00016  * functions for managing network connections.
00017  */
00018 
00019 #include "includes.h"
00020 
00021 #include "common.h"
00022 #include "eapol_supp/eapol_supp_sm.h"
00023 #include "eap_peer/eap.h"
00024 #include "eap_server/eap_methods.h"
00025 #include "rsn_supp/wpa.h"
00026 #include "eloop.h"
00027 #include "config.h"
00028 #include "l2_packet/l2_packet.h"
00029 #include "wpa_supplicant_i.h"
00030 #include "driver_i.h"
00031 #include "ctrl_iface.h"
00032 #include "pcsc_funcs.h"
00033 #include "common/version.h"
00034 #include "rsn_supp/preauth.h"
00035 #include "rsn_supp/pmksa_cache.h"
00036 #include "common/wpa_ctrl.h"
00037 #include "mlme.h"
00038 #include "common/ieee802_11_defs.h"
00039 #include "blacklist.h"
00040 #include "wpas_glue.h"
00041 #include "wps_supplicant.h"
00042 #include "ibss_rsn.h"
00043 #include "sme.h"
00044 #include "ap.h"
00045 #include "notify.h"
00046 #include "bgscan.h"
00047 #include "bss.h"
00048 #include "scan.h"
00049 
00050 const char *wpa_supplicant_version =
00051 "wpa_supplicant v" VERSION_STR "\n"
00052 "Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi> and contributors";
00053 
00054 const char *wpa_supplicant_license =
00055 "This program is free software. You can distribute it and/or modify it\n"
00056 "under the terms of the GNU General Public License version 2.\n"
00057 "\n"
00058 "Alternatively, this software may be distributed under the terms of the\n"
00059 "BSD license. See README and COPYING for more details.\n"
00060 #ifdef EAP_TLS_OPENSSL
00061 "\nThis product includes software developed by the OpenSSL Project\n"
00062 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
00063 #endif /* EAP_TLS_OPENSSL */
00064 ;
00065 
00066 #ifndef CONFIG_NO_STDOUT_DEBUG
00067 /* Long text divided into parts in order to fit in C89 strings size limits. */
00068 const char *wpa_supplicant_full_license1 =
00069 "This program is free software; you can redistribute it and/or modify\n"
00070 "it under the terms of the GNU General Public License version 2 as\n"
00071 "published by the Free Software Foundation.\n"
00072 "\n"
00073 "This program is distributed in the hope that it will be useful,\n"
00074 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00075 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
00076 "GNU General Public License for more details.\n"
00077 "\n";
00078 const char *wpa_supplicant_full_license2 =
00079 "You should have received a copy of the GNU General Public License\n"
00080 "along with this program; if not, write to the Free Software\n"
00081 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n"
00082 "\n"
00083 "Alternatively, this software may be distributed under the terms of the\n"
00084 "BSD license.\n"
00085 "\n"
00086 "Redistribution and use in source and binary forms, with or without\n"
00087 "modification, are permitted provided that the following conditions are\n"
00088 "met:\n"
00089 "\n";
00090 const char *wpa_supplicant_full_license3 =
00091 "1. Redistributions of source code must retain the above copyright\n"
00092 "   notice, this list of conditions and the following disclaimer.\n"
00093 "\n"
00094 "2. Redistributions in binary form must reproduce the above copyright\n"
00095 "   notice, this list of conditions and the following disclaimer in the\n"
00096 "   documentation and/or other materials provided with the distribution.\n"
00097 "\n";
00098 const char *wpa_supplicant_full_license4 =
00099 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
00100 "   names of its contributors may be used to endorse or promote products\n"
00101 "   derived from this software without specific prior written permission.\n"
00102 "\n"
00103 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
00104 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
00105 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
00106 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
00107 const char *wpa_supplicant_full_license5 =
00108 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
00109 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
00110 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
00111 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
00112 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
00113 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
00114 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
00115 "\n";
00116 #endif /* CONFIG_NO_STDOUT_DEBUG */
00117 
00118 extern int wpa_debug_level;
00119 extern int wpa_debug_show_keys;
00120 extern int wpa_debug_timestamp;
00121 extern struct wpa_driver_ops *wpa_drivers[];
00122 
00123 /* Configure default/group WEP keys for static WEP */
00124 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
00125 {
00126         int i, set = 0;
00127 
00128         for (i = 0; i < NUM_WEP_KEYS; i++) {
00129                 if (ssid->wep_key_len[i] == 0)
00130                         continue;
00131 
00132                 set = 1;
00133                 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
00134                                 (u8 *) "\xff\xff\xff\xff\xff\xff",
00135                                 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
00136                                 ssid->wep_key[i], ssid->wep_key_len[i]);
00137         }
00138 
00139         return set;
00140 }
00141 
00142 
00143 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
00144                                            struct wpa_ssid *ssid)
00145 {
00146         u8 key[32];
00147         size_t keylen;
00148         enum wpa_alg alg;
00149         u8 seq[6] = { 0 };
00150 
00151         /* IBSS/WPA-None uses only one key (Group) for both receiving and
00152          * sending unicast and multicast packets. */
00153 
00154         if (ssid->mode != WPAS_MODE_IBSS) {
00155                 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
00156                            "for WPA-None", ssid->mode);
00157                 return -1;
00158         }
00159 
00160         if (!ssid->psk_set) {
00161                 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
00162                 return -1;
00163         }
00164 
00165         switch (wpa_s->group_cipher) {
00166         case WPA_CIPHER_CCMP:
00167                 os_memcpy(key, ssid->psk, 16);
00168                 keylen = 16;
00169                 alg = WPA_ALG_CCMP;
00170                 break;
00171         case WPA_CIPHER_TKIP:
00172                 /* WPA-None uses the same Michael MIC key for both TX and RX */
00173                 os_memcpy(key, ssid->psk, 16 + 8);
00174                 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
00175                 keylen = 32;
00176                 alg = WPA_ALG_TKIP;
00177                 break;
00178         default:
00179                 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
00180                            "WPA-None", wpa_s->group_cipher);
00181                 return -1;
00182         }
00183 
00184         /* TODO: should actually remember the previously used seq#, both for TX
00185          * and RX from each STA.. */
00186 
00187         return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
00188                                0, 1, seq, 6, key, keylen);
00189 }
00190 
00191 
00192 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
00193 {
00194         struct wpa_supplicant *wpa_s = eloop_ctx;
00195         const u8 *bssid = wpa_s->bssid;
00196         if (is_zero_ether_addr(bssid))
00197                 bssid = wpa_s->pending_bssid;
00198         wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
00199                 MAC2STR(bssid));
00200         wpa_blacklist_add(wpa_s, bssid);
00201         wpa_sm_notify_disassoc(wpa_s->wpa);
00202         wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
00203         wpa_s->reassociate = 1;
00204         wpa_supplicant_req_scan(wpa_s, 0, 0);
00205 }
00206 
00207 
00217 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
00218                                      int sec, int usec)
00219 {
00220         if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
00221             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
00222                 return;
00223 
00224         wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
00225                 "%d usec", sec, usec);
00226         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
00227         eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
00228 }
00229 
00230 
00239 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
00240 {
00241         wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
00242         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
00243         wpa_blacklist_del(wpa_s, wpa_s->bssid);
00244 }
00245 
00246 
00254 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
00255 {
00256 #ifdef IEEE8021X_EAPOL
00257         struct eapol_config eapol_conf;
00258         struct wpa_ssid *ssid = wpa_s->current_ssid;
00259 
00260 #ifdef CONFIG_IBSS_RSN
00261         if (ssid->mode == WPAS_MODE_IBSS &&
00262             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
00263             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
00264                 /*
00265                  * RSN IBSS authentication is per-STA and we can disable the
00266                  * per-BSSID EAPOL authentication.
00267                  */
00268                 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
00269                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
00270                 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
00271                 return;
00272         }
00273 #endif /* CONFIG_IBSS_RSN */
00274 
00275         eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
00276         eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
00277 
00278         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
00279             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
00280                 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
00281         else
00282                 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
00283 
00284         os_memset(&eapol_conf, 0, sizeof(eapol_conf));
00285         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
00286                 eapol_conf.accept_802_1x_keys = 1;
00287                 eapol_conf.required_keys = 0;
00288                 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
00289                         eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
00290                 }
00291                 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
00292                         eapol_conf.required_keys |=
00293                                 EAPOL_REQUIRE_KEY_BROADCAST;
00294                 }
00295 
00296                 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
00297                         eapol_conf.required_keys = 0;
00298         }
00299         if (wpa_s->conf)
00300                 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
00301         eapol_conf.workaround = ssid->eap_workaround;
00302         eapol_conf.eap_disabled =
00303                 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
00304                 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
00305                 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
00306         eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
00307 #endif /* IEEE8021X_EAPOL */
00308 }
00309 
00310 
00320 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
00321                                        struct wpa_ssid *ssid)
00322 {
00323         int i;
00324 
00325         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
00326                 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
00327         else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
00328                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
00329         else
00330                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
00331         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
00332         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
00333         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
00334         wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
00335         wpa_s->group_cipher = WPA_CIPHER_NONE;
00336         wpa_s->mgmt_group_cipher = 0;
00337 
00338         for (i = 0; i < NUM_WEP_KEYS; i++) {
00339                 if (ssid->wep_key_len[i] > 5) {
00340                         wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
00341                         wpa_s->group_cipher = WPA_CIPHER_WEP104;
00342                         break;
00343                 } else if (ssid->wep_key_len[i] > 0) {
00344                         wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
00345                         wpa_s->group_cipher = WPA_CIPHER_WEP40;
00346                         break;
00347                 }
00348         }
00349 
00350         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
00351         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
00352         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
00353                          wpa_s->pairwise_cipher);
00354         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
00355 #ifdef CONFIG_IEEE80211W
00356         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
00357                          wpa_s->mgmt_group_cipher);
00358 #endif /* CONFIG_IEEE80211W */
00359 
00360         pmksa_cache_clear_current(wpa_s->wpa);
00361 }
00362 
00363 
00364 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
00365 {
00366         bgscan_deinit(wpa_s);
00367         scard_deinit(wpa_s->scard);
00368         wpa_s->scard = NULL;
00369         wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
00370         eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
00371         l2_packet_deinit(wpa_s->l2);
00372         wpa_s->l2 = NULL;
00373         if (wpa_s->l2_br) {
00374                 l2_packet_deinit(wpa_s->l2_br);
00375                 wpa_s->l2_br = NULL;
00376         }
00377 
00378         if (wpa_s->ctrl_iface) {
00379                 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
00380                 wpa_s->ctrl_iface = NULL;
00381         }
00382         if (wpa_s->conf != NULL) {
00383                 struct wpa_ssid *ssid;
00384                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
00385                         wpas_notify_network_removed(wpa_s, ssid);
00386                 wpa_config_free(wpa_s->conf);
00387                 wpa_s->conf = NULL;
00388         }
00389 
00390         os_free(wpa_s->confname);
00391         wpa_s->confname = NULL;
00392 
00393         wpa_sm_set_eapol(wpa_s->wpa, NULL);
00394         eapol_sm_deinit(wpa_s->eapol);
00395         wpa_s->eapol = NULL;
00396 
00397         rsn_preauth_deinit(wpa_s->wpa);
00398 
00399         pmksa_candidate_free(wpa_s->wpa);
00400         wpa_sm_deinit(wpa_s->wpa);
00401         wpa_s->wpa = NULL;
00402         wpa_blacklist_clear(wpa_s);
00403 
00404         wpa_bss_deinit(wpa_s);
00405 
00406         wpa_supplicant_cancel_scan(wpa_s);
00407         wpa_supplicant_cancel_auth_timeout(wpa_s);
00408 
00409         ieee80211_sta_deinit(wpa_s);
00410 
00411         wpas_wps_deinit(wpa_s);
00412 
00413         wpabuf_free(wpa_s->pending_eapol_rx);
00414         wpa_s->pending_eapol_rx = NULL;
00415 
00416 #ifdef CONFIG_IBSS_RSN
00417         ibss_rsn_deinit(wpa_s->ibss_rsn);
00418         wpa_s->ibss_rsn = NULL;
00419 #endif /* CONFIG_IBSS_RSN */
00420 
00421 #ifdef CONFIG_SME
00422         os_free(wpa_s->sme.ft_ies);
00423         wpa_s->sme.ft_ies = NULL;
00424         wpa_s->sme.ft_ies_len = 0;
00425 #endif /* CONFIG_SME */
00426 
00427 #ifdef CONFIG_AP
00428         wpa_supplicant_ap_deinit(wpa_s);
00429 #endif /* CONFIG_AP */
00430 }
00431 
00432 
00441 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
00442 {
00443         u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
00444 
00445         if (wpa_s->keys_cleared) {
00446                 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
00447                  * timing issues with keys being cleared just before new keys
00448                  * are set or just after association or something similar. This
00449                  * shows up in group key handshake failing often because of the
00450                  * client not receiving the first encrypted packets correctly.
00451                  * Skipping some of the extra key clearing steps seems to help
00452                  * in completing group key handshake more reliably. */
00453                 wpa_printf(MSG_DEBUG, "No keys have been configured - "
00454                            "skip key clearing");
00455                 return;
00456         }
00457 
00458         /* MLME-DELETEKEYS.request */
00459         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
00460         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
00461         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
00462         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
00463 #ifdef CONFIG_IEEE80211W
00464         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
00465         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
00466 #endif /* CONFIG_IEEE80211W */
00467         if (addr) {
00468                 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
00469                                 0);
00470                 /* MLME-SETPROTECTION.request(None) */
00471                 wpa_drv_mlme_setprotection(
00472                         wpa_s, addr,
00473                         MLME_SETPROTECTION_PROTECT_TYPE_NONE,
00474                         MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
00475         }
00476         wpa_s->keys_cleared = 1;
00477 }
00478 
00479 
00485 const char * wpa_supplicant_state_txt(enum wpa_states state)
00486 {
00487         switch (state) {
00488         case WPA_DISCONNECTED:
00489                 return "DISCONNECTED";
00490         case WPA_INACTIVE:
00491                 return "INACTIVE";
00492         case WPA_SCANNING:
00493                 return "SCANNING";
00494         case WPA_AUTHENTICATING:
00495                 return "AUTHENTICATING";
00496         case WPA_ASSOCIATING:
00497                 return "ASSOCIATING";
00498         case WPA_ASSOCIATED:
00499                 return "ASSOCIATED";
00500         case WPA_4WAY_HANDSHAKE:
00501                 return "4WAY_HANDSHAKE";
00502         case WPA_GROUP_HANDSHAKE:
00503                 return "GROUP_HANDSHAKE";
00504         case WPA_COMPLETED:
00505                 return "COMPLETED";
00506         default:
00507                 return "UNKNOWN";
00508         }
00509 }
00510 
00511 
00520 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
00521                               enum wpa_states state)
00522 {
00523         enum wpa_states old_state = wpa_s->wpa_state;
00524 
00525         wpa_printf(MSG_DEBUG, "State: %s -> %s",
00526                    wpa_supplicant_state_txt(wpa_s->wpa_state),
00527                    wpa_supplicant_state_txt(state));
00528 
00529         if (state != WPA_SCANNING)
00530                 wpa_supplicant_notify_scanning(wpa_s, 0);
00531 
00532         if (state == WPA_COMPLETED && wpa_s->new_connection) {
00533 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
00534                 struct wpa_ssid *ssid = wpa_s->current_ssid;
00535                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
00536                         MACSTR " completed %s [id=%d id_str=%s]",
00537                         MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
00538                         "(reauth)" : "(auth)",
00539                         ssid ? ssid->id : -1,
00540                         ssid && ssid->id_str ? ssid->id_str : "");
00541 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
00542                 wpa_s->new_connection = 0;
00543                 wpa_s->reassociated_connection = 1;
00544                 wpa_drv_set_operstate(wpa_s, 1);
00545                 wpa_s->after_wps = 0;
00546         } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
00547                    state == WPA_ASSOCIATED) {
00548                 wpa_s->new_connection = 1;
00549                 wpa_drv_set_operstate(wpa_s, 0);
00550         }
00551         wpa_s->wpa_state = state;
00552 
00553         if (wpa_s->wpa_state != old_state)
00554                 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
00555 }
00556 
00557 
00558 void wpa_supplicant_terminate_proc(struct wpa_global *global)
00559 {
00560         int pending = 0;
00561 #ifdef CONFIG_WPS
00562         struct wpa_supplicant *wpa_s = global->ifaces;
00563         while (wpa_s) {
00564                 if (wpas_wps_terminate_pending(wpa_s) == 1)
00565                         pending = 1;
00566                 wpa_s = wpa_s->next;
00567         }
00568 #endif /* CONFIG_WPS */
00569         if (pending)
00570                 return;
00571         eloop_terminate();
00572 }
00573 
00574 
00575 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
00576 {
00577         struct wpa_global *global = signal_ctx;
00578         struct wpa_supplicant *wpa_s;
00579         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
00580                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
00581                         "received", sig);
00582         }
00583         wpa_supplicant_terminate_proc(global);
00584 }
00585 
00586 
00587 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
00588 {
00589         enum wpa_states old_state = wpa_s->wpa_state;
00590 
00591         wpa_s->pairwise_cipher = 0;
00592         wpa_s->group_cipher = 0;
00593         wpa_s->mgmt_group_cipher = 0;
00594         wpa_s->key_mgmt = 0;
00595         wpa_s->wpa_state = WPA_DISCONNECTED;
00596 
00597         if (wpa_s->wpa_state != old_state)
00598                 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
00599 }
00600 
00601 
00613 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
00614 {
00615         struct wpa_config *conf;
00616         struct wpa_ssid *old_ssid;
00617         int reconf_ctrl;
00618         int old_ap_scan;
00619 
00620         if (wpa_s->confname == NULL)
00621                 return -1;
00622         conf = wpa_config_read(wpa_s->confname);
00623         if (conf == NULL) {
00624                 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
00625                         "file '%s' - exiting", wpa_s->confname);
00626                 return -1;
00627         }
00628 
00629         reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
00630                 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
00631                     os_strcmp(conf->ctrl_interface,
00632                               wpa_s->conf->ctrl_interface) != 0);
00633 
00634         if (reconf_ctrl && wpa_s->ctrl_iface) {
00635                 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
00636                 wpa_s->ctrl_iface = NULL;
00637         }
00638 
00639         eapol_sm_invalidate_cached_session(wpa_s->eapol);
00640         old_ssid = wpa_s->current_ssid;
00641         wpa_s->current_ssid = NULL;
00642         if (old_ssid != wpa_s->current_ssid)
00643                 wpas_notify_network_changed(wpa_s);
00644 
00645         /*
00646          * TODO: should notify EAPOL SM about changes in opensc_engine_path,
00647          * pkcs11_engine_path, pkcs11_module_path.
00648          */
00649         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
00650                 /*
00651                  * Clear forced success to clear EAP state for next
00652                  * authentication.
00653                  */
00654                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
00655         }
00656         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
00657         wpa_sm_set_config(wpa_s->wpa, NULL);
00658         wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
00659         rsn_preauth_deinit(wpa_s->wpa);
00660 
00661         old_ap_scan = wpa_s->conf->ap_scan;
00662         wpa_config_free(wpa_s->conf);
00663         wpa_s->conf = conf;
00664         if (old_ap_scan != wpa_s->conf->ap_scan)
00665                 wpas_notify_ap_scan_changed(wpa_s);
00666 
00667         if (reconf_ctrl)
00668                 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
00669 
00670         wpa_supplicant_clear_status(wpa_s);
00671         wpa_s->reassociate = 1;
00672         wpa_supplicant_req_scan(wpa_s, 0, 0);
00673         wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
00674         return 0;
00675 }
00676 
00677 
00678 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
00679 {
00680         struct wpa_global *global = signal_ctx;
00681         struct wpa_supplicant *wpa_s;
00682         wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
00683         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
00684                 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
00685                         wpa_supplicant_terminate_proc(global);
00686                 }
00687         }
00688 }
00689 
00690 
00691 enum wpa_cipher cipher_suite2driver(int cipher)
00692 {
00693         switch (cipher) {
00694         case WPA_CIPHER_NONE:
00695                 return CIPHER_NONE;
00696         case WPA_CIPHER_WEP40:
00697                 return CIPHER_WEP40;
00698         case WPA_CIPHER_WEP104:
00699                 return CIPHER_WEP104;
00700         case WPA_CIPHER_CCMP:
00701                 return CIPHER_CCMP;
00702         case WPA_CIPHER_TKIP:
00703         default:
00704                 return CIPHER_TKIP;
00705         }
00706 }
00707 
00708 
00709 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
00710 {
00711         switch (key_mgmt) {
00712         case WPA_KEY_MGMT_NONE:
00713                 return KEY_MGMT_NONE;
00714         case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
00715                 return KEY_MGMT_802_1X_NO_WPA;
00716         case WPA_KEY_MGMT_IEEE8021X:
00717                 return KEY_MGMT_802_1X;
00718         case WPA_KEY_MGMT_WPA_NONE:
00719                 return KEY_MGMT_WPA_NONE;
00720         case WPA_KEY_MGMT_FT_IEEE8021X:
00721                 return KEY_MGMT_FT_802_1X;
00722         case WPA_KEY_MGMT_FT_PSK:
00723                 return KEY_MGMT_FT_PSK;
00724         case WPA_KEY_MGMT_IEEE8021X_SHA256:
00725                 return KEY_MGMT_802_1X_SHA256;
00726         case WPA_KEY_MGMT_PSK_SHA256:
00727                 return KEY_MGMT_PSK_SHA256;
00728         case WPA_KEY_MGMT_WPS:
00729                 return KEY_MGMT_WPS;
00730         case WPA_KEY_MGMT_PSK:
00731         default:
00732                 return KEY_MGMT_PSK;
00733         }
00734 }
00735 
00736 
00737 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
00738                                          struct wpa_ssid *ssid,
00739                                          struct wpa_ie_data *ie)
00740 {
00741         int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
00742         if (ret) {
00743                 if (ret == -2) {
00744                         wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
00745                                 "from association info");
00746                 }
00747                 return -1;
00748         }
00749 
00750         wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
00751                    "suites");
00752         if (!(ie->group_cipher & ssid->group_cipher)) {
00753                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
00754                         "cipher 0x%x (mask 0x%x) - reject",
00755                         ie->group_cipher, ssid->group_cipher);
00756                 return -1;
00757         }
00758         if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
00759                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
00760                         "cipher 0x%x (mask 0x%x) - reject",
00761                         ie->pairwise_cipher, ssid->pairwise_cipher);
00762                 return -1;
00763         }
00764         if (!(ie->key_mgmt & ssid->key_mgmt)) {
00765                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
00766                         "management 0x%x (mask 0x%x) - reject",
00767                         ie->key_mgmt, ssid->key_mgmt);
00768                 return -1;
00769         }
00770 
00771 #ifdef CONFIG_IEEE80211W
00772         if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
00773             ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
00774                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
00775                         "that does not support management frame protection - "
00776                         "reject");
00777                 return -1;
00778         }
00779 #endif /* CONFIG_IEEE80211W */
00780 
00781         return 0;
00782 }
00783 
00784 
00799 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
00800                               struct wpa_bss *bss, struct wpa_ssid *ssid,
00801                               u8 *wpa_ie, size_t *wpa_ie_len)
00802 {
00803         struct wpa_ie_data ie;
00804         int sel, proto;
00805         const u8 *bss_wpa, *bss_rsn;
00806 
00807         if (bss) {
00808                 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
00809                 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
00810         } else
00811                 bss_wpa = bss_rsn = NULL;
00812 
00813         if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
00814             wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
00815             (ie.group_cipher & ssid->group_cipher) &&
00816             (ie.pairwise_cipher & ssid->pairwise_cipher) &&
00817             (ie.key_mgmt & ssid->key_mgmt)) {
00818                 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
00819                 proto = WPA_PROTO_RSN;
00820         } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
00821                    wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
00822                    (ie.group_cipher & ssid->group_cipher) &&
00823                    (ie.pairwise_cipher & ssid->pairwise_cipher) &&
00824                    (ie.key_mgmt & ssid->key_mgmt)) {
00825                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
00826                 proto = WPA_PROTO_WPA;
00827         } else if (bss) {
00828                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
00829                 return -1;
00830         } else {
00831                 if (ssid->proto & WPA_PROTO_RSN)
00832                         proto = WPA_PROTO_RSN;
00833                 else
00834                         proto = WPA_PROTO_WPA;
00835                 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
00836                         os_memset(&ie, 0, sizeof(ie));
00837                         ie.group_cipher = ssid->group_cipher;
00838                         ie.pairwise_cipher = ssid->pairwise_cipher;
00839                         ie.key_mgmt = ssid->key_mgmt;
00840 #ifdef CONFIG_IEEE80211W
00841                         ie.mgmt_group_cipher =
00842                                 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
00843                                 WPA_CIPHER_AES_128_CMAC : 0;
00844 #endif /* CONFIG_IEEE80211W */
00845                         wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
00846                                    "on configuration");
00847                 } else
00848                         proto = ie.proto;
00849         }
00850 
00851         wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
00852                    "pairwise %d key_mgmt %d proto %d",
00853                    ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
00854 #ifdef CONFIG_IEEE80211W
00855         if (ssid->ieee80211w) {
00856                 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
00857                            ie.mgmt_group_cipher);
00858         }
00859 #endif /* CONFIG_IEEE80211W */
00860 
00861         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
00862         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
00863                          !!(ssid->proto & WPA_PROTO_RSN));
00864 
00865         if (bss || !wpa_s->ap_ies_from_associnfo) {
00866                 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
00867                                          bss_wpa ? 2 + bss_wpa[1] : 0) ||
00868                     wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
00869                                          bss_rsn ? 2 + bss_rsn[1] : 0))
00870                         return -1;
00871         }
00872 
00873         sel = ie.group_cipher & ssid->group_cipher;
00874         if (sel & WPA_CIPHER_CCMP) {
00875                 wpa_s->group_cipher = WPA_CIPHER_CCMP;
00876                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
00877         } else if (sel & WPA_CIPHER_TKIP) {
00878                 wpa_s->group_cipher = WPA_CIPHER_TKIP;
00879                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
00880         } else if (sel & WPA_CIPHER_WEP104) {
00881                 wpa_s->group_cipher = WPA_CIPHER_WEP104;
00882                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
00883         } else if (sel & WPA_CIPHER_WEP40) {
00884                 wpa_s->group_cipher = WPA_CIPHER_WEP40;
00885                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
00886         } else {
00887                 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
00888                 return -1;
00889         }
00890 
00891         sel = ie.pairwise_cipher & ssid->pairwise_cipher;
00892         if (sel & WPA_CIPHER_CCMP) {
00893                 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
00894                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
00895         } else if (sel & WPA_CIPHER_TKIP) {
00896                 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
00897                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
00898         } else if (sel & WPA_CIPHER_NONE) {
00899                 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
00900                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
00901         } else {
00902                 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
00903                            "cipher.");
00904                 return -1;
00905         }
00906 
00907         sel = ie.key_mgmt & ssid->key_mgmt;
00908         if (0) {
00909 #ifdef CONFIG_IEEE80211R
00910         } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
00911                 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
00912                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
00913         } else if (sel & WPA_KEY_MGMT_FT_PSK) {
00914                 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
00915                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
00916 #endif /* CONFIG_IEEE80211R */
00917 #ifdef CONFIG_IEEE80211W
00918         } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
00919                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
00920                 wpa_msg(wpa_s, MSG_DEBUG,
00921                         "WPA: using KEY_MGMT 802.1X with SHA256");
00922         } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
00923                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
00924                 wpa_msg(wpa_s, MSG_DEBUG,
00925                         "WPA: using KEY_MGMT PSK with SHA256");
00926 #endif /* CONFIG_IEEE80211W */
00927         } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
00928                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
00929                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
00930         } else if (sel & WPA_KEY_MGMT_PSK) {
00931                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
00932                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
00933         } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
00934                 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
00935                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
00936         } else {
00937                 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
00938                            "key management type.");
00939                 return -1;
00940         }
00941 
00942         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
00943         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
00944                          wpa_s->pairwise_cipher);
00945         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
00946 
00947 #ifdef CONFIG_IEEE80211W
00948         sel = ie.mgmt_group_cipher;
00949         if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
00950             !(ie.capabilities & WPA_CAPABILITY_MFPC))
00951                 sel = 0;
00952         if (sel & WPA_CIPHER_AES_128_CMAC) {
00953                 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
00954                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
00955                         "AES-128-CMAC");
00956         } else {
00957                 wpa_s->mgmt_group_cipher = 0;
00958                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
00959         }
00960         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
00961                          wpa_s->mgmt_group_cipher);
00962         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
00963 #endif /* CONFIG_IEEE80211W */
00964 
00965         if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
00966                 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
00967                 return -1;
00968         }
00969 
00970         if (ssid->key_mgmt &
00971             (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
00972                 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
00973         else
00974                 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
00975 
00976         return 0;
00977 }
00978 
00979 
00988 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
00989                               struct wpa_bss *bss, struct wpa_ssid *ssid)
00990 {
00991         u8 wpa_ie[80];
00992         size_t wpa_ie_len;
00993         int use_crypt, ret, i, bssid_changed;
00994         int algs = WPA_AUTH_ALG_OPEN;
00995         enum wpa_cipher cipher_pairwise, cipher_group;
00996         struct wpa_driver_associate_params params;
00997         int wep_keys_set = 0;
00998         struct wpa_driver_capa capa;
00999         int assoc_failed = 0;
01000         struct wpa_ssid *old_ssid;
01001 
01002         if (ssid->mode == WPAS_MODE_AP) {
01003 #ifdef CONFIG_AP
01004                 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
01005                         wpa_printf(MSG_INFO, "Driver does not support AP "
01006                                    "mode");
01007                         return;
01008                 }
01009                 wpa_supplicant_create_ap(wpa_s, ssid);
01010                 wpa_s->current_bss = bss;
01011 #else /* CONFIG_AP */
01012                 wpa_printf(MSG_ERROR, "AP mode support not included in the "
01013                            "build");
01014 #endif /* CONFIG_AP */
01015                 return;
01016         }
01017 
01018         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
01019             ssid->mode == IEEE80211_MODE_INFRA) {
01020                 sme_authenticate(wpa_s, bss, ssid);
01021                 return;
01022         }
01023 
01024         wpa_s->reassociate = 0;
01025         if (bss) {
01026 #ifdef CONFIG_IEEE80211R
01027                 const u8 *ie, *md = NULL;
01028 #endif /* CONFIG_IEEE80211R */
01029                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
01030                         " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
01031                         wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
01032                 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
01033                 os_memset(wpa_s->bssid, 0, ETH_ALEN);
01034                 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
01035                 if (bssid_changed)
01036                         wpas_notify_bssid_changed(wpa_s);
01037 #ifdef CONFIG_IEEE80211R
01038                 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
01039                 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
01040                         md = ie + 2;
01041                 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
01042                 if (md) {
01043                         /* Prepare for the next transition */
01044                         wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
01045                 }
01046 #endif /* CONFIG_IEEE80211R */
01047 #ifdef CONFIG_WPS
01048         } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
01049                    wpa_s->conf->ap_scan == 2 &&
01050                    (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
01051                 /* Use ap_scan==1 style network selection to find the network
01052                  */
01053                 wpa_s->scan_req = 2;
01054                 wpa_s->reassociate = 1;
01055                 wpa_supplicant_req_scan(wpa_s, 0, 0);
01056                 return;
01057 #endif /* CONFIG_WPS */
01058         } else {
01059                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
01060                         wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
01061                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
01062         }
01063         wpa_supplicant_cancel_scan(wpa_s);
01064 
01065         /* Starting new association, so clear the possibly used WPA IE from the
01066          * previous association. */
01067         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
01068 
01069 #ifdef IEEE8021X_EAPOL
01070         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
01071                 if (ssid->leap) {
01072                         if (ssid->non_leap == 0)
01073                                 algs = WPA_AUTH_ALG_LEAP;
01074                         else
01075                                 algs |= WPA_AUTH_ALG_LEAP;
01076                 }
01077         }
01078 #endif /* IEEE8021X_EAPOL */
01079         wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
01080         if (ssid->auth_alg) {
01081                 algs = ssid->auth_alg;
01082                 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
01083                            algs);
01084         }
01085 
01086         if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
01087                     wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
01088             (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
01089                                WPA_KEY_MGMT_FT_IEEE8021X |
01090                                WPA_KEY_MGMT_FT_PSK |
01091                                WPA_KEY_MGMT_IEEE8021X_SHA256 |
01092                                WPA_KEY_MGMT_PSK_SHA256))) {
01093                 int try_opportunistic;
01094                 try_opportunistic = ssid->proactive_key_caching &&
01095                         (ssid->proto & WPA_PROTO_RSN);
01096                 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
01097                                             wpa_s->current_ssid,
01098                                             try_opportunistic) == 0)
01099                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
01100                 wpa_ie_len = sizeof(wpa_ie);
01101                 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
01102                                               wpa_ie, &wpa_ie_len)) {
01103                         wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
01104                                    "management and encryption suites");
01105                         return;
01106                 }
01107         } else if (ssid->key_mgmt &
01108                    (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
01109                     WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
01110                     WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
01111                     WPA_KEY_MGMT_IEEE8021X_SHA256)) {
01112                 wpa_ie_len = sizeof(wpa_ie);
01113                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
01114                                               wpa_ie, &wpa_ie_len)) {
01115                         wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
01116                                    "management and encryption suites (no scan "
01117                                    "results)");
01118                         return;
01119                 }
01120 #ifdef CONFIG_WPS
01121         } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
01122                 struct wpabuf *wps_ie;
01123                 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
01124                 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
01125                         wpa_ie_len = wpabuf_len(wps_ie);
01126                         os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
01127                 } else
01128                         wpa_ie_len = 0;
01129                 wpabuf_free(wps_ie);
01130                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
01131 #endif /* CONFIG_WPS */
01132         } else {
01133                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
01134                 wpa_ie_len = 0;
01135         }
01136 
01137         wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
01138         use_crypt = 1;
01139         cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
01140         cipher_group = cipher_suite2driver(wpa_s->group_cipher);
01141         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
01142             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
01143                 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
01144                         use_crypt = 0;
01145                 if (wpa_set_wep_keys(wpa_s, ssid)) {
01146                         use_crypt = 1;
01147                         wep_keys_set = 1;
01148                 }
01149         }
01150         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
01151                 use_crypt = 0;
01152 
01153 #ifdef IEEE8021X_EAPOL
01154         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
01155                 if ((ssid->eapol_flags &
01156                      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
01157                       EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
01158                     !wep_keys_set) {
01159                         use_crypt = 0;
01160                 } else {
01161                         /* Assume that dynamic WEP-104 keys will be used and
01162                          * set cipher suites in order for drivers to expect
01163                          * encryption. */
01164                         cipher_pairwise = cipher_group = CIPHER_WEP104;
01165                 }
01166         }
01167 #endif /* IEEE8021X_EAPOL */
01168 
01169         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
01170                 /* Set the key before (and later after) association */
01171                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
01172         }
01173 
01174         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
01175         os_memset(&params, 0, sizeof(params));
01176         if (bss) {
01177                 params.bssid = bss->bssid;
01178                 params.ssid = bss->ssid;
01179                 params.ssid_len = bss->ssid_len;
01180                 params.freq = bss->freq;
01181         } else {
01182                 params.ssid = ssid->ssid;
01183                 params.ssid_len = ssid->ssid_len;
01184         }
01185         if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
01186             params.freq == 0)
01187                 params.freq = ssid->frequency; /* Initial channel for IBSS */
01188         params.wpa_ie = wpa_ie;
01189         params.wpa_ie_len = wpa_ie_len;
01190         params.pairwise_suite = cipher_pairwise;
01191         params.group_suite = cipher_group;
01192         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
01193         params.auth_alg = algs;
01194         params.mode = ssid->mode;
01195         for (i = 0; i < NUM_WEP_KEYS; i++) {
01196                 if (ssid->wep_key_len[i])
01197                         params.wep_key[i] = ssid->wep_key[i];
01198                 params.wep_key_len[i] = ssid->wep_key_len[i];
01199         }
01200         params.wep_tx_keyidx = ssid->wep_tx_keyidx;
01201 
01202         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
01203             (params.key_mgmt_suite == KEY_MGMT_PSK ||
01204              params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
01205                 params.passphrase = ssid->passphrase;
01206                 if (ssid->psk_set)
01207                         params.psk = ssid->psk;
01208         }
01209 
01210         params.drop_unencrypted = use_crypt;
01211 
01212 #ifdef CONFIG_IEEE80211W
01213         params.mgmt_frame_protection = ssid->ieee80211w;
01214         if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
01215                 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
01216                 struct wpa_ie_data ie;
01217                 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
01218                     ie.capabilities &
01219                     (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
01220                         wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
01221                                    "require MFP");
01222                         params.mgmt_frame_protection =
01223                                 MGMT_FRAME_PROTECTION_REQUIRED;
01224                 }
01225         }
01226 #endif /* CONFIG_IEEE80211W */
01227 
01228         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
01229                 ret = ieee80211_sta_associate(wpa_s, &params);
01230         else
01231                 ret = wpa_drv_associate(wpa_s, &params);
01232         if (ret < 0) {
01233                 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
01234                         "failed");
01235                 /* try to continue anyway; new association will be tried again
01236                  * after timeout */
01237                 assoc_failed = 1;
01238         }
01239 
01240         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
01241                 /* Set the key after the association just in case association
01242                  * cleared the previously configured key. */
01243                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
01244                 /* No need to timeout authentication since there is no key
01245                  * management. */
01246                 wpa_supplicant_cancel_auth_timeout(wpa_s);
01247                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
01248 #ifdef CONFIG_IBSS_RSN
01249         } else if (ssid->mode == WPAS_MODE_IBSS &&
01250                    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
01251                    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
01252                 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
01253                 /*
01254                  * RSN IBSS authentication is per-STA and we can disable the
01255                  * per-BSSID authentication.
01256                  */
01257                 wpa_supplicant_cancel_auth_timeout(wpa_s);
01258 #endif /* CONFIG_IBSS_RSN */
01259         } else {
01260                 /* Timeout for IEEE 802.11 authentication and association */
01261                 int timeout = 60;
01262 
01263                 if (assoc_failed) {
01264                         /* give IBSS a bit more time */
01265                         timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
01266                 } else if (wpa_s->conf->ap_scan == 1) {
01267                         /* give IBSS a bit more time */
01268                         timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
01269                 }
01270                 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
01271         }
01272 
01273         if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
01274             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
01275                 /* Set static WEP keys again */
01276                 wpa_set_wep_keys(wpa_s, ssid);
01277         }
01278 
01279         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
01280                 /*
01281                  * Do not allow EAP session resumption between different
01282                  * network configurations.
01283                  */
01284                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
01285         }
01286         old_ssid = wpa_s->current_ssid;
01287         wpa_s->current_ssid = ssid;
01288         wpa_s->current_bss = bss;
01289         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
01290         wpa_supplicant_initiate_eapol(wpa_s);
01291         if (old_ssid != wpa_s->current_ssid)
01292                 wpas_notify_network_changed(wpa_s);
01293 }
01294 
01295 
01304 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
01305                                  int reason_code)
01306 {
01307         struct wpa_ssid *old_ssid;
01308         u8 *addr = NULL;
01309 
01310         if (!is_zero_ether_addr(wpa_s->bssid)) {
01311                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
01312                         ieee80211_sta_disassociate(wpa_s, reason_code);
01313                 else
01314                         wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
01315                 addr = wpa_s->bssid;
01316         }
01317         wpa_clear_keys(wpa_s, addr);
01318         wpa_supplicant_mark_disassoc(wpa_s);
01319         old_ssid = wpa_s->current_ssid;
01320         wpa_s->current_ssid = NULL;
01321         wpa_s->current_bss = NULL;
01322         wpa_sm_set_config(wpa_s->wpa, NULL);
01323         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
01324         if (old_ssid != wpa_s->current_ssid)
01325                 wpas_notify_network_changed(wpa_s);
01326 }
01327 
01328 
01337 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
01338                                    int reason_code)
01339 {
01340         struct wpa_ssid *old_ssid;
01341         u8 *addr = NULL;
01342 
01343         if (!is_zero_ether_addr(wpa_s->bssid)) {
01344                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
01345                         ieee80211_sta_deauthenticate(wpa_s, reason_code);
01346                 else
01347                         wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
01348                                                reason_code);
01349                 addr = wpa_s->bssid;
01350         }
01351         wpa_clear_keys(wpa_s, addr);
01352         wpa_supplicant_mark_disassoc(wpa_s);
01353         old_ssid = wpa_s->current_ssid;
01354         wpa_s->current_ssid = NULL;
01355         wpa_s->current_bss = NULL;
01356         wpa_sm_set_config(wpa_s->wpa, NULL);
01357         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
01358         if (old_ssid != wpa_s->current_ssid)
01359                 wpas_notify_network_changed(wpa_s);
01360 }
01361 
01362 
01370 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
01371                                    struct wpa_ssid *ssid)
01372 {
01373         struct wpa_ssid *other_ssid;
01374         int was_disabled;
01375 
01376         if (ssid == NULL) {
01377                 other_ssid = wpa_s->conf->ssid;
01378                 while (other_ssid) {
01379                         if (other_ssid == wpa_s->current_ssid &&
01380                             other_ssid->disabled)
01381                                 wpa_s->reassociate = 1;
01382 
01383                         was_disabled = other_ssid->disabled;
01384 
01385                         other_ssid->disabled = 0;
01386 
01387                         if (was_disabled != other_ssid->disabled)
01388                                 wpas_notify_network_enabled_changed(
01389                                         wpa_s, other_ssid);
01390 
01391                         other_ssid = other_ssid->next;
01392                 }
01393                 if (wpa_s->reassociate)
01394                         wpa_supplicant_req_scan(wpa_s, 0, 0);
01395         } else if (wpa_s->current_ssid == NULL && ssid->disabled) {
01396                 /*
01397                  * Try to reassociate since there is no current configuration
01398                  * and a new network was made available.
01399                  */
01400                 wpa_s->reassociate = 1;
01401                 wpa_supplicant_req_scan(wpa_s, 0, 0);
01402 
01403                 was_disabled = ssid->disabled;
01404 
01405                 ssid->disabled = 0;
01406 
01407                 if (was_disabled != ssid->disabled)
01408                         wpas_notify_network_enabled_changed(wpa_s, ssid);
01409         }
01410 }
01411 
01412 
01420 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
01421                                     struct wpa_ssid *ssid)
01422 {
01423         struct wpa_ssid *other_ssid;
01424         int was_disabled;
01425 
01426         if (ssid == NULL) {
01427                 other_ssid = wpa_s->conf->ssid;
01428                 while (other_ssid) {
01429                         was_disabled = other_ssid->disabled;
01430 
01431                         other_ssid->disabled = 1;
01432 
01433                         if (was_disabled != other_ssid->disabled)
01434                                 wpas_notify_network_enabled_changed(
01435                                         wpa_s, other_ssid);
01436 
01437                         other_ssid = other_ssid->next;
01438                 }
01439                 if (wpa_s->current_ssid)
01440                         wpa_supplicant_disassociate(
01441                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
01442         } else {
01443                 if (ssid == wpa_s->current_ssid)
01444                         wpa_supplicant_disassociate(
01445                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
01446 
01447                 was_disabled = ssid->disabled;
01448 
01449                 ssid->disabled = 1;
01450 
01451                 if (was_disabled != ssid->disabled)
01452                         wpas_notify_network_enabled_changed(wpa_s, ssid);
01453         }
01454 }
01455 
01456 
01462 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
01463                                    struct wpa_ssid *ssid)
01464 {
01465 
01466         struct wpa_ssid *other_ssid;
01467 
01468         if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
01469                 wpa_supplicant_disassociate(
01470                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
01471 
01472         /*
01473          * Mark all other networks disabled or mark all networks enabled if no
01474          * network specified.
01475          */
01476         other_ssid = wpa_s->conf->ssid;
01477         while (other_ssid) {
01478                 int was_disabled = other_ssid->disabled;
01479 
01480                 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
01481 
01482                 if (was_disabled != other_ssid->disabled)
01483                         wpas_notify_network_enabled_changed(wpa_s, other_ssid);
01484 
01485                 other_ssid = other_ssid->next;
01486         }
01487         wpa_s->disconnected = 0;
01488         wpa_s->reassociate = 1;
01489         wpa_supplicant_req_scan(wpa_s, 0, 0);
01490 
01491         if (ssid)
01492                 wpas_notify_network_selected(wpa_s, ssid);
01493 }
01494 
01495 
01503 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
01504 {
01505 
01506         int old_ap_scan;
01507 
01508         if (ap_scan < 0 || ap_scan > 2)
01509                 return -1;
01510 
01511         old_ap_scan = wpa_s->conf->ap_scan;
01512         wpa_s->conf->ap_scan = ap_scan;
01513 
01514         if (old_ap_scan != wpa_s->conf->ap_scan)
01515                 wpas_notify_ap_scan_changed(wpa_s);
01516 
01517         return 0;
01518 }
01519 
01520 
01529 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
01530                                     int debug_timestamp, int debug_show_keys)
01531 {
01532 
01533         int old_level, old_timestamp, old_show_keys;
01534 
01535         /* check for allowed debuglevels */
01536         if (debug_level != MSG_MSGDUMP &&
01537             debug_level != MSG_DEBUG &&
01538             debug_level != MSG_INFO &&
01539             debug_level != MSG_WARNING &&
01540             debug_level != MSG_ERROR)
01541                 return -1;
01542 
01543         old_level = wpa_debug_level;
01544         old_timestamp = wpa_debug_timestamp;
01545         old_show_keys = wpa_debug_show_keys;
01546 
01547         wpa_debug_level = debug_level;
01548         wpa_debug_timestamp = debug_timestamp ? 1 : 0;
01549         wpa_debug_show_keys = debug_show_keys ? 1 : 0;
01550 
01551         if (wpa_debug_level != old_level)
01552                 wpas_notify_debug_level_changed(global);
01553         if (wpa_debug_timestamp != old_timestamp)
01554                 wpas_notify_debug_timestamp_changed(global);
01555         if (wpa_debug_show_keys != old_show_keys)
01556                 wpas_notify_debug_show_keys_changed(global);
01557 
01558         return 0;
01559 }
01560 
01561 
01567 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
01568 {
01569         struct wpa_ssid *entry;
01570         u8 ssid[MAX_SSID_LEN];
01571         int res;
01572         size_t ssid_len;
01573         u8 bssid[ETH_ALEN];
01574         int wired;
01575 
01576         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
01577                 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
01578                         wpa_printf(MSG_WARNING, "Could not read SSID from "
01579                                    "MLME.");
01580                         return NULL;
01581                 }
01582         } else {
01583                 res = wpa_drv_get_ssid(wpa_s, ssid);
01584                 if (res < 0) {
01585                         wpa_printf(MSG_WARNING, "Could not read SSID from "
01586                                    "driver.");
01587                         return NULL;
01588                 }
01589                 ssid_len = res;
01590         }
01591 
01592         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
01593                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
01594         else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
01595                 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
01596                 return NULL;
01597         }
01598 
01599         wired = wpa_s->conf->ap_scan == 0 &&
01600                 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
01601 
01602         entry = wpa_s->conf->ssid;
01603         while (entry) {
01604                 if (!entry->disabled &&
01605                     ((ssid_len == entry->ssid_len &&
01606                       os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
01607                     (!entry->bssid_set ||
01608                      os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
01609                         return entry;
01610 #ifdef CONFIG_WPS
01611                 if (!entry->disabled &&
01612                     (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
01613                     (entry->ssid == NULL || entry->ssid_len == 0) &&
01614                     (!entry->bssid_set ||
01615                      os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
01616                         return entry;
01617 #endif /* CONFIG_WPS */
01618                 entry = entry->next;
01619         }
01620 
01621         return NULL;
01622 }
01623 
01624 
01625 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
01626                                      const char *name)
01627 {
01628         int i;
01629         size_t len;
01630         const char *pos;
01631 
01632         if (wpa_s == NULL)
01633                 return -1;
01634 
01635         if (wpa_drivers[0] == NULL) {
01636                 wpa_printf(MSG_ERROR, "No driver interfaces build into "
01637                            "wpa_supplicant.");
01638                 return -1;
01639         }
01640 
01641         if (name == NULL) {
01642                 /* default to first driver in the list */
01643                 wpa_s->driver = wpa_drivers[0];
01644                 wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
01645                 return 0;
01646         }
01647 
01648         pos = os_strchr(name, ',');
01649         if (pos)
01650                 len = pos - name;
01651         else
01652                 len = os_strlen(name);
01653         for (i = 0; wpa_drivers[i]; i++) {
01654                 if (os_strlen(wpa_drivers[i]->name) == len &&
01655                     os_strncmp(name, wpa_drivers[i]->name, len) ==
01656                     0) {
01657                         wpa_s->driver = wpa_drivers[i];
01658                         wpa_s->global_drv_priv = wpa_s->global->drv_priv[i];
01659                         return 0;
01660                 }
01661         }
01662 
01663         wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
01664         return -1;
01665 }
01666 
01667 
01682 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
01683                              const u8 *buf, size_t len)
01684 {
01685         struct wpa_supplicant *wpa_s = ctx;
01686 
01687         wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
01688         wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
01689 
01690         if (wpa_s->wpa_state < WPA_ASSOCIATED) {
01691                 /*
01692                  * There is possible race condition between receiving the
01693                  * association event and the EAPOL frame since they are coming
01694                  * through different paths from the driver. In order to avoid
01695                  * issues in trying to process the EAPOL frame before receiving
01696                  * association information, lets queue it for processing until
01697                  * the association event is received.
01698                  */
01699                 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
01700                            "received EAPOL frame");
01701                 wpabuf_free(wpa_s->pending_eapol_rx);
01702                 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
01703                 if (wpa_s->pending_eapol_rx) {
01704                         os_get_time(&wpa_s->pending_eapol_rx_time);
01705                         os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
01706                                   ETH_ALEN);
01707                 }
01708                 return;
01709         }
01710 
01711 #ifdef CONFIG_AP
01712         if (wpa_s->ap_iface) {
01713                 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
01714                 return;
01715         }
01716 #endif /* CONFIG_AP */
01717 
01718         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
01719                 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
01720                            "no key management is configured");
01721                 return;
01722         }
01723 
01724         if (wpa_s->eapol_received == 0 &&
01725             (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
01726              !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
01727              wpa_s->wpa_state != WPA_COMPLETED) &&
01728             (wpa_s->current_ssid == NULL ||
01729              wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
01730                 /* Timeout for completing IEEE 802.1X and WPA authentication */
01731                 wpa_supplicant_req_auth_timeout(
01732                         wpa_s,
01733                         (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
01734                          wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
01735                          wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
01736                         70 : 10, 0);
01737         }
01738         wpa_s->eapol_received++;
01739 
01740         if (wpa_s->countermeasures) {
01741                 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
01742                            "packet");
01743                 return;
01744         }
01745 
01746 #ifdef CONFIG_IBSS_RSN
01747         if (wpa_s->current_ssid &&
01748             wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
01749                 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
01750                 return;
01751         }
01752 #endif /* CONFIG_IBSS_RSN */
01753 
01754         /* Source address of the incoming EAPOL frame could be compared to the
01755          * current BSSID. However, it is possible that a centralized
01756          * Authenticator could be using another MAC address than the BSSID of
01757          * an AP, so just allow any address to be used for now. The replies are
01758          * still sent to the current BSSID (if available), though. */
01759 
01760         os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
01761         if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
01762             eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
01763                 return;
01764         wpa_drv_poll(wpa_s);
01765         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
01766                 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
01767         else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
01768                 /*
01769                  * Set portValid = TRUE here since we are going to skip 4-way
01770                  * handshake processing which would normally set portValid. We
01771                  * need this to allow the EAPOL state machines to be completed
01772                  * without going through EAPOL-Key handshake.
01773                  */
01774                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
01775         }
01776 }
01777 
01778 
01788 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
01789 {
01790         static int interface_count = 0;
01791 
01792         if (wpa_s->driver->send_eapol) {
01793                 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
01794                 if (addr)
01795                         os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
01796         } else {
01797                 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
01798                                            wpa_drv_get_mac_addr(wpa_s),
01799                                            ETH_P_EAPOL,
01800                                            wpa_supplicant_rx_eapol, wpa_s, 0);
01801                 if (wpa_s->l2 == NULL)
01802                         return -1;
01803         }
01804 
01805         if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
01806                 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
01807                 return -1;
01808         }
01809 
01810         wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
01811                    MAC2STR(wpa_s->own_addr));
01812 
01813         if (wpa_s->bridge_ifname[0]) {
01814                 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
01815                            " '%s'", wpa_s->bridge_ifname);
01816                 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
01817                                               wpa_s->own_addr,
01818                                               ETH_P_EAPOL,
01819                                               wpa_supplicant_rx_eapol, wpa_s,
01820                                               0);
01821                 if (wpa_s->l2_br == NULL) {
01822                         wpa_printf(MSG_ERROR, "Failed to open l2_packet "
01823                                    "connection for the bridge interface '%s'",
01824                                    wpa_s->bridge_ifname);
01825                         return -1;
01826                 }
01827         }
01828 
01829         wpa_clear_keys(wpa_s, NULL);
01830 
01831         /* Make sure that TKIP countermeasures are not left enabled (could
01832          * happen if wpa_supplicant is killed during countermeasures. */
01833         wpa_drv_set_countermeasures(wpa_s, 0);
01834 
01835         wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
01836         wpa_drv_flush_pmkid(wpa_s);
01837 
01838         wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
01839         if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
01840                 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
01841                 interface_count++;
01842         } else
01843                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
01844 
01845         return 0;
01846 }
01847 
01848 
01849 static int wpa_supplicant_daemon(const char *pid_file)
01850 {
01851         wpa_printf(MSG_DEBUG, "Daemonize..");
01852         return os_daemonize(pid_file);
01853 }
01854 
01855 
01856 static struct wpa_supplicant * wpa_supplicant_alloc(void)
01857 {
01858         struct wpa_supplicant *wpa_s;
01859 
01860         wpa_s = os_zalloc(sizeof(*wpa_s));
01861         if (wpa_s == NULL)
01862                 return NULL;
01863         wpa_s->scan_req = 1;
01864         wpa_s->new_connection = 1;
01865 
01866         return wpa_s;
01867 }
01868 
01869 
01870 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
01871                                      struct wpa_interface *iface)
01872 {
01873         const char *ifname, *driver;
01874         struct wpa_driver_capa capa;
01875 
01876         wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
01877                    "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
01878                    iface->confname ? iface->confname : "N/A",
01879                    iface->driver ? iface->driver : "default",
01880                    iface->ctrl_interface ? iface->ctrl_interface : "N/A",
01881                    iface->bridge_ifname ? iface->bridge_ifname : "N/A");
01882 
01883         if (iface->confname) {
01884 #ifdef CONFIG_BACKEND_FILE
01885                 wpa_s->confname = os_rel2abs_path(iface->confname);
01886                 if (wpa_s->confname == NULL) {
01887                         wpa_printf(MSG_ERROR, "Failed to get absolute path "
01888                                    "for configuration file '%s'.",
01889                                    iface->confname);
01890                         return -1;
01891                 }
01892                 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
01893                            iface->confname, wpa_s->confname);
01894 #else /* CONFIG_BACKEND_FILE */
01895                 wpa_s->confname = os_strdup(iface->confname);
01896 #endif /* CONFIG_BACKEND_FILE */
01897                 wpa_s->conf = wpa_config_read(wpa_s->confname);
01898                 if (wpa_s->conf == NULL) {
01899                         wpa_printf(MSG_ERROR, "Failed to read or parse "
01900                                    "configuration '%s'.", wpa_s->confname);
01901                         return -1;
01902                 }
01903 
01904                 /*
01905                  * Override ctrl_interface and driver_param if set on command
01906                  * line.
01907                  */
01908                 if (iface->ctrl_interface) {
01909                         os_free(wpa_s->conf->ctrl_interface);
01910                         wpa_s->conf->ctrl_interface =
01911                                 os_strdup(iface->ctrl_interface);
01912                 }
01913 
01914                 if (iface->driver_param) {
01915                         os_free(wpa_s->conf->driver_param);
01916                         wpa_s->conf->driver_param =
01917                                 os_strdup(iface->driver_param);
01918                 }
01919         } else
01920                 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
01921                                                      iface->driver_param);
01922 
01923         if (wpa_s->conf == NULL) {
01924                 wpa_printf(MSG_ERROR, "\nNo configuration found.");
01925                 return -1;
01926         }
01927 
01928         if (iface->ifname == NULL) {
01929                 wpa_printf(MSG_ERROR, "\nInterface name is required.");
01930                 return -1;
01931         }
01932         if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
01933                 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
01934                            iface->ifname);
01935                 return -1;
01936         }
01937         os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
01938 
01939         if (iface->bridge_ifname) {
01940                 if (os_strlen(iface->bridge_ifname) >=
01941                     sizeof(wpa_s->bridge_ifname)) {
01942                         wpa_printf(MSG_ERROR, "\nToo long bridge interface "
01943                                    "name '%s'.", iface->bridge_ifname);
01944                         return -1;
01945                 }
01946                 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
01947                            sizeof(wpa_s->bridge_ifname));
01948         }
01949 
01950         /* RSNA Supplicant Key Management - INITIALIZE */
01951         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
01952         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
01953 
01954         /* Initialize driver interface and register driver event handler before
01955          * L2 receive handler so that association events are processed before
01956          * EAPOL-Key packets if both become available for the same select()
01957          * call. */
01958         driver = iface->driver;
01959 next_driver:
01960         if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
01961                 return -1;
01962 
01963         wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
01964         if (wpa_s->drv_priv == NULL) {
01965                 const char *pos;
01966                 pos = driver ? os_strchr(driver, ',') : NULL;
01967                 if (pos) {
01968                         wpa_printf(MSG_DEBUG, "Failed to initialize driver "
01969                                    "interface - try next driver wrapper");
01970                         driver = pos + 1;
01971                         goto next_driver;
01972                 }
01973                 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
01974                 return -1;
01975         }
01976         if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
01977                 wpa_printf(MSG_ERROR, "Driver interface rejected "
01978                            "driver_param '%s'", wpa_s->conf->driver_param);
01979                 return -1;
01980         }
01981 
01982         ifname = wpa_drv_get_ifname(wpa_s);
01983         if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
01984                 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
01985                            "name with '%s'", ifname);
01986                 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
01987         }
01988 
01989         if (wpa_supplicant_init_wpa(wpa_s) < 0)
01990                 return -1;
01991 
01992         wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
01993                           wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
01994                           NULL);
01995         wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
01996 
01997         if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
01998             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
01999                              wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
02000                 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
02001                            "dot11RSNAConfigPMKLifetime");
02002                 return -1;
02003         }
02004 
02005         if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
02006             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
02007                              wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
02008                 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
02009                         "dot11RSNAConfigPMKReauthThreshold");
02010                 return -1;
02011         }
02012 
02013         if (wpa_s->conf->dot11RSNAConfigSATimeout &&
02014             wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
02015                              wpa_s->conf->dot11RSNAConfigSATimeout)) {
02016                 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
02017                            "dot11RSNAConfigSATimeout");
02018                 return -1;
02019         }
02020 
02021         if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
02022                 wpa_s->drv_flags = capa.flags;
02023                 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
02024                         if (ieee80211_sta_init(wpa_s))
02025                                 return -1;
02026                 }
02027                 wpa_s->max_scan_ssids = capa.max_scan_ssids;
02028                 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
02029         }
02030         if (wpa_s->max_remain_on_chan == 0)
02031                 wpa_s->max_remain_on_chan = 1000;
02032 
02033         if (wpa_supplicant_driver_init(wpa_s) < 0)
02034                 return -1;
02035 
02036         if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
02037             wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
02038                 wpa_printf(MSG_DEBUG, "Failed to set country");
02039                 return -1;
02040         }
02041 
02042         wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
02043 
02044         if (wpas_wps_init(wpa_s))
02045                 return -1;
02046 
02047         if (wpa_supplicant_init_eapol(wpa_s) < 0)
02048                 return -1;
02049         wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
02050 
02051         wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
02052         if (wpa_s->ctrl_iface == NULL) {
02053                 wpa_printf(MSG_ERROR,
02054                            "Failed to initialize control interface '%s'.\n"
02055                            "You may have another wpa_supplicant process "
02056                            "already running or the file was\n"
02057                            "left by an unclean termination of wpa_supplicant "
02058                            "in which case you will need\n"
02059                            "to manually remove this file before starting "
02060                            "wpa_supplicant again.\n",
02061                            wpa_s->conf->ctrl_interface);
02062                 return -1;
02063         }
02064 
02065 #ifdef CONFIG_IBSS_RSN
02066         wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
02067         if (!wpa_s->ibss_rsn) {
02068                 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
02069                 return -1;
02070         }
02071 #endif /* CONFIG_IBSS_RSN */
02072 
02073         if (wpa_bss_init(wpa_s) < 0)
02074                 return -1;
02075 
02076         return 0;
02077 }
02078 
02079 
02080 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
02081                                         int notify)
02082 {
02083         if (wpa_s->drv_priv) {
02084                 wpa_supplicant_deauthenticate(wpa_s,
02085                                               WLAN_REASON_DEAUTH_LEAVING);
02086 
02087                 wpa_drv_set_countermeasures(wpa_s, 0);
02088                 wpa_clear_keys(wpa_s, NULL);
02089         }
02090 
02091         wpa_supplicant_cleanup(wpa_s);
02092 
02093         if (notify)
02094                 wpas_notify_iface_removed(wpa_s);
02095 
02096         if (wpa_s->drv_priv)
02097                 wpa_drv_deinit(wpa_s);
02098 }
02099 
02100 
02113 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
02114                                                  struct wpa_interface *iface)
02115 {
02116         struct wpa_supplicant *wpa_s;
02117         struct wpa_interface t_iface;
02118         struct wpa_ssid *ssid;
02119 
02120         if (global == NULL || iface == NULL)
02121                 return NULL;
02122 
02123         wpa_s = wpa_supplicant_alloc();
02124         if (wpa_s == NULL)
02125                 return NULL;
02126 
02127         wpa_s->global = global;
02128 
02129         t_iface = *iface;
02130         if (global->params.override_driver) {
02131                 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
02132                            "('%s' -> '%s')",
02133                            iface->driver, global->params.override_driver);
02134                 t_iface.driver = global->params.override_driver;
02135         }
02136         if (global->params.override_ctrl_interface) {
02137                 wpa_printf(MSG_DEBUG, "Override interface parameter: "
02138                            "ctrl_interface ('%s' -> '%s')",
02139                            iface->ctrl_interface,
02140                            global->params.override_ctrl_interface);
02141                 t_iface.ctrl_interface =
02142                         global->params.override_ctrl_interface;
02143         }
02144         if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
02145                 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
02146                            iface->ifname);
02147                 wpa_supplicant_deinit_iface(wpa_s, 0);
02148                 os_free(wpa_s);
02149                 return NULL;
02150         }
02151 
02152         /* Notify the control interfaces about new iface */
02153         if (wpas_notify_iface_added(wpa_s)) {
02154                 wpa_supplicant_deinit_iface(wpa_s, 1);
02155                 os_free(wpa_s);
02156                 return NULL;
02157         }
02158 
02159         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
02160                 wpas_notify_network_added(wpa_s, ssid);
02161 
02162         wpa_s->next = global->ifaces;
02163         global->ifaces = wpa_s;
02164 
02165         wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
02166 
02167         return wpa_s;
02168 }
02169 
02170 
02182 int wpa_supplicant_remove_iface(struct wpa_global *global,
02183                                 struct wpa_supplicant *wpa_s)
02184 {
02185         struct wpa_supplicant *prev;
02186 
02187         /* Remove interface from the global list of interfaces */
02188         prev = global->ifaces;
02189         if (prev == wpa_s) {
02190                 global->ifaces = wpa_s->next;
02191         } else {
02192                 while (prev && prev->next != wpa_s)
02193                         prev = prev->next;
02194                 if (prev == NULL)
02195                         return -1;
02196                 prev->next = wpa_s->next;
02197         }
02198 
02199         wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
02200 
02201         wpa_supplicant_deinit_iface(wpa_s, 1);
02202         os_free(wpa_s);
02203 
02204         return 0;
02205 }
02206 
02207 
02214 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
02215                                                  const char *ifname)
02216 {
02217         struct wpa_supplicant *wpa_s;
02218 
02219         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
02220                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
02221                         return wpa_s;
02222         }
02223         return NULL;
02224 }
02225 
02226 
02236 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
02237 {
02238         struct wpa_global *global;
02239         int ret, i;
02240 
02241         if (params == NULL)
02242                 return NULL;
02243 
02244         wpa_debug_open_file(params->wpa_debug_file_path);
02245         if (params->wpa_debug_syslog)
02246                 wpa_debug_open_syslog();
02247 
02248         ret = eap_register_methods();
02249         if (ret) {
02250                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
02251                 if (ret == -2)
02252                         wpa_printf(MSG_ERROR, "Two or more EAP methods used "
02253                                    "the same EAP type.");
02254                 return NULL;
02255         }
02256 
02257         global = os_zalloc(sizeof(*global));
02258         if (global == NULL)
02259                 return NULL;
02260         global->params.daemonize = params->daemonize;
02261         global->params.wait_for_monitor = params->wait_for_monitor;
02262         global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
02263         if (params->pid_file)
02264                 global->params.pid_file = os_strdup(params->pid_file);
02265         if (params->ctrl_interface)
02266                 global->params.ctrl_interface =
02267                         os_strdup(params->ctrl_interface);
02268         if (params->override_driver)
02269                 global->params.override_driver =
02270                         os_strdup(params->override_driver);
02271         if (params->override_ctrl_interface)
02272                 global->params.override_ctrl_interface =
02273                         os_strdup(params->override_ctrl_interface);
02274         wpa_debug_level = global->params.wpa_debug_level =
02275                 params->wpa_debug_level;
02276         wpa_debug_show_keys = global->params.wpa_debug_show_keys =
02277                 params->wpa_debug_show_keys;
02278         wpa_debug_timestamp = global->params.wpa_debug_timestamp =
02279                 params->wpa_debug_timestamp;
02280 
02281         if (eloop_init()) {
02282                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
02283                 wpa_supplicant_deinit(global);
02284                 return NULL;
02285         }
02286 
02287         global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
02288         if (global->ctrl_iface == NULL) {
02289                 wpa_supplicant_deinit(global);
02290                 return NULL;
02291         }
02292 
02293         if (wpas_notify_supplicant_initialized(global)) {
02294                 wpa_supplicant_deinit(global);
02295                 return NULL;
02296         }
02297 
02298         for (i = 0; wpa_drivers[i]; i++)
02299                 global->drv_count++;
02300         if (global->drv_count == 0) {
02301                 wpa_printf(MSG_ERROR, "No drivers enabled");
02302                 wpa_supplicant_deinit(global);
02303                 return NULL;
02304         }
02305         global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
02306         if (global->drv_priv == NULL) {
02307                 wpa_supplicant_deinit(global);
02308                 return NULL;
02309         }
02310         for (i = 0; wpa_drivers[i]; i++) {
02311                 if (!wpa_drivers[i]->global_init)
02312                         continue;
02313                 global->drv_priv[i] = wpa_drivers[i]->global_init();
02314                 if (global->drv_priv[i] == NULL) {
02315                         wpa_printf(MSG_ERROR, "Failed to initialize driver "
02316                                    "'%s'", wpa_drivers[i]->name);
02317                         wpa_supplicant_deinit(global);
02318                         return NULL;
02319                 }
02320         }
02321 
02322         return global;
02323 }
02324 
02325 
02335 int wpa_supplicant_run(struct wpa_global *global)
02336 {
02337         struct wpa_supplicant *wpa_s;
02338 
02339         if (global->params.daemonize &&
02340             wpa_supplicant_daemon(global->params.pid_file))
02341                 return -1;
02342 
02343         if (global->params.wait_for_monitor) {
02344                 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
02345                         if (wpa_s->ctrl_iface)
02346                                 wpa_supplicant_ctrl_iface_wait(
02347                                         wpa_s->ctrl_iface);
02348         }
02349 
02350         eloop_register_signal_terminate(wpa_supplicant_terminate, global);
02351         eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
02352 
02353         eloop_run();
02354 
02355         return 0;
02356 }
02357 
02358 
02366 void wpa_supplicant_deinit(struct wpa_global *global)
02367 {
02368         int i;
02369 
02370         if (global == NULL)
02371                 return;
02372 
02373         while (global->ifaces)
02374                 wpa_supplicant_remove_iface(global, global->ifaces);
02375 
02376         if (global->ctrl_iface)
02377                 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
02378 
02379         wpas_notify_supplicant_deinitialized(global);
02380 
02381         eap_peer_unregister_methods();
02382 #ifdef CONFIG_AP
02383         eap_server_unregister_methods();
02384 #endif /* CONFIG_AP */
02385 
02386         for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
02387                 if (!global->drv_priv[i])
02388                         continue;
02389                 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
02390         }
02391         os_free(global->drv_priv);
02392 
02393         eloop_destroy();
02394 
02395         if (global->params.pid_file) {
02396                 os_daemonize_terminate(global->params.pid_file);
02397                 os_free(global->params.pid_file);
02398         }
02399         os_free(global->params.ctrl_interface);
02400         os_free(global->params.override_driver);
02401         os_free(global->params.override_ctrl_interface);
02402 
02403         os_free(global);
02404         wpa_debug_close_syslog();
02405         wpa_debug_close_file();
02406 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Jan 2 2014 11:26:39