Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / rsn_supp / preauth.c
index 6109f5e..c6534af 100644 (file)
@@ -1,15 +1,9 @@
 /*
  * RSN pre-authentication (supplicant)
- * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2003-2015, 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
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "includes.h"
 #include "preauth.h"
 #include "pmksa_cache.h"
 #include "wpa_i.h"
-#include "common/ieee802_11_defs.h"
 
 
-#if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA2)
+#ifdef IEEE8021X_EAPOL
 
 #define PMKID_CANDIDATE_PRIO_SCAN 1000
 
@@ -77,13 +70,14 @@ static void rsn_preauth_receive(void *ctx, const u8 *src_addr,
 }
 
 
-static void rsn_preauth_eapol_cb(struct eapol_sm *eapol, int success,
+static void rsn_preauth_eapol_cb(struct eapol_sm *eapol,
+                                enum eapol_supp_result result,
                                 void *ctx)
 {
        struct wpa_sm *sm = ctx;
        u8 pmk[PMK_LEN];
 
-       if (success) {
+       if (result == EAPOL_SUPP_RESULT_SUCCESS) {
                int res, pmk_len;
                pmk_len = PMK_LEN;
                res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
@@ -100,6 +94,7 @@ static void rsn_preauth_eapol_cb(struct eapol_sm *eapol, int success,
                                        pmk, pmk_len);
                        sm->pmk_len = pmk_len;
                        pmksa_cache_add(sm->pmksa, pmk, pmk_len,
+                                       NULL, 0,
                                        sm->preauth_bssid, sm->own_addr,
                                        sm->network_ctx,
                                        WPA_KEY_MGMT_IEEE8021X);
@@ -107,13 +102,14 @@ static void rsn_preauth_eapol_cb(struct eapol_sm *eapol, int success,
                        wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
                                "RSN: failed to get master session key from "
                                "pre-auth EAPOL state machines");
-                       success = 0;
+                       result = EAPOL_SUPP_RESULT_FAILURE;
                }
        }
 
        wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "RSN: pre-authentication with "
                MACSTR " %s", MAC2STR(sm->preauth_bssid),
-               success ? "completed successfully" : "failed");
+               result == EAPOL_SUPP_RESULT_SUCCESS ? "completed successfully" :
+               "failed");
 
        rsn_preauth_deinit(sm);
        rsn_preauth_candidate_process(sm);
@@ -176,6 +172,7 @@ int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst,
 {
        struct eapol_config eapol_conf;
        struct eapol_ctx *ctx;
+       int ret;
 
        if (sm->preauth_eapol)
                return -1;
@@ -201,14 +198,16 @@ int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst,
                        wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 "
                                   "packet processing (bridge) for "
                                   "pre-authentication");
-                       return -2;
+                       ret = -2;
+                       goto fail;
                }
        }
 
        ctx = os_zalloc(sizeof(*ctx));
        if (ctx == NULL) {
                wpa_printf(MSG_WARNING, "Failed to allocate EAPOL context.");
-               return -4;
+               ret = -4;
+               goto fail;
        }
        ctx->ctx = sm->ctx->ctx;
        ctx->msg_ctx = sm->ctx->ctx;
@@ -226,7 +225,8 @@ int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst,
                os_free(ctx);
                wpa_printf(MSG_WARNING, "RSN: Failed to initialize EAPOL "
                           "state machines for pre-authentication");
-               return -3;
+               ret = -3;
+               goto fail;
        }
        os_memset(&eapol_conf, 0, sizeof(eapol_conf));
        eapol_conf.accept_802_1x_keys = 0;
@@ -251,6 +251,15 @@ int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst,
                               rsn_preauth_timeout, sm, NULL);
 
        return 0;
+
+fail:
+       if (sm->l2_preauth_br) {
+               l2_packet_deinit(sm->l2_preauth_br);
+               sm->l2_preauth_br = NULL;
+       }
+       l2_packet_deinit(sm->l2_preauth);
+       sm->l2_preauth = NULL;
+       return ret;
 }
 
 
@@ -303,7 +312,9 @@ void rsn_preauth_candidate_process(struct wpa_sm *sm)
            sm->proto != WPA_PROTO_RSN ||
            wpa_sm_get_state(sm) != WPA_COMPLETED ||
            (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
-            sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SHA256)) {
+            sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SHA256 &&
+            sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SUITE_B &&
+            sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)) {
                wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: not in suitable "
                        "state for new pre-authentication");
                return; /* invalid state for new pre-auth */
@@ -312,7 +323,7 @@ void rsn_preauth_candidate_process(struct wpa_sm *sm)
        dl_list_for_each_safe(candidate, n, &sm->pmksa_candidates,
                              struct rsn_pmksa_candidate, list) {
                struct rsn_pmksa_cache_entry *p = NULL;
-               p = pmksa_cache_get(sm->pmksa, candidate->bssid, NULL);
+               p = pmksa_cache_get(sm->pmksa, candidate->bssid, NULL, NULL);
                if (os_memcmp(sm->bssid, candidate->bssid, ETH_ALEN) != 0 &&
                    (p == NULL || p->opportunistic)) {
                        wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA "
@@ -396,6 +407,18 @@ void pmksa_candidate_add(struct wpa_sm *sm, const u8 *bssid,
        dl_list_for_each(pos, &sm->pmksa_candidates,
                         struct rsn_pmksa_candidate, list) {
                if (cand->priority <= pos->priority) {
+                       if (!pos->list.prev) {
+                               /*
+                                * This cannot really happen in pracrice since
+                                * pos was fetched from the list and the prev
+                                * pointer must be set. It looks like clang
+                                * static analyzer gets confused with the
+                                * dl_list_del(&cand->list) call above and ends
+                                * up assuming pos->list.prev could be NULL.
+                                */
+                               os_free(cand);
+                               return;
+                       }
                        dl_list_add(pos->list.prev, &cand->list);
                        cand = NULL;
                        break;
@@ -459,7 +482,7 @@ void rsn_preauth_scan_result(struct wpa_sm *sm, const u8 *bssid,
        if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie))
                return;
 
-       pmksa = pmksa_cache_get(sm->pmksa, bssid, NULL);
+       pmksa = pmksa_cache_get(sm->pmksa, bssid, NULL, NULL);
        if (pmksa && (!pmksa->opportunistic ||
                      !(ie.capabilities & WPA_CAPABILITY_PREAUTH)))
                return;
@@ -492,7 +515,7 @@ int rsn_preauth_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
        if (sm->preauth_eapol) {
                ret = os_snprintf(pos, end - pos, "Pre-authentication "
                                  "EAPOL state machines:\n");
-               if (ret < 0 || ret >= end - pos)
+               if (os_snprintf_error(end - pos, ret))
                        return pos - buf;
                pos += ret;
                res = eapol_sm_get_status(sm->preauth_eapol,
@@ -515,4 +538,4 @@ int rsn_preauth_in_progress(struct wpa_sm *sm)
        return sm->preauth_eapol != NULL;
 }
 
-#endif /* IEEE8021X_EAPOL and !CONFIG_NO_WPA2 */
+#endif /* IEEE8021X_EAPOL */