wlantest: Validate reserved bits in TKIP/CCMP header
[mech_eap.git] / wlantest / rx_data.c
1 /*
2  * Received Data frame processing
3  * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "crypto/aes_wrap.h"
19 #include "crypto/crypto.h"
20 #include "common/defs.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/eapol_common.h"
23 #include "common/wpa_common.h"
24 #include "rsn_supp/wpa_ie.h"
25 #include "wlantest.h"
26
27
28 static const char * data_stype(u16 stype)
29 {
30         switch (stype) {
31         case WLAN_FC_STYPE_DATA:
32                 return "DATA";
33         case WLAN_FC_STYPE_DATA_CFACK:
34                 return "DATA-CFACK";
35         case WLAN_FC_STYPE_DATA_CFPOLL:
36                 return "DATA-CFPOLL";
37         case WLAN_FC_STYPE_DATA_CFACKPOLL:
38                 return "DATA-CFACKPOLL";
39         case WLAN_FC_STYPE_NULLFUNC:
40                 return "NULLFUNC";
41         case WLAN_FC_STYPE_CFACK:
42                 return "CFACK";
43         case WLAN_FC_STYPE_CFPOLL:
44                 return "CFPOLL";
45         case WLAN_FC_STYPE_CFACKPOLL:
46                 return "CFACKPOLL";
47         case WLAN_FC_STYPE_QOS_DATA:
48                 return "QOSDATA";
49         case WLAN_FC_STYPE_QOS_DATA_CFACK:
50                 return "QOSDATA-CFACK";
51         case WLAN_FC_STYPE_QOS_DATA_CFPOLL:
52                 return "QOSDATA-CFPOLL";
53         case WLAN_FC_STYPE_QOS_DATA_CFACKPOLL:
54                 return "QOSDATA-CFACKPOLL";
55         case WLAN_FC_STYPE_QOS_NULL:
56                 return "QOS-NULL";
57         case WLAN_FC_STYPE_QOS_CFPOLL:
58                 return "QOS-CFPOLL";
59         case WLAN_FC_STYPE_QOS_CFACKPOLL:
60                 return "QOS-CFACKPOLL";
61         }
62         return "??";
63 }
64
65
66 static int check_mic(const u8 *kck, int ver, const u8 *data, size_t len)
67 {
68         u8 *buf;
69         int ret = -1;
70         struct ieee802_1x_hdr *hdr;
71         struct wpa_eapol_key *key;
72         u8 rx_mic[16];
73
74         buf = os_malloc(len);
75         if (buf == NULL)
76                 return -1;
77         os_memcpy(buf, data, len);
78         hdr = (struct ieee802_1x_hdr *) buf;
79         key = (struct wpa_eapol_key *) (hdr + 1);
80
81         os_memcpy(rx_mic, key->key_mic, 16);
82         os_memset(key->key_mic, 0, 16);
83
84         if (wpa_eapol_key_mic(kck, ver, buf, len, key->key_mic) == 0 &&
85             os_memcmp(rx_mic, key->key_mic, 16) == 0)
86                 ret = 0;
87
88         os_free(buf);
89
90         return ret;
91 }
92
93
94 static void rx_data_eapol_key_1_of_4(struct wlantest *wt, const u8 *dst,
95                                      const u8 *src, const u8 *data, size_t len)
96 {
97         struct wlantest_bss *bss;
98         struct wlantest_sta *sta;
99         const struct ieee802_1x_hdr *eapol;
100         const struct wpa_eapol_key *hdr;
101
102         wpa_printf(MSG_DEBUG, "EAPOL-Key 1/4 " MACSTR " -> " MACSTR,
103                    MAC2STR(src), MAC2STR(dst));
104         bss = bss_get(wt, src);
105         if (bss == NULL)
106                 return;
107         sta = sta_get(bss, dst);
108         if (sta == NULL)
109                 return;
110
111         eapol = (const struct ieee802_1x_hdr *) data;
112         hdr = (const struct wpa_eapol_key *) (eapol + 1);
113         os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
114 }
115
116
117 static int try_pmk(struct wlantest_bss *bss, struct wlantest_sta *sta,
118                    u16 ver, const u8 *data, size_t len,
119                    struct wlantest_pmk *pmk)
120 {
121         struct wpa_ptk ptk;
122         size_t ptk_len = sta->pairwise_cipher == WPA_CIPHER_TKIP ? 64 : 48;
123         wpa_pmk_to_ptk(pmk->pmk, sizeof(pmk->pmk),
124                        "Pairwise key expansion",
125                        bss->bssid, sta->addr, sta->anonce, sta->snonce,
126                        (u8 *) &ptk, ptk_len,
127                        wpa_key_mgmt_sha256(sta->key_mgmt));
128         if (check_mic(ptk.kck, ver, data, len) < 0)
129                 return -1;
130
131         wpa_printf(MSG_INFO, "Derived PTK for STA " MACSTR " BSSID " MACSTR,
132                    MAC2STR(sta->addr), MAC2STR(bss->bssid));
133         os_memcpy(&sta->ptk, &ptk, sizeof(ptk));
134         wpa_hexdump(MSG_DEBUG, "PTK:KCK", sta->ptk.kck, 16);
135         wpa_hexdump(MSG_DEBUG, "PTK:KEK", sta->ptk.kek, 16);
136         wpa_hexdump(MSG_DEBUG, "PTK:TK1", sta->ptk.tk1, 16);
137         if (ptk_len > 48)
138                 wpa_hexdump(MSG_DEBUG, "PTK:TK2", sta->ptk.u.tk2, 16);
139         sta->ptk_set = 1;
140         os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
141         os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
142         return 0;
143 }
144
145
146 static void derive_ptk(struct wlantest *wt, struct wlantest_bss *bss,
147                        struct wlantest_sta *sta, u16 ver,
148                        const u8 *data, size_t len)
149 {
150         struct wlantest_pmk *pmk;
151
152         dl_list_for_each(pmk, &bss->pmk, struct wlantest_pmk, list) {
153                 if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
154                         return;
155         }
156
157         dl_list_for_each(pmk, &wt->pmk, struct wlantest_pmk, list) {
158                 if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
159                         return;
160         }
161 }
162
163
164 static void rx_data_eapol_key_2_of_4(struct wlantest *wt, const u8 *dst,
165                                      const u8 *src, const u8 *data, size_t len)
166 {
167         struct wlantest_bss *bss;
168         struct wlantest_sta *sta;
169         const struct ieee802_1x_hdr *eapol;
170         const struct wpa_eapol_key *hdr;
171         const u8 *key_data;
172         u16 key_info, key_data_len;
173         struct wpa_eapol_ie_parse ie;
174
175         wpa_printf(MSG_DEBUG, "EAPOL-Key 2/4 " MACSTR " -> " MACSTR,
176                    MAC2STR(src), MAC2STR(dst));
177         bss = bss_get(wt, dst);
178         if (bss == NULL)
179                 return;
180         sta = sta_get(bss, src);
181         if (sta == NULL)
182                 return;
183
184         eapol = (const struct ieee802_1x_hdr *) data;
185         hdr = (const struct wpa_eapol_key *) (eapol + 1);
186         os_memcpy(sta->snonce, hdr->key_nonce, WPA_NONCE_LEN);
187         key_info = WPA_GET_BE16(hdr->key_info);
188         key_data_len = WPA_GET_BE16(hdr->key_data_length);
189         derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK, data, len);
190
191         if (!sta->ptk_set) {
192                 wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 2/4");
193                 return;
194         }
195
196         if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
197                       data, len) < 0) {
198                 wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 2/4 MIC");
199                 return;
200         }
201         wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/4");
202
203         key_data = (const u8 *) (hdr + 1);
204
205         if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
206                 wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
207                 return;
208         }
209
210         if (ie.wpa_ie) {
211                 wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
212                             ie.wpa_ie, ie.wpa_ie_len);
213                 if (os_memcmp(ie.wpa_ie, sta->rsnie, ie.wpa_ie_len) != 0) {
214                         wpa_printf(MSG_INFO, "Mismatch in WPA IE between "
215                                    "EAPOL-Key 2/4 and (Re)Association "
216                                    "Request from " MACSTR, MAC2STR(sta->addr));
217                         wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
218                                     ie.wpa_ie, ie.wpa_ie_len);
219                         wpa_hexdump(MSG_INFO, "WPA IE in (Re)Association "
220                                     "Request",
221                                     sta->rsnie,
222                                     sta->rsnie[0] ? 2 + sta->rsnie[1] : 0);
223                 }
224         }
225
226         if (ie.rsn_ie) {
227                 wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
228                             ie.rsn_ie, ie.rsn_ie_len);
229                 if (os_memcmp(ie.rsn_ie, sta->rsnie, ie.rsn_ie_len) != 0) {
230                         wpa_printf(MSG_INFO, "Mismatch in RSN IE between "
231                                    "EAPOL-Key 2/4 and (Re)Association "
232                                    "Request from " MACSTR, MAC2STR(sta->addr));
233                         wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
234                                     ie.rsn_ie, ie.rsn_ie_len);
235                         wpa_hexdump(MSG_INFO, "RSN IE in (Re)Association "
236                                     "Request",
237                                     sta->rsnie,
238                                     sta->rsnie[0] ? 2 + sta->rsnie[1] : 0);
239                 }
240         }
241 }
242
243
244 static u8 * decrypt_eapol_key_data_rc4(const u8 *kek,
245                                        const struct wpa_eapol_key *hdr,
246                                        size_t *len)
247 {
248         u8 ek[32], *buf;
249         u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
250
251         buf = os_malloc(keydatalen);
252         if (buf == NULL)
253                 return NULL;
254
255         os_memcpy(ek, hdr->key_iv, 16);
256         os_memcpy(ek + 16, kek, 16);
257         os_memcpy(buf, hdr + 1, keydatalen);
258         if (rc4_skip(ek, 32, 256, buf, keydatalen)) {
259                 wpa_printf(MSG_INFO, "RC4 failed");
260                 os_free(buf);
261                 return NULL;
262         }
263
264         *len = keydatalen;
265         return buf;
266 }
267
268
269 static u8 * decrypt_eapol_key_data_aes(const u8 *kek,
270                                        const struct wpa_eapol_key *hdr,
271                                        size_t *len)
272 {
273         u8 *buf;
274         u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
275
276         if (keydatalen % 8) {
277                 wpa_printf(MSG_INFO, "Unsupported AES-WRAP len %d",
278                            keydatalen);
279                 return NULL;
280         }
281         keydatalen -= 8; /* AES-WRAP adds 8 bytes */
282         buf = os_malloc(keydatalen);
283         if (buf == NULL)
284                 return NULL;
285         if (aes_unwrap(kek, keydatalen / 8, (u8 *) (hdr + 1), buf)) {
286                 os_free(buf);
287                 wpa_printf(MSG_INFO, "AES unwrap failed - "
288                            "could not decrypt EAPOL-Key key data");
289                 return NULL;
290         }
291
292         *len = keydatalen;
293         return buf;
294 }
295
296
297 static u8 * decrypt_eapol_key_data(const u8 *kek, u16 ver,
298                                    const struct wpa_eapol_key *hdr,
299                                    size_t *len)
300 {
301         switch (ver) {
302         case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
303                 return decrypt_eapol_key_data_rc4(kek, hdr, len);
304         case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
305         case WPA_KEY_INFO_TYPE_AES_128_CMAC:
306                 return decrypt_eapol_key_data_aes(kek, hdr, len);
307         default:
308                 wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Key Descriptor "
309                            "Version %u", ver);
310                 return NULL;
311         }
312 }
313
314
315 static void learn_kde_keys(struct wlantest_bss *bss, const u8 *buf, size_t len,
316                            const u8 *rsc)
317 {
318         struct wpa_eapol_ie_parse ie;
319
320         if (wpa_supplicant_parse_ies(buf, len, &ie) < 0) {
321                 wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
322                 return;
323         }
324
325         if (ie.wpa_ie) {
326                 wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
327                             ie.wpa_ie, ie.wpa_ie_len);
328         }
329
330         if (ie.rsn_ie) {
331                 wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
332                             ie.rsn_ie, ie.rsn_ie_len);
333         }
334
335         if (ie.gtk) {
336                 wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - GTK KDE",
337                             ie.gtk, ie.gtk_len);
338                 if (ie.gtk_len >= 2 && ie.gtk_len <= 2 + 32) {
339                         int id;
340                         id = ie.gtk[0] & 0x03;
341                         wpa_printf(MSG_DEBUG, "GTK KeyID=%u tx=%u",
342                                    id, !!(ie.gtk[0] & 0x04));
343                         if ((ie.gtk[0] & 0xf8) || ie.gtk[1])
344                                 wpa_printf(MSG_INFO, "GTK KDE: Reserved field "
345                                            "set: %02x %02x",
346                                            ie.gtk[0], ie.gtk[1]);
347                         wpa_hexdump(MSG_DEBUG, "GTK", ie.gtk + 2,
348                                     ie.gtk_len - 2);
349                         bss->gtk_len[id] = ie.gtk_len - 2;
350                         os_memcpy(bss->gtk[id], ie.gtk + 2, ie.gtk_len - 2);
351                         bss->rsc[id][0] = rsc[5];
352                         bss->rsc[id][1] = rsc[4];
353                         bss->rsc[id][2] = rsc[3];
354                         bss->rsc[id][3] = rsc[2];
355                         bss->rsc[id][4] = rsc[1];
356                         bss->rsc[id][5] = rsc[0];
357                         wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
358                 } else {
359                         wpa_printf(MSG_INFO, "Invalid GTK KDE length %u",
360                                    (unsigned) ie.gtk_len);
361                 }
362         }
363
364         if (ie.igtk) {
365                 wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - IGTK KDE",
366                             ie.igtk, ie.igtk_len);
367                 if (ie.igtk_len == 24) {
368                         u16 id;
369                         id = WPA_GET_LE16(ie.igtk);
370                         if (id > 5) {
371                                 wpa_printf(MSG_INFO, "Unexpected IGTK KeyID "
372                                            "%u", id);
373                         } else {
374                                 wpa_printf(MSG_DEBUG, "IGTK KeyID %u", id);
375                                 wpa_hexdump(MSG_DEBUG, "IPN", ie.igtk + 2, 6);
376                                 wpa_hexdump(MSG_DEBUG, "IGTK", ie.igtk + 8,
377                                             16);
378                                 os_memcpy(bss->igtk[id], ie.igtk + 8, 16);
379                                 bss->igtk_set[id] = 1;
380                         }
381                 } else {
382                         wpa_printf(MSG_INFO, "Invalid IGTK KDE length %u",
383                                    (unsigned) ie.igtk_len);
384                 }
385         }
386 }
387
388
389 static void rx_data_eapol_key_3_of_4(struct wlantest *wt, const u8 *dst,
390                                      const u8 *src, const u8 *data, size_t len)
391 {
392         struct wlantest_bss *bss;
393         struct wlantest_sta *sta;
394         const struct ieee802_1x_hdr *eapol;
395         const struct wpa_eapol_key *hdr;
396         const u8 *key_data;
397         int recalc = 0;
398         u16 key_info, ver;
399         u8 *decrypted_buf = NULL;
400         const u8 *decrypted;
401         size_t decrypted_len = 0;
402         struct wpa_eapol_ie_parse ie;
403
404         wpa_printf(MSG_DEBUG, "EAPOL-Key 3/4 " MACSTR " -> " MACSTR,
405                    MAC2STR(src), MAC2STR(dst));
406         bss = bss_get(wt, src);
407         if (bss == NULL)
408                 return;
409         sta = sta_get(bss, dst);
410         if (sta == NULL)
411                 return;
412
413         eapol = (const struct ieee802_1x_hdr *) data;
414         hdr = (const struct wpa_eapol_key *) (eapol + 1);
415         key_info = WPA_GET_BE16(hdr->key_info);
416
417         if (os_memcmp(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN) != 0) {
418                 wpa_printf(MSG_INFO, "EAPOL-Key ANonce mismatch between 1/4 "
419                            "and 3/4");
420                 recalc = 1;
421         }
422         os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
423         if (recalc) {
424                 derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK,
425                            data, len);
426         }
427
428         if (!sta->ptk_set) {
429                 wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 3/4");
430                 return;
431         }
432
433         if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
434                       data, len) < 0) {
435                 wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 3/4 MIC");
436                 return;
437         }
438         wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 3/4");
439
440         key_data = (const u8 *) (hdr + 1);
441         if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
442                 if (sta->proto & WPA_PROTO_RSN)
443                         wpa_printf(MSG_INFO, "EAPOL-Key 3/4 without "
444                                    "EncrKeyData bit");
445                 decrypted = key_data;
446                 decrypted_len = WPA_GET_BE16(hdr->key_data_length);
447         } else {
448                 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
449                 decrypted_buf = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
450                                                        &decrypted_len);
451                 if (decrypted_buf == NULL) {
452                         wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key "
453                                    "Data");
454                         return;
455                 }
456                 decrypted = decrypted_buf;
457                 wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
458                             decrypted, decrypted_len);
459         }
460         if (wt->write_pcap_dumper && decrypted != key_data) {
461                 /* Fill in a dummy Data frame header */
462                 u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
463                 struct ieee80211_hdr *h;
464                 struct wpa_eapol_key *k;
465                 const u8 *p;
466                 u8 *pos;
467                 size_t plain_len;
468
469                 plain_len = decrypted_len;
470                 p = decrypted;
471                 while (p + 1 < decrypted + decrypted_len) {
472                         if (p[0] == 0xdd && p[1] == 0x00) {
473                                 /* Remove padding */
474                                 plain_len = p - decrypted;
475                                 break;
476                         }
477                         p += 2 + p[1];
478                 }
479
480                 os_memset(buf, 0, sizeof(buf));
481                 h = (struct ieee80211_hdr *) buf;
482                 h->frame_control = host_to_le16(0x0208);
483                 os_memcpy(h->addr1, dst, ETH_ALEN);
484                 os_memcpy(h->addr2, src, ETH_ALEN);
485                 os_memcpy(h->addr3, src, ETH_ALEN);
486                 pos = (u8 *) (h + 1);
487                 os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
488                 pos += 8;
489                 os_memcpy(pos, eapol, sizeof(*eapol));
490                 pos += sizeof(*eapol);
491                 os_memcpy(pos, hdr, sizeof(*hdr));
492                 k = (struct wpa_eapol_key *) pos;
493                 WPA_PUT_BE16(k->key_info,
494                              key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
495                 WPA_PUT_BE16(k->key_data_length, plain_len);
496                 write_pcap_decrypted(wt, buf, sizeof(buf),
497                                      decrypted, plain_len);
498         }
499
500         if (wpa_supplicant_parse_ies(decrypted, decrypted_len, &ie) < 0) {
501                 wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
502                 os_free(decrypted_buf);
503                 return;
504         }
505
506         if ((ie.wpa_ie &&
507              os_memcmp(ie.wpa_ie, bss->wpaie, ie.wpa_ie_len) != 0) ||
508             (ie.wpa_ie == NULL && bss->wpaie[0])) {
509                 wpa_printf(MSG_INFO, "Mismatch in WPA IE between "
510                            "EAPOL-Key 3/4 and Beacon/Probe Response "
511                            "from " MACSTR, MAC2STR(bss->bssid));
512                 wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
513                             ie.wpa_ie, ie.wpa_ie_len);
514                 wpa_hexdump(MSG_INFO, "WPA IE in Beacon/Probe "
515                             "Response",
516                             bss->wpaie,
517                             bss->wpaie[0] ? 2 + bss->wpaie[1] : 0);
518         }
519
520         if ((ie.rsn_ie &&
521              os_memcmp(ie.rsn_ie, bss->rsnie, ie.rsn_ie_len) != 0) ||
522             (ie.rsn_ie == NULL && bss->rsnie[0])) {
523                 wpa_printf(MSG_INFO, "Mismatch in RSN IE between "
524                            "EAPOL-Key 3/4 and Beacon/Probe Response "
525                            "from " MACSTR, MAC2STR(bss->bssid));
526                 wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
527                             ie.rsn_ie, ie.rsn_ie_len);
528                 wpa_hexdump(MSG_INFO, "RSN IE in (Re)Association "
529                             "Request",
530                             bss->rsnie,
531                             bss->rsnie[0] ? 2 + bss->rsnie[1] : 0);
532         }
533
534         learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
535         os_free(decrypted_buf);
536 }
537
538
539 static void rx_data_eapol_key_4_of_4(struct wlantest *wt, const u8 *dst,
540                                      const u8 *src, const u8 *data, size_t len)
541 {
542         struct wlantest_bss *bss;
543         struct wlantest_sta *sta;
544         const struct ieee802_1x_hdr *eapol;
545         const struct wpa_eapol_key *hdr;
546         u16 key_info;
547
548         wpa_printf(MSG_DEBUG, "EAPOL-Key 4/4 " MACSTR " -> " MACSTR,
549                    MAC2STR(src), MAC2STR(dst));
550         bss = bss_get(wt, dst);
551         if (bss == NULL)
552                 return;
553         sta = sta_get(bss, src);
554         if (sta == NULL)
555                 return;
556
557         eapol = (const struct ieee802_1x_hdr *) data;
558         hdr = (const struct wpa_eapol_key *) (eapol + 1);
559         key_info = WPA_GET_BE16(hdr->key_info);
560
561         if (!sta->ptk_set) {
562                 wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 4/4");
563                 return;
564         }
565
566         if (sta->ptk_set &&
567             check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
568                       data, len) < 0) {
569                 wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 4/4 MIC");
570                 return;
571         }
572         wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 4/4");
573 }
574
575
576 static void rx_data_eapol_key_1_of_2(struct wlantest *wt, const u8 *dst,
577                                      const u8 *src, const u8 *data, size_t len)
578 {
579         struct wlantest_bss *bss;
580         struct wlantest_sta *sta;
581         const struct ieee802_1x_hdr *eapol;
582         const struct wpa_eapol_key *hdr;
583         const u8 *key_data;
584         u16 key_info, ver;
585         u8 *decrypted;
586         size_t decrypted_len = 0;
587
588         wpa_printf(MSG_DEBUG, "EAPOL-Key 1/2 " MACSTR " -> " MACSTR,
589                    MAC2STR(src), MAC2STR(dst));
590         bss = bss_get(wt, src);
591         if (bss == NULL)
592                 return;
593         sta = sta_get(bss, dst);
594         if (sta == NULL)
595                 return;
596
597         eapol = (const struct ieee802_1x_hdr *) data;
598         hdr = (const struct wpa_eapol_key *) (eapol + 1);
599         key_info = WPA_GET_BE16(hdr->key_info);
600
601         if (!sta->ptk_set) {
602                 wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 1/2");
603                 return;
604         }
605
606         if (sta->ptk_set &&
607             check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
608                       data, len) < 0) {
609                 wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 1/2 MIC");
610                 return;
611         }
612         wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 1/2");
613
614         key_data = (const u8 *) (hdr + 1);
615         if (sta->proto & WPA_PROTO_RSN &&
616             !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
617                 wpa_printf(MSG_INFO, "EAPOL-Key 1/2 without EncrKeyData bit");
618                 return;
619         }
620         ver = key_info & WPA_KEY_INFO_TYPE_MASK;
621         decrypted = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
622                                            &decrypted_len);
623         if (decrypted == NULL) {
624                 wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
625                 return;
626         }
627         wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
628                     decrypted, decrypted_len);
629         if (wt->write_pcap_dumper) {
630                 /* Fill in a dummy Data frame header */
631                 u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
632                 struct ieee80211_hdr *h;
633                 struct wpa_eapol_key *k;
634                 u8 *pos;
635                 size_t plain_len;
636
637                 plain_len = decrypted_len;
638                 pos = decrypted;
639                 while (pos + 1 < decrypted + decrypted_len) {
640                         if (pos[0] == 0xdd && pos[1] == 0x00) {
641                                 /* Remove padding */
642                                 plain_len = pos - decrypted;
643                                 break;
644                         }
645                         pos += 2 + pos[1];
646                 }
647
648                 os_memset(buf, 0, sizeof(buf));
649                 h = (struct ieee80211_hdr *) buf;
650                 h->frame_control = host_to_le16(0x0208);
651                 os_memcpy(h->addr1, dst, ETH_ALEN);
652                 os_memcpy(h->addr2, src, ETH_ALEN);
653                 os_memcpy(h->addr3, src, ETH_ALEN);
654                 pos = (u8 *) (h + 1);
655                 os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
656                 pos += 8;
657                 os_memcpy(pos, eapol, sizeof(*eapol));
658                 pos += sizeof(*eapol);
659                 os_memcpy(pos, hdr, sizeof(*hdr));
660                 k = (struct wpa_eapol_key *) pos;
661                 WPA_PUT_BE16(k->key_info,
662                              key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
663                 WPA_PUT_BE16(k->key_data_length, plain_len);
664                 write_pcap_decrypted(wt, buf, sizeof(buf),
665                                      decrypted, plain_len);
666         }
667         if (sta->proto & WPA_PROTO_RSN)
668                 learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
669         else {
670                 int len = bss->group_cipher == WPA_CIPHER_TKIP ? 32 : 16;
671                 if (decrypted_len == len) {
672                         const u8 *rsc = hdr->key_rsc;
673                         int id;
674                         id = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
675                                 WPA_KEY_INFO_KEY_INDEX_SHIFT;
676                         wpa_printf(MSG_DEBUG, "GTK key index %d", id);
677                         wpa_hexdump(MSG_DEBUG, "GTK", decrypted,
678                                     decrypted_len);
679                         bss->gtk_len[id] = decrypted_len;
680                         os_memcpy(bss->gtk[id], decrypted, decrypted_len);
681                         bss->rsc[id][0] = rsc[5];
682                         bss->rsc[id][1] = rsc[4];
683                         bss->rsc[id][2] = rsc[3];
684                         bss->rsc[id][3] = rsc[2];
685                         bss->rsc[id][4] = rsc[1];
686                         bss->rsc[id][5] = rsc[0];
687                         wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
688                 } else {
689                         wpa_printf(MSG_INFO, "Unexpected WPA Key Data length "
690                                    "in Group Key msg 1/2 from " MACSTR,
691                                    MAC2STR(src));
692                 }
693         }
694         os_free(decrypted);
695 }
696
697
698 static void rx_data_eapol_key_2_of_2(struct wlantest *wt, const u8 *dst,
699                                      const u8 *src, const u8 *data, size_t len)
700 {
701         struct wlantest_bss *bss;
702         struct wlantest_sta *sta;
703         const struct ieee802_1x_hdr *eapol;
704         const struct wpa_eapol_key *hdr;
705         u16 key_info;
706
707         wpa_printf(MSG_DEBUG, "EAPOL-Key 2/2 " MACSTR " -> " MACSTR,
708                    MAC2STR(src), MAC2STR(dst));
709         bss = bss_get(wt, dst);
710         if (bss == NULL)
711                 return;
712         sta = sta_get(bss, src);
713         if (sta == NULL)
714                 return;
715
716         eapol = (const struct ieee802_1x_hdr *) data;
717         hdr = (const struct wpa_eapol_key *) (eapol + 1);
718         key_info = WPA_GET_BE16(hdr->key_info);
719
720         if (!sta->ptk_set) {
721                 wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 2/2");
722                 return;
723         }
724
725         if (sta->ptk_set &&
726             check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
727                       data, len) < 0) {
728                 wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 2/2 MIC");
729                 return;
730         }
731         wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/2");
732 }
733
734
735 static void rx_data_eapol_key(struct wlantest *wt, const u8 *dst,
736                               const u8 *src, const u8 *data, size_t len,
737                               int prot)
738 {
739         const struct ieee802_1x_hdr *eapol;
740         const struct wpa_eapol_key *hdr;
741         const u8 *key_data;
742         u16 key_info, key_length, ver, key_data_length;
743
744         eapol = (const struct ieee802_1x_hdr *) data;
745         hdr = (const struct wpa_eapol_key *) (eapol + 1);
746
747         wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key",
748                     (const u8 *) hdr, len - sizeof(*eapol));
749         if (len < sizeof(*hdr)) {
750                 wpa_printf(MSG_INFO, "Too short EAPOL-Key frame from " MACSTR,
751                            MAC2STR(src));
752                 return;
753         }
754
755         if (hdr->type == EAPOL_KEY_TYPE_RC4) {
756                 /* TODO: EAPOL-Key RC4 for WEP */
757                 return;
758         }
759
760         if (hdr->type != EAPOL_KEY_TYPE_RSN &&
761             hdr->type != EAPOL_KEY_TYPE_WPA) {
762                 wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key type %u",
763                            hdr->type);
764                 return;
765         }
766
767         key_info = WPA_GET_BE16(hdr->key_info);
768         key_length = WPA_GET_BE16(hdr->key_length);
769         key_data_length = WPA_GET_BE16(hdr->key_data_length);
770         key_data = (const u8 *) (hdr + 1);
771         if (key_data + key_data_length > data + len) {
772                 wpa_printf(MSG_INFO, "Truncated EAPOL-Key from " MACSTR,
773                            MAC2STR(src));
774                 return;
775         }
776         if (key_data + key_data_length < data + len) {
777                 wpa_hexdump(MSG_DEBUG, "Extra data after EAPOL-Key Key Data "
778                             "field", key_data + key_data_length,
779                         data + len - key_data - key_data_length);
780         }
781
782
783         ver = key_info & WPA_KEY_INFO_TYPE_MASK;
784         wpa_printf(MSG_DEBUG, "EAPOL-Key ver=%u %c idx=%u%s%s%s%s%s%s%s%s "
785                    "datalen=%u",
786                    ver, key_info & WPA_KEY_INFO_KEY_TYPE ? 'P' : 'G',
787                    (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
788                    WPA_KEY_INFO_KEY_INDEX_SHIFT,
789                    (key_info & WPA_KEY_INFO_INSTALL) ? " Install" : "",
790                    (key_info & WPA_KEY_INFO_ACK) ? " ACK" : "",
791                    (key_info & WPA_KEY_INFO_MIC) ? " MIC" : "",
792                    (key_info & WPA_KEY_INFO_SECURE) ? " Secure" : "",
793                    (key_info & WPA_KEY_INFO_ERROR) ? " Error" : "",
794                    (key_info & WPA_KEY_INFO_REQUEST) ? " Request" : "",
795                    (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) ? " Encr" : "",
796                    (key_info & WPA_KEY_INFO_SMK_MESSAGE) ? " SMK" : "",
797                    key_data_length);
798
799         if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
800             ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
801             ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
802                 wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key Key Descriptor "
803                            "Version %u", ver);
804                 return;
805         }
806
807         wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Replay Counter",
808                     hdr->replay_counter, WPA_REPLAY_COUNTER_LEN);
809         wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Nonce",
810                     hdr->key_nonce, WPA_NONCE_LEN);
811         wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key IV",
812                     hdr->key_iv, 16);
813         wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key RSC",
814                     hdr->key_rsc, WPA_KEY_RSC_LEN);
815         wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key MIC",
816                     hdr->key_mic, 16);
817         wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data",
818                     key_data, key_data_length);
819
820         if (key_info & (WPA_KEY_INFO_ERROR | WPA_KEY_INFO_REQUEST))
821                 return;
822
823         if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
824                 return;
825
826         if (key_info & WPA_KEY_INFO_KEY_TYPE) {
827                 /* 4-Way Handshake */
828                 switch (key_info & (WPA_KEY_INFO_SECURE |
829                                     WPA_KEY_INFO_MIC |
830                                     WPA_KEY_INFO_ACK |
831                                     WPA_KEY_INFO_INSTALL)) {
832                 case WPA_KEY_INFO_ACK:
833                         rx_data_eapol_key_1_of_4(wt, dst, src, data, len);
834                         break;
835                 case WPA_KEY_INFO_MIC:
836                         rx_data_eapol_key_2_of_4(wt, dst, src, data, len);
837                         break;
838                 case WPA_KEY_INFO_MIC | WPA_KEY_INFO_ACK |
839                         WPA_KEY_INFO_INSTALL:
840                         /* WPA does not include Secure bit in 3/4 */
841                         rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
842                         break;
843                 case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
844                         WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL:
845                         rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
846                         break;
847                 case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
848                         rx_data_eapol_key_4_of_4(wt, dst, src, data, len);
849                         break;
850                 default:
851                         wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
852                         break;
853                 }
854         } else {
855                 /* Group Key Handshake */
856                 switch (key_info & (WPA_KEY_INFO_SECURE |
857                                     WPA_KEY_INFO_MIC |
858                                     WPA_KEY_INFO_ACK)) {
859                 case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
860                         WPA_KEY_INFO_ACK:
861                         rx_data_eapol_key_1_of_2(wt, dst, src, data, len);
862                         break;
863                 case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
864                         rx_data_eapol_key_2_of_2(wt, dst, src, data, len);
865                         break;
866                 default:
867                         wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
868                         break;
869                 }
870         }
871 }
872
873
874 static void rx_data_eapol(struct wlantest *wt, const u8 *dst, const u8 *src,
875                           const u8 *data, size_t len, int prot)
876 {
877         const struct ieee802_1x_hdr *hdr;
878         u16 length;
879         const u8 *p;
880
881         wpa_hexdump(MSG_EXCESSIVE, "EAPOL", data, len);
882         if (len < sizeof(*hdr)) {
883                 wpa_printf(MSG_INFO, "Too short EAPOL frame from " MACSTR,
884                            MAC2STR(src));
885                 return;
886         }
887
888         hdr = (const struct ieee802_1x_hdr *) data;
889         length = be_to_host16(hdr->length);
890         wpa_printf(MSG_DEBUG, "RX EAPOL: " MACSTR " -> " MACSTR "%s ver=%u "
891                    "type=%u len=%u",
892                    MAC2STR(src), MAC2STR(dst), prot ? " Prot" : "",
893                    hdr->version, hdr->type, length);
894         if (sizeof(*hdr) + length > len) {
895                 wpa_printf(MSG_INFO, "Truncated EAPOL frame from " MACSTR,
896                            MAC2STR(src));
897                 return;
898         }
899
900         if (sizeof(*hdr) + length < len) {
901                 wpa_printf(MSG_INFO, "EAPOL frame with %d extra bytes",
902                            (int) (len - sizeof(*hdr) - length));
903         }
904         p = (const u8 *) (hdr + 1);
905
906         switch (hdr->type) {
907         case IEEE802_1X_TYPE_EAP_PACKET:
908                 wpa_hexdump(MSG_MSGDUMP, "EAPOL - EAP packet", p, length);
909                 break;
910         case IEEE802_1X_TYPE_EAPOL_START:
911                 wpa_hexdump(MSG_MSGDUMP, "EAPOL-Start", p, length);
912                 break;
913         case IEEE802_1X_TYPE_EAPOL_LOGOFF:
914                 wpa_hexdump(MSG_MSGDUMP, "EAPOL-Logoff", p, length);
915                 break;
916         case IEEE802_1X_TYPE_EAPOL_KEY:
917                 rx_data_eapol_key(wt, dst, src, data, sizeof(*hdr) + length,
918                                   prot);
919                 break;
920         case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
921                 wpa_hexdump(MSG_MSGDUMP, "EAPOL - Encapsulated ASF alert",
922                             p, length);
923                 break;
924         default:
925                 wpa_hexdump(MSG_MSGDUMP, "Unknown EAPOL payload", p, length);
926                 break;
927         }
928 }
929
930
931 static void rx_data_eth(struct wlantest *wt, const u8 *dst, const u8 *src,
932                         u16 ethertype, const u8 *data, size_t len, int prot)
933 {
934         if (ethertype == ETH_P_PAE)
935                 rx_data_eapol(wt, dst, src, data, len, prot);
936 }
937
938
939 static void rx_data_process(struct wlantest *wt, const u8 *dst, const u8 *src,
940                             const u8 *data, size_t len, int prot)
941 {
942         if (len == 0)
943                 return;
944
945         if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
946                 rx_data_eth(wt, dst, src, WPA_GET_BE16(data + 6),
947                             data + 8, len - 8, prot);
948                 return;
949         }
950
951         wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
952 }
953
954
955 static void rx_data_bss_prot_group(struct wlantest *wt,
956                                    const struct ieee80211_hdr *hdr,
957                                    const u8 *qos, const u8 *dst, const u8 *src,
958                                    const u8 *data, size_t len)
959 {
960         struct wlantest_bss *bss;
961         int keyid;
962         u8 *decrypted;
963         size_t dlen;
964         u8 pn[6];
965
966         bss = bss_get(wt, hdr->addr2);
967         if (bss == NULL)
968                 return;
969         if (len < 4) {
970                 wpa_printf(MSG_INFO, "Too short group addressed data frame");
971                 return;
972         }
973
974         if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
975             !(data[3] & 0x20)) {
976                     wpa_printf(MSG_INFO, "Expected TKIP/CCMP frame from "
977                                MACSTR " did not have ExtIV bit set to 1",
978                                MAC2STR(bss->bssid));
979                     return;
980         }
981
982         if (bss->group_cipher == WPA_CIPHER_TKIP) {
983                 if (data[3] & 0x1f) {
984                         wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
985                                    "non-zero reserved bit",
986                                    MAC2STR(bss->bssid));
987                 }
988                 if (data[1] != ((data[0] | 0x20) & 0x7f)) {
989                         wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
990                                    "incorrect WEPSeed[1] (was 0x%x, expected "
991                                    "0x%x)",
992                                    MAC2STR(bss->bssid), data[1],
993                                    (data[0] | 0x20) & 0x7f);
994                 }
995         } else if (bss->group_cipher == WPA_CIPHER_CCMP) {
996                 if (data[2] != 0 || (data[3] & 0x1f) != 0) {
997                         wpa_printf(MSG_INFO, "CCMP frame from " MACSTR " used "
998                                    "non-zero reserved bit",
999                                    MAC2STR(bss->bssid));
1000                 }
1001         }
1002
1003         keyid = data[3] >> 6;
1004         if (bss->gtk_len[keyid] == 0) {
1005                 wpa_printf(MSG_MSGDUMP, "No GTK known to decrypt the frame "
1006                            "(A2=" MACSTR " KeyID=%d)",
1007                            MAC2STR(hdr->addr2), keyid);
1008                 return;
1009         }
1010
1011         if (bss->group_cipher == WPA_CIPHER_TKIP)
1012                 tkip_get_pn(pn, data);
1013         else
1014                 ccmp_get_pn(pn, data);
1015         if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
1016                 wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
1017                            MAC2STR(hdr->addr2));
1018                 wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
1019                 wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
1020         }
1021
1022         if (bss->group_cipher == WPA_CIPHER_TKIP)
1023                 decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
1024                                          &dlen);
1025         else
1026                 decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
1027                                          &dlen);
1028         if (decrypted) {
1029                 rx_data_process(wt, dst, src, decrypted, dlen, 1);
1030                 os_memcpy(bss->rsc[keyid], pn, 6);
1031                 write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
1032                                      decrypted, dlen);
1033         }
1034         os_free(decrypted);
1035 }
1036
1037
1038 static void rx_data_bss_prot(struct wlantest *wt,
1039                              const struct ieee80211_hdr *hdr, const u8 *qos,
1040                              const u8 *dst, const u8 *src, const u8 *data,
1041                              size_t len)
1042 {
1043         struct wlantest_bss *bss;
1044         struct wlantest_sta *sta;
1045         int keyid;
1046         u16 fc = le_to_host16(hdr->frame_control);
1047         u8 *decrypted;
1048         size_t dlen;
1049         int tid;
1050         u8 pn[6], *rsc;
1051
1052         if (hdr->addr1[0] & 0x01) {
1053                 rx_data_bss_prot_group(wt, hdr, qos, dst, src, data, len);
1054                 return;
1055         }
1056
1057         if (fc & WLAN_FC_TODS) {
1058                 bss = bss_get(wt, hdr->addr1);
1059                 if (bss == NULL)
1060                         return;
1061                 sta = sta_get(bss, hdr->addr2);
1062         } else {
1063                 bss = bss_get(wt, hdr->addr2);
1064                 if (bss == NULL)
1065                         return;
1066                 sta = sta_get(bss, hdr->addr1);
1067         }
1068         if (sta == NULL || !sta->ptk_set) {
1069                 wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
1070                 return;
1071         }
1072
1073         if (len < 4) {
1074                 wpa_printf(MSG_INFO, "Too short encrypted data frame");
1075                 return;
1076         }
1077
1078         if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
1079             !(data[3] & 0x20)) {
1080                     wpa_printf(MSG_INFO, "Expected TKIP/CCMP frame from "
1081                                MACSTR " did not have ExtIV bit set to 1",
1082                                MAC2STR(src));
1083                     return;
1084         }
1085
1086         if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
1087                 if (data[3] & 0x1f) {
1088                         wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
1089                                    "non-zero reserved bit",
1090                                    MAC2STR(hdr->addr2));
1091                 }
1092                 if (data[1] != ((data[0] | 0x20) & 0x7f)) {
1093                         wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
1094                                    "incorrect WEPSeed[1] (was 0x%x, expected "
1095                                    "0x%x)",
1096                                    MAC2STR(hdr->addr2), data[1],
1097                                    (data[0] | 0x20) & 0x7f);
1098                 }
1099         } else if (sta->pairwise_cipher == WPA_CIPHER_CCMP) {
1100                 if (data[2] != 0 || (data[3] & 0x1f) != 0) {
1101                         wpa_printf(MSG_INFO, "CCMP frame from " MACSTR " used "
1102                                    "non-zero reserved bit",
1103                                    MAC2STR(hdr->addr2));
1104                 }
1105         }
1106
1107         keyid = data[3] >> 6;
1108         if (keyid != 0) {
1109                 wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
1110                            "individually addressed Data frame from " MACSTR,
1111                            keyid, MAC2STR(hdr->addr2));
1112         }
1113
1114         if (qos)
1115                 tid = qos[0] & 0x0f;
1116         else
1117                 tid = 0;
1118         if (fc & WLAN_FC_TODS)
1119                 rsc = sta->rsc_tods[tid];
1120         else
1121                 rsc = sta->rsc_fromds[tid];
1122
1123
1124         if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
1125                 tkip_get_pn(pn, data);
1126         else
1127                 ccmp_get_pn(pn, data);
1128         if (os_memcmp(pn, rsc, 6) <= 0) {
1129                 wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
1130                            MAC2STR(hdr->addr2));
1131                 wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
1132                 wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
1133         }
1134
1135         if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
1136                 decrypted = tkip_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
1137         else
1138                 decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
1139         if (decrypted) {
1140                 rx_data_process(wt, dst, src, decrypted, dlen, 1);
1141                 os_memcpy(rsc, pn, 6);
1142                 write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
1143                                      decrypted, dlen);
1144         }
1145         os_free(decrypted);
1146 }
1147
1148
1149 static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
1150                         const u8 *qos, const u8 *dst, const u8 *src,
1151                         const u8 *data, size_t len)
1152 {
1153         u16 fc = le_to_host16(hdr->frame_control);
1154         int prot = !!(fc & WLAN_FC_ISWEP);
1155
1156         if (qos) {
1157                 u8 ack = (qos[0] & 0x60) >> 5;
1158                 wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
1159                            " len=%u%s tid=%u%s%s",
1160                            MAC2STR(src), MAC2STR(dst), (unsigned int) len,
1161                            prot ? " Prot" : "", qos[0] & 0x0f,
1162                            (qos[0] & 0x10) ? " EOSP" : "",
1163                            ack == 0 ? "" :
1164                            (ack == 1 ? " NoAck" :
1165                             (ack == 2 ? " NoExpAck" : " BA")));
1166         } else {
1167                 wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
1168                            " len=%u%s",
1169                            MAC2STR(src), MAC2STR(dst), (unsigned int) len,
1170                            prot ? " Prot" : "");
1171         }
1172
1173         if (prot)
1174                 rx_data_bss_prot(wt, hdr, qos, dst, src, data, len);
1175         else
1176                 rx_data_process(wt, dst, src, data, len, 0);
1177 }
1178
1179
1180 void rx_data(struct wlantest *wt, const u8 *data, size_t len)
1181 {
1182         const struct ieee80211_hdr *hdr;
1183         u16 fc, stype;
1184         size_t hdrlen;
1185         const u8 *qos = NULL;
1186
1187         if (len < 24)
1188                 return;
1189
1190         hdr = (const struct ieee80211_hdr *) data;
1191         fc = le_to_host16(hdr->frame_control);
1192         stype = WLAN_FC_GET_STYPE(fc);
1193         hdrlen = 24;
1194         if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
1195             (WLAN_FC_TODS | WLAN_FC_FROMDS))
1196                 hdrlen += ETH_ALEN;
1197         if (stype & 0x08) {
1198                 qos = data + hdrlen;
1199                 hdrlen += 2;
1200         }
1201         if (len < hdrlen)
1202                 return;
1203         wt->rx_data++;
1204
1205         switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
1206         case 0:
1207                 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
1208                            MACSTR " BSSID=" MACSTR,
1209                            data_stype(WLAN_FC_GET_STYPE(fc)),
1210                            fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
1211                            fc & WLAN_FC_ISWEP ? " Prot" : "",
1212                            MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
1213                            MAC2STR(hdr->addr3));
1214                 break;
1215         case WLAN_FC_FROMDS:
1216                 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
1217                            " BSSID=" MACSTR " SA=" MACSTR,
1218                            data_stype(WLAN_FC_GET_STYPE(fc)),
1219                            fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
1220                            fc & WLAN_FC_ISWEP ? " Prot" : "",
1221                            MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
1222                            MAC2STR(hdr->addr3));
1223                 rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
1224                             data + hdrlen, len - hdrlen);
1225                 break;
1226         case WLAN_FC_TODS:
1227                 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
1228                            " SA=" MACSTR " DA=" MACSTR,
1229                            data_stype(WLAN_FC_GET_STYPE(fc)),
1230                            fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
1231                            fc & WLAN_FC_ISWEP ? " Prot" : "",
1232                            MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
1233                            MAC2STR(hdr->addr3));
1234                 rx_data_bss(wt, hdr, qos, hdr->addr3, hdr->addr2,
1235                             data + hdrlen, len - hdrlen);
1236                 break;
1237         case WLAN_FC_TODS | WLAN_FC_FROMDS:
1238                 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
1239                            MACSTR " DA=" MACSTR " SA=" MACSTR,
1240                            data_stype(WLAN_FC_GET_STYPE(fc)),
1241                            fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
1242                            fc & WLAN_FC_ISWEP ? " Prot" : "",
1243                            MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
1244                            MAC2STR(hdr->addr3),
1245                            MAC2STR((const u8 *) (hdr + 1)));
1246                 break;
1247         }
1248 }