rx_data.c 37 KB

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