rx_data.c 34 KB

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