PMKSA: Flush AP/mesh PMKSA cache by PMKSA_FLUSH command
[mech_eap.git] / src / ap / ctrl_iface_ap.c
index 5ecafe3..317b238 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Control interface for shared AP commands
- * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
  *
  * This software may be distributed under the terms of the BSD license.
  * See README for more details.
@@ -10,6 +10,9 @@
 
 #include "utils/common.h"
 #include "common/ieee802_11_defs.h"
+#include "common/sae.h"
+#include "eapol_auth/eapol_auth_sm.h"
+#include "fst/fst_ctrl_iface.h"
 #include "hostapd.h"
 #include "ieee802_1x.h"
 #include "wpa_auth.h"
@@ -19,6 +22,7 @@
 #include "p2p_hostapd.h"
 #include "ctrl_iface_ap.h"
 #include "ap_drv_ops.h"
+#include "mbo_ap.h"
 
 
 static int hostapd_get_sta_tx_rx(struct hostapd_data *hapd,
@@ -32,10 +36,10 @@ static int hostapd_get_sta_tx_rx(struct hostapd_data *hapd,
                return 0;
 
        ret = os_snprintf(buf, buflen, "rx_packets=%lu\ntx_packets=%lu\n"
-                         "rx_bytes=%lu\ntx_bytes=%lu\n",
+                         "rx_bytes=%llu\ntx_bytes=%llu\n",
                          data.rx_packets, data.tx_packets,
                          data.rx_bytes, data.tx_bytes);
-       if (ret < 0 || (size_t) ret >= buflen)
+       if (os_snprintf_error(buflen, ret))
                return 0;
        return ret;
 }
@@ -54,7 +58,7 @@ static int hostapd_get_sta_conn_time(struct sta_info *sta,
 
        ret = os_snprintf(buf, buflen, "connected_time=%u\n",
                          (unsigned int) age.sec);
-       if (ret < 0 || (size_t) ret >= buflen)
+       if (os_snprintf_error(buflen, ret))
                return 0;
        return ret;
 }
@@ -85,17 +89,13 @@ static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
 {
        int len, res, ret, i;
 
-       if (sta == NULL) {
-               ret = os_snprintf(buf, buflen, "FAIL\n");
-               if (ret < 0 || (size_t) ret >= buflen)
-                       return 0;
-               return ret;
-       }
+       if (!sta)
+               return 0;
 
        len = 0;
        ret = os_snprintf(buf + len, buflen - len, MACSTR "\nflags=",
                          MAC2STR(sta->addr));
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -107,7 +107,7 @@ static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
        ret = os_snprintf(buf + len, buflen - len, "\naid=%d\ncapability=0x%x\n"
                          "listen_interval=%d\nsupported_rates=",
                          sta->aid, sta->capability, sta->listen_interval);
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -115,14 +115,14 @@ static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
                ret = os_snprintf(buf + len, buflen - len, "%02x%s",
                                  sta->supported_rates[i],
                                  i + 1 < sta->supported_rates_len ? " " : "");
-               if (ret < 0 || (size_t) ret >= buflen - len)
+               if (os_snprintf_error(buflen - len, ret))
                        return len;
                len += ret;
        }
 
        ret = os_snprintf(buf + len, buflen - len, "\ntimeout_next=%s\n",
                          timeout_next_str(sta->timeout_next));
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -146,6 +146,35 @@ static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
        len += hostapd_get_sta_tx_rx(hapd, sta, buf + len, buflen - len);
        len += hostapd_get_sta_conn_time(sta, buf + len, buflen - len);
 
+#ifdef CONFIG_SAE
+       if (sta->sae && sta->sae->state == SAE_ACCEPTED) {
+               res = os_snprintf(buf + len, buflen - len, "sae_group=%d\n",
+                                 sta->sae->group);
+               if (!os_snprintf_error(buflen - len, res))
+                       len += res;
+       }
+#endif /* CONFIG_SAE */
+
+       if (sta->vlan_id > 0) {
+               res = os_snprintf(buf + len, buflen - len, "vlan_id=%d\n",
+                                 sta->vlan_id);
+               if (!os_snprintf_error(buflen - len, res))
+                       len += res;
+       }
+
+       res = mbo_ap_get_info(sta, buf + len, buflen - len);
+       if (res >= 0)
+               len += res;
+
+       if (sta->supp_op_classes &&
+           buflen - len > (unsigned) (17 + 2 * sta->supp_op_classes[0])) {
+               len += os_snprintf(buf + len, buflen - len, "supp_op_classes=");
+               len += wpa_snprintf_hex(buf + len, buflen - len,
+                                       sta->supp_op_classes + 1,
+                                       sta->supp_op_classes[0]);
+               len += os_snprintf(buf + len, buflen - len, "\n");
+       }
+
        return len;
 }
 
@@ -162,15 +191,40 @@ int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
 {
        u8 addr[ETH_ALEN];
        int ret;
+       const char *pos;
+       struct sta_info *sta;
 
        if (hwaddr_aton(txtaddr, addr)) {
                ret = os_snprintf(buf, buflen, "FAIL\n");
-               if (ret < 0 || (size_t) ret >= buflen)
+               if (os_snprintf_error(buflen, ret))
                        return 0;
                return ret;
        }
-       return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
-                                         buf, buflen);
+
+       sta = ap_get_sta(hapd, addr);
+       if (sta == NULL)
+               return -1;
+
+       pos = os_strchr(txtaddr, ' ');
+       if (pos) {
+               pos++;
+
+#ifdef HOSTAPD_DUMP_STATE
+               if (os_strcmp(pos, "eapol") == 0) {
+                       if (sta->eapol_sm == NULL)
+                               return -1;
+                       return eapol_auth_dump_state(sta->eapol_sm, buf,
+                                                    buflen);
+               }
+#endif /* HOSTAPD_DUMP_STATE */
+
+               return -1;
+       }
+
+       ret = hostapd_ctrl_iface_sta_mib(hapd, sta, buf, buflen);
+       ret += fst_ctrl_iface_mb_info(addr, buf + ret, buflen - ret);
+
+       return ret;
 }
 
 
@@ -184,10 +238,14 @@ int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
        if (hwaddr_aton(txtaddr, addr) ||
            (sta = ap_get_sta(hapd, addr)) == NULL) {
                ret = os_snprintf(buf, buflen, "FAIL\n");
-               if (ret < 0 || (size_t) ret >= buflen)
+               if (os_snprintf_error(buflen, ret))
                        return 0;
                return ret;
-       }               
+       }
+
+       if (!sta->next)
+               return 0;
+
        return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
 }
 
@@ -207,11 +265,12 @@ static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
        if (mgmt == NULL)
                return -1;
 
+       mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
        wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
-               " with minor reason code %u (stype=%u)",
-               MAC2STR(addr), minor_reason_code, stype);
+               " with minor reason code %u (stype=%u (%s))",
+               MAC2STR(addr), minor_reason_code, stype,
+               fc2str(mgmt->frame_control));
 
-       mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
        os_memcpy(mgmt->da, addr, ETH_ALEN);
        os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
        os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
@@ -227,9 +286,8 @@ static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
 
        *pos++ = WLAN_EID_VENDOR_SPECIFIC;
        *pos++ = 4 + 3 + 1;
-       WPA_PUT_BE24(pos, OUI_WFA);
-       pos += 3;
-       *pos++ = P2P_OUI_TYPE;
+       WPA_PUT_BE32(pos, P2P_IE_VENDOR_TYPE);
+       pos += 4;
 
        *pos++ = P2P_ATTR_MINOR_REASON_CODE;
        WPA_PUT_LE16(pos, 1);
@@ -259,6 +317,10 @@ int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
        if (hwaddr_aton(txtaddr, addr))
                return -1;
 
+       pos = os_strstr(txtaddr, " reason=");
+       if (pos)
+               reason = atoi(pos + 8);
+
        pos = os_strstr(txtaddr, " test=");
        if (pos) {
                struct ieee80211_mgmt mgmt;
@@ -273,8 +335,7 @@ int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
                os_memcpy(mgmt.da, addr, ETH_ALEN);
                os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
                os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
-               mgmt.u.deauth.reason_code =
-                       host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
+               mgmt.u.deauth.reason_code = host_to_le16(reason);
                if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
                                             IEEE80211_HDRLEN +
                                             sizeof(mgmt.u.deauth),
@@ -291,10 +352,6 @@ int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
        }
 #endif /* CONFIG_P2P_MANAGER */
 
-       pos = os_strstr(txtaddr, " reason=");
-       if (pos)
-               reason = atoi(pos + 8);
-
        hostapd_drv_sta_deauth(hapd, addr, reason);
        sta = ap_get_sta(hapd, addr);
        if (sta)
@@ -320,6 +377,10 @@ int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
        if (hwaddr_aton(txtaddr, addr))
                return -1;
 
+       pos = os_strstr(txtaddr, " reason=");
+       if (pos)
+               reason = atoi(pos + 8);
+
        pos = os_strstr(txtaddr, " test=");
        if (pos) {
                struct ieee80211_mgmt mgmt;
@@ -334,8 +395,7 @@ int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
                os_memcpy(mgmt.da, addr, ETH_ALEN);
                os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
                os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
-               mgmt.u.disassoc.reason_code =
-                       host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
+               mgmt.u.disassoc.reason_code = host_to_le16(reason);
                if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
                                             IEEE80211_HDRLEN +
                                             sizeof(mgmt.u.deauth),
@@ -352,10 +412,6 @@ int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
        }
 #endif /* CONFIG_P2P_MANAGER */
 
-       pos = os_strstr(txtaddr, " reason=");
-       if (pos)
-               reason = atoi(pos + 8);
-
        hostapd_drv_sta_disassoc(hapd, addr, reason);
        sta = ap_get_sta(hapd, addr);
        if (sta)
@@ -385,6 +441,7 @@ int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
                          "num_sta_ht_no_gf=%d\n"
                          "num_sta_no_ht=%d\n"
                          "num_sta_ht_20_mhz=%d\n"
+                         "num_sta_ht40_intolerant=%d\n"
                          "olbc_ht=%d\n"
                          "ht_op_mode=0x%x\n",
                          hostapd_state_text(iface->state),
@@ -397,9 +454,32 @@ int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
                          iface->num_sta_ht_no_gf,
                          iface->num_sta_no_ht,
                          iface->num_sta_ht_20mhz,
+                         iface->num_sta_ht40_intolerant,
                          iface->olbc_ht,
                          iface->ht_op_mode);
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
+               return len;
+       len += ret;
+
+       if (!iface->cac_started || !iface->dfs_cac_ms) {
+               ret = os_snprintf(buf + len, buflen - len,
+                                 "cac_time_seconds=%d\n"
+                                 "cac_time_left_seconds=N/A\n",
+                                 iface->dfs_cac_ms / 1000);
+       } else {
+               /* CAC started and CAC time set - calculate remaining time */
+               struct os_reltime now;
+               unsigned int left_time;
+
+               os_reltime_age(&iface->dfs_cac_start, &now);
+               left_time = iface->dfs_cac_ms / 1000 - now.sec;
+               ret = os_snprintf(buf + len, buflen - len,
+                                 "cac_time_seconds=%u\n"
+                                 "cac_time_left_seconds=%u\n",
+                                 iface->dfs_cac_ms / 1000,
+                                 left_time);
+       }
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -418,7 +498,7 @@ int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
                          iface->conf->vht_oper_chwidth,
                          iface->conf->vht_oper_centr_freq_seg0_idx,
                          iface->conf->vht_oper_centr_freq_seg1_idx);
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -435,7 +515,7 @@ int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
                                  wpa_ssid_txt(bss->conf->ssid.ssid,
                                               bss->conf->ssid.ssid_len),
                                  (int) i, bss->num_sta);
-               if (ret < 0 || (size_t) ret >= buflen - len)
+               if (os_snprintf_error(buflen - len, ret))
                        return len;
                len += ret;
        }
@@ -449,9 +529,6 @@ int hostapd_parse_csa_settings(const char *pos,
 {
        char *end;
 
-       if (!settings)
-               return -1;
-
        os_memset(settings, 0, sizeof(*settings));
        settings->cs_count = strtol(pos, &end, 10);
        if (pos == end) {
@@ -485,3 +562,22 @@ int hostapd_parse_csa_settings(const char *pos,
 
        return 0;
 }
+
+
+int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd)
+{
+       return hostapd_drv_stop_ap(hapd);
+}
+
+
+int hostapd_ctrl_iface_pmksa_list(struct hostapd_data *hapd, char *buf,
+                                 size_t len)
+{
+       return wpa_auth_pmksa_list(hapd->wpa_auth, buf, len);
+}
+
+
+void hostapd_ctrl_iface_pmksa_flush(struct hostapd_data *hapd)
+{
+       wpa_auth_pmksa_flush(hapd->wpa_auth);
+}