mesh: Indicate OPN_RJCT event if AES-SIV decrypt fails
[mech_eap.git] / wpa_supplicant / mesh_mpm.c
index 21b908a..8afdc16 100644 (file)
@@ -14,6 +14,7 @@
 #include "ap/hostapd.h"
 #include "ap/sta_info.h"
 #include "ap/ieee802_11.h"
+#include "ap/wpa_auth.h"
 #include "wpa_supplicant_i.h"
 #include "driver_i.h"
 #include "mesh_mpm.h"
@@ -43,6 +44,7 @@ enum plink_event {
 };
 
 static const char * const mplstate[] = {
+       [0] = "UNINITIALIZED",
        [PLINK_LISTEN] = "LISTEN",
        [PLINK_OPEN_SENT] = "OPEN_SENT",
        [PLINK_OPEN_RCVD] = "OPEN_RCVD",
@@ -288,7 +290,8 @@ static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
                /* TODO: Add Connected to Mesh Gate/AS subfields */
                wpabuf_put_u8(buf, info);
                /* always forwarding & accepting plinks for now */
-               wpabuf_put_u8(buf, 0x1 | 0x8);
+               wpabuf_put_u8(buf, MESH_CAP_ACCEPT_ADDITIONAL_PEER |
+                             MESH_CAP_FORWARDING);
        } else {        /* Peer closing frame */
                /* IE: Mesh ID */
                wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
@@ -360,6 +363,9 @@ static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
                goto fail;
        }
 
+       wpa_msg(wpa_s, MSG_DEBUG, "Mesh MPM: Sending peering frame type %d to "
+               MACSTR " (my_lid=0x%x peer_lid=0x%x)",
+               type, MAC2STR(sta->addr), sta->my_lid, sta->peer_lid);
        ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
                                  sta->addr, wpa_s->own_addr, wpa_s->own_addr,
                                  wpabuf_head(buf), wpabuf_len(buf), 0);
@@ -380,6 +386,9 @@ void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
        struct hostapd_sta_add_params params;
        int ret;
 
+       wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " from %s into %s",
+               MAC2STR(sta->addr), mplstate[sta->plink_state],
+               mplstate[state]);
        sta->plink_state = state;
 
        os_memset(&params, 0, sizeof(params));
@@ -387,8 +396,6 @@ void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
        params.plink_state = state;
        params.set = 1;
 
-       wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " into %s",
-               MAC2STR(sta->addr), mplstate[state]);
        ret = wpa_drv_sta_add(wpa_s, &params);
        if (ret) {
                wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
@@ -414,6 +421,7 @@ static void plink_timer(void *eloop_ctx, void *user_data)
        struct sta_info *sta = user_data;
        u16 reason = 0;
        struct mesh_conf *conf = wpa_s->ifmsh->mconf;
+       struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
 
        switch (sta->plink_state) {
        case PLINK_OPEN_RCVD:
@@ -443,6 +451,13 @@ static void plink_timer(void *eloop_ctx, void *user_data)
                break;
        case PLINK_HOLDING:
                /* holding timer */
+
+               if (sta->mesh_sae_pmksa_caching) {
+                       wpa_printf(MSG_DEBUG, "MPM: Peer " MACSTR
+                                  " looks like it does not support mesh SAE PMKSA caching, so remove the cached entry for it",
+                                  MAC2STR(sta->addr));
+                       wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
+               }
                mesh_mpm_fsm_restart(wpa_s, sta);
                break;
        default:
@@ -467,8 +482,8 @@ mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
 }
 
 
-int mesh_mpm_plink_close(struct hostapd_data *hapd,
-                        struct sta_info *sta, void *ctx)
+static int mesh_mpm_plink_close(struct hostapd_data *hapd, struct sta_info *sta,
+                               void *ctx)
 {
        struct wpa_supplicant *wpa_s = ctx;
        int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
@@ -486,6 +501,85 @@ int mesh_mpm_plink_close(struct hostapd_data *hapd,
 }
 
 
+int mesh_mpm_close_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
+{
+       struct hostapd_data *hapd;
+       struct sta_info *sta;
+
+       if (!wpa_s->ifmsh) {
+               wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
+               return -1;
+       }
+
+       hapd = wpa_s->ifmsh->bss[0];
+       sta = ap_get_sta(hapd, addr);
+       if (!sta) {
+               wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
+               return -1;
+       }
+
+       return mesh_mpm_plink_close(hapd, sta, wpa_s) == 0 ? 0 : -1;
+}
+
+
+static void peer_add_timer(void *eloop_ctx, void *user_data)
+{
+       struct wpa_supplicant *wpa_s = eloop_ctx;
+       struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
+
+       os_memset(hapd->mesh_required_peer, 0, ETH_ALEN);
+}
+
+
+int mesh_mpm_connect_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
+                         int duration)
+{
+       struct wpa_ssid *ssid = wpa_s->current_ssid;
+       struct hostapd_data *hapd;
+       struct sta_info *sta;
+       struct mesh_conf *conf;
+
+       if (!wpa_s->ifmsh) {
+               wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
+               return -1;
+       }
+
+       if (!ssid || !ssid->no_auto_peer) {
+               wpa_msg(wpa_s, MSG_INFO,
+                       "This command is available only with no_auto_peer mesh network");
+               return -1;
+       }
+
+       hapd = wpa_s->ifmsh->bss[0];
+       conf = wpa_s->ifmsh->mconf;
+
+       sta = ap_get_sta(hapd, addr);
+       if (!sta) {
+               wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
+               return -1;
+       }
+
+       if ((PLINK_OPEN_SENT <= sta->plink_state &&
+           sta->plink_state <= PLINK_ESTAB) ||
+           (sta->sae && sta->sae->state > SAE_NOTHING)) {
+               wpa_msg(wpa_s, MSG_INFO,
+                       "Specified peer is connecting/connected");
+               return -1;
+       }
+
+       if (conf->security == MESH_CONF_SEC_NONE) {
+               mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
+       } else {
+               mesh_rsn_auth_sae_sta(wpa_s, sta);
+               os_memcpy(hapd->mesh_required_peer, addr, ETH_ALEN);
+               eloop_register_timeout(duration == -1 ? 10 : duration, 0,
+                                      peer_add_timer, wpa_s, NULL);
+       }
+
+       return 0;
+}
+
+
 void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
 {
        struct hostapd_data *hapd = ifmsh->bss[0];
@@ -495,6 +589,7 @@ void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
 
        hapd->num_plinks = 0;
        hostapd_free_stas(hapd);
+       eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
 }
 
 
@@ -555,6 +650,14 @@ static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
        struct sta_info *sta;
        int ret;
 
+       if (elems->mesh_config_len >= 7 &&
+           !(elems->mesh_config[6] & MESH_CAP_ACCEPT_ADDITIONAL_PEER)) {
+               wpa_msg(wpa_s, MSG_DEBUG,
+                       "mesh: Ignore a crowded peer " MACSTR,
+                       MAC2STR(addr));
+               return NULL;
+       }
+
        sta = ap_get_sta(data, addr);
        if (!sta) {
                sta = ap_sta_add(data, addr);
@@ -571,7 +674,8 @@ static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
                return NULL;
        }
 
-       mesh_mpm_init_link(wpa_s, sta);
+       if (!sta->my_lid)
+               mesh_mpm_init_link(wpa_s, sta);
 
 #ifdef CONFIG_IEEE80211N
        copy_sta_ht_capab(data, sta, elems->ht_capabilities);
@@ -634,7 +738,9 @@ void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
        if (!sta)
                return;
 
-       if (ssid && ssid->no_auto_peer) {
+       if (ssid && ssid->no_auto_peer &&
+           (is_zero_ether_addr(data->mesh_required_peer) ||
+            os_memcmp(data->mesh_required_peer, addr, ETH_ALEN) != 0)) {
                wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
                        MACSTR " because of no_auto_peer", MAC2STR(addr));
                if (data->mesh_pending_auth) {
@@ -663,10 +769,13 @@ void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
                return;
        }
 
-       if (conf->security == MESH_CONF_SEC_NONE)
-               mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
-       else
+       if (conf->security == MESH_CONF_SEC_NONE) {
+               if (sta->plink_state < PLINK_OPEN_SENT ||
+                   sta->plink_state > PLINK_ESTAB)
+                       mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
+       } else {
                mesh_rsn_auth_sae_sta(wpa_s, sta);
+       }
 }
 
 
@@ -693,25 +802,42 @@ static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
                MAC2STR(sta->addr));
 
        if (conf->security & MESH_CONF_SEC_AMPE) {
-               wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
-                               seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
-               wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
-                               seq, sizeof(seq),
-                               sta->mgtk, sizeof(sta->mgtk));
-               wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
-                               seq, sizeof(seq),
-                               sta->mgtk, sizeof(sta->mgtk));
-
-               wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
-               wpa_hexdump_key(MSG_DEBUG, "mgtk:",
-                               sta->mgtk, sizeof(sta->mgtk));
+               wpa_hexdump_key(MSG_DEBUG, "mesh: MTK", sta->mtk, sta->mtk_len);
+               wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->pairwise_cipher),
+                               sta->addr, 0, 0, seq, sizeof(seq),
+                               sta->mtk, sta->mtk_len);
+
+               wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK Key RSC",
+                               sta->mgtk_rsc, sizeof(sta->mgtk_rsc));
+               wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK",
+                               sta->mgtk, sta->mgtk_len);
+               wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->group_cipher),
+                               sta->addr, sta->mgtk_key_id, 0,
+                               sta->mgtk_rsc, sizeof(sta->mgtk_rsc),
+                               sta->mgtk, sta->mgtk_len);
+
+               if (sta->igtk_len) {
+                       wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK Key RSC",
+                                       sta->igtk_rsc, sizeof(sta->igtk_rsc));
+                       wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK",
+                                       sta->igtk, sta->igtk_len);
+                       wpa_drv_set_key(
+                               wpa_s,
+                               wpa_cipher_to_alg(conf->mgmt_group_cipher),
+                               sta->addr, sta->igtk_key_id, 0,
+                               sta->igtk_rsc, sizeof(sta->igtk_rsc),
+                               sta->igtk, sta->igtk_len);
+               }
        }
 
        wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
        hapd->num_plinks++;
 
        sta->flags |= WLAN_STA_ASSOC;
+       sta->mesh_sae_pmksa_caching = 0;
 
+       eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
+       peer_add_timer(wpa_s, NULL);
        eloop_cancel_timeout(plink_timer, wpa_s, sta);
 
        /* Send ctrl event */
@@ -771,6 +897,7 @@ static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
                        break;
                case CNF_ACPT:
                        wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
+                       eloop_cancel_timeout(plink_timer, wpa_s, sta);
                        eloop_register_timeout(
                                conf->dot11MeshConfirmTimeout / 1000,
                                (conf->dot11MeshConfirmTimeout % 1000) * 1000,
@@ -830,6 +957,8 @@ static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
                                                   PLINK_CLOSE, reason);
                        break;
                case OPN_ACPT:
+                       if (conf->security & MESH_CONF_SEC_AMPE)
+                               mesh_rsn_derive_mtk(wpa_s, sta);
                        mesh_mpm_plink_estab(wpa_s, sta);
                        mesh_mpm_send_plink_action(wpa_s, sta,
                                                   PLINK_CONFIRM, 0);
@@ -984,6 +1113,10 @@ void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
                llid = WPA_GET_LE16(peer_mgmt_ie.plid);
        wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
 
+       if (action_field == PLINK_CLOSE)
+               wpa_printf(MSG_DEBUG, "MPM: close reason=%u",
+                          WPA_GET_LE16(peer_mgmt_ie.reason));
+
        sta = ap_get_sta(hapd, mgmt->sa);
 
        /*
@@ -991,7 +1124,8 @@ void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
         * open mesh, then go ahead and add the peer before proceeding.
         */
        if (!sta && action_field == PLINK_OPEN &&
-           !(mconf->security & MESH_CONF_SEC_AMPE))
+           (!(mconf->security & MESH_CONF_SEC_AMPE) ||
+            wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa)))
                sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
 
        if (!sta) {
@@ -1010,13 +1144,23 @@ void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
        if (!sta->my_lid)
                mesh_mpm_init_link(wpa_s, sta);
 
-       if ((mconf->security & MESH_CONF_SEC_AMPE) &&
-           mesh_rsn_process_ampe(wpa_s, sta, &elems,
-                                 &mgmt->u.action.category,
-                                 peer_mgmt_ie.chosen_pmk,
-                                 ies, ie_len)) {
-               wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
-               return;
+       if (mconf->security & MESH_CONF_SEC_AMPE) {
+               int res;
+
+               res = mesh_rsn_process_ampe(wpa_s, sta, &elems,
+                                           &mgmt->u.action.category,
+                                           peer_mgmt_ie.chosen_pmk,
+                                           ies, ie_len);
+               if (res) {
+                       wpa_printf(MSG_DEBUG,
+                                  "MPM: RSN process rejected frame (res=%d)",
+                                  res);
+                       if (action_field == PLINK_OPEN && res == -2) {
+                               /* AES-SIV decryption failed */
+                               mesh_mpm_fsm(wpa_s, sta, OPN_RJCT);
+                       }
+                       return;
+               }
        }
 
        if (sta->plink_state == PLINK_BLOCKED) {
@@ -1086,8 +1230,10 @@ void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
 
 
 /* called by ap_free_sta */
-void mesh_mpm_free_sta(struct sta_info *sta)
+void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
 {
+       if (sta->plink_state == PLINK_ESTAB)
+               hapd->num_plinks--;
        eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
        eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
 }