rx_data.c 21 KB

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