Do not trigger initial scan if there are no enabled networks
[libeap.git] / wpa_supplicant / wpa_supplicant.c
index fc42d29..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 "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"
@@ -519,6 +520,8 @@ const char * wpa_supplicant_state_txt(enum 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));
@@ -526,8 +529,6 @@ void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
        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;
@@ -541,12 +542,16 @@ void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
                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);
 }
 
 
@@ -582,12 +587,15 @@ static void wpa_supplicant_terminate(int sig, void *signal_ctx)
 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
 {
        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);
 }
 
 
@@ -680,7 +688,7 @@ static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
 }
 
 
-static enum wpa_cipher cipher_suite2driver(int cipher)
+enum wpa_cipher cipher_suite2driver(int cipher)
 {
        switch (cipher) {
        case WPA_CIPHER_NONE:
@@ -698,7 +706,7 @@ static enum wpa_cipher cipher_suite2driver(int cipher)
 }
 
 
-static enum 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:
@@ -762,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");
@@ -831,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 "
@@ -938,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) {
@@ -951,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)) {
@@ -982,7 +991,7 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
        u8 wpa_ie[80];
        size_t wpa_ie_len;
        int use_crypt, ret, i, bssid_changed;
-       int algs = AUTH_ALG_OPEN_SYSTEM;
+       int algs = WPA_AUTH_ALG_OPEN;
        enum wpa_cipher cipher_pairwise, cipher_group;
        struct wpa_driver_associate_params params;
        int wep_keys_set = 0;
@@ -1029,10 +1038,10 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
                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
@@ -1061,21 +1070,15 @@ 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);
        }
@@ -1207,18 +1210,8 @@ 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) {
+       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 &&
@@ -1567,56 +1560,6 @@ int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
 
 
 /**
- * wpa_supplicant_get_scan_results - Get scan results
- * @wpa_s: Pointer to wpa_supplicant data
- * @info: Information about what was scanned or %NULL if not available
- * @new_scan: Whether a new scan was performed
- * Returns: Scan results, %NULL on failure
- *
- * This function request the current scan results from the driver and updates
- * the local BSS list wpa_s->bss. The caller is responsible for freeing the
- * results with wpa_scan_results_free().
- */
-struct wpa_scan_results *
-wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
-                               struct scan_info *info, int new_scan)
-{
-       struct wpa_scan_results *scan_res;
-       size_t i;
-
-       if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
-               scan_res = ieee80211_sta_get_scan_results(wpa_s);
-       else
-               scan_res = wpa_drv_get_scan_results2(wpa_s);
-       if (scan_res == NULL) {
-               wpa_printf(MSG_DEBUG, "Failed to get scan results");
-               return NULL;
-       }
-
-       wpa_scan_sort_results(scan_res);
-
-       wpa_bss_update_start(wpa_s);
-       for (i = 0; i < scan_res->num; i++)
-               wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
-       wpa_bss_update_end(wpa_s, info, new_scan);
-
-       return scan_res;
-}
-
-
-int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
-{
-       struct wpa_scan_results *scan_res;
-       scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
-       if (scan_res == NULL)
-               return -1;
-       wpa_scan_results_free(scan_res);
-
-       return 0;
-}
-
-
-/**
  * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  * @wpa_s: Pointer to wpa_supplicant data
  * Returns: A pointer to the current network structure or %NULL on failure
@@ -1722,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)
 {
@@ -1879,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;
 }
@@ -2058,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;
 
@@ -2090,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) {