rx_data.c 37 KB

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