rx_data.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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. keyid = data[3] >> 6;
  860. if (bss->gtk_len[keyid] == 0) {
  861. wpa_printf(MSG_MSGDUMP, "No GTK known to decrypt the frame "
  862. "(A2=" MACSTR " KeyID=%d)",
  863. MAC2STR(hdr->addr2), keyid);
  864. return;
  865. }
  866. if (bss->group_cipher == WPA_CIPHER_TKIP)
  867. tkip_get_pn(pn, data);
  868. else
  869. ccmp_get_pn(pn, data);
  870. if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
  871. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  872. MAC2STR(hdr->addr2));
  873. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  874. wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
  875. }
  876. if (bss->group_cipher == WPA_CIPHER_TKIP)
  877. decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
  878. &dlen);
  879. else
  880. decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
  881. &dlen);
  882. if (decrypted) {
  883. rx_data_process(wt, dst, src, decrypted, dlen, 1);
  884. os_memcpy(bss->rsc[keyid], pn, 6);
  885. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  886. decrypted, dlen);
  887. }
  888. os_free(decrypted);
  889. }
  890. static void rx_data_bss_prot(struct wlantest *wt,
  891. const struct ieee80211_hdr *hdr, const u8 *qos,
  892. const u8 *dst, const u8 *src, const u8 *data,
  893. size_t len)
  894. {
  895. struct wlantest_bss *bss;
  896. struct wlantest_sta *sta;
  897. int keyid;
  898. u16 fc = le_to_host16(hdr->frame_control);
  899. u8 *decrypted;
  900. size_t dlen;
  901. int tid;
  902. u8 pn[6], *rsc;
  903. if (hdr->addr1[0] & 0x01) {
  904. rx_data_bss_prot_group(wt, hdr, qos, dst, src, data, len);
  905. return;
  906. }
  907. if (fc & WLAN_FC_TODS) {
  908. bss = bss_get(wt, hdr->addr1);
  909. if (bss == NULL)
  910. return;
  911. sta = sta_get(bss, hdr->addr2);
  912. } else {
  913. bss = bss_get(wt, hdr->addr2);
  914. if (bss == NULL)
  915. return;
  916. sta = sta_get(bss, hdr->addr1);
  917. }
  918. if (sta == NULL || !sta->ptk_set) {
  919. wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
  920. return;
  921. }
  922. if (len < 4) {
  923. wpa_printf(MSG_INFO, "Too short encrypted data frame");
  924. return;
  925. }
  926. keyid = data[3] >> 6;
  927. if (keyid != 0) {
  928. wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
  929. "individually addressed Data frame from " MACSTR,
  930. keyid, MAC2STR(hdr->addr2));
  931. }
  932. if (qos)
  933. tid = qos[0] & 0x0f;
  934. else
  935. tid = 0;
  936. if (fc & WLAN_FC_TODS)
  937. rsc = sta->rsc_tods[tid];
  938. else
  939. rsc = sta->rsc_fromds[tid];
  940. if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
  941. tkip_get_pn(pn, data);
  942. else
  943. ccmp_get_pn(pn, data);
  944. if (os_memcmp(pn, rsc, 6) <= 0) {
  945. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  946. MAC2STR(hdr->addr2));
  947. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  948. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  949. }
  950. if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
  951. decrypted = tkip_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  952. else
  953. decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  954. if (decrypted) {
  955. rx_data_process(wt, dst, src, decrypted, dlen, 1);
  956. os_memcpy(rsc, pn, 6);
  957. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  958. decrypted, dlen);
  959. }
  960. os_free(decrypted);
  961. }
  962. static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  963. const u8 *qos, const u8 *dst, const u8 *src,
  964. const u8 *data, size_t len)
  965. {
  966. u16 fc = le_to_host16(hdr->frame_control);
  967. int prot = !!(fc & WLAN_FC_ISWEP);
  968. if (qos) {
  969. u8 ack = (qos[0] & 0x60) >> 5;
  970. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  971. " len=%u%s tid=%u%s%s",
  972. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  973. prot ? " Prot" : "", qos[0] & 0x0f,
  974. (qos[0] & 0x10) ? " EOSP" : "",
  975. ack == 0 ? "" :
  976. (ack == 1 ? " NoAck" :
  977. (ack == 2 ? " NoExpAck" : " BA")));
  978. } else {
  979. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  980. " len=%u%s",
  981. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  982. prot ? " Prot" : "");
  983. }
  984. if (prot)
  985. rx_data_bss_prot(wt, hdr, qos, dst, src, data, len);
  986. else
  987. rx_data_process(wt, dst, src, data, len, 0);
  988. }
  989. void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  990. {
  991. const struct ieee80211_hdr *hdr;
  992. u16 fc, stype;
  993. size_t hdrlen;
  994. const u8 *qos = NULL;
  995. if (len < 24)
  996. return;
  997. hdr = (const struct ieee80211_hdr *) data;
  998. fc = le_to_host16(hdr->frame_control);
  999. stype = WLAN_FC_GET_STYPE(fc);
  1000. hdrlen = 24;
  1001. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  1002. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  1003. hdrlen += ETH_ALEN;
  1004. if (stype & 0x08) {
  1005. qos = data + hdrlen;
  1006. hdrlen += 2;
  1007. }
  1008. if (len < hdrlen)
  1009. return;
  1010. wt->rx_data++;
  1011. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  1012. case 0:
  1013. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  1014. MACSTR " BSSID=" MACSTR,
  1015. data_stype(WLAN_FC_GET_STYPE(fc)),
  1016. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  1017. fc & WLAN_FC_ISWEP ? " Prot" : "",
  1018. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1019. MAC2STR(hdr->addr3));
  1020. break;
  1021. case WLAN_FC_FROMDS:
  1022. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  1023. " BSSID=" MACSTR " SA=" MACSTR,
  1024. data_stype(WLAN_FC_GET_STYPE(fc)),
  1025. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  1026. fc & WLAN_FC_ISWEP ? " Prot" : "",
  1027. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1028. MAC2STR(hdr->addr3));
  1029. rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
  1030. data + hdrlen, len - hdrlen);
  1031. break;
  1032. case WLAN_FC_TODS:
  1033. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  1034. " SA=" MACSTR " DA=" MACSTR,
  1035. data_stype(WLAN_FC_GET_STYPE(fc)),
  1036. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  1037. fc & WLAN_FC_ISWEP ? " Prot" : "",
  1038. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1039. MAC2STR(hdr->addr3));
  1040. rx_data_bss(wt, hdr, qos, hdr->addr3, hdr->addr2,
  1041. data + hdrlen, len - hdrlen);
  1042. break;
  1043. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  1044. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  1045. MACSTR " DA=" MACSTR " SA=" MACSTR,
  1046. data_stype(WLAN_FC_GET_STYPE(fc)),
  1047. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  1048. fc & WLAN_FC_ISWEP ? " Prot" : "",
  1049. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1050. MAC2STR(hdr->addr3),
  1051. MAC2STR((const u8 *) (hdr + 1)));
  1052. break;
  1053. }
  1054. }