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