Do not trigger initial scan if there are no enabled networks
[libeap.git] / wpa_supplicant / wpa_supplicant.c
index bda436f..01e89df 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * WPA Supplicant
- * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
 #include "ap.h"
 #include "notify.h"
 #include "bgscan.h"
+#include "bss.h"
+#include "scan.h"
 
 const char *wpa_supplicant_version =
 "wpa_supplicant v" VERSION_STR "\n"
-"Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi> and contributors";
+"Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi> and contributors";
 
 const char *wpa_supplicant_license =
 "This program is free software. You can distribute it and/or modify it\n"
@@ -143,13 +145,13 @@ static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
 {
        u8 key[32];
        size_t keylen;
-       wpa_alg alg;
+       enum wpa_alg alg;
        u8 seq[6] = { 0 };
 
        /* IBSS/WPA-None uses only one key (Group) for both receiving and
         * sending unicast and multicast packets. */
 
-       if (ssid->mode != IEEE80211_MODE_IBSS) {
+       if (ssid->mode != WPAS_MODE_IBSS) {
                wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
                           "for WPA-None", ssid->mode);
                return -1;
@@ -256,7 +258,7 @@ void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
        struct wpa_ssid *ssid = wpa_s->current_ssid;
 
 #ifdef CONFIG_IBSS_RSN
-       if (ssid->mode == IEEE80211_MODE_IBSS &&
+       if (ssid->mode == WPAS_MODE_IBSS &&
            wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
            wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
                /*
@@ -378,6 +380,9 @@ static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
                wpa_s->ctrl_iface = NULL;
        }
        if (wpa_s->conf != NULL) {
+               struct wpa_ssid *ssid;
+               for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
+                       wpas_notify_network_removed(wpa_s, ssid);
                wpa_config_free(wpa_s->conf);
                wpa_s->conf = NULL;
        }
@@ -396,8 +401,7 @@ static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
        wpa_s->wpa = NULL;
        wpa_blacklist_clear(wpa_s);
 
-       wpa_scan_results_free(wpa_s->scan_res);
-       wpa_s->scan_res = NULL;
+       wpa_bss_deinit(wpa_s);
 
        wpa_supplicant_cancel_scan(wpa_s);
        wpa_supplicant_cancel_auth_timeout(wpa_s);
@@ -478,7 +482,7 @@ void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  * @state: State (wpa_state; WPA_*)
  * Returns: The state name as a printable text string
  */
-const char * wpa_supplicant_state_txt(int state)
+const char * wpa_supplicant_state_txt(enum wpa_states state)
 {
        switch (state) {
        case WPA_DISCONNECTED:
@@ -513,8 +517,11 @@ const char * wpa_supplicant_state_txt(int state)
  * This function is called whenever the connection state changes, e.g.,
  * association is completed for WPA/WPA2 4-Way Handshake is started.
  */
-void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
+void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
+                             enum wpa_states state)
 {
+       enum wpa_states old_state = wpa_s->wpa_state;
+
        wpa_printf(MSG_DEBUG, "State: %s -> %s",
                   wpa_supplicant_state_txt(wpa_s->wpa_state),
                   wpa_supplicant_state_txt(state));
@@ -522,8 +529,6 @@ void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
        if (state != WPA_SCANNING)
                wpa_supplicant_notify_scanning(wpa_s, 0);
 
-       wpas_notify_state_changed(wpa_s, state, wpa_s->wpa_state);
-
        if (state == WPA_COMPLETED && wpa_s->new_connection) {
 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
                struct wpa_ssid *ssid = wpa_s->current_ssid;
@@ -537,37 +542,60 @@ void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
                wpa_s->new_connection = 0;
                wpa_s->reassociated_connection = 1;
                wpa_drv_set_operstate(wpa_s, 1);
+               wpa_s->after_wps = 0;
        } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
                   state == WPA_ASSOCIATED) {
                wpa_s->new_connection = 1;
                wpa_drv_set_operstate(wpa_s, 0);
        }
        wpa_s->wpa_state = state;
+
+       if (wpa_s->wpa_state != old_state)
+               wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
+}
+
+
+void wpa_supplicant_terminate_proc(struct wpa_global *global)
+{
+       int pending = 0;
+#ifdef CONFIG_WPS
+       struct wpa_supplicant *wpa_s = global->ifaces;
+       while (wpa_s) {
+               if (wpas_wps_terminate_pending(wpa_s) == 1)
+                       pending = 1;
+               wpa_s = wpa_s->next;
+       }
+#endif /* CONFIG_WPS */
+       if (pending)
+               return;
+       eloop_terminate();
 }
 
 
-static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
-                                    void *signal_ctx)
+static void wpa_supplicant_terminate(int sig, void *signal_ctx)
 {
-       struct wpa_global *global = eloop_ctx;
+       struct wpa_global *global = signal_ctx;
        struct wpa_supplicant *wpa_s;
        for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
                wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
                        "received", sig);
        }
-       eloop_terminate();
+       wpa_supplicant_terminate_proc(global);
 }
 
 
 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
 {
-       wpa_states old_state = wpa_s->wpa_state;
+       enum wpa_states old_state = wpa_s->wpa_state;
+
        wpa_s->pairwise_cipher = 0;
        wpa_s->group_cipher = 0;
        wpa_s->mgmt_group_cipher = 0;
        wpa_s->key_mgmt = 0;
        wpa_s->wpa_state = WPA_DISCONNECTED;
-       wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
+
+       if (wpa_s->wpa_state != old_state)
+               wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
 }
 
 
@@ -647,21 +675,20 @@ int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
 }
 
 
-static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
-                                   void *signal_ctx)
+static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
 {
-       struct wpa_global *global = eloop_ctx;
+       struct wpa_global *global = signal_ctx;
        struct wpa_supplicant *wpa_s;
        wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
        for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
                if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
-                       eloop_terminate();
+                       wpa_supplicant_terminate_proc(global);
                }
        }
 }
 
 
-static wpa_cipher cipher_suite2driver(int cipher)
+enum wpa_cipher cipher_suite2driver(int cipher)
 {
        switch (cipher) {
        case WPA_CIPHER_NONE:
@@ -679,7 +706,7 @@ static wpa_cipher cipher_suite2driver(int cipher)
 }
 
 
-static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
+enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
 {
        switch (key_mgmt) {
        case WPA_KEY_MGMT_NONE:
@@ -743,7 +770,7 @@ static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
 
 #ifdef CONFIG_IEEE80211W
        if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
-           ssid->ieee80211w == IEEE80211W_REQUIRED) {
+           ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
                wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
                        "that does not support management frame protection - "
                        "reject");
@@ -770,8 +797,7 @@ static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  * available).
  */
 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
-                             struct wpa_scan_res *bss,
-                             struct wpa_ssid *ssid,
+                             struct wpa_bss *bss, struct wpa_ssid *ssid,
                              u8 *wpa_ie, size_t *wpa_ie_len)
 {
        struct wpa_ie_data ie;
@@ -779,8 +805,8 @@ int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
        const u8 *bss_wpa, *bss_rsn;
 
        if (bss) {
-               bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
-               bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
+               bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
+               bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
        } else
                bss_wpa = bss_rsn = NULL;
 
@@ -813,7 +839,7 @@ int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
                        ie.key_mgmt = ssid->key_mgmt;
 #ifdef CONFIG_IEEE80211W
                        ie.mgmt_group_cipher =
-                               ssid->ieee80211w != NO_IEEE80211W ?
+                               ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
                                WPA_CIPHER_AES_128_CMAC : 0;
 #endif /* CONFIG_IEEE80211W */
                        wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
@@ -920,7 +946,7 @@ int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
 
 #ifdef CONFIG_IEEE80211W
        sel = ie.mgmt_group_cipher;
-       if (ssid->ieee80211w == NO_IEEE80211W ||
+       if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
            !(ie.capabilities & WPA_CAPABILITY_MFPC))
                sel = 0;
        if (sel & WPA_CIPHER_AES_128_CMAC) {
@@ -933,6 +959,7 @@ int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
        }
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
                         wpa_s->mgmt_group_cipher);
+       wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
 #endif /* CONFIG_IEEE80211W */
 
        if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
@@ -959,20 +986,20 @@ int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  * This function is used to request %wpa_supplicant to associate with a BSS.
  */
 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
-                             struct wpa_scan_res *bss, struct wpa_ssid *ssid)
+                             struct wpa_bss *bss, struct wpa_ssid *ssid)
 {
        u8 wpa_ie[80];
        size_t wpa_ie_len;
        int use_crypt, ret, i, bssid_changed;
-       int algs = AUTH_ALG_OPEN_SYSTEM;
-       wpa_cipher cipher_pairwise, cipher_group;
+       int algs = WPA_AUTH_ALG_OPEN;
+       enum wpa_cipher cipher_pairwise, cipher_group;
        struct wpa_driver_associate_params params;
        int wep_keys_set = 0;
        struct wpa_driver_capa capa;
        int assoc_failed = 0;
        struct wpa_ssid *old_ssid;
 
-       if (ssid->mode == IEEE80211_MODE_AP) {
+       if (ssid->mode == WPAS_MODE_AP) {
 #ifdef CONFIG_AP
                if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
                        wpa_printf(MSG_INFO, "Driver does not support AP "
@@ -980,6 +1007,7 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
                        return;
                }
                wpa_supplicant_create_ap(wpa_s, ssid);
+               wpa_s->current_bss = bss;
 #else /* CONFIG_AP */
                wpa_printf(MSG_ERROR, "AP mode support not included in the "
                           "build");
@@ -996,25 +1024,24 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
        wpa_s->reassociate = 0;
        if (bss) {
 #ifdef CONFIG_IEEE80211R
-               const u8 *md = NULL;
+               const u8 *ie, *md = NULL;
 #endif /* CONFIG_IEEE80211R */
-               const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
                wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
                        " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
-                       ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
+                       wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
                bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
                os_memset(wpa_s->bssid, 0, ETH_ALEN);
                os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
                if (bssid_changed)
                        wpas_notify_bssid_changed(wpa_s);
 #ifdef CONFIG_IEEE80211R
-               ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
+               ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
                if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
                        md = ie + 2;
-               wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
+               wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
                if (md) {
                        /* Prepare for the next transition */
-                       wpa_ft_prepare_auth_request(wpa_s->wpa);
+                       wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
                }
 #endif /* CONFIG_IEEE80211R */
 #ifdef CONFIG_WPS
@@ -1043,27 +1070,21 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
        if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
                if (ssid->leap) {
                        if (ssid->non_leap == 0)
-                               algs = AUTH_ALG_LEAP;
+                               algs = WPA_AUTH_ALG_LEAP;
                        else
-                               algs |= AUTH_ALG_LEAP;
+                               algs |= WPA_AUTH_ALG_LEAP;
                }
        }
 #endif /* IEEE8021X_EAPOL */
        wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
        if (ssid->auth_alg) {
-               algs = 0;
-               if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
-                       algs |= AUTH_ALG_OPEN_SYSTEM;
-               if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
-                       algs |= AUTH_ALG_SHARED_KEY;
-               if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
-                       algs |= AUTH_ALG_LEAP;
+               algs = ssid->auth_alg;
                wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
                           algs);
        }
 
-       if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
-                   wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
+       if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
+                   wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
            (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
                               WPA_KEY_MGMT_FT_IEEE8021X |
                               WPA_KEY_MGMT_FT_PSK |
@@ -1153,16 +1174,16 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
        wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
        os_memset(&params, 0, sizeof(params));
        if (bss) {
-               const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
                params.bssid = bss->bssid;
-               params.ssid = ie ? ie + 2 : (u8 *) "";
-               params.ssid_len = ie ? ie[1] : 0;
+               params.ssid = bss->ssid;
+               params.ssid_len = bss->ssid_len;
                params.freq = bss->freq;
        } else {
                params.ssid = ssid->ssid;
                params.ssid_len = ssid->ssid_len;
        }
-       if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
+       if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
+           params.freq == 0)
                params.freq = ssid->frequency; /* Initial channel for IBSS */
        params.wpa_ie = wpa_ie;
        params.wpa_ie_len = wpa_ie_len;
@@ -1189,19 +1210,9 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
        params.drop_unencrypted = use_crypt;
 
 #ifdef CONFIG_IEEE80211W
-       switch (ssid->ieee80211w) {
-       case NO_IEEE80211W:
-               params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
-               break;
-       case IEEE80211W_OPTIONAL:
-               params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
-               break;
-       case IEEE80211W_REQUIRED:
-               params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
-               break;
-       }
-       if (ssid->ieee80211w != NO_IEEE80211W && bss) {
-               const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
+       params.mgmt_frame_protection = ssid->ieee80211w;
+       if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
+               const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
                struct wpa_ie_data ie;
                if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
                    ie.capabilities &
@@ -1235,7 +1246,7 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
                wpa_supplicant_cancel_auth_timeout(wpa_s);
                wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
 #ifdef CONFIG_IBSS_RSN
-       } else if (ssid->mode == IEEE80211_MODE_IBSS &&
+       } else if (ssid->mode == WPAS_MODE_IBSS &&
                   wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
                   wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
                ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
@@ -1251,10 +1262,10 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
 
                if (assoc_failed) {
                        /* give IBSS a bit more time */
-                       timeout = ssid->mode ? 10 : 5;
+                       timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
                } else if (wpa_s->conf->ap_scan == 1) {
                        /* give IBSS a bit more time */
-                       timeout = ssid->mode ? 20 : 10;
+                       timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
                }
                wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
        }
@@ -1274,6 +1285,7 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
        }
        old_ssid = wpa_s->current_ssid;
        wpa_s->current_ssid = ssid;
+       wpa_s->current_bss = bss;
        wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
        wpa_supplicant_initiate_eapol(wpa_s);
        if (old_ssid != wpa_s->current_ssid)
@@ -1306,6 +1318,7 @@ void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
        wpa_supplicant_mark_disassoc(wpa_s);
        old_ssid = wpa_s->current_ssid;
        wpa_s->current_ssid = NULL;
+       wpa_s->current_bss = NULL;
        wpa_sm_set_config(wpa_s->wpa, NULL);
        eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
        if (old_ssid != wpa_s->current_ssid)
@@ -1339,6 +1352,7 @@ void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
        wpa_supplicant_mark_disassoc(wpa_s);
        old_ssid = wpa_s->current_ssid;
        wpa_s->current_ssid = NULL;
+       wpa_s->current_bss = NULL;
        wpa_sm_set_config(wpa_s->wpa, NULL);
        eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
        if (old_ssid != wpa_s->current_ssid)
@@ -1534,94 +1548,17 @@ int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
        wpa_debug_timestamp = debug_timestamp ? 1 : 0;
        wpa_debug_show_keys = debug_show_keys ? 1 : 0;
 
-       if (wpa_debug_level != old_level ||
-           wpa_debug_timestamp != old_timestamp ||
-           wpa_debug_show_keys != old_show_keys)
-               wpas_notify_debug_params_changed(global);
+       if (wpa_debug_level != old_level)
+               wpas_notify_debug_level_changed(global);
+       if (wpa_debug_timestamp != old_timestamp)
+               wpas_notify_debug_timestamp_changed(global);
+       if (wpa_debug_show_keys != old_show_keys)
+               wpas_notify_debug_show_keys_changed(global);
 
        return 0;
 }
 
 
-static void notify_bss_changes(struct wpa_supplicant *wpa_s,
-                              u8 (*prev_bssids)[ETH_ALEN], int prev_num,
-                              struct wpa_scan_results *new)
-{
-       int new_num, i, j;
-
-       new_num = new != NULL ? new->num : 0;
-       if (prev_bssids == NULL)
-               prev_num = 0;
-
-       for (i = 0; i < prev_num; i++) {
-               for (j = 0; j < new_num; j++) {
-                       if (!os_memcmp(prev_bssids[i], new->res[j]->bssid,
-                                      ETH_ALEN))
-                               break;
-               }
-               if (j == new_num)
-                       wpas_notify_bss_removed(wpa_s, prev_bssids[i]);
-       }
-       for (i = 0; i < new_num; i++) {
-               for (j = 0; j < prev_num; j++) {
-                       if (!os_memcmp(new->res[i]->bssid, prev_bssids[j],
-                                      ETH_ALEN))
-                               break;
-               }
-               if (j == prev_num)
-                       wpas_notify_bss_added(wpa_s, new->res[i]->bssid);
-       }
-}
-
-
-/**
- * wpa_supplicant_get_scan_results - Get scan results
- * @wpa_s: Pointer to wpa_supplicant data
- * Returns: 0 on success, -1 on failure
- *
- * This function is request the current scan results from the driver and stores
- * a local copy of the results in wpa_s->scan_res.
- */
-int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
-{
-       int ret, i, prev_scan_res_num;
-       u8 (*prev_scan_bssids)[ETH_ALEN];
-
-       prev_scan_res_num = wpa_s->scan_res ? wpa_s->scan_res->num : 0;
-       prev_scan_bssids = os_malloc(prev_scan_res_num * ETH_ALEN);
-
-       if (prev_scan_bssids) {
-               for (i = 0; i < prev_scan_res_num; i++) {
-                       os_memcpy(prev_scan_bssids[i],
-                                 wpa_s->scan_res->res[i]->bssid, ETH_ALEN);
-               }
-       } else {
-               wpa_printf(MSG_WARNING, "Not enough memory for old scan "
-                          "results list");
-               prev_scan_res_num = 0;
-       }
-
-       wpa_scan_results_free(wpa_s->scan_res);
-       if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
-               wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
-       else
-               wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
-       if (wpa_s->scan_res == NULL) {
-               wpa_printf(MSG_DEBUG, "Failed to get scan results");
-               ret = -1;
-       } else {
-               ret = 0;
-               wpa_scan_sort_results(wpa_s->scan_res);
-       }
-
-       notify_bss_changes(wpa_s, prev_scan_bssids, prev_scan_res_num,
-                          wpa_s->scan_res);
-       os_free(prev_scan_bssids);
-
-       return ret;
-}
-
-
 /**
  * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  * @wpa_s: Pointer to wpa_supplicant data
@@ -1728,6 +1665,20 @@ static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
 }
 
 
+/**
+ * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
+ * @ctx: Context pointer (wpa_s); this is the ctx variable registered
+ *     with struct wpa_driver_ops::init()
+ * @src_addr: Source address of the EAPOL frame
+ * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
+ * @len: Length of the EAPOL data
+ *
+ * This function is called for each received EAPOL frame. Most driver
+ * interfaces rely on more generic OS mechanism for receiving frames through
+ * l2_packet, but if such a mechanism is not available, the driver wrapper may
+ * take care of received EAPOL frames and deliver them to the core supplicant
+ * code by calling this function.
+ */
 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
                             const u8 *buf, size_t len)
 {
@@ -1794,7 +1745,7 @@ void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
 
 #ifdef CONFIG_IBSS_RSN
        if (wpa_s->current_ssid &&
-           wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS) {
+           wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
                ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
                return;
        }
@@ -1825,14 +1776,6 @@ void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
 }
 
 
-void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
-                          struct ieee80211_rx_status *rx_status)
-{
-       struct wpa_supplicant *wpa_s = ctx;
-       ieee80211_sta_rx(wpa_s, buf, len, rx_status);
-}
-
-
 /**
  * wpa_supplicant_driver_init - Initialize driver interface parameters
  * @wpa_s: Pointer to wpa_supplicant data
@@ -1893,8 +1836,11 @@ int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
        wpa_drv_flush_pmkid(wpa_s);
 
        wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
-       wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
-       interface_count++;
+       if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
+               wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
+               interface_count++;
+       } else
+               wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
 
        return 0;
 }
@@ -1915,6 +1861,7 @@ static struct wpa_supplicant * wpa_supplicant_alloc(void)
        if (wpa_s == NULL)
                return NULL;
        wpa_s->scan_req = 1;
+       wpa_s->new_connection = 1;
 
        return wpa_s;
 }
@@ -2016,7 +1963,7 @@ next_driver:
        wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
        if (wpa_s->drv_priv == NULL) {
                const char *pos;
-               pos = os_strchr(driver, ',');
+               pos = driver ? os_strchr(driver, ',') : NULL;
                if (pos) {
                        wpa_printf(MSG_DEBUG, "Failed to initialize driver "
                                   "interface - try next driver wrapper");
@@ -2071,6 +2018,18 @@ next_driver:
                return -1;
        }
 
+       if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
+               wpa_s->drv_flags = capa.flags;
+               if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
+                       if (ieee80211_sta_init(wpa_s))
+                               return -1;
+               }
+               wpa_s->max_scan_ssids = capa.max_scan_ssids;
+               wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
+       }
+       if (wpa_s->max_remain_on_chan == 0)
+               wpa_s->max_remain_on_chan = 1000;
+
        if (wpa_supplicant_driver_init(wpa_s) < 0)
                return -1;
 
@@ -2103,15 +2062,6 @@ next_driver:
                return -1;
        }
 
-       if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
-               wpa_s->drv_flags = capa.flags;
-               if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
-                       if (ieee80211_sta_init(wpa_s))
-                               return -1;
-               }
-               wpa_s->max_scan_ssids = capa.max_scan_ssids;
-       }
-
 #ifdef CONFIG_IBSS_RSN
        wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
        if (!wpa_s->ibss_rsn) {
@@ -2120,11 +2070,15 @@ next_driver:
        }
 #endif /* CONFIG_IBSS_RSN */
 
+       if (wpa_bss_init(wpa_s) < 0)
+               return -1;
+
        return 0;
 }
 
 
-static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
+static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
+                                       int notify)
 {
        if (wpa_s->drv_priv) {
                wpa_supplicant_deauthenticate(wpa_s,
@@ -2134,10 +2088,11 @@ static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
                wpa_clear_keys(wpa_s, NULL);
        }
 
-       wpas_notify_iface_removed(wpa_s);
-
        wpa_supplicant_cleanup(wpa_s);
 
+       if (notify)
+               wpas_notify_iface_removed(wpa_s);
+
        if (wpa_s->drv_priv)
                wpa_drv_deinit(wpa_s);
 }
@@ -2160,6 +2115,7 @@ struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
 {
        struct wpa_supplicant *wpa_s;
        struct wpa_interface t_iface;
+       struct wpa_ssid *ssid;
 
        if (global == NULL || iface == NULL)
                return NULL;
@@ -2188,18 +2144,21 @@ struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
        if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
                wpa_printf(MSG_DEBUG, "Failed to add interface %s",
                           iface->ifname);
-               wpa_supplicant_deinit_iface(wpa_s);
+               wpa_supplicant_deinit_iface(wpa_s, 0);
                os_free(wpa_s);
                return NULL;
        }
 
        /* Notify the control interfaces about new iface */
        if (wpas_notify_iface_added(wpa_s)) {
-               wpa_supplicant_deinit_iface(wpa_s);
+               wpa_supplicant_deinit_iface(wpa_s, 1);
                os_free(wpa_s);
                return NULL;
        }
 
+       for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
+               wpas_notify_network_added(wpa_s, ssid);
+
        wpa_s->next = global->ifaces;
        global->ifaces = wpa_s;
 
@@ -2239,7 +2198,7 @@ int wpa_supplicant_remove_iface(struct wpa_global *global,
 
        wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
 
-       wpa_supplicant_deinit_iface(wpa_s);
+       wpa_supplicant_deinit_iface(wpa_s, 1);
        os_free(wpa_s);
 
        return 0;
@@ -2286,7 +2245,7 @@ struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
        if (params->wpa_debug_syslog)
                wpa_debug_open_syslog();
 
-       ret = eap_peer_register_methods();
+       ret = eap_register_methods();
        if (ret) {
                wpa_printf(MSG_ERROR, "Failed to register EAP methods");
                if (ret == -2)
@@ -2295,17 +2254,6 @@ struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
                return NULL;
        }
 
-#ifdef CONFIG_AP
-       ret = eap_server_register_methods();
-       if (ret) {
-               wpa_printf(MSG_ERROR, "Failed to register EAP server methods");
-               if (ret == -2)
-                       wpa_printf(MSG_ERROR, "Two or more EAP methods used "
-                                  "the same EAP type.");
-               return NULL;
-       }
-#endif /* CONFIG_AP */
-
        global = os_zalloc(sizeof(*global));
        if (global == NULL)
                return NULL;
@@ -2330,7 +2278,7 @@ struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
        wpa_debug_timestamp = global->params.wpa_debug_timestamp =
                params->wpa_debug_timestamp;
 
-       if (eloop_init(global)) {
+       if (eloop_init()) {
                wpa_printf(MSG_ERROR, "Failed to initialize event loop");
                wpa_supplicant_deinit(global);
                return NULL;
@@ -2399,8 +2347,8 @@ int wpa_supplicant_run(struct wpa_global *global)
                                        wpa_s->ctrl_iface);
        }
 
-       eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
-       eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
+       eloop_register_signal_terminate(wpa_supplicant_terminate, global);
+       eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
 
        eloop_run();