]> git.ipfire.org Git - thirdparty/hostap.git/blobdiff - wlantest/rx_data.c
wlantest: Check for zero TK even when the real PTK is not known
[thirdparty/hostap.git] / wlantest / rx_data.c
index 56c495470629f3d83c4e3e2a31e0b354a479e79e..91c0144f8a8d56c8e2e22ca1cb7b3e07099cd5ca 100644 (file)
@@ -1,41 +1,19 @@
 /*
  * Received Data frame processing
- * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2010-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 "utils/includes.h"
 
 #include "utils/common.h"
-#include "crypto/aes_wrap.h"
-#include "crypto/crypto.h"
 #include "common/defs.h"
 #include "common/ieee802_11_defs.h"
-#include "common/eapol_common.h"
-#include "common/wpa_common.h"
-#include "rsn_supp/wpa_ie.h"
 #include "wlantest.h"
 
 
-static int is_zero(const u8 *buf, size_t len)
-{
-       size_t i;
-       for (i = 0; i < len; i++) {
-               if (buf[i])
-                       return 0;
-       }
-       return 1;
-}
-
-
 static const char * data_stype(u16 stype)
 {
        switch (stype) {
@@ -74,1170 +52,589 @@ static const char * data_stype(u16 stype)
 }
 
 
-static int check_mic(const u8 *kck, int ver, const u8 *data, size_t len)
-{
-       u8 *buf;
-       int ret = -1;
-       struct ieee802_1x_hdr *hdr;
-       struct wpa_eapol_key *key;
-       u8 rx_mic[16];
-
-       buf = os_malloc(len);
-       if (buf == NULL)
-               return -1;
-       os_memcpy(buf, data, len);
-       hdr = (struct ieee802_1x_hdr *) buf;
-       key = (struct wpa_eapol_key *) (hdr + 1);
-
-       os_memcpy(rx_mic, key->key_mic, 16);
-       os_memset(key->key_mic, 0, 16);
-
-       if (wpa_eapol_key_mic(kck, ver, buf, len, key->key_mic) == 0 &&
-           os_memcmp(rx_mic, key->key_mic, 16) == 0)
-               ret = 0;
+static void rx_data_eth(struct wlantest *wt, const u8 *bssid,
+                       const u8 *sta_addr, const u8 *dst, const u8 *src,
+                       u16 ethertype, const u8 *data, size_t len, int prot,
+                       const u8 *peer_addr);
 
-       os_free(buf);
-
-       return ret;
-}
-
-
-static void rx_data_eapol_key_1_of_4(struct wlantest *wt, const u8 *dst,
-                                    const u8 *src, const u8 *data, size_t len)
+static void rx_data_vlan(struct wlantest *wt, const u8 *bssid,
+                        const u8 *sta_addr, const u8 *dst, const u8 *src,
+                        const u8 *data, size_t len, int prot,
+                        const u8 *peer_addr)
 {
-       struct wlantest_bss *bss;
-       struct wlantest_sta *sta;
-       const struct ieee802_1x_hdr *eapol;
-       const struct wpa_eapol_key *hdr;
-
-       wpa_printf(MSG_DEBUG, "EAPOL-Key 1/4 " MACSTR " -> " MACSTR,
-                  MAC2STR(src), MAC2STR(dst));
-       bss = bss_get(wt, src);
-       if (bss == NULL)
-               return;
-       sta = sta_get(bss, dst);
-       if (sta == NULL)
-               return;
-
-       eapol = (const struct ieee802_1x_hdr *) data;
-       hdr = (const struct wpa_eapol_key *) (eapol + 1);
-       if (is_zero(hdr->key_nonce, WPA_NONCE_LEN)) {
-               wpa_printf(MSG_INFO, "EAPOL-Key 1/4 from " MACSTR " used "
-                          "zero nonce", MAC2STR(src));
-       }
-       if (!is_zero(hdr->key_rsc, 8)) {
-               wpa_printf(MSG_INFO, "EAPOL-Key 1/4 from " MACSTR " used "
-                          "non-zero Key RSC", MAC2STR(src));
-       }
-       os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
-}
-
-
-static int try_pmk(struct wlantest_bss *bss, struct wlantest_sta *sta,
-                  u16 ver, const u8 *data, size_t len,
-                  struct wlantest_pmk *pmk)
-{
-       struct wpa_ptk ptk;
-       size_t ptk_len = sta->pairwise_cipher == WPA_CIPHER_TKIP ? 64 : 48;
-       wpa_pmk_to_ptk(pmk->pmk, sizeof(pmk->pmk),
-                      "Pairwise key expansion",
-                      bss->bssid, sta->addr, sta->anonce, sta->snonce,
-                      (u8 *) &ptk, ptk_len,
-                      wpa_key_mgmt_sha256(sta->key_mgmt));
-       if (check_mic(ptk.kck, ver, data, len) < 0)
-               return -1;
-
-       wpa_printf(MSG_INFO, "Derived PTK for STA " MACSTR " BSSID " MACSTR,
-                  MAC2STR(sta->addr), MAC2STR(bss->bssid));
-       sta->counters[WLANTEST_STA_COUNTER_PTK_LEARNED]++;
-       os_memcpy(&sta->ptk, &ptk, sizeof(ptk));
-       wpa_hexdump(MSG_DEBUG, "PTK:KCK", sta->ptk.kck, 16);
-       wpa_hexdump(MSG_DEBUG, "PTK:KEK", sta->ptk.kek, 16);
-       wpa_hexdump(MSG_DEBUG, "PTK:TK1", sta->ptk.tk1, 16);
-       if (ptk_len > 48)
-               wpa_hexdump(MSG_DEBUG, "PTK:TK2", sta->ptk.u.tk2, 16);
-       sta->ptk_set = 1;
-       os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
-       os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
-       return 0;
-}
-
-
-static void derive_ptk(struct wlantest *wt, struct wlantest_bss *bss,
-                      struct wlantest_sta *sta, u16 ver,
-                      const u8 *data, size_t len)
-{
-       struct wlantest_pmk *pmk;
-
-       dl_list_for_each(pmk, &bss->pmk, struct wlantest_pmk, list) {
-               if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
-                       return;
-       }
-
-       dl_list_for_each(pmk, &wt->pmk, struct wlantest_pmk, list) {
-               if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
-                       return;
-       }
-}
-
-
-static void rx_data_eapol_key_2_of_4(struct wlantest *wt, const u8 *dst,
-                                    const u8 *src, const u8 *data, size_t len)
-{
-       struct wlantest_bss *bss;
-       struct wlantest_sta *sta;
-       const struct ieee802_1x_hdr *eapol;
-       const struct wpa_eapol_key *hdr;
-       const u8 *key_data;
-       u16 key_info, key_data_len;
-       struct wpa_eapol_ie_parse ie;
-
-       wpa_printf(MSG_DEBUG, "EAPOL-Key 2/4 " MACSTR " -> " MACSTR,
-                  MAC2STR(src), MAC2STR(dst));
-       bss = bss_get(wt, dst);
-       if (bss == NULL)
-               return;
-       sta = sta_get(bss, src);
-       if (sta == NULL)
-               return;
-
-       eapol = (const struct ieee802_1x_hdr *) data;
-       hdr = (const struct wpa_eapol_key *) (eapol + 1);
-       if (is_zero(hdr->key_nonce, WPA_NONCE_LEN)) {
-               wpa_printf(MSG_INFO, "EAPOL-Key 2/4 from " MACSTR " used "
-                          "zero nonce", MAC2STR(src));
-       }
-       if (!is_zero(hdr->key_rsc, 8)) {
-               wpa_printf(MSG_INFO, "EAPOL-Key 2/4 from " MACSTR " used "
-                          "non-zero Key RSC", MAC2STR(src));
-       }
-       os_memcpy(sta->snonce, hdr->key_nonce, WPA_NONCE_LEN);
-       key_info = WPA_GET_BE16(hdr->key_info);
-       key_data_len = WPA_GET_BE16(hdr->key_data_length);
-       derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK, data, len);
-
-       if (!sta->ptk_set) {
-               wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 2/4");
-               return;
-       }
-
-       if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
-                     data, len) < 0) {
-               wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 2/4 MIC");
-               return;
-       }
-       wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/4");
-
-       key_data = (const u8 *) (hdr + 1);
+       u16 tag;
 
-       if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
-               wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
+       if (len < 4)
                return;
-       }
-
-       if (ie.wpa_ie) {
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
-                           ie.wpa_ie, ie.wpa_ie_len);
-               if (os_memcmp(ie.wpa_ie, sta->rsnie, ie.wpa_ie_len) != 0) {
-                       wpa_printf(MSG_INFO, "Mismatch in WPA IE between "
-                                  "EAPOL-Key 2/4 and (Re)Association "
-                                  "Request from " MACSTR, MAC2STR(sta->addr));
-                       wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
-                                   ie.wpa_ie, ie.wpa_ie_len);
-                       wpa_hexdump(MSG_INFO, "WPA IE in (Re)Association "
-                                   "Request",
-                                   sta->rsnie,
-                                   sta->rsnie[0] ? 2 + sta->rsnie[1] : 0);
-               }
-       }
-
-       if (ie.rsn_ie) {
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
-                           ie.rsn_ie, ie.rsn_ie_len);
-               if (os_memcmp(ie.rsn_ie, sta->rsnie, ie.rsn_ie_len) != 0) {
-                       wpa_printf(MSG_INFO, "Mismatch in RSN IE between "
-                                  "EAPOL-Key 2/4 and (Re)Association "
-                                  "Request from " MACSTR, MAC2STR(sta->addr));
-                       wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
-                                   ie.rsn_ie, ie.rsn_ie_len);
-                       wpa_hexdump(MSG_INFO, "RSN IE in (Re)Association "
-                                   "Request",
-                                   sta->rsnie,
-                                   sta->rsnie[0] ? 2 + sta->rsnie[1] : 0);
-               }
-       }
+       tag = WPA_GET_BE16(data);
+       wpa_printf(MSG_MSGDUMP, "VLAN tag: Priority=%u ID=%u",
+                  tag >> 12, tag & 0x0ffff);
+       /* ignore VLAN information and process the original frame */
+       rx_data_eth(wt, bssid, sta_addr, dst, src, WPA_GET_BE16(data + 2),
+                   data + 4, len - 4, prot, peer_addr);
 }
 
 
-static u8 * decrypt_eapol_key_data_rc4(const u8 *kek,
-                                      const struct wpa_eapol_key *hdr,
-                                      size_t *len)
+static void rx_data_eth(struct wlantest *wt, const u8 *bssid,
+                       const u8 *sta_addr, const u8 *dst, const u8 *src,
+                       u16 ethertype, const u8 *data, size_t len, int prot,
+                       const u8 *peer_addr)
 {
-       u8 ek[32], *buf;
-       u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
-
-       buf = os_malloc(keydatalen);
-       if (buf == NULL)
-               return NULL;
-
-       os_memcpy(ek, hdr->key_iv, 16);
-       os_memcpy(ek + 16, kek, 16);
-       os_memcpy(buf, hdr + 1, keydatalen);
-       if (rc4_skip(ek, 32, 256, buf, keydatalen)) {
-               wpa_printf(MSG_INFO, "RC4 failed");
-               os_free(buf);
-               return NULL;
-       }
-
-       *len = keydatalen;
-       return buf;
-}
-
-
-static u8 * decrypt_eapol_key_data_aes(const u8 *kek,
-                                      const struct wpa_eapol_key *hdr,
-                                      size_t *len)
-{
-       u8 *buf;
-       u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
-
-       if (keydatalen % 8) {
-               wpa_printf(MSG_INFO, "Unsupported AES-WRAP len %d",
-                          keydatalen);
-               return NULL;
-       }
-       keydatalen -= 8; /* AES-WRAP adds 8 bytes */
-       buf = os_malloc(keydatalen);
-       if (buf == NULL)
-               return NULL;
-       if (aes_unwrap(kek, keydatalen / 8, (u8 *) (hdr + 1), buf)) {
-               os_free(buf);
-               wpa_printf(MSG_INFO, "AES unwrap failed - "
-                          "could not decrypt EAPOL-Key key data");
-               return NULL;
-       }
-
-       *len = keydatalen;
-       return buf;
-}
-
-
-static u8 * decrypt_eapol_key_data(const u8 *kek, u16 ver,
-                                  const struct wpa_eapol_key *hdr,
-                                  size_t *len)
-{
-       switch (ver) {
-       case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
-               return decrypt_eapol_key_data_rc4(kek, hdr, len);
-       case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
-       case WPA_KEY_INFO_TYPE_AES_128_CMAC:
-               return decrypt_eapol_key_data_aes(kek, hdr, len);
-       default:
-               wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Key Descriptor "
-                          "Version %u", ver);
-               return NULL;
-       }
-}
-
-
-static void learn_kde_keys(struct wlantest_bss *bss, const u8 *buf, size_t len,
-                          const u8 *rsc)
-{
-       struct wpa_eapol_ie_parse ie;
-
-       if (wpa_supplicant_parse_ies(buf, len, &ie) < 0) {
-               wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
-               return;
-       }
-
-       if (ie.wpa_ie) {
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
-                           ie.wpa_ie, ie.wpa_ie_len);
-       }
-
-       if (ie.rsn_ie) {
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
-                           ie.rsn_ie, ie.rsn_ie_len);
-       }
-
-       if (ie.gtk) {
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - GTK KDE",
-                           ie.gtk, ie.gtk_len);
-               if (ie.gtk_len >= 2 && ie.gtk_len <= 2 + 32) {
-                       int id;
-                       id = ie.gtk[0] & 0x03;
-                       wpa_printf(MSG_DEBUG, "GTK KeyID=%u tx=%u",
-                                  id, !!(ie.gtk[0] & 0x04));
-                       if ((ie.gtk[0] & 0xf8) || ie.gtk[1])
-                               wpa_printf(MSG_INFO, "GTK KDE: Reserved field "
-                                          "set: %02x %02x",
-                                          ie.gtk[0], ie.gtk[1]);
-                       wpa_hexdump(MSG_DEBUG, "GTK", ie.gtk + 2,
-                                   ie.gtk_len - 2);
-                       bss->gtk_len[id] = ie.gtk_len - 2;
-                       os_memcpy(bss->gtk[id], ie.gtk + 2, ie.gtk_len - 2);
-                       bss->rsc[id][0] = rsc[5];
-                       bss->rsc[id][1] = rsc[4];
-                       bss->rsc[id][2] = rsc[3];
-                       bss->rsc[id][3] = rsc[2];
-                       bss->rsc[id][4] = rsc[1];
-                       bss->rsc[id][5] = rsc[0];
-                       wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
-               } else {
-                       wpa_printf(MSG_INFO, "Invalid GTK KDE length %u",
-                                  (unsigned) ie.gtk_len);
-               }
-       }
-
-       if (ie.igtk) {
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - IGTK KDE",
-                           ie.igtk, ie.igtk_len);
-               if (ie.igtk_len == 24) {
-                       u16 id;
-                       id = WPA_GET_LE16(ie.igtk);
-                       if (id > 5) {
-                               wpa_printf(MSG_INFO, "Unexpected IGTK KeyID "
-                                          "%u", id);
-                       } else {
-                               wpa_printf(MSG_DEBUG, "IGTK KeyID %u", id);
-                               wpa_hexdump(MSG_DEBUG, "IPN", ie.igtk + 2, 6);
-                               wpa_hexdump(MSG_DEBUG, "IGTK", ie.igtk + 8,
-                                           16);
-                               os_memcpy(bss->igtk[id], ie.igtk + 8, 16);
-                               bss->igtk_set[id] = 1;
-                       }
-               } else {
-                       wpa_printf(MSG_INFO, "Invalid IGTK KDE length %u",
-                                  (unsigned) ie.igtk_len);
-               }
+       switch (ethertype) {
+       case ETH_P_PAE:
+               rx_data_eapol(wt, bssid, sta_addr, dst, src, data, len, prot);
+               break;
+       case ETH_P_IP:
+               rx_data_ip(wt, bssid, sta_addr, dst, src, data, len,
+                          peer_addr);
+               break;
+       case 0x890d:
+               rx_data_80211_encap(wt, bssid, sta_addr, dst, src, data, len);
+               break;
+       case ETH_P_8021Q:
+               rx_data_vlan(wt, bssid, sta_addr, dst, src, data, len, prot,
+                            peer_addr);
+               break;
        }
 }
 
 
-static void rx_data_eapol_key_3_of_4(struct wlantest *wt, const u8 *dst,
-                                    const u8 *src, const u8 *data, size_t len)
+static void rx_data_process(struct wlantest *wt, const u8 *bssid,
+                           const u8 *sta_addr,
+                           const u8 *dst, const u8 *src,
+                           const u8 *data, size_t len, int prot,
+                           const u8 *peer_addr)
 {
-       struct wlantest_bss *bss;
-       struct wlantest_sta *sta;
-       const struct ieee802_1x_hdr *eapol;
-       const struct wpa_eapol_key *hdr;
-       const u8 *key_data;
-       int recalc = 0;
-       u16 key_info, ver;
-       u8 *decrypted_buf = NULL;
-       const u8 *decrypted;
-       size_t decrypted_len = 0;
-       struct wpa_eapol_ie_parse ie;
-
-       wpa_printf(MSG_DEBUG, "EAPOL-Key 3/4 " MACSTR " -> " MACSTR,
-                  MAC2STR(src), MAC2STR(dst));
-       bss = bss_get(wt, src);
-       if (bss == NULL)
-               return;
-       sta = sta_get(bss, dst);
-       if (sta == NULL)
-               return;
-
-       eapol = (const struct ieee802_1x_hdr *) data;
-       hdr = (const struct wpa_eapol_key *) (eapol + 1);
-       key_info = WPA_GET_BE16(hdr->key_info);
-
-       if (os_memcmp(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN) != 0) {
-               wpa_printf(MSG_INFO, "EAPOL-Key ANonce mismatch between 1/4 "
-                          "and 3/4");
-               recalc = 1;
-       }
-       os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
-       if (recalc) {
-               derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK,
-                          data, len);
-       }
-
-       if (!sta->ptk_set) {
-               wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 3/4");
+       if (len == 0)
                return;
-       }
 
-       if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
-                     data, len) < 0) {
-               wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 3/4 MIC");
-               return;
-       }
-       wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 3/4");
-
-       key_data = (const u8 *) (hdr + 1);
-       if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
-               if (sta->proto & WPA_PROTO_RSN)
-                       wpa_printf(MSG_INFO, "EAPOL-Key 3/4 without "
-                                  "EncrKeyData bit");
-               decrypted = key_data;
-               decrypted_len = WPA_GET_BE16(hdr->key_data_length);
-       } else {
-               ver = key_info & WPA_KEY_INFO_TYPE_MASK;
-               decrypted_buf = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
-                                                      &decrypted_len);
-               if (decrypted_buf == NULL) {
-                       wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key "
-                                  "Data");
-                       return;
-               }
-               decrypted = decrypted_buf;
-               wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
-                           decrypted, decrypted_len);
-       }
-       if (wt->write_pcap_dumper && decrypted != key_data) {
-               /* Fill in a dummy Data frame header */
-               u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
-               struct ieee80211_hdr *h;
-               struct wpa_eapol_key *k;
-               const u8 *p;
-               u8 *pos;
-               size_t plain_len;
-
-               plain_len = decrypted_len;
-               p = decrypted;
-               while (p + 1 < decrypted + decrypted_len) {
-                       if (p[0] == 0xdd && p[1] == 0x00) {
-                               /* Remove padding */
-                               plain_len = p - decrypted;
-                               break;
-                       }
-                       p += 2 + p[1];
-               }
-
-               os_memset(buf, 0, sizeof(buf));
-               h = (struct ieee80211_hdr *) buf;
-               h->frame_control = host_to_le16(0x0208);
-               os_memcpy(h->addr1, dst, ETH_ALEN);
-               os_memcpy(h->addr2, src, ETH_ALEN);
-               os_memcpy(h->addr3, src, ETH_ALEN);
-               pos = (u8 *) (h + 1);
-               os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
-               pos += 8;
-               os_memcpy(pos, eapol, sizeof(*eapol));
-               pos += sizeof(*eapol);
-               os_memcpy(pos, hdr, sizeof(*hdr));
-               k = (struct wpa_eapol_key *) pos;
-               WPA_PUT_BE16(k->key_info,
-                            key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
-               WPA_PUT_BE16(k->key_data_length, plain_len);
-               write_pcap_decrypted(wt, buf, sizeof(buf),
-                                    decrypted, plain_len);
-       }
-
-       if (wpa_supplicant_parse_ies(decrypted, decrypted_len, &ie) < 0) {
-               wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
-               os_free(decrypted_buf);
+       if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
+               rx_data_eth(wt, bssid, sta_addr, dst, src,
+                           WPA_GET_BE16(data + 6), data + 8, len - 8, prot,
+                           peer_addr);
                return;
        }
 
-       if ((ie.wpa_ie &&
-            os_memcmp(ie.wpa_ie, bss->wpaie, ie.wpa_ie_len) != 0) ||
-           (ie.wpa_ie == NULL && bss->wpaie[0])) {
-               wpa_printf(MSG_INFO, "Mismatch in WPA IE between "
-                          "EAPOL-Key 3/4 and Beacon/Probe Response "
-                          "from " MACSTR, MAC2STR(bss->bssid));
-               wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
-                           ie.wpa_ie, ie.wpa_ie_len);
-               wpa_hexdump(MSG_INFO, "WPA IE in Beacon/Probe "
-                           "Response",
-                           bss->wpaie,
-                           bss->wpaie[0] ? 2 + bss->wpaie[1] : 0);
-       }
-
-       if ((ie.rsn_ie &&
-            os_memcmp(ie.rsn_ie, bss->rsnie, ie.rsn_ie_len) != 0) ||
-           (ie.rsn_ie == NULL && bss->rsnie[0])) {
-               wpa_printf(MSG_INFO, "Mismatch in RSN IE between "
-                          "EAPOL-Key 3/4 and Beacon/Probe Response "
-                          "from " MACSTR, MAC2STR(bss->bssid));
-               wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
-                           ie.rsn_ie, ie.rsn_ie_len);
-               wpa_hexdump(MSG_INFO, "RSN IE in (Re)Association "
-                           "Request",
-                           bss->rsnie,
-                           bss->rsnie[0] ? 2 + bss->rsnie[1] : 0);
-       }
-
-       learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
-       os_free(decrypted_buf);
+       wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
 }
 
 
-static void rx_data_eapol_key_4_of_4(struct wlantest *wt, const u8 *dst,
-                                    const u8 *src, const u8 *data, size_t len)
+static void write_decrypted_note(struct wlantest *wt, const u8 *decrypted,
+                                const u8 *tk, size_t tk_len, int keyid)
 {
-       struct wlantest_bss *bss;
-       struct wlantest_sta *sta;
-       const struct ieee802_1x_hdr *eapol;
-       const struct wpa_eapol_key *hdr;
-       u16 key_info;
-
-       wpa_printf(MSG_DEBUG, "EAPOL-Key 4/4 " MACSTR " -> " MACSTR,
-                  MAC2STR(src), MAC2STR(dst));
-       bss = bss_get(wt, dst);
-       if (bss == NULL)
-               return;
-       sta = sta_get(bss, src);
-       if (sta == NULL)
-               return;
+       char tk_hex[65];
 
-       eapol = (const struct ieee802_1x_hdr *) data;
-       hdr = (const struct wpa_eapol_key *) (eapol + 1);
-       if (!is_zero(hdr->key_rsc, 8)) {
-               wpa_printf(MSG_INFO, "EAPOL-Key 4/4 from " MACSTR " used "
-                          "non-zero Key RSC", MAC2STR(src));
-       }
-       key_info = WPA_GET_BE16(hdr->key_info);
-
-       if (!sta->ptk_set) {
-               wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 4/4");
+       if (!decrypted)
                return;
-       }
 
-       if (sta->ptk_set &&
-           check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
-                     data, len) < 0) {
-               wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 4/4 MIC");
-               return;
-       }
-       wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 4/4");
+       wpa_snprintf_hex(tk_hex, sizeof(tk_hex), tk, tk_len);
+       add_note(wt, MSG_EXCESSIVE, "TK[%d] %s", keyid, tk_hex);
 }
 
 
-static void rx_data_eapol_key_1_of_2(struct wlantest *wt, const u8 *dst,
-                                    const u8 *src, const u8 *data, size_t len)
+static u8 * try_ptk(int pairwise_cipher, struct wpa_ptk *ptk,
+                   const struct ieee80211_hdr *hdr,
+                   const u8 *data, size_t data_len, size_t *decrypted_len)
 {
-       struct wlantest_bss *bss;
-       struct wlantest_sta *sta;
-       const struct ieee802_1x_hdr *eapol;
-       const struct wpa_eapol_key *hdr;
-       const u8 *key_data;
-       u16 key_info, ver;
        u8 *decrypted;
-       size_t decrypted_len = 0;
-
-       wpa_printf(MSG_DEBUG, "EAPOL-Key 1/2 " MACSTR " -> " MACSTR,
-                  MAC2STR(src), MAC2STR(dst));
-       bss = bss_get(wt, src);
-       if (bss == NULL)
-               return;
-       sta = sta_get(bss, dst);
-       if (sta == NULL)
-               return;
-
-       eapol = (const struct ieee802_1x_hdr *) data;
-       hdr = (const struct wpa_eapol_key *) (eapol + 1);
-       key_info = WPA_GET_BE16(hdr->key_info);
-
-       if (!sta->ptk_set) {
-               wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 1/2");
-               return;
-       }
-
-       if (sta->ptk_set &&
-           check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
-                     data, len) < 0) {
-               wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 1/2 MIC");
-               return;
-       }
-       wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 1/2");
-
-       key_data = (const u8 *) (hdr + 1);
-       if (sta->proto & WPA_PROTO_RSN &&
-           !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
-               wpa_printf(MSG_INFO, "EAPOL-Key 1/2 without EncrKeyData bit");
-               return;
-       }
-       ver = key_info & WPA_KEY_INFO_TYPE_MASK;
-       decrypted = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
-                                          &decrypted_len);
-       if (decrypted == NULL) {
-               wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
-               return;
-       }
-       wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
-                   decrypted, decrypted_len);
-       if (wt->write_pcap_dumper) {
-               /* Fill in a dummy Data frame header */
-               u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
-               struct ieee80211_hdr *h;
-               struct wpa_eapol_key *k;
-               u8 *pos;
-               size_t plain_len;
-
-               plain_len = decrypted_len;
-               pos = decrypted;
-               while (pos + 1 < decrypted + decrypted_len) {
-                       if (pos[0] == 0xdd && pos[1] == 0x00) {
-                               /* Remove padding */
-                               plain_len = pos - decrypted;
-                               break;
-                       }
-                       pos += 2 + pos[1];
-               }
-
-               os_memset(buf, 0, sizeof(buf));
-               h = (struct ieee80211_hdr *) buf;
-               h->frame_control = host_to_le16(0x0208);
-               os_memcpy(h->addr1, dst, ETH_ALEN);
-               os_memcpy(h->addr2, src, ETH_ALEN);
-               os_memcpy(h->addr3, src, ETH_ALEN);
-               pos = (u8 *) (h + 1);
-               os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
-               pos += 8;
-               os_memcpy(pos, eapol, sizeof(*eapol));
-               pos += sizeof(*eapol);
-               os_memcpy(pos, hdr, sizeof(*hdr));
-               k = (struct wpa_eapol_key *) pos;
-               WPA_PUT_BE16(k->key_info,
-                            key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
-               WPA_PUT_BE16(k->key_data_length, plain_len);
-               write_pcap_decrypted(wt, buf, sizeof(buf),
-                                    decrypted, plain_len);
-       }
-       if (sta->proto & WPA_PROTO_RSN)
-               learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
-       else {
-               int len = bss->group_cipher == WPA_CIPHER_TKIP ? 32 : 16;
-               if (decrypted_len == len) {
-                       const u8 *rsc = hdr->key_rsc;
-                       int id;
-                       id = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
-                               WPA_KEY_INFO_KEY_INDEX_SHIFT;
-                       wpa_printf(MSG_DEBUG, "GTK key index %d", id);
-                       wpa_hexdump(MSG_DEBUG, "GTK", decrypted,
-                                   decrypted_len);
-                       bss->gtk_len[id] = decrypted_len;
-                       os_memcpy(bss->gtk[id], decrypted, decrypted_len);
-                       bss->rsc[id][0] = rsc[5];
-                       bss->rsc[id][1] = rsc[4];
-                       bss->rsc[id][2] = rsc[3];
-                       bss->rsc[id][3] = rsc[2];
-                       bss->rsc[id][4] = rsc[1];
-                       bss->rsc[id][5] = rsc[0];
-                       wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
-               } else {
-                       wpa_printf(MSG_INFO, "Unexpected WPA Key Data length "
-                                  "in Group Key msg 1/2 from " MACSTR,
-                                  MAC2STR(src));
-               }
-       }
-       os_free(decrypted);
+       unsigned int tk_len = ptk->tk_len;
+
+       decrypted = NULL;
+       if ((pairwise_cipher == WPA_CIPHER_CCMP ||
+            pairwise_cipher == 0) && tk_len == 16) {
+               decrypted = ccmp_decrypt(ptk->tk, hdr, data,
+                                        data_len, decrypted_len);
+       } else if ((pairwise_cipher == WPA_CIPHER_CCMP_256 ||
+                   pairwise_cipher == 0) && tk_len == 32) {
+               decrypted = ccmp_256_decrypt(ptk->tk, hdr, data,
+                                            data_len, decrypted_len);
+       } else if ((pairwise_cipher == WPA_CIPHER_GCMP ||
+                   pairwise_cipher == WPA_CIPHER_GCMP_256 ||
+                   pairwise_cipher == 0) &&
+                  (tk_len == 16 || tk_len == 32)) {
+               decrypted = gcmp_decrypt(ptk->tk, tk_len, hdr,
+                                        data, data_len, decrypted_len);
+       } else if ((pairwise_cipher == WPA_CIPHER_TKIP ||
+                   pairwise_cipher == 0) && tk_len == 32) {
+               decrypted = tkip_decrypt(ptk->tk, hdr, data, data_len,
+                                        decrypted_len);
+       }
+
+       return decrypted;
 }
 
 
-static void rx_data_eapol_key_2_of_2(struct wlantest *wt, const u8 *dst,
-                                    const u8 *src, const u8 *data, size_t len)
+static u8 * try_all_ptk(struct wlantest *wt, int pairwise_cipher,
+                       const struct ieee80211_hdr *hdr, int keyid,
+                       const u8 *data, size_t data_len, size_t *decrypted_len)
 {
-       struct wlantest_bss *bss;
-       struct wlantest_sta *sta;
-       const struct ieee802_1x_hdr *eapol;
-       const struct wpa_eapol_key *hdr;
-       u16 key_info;
-
-       wpa_printf(MSG_DEBUG, "EAPOL-Key 2/2 " MACSTR " -> " MACSTR,
-                  MAC2STR(src), MAC2STR(dst));
-       bss = bss_get(wt, dst);
-       if (bss == NULL)
-               return;
-       sta = sta_get(bss, src);
-       if (sta == NULL)
-               return;
-
-       eapol = (const struct ieee802_1x_hdr *) data;
-       hdr = (const struct wpa_eapol_key *) (eapol + 1);
-       if (!is_zero(hdr->key_rsc, 8)) {
-               wpa_printf(MSG_INFO, "EAPOL-Key 2/2 from " MACSTR " used "
-                          "non-zero Key RSC", MAC2STR(src));
-       }
-       key_info = WPA_GET_BE16(hdr->key_info);
-
-       if (!sta->ptk_set) {
-               wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 2/2");
-               return;
-       }
-
-       if (sta->ptk_set &&
-           check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
-                     data, len) < 0) {
-               wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 2/2 MIC");
-               return;
-       }
-       wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/2");
-}
-
-
-static void rx_data_eapol_key(struct wlantest *wt, const u8 *dst,
-                             const u8 *src, const u8 *data, size_t len,
-                             int prot)
-{
-       const struct ieee802_1x_hdr *eapol;
-       const struct wpa_eapol_key *hdr;
-       const u8 *key_data;
-       u16 key_info, key_length, ver, key_data_length;
-
-       eapol = (const struct ieee802_1x_hdr *) data;
-       hdr = (const struct wpa_eapol_key *) (eapol + 1);
-
-       wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key",
-                   (const u8 *) hdr, len - sizeof(*eapol));
-       if (len < sizeof(*hdr)) {
-               wpa_printf(MSG_INFO, "Too short EAPOL-Key frame from " MACSTR,
-                          MAC2STR(src));
-               return;
-       }
-
-       if (hdr->type == EAPOL_KEY_TYPE_RC4) {
-               /* TODO: EAPOL-Key RC4 for WEP */
-               wpa_printf(MSG_INFO, "EAPOL-Key Descriptor Type RC4 from "
-                          MACSTR, MAC2STR(src));
-               return;
-       }
-
-       if (hdr->type != EAPOL_KEY_TYPE_RSN &&
-           hdr->type != EAPOL_KEY_TYPE_WPA) {
-               wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Descriptor Type "
-                          "%u from " MACSTR, hdr->type, MAC2STR(src));
-               return;
-       }
-
-       key_info = WPA_GET_BE16(hdr->key_info);
-       key_length = WPA_GET_BE16(hdr->key_length);
-       key_data_length = WPA_GET_BE16(hdr->key_data_length);
-       key_data = (const u8 *) (hdr + 1);
-       if (key_data + key_data_length > data + len) {
-               wpa_printf(MSG_INFO, "Truncated EAPOL-Key from " MACSTR,
-                          MAC2STR(src));
-               return;
-       }
-       if (key_data + key_data_length < data + len) {
-               wpa_hexdump(MSG_DEBUG, "Extra data after EAPOL-Key Key Data "
-                           "field", key_data + key_data_length,
-                       data + len - key_data - key_data_length);
-       }
-
-
-       ver = key_info & WPA_KEY_INFO_TYPE_MASK;
-       wpa_printf(MSG_DEBUG, "EAPOL-Key ver=%u %c idx=%u%s%s%s%s%s%s%s%s "
-                  "datalen=%u",
-                  ver, key_info & WPA_KEY_INFO_KEY_TYPE ? 'P' : 'G',
-                  (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
-                  WPA_KEY_INFO_KEY_INDEX_SHIFT,
-                  (key_info & WPA_KEY_INFO_INSTALL) ? " Install" : "",
-                  (key_info & WPA_KEY_INFO_ACK) ? " ACK" : "",
-                  (key_info & WPA_KEY_INFO_MIC) ? " MIC" : "",
-                  (key_info & WPA_KEY_INFO_SECURE) ? " Secure" : "",
-                  (key_info & WPA_KEY_INFO_ERROR) ? " Error" : "",
-                  (key_info & WPA_KEY_INFO_REQUEST) ? " Request" : "",
-                  (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) ? " Encr" : "",
-                  (key_info & WPA_KEY_INFO_SMK_MESSAGE) ? " SMK" : "",
-                  key_data_length);
-
-       if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
-           ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
-           ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
-               wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Key Descriptor "
-                          "Version %u from " MACSTR, ver, MAC2STR(src));
-               return;
-       }
-
-       wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Replay Counter",
-                   hdr->replay_counter, WPA_REPLAY_COUNTER_LEN);
-       wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Nonce",
-                   hdr->key_nonce, WPA_NONCE_LEN);
-       wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key IV",
-                   hdr->key_iv, 16);
-       wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key RSC",
-                   hdr->key_rsc, WPA_KEY_RSC_LEN);
-       wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key MIC",
-                   hdr->key_mic, 16);
-       wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data",
-                   key_data, key_data_length);
-
-       if (hdr->type == EAPOL_KEY_TYPE_RSN &&
-           (key_info & (WPA_KEY_INFO_KEY_INDEX_MASK | BIT(14) | BIT(15))) !=
-           0) {
-               wpa_printf(MSG_INFO, "RSN EAPOL-Key with non-zero reserved "
-                          "Key Info bits 0x%x from " MACSTR,
-                          key_info, MAC2STR(src));
-       }
-
-       if (hdr->type == EAPOL_KEY_TYPE_WPA &&
-           (key_info & (WPA_KEY_INFO_ENCR_KEY_DATA |
-                        WPA_KEY_INFO_SMK_MESSAGE |BIT(14) | BIT(15))) != 0) {
-               wpa_printf(MSG_INFO, "WPA EAPOL-Key with non-zero reserved "
-                          "Key Info bits 0x%x from " MACSTR,
-                          key_info, MAC2STR(src));
-       }
-
-       if (key_length > 32) {
-               wpa_printf(MSG_INFO, "EAPOL-Key with invalid Key Length %d "
-                          "from " MACSTR, key_length, MAC2STR(src));
-       }
-
-       if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
-           !is_zero(hdr->key_iv, 16)) {
-               wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key IV "
-                          "(reserved with ver=%d) field from " MACSTR,
-                          ver, MAC2STR(src));
-               wpa_hexdump(MSG_INFO, "EAPOL-Key Key IV (reserved)",
-                           hdr->key_iv, 16);
-       }
-
-       if (!is_zero(hdr->key_id, 8)) {
-               wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key ID "
-                          "(reserved) field from " MACSTR, MAC2STR(src));
-               wpa_hexdump(MSG_INFO, "EAPOL-Key Key ID (reserved)",
-                           hdr->key_id, 8);
-       }
-
-       if (hdr->key_rsc[6] || hdr->key_rsc[7]) {
-               wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key RSC octets "
-                          "(last two are unused)" MACSTR, MAC2STR(src));
-       }
-
-       if (key_info & (WPA_KEY_INFO_ERROR | WPA_KEY_INFO_REQUEST))
-               return;
-
-       if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
-               return;
-
-       if (key_info & WPA_KEY_INFO_KEY_TYPE) {
-               /* 4-Way Handshake */
-               switch (key_info & (WPA_KEY_INFO_SECURE |
-                                   WPA_KEY_INFO_MIC |
-                                   WPA_KEY_INFO_ACK |
-                                   WPA_KEY_INFO_INSTALL)) {
-               case WPA_KEY_INFO_ACK:
-                       rx_data_eapol_key_1_of_4(wt, dst, src, data, len);
-                       break;
-               case WPA_KEY_INFO_MIC:
-                       if (key_data_length == 0)
-                               rx_data_eapol_key_4_of_4(wt, dst, src, data,
-                                                        len);
-                       else
-                               rx_data_eapol_key_2_of_4(wt, dst, src, data,
-                                                        len);
-                       break;
-               case WPA_KEY_INFO_MIC | WPA_KEY_INFO_ACK |
-                       WPA_KEY_INFO_INSTALL:
-                       /* WPA does not include Secure bit in 3/4 */
-                       rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
-                       break;
-               case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
-                       WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL:
-                       rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
-                       break;
-               case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
-                       rx_data_eapol_key_4_of_4(wt, dst, src, data, len);
-                       break;
-               default:
-                       wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
-                       break;
-               }
-       } else {
-               /* Group Key Handshake */
-               switch (key_info & (WPA_KEY_INFO_SECURE |
-                                   WPA_KEY_INFO_MIC |
-                                   WPA_KEY_INFO_ACK)) {
-               case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
-                       WPA_KEY_INFO_ACK:
-                       rx_data_eapol_key_1_of_2(wt, dst, src, data, len);
-                       break;
-               case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
-                       rx_data_eapol_key_2_of_2(wt, dst, src, data, len);
-                       break;
-               default:
-                       wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
-                       break;
+       struct wlantest_ptk *ptk;
+       u8 *decrypted;
+       int prev_level = wpa_debug_level;
+
+       wpa_debug_level = MSG_WARNING;
+       dl_list_for_each(ptk, &wt->ptk, struct wlantest_ptk, list) {
+               decrypted = try_ptk(pairwise_cipher, &ptk->ptk, hdr,
+                                   data, data_len, decrypted_len);
+               if (decrypted) {
+                       wpa_debug_level = prev_level;
+                       add_note(wt, MSG_DEBUG,
+                                "Found PTK match from list of all known PTKs");
+                       write_decrypted_note(wt, decrypted, ptk->ptk.tk,
+                                            ptk->ptk.tk_len, keyid);
+                       return decrypted;
                }
        }
-}
-
-
-static void rx_data_eapol(struct wlantest *wt, const u8 *dst, const u8 *src,
-                         const u8 *data, size_t len, int prot)
-{
-       const struct ieee802_1x_hdr *hdr;
-       u16 length;
-       const u8 *p;
-
-       wpa_hexdump(MSG_EXCESSIVE, "EAPOL", data, len);
-       if (len < sizeof(*hdr)) {
-               wpa_printf(MSG_INFO, "Too short EAPOL frame from " MACSTR,
-                          MAC2STR(src));
-               return;
-       }
-
-       hdr = (const struct ieee802_1x_hdr *) data;
-       length = be_to_host16(hdr->length);
-       wpa_printf(MSG_DEBUG, "RX EAPOL: " MACSTR " -> " MACSTR "%s ver=%u "
-                  "type=%u len=%u",
-                  MAC2STR(src), MAC2STR(dst), prot ? " Prot" : "",
-                  hdr->version, hdr->type, length);
-       if (hdr->version < 1 || hdr->version > 3) {
-               wpa_printf(MSG_INFO, "Unexpected EAPOL version %u from "
-                          MACSTR, hdr->version, MAC2STR(src));
-       }
-       if (sizeof(*hdr) + length > len) {
-               wpa_printf(MSG_INFO, "Truncated EAPOL frame from " MACSTR,
-                          MAC2STR(src));
-               return;
-       }
+       wpa_debug_level = prev_level;
 
-       if (sizeof(*hdr) + length < len) {
-               wpa_printf(MSG_INFO, "EAPOL frame with %d extra bytes",
-                          (int) (len - sizeof(*hdr) - length));
-       }
-       p = (const u8 *) (hdr + 1);
-
-       switch (hdr->type) {
-       case IEEE802_1X_TYPE_EAP_PACKET:
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL - EAP packet", p, length);
-               break;
-       case IEEE802_1X_TYPE_EAPOL_START:
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL-Start", p, length);
-               break;
-       case IEEE802_1X_TYPE_EAPOL_LOGOFF:
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL-Logoff", p, length);
-               break;
-       case IEEE802_1X_TYPE_EAPOL_KEY:
-               rx_data_eapol_key(wt, dst, src, data, sizeof(*hdr) + length,
-                                 prot);
-               break;
-       case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
-               wpa_hexdump(MSG_MSGDUMP, "EAPOL - Encapsulated ASF alert",
-                           p, length);
-               break;
-       default:
-               wpa_hexdump(MSG_MSGDUMP, "Unknown EAPOL payload", p, length);
-               break;
-       }
-}
-
-
-static void rx_data_eth(struct wlantest *wt, const u8 *dst, const u8 *src,
-                       u16 ethertype, const u8 *data, size_t len, int prot)
-{
-       if (ethertype == ETH_P_PAE)
-               rx_data_eapol(wt, dst, src, data, len, prot);
+       return NULL;
 }
 
 
-static void rx_data_process(struct wlantest *wt, const u8 *dst, const u8 *src,
-                           const u8 *data, size_t len, int prot)
+static void check_plaintext_prot(struct wlantest *wt,
+                                const struct ieee80211_hdr *hdr,
+                                const u8 *data, size_t len)
 {
-       if (len == 0)
-               return;
-
-       if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
-               rx_data_eth(wt, dst, src, WPA_GET_BE16(data + 6),
-                           data + 8, len - 8, prot);
+       if (len < 8 + 3 || data[8] != 0xaa || data[9] != 0xaa ||
+           data[10] != 0x03)
                return;
-       }
 
-       wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
+       add_note(wt, MSG_DEBUG,
+                "Plaintext payload in protected frame");
+       wpa_printf(MSG_INFO, "Plaintext payload in protected frame #%u: A2="
+                  MACSTR " seq=%u",
+                  wt->frame_num, MAC2STR(hdr->addr2),
+                  WLAN_GET_SEQ_SEQ(le_to_host16(hdr->seq_ctrl)));
 }
 
 
 static void rx_data_bss_prot_group(struct wlantest *wt,
                                   const struct ieee80211_hdr *hdr,
+                                  size_t hdrlen,
                                   const u8 *qos, const u8 *dst, const u8 *src,
                                   const u8 *data, size_t len)
 {
        struct wlantest_bss *bss;
        int keyid;
-       u8 *decrypted;
+       u8 *decrypted = NULL;
        size_t dlen;
        u8 pn[6];
+       int replay = 0;
 
        bss = bss_get(wt, hdr->addr2);
        if (bss == NULL)
                return;
        if (len < 4) {
-               wpa_printf(MSG_INFO, "Too short group addressed data frame");
+               add_note(wt, MSG_INFO, "Too short group addressed data frame");
                return;
        }
 
        if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
            !(data[3] & 0x20)) {
-                   wpa_printf(MSG_INFO, "Expected TKIP/CCMP frame from "
-                              MACSTR " did not have ExtIV bit set to 1",
-                              MAC2STR(bss->bssid));
-                   return;
+               add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
+                        MACSTR " did not have ExtIV bit set to 1",
+                        MAC2STR(bss->bssid));
+               return;
        }
 
        if (bss->group_cipher == WPA_CIPHER_TKIP) {
                if (data[3] & 0x1f) {
-                       wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
-                                  "non-zero reserved bit",
-                                  MAC2STR(bss->bssid));
+                       add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
+                                " used non-zero reserved bit",
+                                MAC2STR(bss->bssid));
                }
                if (data[1] != ((data[0] | 0x20) & 0x7f)) {
-                       wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
-                                  "incorrect WEPSeed[1] (was 0x%x, expected "
-                                  "0x%x)",
-                                  MAC2STR(bss->bssid), data[1],
-                                  (data[0] | 0x20) & 0x7f);
+                       add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
+                                " used incorrect WEPSeed[1] (was 0x%x, "
+                                "expected 0x%x)",
+                                MAC2STR(bss->bssid), data[1],
+                                (data[0] | 0x20) & 0x7f);
                }
        } else if (bss->group_cipher == WPA_CIPHER_CCMP) {
                if (data[2] != 0 || (data[3] & 0x1f) != 0) {
-                       wpa_printf(MSG_INFO, "CCMP frame from " MACSTR " used "
-                                  "non-zero reserved bit",
-                                  MAC2STR(bss->bssid));
+                       add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
+                                " used non-zero reserved bit",
+                                MAC2STR(bss->bssid));
                }
        }
 
+       check_plaintext_prot(wt, hdr, data, len);
        keyid = data[3] >> 6;
-       if (bss->gtk_len[keyid] == 0) {
-               wpa_printf(MSG_MSGDUMP, "No GTK known to decrypt the frame "
-                          "(A2=" MACSTR " KeyID=%d)",
-                          MAC2STR(hdr->addr2), keyid);
+       if (bss->gtk_len[keyid] == 0 && bss->group_cipher != WPA_CIPHER_WEP40)
+       {
+               add_note(wt, MSG_MSGDUMP, "No GTK known to decrypt the frame "
+                        "(A2=" MACSTR " KeyID=%d)",
+                        MAC2STR(hdr->addr2), keyid);
                return;
        }
 
        if (bss->group_cipher == WPA_CIPHER_TKIP)
                tkip_get_pn(pn, data);
+       else if (bss->group_cipher == WPA_CIPHER_WEP40)
+               goto skip_replay_det;
        else
                ccmp_get_pn(pn, data);
        if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
-               wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
-                          MAC2STR(hdr->addr2));
+               u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
+               char pn_hex[6 * 2 + 1], rsc_hex[6 * 2 + 1];
+
+               wpa_snprintf_hex(pn_hex, sizeof(pn_hex), pn, 6);
+               wpa_snprintf_hex(rsc_hex, sizeof(rsc_hex), bss->rsc[keyid], 6);
+               add_note(wt, MSG_INFO, "replay detected: A1=" MACSTR
+                        " A2=" MACSTR " A3=" MACSTR
+                        " seq=%u frag=%u%s keyid=%d %s<=%s",
+                        MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
+                        MAC2STR(hdr->addr3),
+                        WLAN_GET_SEQ_SEQ(seq_ctrl),
+                        WLAN_GET_SEQ_FRAG(seq_ctrl),
+                        (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
+                        " Retry" : "",
+                        keyid, pn_hex, rsc_hex);
                wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
                wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
+               replay = 1;
        }
 
+skip_replay_det:
        if (bss->group_cipher == WPA_CIPHER_TKIP)
                decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
                                         &dlen);
-       else
+       else if (bss->group_cipher == WPA_CIPHER_WEP40)
+               decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
+       else if (bss->group_cipher == WPA_CIPHER_CCMP)
                decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
                                         &dlen);
+       else if (bss->group_cipher == WPA_CIPHER_CCMP_256)
+               decrypted = ccmp_256_decrypt(bss->gtk[keyid], hdr, data, len,
+                                            &dlen);
+       else if (bss->group_cipher == WPA_CIPHER_GCMP ||
+                bss->group_cipher == WPA_CIPHER_GCMP_256)
+               decrypted = gcmp_decrypt(bss->gtk[keyid], bss->gtk_len[keyid],
+                                        hdr, data, len, &dlen);
+
        if (decrypted) {
-               rx_data_process(wt, dst, src, decrypted, dlen, 1);
-               os_memcpy(bss->rsc[keyid], pn, 6);
-               write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
+               char gtk[65];
+
+               wpa_snprintf_hex(gtk, sizeof(gtk), bss->gtk[keyid],
+                                bss->gtk_len[keyid]);
+               add_note(wt, MSG_EXCESSIVE, "GTK[%d] %s", keyid, gtk);
+               rx_data_process(wt, bss->bssid, NULL, dst, src, decrypted,
+                               dlen, 1, NULL);
+               if (!replay)
+                       os_memcpy(bss->rsc[keyid], pn, 6);
+               write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
                                     decrypted, dlen);
-       }
+       } else
+               add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
        os_free(decrypted);
 }
 
 
 static void rx_data_bss_prot(struct wlantest *wt,
-                            const struct ieee80211_hdr *hdr, const u8 *qos,
-                            const u8 *dst, const u8 *src, const u8 *data,
-                            size_t len)
+                            const struct ieee80211_hdr *hdr, size_t hdrlen,
+                            const u8 *qos, const u8 *dst, const u8 *src,
+                            const u8 *data, size_t len)
 {
-       struct wlantest_bss *bss;
-       struct wlantest_sta *sta;
+       struct wlantest_bss *bss, *bss2;
+       struct wlantest_sta *sta, *sta2;
        int keyid;
        u16 fc = le_to_host16(hdr->frame_control);
-       u8 *decrypted;
+       u8 *decrypted = NULL;
        size_t dlen;
        int tid;
-       u8 pn[6], *rsc;
+       u8 pn[6], *rsc = NULL;
+       struct wlantest_tdls *tdls = NULL, *found;
+       const u8 *tk = NULL;
+       int ptk_iter_done = 0;
+       int try_ptk_iter = 0;
+       int replay = 0;
 
        if (hdr->addr1[0] & 0x01) {
-               rx_data_bss_prot_group(wt, hdr, qos, dst, src, data, len);
+               rx_data_bss_prot_group(wt, hdr, hdrlen, qos, dst, src,
+                                      data, len);
                return;
        }
 
-       if (fc & WLAN_FC_TODS) {
+       if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
+           (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
+               bss = bss_find(wt, hdr->addr1);
+               if (bss) {
+                       sta = sta_find(bss, hdr->addr2);
+                       if (sta) {
+                               sta->counters[
+                                       WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
+                       }
+                       if (!sta || !sta->ptk_set) {
+                               bss2 = bss_find(wt, hdr->addr2);
+                               if (bss2) {
+                                       sta2 = sta_find(bss2, hdr->addr1);
+                                       if (sta2 && (!sta || sta2->ptk_set)) {
+                                               bss = bss2;
+                                               sta = sta2;
+                                       }
+                               }
+                       }
+               } else {
+                       bss = bss_find(wt, hdr->addr2);
+                       if (!bss)
+                               return;
+                       sta = sta_find(bss, hdr->addr1);
+               }
+       } else if (fc & WLAN_FC_TODS) {
                bss = bss_get(wt, hdr->addr1);
                if (bss == NULL)
                        return;
                sta = sta_get(bss, hdr->addr2);
-       } else {
+               if (sta)
+                       sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
+       } else if (fc & WLAN_FC_FROMDS) {
                bss = bss_get(wt, hdr->addr2);
                if (bss == NULL)
                        return;
                sta = sta_get(bss, hdr->addr1);
+       } else {
+               bss = bss_get(wt, hdr->addr3);
+               if (bss == NULL)
+                       return;
+               sta = sta_find(bss, hdr->addr2);
+               sta2 = sta_find(bss, hdr->addr1);
+               if (sta == NULL || sta2 == NULL)
+                       return;
+               found = NULL;
+               dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
+               {
+                       if ((tdls->init == sta && tdls->resp == sta2) ||
+                           (tdls->init == sta2 && tdls->resp == sta)) {
+                               found = tdls;
+                               if (tdls->link_up)
+                                       break;
+                       }
+               }
+               if (found) {
+                       if (!found->link_up)
+                               add_note(wt, MSG_DEBUG,
+                                        "TDLS: Link not up, but Data "
+                                        "frame seen");
+                       tk = found->tpk.tk;
+                       tdls = found;
+               }
        }
-       if (sta == NULL || !sta->ptk_set) {
-               wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
-               return;
+       check_plaintext_prot(wt, hdr, data, len);
+       if ((sta == NULL ||
+            (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) &&
+           tk == NULL) {
+               add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
+               if (dl_list_empty(&wt->ptk)) {
+                       if (len >= 4 && sta) {
+                               keyid = data[3] >> 6;
+                               goto check_zero_tk;
+                       }
+                       return;
+               }
+
+               try_ptk_iter = 1;
        }
 
        if (len < 4) {
-               wpa_printf(MSG_INFO, "Too short encrypted data frame");
+               add_note(wt, MSG_INFO, "Too short encrypted data frame");
                return;
        }
 
+       if (sta == NULL)
+               return;
        if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
            !(data[3] & 0x20)) {
-                   wpa_printf(MSG_INFO, "Expected TKIP/CCMP frame from "
-                              MACSTR " did not have ExtIV bit set to 1",
-                              MAC2STR(src));
-                   return;
+               add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
+                        MACSTR " did not have ExtIV bit set to 1",
+                        MAC2STR(src));
+               return;
        }
 
-       if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
+       if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) {
                if (data[3] & 0x1f) {
-                       wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
-                                  "non-zero reserved bit",
-                                  MAC2STR(hdr->addr2));
+                       add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
+                                " used non-zero reserved bit",
+                                MAC2STR(hdr->addr2));
                }
                if (data[1] != ((data[0] | 0x20) & 0x7f)) {
-                       wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
-                                  "incorrect WEPSeed[1] (was 0x%x, expected "
-                                  "0x%x)",
-                                  MAC2STR(hdr->addr2), data[1],
-                                  (data[0] | 0x20) & 0x7f);
+                       add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
+                                " used incorrect WEPSeed[1] (was 0x%x, "
+                                "expected 0x%x)",
+                                MAC2STR(hdr->addr2), data[1],
+                                (data[0] | 0x20) & 0x7f);
                }
-       } else if (sta->pairwise_cipher == WPA_CIPHER_CCMP) {
+       } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) {
                if (data[2] != 0 || (data[3] & 0x1f) != 0) {
-                       wpa_printf(MSG_INFO, "CCMP frame from " MACSTR " used "
-                                  "non-zero reserved bit",
-                                  MAC2STR(hdr->addr2));
+                       add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
+                                " used non-zero reserved bit",
+                                MAC2STR(hdr->addr2));
                }
        }
 
        keyid = data[3] >> 6;
-       if (keyid != 0) {
-               wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
-                          "individually addressed Data frame from " MACSTR,
-                          keyid, MAC2STR(hdr->addr2));
+       if (keyid != 0 &&
+           (!(sta->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST) ||
+            !(bss->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST) ||
+            keyid != 1)) {
+               add_note(wt, MSG_INFO,
+                        "Unexpected KeyID %d in individually addressed Data frame from "
+                        MACSTR,
+                        keyid, MAC2STR(hdr->addr2));
        }
 
-       if (qos)
+       if (qos) {
                tid = qos[0] & 0x0f;
-       else
+               if (fc & WLAN_FC_TODS)
+                       sta->tx_tid[tid]++;
+               else
+                       sta->rx_tid[tid]++;
+       } else {
                tid = 0;
-       if (fc & WLAN_FC_TODS)
+               if (fc & WLAN_FC_TODS)
+                       sta->tx_tid[16]++;
+               else
+                       sta->rx_tid[16]++;
+       }
+       if (tk) {
+               if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
+                       rsc = tdls->rsc_init[tid];
+               else
+                       rsc = tdls->rsc_resp[tid];
+       } else if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
+                  (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
+               if (os_memcmp(sta->addr, hdr->addr2, ETH_ALEN) == 0)
+                       rsc = sta->rsc_tods[tid];
+               else
+                       rsc = sta->rsc_fromds[tid];
+       } else if (fc & WLAN_FC_TODS)
                rsc = sta->rsc_tods[tid];
        else
                rsc = sta->rsc_fromds[tid];
 
 
-       if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
+       if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP)
                tkip_get_pn(pn, data);
+       else if (sta->pairwise_cipher == WPA_CIPHER_WEP40)
+               goto skip_replay_det;
        else
                ccmp_get_pn(pn, data);
        if (os_memcmp(pn, rsc, 6) <= 0) {
-               wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
-                          MAC2STR(hdr->addr2));
+               u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
+               char pn_hex[6 * 2 + 1], rsc_hex[6 * 2 + 1];
+
+               wpa_snprintf_hex(pn_hex, sizeof(pn_hex), pn, 6);
+               wpa_snprintf_hex(rsc_hex, sizeof(rsc_hex), rsc, 6);
+               add_note(wt, MSG_INFO, "replay detected: A1=" MACSTR
+                        " A2=" MACSTR " A3=" MACSTR
+                        " seq=%u frag=%u%s keyid=%d tid=%d %s<=%s",
+                        MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
+                        MAC2STR(hdr->addr3),
+                        WLAN_GET_SEQ_SEQ(seq_ctrl),
+                        WLAN_GET_SEQ_FRAG(seq_ctrl),
+                        (le_to_host16(hdr->frame_control) &  WLAN_FC_RETRY) ?
+                        " Retry" : "",
+                        keyid, tid, pn_hex, rsc_hex);
                wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
                wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
+               replay = 1;
+       }
+
+skip_replay_det:
+       if (tk) {
+               if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256) {
+                       decrypted = ccmp_256_decrypt(tk, hdr, data, len, &dlen);
+                       write_decrypted_note(wt, decrypted, tk, 32, keyid);
+               } else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
+                          sta->pairwise_cipher == WPA_CIPHER_GCMP_256) {
+                       decrypted = gcmp_decrypt(tk, sta->ptk.tk_len, hdr, data,
+                                                len, &dlen);
+                       write_decrypted_note(wt, decrypted, tk, sta->ptk.tk_len,
+                                            keyid);
+               } else {
+                       decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen);
+                       write_decrypted_note(wt, decrypted, tk, 16, keyid);
+               }
+       } else if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
+               decrypted = tkip_decrypt(sta->ptk.tk, hdr, data, len, &dlen);
+               write_decrypted_note(wt, decrypted, sta->ptk.tk, 32, keyid);
+       } else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) {
+               decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
+       } else if (sta->ptk_set) {
+               if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
+                       decrypted = ccmp_256_decrypt(sta->ptk.tk, hdr, data,
+                                                    len, &dlen);
+               else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
+                        sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
+                       decrypted = gcmp_decrypt(sta->ptk.tk, sta->ptk.tk_len,
+                                                hdr, data, len, &dlen);
+               else
+                       decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data, len,
+                                                &dlen);
+               write_decrypted_note(wt, decrypted, sta->ptk.tk,
+                                    sta->ptk.tk_len, keyid);
+       } else {
+               decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, keyid,
+                                       data, len, &dlen);
+               ptk_iter_done = 1;
+       }
+       if (!decrypted && !ptk_iter_done) {
+               decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, keyid,
+                                       data, len, &dlen);
+               if (decrypted) {
+                       add_note(wt, MSG_DEBUG, "Current PTK did not work, but found a match from all known PTKs");
+               }
+       }
+check_zero_tk:
+       if (!decrypted) {
+               struct wpa_ptk zero_ptk;
+               int old_debug_level = wpa_debug_level;
+
+               os_memset(&zero_ptk, 0, sizeof(zero_ptk));
+               zero_ptk.tk_len = wpa_cipher_key_len(sta->pairwise_cipher);
+               wpa_debug_level = MSG_ERROR;
+               decrypted = try_ptk(sta->pairwise_cipher, &zero_ptk, hdr,
+                                   data, len, &dlen);
+               wpa_debug_level = old_debug_level;
+               if (decrypted) {
+                       add_note(wt, MSG_DEBUG,
+                                "Frame was encrypted with zero TK");
+                       wpa_printf(MSG_INFO, "Zero TK used in frame #%u: A2="
+                                  MACSTR " seq=%u",
+                                  wt->frame_num, MAC2STR(hdr->addr2),
+                                  WLAN_GET_SEQ_SEQ(
+                                          le_to_host16(hdr->seq_ctrl)));
+                       write_decrypted_note(wt, decrypted, zero_ptk.tk,
+                                            zero_ptk.tk_len, keyid);
+               }
        }
-
-       if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
-               decrypted = tkip_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
-       else
-               decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
        if (decrypted) {
-               rx_data_process(wt, dst, src, decrypted, dlen, 1);
-               os_memcpy(rsc, pn, 6);
-               write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
+               u16 fc = le_to_host16(hdr->frame_control);
+               const u8 *peer_addr = NULL;
+               if (!(fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)))
+                       peer_addr = hdr->addr1;
+               if (!replay && rsc)
+                       os_memcpy(rsc, pn, 6);
+               rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted,
+                               dlen, 1, peer_addr);
+               write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
                                     decrypted, dlen);
+       } else {
+               if (!try_ptk_iter)
+                       add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
+
+               /* Assume the frame was corrupted and there was no FCS to check.
+                * Allow retry of this particular frame to be processed so that
+                * it could end up getting decrypted if it was received without
+                * corruption. */
+               sta->allow_duplicate = 1;
        }
        os_free(decrypted);
 }
 
 
 static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
-                       const u8 *qos, const u8 *dst, const u8 *src,
-                       const u8 *data, size_t len)
+                       size_t hdrlen, const u8 *qos, const u8 *dst,
+                       const u8 *src, const u8 *data, size_t len)
 {
        u16 fc = le_to_host16(hdr->frame_control);
        int prot = !!(fc & WLAN_FC_ISWEP);
@@ -1260,9 +657,111 @@ static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
        }
 
        if (prot)
-               rx_data_bss_prot(wt, hdr, qos, dst, src, data, len);
+               rx_data_bss_prot(wt, hdr, hdrlen, qos, dst, src, data, len);
+       else {
+               const u8 *bssid, *sta_addr, *peer_addr;
+               struct wlantest_bss *bss;
+
+               if (fc & WLAN_FC_TODS) {
+                       bssid = hdr->addr1;
+                       sta_addr = hdr->addr2;
+                       peer_addr = NULL;
+               } else if (fc & WLAN_FC_FROMDS) {
+                       bssid = hdr->addr2;
+                       sta_addr = hdr->addr1;
+                       peer_addr = NULL;
+               } else {
+                       bssid = hdr->addr3;
+                       sta_addr = hdr->addr2;
+                       peer_addr = hdr->addr1;
+               }
+
+               bss = bss_get(wt, bssid);
+               if (bss) {
+                       struct wlantest_sta *sta = sta_get(bss, sta_addr);
+
+                       if (sta) {
+                               if (qos) {
+                                       int tid = qos[0] & 0x0f;
+                                       if (fc & WLAN_FC_TODS)
+                                               sta->tx_tid[tid]++;
+                                       else
+                                               sta->rx_tid[tid]++;
+                               } else {
+                                       if (fc & WLAN_FC_TODS)
+                                               sta->tx_tid[16]++;
+                                       else
+                                               sta->rx_tid[16]++;
+                               }
+                       }
+               }
+
+               rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0,
+                               peer_addr);
+       }
+}
+
+
+static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid,
+                                      const u8 *sta1_addr,
+                                      const u8 *sta2_addr)
+{
+       struct wlantest_bss *bss;
+       struct wlantest_sta *sta1, *sta2;
+       struct wlantest_tdls *tdls, *found = NULL;
+
+       bss = bss_find(wt, bssid);
+       if (bss == NULL)
+               return NULL;
+       sta1 = sta_find(bss, sta1_addr);
+       if (sta1 == NULL)
+               return NULL;
+       sta2 = sta_find(bss, sta2_addr);
+       if (sta2 == NULL)
+               return NULL;
+
+       dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
+               if ((tdls->init == sta1 && tdls->resp == sta2) ||
+                   (tdls->init == sta2 && tdls->resp == sta1)) {
+                       found = tdls;
+                       if (tdls->link_up)
+                               break;
+               }
+       }
+
+       return found;
+}
+
+
+static void add_direct_link(struct wlantest *wt, const u8 *bssid,
+                           const u8 *sta1_addr, const u8 *sta2_addr)
+{
+       struct wlantest_tdls *tdls;
+
+       tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
+       if (tdls == NULL)
+               return;
+
+       if (tdls->link_up)
+               tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++;
        else
-               rx_data_process(wt, dst, src, data, len, 0);
+               tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++;
+}
+
+
+static void add_ap_path(struct wlantest *wt, const u8 *bssid,
+                       const u8 *sta1_addr, const u8 *sta2_addr)
+{
+       struct wlantest_tdls *tdls;
+
+       tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
+       if (tdls == NULL)
+               return;
+
+       if (tdls->link_up)
+               tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++;
+       else
+               tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++;
 }
 
 
@@ -1300,6 +799,9 @@ void rx_data(struct wlantest *wt, const u8 *data, size_t len)
                           fc & WLAN_FC_ISWEP ? " Prot" : "",
                           MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
                           MAC2STR(hdr->addr3));
+               add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2);
+               rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
+                           data + hdrlen, len - hdrlen);
                break;
        case WLAN_FC_FROMDS:
                wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
@@ -1309,7 +811,8 @@ void rx_data(struct wlantest *wt, const u8 *data, size_t len)
                           fc & WLAN_FC_ISWEP ? " Prot" : "",
                           MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
                           MAC2STR(hdr->addr3));
-               rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
+               add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3);
+               rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr3,
                            data + hdrlen, len - hdrlen);
                break;
        case WLAN_FC_TODS:
@@ -1320,7 +823,8 @@ void rx_data(struct wlantest *wt, const u8 *data, size_t len)
                           fc & WLAN_FC_ISWEP ? " Prot" : "",
                           MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
                           MAC2STR(hdr->addr3));
-               rx_data_bss(wt, hdr, qos, hdr->addr3, hdr->addr2,
+               add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2);
+               rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr3, hdr->addr2,
                            data + hdrlen, len - hdrlen);
                break;
        case WLAN_FC_TODS | WLAN_FC_FROMDS:
@@ -1332,6 +836,8 @@ void rx_data(struct wlantest *wt, const u8 *data, size_t len)
                           MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
                           MAC2STR(hdr->addr3),
                           MAC2STR((const u8 *) (hdr + 1)));
+               rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
+                           data + hdrlen, len - hdrlen);
                break;
        }
 }