2 * Received Data frame processing
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
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"
28 static const char * data_stype(u16 stype
)
31 case WLAN_FC_STYPE_DATA
:
33 case WLAN_FC_STYPE_DATA_CFACK
:
35 case WLAN_FC_STYPE_DATA_CFPOLL
:
37 case WLAN_FC_STYPE_DATA_CFACKPOLL
:
38 return "DATA-CFACKPOLL";
39 case WLAN_FC_STYPE_NULLFUNC
:
41 case WLAN_FC_STYPE_CFACK
:
43 case WLAN_FC_STYPE_CFPOLL
:
45 case WLAN_FC_STYPE_CFACKPOLL
:
47 case WLAN_FC_STYPE_QOS_DATA
:
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
:
57 case WLAN_FC_STYPE_QOS_CFPOLL
:
59 case WLAN_FC_STYPE_QOS_CFACKPOLL
:
60 return "QOS-CFACKPOLL";
66 static int check_mic(const u8
*kck
, int ver
, const u8
*data
, size_t len
)
70 struct ieee802_1x_hdr
*hdr
;
71 struct wpa_eapol_key
*key
;
77 os_memcpy(buf
, data
, len
);
78 hdr
= (struct ieee802_1x_hdr
*) buf
;
79 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
81 os_memcpy(rx_mic
, key
->key_mic
, 16);
82 os_memset(key
->key_mic
, 0, 16);
84 if (wpa_eapol_key_mic(kck
, ver
, buf
, len
, key
->key_mic
) == 0 &&
85 os_memcmp(rx_mic
, key
->key_mic
, 16) == 0)
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
)
97 struct wlantest_bss
*bss
;
98 struct wlantest_sta
*sta
;
99 const struct ieee802_1x_hdr
*eapol
;
100 const struct wpa_eapol_key
*hdr
;
102 wpa_printf(MSG_DEBUG
, "EAPOL-Key 1/4 " MACSTR
" -> " MACSTR
,
103 MAC2STR(src
), MAC2STR(dst
));
104 bss
= bss_get(wt
, src
);
107 sta
= sta_get(bss
, dst
);
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
);
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
)
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)
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);
138 wpa_hexdump(MSG_DEBUG
, "PTK:TK2", sta
->ptk
.u
.tk2
, 16);
140 os_memset(sta
->rsc_tods
, 0, sizeof(sta
->rsc_tods
));
141 os_memset(sta
->rsc_fromds
, 0, sizeof(sta
->rsc_fromds
));
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
)
150 struct wlantest_pmk
*pmk
;
152 dl_list_for_each(pmk
, &bss
->pmk
, struct wlantest_pmk
, list
) {
153 if (try_pmk(bss
, sta
, ver
, data
, len
, pmk
) == 0)
157 dl_list_for_each(pmk
, &wt
->pmk
, struct wlantest_pmk
, list
) {
158 if (try_pmk(bss
, sta
, ver
, data
, len
, pmk
) == 0)
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
)
167 struct wlantest_bss
*bss
;
168 struct wlantest_sta
*sta
;
169 const struct ieee802_1x_hdr
*eapol
;
170 const struct wpa_eapol_key
*hdr
;
172 u16 key_info
, key_data_len
;
173 struct wpa_eapol_ie_parse ie
;
175 wpa_printf(MSG_DEBUG
, "EAPOL-Key 2/4 " MACSTR
" -> " MACSTR
,
176 MAC2STR(src
), MAC2STR(dst
));
177 bss
= bss_get(wt
, dst
);
180 sta
= sta_get(bss
, src
);
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
);
192 wpa_printf(MSG_DEBUG
, "No PTK known to process EAPOL-Key 2/4");
196 if (check_mic(sta
->ptk
.kck
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
198 wpa_printf(MSG_INFO
, "Mismatch in EAPOL-Key 2/4 MIC");
201 wpa_printf(MSG_DEBUG
, "Valid MIC found in EAPOL-Key 2/4");
203 key_data
= (const u8
*) (hdr
+ 1);
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");
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 "
222 sta
->rsnie
[0] ? 2 + sta
->rsnie
[1] : 0);
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 WPA IE between "
231 "EAPOL-Key 2/4 and (Re)Association "
232 "Request from " MACSTR
, MAC2STR(sta
->addr
));
233 wpa_hexdump(MSG_INFO
, "WPA IE in EAPOL-Key",
234 ie
.rsn_ie
, ie
.rsn_ie_len
);
235 wpa_hexdump(MSG_INFO
, "WPA IE in (Re)Association "
238 sta
->rsnie
[0] ? 2 + sta
->rsnie
[1] : 0);
244 static u8
* decrypt_eapol_key_data_rc4(const u8
*kek
,
245 const struct wpa_eapol_key
*hdr
,
249 u16 keydatalen
= WPA_GET_BE16(hdr
->key_data_length
);
251 buf
= os_malloc(keydatalen
);
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");
269 static u8
* decrypt_eapol_key_data_aes(const u8
*kek
,
270 const struct wpa_eapol_key
*hdr
,
274 u16 keydatalen
= WPA_GET_BE16(hdr
->key_data_length
);
276 if (keydatalen
% 8) {
277 wpa_printf(MSG_INFO
, "Unsupported AES-WRAP len %d",
281 keydatalen
-= 8; /* AES-WRAP adds 8 bytes */
282 buf
= os_malloc(keydatalen
);
285 if (aes_unwrap(kek
, keydatalen
/ 8, (u8
*) (hdr
+ 1), buf
)) {
287 wpa_printf(MSG_INFO
, "AES unwrap failed - "
288 "could not decrypt EAPOL-Key key data");
297 static u8
* decrypt_eapol_key_data(const u8
*kek
, u16 ver
,
298 const struct wpa_eapol_key
*hdr
,
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
);
308 wpa_printf(MSG_INFO
, "Unsupported EAPOL-Key Key Descriptor "
315 static void learn_kde_keys(struct wlantest_bss
*bss
, u8
*buf
, size_t len
,
318 struct wpa_eapol_ie_parse ie
;
320 if (wpa_supplicant_parse_ies(buf
, len
, &ie
) < 0) {
321 wpa_printf(MSG_INFO
, "Failed to parse EAPOL-Key Key Data");
326 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key Key Data - WPA IE",
327 ie
.wpa_ie
, ie
.wpa_ie_len
);
331 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key Key Data - RSN IE",
332 ie
.rsn_ie
, ie
.rsn_ie_len
);
336 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key Key Data - GTK KDE",
338 if (ie
.gtk_len
>= 2 && ie
.gtk_len
<= 2 + 32) {
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 "
346 ie
.gtk
[0], ie
.gtk
[1]);
347 wpa_hexdump(MSG_DEBUG
, "GTK", ie
.gtk
+ 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);
359 wpa_printf(MSG_INFO
, "Invalid GTK KDE length %u",
360 (unsigned) ie
.gtk_len
);
365 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key Key Data - IGTK KDE",
366 ie
.igtk
, ie
.igtk_len
);
367 if (ie
.igtk_len
== 24) {
369 id
= WPA_GET_LE16(ie
.igtk
);
371 wpa_printf(MSG_INFO
, "Unexpected IGTK KeyID "
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,
378 os_memcpy(bss
->igtk
[id
], ie
.igtk
+ 8, 16);
379 bss
->igtk_set
[id
] = 1;
382 wpa_printf(MSG_INFO
, "Invalid IGTK KDE length %u",
383 (unsigned) ie
.igtk_len
);
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
)
392 struct wlantest_bss
*bss
;
393 struct wlantest_sta
*sta
;
394 const struct ieee802_1x_hdr
*eapol
;
395 const struct wpa_eapol_key
*hdr
;
400 size_t decrypted_len
= 0;
401 struct wpa_eapol_ie_parse ie
;
403 wpa_printf(MSG_DEBUG
, "EAPOL-Key 3/4 " MACSTR
" -> " MACSTR
,
404 MAC2STR(src
), MAC2STR(dst
));
405 bss
= bss_get(wt
, src
);
408 sta
= sta_get(bss
, dst
);
412 eapol
= (const struct ieee802_1x_hdr
*) data
;
413 hdr
= (const struct wpa_eapol_key
*) (eapol
+ 1);
414 key_info
= WPA_GET_BE16(hdr
->key_info
);
416 if (os_memcmp(sta
->anonce
, hdr
->key_nonce
, WPA_NONCE_LEN
) != 0) {
417 wpa_printf(MSG_INFO
, "EAPOL-Key ANonce mismatch between 1/4 "
421 os_memcpy(sta
->anonce
, hdr
->key_nonce
, WPA_NONCE_LEN
);
423 derive_ptk(wt
, bss
, sta
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
428 wpa_printf(MSG_DEBUG
, "No PTK known to process EAPOL-Key 3/4");
432 if (check_mic(sta
->ptk
.kck
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
434 wpa_printf(MSG_INFO
, "Mismatch in EAPOL-Key 3/4 MIC");
437 wpa_printf(MSG_DEBUG
, "Valid MIC found in EAPOL-Key 3/4");
439 key_data
= (const u8
*) (hdr
+ 1);
440 /* TODO: handle WPA without EncrKeyData bit */
441 if (!(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
442 wpa_printf(MSG_INFO
, "EAPOL-Key 3/4 without EncrKeyData bit");
445 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
446 decrypted
= decrypt_eapol_key_data(sta
->ptk
.kek
, ver
, hdr
,
448 if (decrypted
== NULL
) {
449 wpa_printf(MSG_INFO
, "Failed to decrypt EAPOL-Key Key Data");
452 wpa_hexdump(MSG_DEBUG
, "Decrypted EAPOL-Key Key Data",
453 decrypted
, decrypted_len
);
454 if (wt
->write_pcap_dumper
) {
455 /* Fill in a dummy Data frame header */
456 u8 buf
[24 + 8 + sizeof(*eapol
) + sizeof(*hdr
)];
457 struct ieee80211_hdr
*h
;
458 struct wpa_eapol_key
*k
;
462 plain_len
= decrypted_len
;
464 while (pos
+ 1 < decrypted
+ decrypted_len
) {
465 if (pos
[0] == 0xdd && pos
[1] == 0x00) {
467 plain_len
= pos
- decrypted
;
473 os_memset(buf
, 0, sizeof(buf
));
474 h
= (struct ieee80211_hdr
*) buf
;
475 h
->frame_control
= host_to_le16(0x0208);
476 os_memcpy(h
->addr1
, dst
, ETH_ALEN
);
477 os_memcpy(h
->addr2
, src
, ETH_ALEN
);
478 os_memcpy(h
->addr3
, src
, ETH_ALEN
);
479 pos
= (u8
*) (h
+ 1);
480 os_memcpy(pos
, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
482 os_memcpy(pos
, eapol
, sizeof(*eapol
));
483 pos
+= sizeof(*eapol
);
484 os_memcpy(pos
, hdr
, sizeof(*hdr
));
485 k
= (struct wpa_eapol_key
*) pos
;
486 WPA_PUT_BE16(k
->key_info
,
487 key_info
& ~WPA_KEY_INFO_ENCR_KEY_DATA
);
488 WPA_PUT_BE16(k
->key_data_length
, plain_len
);
489 write_pcap_decrypted(wt
, buf
, sizeof(buf
),
490 decrypted
, plain_len
);
493 if (wpa_supplicant_parse_ies(decrypted
, decrypted_len
, &ie
) < 0) {
494 wpa_printf(MSG_INFO
, "Failed to parse EAPOL-Key Key Data");
500 os_memcmp(ie
.wpa_ie
, bss
->wpaie
, ie
.wpa_ie_len
) != 0) ||
501 (ie
.wpa_ie
== NULL
&& bss
->wpaie
[0])) {
502 wpa_printf(MSG_INFO
, "Mismatch in WPA IE between "
503 "EAPOL-Key 3/4 and Beacon/Probe Response "
504 "from " MACSTR
, MAC2STR(bss
->bssid
));
505 wpa_hexdump(MSG_INFO
, "WPA IE in EAPOL-Key",
506 ie
.wpa_ie
, ie
.wpa_ie_len
);
507 wpa_hexdump(MSG_INFO
, "WPA IE in Beacon/Probe "
510 bss
->wpaie
[0] ? 2 + bss
->wpaie
[1] : 0);
514 os_memcmp(ie
.rsn_ie
, bss
->rsnie
, ie
.rsn_ie_len
) != 0) ||
515 (ie
.rsn_ie
== NULL
&& bss
->rsnie
[0])) {
516 wpa_printf(MSG_INFO
, "Mismatch in RSN IE between "
517 "EAPOL-Key 3/4 and Beacon/Probe Response "
518 "from " MACSTR
, MAC2STR(bss
->bssid
));
519 wpa_hexdump(MSG_INFO
, "RSN IE in EAPOL-Key",
520 ie
.rsn_ie
, ie
.rsn_ie_len
);
521 wpa_hexdump(MSG_INFO
, "RSN IE in (Re)Association "
524 bss
->rsnie
[0] ? 2 + bss
->rsnie
[1] : 0);
527 learn_kde_keys(bss
, decrypted
, decrypted_len
, hdr
->key_rsc
);
532 static void rx_data_eapol_key_4_of_4(struct wlantest
*wt
, const u8
*dst
,
533 const u8
*src
, const u8
*data
, size_t len
)
535 struct wlantest_bss
*bss
;
536 struct wlantest_sta
*sta
;
537 const struct ieee802_1x_hdr
*eapol
;
538 const struct wpa_eapol_key
*hdr
;
541 wpa_printf(MSG_DEBUG
, "EAPOL-Key 4/4 " MACSTR
" -> " MACSTR
,
542 MAC2STR(src
), MAC2STR(dst
));
543 bss
= bss_get(wt
, dst
);
546 sta
= sta_get(bss
, src
);
550 eapol
= (const struct ieee802_1x_hdr
*) data
;
551 hdr
= (const struct wpa_eapol_key
*) (eapol
+ 1);
552 key_info
= WPA_GET_BE16(hdr
->key_info
);
555 wpa_printf(MSG_DEBUG
, "No PTK known to process EAPOL-Key 4/4");
560 check_mic(sta
->ptk
.kck
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
562 wpa_printf(MSG_INFO
, "Mismatch in EAPOL-Key 4/4 MIC");
565 wpa_printf(MSG_DEBUG
, "Valid MIC found in EAPOL-Key 4/4");
569 static void rx_data_eapol_key_1_of_2(struct wlantest
*wt
, const u8
*dst
,
570 const u8
*src
, const u8
*data
, size_t len
)
572 struct wlantest_bss
*bss
;
573 struct wlantest_sta
*sta
;
574 const struct ieee802_1x_hdr
*eapol
;
575 const struct wpa_eapol_key
*hdr
;
579 size_t decrypted_len
= 0;
581 wpa_printf(MSG_DEBUG
, "EAPOL-Key 1/2 " MACSTR
" -> " MACSTR
,
582 MAC2STR(src
), MAC2STR(dst
));
583 bss
= bss_get(wt
, src
);
586 sta
= sta_get(bss
, dst
);
590 eapol
= (const struct ieee802_1x_hdr
*) data
;
591 hdr
= (const struct wpa_eapol_key
*) (eapol
+ 1);
592 key_info
= WPA_GET_BE16(hdr
->key_info
);
595 wpa_printf(MSG_DEBUG
, "No PTK known to process EAPOL-Key 1/2");
600 check_mic(sta
->ptk
.kck
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
602 wpa_printf(MSG_INFO
, "Mismatch in EAPOL-Key 1/2 MIC");
605 wpa_printf(MSG_DEBUG
, "Valid MIC found in EAPOL-Key 1/2");
607 key_data
= (const u8
*) (hdr
+ 1);
608 /* TODO: handle WPA without EncrKeyData bit */
609 if (!(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
610 wpa_printf(MSG_INFO
, "EAPOL-Key 1/2 without EncrKeyData bit");
613 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
614 decrypted
= decrypt_eapol_key_data(sta
->ptk
.kek
, ver
, hdr
,
616 if (decrypted
== NULL
) {
617 wpa_printf(MSG_INFO
, "Failed to decrypt EAPOL-Key Key Data");
620 wpa_hexdump(MSG_DEBUG
, "Decrypted EAPOL-Key Key Data",
621 decrypted
, decrypted_len
);
622 if (wt
->write_pcap_dumper
) {
623 /* Fill in a dummy Data frame header */
624 u8 buf
[24 + 8 + sizeof(*eapol
) + sizeof(*hdr
)];
625 struct ieee80211_hdr
*h
;
626 struct wpa_eapol_key
*k
;
630 plain_len
= decrypted_len
;
632 while (pos
+ 1 < decrypted
+ decrypted_len
) {
633 if (pos
[0] == 0xdd && pos
[1] == 0x00) {
635 plain_len
= pos
- decrypted
;
641 os_memset(buf
, 0, sizeof(buf
));
642 h
= (struct ieee80211_hdr
*) buf
;
643 h
->frame_control
= host_to_le16(0x0208);
644 os_memcpy(h
->addr1
, dst
, ETH_ALEN
);
645 os_memcpy(h
->addr2
, src
, ETH_ALEN
);
646 os_memcpy(h
->addr3
, src
, ETH_ALEN
);
647 pos
= (u8
*) (h
+ 1);
648 os_memcpy(pos
, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
650 os_memcpy(pos
, eapol
, sizeof(*eapol
));
651 pos
+= sizeof(*eapol
);
652 os_memcpy(pos
, hdr
, sizeof(*hdr
));
653 k
= (struct wpa_eapol_key
*) pos
;
654 WPA_PUT_BE16(k
->key_info
,
655 key_info
& ~WPA_KEY_INFO_ENCR_KEY_DATA
);
656 WPA_PUT_BE16(k
->key_data_length
, plain_len
);
657 write_pcap_decrypted(wt
, buf
, sizeof(buf
),
658 decrypted
, plain_len
);
660 learn_kde_keys(bss
, decrypted
, decrypted_len
, hdr
->key_rsc
);
665 static void rx_data_eapol_key_2_of_2(struct wlantest
*wt
, const u8
*dst
,
666 const u8
*src
, const u8
*data
, size_t len
)
668 struct wlantest_bss
*bss
;
669 struct wlantest_sta
*sta
;
670 const struct ieee802_1x_hdr
*eapol
;
671 const struct wpa_eapol_key
*hdr
;
674 wpa_printf(MSG_DEBUG
, "EAPOL-Key 2/2 " MACSTR
" -> " MACSTR
,
675 MAC2STR(src
), MAC2STR(dst
));
676 bss
= bss_get(wt
, dst
);
679 sta
= sta_get(bss
, src
);
683 eapol
= (const struct ieee802_1x_hdr
*) data
;
684 hdr
= (const struct wpa_eapol_key
*) (eapol
+ 1);
685 key_info
= WPA_GET_BE16(hdr
->key_info
);
688 wpa_printf(MSG_DEBUG
, "No PTK known to process EAPOL-Key 2/2");
693 check_mic(sta
->ptk
.kck
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
695 wpa_printf(MSG_INFO
, "Mismatch in EAPOL-Key 2/2 MIC");
698 wpa_printf(MSG_DEBUG
, "Valid MIC found in EAPOL-Key 2/2");
702 static void rx_data_eapol_key(struct wlantest
*wt
, const u8
*dst
,
703 const u8
*src
, const u8
*data
, size_t len
,
706 const struct ieee802_1x_hdr
*eapol
;
707 const struct wpa_eapol_key
*hdr
;
709 u16 key_info
, key_length
, ver
, key_data_length
;
711 eapol
= (const struct ieee802_1x_hdr
*) data
;
712 hdr
= (const struct wpa_eapol_key
*) (eapol
+ 1);
714 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key",
715 (const u8
*) hdr
, len
- sizeof(*eapol
));
716 if (len
< sizeof(*hdr
)) {
717 wpa_printf(MSG_INFO
, "Too short EAPOL-Key frame from " MACSTR
,
722 if (hdr
->type
== EAPOL_KEY_TYPE_RC4
) {
723 /* TODO: EAPOL-Key RC4 for WEP */
727 if (hdr
->type
!= EAPOL_KEY_TYPE_RSN
&&
728 hdr
->type
!= EAPOL_KEY_TYPE_WPA
) {
729 wpa_printf(MSG_DEBUG
, "Unsupported EAPOL-Key type %u",
734 key_info
= WPA_GET_BE16(hdr
->key_info
);
735 key_length
= WPA_GET_BE16(hdr
->key_length
);
736 key_data_length
= WPA_GET_BE16(hdr
->key_data_length
);
737 key_data
= (const u8
*) (hdr
+ 1);
738 if (key_data
+ key_data_length
> data
+ len
) {
739 wpa_printf(MSG_INFO
, "Truncated EAPOL-Key from " MACSTR
,
743 if (key_data
+ key_data_length
< data
+ len
) {
744 wpa_hexdump(MSG_DEBUG
, "Extra data after EAPOL-Key Key Data "
745 "field", key_data
+ key_data_length
,
746 data
+ len
- key_data
- key_data_length
);
750 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
751 wpa_printf(MSG_DEBUG
, "EAPOL-Key ver=%u %c idx=%u%s%s%s%s%s%s%s%s "
753 ver
, key_info
& WPA_KEY_INFO_KEY_TYPE
? 'P' : 'G',
754 (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
755 WPA_KEY_INFO_KEY_INDEX_SHIFT
,
756 (key_info
& WPA_KEY_INFO_INSTALL
) ? " Install" : "",
757 (key_info
& WPA_KEY_INFO_ACK
) ? " ACK" : "",
758 (key_info
& WPA_KEY_INFO_MIC
) ? " MIC" : "",
759 (key_info
& WPA_KEY_INFO_SECURE
) ? " Secure" : "",
760 (key_info
& WPA_KEY_INFO_ERROR
) ? " Error" : "",
761 (key_info
& WPA_KEY_INFO_REQUEST
) ? " Request" : "",
762 (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) ? " Encr" : "",
763 (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) ? " SMK" : "",
766 if (ver
!= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&&
767 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
&&
768 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
769 wpa_printf(MSG_DEBUG
, "Unsupported EAPOL-Key Key Descriptor "
774 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key Replay Counter",
775 hdr
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
776 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key Key Nonce",
777 hdr
->key_nonce
, WPA_NONCE_LEN
);
778 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key Key IV",
780 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key RSC",
781 hdr
->key_rsc
, WPA_KEY_RSC_LEN
);
782 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key Key MIC",
784 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Key Key Data",
785 key_data
, key_data_length
);
787 if (key_info
& (WPA_KEY_INFO_ERROR
| WPA_KEY_INFO_REQUEST
))
790 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
)
793 if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
794 /* 4-Way Handshake */
795 switch (key_info
& (WPA_KEY_INFO_SECURE
|
798 WPA_KEY_INFO_INSTALL
)) {
799 case WPA_KEY_INFO_ACK
:
800 rx_data_eapol_key_1_of_4(wt
, dst
, src
, data
, len
);
802 case WPA_KEY_INFO_MIC
:
803 rx_data_eapol_key_2_of_4(wt
, dst
, src
, data
, len
);
805 case WPA_KEY_INFO_SECURE
| WPA_KEY_INFO_MIC
|
806 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
:
807 rx_data_eapol_key_3_of_4(wt
, dst
, src
, data
, len
);
809 case WPA_KEY_INFO_SECURE
| WPA_KEY_INFO_MIC
:
810 rx_data_eapol_key_4_of_4(wt
, dst
, src
, data
, len
);
813 wpa_printf(MSG_DEBUG
, "Unsupported EAPOL-Key frame");
817 /* Group Key Handshake */
818 switch (key_info
& (WPA_KEY_INFO_SECURE
|
821 case WPA_KEY_INFO_SECURE
| WPA_KEY_INFO_MIC
|
823 rx_data_eapol_key_1_of_2(wt
, dst
, src
, data
, len
);
825 case WPA_KEY_INFO_SECURE
| WPA_KEY_INFO_MIC
:
826 rx_data_eapol_key_2_of_2(wt
, dst
, src
, data
, len
);
829 wpa_printf(MSG_DEBUG
, "Unsupported EAPOL-Key frame");
836 static void rx_data_eapol(struct wlantest
*wt
, const u8
*dst
, const u8
*src
,
837 const u8
*data
, size_t len
, int prot
)
839 const struct ieee802_1x_hdr
*hdr
;
843 wpa_hexdump(MSG_EXCESSIVE
, "EAPOL", data
, len
);
844 if (len
< sizeof(*hdr
)) {
845 wpa_printf(MSG_INFO
, "Too short EAPOL frame from " MACSTR
,
850 hdr
= (const struct ieee802_1x_hdr
*) data
;
851 length
= be_to_host16(hdr
->length
);
852 wpa_printf(MSG_DEBUG
, "RX EAPOL: " MACSTR
" -> " MACSTR
"%s ver=%u "
854 MAC2STR(src
), MAC2STR(dst
), prot
? " Prot" : "",
855 hdr
->version
, hdr
->type
, length
);
856 if (sizeof(*hdr
) + length
> len
) {
857 wpa_printf(MSG_INFO
, "Truncated EAPOL frame from " MACSTR
,
862 if (sizeof(*hdr
) + length
< len
) {
863 wpa_printf(MSG_INFO
, "EAPOL frame with %d extra bytes",
864 (int) (len
- sizeof(*hdr
) - length
));
866 p
= (const u8
*) (hdr
+ 1);
869 case IEEE802_1X_TYPE_EAP_PACKET
:
870 wpa_hexdump(MSG_MSGDUMP
, "EAPOL - EAP packet", p
, length
);
872 case IEEE802_1X_TYPE_EAPOL_START
:
873 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Start", p
, length
);
875 case IEEE802_1X_TYPE_EAPOL_LOGOFF
:
876 wpa_hexdump(MSG_MSGDUMP
, "EAPOL-Logoff", p
, length
);
878 case IEEE802_1X_TYPE_EAPOL_KEY
:
879 rx_data_eapol_key(wt
, dst
, src
, data
, sizeof(*hdr
) + length
,
882 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT
:
883 wpa_hexdump(MSG_MSGDUMP
, "EAPOL - Encapsulated ASF alert",
887 wpa_hexdump(MSG_MSGDUMP
, "Unknown EAPOL payload", p
, length
);
893 static void rx_data_eth(struct wlantest
*wt
, const u8
*dst
, const u8
*src
,
894 u16 ethertype
, const u8
*data
, size_t len
, int prot
)
896 if (ethertype
== ETH_P_PAE
)
897 rx_data_eapol(wt
, dst
, src
, data
, len
, prot
);
901 static void rx_data_process(struct wlantest
*wt
, const u8
*dst
, const u8
*src
,
902 const u8
*data
, size_t len
, int prot
)
907 if (len
>= 8 && os_memcmp(data
, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
908 rx_data_eth(wt
, dst
, src
, WPA_GET_BE16(data
+ 6),
909 data
+ 8, len
- 8, prot
);
913 wpa_hexdump(MSG_DEBUG
, "Unrecognized LLC", data
, len
> 8 ? 8 : len
);
917 static void rx_data_bss_prot_group(struct wlantest
*wt
,
918 const struct ieee80211_hdr
*hdr
,
919 const u8
*qos
, const u8
*dst
, const u8
*src
,
920 const u8
*data
, size_t len
)
922 struct wlantest_bss
*bss
;
928 bss
= bss_get(wt
, hdr
->addr2
);
932 wpa_printf(MSG_INFO
, "Too short group addressed data frame");
936 keyid
= data
[3] >> 6;
937 if (bss
->gtk_len
[keyid
] == 0) {
938 wpa_printf(MSG_MSGDUMP
, "No GTK known to decrypt the frame "
939 "(A2=" MACSTR
" KeyID=%d)",
940 MAC2STR(hdr
->addr2
), keyid
);
944 /* TODO: different replay protection for TKIP */
945 ccmp_get_pn(pn
, data
);
946 if (os_memcmp(pn
, bss
->rsc
[keyid
], 6) <= 0) {
947 wpa_printf(MSG_INFO
, "CCMP/TKIP replay detected: SA=" MACSTR
,
948 MAC2STR(hdr
->addr2
));
949 wpa_hexdump(MSG_INFO
, "RX PN", pn
, 6);
950 wpa_hexdump(MSG_INFO
, "RSC", bss
->rsc
[keyid
], 6);
953 if (bss
->group_cipher
== WPA_CIPHER_TKIP
)
954 decrypted
= tkip_decrypt(bss
->gtk
[keyid
], hdr
, data
, len
,
957 decrypted
= ccmp_decrypt(bss
->gtk
[keyid
], hdr
, data
, len
,
960 rx_data_process(wt
, dst
, src
, decrypted
, dlen
, 1);
961 os_memcpy(bss
->rsc
[keyid
], pn
, 6);
962 write_pcap_decrypted(wt
, (const u8
*) hdr
, 24 + (qos
? 2 : 0),
969 static void rx_data_bss_prot(struct wlantest
*wt
,
970 const struct ieee80211_hdr
*hdr
, const u8
*qos
,
971 const u8
*dst
, const u8
*src
, const u8
*data
,
974 struct wlantest_bss
*bss
;
975 struct wlantest_sta
*sta
;
977 u16 fc
= le_to_host16(hdr
->frame_control
);
983 if (hdr
->addr1
[0] & 0x01) {
984 rx_data_bss_prot_group(wt
, hdr
, qos
, dst
, src
, data
, len
);
988 if (fc
& WLAN_FC_TODS
) {
989 bss
= bss_get(wt
, hdr
->addr1
);
992 sta
= sta_get(bss
, hdr
->addr2
);
994 bss
= bss_get(wt
, hdr
->addr2
);
997 sta
= sta_get(bss
, hdr
->addr1
);
999 if (sta
== NULL
|| !sta
->ptk_set
) {
1000 wpa_printf(MSG_MSGDUMP
, "No PTK known to decrypt the frame");
1005 wpa_printf(MSG_INFO
, "Too short encrypted data frame");
1009 keyid
= data
[3] >> 6;
1011 wpa_printf(MSG_INFO
, "Unexpected non-zero KeyID %d in "
1012 "individually addressed Data frame from " MACSTR
,
1013 keyid
, MAC2STR(hdr
->addr2
));
1017 tid
= qos
[0] & 0x0f;
1020 if (fc
& WLAN_FC_TODS
)
1021 rsc
= sta
->rsc_tods
[tid
];
1023 rsc
= sta
->rsc_fromds
[tid
];
1026 ccmp_get_pn(pn
, data
);
1027 if (os_memcmp(pn
, rsc
, 6) <= 0) {
1028 wpa_printf(MSG_INFO
, "CCMP/TKIP replay detected: SA=" MACSTR
,
1029 MAC2STR(hdr
->addr2
));
1030 wpa_hexdump(MSG_INFO
, "RX PN", pn
, 6);
1031 wpa_hexdump(MSG_INFO
, "RSC", rsc
, 6);
1034 if (sta
->pairwise_cipher
== WPA_CIPHER_TKIP
)
1035 decrypted
= tkip_decrypt(sta
->ptk
.tk1
, hdr
, data
, len
, &dlen
);
1037 decrypted
= ccmp_decrypt(sta
->ptk
.tk1
, hdr
, data
, len
, &dlen
);
1039 rx_data_process(wt
, dst
, src
, decrypted
, dlen
, 1);
1040 os_memcpy(rsc
, pn
, 6);
1041 write_pcap_decrypted(wt
, (const u8
*) hdr
, 24 + (qos
? 2 : 0),
1048 static void rx_data_bss(struct wlantest
*wt
, const struct ieee80211_hdr
*hdr
,
1049 const u8
*qos
, const u8
*dst
, const u8
*src
,
1050 const u8
*data
, size_t len
)
1052 u16 fc
= le_to_host16(hdr
->frame_control
);
1053 int prot
= !!(fc
& WLAN_FC_ISWEP
);
1056 u8 ack
= (qos
[0] & 0x60) >> 5;
1057 wpa_printf(MSG_MSGDUMP
, "BSS DATA: " MACSTR
" -> " MACSTR
1058 " len=%u%s tid=%u%s%s",
1059 MAC2STR(src
), MAC2STR(dst
), (unsigned int) len
,
1060 prot
? " Prot" : "", qos
[0] & 0x0f,
1061 (qos
[0] & 0x10) ? " EOSP" : "",
1063 (ack
== 1 ? " NoAck" :
1064 (ack
== 2 ? " NoExpAck" : " BA")));
1066 wpa_printf(MSG_MSGDUMP
, "BSS DATA: " MACSTR
" -> " MACSTR
1068 MAC2STR(src
), MAC2STR(dst
), (unsigned int) len
,
1069 prot
? " Prot" : "");
1073 rx_data_bss_prot(wt
, hdr
, qos
, dst
, src
, data
, len
);
1075 rx_data_process(wt
, dst
, src
, data
, len
, 0);
1079 void rx_data(struct wlantest
*wt
, const u8
*data
, size_t len
)
1081 const struct ieee80211_hdr
*hdr
;
1084 const u8
*qos
= NULL
;
1089 hdr
= (const struct ieee80211_hdr
*) data
;
1090 fc
= le_to_host16(hdr
->frame_control
);
1091 stype
= WLAN_FC_GET_STYPE(fc
);
1093 if ((fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
1094 (WLAN_FC_TODS
| WLAN_FC_FROMDS
))
1097 qos
= data
+ hdrlen
;
1104 switch (fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) {
1106 wpa_printf(MSG_EXCESSIVE
, "DATA %s%s%s IBSS DA=" MACSTR
" SA="
1107 MACSTR
" BSSID=" MACSTR
,
1108 data_stype(WLAN_FC_GET_STYPE(fc
)),
1109 fc
& WLAN_FC_PWRMGT
? " PwrMgt" : "",
1110 fc
& WLAN_FC_ISWEP
? " Prot" : "",
1111 MAC2STR(hdr
->addr1
), MAC2STR(hdr
->addr2
),
1112 MAC2STR(hdr
->addr3
));
1114 case WLAN_FC_FROMDS
:
1115 wpa_printf(MSG_EXCESSIVE
, "DATA %s%s%s FromDS DA=" MACSTR
1116 " BSSID=" MACSTR
" SA=" MACSTR
,
1117 data_stype(WLAN_FC_GET_STYPE(fc
)),
1118 fc
& WLAN_FC_PWRMGT
? " PwrMgt" : "",
1119 fc
& WLAN_FC_ISWEP
? " Prot" : "",
1120 MAC2STR(hdr
->addr1
), MAC2STR(hdr
->addr2
),
1121 MAC2STR(hdr
->addr3
));
1122 rx_data_bss(wt
, hdr
, qos
, hdr
->addr1
, hdr
->addr2
,
1123 data
+ hdrlen
, len
- hdrlen
);
1126 wpa_printf(MSG_EXCESSIVE
, "DATA %s%s%s ToDS BSSID=" MACSTR
1127 " SA=" MACSTR
" DA=" MACSTR
,
1128 data_stype(WLAN_FC_GET_STYPE(fc
)),
1129 fc
& WLAN_FC_PWRMGT
? " PwrMgt" : "",
1130 fc
& WLAN_FC_ISWEP
? " Prot" : "",
1131 MAC2STR(hdr
->addr1
), MAC2STR(hdr
->addr2
),
1132 MAC2STR(hdr
->addr3
));
1133 rx_data_bss(wt
, hdr
, qos
, hdr
->addr3
, hdr
->addr2
,
1134 data
+ hdrlen
, len
- hdrlen
);
1136 case WLAN_FC_TODS
| WLAN_FC_FROMDS
:
1137 wpa_printf(MSG_EXCESSIVE
, "DATA %s%s%s WDS RA=" MACSTR
" TA="
1138 MACSTR
" DA=" MACSTR
" SA=" MACSTR
,
1139 data_stype(WLAN_FC_GET_STYPE(fc
)),
1140 fc
& WLAN_FC_PWRMGT
? " PwrMgt" : "",
1141 fc
& WLAN_FC_ISWEP
? " Prot" : "",
1142 MAC2STR(hdr
->addr1
), MAC2STR(hdr
->addr2
),
1143 MAC2STR(hdr
->addr3
),
1144 MAC2STR((const u8
*) (hdr
+ 1)));