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


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:33:21