rx_data.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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/ieee802_11_defs.h"
  19. #include "common/eapol_common.h"
  20. #include "common/wpa_common.h"
  21. #include "rsn_supp/wpa_ie.h"
  22. #include "wlantest.h"
  23. static const char * data_stype(u16 stype)
  24. {
  25. switch (stype) {
  26. case WLAN_FC_STYPE_DATA:
  27. return "DATA";
  28. case WLAN_FC_STYPE_DATA_CFACK:
  29. return "DATA-CFACK";
  30. case WLAN_FC_STYPE_DATA_CFPOLL:
  31. return "DATA-CFPOLL";
  32. case WLAN_FC_STYPE_DATA_CFACKPOLL:
  33. return "DATA-CFACKPOLL";
  34. case WLAN_FC_STYPE_NULLFUNC:
  35. return "NULLFUNC";
  36. case WLAN_FC_STYPE_CFACK:
  37. return "CFACK";
  38. case WLAN_FC_STYPE_CFPOLL:
  39. return "CFPOLL";
  40. case WLAN_FC_STYPE_CFACKPOLL:
  41. return "CFACKPOLL";
  42. case WLAN_FC_STYPE_QOS_DATA:
  43. return "QOSDATA";
  44. case WLAN_FC_STYPE_QOS_DATA_CFACK:
  45. return "QOSDATA-CFACK";
  46. case WLAN_FC_STYPE_QOS_DATA_CFPOLL:
  47. return "QOSDATA-CFPOLL";
  48. case WLAN_FC_STYPE_QOS_DATA_CFACKPOLL:
  49. return "QOSDATA-CFACKPOLL";
  50. case WLAN_FC_STYPE_QOS_NULL:
  51. return "QOS-NULL";
  52. case WLAN_FC_STYPE_QOS_CFPOLL:
  53. return "QOS-CFPOLL";
  54. case WLAN_FC_STYPE_QOS_CFACKPOLL:
  55. return "QOS-CFACKPOLL";
  56. }
  57. return "??";
  58. }
  59. static int check_mic(const u8 *kck, int ver, const u8 *data, size_t len)
  60. {
  61. u8 *buf;
  62. int ret = -1;
  63. struct ieee802_1x_hdr *hdr;
  64. struct wpa_eapol_key *key;
  65. u8 rx_mic[16];
  66. buf = os_malloc(len);
  67. if (buf == NULL)
  68. return -1;
  69. os_memcpy(buf, data, len);
  70. hdr = (struct ieee802_1x_hdr *) buf;
  71. key = (struct wpa_eapol_key *) (hdr + 1);
  72. os_memcpy(rx_mic, key->key_mic, 16);
  73. os_memset(key->key_mic, 0, 16);
  74. if (wpa_eapol_key_mic(kck, ver, buf, len, key->key_mic) == 0 &&
  75. os_memcmp(rx_mic, key->key_mic, 16) == 0)
  76. ret = 0;
  77. os_free(buf);
  78. return ret;
  79. }
  80. static void rx_data_eapol_key_1_of_4(struct wlantest *wt, const u8 *dst,
  81. const u8 *src, const u8 *data, size_t len)
  82. {
  83. struct wlantest_bss *bss;
  84. struct wlantest_sta *sta;
  85. const struct ieee802_1x_hdr *eapol;
  86. const struct wpa_eapol_key *hdr;
  87. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/4 " MACSTR " -> " MACSTR,
  88. MAC2STR(src), MAC2STR(dst));
  89. bss = bss_get(wt, src);
  90. if (bss == NULL)
  91. return;
  92. sta = sta_get(bss, dst);
  93. if (sta == NULL)
  94. return;
  95. eapol = (const struct ieee802_1x_hdr *) data;
  96. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  97. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  98. }
  99. static int try_pmk(struct wlantest_bss *bss, struct wlantest_sta *sta,
  100. u16 ver, const u8 *data, size_t len,
  101. struct wlantest_pmk *pmk)
  102. {
  103. struct wpa_ptk ptk;
  104. size_t ptk_len = 48; /* FIX: 64 for TKIP */
  105. wpa_pmk_to_ptk(pmk->pmk, sizeof(pmk->pmk),
  106. "Pairwise key expansion",
  107. bss->bssid, sta->addr, sta->anonce, sta->snonce,
  108. (u8 *) &ptk, ptk_len,
  109. 0 /* FIX: SHA256 based on AKM */);
  110. if (check_mic(ptk.kck, ver, data, len) < 0)
  111. return -1;
  112. wpa_printf(MSG_INFO, "Derived PTK for STA " MACSTR " BSSID " MACSTR,
  113. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  114. os_memcpy(&sta->ptk, &ptk, sizeof(ptk));
  115. wpa_hexdump(MSG_DEBUG, "PTK:KCK", sta->ptk.kck, 16);
  116. wpa_hexdump(MSG_DEBUG, "PTK:KEK", sta->ptk.kek, 16);
  117. wpa_hexdump(MSG_DEBUG, "PTK:TK1", sta->ptk.tk1, 16);
  118. if (ptk_len > 48)
  119. wpa_hexdump(MSG_DEBUG, "PTK:TK2", sta->ptk.u.tk2, 16);
  120. sta->ptk_set = 1;
  121. os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
  122. os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
  123. return 0;
  124. }
  125. static void derive_ptk(struct wlantest *wt, struct wlantest_bss *bss,
  126. struct wlantest_sta *sta, u16 ver,
  127. const u8 *data, size_t len)
  128. {
  129. struct wlantest_pmk *pmk;
  130. dl_list_for_each(pmk, &bss->pmk, struct wlantest_pmk, list) {
  131. if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
  132. return;
  133. }
  134. dl_list_for_each(pmk, &wt->pmk, struct wlantest_pmk, list) {
  135. if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
  136. return;
  137. }
  138. }
  139. static void rx_data_eapol_key_2_of_4(struct wlantest *wt, const u8 *dst,
  140. const u8 *src, const u8 *data, size_t len)
  141. {
  142. struct wlantest_bss *bss;
  143. struct wlantest_sta *sta;
  144. const struct ieee802_1x_hdr *eapol;
  145. const struct wpa_eapol_key *hdr;
  146. const u8 *key_data;
  147. u16 key_info, key_data_len;
  148. struct wpa_eapol_ie_parse ie;
  149. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/4 " MACSTR " -> " MACSTR,
  150. MAC2STR(src), MAC2STR(dst));
  151. bss = bss_get(wt, dst);
  152. if (bss == NULL)
  153. return;
  154. sta = sta_get(bss, src);
  155. if (sta == NULL)
  156. return;
  157. eapol = (const struct ieee802_1x_hdr *) data;
  158. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  159. os_memcpy(sta->snonce, hdr->key_nonce, WPA_NONCE_LEN);
  160. key_info = WPA_GET_BE16(hdr->key_info);
  161. key_data_len = WPA_GET_BE16(hdr->key_data_length);
  162. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK, data, len);
  163. if (!sta->ptk_set) {
  164. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 2/4");
  165. return;
  166. }
  167. if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  168. data, len) < 0) {
  169. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 2/4 MIC");
  170. return;
  171. }
  172. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/4");
  173. key_data = (const u8 *) (hdr + 1);
  174. if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
  175. wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  176. return;
  177. }
  178. if (ie.wpa_ie) {
  179. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
  180. ie.wpa_ie, ie.wpa_ie_len);
  181. }
  182. if (ie.rsn_ie) {
  183. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
  184. ie.rsn_ie, ie.rsn_ie_len);
  185. }
  186. }
  187. static u8 * decrypt_eapol_key_data_rc4(const u8 *kek,
  188. const struct wpa_eapol_key *hdr,
  189. size_t *len)
  190. {
  191. u8 ek[32], *buf;
  192. u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
  193. buf = os_malloc(keydatalen);
  194. if (buf == NULL)
  195. return NULL;
  196. os_memcpy(ek, hdr->key_iv, 16);
  197. os_memcpy(ek + 16, kek, 16);
  198. os_memcpy(buf, hdr + 1, keydatalen);
  199. if (rc4_skip(ek, 32, 256, buf, keydatalen)) {
  200. wpa_printf(MSG_INFO, "RC4 failed");
  201. os_free(buf);
  202. return NULL;
  203. }
  204. *len = keydatalen;
  205. return buf;
  206. }
  207. static u8 * decrypt_eapol_key_data_aes(const u8 *kek,
  208. const struct wpa_eapol_key *hdr,
  209. size_t *len)
  210. {
  211. u8 *buf;
  212. u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
  213. if (keydatalen % 8) {
  214. wpa_printf(MSG_INFO, "Unsupported AES-WRAP len %d",
  215. keydatalen);
  216. return NULL;
  217. }
  218. keydatalen -= 8; /* AES-WRAP adds 8 bytes */
  219. buf = os_malloc(keydatalen);
  220. if (buf == NULL)
  221. return NULL;
  222. if (aes_unwrap(kek, keydatalen / 8, (u8 *) (hdr + 1), buf)) {
  223. os_free(buf);
  224. wpa_printf(MSG_INFO, "AES unwrap failed - "
  225. "could not decrypt EAPOL-Key key data");
  226. return NULL;
  227. }
  228. *len = keydatalen;
  229. return buf;
  230. }
  231. static u8 * decrypt_eapol_key_data(const u8 *kek, u16 ver,
  232. const struct wpa_eapol_key *hdr,
  233. size_t *len)
  234. {
  235. switch (ver) {
  236. case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
  237. return decrypt_eapol_key_data_rc4(kek, hdr, len);
  238. case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
  239. case WPA_KEY_INFO_TYPE_AES_128_CMAC:
  240. return decrypt_eapol_key_data_aes(kek, hdr, len);
  241. default:
  242. wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Key Descriptor "
  243. "Version %u", ver);
  244. return NULL;
  245. }
  246. }
  247. static void learn_kde_keys(struct wlantest_bss *bss, u8 *buf, size_t len,
  248. const u8 *rsc)
  249. {
  250. struct wpa_eapol_ie_parse ie;
  251. if (wpa_supplicant_parse_ies(buf, len, &ie) < 0) {
  252. wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  253. return;
  254. }
  255. if (ie.wpa_ie) {
  256. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
  257. ie.wpa_ie, ie.wpa_ie_len);
  258. }
  259. if (ie.rsn_ie) {
  260. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
  261. ie.rsn_ie, ie.rsn_ie_len);
  262. }
  263. if (ie.gtk) {
  264. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - GTK KDE",
  265. ie.gtk, ie.gtk_len);
  266. if (ie.gtk_len >= 2 && ie.gtk_len <= 2 + 32) {
  267. int id;
  268. id = ie.gtk[0] & 0x03;
  269. wpa_printf(MSG_DEBUG, "GTK KeyID=%u tx=%u",
  270. id, !!(ie.gtk[0] & 0x04));
  271. if ((ie.gtk[0] & 0xf8) || ie.gtk[1])
  272. wpa_printf(MSG_INFO, "GTK KDE: Reserved field "
  273. "set: %02x %02x",
  274. ie.gtk[0], ie.gtk[1]);
  275. wpa_hexdump(MSG_DEBUG, "GTK", ie.gtk + 2,
  276. ie.gtk_len - 2);
  277. bss->gtk_len[id] = ie.gtk_len - 2;
  278. os_memcpy(bss->gtk[id], ie.gtk + 2, ie.gtk_len - 2);
  279. bss->rsc[id][0] = rsc[5];
  280. bss->rsc[id][1] = rsc[4];
  281. bss->rsc[id][2] = rsc[3];
  282. bss->rsc[id][3] = rsc[2];
  283. bss->rsc[id][4] = rsc[1];
  284. bss->rsc[id][5] = rsc[0];
  285. wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
  286. } else {
  287. wpa_printf(MSG_INFO, "Invalid GTK KDE length %u",
  288. (unsigned) ie.gtk_len);
  289. }
  290. }
  291. if (ie.igtk) {
  292. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - IGTK KDE",
  293. ie.igtk, ie.igtk_len);
  294. if (ie.igtk_len == 24) {
  295. u16 id;
  296. id = WPA_GET_LE16(ie.igtk);
  297. if (id > 5) {
  298. wpa_printf(MSG_INFO, "Unexpected IGTK KeyID "
  299. "%u", id);
  300. } else {
  301. wpa_printf(MSG_DEBUG, "IGTK KeyID %u", id);
  302. wpa_hexdump(MSG_DEBUG, "IPN", ie.igtk + 2, 6);
  303. wpa_hexdump(MSG_DEBUG, "IGTK", ie.igtk + 8,
  304. 16);
  305. os_memcpy(bss->igtk[id], ie.igtk + 8, 16);
  306. bss->igtk_set[id] = 1;
  307. }
  308. } else {
  309. wpa_printf(MSG_INFO, "Invalid IGTK KDE length %u",
  310. (unsigned) ie.igtk_len);
  311. }
  312. }
  313. }
  314. static void rx_data_eapol_key_3_of_4(struct wlantest *wt, const u8 *dst,
  315. const u8 *src, const u8 *data, size_t len)
  316. {
  317. struct wlantest_bss *bss;
  318. struct wlantest_sta *sta;
  319. const struct ieee802_1x_hdr *eapol;
  320. const struct wpa_eapol_key *hdr;
  321. const u8 *key_data;
  322. int recalc = 0;
  323. u16 key_info, ver;
  324. u8 *decrypted;
  325. size_t decrypted_len = 0;
  326. wpa_printf(MSG_DEBUG, "EAPOL-Key 3/4 " MACSTR " -> " MACSTR,
  327. MAC2STR(src), MAC2STR(dst));
  328. bss = bss_get(wt, src);
  329. if (bss == NULL)
  330. return;
  331. sta = sta_get(bss, dst);
  332. if (sta == NULL)
  333. return;
  334. eapol = (const struct ieee802_1x_hdr *) data;
  335. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  336. key_info = WPA_GET_BE16(hdr->key_info);
  337. if (os_memcmp(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN) != 0) {
  338. wpa_printf(MSG_INFO, "EAPOL-Key ANonce mismatch between 1/4 "
  339. "and 3/4");
  340. recalc = 1;
  341. }
  342. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  343. if (recalc) {
  344. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK,
  345. data, len);
  346. }
  347. if (!sta->ptk_set) {
  348. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 3/4");
  349. return;
  350. }
  351. if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  352. data, len) < 0) {
  353. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 3/4 MIC");
  354. return;
  355. }
  356. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 3/4");
  357. key_data = (const u8 *) (hdr + 1);
  358. /* TODO: handle WPA without EncrKeyData bit */
  359. if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  360. wpa_printf(MSG_INFO, "EAPOL-Key 3/4 without EncrKeyData bit");
  361. return;
  362. }
  363. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  364. decrypted = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
  365. &decrypted_len);
  366. if (decrypted == NULL) {
  367. wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
  368. return;
  369. }
  370. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  371. decrypted, decrypted_len);
  372. learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
  373. os_free(decrypted);
  374. }
  375. static void rx_data_eapol_key_4_of_4(struct wlantest *wt, const u8 *dst,
  376. const u8 *src, const u8 *data, size_t len)
  377. {
  378. struct wlantest_bss *bss;
  379. struct wlantest_sta *sta;
  380. const struct ieee802_1x_hdr *eapol;
  381. const struct wpa_eapol_key *hdr;
  382. u16 key_info;
  383. wpa_printf(MSG_DEBUG, "EAPOL-Key 4/4 " MACSTR " -> " MACSTR,
  384. MAC2STR(src), MAC2STR(dst));
  385. bss = bss_get(wt, dst);
  386. if (bss == NULL)
  387. return;
  388. sta = sta_get(bss, src);
  389. if (sta == NULL)
  390. return;
  391. eapol = (const struct ieee802_1x_hdr *) data;
  392. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  393. key_info = WPA_GET_BE16(hdr->key_info);
  394. if (!sta->ptk_set) {
  395. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 4/4");
  396. return;
  397. }
  398. if (sta->ptk_set &&
  399. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  400. data, len) < 0) {
  401. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 4/4 MIC");
  402. return;
  403. }
  404. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 4/4");
  405. }
  406. static void rx_data_eapol_key_1_of_2(struct wlantest *wt, const u8 *dst,
  407. const u8 *src, const u8 *data, size_t len)
  408. {
  409. struct wlantest_bss *bss;
  410. struct wlantest_sta *sta;
  411. const struct ieee802_1x_hdr *eapol;
  412. const struct wpa_eapol_key *hdr;
  413. const u8 *key_data;
  414. u16 key_info, ver;
  415. u8 *decrypted;
  416. size_t decrypted_len = 0;
  417. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/2 " MACSTR " -> " MACSTR,
  418. MAC2STR(src), MAC2STR(dst));
  419. bss = bss_get(wt, src);
  420. if (bss == NULL)
  421. return;
  422. sta = sta_get(bss, dst);
  423. if (sta == NULL)
  424. return;
  425. eapol = (const struct ieee802_1x_hdr *) data;
  426. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  427. key_info = WPA_GET_BE16(hdr->key_info);
  428. if (!sta->ptk_set) {
  429. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 1/2");
  430. return;
  431. }
  432. if (sta->ptk_set &&
  433. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  434. data, len) < 0) {
  435. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 1/2 MIC");
  436. return;
  437. }
  438. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 1/2");
  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 1/2 without EncrKeyData bit");
  443. return;
  444. }
  445. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  446. decrypted = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
  447. &decrypted_len);
  448. if (decrypted == NULL) {
  449. wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
  450. return;
  451. }
  452. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  453. decrypted, decrypted_len);
  454. learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
  455. os_free(decrypted);
  456. }
  457. static void rx_data_eapol_key_2_of_2(struct wlantest *wt, const u8 *dst,
  458. const u8 *src, const u8 *data, size_t len)
  459. {
  460. struct wlantest_bss *bss;
  461. struct wlantest_sta *sta;
  462. const struct ieee802_1x_hdr *eapol;
  463. const struct wpa_eapol_key *hdr;
  464. u16 key_info;
  465. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/2 " MACSTR " -> " MACSTR,
  466. MAC2STR(src), MAC2STR(dst));
  467. bss = bss_get(wt, dst);
  468. if (bss == NULL)
  469. return;
  470. sta = sta_get(bss, src);
  471. if (sta == NULL)
  472. return;
  473. eapol = (const struct ieee802_1x_hdr *) data;
  474. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  475. key_info = WPA_GET_BE16(hdr->key_info);
  476. if (!sta->ptk_set) {
  477. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 2/2");
  478. return;
  479. }
  480. if (sta->ptk_set &&
  481. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  482. data, len) < 0) {
  483. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 2/2 MIC");
  484. return;
  485. }
  486. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/2");
  487. }
  488. static void rx_data_eapol_key(struct wlantest *wt, const u8 *dst,
  489. const u8 *src, const u8 *data, size_t len,
  490. int prot)
  491. {
  492. const struct ieee802_1x_hdr *eapol;
  493. const struct wpa_eapol_key *hdr;
  494. const u8 *key_data;
  495. u16 key_info, key_length, ver, key_data_length;
  496. eapol = (const struct ieee802_1x_hdr *) data;
  497. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  498. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key",
  499. (const u8 *) hdr, len - sizeof(*eapol));
  500. if (len < sizeof(*hdr)) {
  501. wpa_printf(MSG_INFO, "Too short EAPOL-Key frame from " MACSTR,
  502. MAC2STR(src));
  503. return;
  504. }
  505. if (hdr->type == EAPOL_KEY_TYPE_RC4) {
  506. /* TODO: EAPOL-Key RC4 for WEP */
  507. return;
  508. }
  509. if (hdr->type != EAPOL_KEY_TYPE_RSN &&
  510. hdr->type != EAPOL_KEY_TYPE_WPA) {
  511. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key type %u",
  512. hdr->type);
  513. return;
  514. }
  515. key_info = WPA_GET_BE16(hdr->key_info);
  516. key_length = WPA_GET_BE16(hdr->key_length);
  517. key_data_length = WPA_GET_BE16(hdr->key_data_length);
  518. key_data = (const u8 *) (hdr + 1);
  519. if (key_data + key_data_length > data + len) {
  520. wpa_printf(MSG_INFO, "Truncated EAPOL-Key from " MACSTR,
  521. MAC2STR(src));
  522. return;
  523. }
  524. if (key_data + key_data_length < data + len) {
  525. wpa_hexdump(MSG_DEBUG, "Extra data after EAPOL-Key Key Data "
  526. "field", key_data + key_data_length,
  527. data + len - key_data - key_data_length);
  528. }
  529. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  530. wpa_printf(MSG_DEBUG, "EAPOL-Key ver=%u %c idx=%u%s%s%s%s%s%s%s%s "
  531. "datalen=%u",
  532. ver, key_info & WPA_KEY_INFO_KEY_TYPE ? 'P' : 'G',
  533. (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  534. WPA_KEY_INFO_KEY_INDEX_SHIFT,
  535. (key_info & WPA_KEY_INFO_INSTALL) ? " Install" : "",
  536. (key_info & WPA_KEY_INFO_ACK) ? " ACK" : "",
  537. (key_info & WPA_KEY_INFO_MIC) ? " MIC" : "",
  538. (key_info & WPA_KEY_INFO_SECURE) ? " Secure" : "",
  539. (key_info & WPA_KEY_INFO_ERROR) ? " Error" : "",
  540. (key_info & WPA_KEY_INFO_REQUEST) ? " Request" : "",
  541. (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) ? " Encr" : "",
  542. (key_info & WPA_KEY_INFO_SMK_MESSAGE) ? " SMK" : "",
  543. key_data_length);
  544. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  545. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
  546. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  547. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key Key Descriptor "
  548. "Version %u", ver);
  549. return;
  550. }
  551. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Replay Counter",
  552. hdr->replay_counter, WPA_REPLAY_COUNTER_LEN);
  553. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Nonce",
  554. hdr->key_nonce, WPA_NONCE_LEN);
  555. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key IV",
  556. hdr->key_iv, 16);
  557. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key RSC",
  558. hdr->key_rsc, WPA_KEY_RSC_LEN);
  559. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key MIC",
  560. hdr->key_mic, 16);
  561. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data",
  562. key_data, key_data_length);
  563. if (key_info & (WPA_KEY_INFO_ERROR | WPA_KEY_INFO_REQUEST))
  564. return;
  565. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  566. return;
  567. if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  568. /* 4-Way Handshake */
  569. switch (key_info & (WPA_KEY_INFO_SECURE |
  570. WPA_KEY_INFO_MIC |
  571. WPA_KEY_INFO_ACK |
  572. WPA_KEY_INFO_INSTALL)) {
  573. case WPA_KEY_INFO_ACK:
  574. rx_data_eapol_key_1_of_4(wt, dst, src, data, len);
  575. break;
  576. case WPA_KEY_INFO_MIC:
  577. rx_data_eapol_key_2_of_4(wt, dst, src, data, len);
  578. break;
  579. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  580. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL:
  581. rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
  582. break;
  583. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  584. rx_data_eapol_key_4_of_4(wt, dst, src, data, len);
  585. break;
  586. default:
  587. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  588. break;
  589. }
  590. } else {
  591. /* Group Key Handshake */
  592. switch (key_info & (WPA_KEY_INFO_SECURE |
  593. WPA_KEY_INFO_MIC |
  594. WPA_KEY_INFO_ACK)) {
  595. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  596. WPA_KEY_INFO_ACK:
  597. rx_data_eapol_key_1_of_2(wt, dst, src, data, len);
  598. break;
  599. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  600. rx_data_eapol_key_2_of_2(wt, dst, src, data, len);
  601. break;
  602. default:
  603. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  604. break;
  605. }
  606. }
  607. }
  608. static void rx_data_eapol(struct wlantest *wt, const u8 *dst, const u8 *src,
  609. const u8 *data, size_t len, int prot)
  610. {
  611. const struct ieee802_1x_hdr *hdr;
  612. u16 length;
  613. const u8 *p;
  614. wpa_hexdump(MSG_EXCESSIVE, "EAPOL", data, len);
  615. if (len < sizeof(*hdr)) {
  616. wpa_printf(MSG_INFO, "Too short EAPOL frame from " MACSTR,
  617. MAC2STR(src));
  618. return;
  619. }
  620. hdr = (const struct ieee802_1x_hdr *) data;
  621. length = be_to_host16(hdr->length);
  622. wpa_printf(MSG_DEBUG, "RX EAPOL: " MACSTR " -> " MACSTR "%s ver=%u "
  623. "type=%u len=%u",
  624. MAC2STR(src), MAC2STR(dst), prot ? " Prot" : "",
  625. hdr->version, hdr->type, length);
  626. if (sizeof(*hdr) + length > len) {
  627. wpa_printf(MSG_INFO, "Truncated EAPOL frame from " MACSTR,
  628. MAC2STR(src));
  629. return;
  630. }
  631. if (sizeof(*hdr) + length < len) {
  632. wpa_printf(MSG_INFO, "EAPOL frame with %d extra bytes",
  633. (int) (len - sizeof(*hdr) - length));
  634. }
  635. p = (const u8 *) (hdr + 1);
  636. switch (hdr->type) {
  637. case IEEE802_1X_TYPE_EAP_PACKET:
  638. wpa_hexdump(MSG_MSGDUMP, "EAPOL - EAP packet", p, length);
  639. break;
  640. case IEEE802_1X_TYPE_EAPOL_START:
  641. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Start", p, length);
  642. break;
  643. case IEEE802_1X_TYPE_EAPOL_LOGOFF:
  644. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Logoff", p, length);
  645. break;
  646. case IEEE802_1X_TYPE_EAPOL_KEY:
  647. rx_data_eapol_key(wt, dst, src, data, sizeof(*hdr) + length,
  648. prot);
  649. break;
  650. case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
  651. wpa_hexdump(MSG_MSGDUMP, "EAPOL - Encapsulated ASF alert",
  652. p, length);
  653. break;
  654. default:
  655. wpa_hexdump(MSG_MSGDUMP, "Unknown EAPOL payload", p, length);
  656. break;
  657. }
  658. }
  659. static void rx_data_eth(struct wlantest *wt, const u8 *dst, const u8 *src,
  660. u16 ethertype, const u8 *data, size_t len, int prot)
  661. {
  662. if (ethertype == ETH_P_PAE)
  663. rx_data_eapol(wt, dst, src, data, len, prot);
  664. }
  665. static void rx_data_process(struct wlantest *wt, const u8 *dst, const u8 *src,
  666. const u8 *data, size_t len, int prot)
  667. {
  668. if (len == 0)
  669. return;
  670. if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
  671. rx_data_eth(wt, dst, src, WPA_GET_BE16(data + 6),
  672. data + 8, len - 8, prot);
  673. return;
  674. }
  675. wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
  676. }
  677. static void rx_data_bss_prot_group(struct wlantest *wt,
  678. const struct ieee80211_hdr *hdr,
  679. const u8 *qos, const u8 *dst, const u8 *src,
  680. const u8 *data, size_t len)
  681. {
  682. struct wlantest_bss *bss;
  683. int keyid;
  684. u8 *decrypted;
  685. size_t dlen;
  686. u8 pn[6];
  687. bss = bss_get(wt, hdr->addr2);
  688. if (bss == NULL)
  689. return;
  690. if (len < 4) {
  691. wpa_printf(MSG_INFO, "Too short group addressed data frame");
  692. return;
  693. }
  694. keyid = data[3] >> 6;
  695. if (bss->gtk_len[keyid] == 0) {
  696. wpa_printf(MSG_MSGDUMP, "No GTK known to decrypt the frame "
  697. "(A2=" MACSTR " KeyID=%d)",
  698. MAC2STR(hdr->addr2), keyid);
  699. return;
  700. }
  701. ccmp_get_pn(pn, data);
  702. if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
  703. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  704. MAC2STR(hdr->addr2));
  705. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  706. wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
  707. }
  708. /* TODO: TKIP */
  709. decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len, &dlen);
  710. if (decrypted) {
  711. rx_data_process(wt, dst, src, decrypted, dlen, 1);
  712. os_memcpy(bss->rsc[keyid], pn, 6);
  713. }
  714. os_free(decrypted);
  715. }
  716. static void rx_data_bss_prot(struct wlantest *wt,
  717. const struct ieee80211_hdr *hdr, const u8 *qos,
  718. const u8 *dst, const u8 *src, const u8 *data,
  719. size_t len)
  720. {
  721. struct wlantest_bss *bss;
  722. struct wlantest_sta *sta;
  723. int keyid;
  724. u16 fc = le_to_host16(hdr->frame_control);
  725. u8 *decrypted;
  726. size_t dlen;
  727. int tid;
  728. u8 pn[6], *rsc;
  729. if (hdr->addr1[0] & 0x01) {
  730. rx_data_bss_prot_group(wt, hdr, qos, dst, src, data, len);
  731. return;
  732. }
  733. if (fc & WLAN_FC_TODS) {
  734. bss = bss_get(wt, hdr->addr1);
  735. if (bss == NULL)
  736. return;
  737. sta = sta_get(bss, hdr->addr2);
  738. } else {
  739. bss = bss_get(wt, hdr->addr2);
  740. if (bss == NULL)
  741. return;
  742. sta = sta_get(bss, hdr->addr1);
  743. }
  744. if (sta == NULL || !sta->ptk_set) {
  745. wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
  746. return;
  747. }
  748. if (len < 4) {
  749. wpa_printf(MSG_INFO, "Too short encrypted data frame");
  750. return;
  751. }
  752. keyid = data[3] >> 6;
  753. if (keyid != 0) {
  754. wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
  755. "individually addressed Data frame from " MACSTR,
  756. keyid, MAC2STR(hdr->addr2));
  757. }
  758. if (qos)
  759. tid = qos[0] & 0x0f;
  760. else
  761. tid = 0;
  762. if (fc & WLAN_FC_TODS)
  763. rsc = sta->rsc_tods[tid];
  764. else
  765. rsc = sta->rsc_fromds[tid];
  766. ccmp_get_pn(pn, data);
  767. if (os_memcmp(pn, rsc, 6) <= 0) {
  768. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  769. MAC2STR(hdr->addr2));
  770. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  771. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  772. }
  773. /* TODO: TKIP */
  774. decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  775. if (decrypted) {
  776. rx_data_process(wt, dst, src, decrypted, dlen, 1);
  777. os_memcpy(rsc, pn, 6);
  778. }
  779. os_free(decrypted);
  780. }
  781. static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  782. const u8 *qos, const u8 *dst, const u8 *src,
  783. const u8 *data, size_t len)
  784. {
  785. u16 fc = le_to_host16(hdr->frame_control);
  786. int prot = !!(fc & WLAN_FC_ISWEP);
  787. if (qos) {
  788. u8 ack = (qos[0] & 0x60) >> 5;
  789. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  790. " len=%u%s tid=%u%s%s",
  791. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  792. prot ? " Prot" : "", qos[0] & 0x0f,
  793. (qos[0] & 0x10) ? " EOSP" : "",
  794. ack == 0 ? "" :
  795. (ack == 1 ? " NoAck" :
  796. (ack == 2 ? " NoExpAck" : " BA")));
  797. } else {
  798. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  799. " len=%u%s",
  800. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  801. prot ? " Prot" : "");
  802. }
  803. if (prot)
  804. rx_data_bss_prot(wt, hdr, qos, dst, src, data, len);
  805. else
  806. rx_data_process(wt, dst, src, data, len, 0);
  807. }
  808. void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  809. {
  810. const struct ieee80211_hdr *hdr;
  811. u16 fc, stype;
  812. size_t hdrlen;
  813. const u8 *qos = NULL;
  814. if (len < 24)
  815. return;
  816. hdr = (const struct ieee80211_hdr *) data;
  817. fc = le_to_host16(hdr->frame_control);
  818. stype = WLAN_FC_GET_STYPE(fc);
  819. hdrlen = 24;
  820. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  821. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  822. hdrlen += ETH_ALEN;
  823. if (stype & 0x08) {
  824. qos = data + hdrlen;
  825. hdrlen += 2;
  826. }
  827. if (len < hdrlen)
  828. return;
  829. wt->rx_data++;
  830. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  831. case 0:
  832. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  833. MACSTR " BSSID=" MACSTR,
  834. data_stype(WLAN_FC_GET_STYPE(fc)),
  835. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  836. fc & WLAN_FC_ISWEP ? " Prot" : "",
  837. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  838. MAC2STR(hdr->addr3));
  839. break;
  840. case WLAN_FC_FROMDS:
  841. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  842. " BSSID=" MACSTR " SA=" MACSTR,
  843. data_stype(WLAN_FC_GET_STYPE(fc)),
  844. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  845. fc & WLAN_FC_ISWEP ? " Prot" : "",
  846. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  847. MAC2STR(hdr->addr3));
  848. rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
  849. data + hdrlen, len - hdrlen);
  850. break;
  851. case WLAN_FC_TODS:
  852. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  853. " SA=" MACSTR " DA=" MACSTR,
  854. data_stype(WLAN_FC_GET_STYPE(fc)),
  855. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  856. fc & WLAN_FC_ISWEP ? " Prot" : "",
  857. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  858. MAC2STR(hdr->addr3));
  859. rx_data_bss(wt, hdr, qos, hdr->addr3, hdr->addr2,
  860. data + hdrlen, len - hdrlen);
  861. break;
  862. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  863. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  864. MACSTR " DA=" MACSTR " SA=" MACSTR,
  865. data_stype(WLAN_FC_GET_STYPE(fc)),
  866. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  867. fc & WLAN_FC_ISWEP ? " Prot" : "",
  868. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  869. MAC2STR(hdr->addr3),
  870. MAC2STR((const u8 *) (hdr + 1)));
  871. break;
  872. }
  873. }