rx_data.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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,
  111. data, len) < 0)
  112. return -1;
  113. wpa_printf(MSG_INFO, "Derived PTK for STA " MACSTR " BSSID " MACSTR
  114. ")", MAC2STR(sta->addr), MAC2STR(bss->bssid));
  115. os_memcpy(&sta->ptk, &ptk, sizeof(ptk));
  116. sta->ptk_set = 1;
  117. return 0;
  118. }
  119. static void derive_ptk(struct wlantest *wt, struct wlantest_bss *bss,
  120. struct wlantest_sta *sta, u16 ver,
  121. const u8 *data, size_t len)
  122. {
  123. struct wlantest_pmk *pmk;
  124. dl_list_for_each(pmk, &bss->pmk, struct wlantest_pmk, list) {
  125. if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
  126. return;
  127. }
  128. dl_list_for_each(pmk, &wt->pmk, struct wlantest_pmk, list) {
  129. if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
  130. return;
  131. }
  132. }
  133. static void rx_data_eapol_key_2_of_4(struct wlantest *wt, const u8 *dst,
  134. const u8 *src, const u8 *data, size_t len)
  135. {
  136. struct wlantest_bss *bss;
  137. struct wlantest_sta *sta;
  138. const struct ieee802_1x_hdr *eapol;
  139. const struct wpa_eapol_key *hdr;
  140. u16 key_info;
  141. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/4 " MACSTR " -> " MACSTR,
  142. MAC2STR(src), MAC2STR(dst));
  143. bss = bss_get(wt, dst);
  144. if (bss == NULL)
  145. return;
  146. sta = sta_get(bss, src);
  147. if (sta == NULL)
  148. return;
  149. eapol = (const struct ieee802_1x_hdr *) data;
  150. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  151. os_memcpy(sta->snonce, hdr->key_nonce, WPA_NONCE_LEN);
  152. key_info = WPA_GET_BE16(hdr->key_info);
  153. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK, data, len);
  154. }
  155. static u8 * decrypt_eapol_key_data_rc4(const u8 *kek,
  156. const struct wpa_eapol_key *hdr,
  157. size_t *len)
  158. {
  159. u8 ek[32], *buf;
  160. u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
  161. buf = os_malloc(keydatalen);
  162. if (buf == NULL)
  163. return NULL;
  164. os_memcpy(ek, hdr->key_iv, 16);
  165. os_memcpy(ek + 16, kek, 16);
  166. os_memcpy(buf, hdr + 1, keydatalen);
  167. if (rc4_skip(ek, 32, 256, buf, keydatalen)) {
  168. wpa_printf(MSG_INFO, "RC4 failed");
  169. os_free(buf);
  170. return NULL;
  171. }
  172. *len = keydatalen;
  173. return buf;
  174. }
  175. static u8 * decrypt_eapol_key_data_aes(const u8 *kek,
  176. const struct wpa_eapol_key *hdr,
  177. size_t *len)
  178. {
  179. u8 *buf;
  180. u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
  181. if (keydatalen % 8) {
  182. wpa_printf(MSG_INFO, "Unsupported AES-WRAP len %d",
  183. keydatalen);
  184. return NULL;
  185. }
  186. keydatalen -= 8; /* AES-WRAP adds 8 bytes */
  187. buf = os_malloc(keydatalen);
  188. if (buf == NULL)
  189. return NULL;
  190. if (aes_unwrap(kek, keydatalen / 8, (u8 *) (hdr + 1), buf)) {
  191. os_free(buf);
  192. wpa_printf(MSG_INFO, "AES unwrap failed - "
  193. "could not decrypt EAPOL-Key key data");
  194. return NULL;
  195. }
  196. *len = keydatalen;
  197. return buf;
  198. }
  199. static u8 * decrypt_eapol_key_data(const u8 *kek, u16 ver,
  200. const struct wpa_eapol_key *hdr,
  201. size_t *len)
  202. {
  203. switch (ver) {
  204. case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
  205. return decrypt_eapol_key_data_rc4(kek, hdr, len);
  206. case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
  207. case WPA_KEY_INFO_TYPE_AES_128_CMAC:
  208. return decrypt_eapol_key_data_aes(kek, hdr, len);
  209. default:
  210. wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Key Descriptor "
  211. "Version %u", ver);
  212. return NULL;
  213. }
  214. }
  215. static void learn_kde_keys(struct wlantest_bss *bss, u8 *buf, size_t len)
  216. {
  217. struct wpa_eapol_ie_parse ie;
  218. if (wpa_supplicant_parse_ies(buf, len, &ie) < 0) {
  219. wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  220. return;
  221. }
  222. if (ie.wpa_ie) {
  223. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
  224. ie.wpa_ie, ie.wpa_ie_len);
  225. }
  226. if (ie.rsn_ie) {
  227. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
  228. ie.rsn_ie, ie.rsn_ie_len);
  229. }
  230. if (ie.gtk) {
  231. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - GTK KDE",
  232. ie.gtk, ie.gtk_len);
  233. if (ie.gtk_len >= 2 && ie.gtk_len <= 2 + 32) {
  234. int id;
  235. id = ie.gtk[0] & 0x03;
  236. wpa_printf(MSG_INFO, "GTK KeyID=%u tx=%u",
  237. id, !!(ie.gtk[0] & 0x04));
  238. if ((ie.gtk[0] & 0xf8) || ie.gtk[1])
  239. wpa_printf(MSG_INFO, "GTK KDE: Reserved field "
  240. "set: %02x %02x",
  241. ie.gtk[0], ie.gtk[1]);
  242. wpa_hexdump(MSG_DEBUG, "GTK", ie.gtk + 2,
  243. ie.gtk_len - 2);
  244. bss->gtk_len[id] = ie.gtk_len - 2;
  245. os_memcpy(bss->gtk[id], ie.gtk + 2, ie.gtk_len - 2);
  246. } else {
  247. wpa_printf(MSG_INFO, "Invalid GTK KDE length %u",
  248. (unsigned) ie.gtk_len);
  249. }
  250. }
  251. if (ie.igtk) {
  252. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - IGTK KDE",
  253. ie.igtk, ie.igtk_len);
  254. if (ie.igtk_len == 24) {
  255. u16 id;
  256. id = WPA_GET_LE16(ie.igtk);
  257. if (id > 5) {
  258. wpa_printf(MSG_INFO, "Unexpected IGTK KeyID "
  259. "%u", id);
  260. } else {
  261. wpa_printf(MSG_INFO, "IGTK KeyID %u", id);
  262. wpa_hexdump(MSG_INFO, "IPN", ie.igtk + 2, 6);
  263. wpa_hexdump(MSG_INFO, "IGTK", ie.igtk + 8, 16);
  264. os_memcpy(bss->igtk[id], ie.igtk + 8, 16);
  265. bss->igtk_set[id] = 1;
  266. }
  267. } else {
  268. wpa_printf(MSG_INFO, "Invalid IGTK KDE length %u",
  269. (unsigned) ie.igtk_len);
  270. }
  271. }
  272. }
  273. static void rx_data_eapol_key_3_of_4(struct wlantest *wt, const u8 *dst,
  274. const u8 *src, const u8 *data, size_t len)
  275. {
  276. struct wlantest_bss *bss;
  277. struct wlantest_sta *sta;
  278. const struct ieee802_1x_hdr *eapol;
  279. const struct wpa_eapol_key *hdr;
  280. const u8 *key_data;
  281. int recalc = 0;
  282. u16 key_info, ver, key_data_len;
  283. u8 *decrypted;
  284. size_t decrypted_len = 0;
  285. wpa_printf(MSG_DEBUG, "EAPOL-Key 3/4 " MACSTR " -> " MACSTR,
  286. MAC2STR(src), MAC2STR(dst));
  287. bss = bss_get(wt, src);
  288. if (bss == NULL)
  289. return;
  290. sta = sta_get(bss, dst);
  291. if (sta == NULL)
  292. return;
  293. eapol = (const struct ieee802_1x_hdr *) data;
  294. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  295. key_info = WPA_GET_BE16(hdr->key_info);
  296. key_data_len = WPA_GET_BE16(hdr->key_data_length);
  297. if (os_memcmp(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN) != 0) {
  298. wpa_printf(MSG_INFO, "EAPOL-Key ANonce mismatch between 1/4 "
  299. "and 3/4");
  300. recalc = 1;
  301. }
  302. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  303. if (recalc) {
  304. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK,
  305. data, len);
  306. }
  307. if (!sta->ptk_set) {
  308. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 3/4");
  309. return;
  310. }
  311. if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  312. data, len) < 0) {
  313. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 3/4 MIC");
  314. return;
  315. }
  316. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 3/4");
  317. key_data = (const u8 *) (hdr + 1);
  318. /* TODO: handle WPA without EncrKeyData bit */
  319. if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  320. wpa_printf(MSG_INFO, "EAPOL-Key 3/4 without EncrKeyData bit");
  321. return;
  322. }
  323. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  324. decrypted = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
  325. &decrypted_len);
  326. if (decrypted == NULL) {
  327. wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
  328. return;
  329. }
  330. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  331. decrypted, decrypted_len);
  332. learn_kde_keys(bss, decrypted, decrypted_len);
  333. os_free(decrypted);
  334. }
  335. static void rx_data_eapol_key_4_of_4(struct wlantest *wt, const u8 *dst,
  336. const u8 *src, const u8 *data, size_t len)
  337. {
  338. struct wlantest_bss *bss;
  339. struct wlantest_sta *sta;
  340. const struct ieee802_1x_hdr *eapol;
  341. const struct wpa_eapol_key *hdr;
  342. u16 key_info;
  343. wpa_printf(MSG_DEBUG, "EAPOL-Key 4/4 " MACSTR " -> " MACSTR,
  344. MAC2STR(src), MAC2STR(dst));
  345. bss = bss_get(wt, dst);
  346. if (bss == NULL)
  347. return;
  348. sta = sta_get(bss, src);
  349. if (sta == NULL)
  350. return;
  351. eapol = (const struct ieee802_1x_hdr *) data;
  352. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  353. key_info = WPA_GET_BE16(hdr->key_info);
  354. if (!sta->ptk_set) {
  355. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 4/4");
  356. return;
  357. }
  358. if (sta->ptk_set &&
  359. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  360. data, len) < 0) {
  361. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 4/4 MIC");
  362. return;
  363. }
  364. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 4/4");
  365. }
  366. static void rx_data_eapol_key_1_of_2(struct wlantest *wt, const u8 *dst,
  367. const u8 *src, const u8 *data, size_t len)
  368. {
  369. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/2 " MACSTR " -> " MACSTR,
  370. MAC2STR(src), MAC2STR(dst));
  371. }
  372. static void rx_data_eapol_key_2_of_2(struct wlantest *wt, const u8 *dst,
  373. const u8 *src, const u8 *data, size_t len)
  374. {
  375. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/2 " MACSTR " -> " MACSTR,
  376. MAC2STR(src), MAC2STR(dst));
  377. }
  378. static void rx_data_eapol_key(struct wlantest *wt, const u8 *dst,
  379. const u8 *src, const u8 *data, size_t len,
  380. int prot)
  381. {
  382. const struct ieee802_1x_hdr *eapol;
  383. const struct wpa_eapol_key *hdr;
  384. const u8 *key_data;
  385. u16 key_info, key_length, ver, key_data_length;
  386. eapol = (const struct ieee802_1x_hdr *) data;
  387. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  388. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key",
  389. (const u8 *) hdr, len - sizeof(*eapol));
  390. if (len < sizeof(*hdr)) {
  391. wpa_printf(MSG_INFO, "Too short EAPOL-Key frame from " MACSTR,
  392. MAC2STR(src));
  393. return;
  394. }
  395. if (hdr->type == EAPOL_KEY_TYPE_RC4) {
  396. /* TODO: EAPOL-Key RC4 for WEP */
  397. return;
  398. }
  399. if (hdr->type != EAPOL_KEY_TYPE_RSN &&
  400. hdr->type != EAPOL_KEY_TYPE_WPA) {
  401. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key type %u",
  402. hdr->type);
  403. return;
  404. }
  405. key_info = WPA_GET_BE16(hdr->key_info);
  406. key_length = WPA_GET_BE16(hdr->key_length);
  407. key_data_length = WPA_GET_BE16(hdr->key_data_length);
  408. key_data = (const u8 *) (hdr + 1);
  409. if (key_data + key_data_length > data + len) {
  410. wpa_printf(MSG_INFO, "Truncated EAPOL-Key from " MACSTR,
  411. MAC2STR(src));
  412. return;
  413. }
  414. if (key_data + key_data_length < data + len) {
  415. wpa_hexdump(MSG_DEBUG, "Extra data after EAPOL-Key Key Data "
  416. "field", key_data + key_data_length,
  417. data + len - key_data - key_data_length);
  418. }
  419. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  420. wpa_printf(MSG_DEBUG, "EAPOL-Key ver=%u %c idx=%u%s%s%s%s%s%s%s%s "
  421. "datalen=%u",
  422. ver, key_info & WPA_KEY_INFO_KEY_TYPE ? 'P' : 'G',
  423. (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  424. WPA_KEY_INFO_KEY_INDEX_SHIFT,
  425. (key_info & WPA_KEY_INFO_INSTALL) ? " Install" : "",
  426. (key_info & WPA_KEY_INFO_ACK) ? " ACK" : "",
  427. (key_info & WPA_KEY_INFO_MIC) ? " MIC" : "",
  428. (key_info & WPA_KEY_INFO_SECURE) ? " Secure" : "",
  429. (key_info & WPA_KEY_INFO_ERROR) ? " Error" : "",
  430. (key_info & WPA_KEY_INFO_REQUEST) ? " Request" : "",
  431. (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) ? " Encr" : "",
  432. (key_info & WPA_KEY_INFO_SMK_MESSAGE) ? " SMK" : "",
  433. key_data_length);
  434. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  435. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
  436. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  437. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key Key Descriptor "
  438. "Version %u", ver);
  439. return;
  440. }
  441. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Replay Counter",
  442. hdr->replay_counter, WPA_REPLAY_COUNTER_LEN);
  443. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Nonce",
  444. hdr->key_nonce, WPA_NONCE_LEN);
  445. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key IV",
  446. hdr->key_iv, 16);
  447. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key RSC",
  448. hdr->key_nonce, WPA_KEY_RSC_LEN);
  449. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key MIC",
  450. hdr->key_mic, 16);
  451. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data",
  452. key_data, key_data_length);
  453. if (key_info & (WPA_KEY_INFO_ERROR | WPA_KEY_INFO_REQUEST))
  454. return;
  455. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  456. return;
  457. if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  458. /* 4-Way Handshake */
  459. switch (key_info & (WPA_KEY_INFO_SECURE |
  460. WPA_KEY_INFO_MIC |
  461. WPA_KEY_INFO_ACK |
  462. WPA_KEY_INFO_INSTALL)) {
  463. case WPA_KEY_INFO_ACK:
  464. rx_data_eapol_key_1_of_4(wt, dst, src, data, len);
  465. break;
  466. case WPA_KEY_INFO_MIC:
  467. rx_data_eapol_key_2_of_4(wt, dst, src, data, len);
  468. break;
  469. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  470. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL:
  471. rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
  472. break;
  473. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  474. rx_data_eapol_key_4_of_4(wt, dst, src, data, len);
  475. break;
  476. default:
  477. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  478. break;
  479. }
  480. } else {
  481. /* Group Key Handshake */
  482. switch (key_info & (WPA_KEY_INFO_SECURE |
  483. WPA_KEY_INFO_MIC |
  484. WPA_KEY_INFO_ACK)) {
  485. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  486. WPA_KEY_INFO_ACK:
  487. rx_data_eapol_key_1_of_2(wt, dst, src, data, len);
  488. break;
  489. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  490. rx_data_eapol_key_2_of_2(wt, dst, src, data, len);
  491. break;
  492. default:
  493. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  494. break;
  495. }
  496. }
  497. }
  498. static void rx_data_eapol(struct wlantest *wt, const u8 *dst, const u8 *src,
  499. const u8 *data, size_t len, int prot)
  500. {
  501. const struct ieee802_1x_hdr *hdr;
  502. u16 length;
  503. const u8 *p;
  504. wpa_hexdump(MSG_EXCESSIVE, "EAPOL", data, len);
  505. if (len < sizeof(*hdr)) {
  506. wpa_printf(MSG_INFO, "Too short EAPOL frame from " MACSTR,
  507. MAC2STR(src));
  508. return;
  509. }
  510. hdr = (const struct ieee802_1x_hdr *) data;
  511. length = be_to_host16(hdr->length);
  512. wpa_printf(MSG_DEBUG, "RX EAPOL: " MACSTR " -> " MACSTR "%s ver=%u "
  513. "type=%u len=%u",
  514. MAC2STR(src), MAC2STR(dst), prot ? " Prot" : "",
  515. hdr->version, hdr->type, length);
  516. if (sizeof(*hdr) + length > len) {
  517. wpa_printf(MSG_INFO, "Truncated EAPOL frame from " MACSTR,
  518. MAC2STR(src));
  519. return;
  520. }
  521. if (sizeof(*hdr) + length < len) {
  522. wpa_printf(MSG_INFO, "EAPOL frame with %d extra bytes",
  523. (int) (len - sizeof(*hdr) - length));
  524. }
  525. p = (const u8 *) (hdr + 1);
  526. switch (hdr->type) {
  527. case IEEE802_1X_TYPE_EAP_PACKET:
  528. wpa_hexdump(MSG_MSGDUMP, "EAPOL - EAP packet", p, length);
  529. break;
  530. case IEEE802_1X_TYPE_EAPOL_START:
  531. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Start", p, length);
  532. break;
  533. case IEEE802_1X_TYPE_EAPOL_LOGOFF:
  534. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Logoff", p, length);
  535. break;
  536. case IEEE802_1X_TYPE_EAPOL_KEY:
  537. rx_data_eapol_key(wt, dst, src, data, sizeof(*hdr) + length,
  538. prot);
  539. break;
  540. case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
  541. wpa_hexdump(MSG_MSGDUMP, "EAPOL - Encapsulated ASF alert",
  542. p, length);
  543. break;
  544. default:
  545. wpa_hexdump(MSG_MSGDUMP, "Unknown EAPOL payload", p, length);
  546. break;
  547. }
  548. }
  549. static void rx_data_eth(struct wlantest *wt, const u8 *dst, const u8 *src,
  550. u16 ethertype, const u8 *data, size_t len, int prot)
  551. {
  552. if (ethertype == ETH_P_PAE)
  553. rx_data_eapol(wt, dst, src, data, len, prot);
  554. }
  555. static void rx_data_process(struct wlantest *wt, const u8 *dst, const u8 *src,
  556. const u8 *data, size_t len, int prot)
  557. {
  558. if (len == 0)
  559. return;
  560. if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
  561. rx_data_eth(wt, dst, src, WPA_GET_BE16(data + 6),
  562. data + 8, len - 8, prot);
  563. return;
  564. }
  565. wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
  566. }
  567. static void rx_data_bss_prot(struct wlantest *wt,
  568. const struct ieee80211_hdr *hdr, const u8 *qos,
  569. const u8 *dst, const u8 *src, const u8 *data,
  570. size_t len)
  571. {
  572. /* TODO: Try to decrypt and if success, call rx_data_process() with
  573. * prot = 1 */
  574. }
  575. static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  576. const u8 *qos, const u8 *dst, const u8 *src,
  577. const u8 *data, size_t len)
  578. {
  579. u16 fc = le_to_host16(hdr->frame_control);
  580. int prot = !!(fc & WLAN_FC_ISWEP);
  581. if (qos) {
  582. u8 ack = (qos[0] & 0x60) >> 5;
  583. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  584. " len=%u%s tid=%u%s%s",
  585. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  586. prot ? " Prot" : "", qos[0] & 0x0f,
  587. (qos[0] & 0x10) ? " EOSP" : "",
  588. ack == 0 ? "" :
  589. (ack == 1 ? " NoAck" :
  590. (ack == 2 ? " NoExpAck" : " BA")));
  591. } else {
  592. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  593. " len=%u%s",
  594. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  595. prot ? " Prot" : "");
  596. }
  597. if (prot)
  598. rx_data_bss_prot(wt, hdr, qos, dst, src, data, len);
  599. else
  600. rx_data_process(wt, dst, src, data, len, 0);
  601. }
  602. void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  603. {
  604. const struct ieee80211_hdr *hdr;
  605. u16 fc, stype;
  606. size_t hdrlen;
  607. const u8 *qos = NULL;
  608. if (len < 24)
  609. return;
  610. hdr = (const struct ieee80211_hdr *) data;
  611. fc = le_to_host16(hdr->frame_control);
  612. stype = WLAN_FC_GET_STYPE(fc);
  613. hdrlen = 24;
  614. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  615. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  616. hdrlen += ETH_ALEN;
  617. if (stype & 0x08) {
  618. qos = data + hdrlen;
  619. hdrlen += 2;
  620. }
  621. if (len < hdrlen)
  622. return;
  623. wt->rx_data++;
  624. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  625. case 0:
  626. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  627. MACSTR " BSSID=" MACSTR,
  628. data_stype(WLAN_FC_GET_STYPE(fc)),
  629. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  630. fc & WLAN_FC_ISWEP ? " Prot" : "",
  631. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  632. MAC2STR(hdr->addr3));
  633. break;
  634. case WLAN_FC_FROMDS:
  635. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  636. " BSSID=" MACSTR " SA=" MACSTR,
  637. data_stype(WLAN_FC_GET_STYPE(fc)),
  638. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  639. fc & WLAN_FC_ISWEP ? " Prot" : "",
  640. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  641. MAC2STR(hdr->addr3));
  642. rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
  643. data + hdrlen, len - hdrlen);
  644. break;
  645. case WLAN_FC_TODS:
  646. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  647. " SA=" MACSTR " DA=" MACSTR,
  648. data_stype(WLAN_FC_GET_STYPE(fc)),
  649. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  650. fc & WLAN_FC_ISWEP ? " Prot" : "",
  651. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  652. MAC2STR(hdr->addr3));
  653. rx_data_bss(wt, hdr, qos, hdr->addr3, hdr->addr2,
  654. data + hdrlen, len - hdrlen);
  655. break;
  656. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  657. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  658. MACSTR " DA=" MACSTR " SA=" MACSTR,
  659. data_stype(WLAN_FC_GET_STYPE(fc)),
  660. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  661. fc & WLAN_FC_ISWEP ? " Prot" : "",
  662. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  663. MAC2STR(hdr->addr3),
  664. MAC2STR((const u8 *) (hdr + 1)));
  665. break;
  666. }
  667. }