mesh: Fix AEK derivation to use AKM suite selector
[mech_eap.git] / wpa_supplicant / mesh_rsn.c
index 5d88274..57e5b15 100644 (file)
 
 #define MESH_AUTH_TIMEOUT 10
 #define MESH_AUTH_RETRY 3
-#define MESH_AUTH_BLOCK_DURATION 3600
 
 void mesh_auth_timer(void *eloop_ctx, void *user_data)
 {
        struct wpa_supplicant *wpa_s = eloop_ctx;
        struct sta_info *sta = user_data;
+       struct hostapd_data *hapd;
 
        if (sta->sae->state != SAE_ACCEPTED) {
                wpa_printf(MSG_DEBUG, "AUTH: Re-authenticate with " MACSTR
@@ -43,23 +43,20 @@ void mesh_auth_timer(void *eloop_ctx, void *user_data)
                if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
                        mesh_rsn_auth_sae_sta(wpa_s, sta);
                } else {
+                       hapd = wpa_s->ifmsh->bss[0];
+
                        if (sta->sae_auth_retry > MESH_AUTH_RETRY) {
-                               ap_free_sta(wpa_s->ifmsh->bss[0], sta);
+                               ap_free_sta(hapd, sta);
                                return;
                        }
 
                        /* block the STA if exceeded the number of attempts */
                        wpa_mesh_set_plink_state(wpa_s, sta, PLINK_BLOCKED);
                        sta->sae->state = SAE_NOTHING;
-                       if (wpa_s->mesh_auth_block_duration <
-                           MESH_AUTH_BLOCK_DURATION)
-                               wpa_s->mesh_auth_block_duration += 60;
-                       eloop_register_timeout(wpa_s->mesh_auth_block_duration,
-                                              0, mesh_auth_timer, wpa_s, sta);
                        wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_BLOCKED "addr="
                                MACSTR " duration=%d",
                                MAC2STR(sta->addr),
-                               wpa_s->mesh_auth_block_duration);
+                               hapd->conf->ap_max_inactivity);
                }
                sta->sae_auth_retry++;
        }
@@ -139,7 +136,8 @@ static int auth_start_ampe(void *ctx, const u8 *addr)
 }
 
 
-static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr)
+static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr,
+                               enum mfp_options ieee80211w)
 {
        struct wpa_auth_config conf;
        struct wpa_auth_callbacks cb;
@@ -155,6 +153,11 @@ static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr)
        conf.wpa_group = WPA_CIPHER_CCMP;
        conf.eapol_version = 0;
        conf.wpa_group_rekey = -1;
+#ifdef CONFIG_IEEE80211W
+       conf.ieee80211w = ieee80211w;
+       if (ieee80211w != NO_MGMT_FRAME_PROTECTION)
+               conf.group_mgmt_cipher = WPA_CIPHER_AES_128_CMAC;
+#endif /* CONFIG_IEEE80211W */
 
        os_memset(&cb, 0, sizeof(cb));
        cb.ctx = rsn;
@@ -170,10 +173,8 @@ static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr)
        }
 
        /* TODO: support rekeying */
-       if (random_get_bytes(rsn->mgtk, 16) < 0) {
-               wpa_deinit(rsn->auth);
+       if (random_get_bytes(rsn->mgtk, 16) < 0)
                return -1;
-       }
 
        /* group mgmt */
        wpa_drv_set_key(rsn->wpa_s, WPA_ALG_IGTK, NULL, 4, 1,
@@ -208,7 +209,8 @@ struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
                return NULL;
        mesh_rsn->wpa_s = wpa_s;
 
-       if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr) < 0) {
+       if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr,
+                                conf->ieee80211w) < 0) {
                mesh_rsn_deinit(mesh_rsn);
                os_free(mesh_rsn);
                return NULL;
@@ -291,6 +293,7 @@ int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
 {
        struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
        struct wpa_ssid *ssid = wpa_s->current_ssid;
+       struct rsn_pmksa_cache_entry *pmksa;
        unsigned int rnd;
        int ret;
 
@@ -306,6 +309,29 @@ int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
                        return -1;
        }
 
+       pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr);
+       if (pmksa) {
+               if (!sta->wpa_sm)
+                       sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
+                                                       sta->addr, NULL);
+               if (!sta->wpa_sm) {
+                       wpa_printf(MSG_ERROR,
+                                  "mesh: Failed to initialize RSN state machine");
+                       return -1;
+               }
+
+               wpa_printf(MSG_DEBUG,
+                          "AUTH: Mesh PMKSA cache entry found for " MACSTR
+                          " - try to use PMKSA caching instead of new SAE authentication",
+                          MAC2STR(sta->addr));
+               wpa_auth_pmksa_set_to_sm(pmksa, sta->wpa_sm, hapd->wpa_auth,
+                                        sta->sae->pmkid, sta->sae->pmk);
+               sae_accept_sta(hapd, sta);
+               sta->mesh_sae_pmksa_caching = 1;
+               return 0;
+       }
+       sta->mesh_sae_pmksa_caching = 0;
+
        if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
                return -1;
 
@@ -340,8 +366,8 @@ mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
        u8 *addr1 = peer, *addr2 = myaddr;
        u8 context[AES_BLOCK_SIZE];
 
-       /* SAE */
-       RSN_SELECTOR_PUT(context, wpa_cipher_to_suite(0, WPA_CIPHER_GCMP));
+       /* Selected AKM Suite: SAE */
+       RSN_SELECTOR_PUT(context, RSN_AUTH_KEY_MGMT_SAE);
 
        if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
                addr1 = myaddr;
@@ -361,7 +387,6 @@ int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
        u8 *ptr;
        u8 *min, *max;
        u16 min_lid, max_lid;
-       size_t nonce_len = sizeof(sta->my_nonce);
        size_t lid_len = sizeof(sta->my_lid);
        u8 *myaddr = wpa_s->own_addr;
        u8 *peer = sta->addr;
@@ -369,16 +394,16 @@ int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
        u8 context[64 + 4 + 4 + 12];
 
        ptr = context;
-       if (os_memcmp(sta->my_nonce, sta->peer_nonce, nonce_len) < 0) {
+       if (os_memcmp(sta->my_nonce, sta->peer_nonce, WPA_NONCE_LEN) < 0) {
                min = sta->my_nonce;
                max = sta->peer_nonce;
        } else {
                min = sta->peer_nonce;
                max = sta->my_nonce;
        }
-       os_memcpy(ptr, min, nonce_len);
-       os_memcpy(ptr + nonce_len, max, nonce_len);
-       ptr += 2 * nonce_len;
+       os_memcpy(ptr, min, WPA_NONCE_LEN);
+       os_memcpy(ptr + WPA_NONCE_LEN, max, WPA_NONCE_LEN);
+       ptr += 2 * WPA_NONCE_LEN;
 
        if (sta->my_lid < sta->peer_lid) {
                min_lid = host_to_le16(sta->my_lid);
@@ -414,11 +439,11 @@ int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
 
 void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
 {
-       if (random_get_bytes(sta->my_nonce, 32) < 0) {
+       if (random_get_bytes(sta->my_nonce, WPA_NONCE_LEN) < 0) {
                wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
                /* TODO: How to handle this more cleanly? */
        }
-       os_memset(sta->peer_nonce, 0, 32);
+       os_memset(sta->peer_nonce, 0, WPA_NONCE_LEN);
        mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
 }
 
@@ -464,8 +489,8 @@ int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
 
        RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
                     wpa_cipher_to_suite(WPA_PROTO_RSN, WPA_CIPHER_CCMP));
-       os_memcpy(ampe->local_nonce, sta->my_nonce, 32);
-       os_memcpy(ampe->peer_nonce, sta->peer_nonce, 32);
+       os_memcpy(ampe->local_nonce, sta->my_nonce, WPA_NONCE_LEN);
+       os_memcpy(ampe->peer_nonce, sta->peer_nonce, WPA_NONCE_LEN);
        /* incomplete: see 13.5.4 */
        /* TODO: static mgtk for now since we don't support rekeying! */
        os_memcpy(ampe->mgtk, rsn->mgtk, 16);
@@ -505,7 +530,7 @@ int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
 {
        int ret = 0;
        struct ieee80211_ampe_ie *ampe;
-       u8 null_nonce[32] = {};
+       u8 null_nonce[WPA_NONCE_LEN] = {};
        u8 ampe_eid;
        u8 ampe_ie_len;
        u8 *ampe_buf, *crypt = NULL;
@@ -514,6 +539,17 @@ int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
        const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
                                   (elems->mic - 2) - cat };
 
+       if (!sta->sae) {
+               struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
+
+               if (!wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr)) {
+                       wpa_printf(MSG_INFO,
+                                  "Mesh RSN: SAE is not prepared yet");
+                       return -1;
+               }
+               mesh_rsn_auth_sae_sta(wpa_s, sta);
+       }
+
        if (chosen_pmk && os_memcmp(chosen_pmk, sta->sae->pmkid, PMKID_LEN)) {
                wpa_msg(wpa_s, MSG_DEBUG,
                        "Mesh RSN: Invalid PMKID (Chosen PMK did not match calculated PMKID)");
@@ -563,8 +599,8 @@ int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
        }
 
        ampe = (struct ieee80211_ampe_ie *) ampe_buf;
-       if (os_memcmp(ampe->peer_nonce, null_nonce, 32) != 0 &&
-           os_memcmp(ampe->peer_nonce, sta->my_nonce, 32) != 0) {
+       if (os_memcmp(ampe->peer_nonce, null_nonce, WPA_NONCE_LEN) != 0 &&
+           os_memcmp(ampe->peer_nonce, sta->my_nonce, WPA_NONCE_LEN) != 0) {
                wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
                ret = -1;
                goto free;