rx_data.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 WPA IE between "
  199. "EAPOL-Key 2/4 and (Re)Association "
  200. "Request from " MACSTR, MAC2STR(sta->addr));
  201. wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
  202. ie.rsn_ie, ie.rsn_ie_len);
  203. wpa_hexdump(MSG_INFO, "WPA 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, 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;
  348. size_t decrypted_len = 0;
  349. struct wpa_eapol_ie_parse ie;
  350. wpa_printf(MSG_DEBUG, "EAPOL-Key 3/4 " MACSTR " -> " MACSTR,
  351. MAC2STR(src), MAC2STR(dst));
  352. bss = bss_get(wt, src);
  353. if (bss == NULL)
  354. return;
  355. sta = sta_get(bss, dst);
  356. if (sta == NULL)
  357. return;
  358. eapol = (const struct ieee802_1x_hdr *) data;
  359. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  360. key_info = WPA_GET_BE16(hdr->key_info);
  361. if (os_memcmp(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN) != 0) {
  362. wpa_printf(MSG_INFO, "EAPOL-Key ANonce mismatch between 1/4 "
  363. "and 3/4");
  364. recalc = 1;
  365. }
  366. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  367. if (recalc) {
  368. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK,
  369. data, len);
  370. }
  371. if (!sta->ptk_set) {
  372. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 3/4");
  373. return;
  374. }
  375. if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  376. data, len) < 0) {
  377. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 3/4 MIC");
  378. return;
  379. }
  380. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 3/4");
  381. key_data = (const u8 *) (hdr + 1);
  382. /* TODO: handle WPA without EncrKeyData bit */
  383. if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  384. wpa_printf(MSG_INFO, "EAPOL-Key 3/4 without EncrKeyData bit");
  385. return;
  386. }
  387. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  388. decrypted = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
  389. &decrypted_len);
  390. if (decrypted == NULL) {
  391. wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
  392. return;
  393. }
  394. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  395. decrypted, decrypted_len);
  396. if (wt->write_pcap_dumper) {
  397. /* Fill in a dummy Data frame header */
  398. u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
  399. struct ieee80211_hdr *h;
  400. struct wpa_eapol_key *k;
  401. u8 *pos;
  402. size_t plain_len;
  403. plain_len = decrypted_len;
  404. pos = decrypted;
  405. while (pos + 1 < decrypted + decrypted_len) {
  406. if (pos[0] == 0xdd && pos[1] == 0x00) {
  407. /* Remove padding */
  408. plain_len = pos - decrypted;
  409. break;
  410. }
  411. pos += 2 + pos[1];
  412. }
  413. os_memset(buf, 0, sizeof(buf));
  414. h = (struct ieee80211_hdr *) buf;
  415. h->frame_control = host_to_le16(0x0208);
  416. os_memcpy(h->addr1, dst, ETH_ALEN);
  417. os_memcpy(h->addr2, src, ETH_ALEN);
  418. os_memcpy(h->addr3, src, ETH_ALEN);
  419. pos = (u8 *) (h + 1);
  420. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  421. pos += 8;
  422. os_memcpy(pos, eapol, sizeof(*eapol));
  423. pos += sizeof(*eapol);
  424. os_memcpy(pos, hdr, sizeof(*hdr));
  425. k = (struct wpa_eapol_key *) pos;
  426. WPA_PUT_BE16(k->key_info,
  427. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  428. WPA_PUT_BE16(k->key_data_length, plain_len);
  429. write_pcap_decrypted(wt, buf, sizeof(buf),
  430. decrypted, plain_len);
  431. }
  432. if (wpa_supplicant_parse_ies(decrypted, decrypted_len, &ie) < 0) {
  433. wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  434. os_free(decrypted);
  435. return;
  436. }
  437. if ((ie.wpa_ie &&
  438. os_memcmp(ie.wpa_ie, bss->wpaie, ie.wpa_ie_len) != 0) ||
  439. (ie.wpa_ie == NULL && bss->wpaie[0])) {
  440. wpa_printf(MSG_INFO, "Mismatch in WPA IE between "
  441. "EAPOL-Key 3/4 and Beacon/Probe Response "
  442. "from " MACSTR, MAC2STR(bss->bssid));
  443. wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
  444. ie.wpa_ie, ie.wpa_ie_len);
  445. wpa_hexdump(MSG_INFO, "WPA IE in Beacon/Probe "
  446. "Response",
  447. bss->wpaie,
  448. bss->wpaie[0] ? 2 + bss->wpaie[1] : 0);
  449. }
  450. if ((ie.rsn_ie &&
  451. os_memcmp(ie.rsn_ie, bss->rsnie, ie.rsn_ie_len) != 0) ||
  452. (ie.rsn_ie == NULL && bss->rsnie[0])) {
  453. wpa_printf(MSG_INFO, "Mismatch in RSN IE between "
  454. "EAPOL-Key 3/4 and Beacon/Probe Response "
  455. "from " MACSTR, MAC2STR(bss->bssid));
  456. wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
  457. ie.rsn_ie, ie.rsn_ie_len);
  458. wpa_hexdump(MSG_INFO, "RSN IE in (Re)Association "
  459. "Request",
  460. bss->rsnie,
  461. bss->rsnie[0] ? 2 + bss->rsnie[1] : 0);
  462. }
  463. learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
  464. os_free(decrypted);
  465. }
  466. static void rx_data_eapol_key_4_of_4(struct wlantest *wt, const u8 *dst,
  467. const u8 *src, const u8 *data, size_t len)
  468. {
  469. struct wlantest_bss *bss;
  470. struct wlantest_sta *sta;
  471. const struct ieee802_1x_hdr *eapol;
  472. const struct wpa_eapol_key *hdr;
  473. u16 key_info;
  474. wpa_printf(MSG_DEBUG, "EAPOL-Key 4/4 " MACSTR " -> " MACSTR,
  475. MAC2STR(src), MAC2STR(dst));
  476. bss = bss_get(wt, dst);
  477. if (bss == NULL)
  478. return;
  479. sta = sta_get(bss, src);
  480. if (sta == NULL)
  481. return;
  482. eapol = (const struct ieee802_1x_hdr *) data;
  483. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  484. key_info = WPA_GET_BE16(hdr->key_info);
  485. if (!sta->ptk_set) {
  486. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 4/4");
  487. return;
  488. }
  489. if (sta->ptk_set &&
  490. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  491. data, len) < 0) {
  492. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 4/4 MIC");
  493. return;
  494. }
  495. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 4/4");
  496. }
  497. static void rx_data_eapol_key_1_of_2(struct wlantest *wt, const u8 *dst,
  498. const u8 *src, const u8 *data, size_t len)
  499. {
  500. struct wlantest_bss *bss;
  501. struct wlantest_sta *sta;
  502. const struct ieee802_1x_hdr *eapol;
  503. const struct wpa_eapol_key *hdr;
  504. const u8 *key_data;
  505. u16 key_info, ver;
  506. u8 *decrypted;
  507. size_t decrypted_len = 0;
  508. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/2 " MACSTR " -> " MACSTR,
  509. MAC2STR(src), MAC2STR(dst));
  510. bss = bss_get(wt, src);
  511. if (bss == NULL)
  512. return;
  513. sta = sta_get(bss, dst);
  514. if (sta == NULL)
  515. return;
  516. eapol = (const struct ieee802_1x_hdr *) data;
  517. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  518. key_info = WPA_GET_BE16(hdr->key_info);
  519. if (!sta->ptk_set) {
  520. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 1/2");
  521. return;
  522. }
  523. if (sta->ptk_set &&
  524. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  525. data, len) < 0) {
  526. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 1/2 MIC");
  527. return;
  528. }
  529. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 1/2");
  530. key_data = (const u8 *) (hdr + 1);
  531. /* TODO: handle WPA without EncrKeyData bit */
  532. if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  533. wpa_printf(MSG_INFO, "EAPOL-Key 1/2 without EncrKeyData bit");
  534. return;
  535. }
  536. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  537. decrypted = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
  538. &decrypted_len);
  539. if (decrypted == NULL) {
  540. wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
  541. return;
  542. }
  543. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  544. decrypted, decrypted_len);
  545. if (wt->write_pcap_dumper) {
  546. /* Fill in a dummy Data frame header */
  547. u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
  548. struct ieee80211_hdr *h;
  549. struct wpa_eapol_key *k;
  550. u8 *pos;
  551. size_t plain_len;
  552. plain_len = decrypted_len;
  553. pos = decrypted;
  554. while (pos + 1 < decrypted + decrypted_len) {
  555. if (pos[0] == 0xdd && pos[1] == 0x00) {
  556. /* Remove padding */
  557. plain_len = pos - decrypted;
  558. break;
  559. }
  560. pos += 2 + pos[1];
  561. }
  562. os_memset(buf, 0, sizeof(buf));
  563. h = (struct ieee80211_hdr *) buf;
  564. h->frame_control = host_to_le16(0x0208);
  565. os_memcpy(h->addr1, dst, ETH_ALEN);
  566. os_memcpy(h->addr2, src, ETH_ALEN);
  567. os_memcpy(h->addr3, src, ETH_ALEN);
  568. pos = (u8 *) (h + 1);
  569. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  570. pos += 8;
  571. os_memcpy(pos, eapol, sizeof(*eapol));
  572. pos += sizeof(*eapol);
  573. os_memcpy(pos, hdr, sizeof(*hdr));
  574. k = (struct wpa_eapol_key *) pos;
  575. WPA_PUT_BE16(k->key_info,
  576. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  577. WPA_PUT_BE16(k->key_data_length, plain_len);
  578. write_pcap_decrypted(wt, buf, sizeof(buf),
  579. decrypted, plain_len);
  580. }
  581. learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
  582. os_free(decrypted);
  583. }
  584. static void rx_data_eapol_key_2_of_2(struct wlantest *wt, const u8 *dst,
  585. const u8 *src, const u8 *data, size_t len)
  586. {
  587. struct wlantest_bss *bss;
  588. struct wlantest_sta *sta;
  589. const struct ieee802_1x_hdr *eapol;
  590. const struct wpa_eapol_key *hdr;
  591. u16 key_info;
  592. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/2 " MACSTR " -> " MACSTR,
  593. MAC2STR(src), MAC2STR(dst));
  594. bss = bss_get(wt, dst);
  595. if (bss == NULL)
  596. return;
  597. sta = sta_get(bss, src);
  598. if (sta == NULL)
  599. return;
  600. eapol = (const struct ieee802_1x_hdr *) data;
  601. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  602. key_info = WPA_GET_BE16(hdr->key_info);
  603. if (!sta->ptk_set) {
  604. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 2/2");
  605. return;
  606. }
  607. if (sta->ptk_set &&
  608. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  609. data, len) < 0) {
  610. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 2/2 MIC");
  611. return;
  612. }
  613. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/2");
  614. }
  615. static void rx_data_eapol_key(struct wlantest *wt, const u8 *dst,
  616. const u8 *src, const u8 *data, size_t len,
  617. int prot)
  618. {
  619. const struct ieee802_1x_hdr *eapol;
  620. const struct wpa_eapol_key *hdr;
  621. const u8 *key_data;
  622. u16 key_info, key_length, ver, key_data_length;
  623. eapol = (const struct ieee802_1x_hdr *) data;
  624. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  625. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key",
  626. (const u8 *) hdr, len - sizeof(*eapol));
  627. if (len < sizeof(*hdr)) {
  628. wpa_printf(MSG_INFO, "Too short EAPOL-Key frame from " MACSTR,
  629. MAC2STR(src));
  630. return;
  631. }
  632. if (hdr->type == EAPOL_KEY_TYPE_RC4) {
  633. /* TODO: EAPOL-Key RC4 for WEP */
  634. return;
  635. }
  636. if (hdr->type != EAPOL_KEY_TYPE_RSN &&
  637. hdr->type != EAPOL_KEY_TYPE_WPA) {
  638. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key type %u",
  639. hdr->type);
  640. return;
  641. }
  642. key_info = WPA_GET_BE16(hdr->key_info);
  643. key_length = WPA_GET_BE16(hdr->key_length);
  644. key_data_length = WPA_GET_BE16(hdr->key_data_length);
  645. key_data = (const u8 *) (hdr + 1);
  646. if (key_data + key_data_length > data + len) {
  647. wpa_printf(MSG_INFO, "Truncated EAPOL-Key from " MACSTR,
  648. MAC2STR(src));
  649. return;
  650. }
  651. if (key_data + key_data_length < data + len) {
  652. wpa_hexdump(MSG_DEBUG, "Extra data after EAPOL-Key Key Data "
  653. "field", key_data + key_data_length,
  654. data + len - key_data - key_data_length);
  655. }
  656. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  657. wpa_printf(MSG_DEBUG, "EAPOL-Key ver=%u %c idx=%u%s%s%s%s%s%s%s%s "
  658. "datalen=%u",
  659. ver, key_info & WPA_KEY_INFO_KEY_TYPE ? 'P' : 'G',
  660. (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  661. WPA_KEY_INFO_KEY_INDEX_SHIFT,
  662. (key_info & WPA_KEY_INFO_INSTALL) ? " Install" : "",
  663. (key_info & WPA_KEY_INFO_ACK) ? " ACK" : "",
  664. (key_info & WPA_KEY_INFO_MIC) ? " MIC" : "",
  665. (key_info & WPA_KEY_INFO_SECURE) ? " Secure" : "",
  666. (key_info & WPA_KEY_INFO_ERROR) ? " Error" : "",
  667. (key_info & WPA_KEY_INFO_REQUEST) ? " Request" : "",
  668. (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) ? " Encr" : "",
  669. (key_info & WPA_KEY_INFO_SMK_MESSAGE) ? " SMK" : "",
  670. key_data_length);
  671. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  672. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
  673. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  674. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key Key Descriptor "
  675. "Version %u", ver);
  676. return;
  677. }
  678. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Replay Counter",
  679. hdr->replay_counter, WPA_REPLAY_COUNTER_LEN);
  680. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Nonce",
  681. hdr->key_nonce, WPA_NONCE_LEN);
  682. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key IV",
  683. hdr->key_iv, 16);
  684. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key RSC",
  685. hdr->key_rsc, WPA_KEY_RSC_LEN);
  686. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key MIC",
  687. hdr->key_mic, 16);
  688. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data",
  689. key_data, key_data_length);
  690. if (key_info & (WPA_KEY_INFO_ERROR | WPA_KEY_INFO_REQUEST))
  691. return;
  692. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  693. return;
  694. if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  695. /* 4-Way Handshake */
  696. switch (key_info & (WPA_KEY_INFO_SECURE |
  697. WPA_KEY_INFO_MIC |
  698. WPA_KEY_INFO_ACK |
  699. WPA_KEY_INFO_INSTALL)) {
  700. case WPA_KEY_INFO_ACK:
  701. rx_data_eapol_key_1_of_4(wt, dst, src, data, len);
  702. break;
  703. case WPA_KEY_INFO_MIC:
  704. rx_data_eapol_key_2_of_4(wt, dst, src, data, len);
  705. break;
  706. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  707. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL:
  708. rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
  709. break;
  710. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  711. rx_data_eapol_key_4_of_4(wt, dst, src, data, len);
  712. break;
  713. default:
  714. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  715. break;
  716. }
  717. } else {
  718. /* Group Key Handshake */
  719. switch (key_info & (WPA_KEY_INFO_SECURE |
  720. WPA_KEY_INFO_MIC |
  721. WPA_KEY_INFO_ACK)) {
  722. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  723. WPA_KEY_INFO_ACK:
  724. rx_data_eapol_key_1_of_2(wt, dst, src, data, len);
  725. break;
  726. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  727. rx_data_eapol_key_2_of_2(wt, dst, src, data, len);
  728. break;
  729. default:
  730. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  731. break;
  732. }
  733. }
  734. }
  735. static void rx_data_eapol(struct wlantest *wt, const u8 *dst, const u8 *src,
  736. const u8 *data, size_t len, int prot)
  737. {
  738. const struct ieee802_1x_hdr *hdr;
  739. u16 length;
  740. const u8 *p;
  741. wpa_hexdump(MSG_EXCESSIVE, "EAPOL", data, len);
  742. if (len < sizeof(*hdr)) {
  743. wpa_printf(MSG_INFO, "Too short EAPOL frame from " MACSTR,
  744. MAC2STR(src));
  745. return;
  746. }
  747. hdr = (const struct ieee802_1x_hdr *) data;
  748. length = be_to_host16(hdr->length);
  749. wpa_printf(MSG_DEBUG, "RX EAPOL: " MACSTR " -> " MACSTR "%s ver=%u "
  750. "type=%u len=%u",
  751. MAC2STR(src), MAC2STR(dst), prot ? " Prot" : "",
  752. hdr->version, hdr->type, length);
  753. if (sizeof(*hdr) + length > len) {
  754. wpa_printf(MSG_INFO, "Truncated EAPOL frame from " MACSTR,
  755. MAC2STR(src));
  756. return;
  757. }
  758. if (sizeof(*hdr) + length < len) {
  759. wpa_printf(MSG_INFO, "EAPOL frame with %d extra bytes",
  760. (int) (len - sizeof(*hdr) - length));
  761. }
  762. p = (const u8 *) (hdr + 1);
  763. switch (hdr->type) {
  764. case IEEE802_1X_TYPE_EAP_PACKET:
  765. wpa_hexdump(MSG_MSGDUMP, "EAPOL - EAP packet", p, length);
  766. break;
  767. case IEEE802_1X_TYPE_EAPOL_START:
  768. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Start", p, length);
  769. break;
  770. case IEEE802_1X_TYPE_EAPOL_LOGOFF:
  771. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Logoff", p, length);
  772. break;
  773. case IEEE802_1X_TYPE_EAPOL_KEY:
  774. rx_data_eapol_key(wt, dst, src, data, sizeof(*hdr) + length,
  775. prot);
  776. break;
  777. case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
  778. wpa_hexdump(MSG_MSGDUMP, "EAPOL - Encapsulated ASF alert",
  779. p, length);
  780. break;
  781. default:
  782. wpa_hexdump(MSG_MSGDUMP, "Unknown EAPOL payload", p, length);
  783. break;
  784. }
  785. }
  786. static void rx_data_eth(struct wlantest *wt, const u8 *dst, const u8 *src,
  787. u16 ethertype, const u8 *data, size_t len, int prot)
  788. {
  789. if (ethertype == ETH_P_PAE)
  790. rx_data_eapol(wt, dst, src, data, len, prot);
  791. }
  792. static void rx_data_process(struct wlantest *wt, const u8 *dst, const u8 *src,
  793. const u8 *data, size_t len, int prot)
  794. {
  795. if (len == 0)
  796. return;
  797. if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
  798. rx_data_eth(wt, dst, src, WPA_GET_BE16(data + 6),
  799. data + 8, len - 8, prot);
  800. return;
  801. }
  802. wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
  803. }
  804. static void rx_data_bss_prot_group(struct wlantest *wt,
  805. const struct ieee80211_hdr *hdr,
  806. const u8 *qos, const u8 *dst, const u8 *src,
  807. const u8 *data, size_t len)
  808. {
  809. struct wlantest_bss *bss;
  810. int keyid;
  811. u8 *decrypted;
  812. size_t dlen;
  813. u8 pn[6];
  814. bss = bss_get(wt, hdr->addr2);
  815. if (bss == NULL)
  816. return;
  817. if (len < 4) {
  818. wpa_printf(MSG_INFO, "Too short group addressed data frame");
  819. return;
  820. }
  821. keyid = data[3] >> 6;
  822. if (bss->gtk_len[keyid] == 0) {
  823. wpa_printf(MSG_MSGDUMP, "No GTK known to decrypt the frame "
  824. "(A2=" MACSTR " KeyID=%d)",
  825. MAC2STR(hdr->addr2), keyid);
  826. return;
  827. }
  828. ccmp_get_pn(pn, data);
  829. if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
  830. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  831. MAC2STR(hdr->addr2));
  832. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  833. wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
  834. }
  835. /* TODO: TKIP */
  836. decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len, &dlen);
  837. if (decrypted) {
  838. rx_data_process(wt, dst, src, decrypted, dlen, 1);
  839. os_memcpy(bss->rsc[keyid], pn, 6);
  840. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  841. decrypted, dlen);
  842. }
  843. os_free(decrypted);
  844. }
  845. static void rx_data_bss_prot(struct wlantest *wt,
  846. const struct ieee80211_hdr *hdr, const u8 *qos,
  847. const u8 *dst, const u8 *src, const u8 *data,
  848. size_t len)
  849. {
  850. struct wlantest_bss *bss;
  851. struct wlantest_sta *sta;
  852. int keyid;
  853. u16 fc = le_to_host16(hdr->frame_control);
  854. u8 *decrypted;
  855. size_t dlen;
  856. int tid;
  857. u8 pn[6], *rsc;
  858. if (hdr->addr1[0] & 0x01) {
  859. rx_data_bss_prot_group(wt, hdr, qos, dst, src, data, len);
  860. return;
  861. }
  862. if (fc & WLAN_FC_TODS) {
  863. bss = bss_get(wt, hdr->addr1);
  864. if (bss == NULL)
  865. return;
  866. sta = sta_get(bss, hdr->addr2);
  867. } else {
  868. bss = bss_get(wt, hdr->addr2);
  869. if (bss == NULL)
  870. return;
  871. sta = sta_get(bss, hdr->addr1);
  872. }
  873. if (sta == NULL || !sta->ptk_set) {
  874. wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
  875. return;
  876. }
  877. if (len < 4) {
  878. wpa_printf(MSG_INFO, "Too short encrypted data frame");
  879. return;
  880. }
  881. keyid = data[3] >> 6;
  882. if (keyid != 0) {
  883. wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
  884. "individually addressed Data frame from " MACSTR,
  885. keyid, MAC2STR(hdr->addr2));
  886. }
  887. if (qos)
  888. tid = qos[0] & 0x0f;
  889. else
  890. tid = 0;
  891. if (fc & WLAN_FC_TODS)
  892. rsc = sta->rsc_tods[tid];
  893. else
  894. rsc = sta->rsc_fromds[tid];
  895. ccmp_get_pn(pn, data);
  896. if (os_memcmp(pn, rsc, 6) <= 0) {
  897. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  898. MAC2STR(hdr->addr2));
  899. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  900. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  901. }
  902. /* TODO: TKIP */
  903. decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  904. if (decrypted) {
  905. rx_data_process(wt, dst, src, decrypted, dlen, 1);
  906. os_memcpy(rsc, pn, 6);
  907. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  908. decrypted, dlen);
  909. }
  910. os_free(decrypted);
  911. }
  912. static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  913. const u8 *qos, const u8 *dst, const u8 *src,
  914. const u8 *data, size_t len)
  915. {
  916. u16 fc = le_to_host16(hdr->frame_control);
  917. int prot = !!(fc & WLAN_FC_ISWEP);
  918. if (qos) {
  919. u8 ack = (qos[0] & 0x60) >> 5;
  920. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  921. " len=%u%s tid=%u%s%s",
  922. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  923. prot ? " Prot" : "", qos[0] & 0x0f,
  924. (qos[0] & 0x10) ? " EOSP" : "",
  925. ack == 0 ? "" :
  926. (ack == 1 ? " NoAck" :
  927. (ack == 2 ? " NoExpAck" : " BA")));
  928. } else {
  929. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  930. " len=%u%s",
  931. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  932. prot ? " Prot" : "");
  933. }
  934. if (prot)
  935. rx_data_bss_prot(wt, hdr, qos, dst, src, data, len);
  936. else
  937. rx_data_process(wt, dst, src, data, len, 0);
  938. }
  939. void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  940. {
  941. const struct ieee80211_hdr *hdr;
  942. u16 fc, stype;
  943. size_t hdrlen;
  944. const u8 *qos = NULL;
  945. if (len < 24)
  946. return;
  947. hdr = (const struct ieee80211_hdr *) data;
  948. fc = le_to_host16(hdr->frame_control);
  949. stype = WLAN_FC_GET_STYPE(fc);
  950. hdrlen = 24;
  951. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  952. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  953. hdrlen += ETH_ALEN;
  954. if (stype & 0x08) {
  955. qos = data + hdrlen;
  956. hdrlen += 2;
  957. }
  958. if (len < hdrlen)
  959. return;
  960. wt->rx_data++;
  961. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  962. case 0:
  963. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  964. MACSTR " BSSID=" MACSTR,
  965. data_stype(WLAN_FC_GET_STYPE(fc)),
  966. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  967. fc & WLAN_FC_ISWEP ? " Prot" : "",
  968. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  969. MAC2STR(hdr->addr3));
  970. break;
  971. case WLAN_FC_FROMDS:
  972. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  973. " BSSID=" MACSTR " SA=" MACSTR,
  974. data_stype(WLAN_FC_GET_STYPE(fc)),
  975. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  976. fc & WLAN_FC_ISWEP ? " Prot" : "",
  977. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  978. MAC2STR(hdr->addr3));
  979. rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
  980. data + hdrlen, len - hdrlen);
  981. break;
  982. case WLAN_FC_TODS:
  983. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  984. " SA=" MACSTR " DA=" MACSTR,
  985. data_stype(WLAN_FC_GET_STYPE(fc)),
  986. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  987. fc & WLAN_FC_ISWEP ? " Prot" : "",
  988. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  989. MAC2STR(hdr->addr3));
  990. rx_data_bss(wt, hdr, qos, hdr->addr3, hdr->addr2,
  991. data + hdrlen, len - hdrlen);
  992. break;
  993. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  994. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  995. MACSTR " DA=" MACSTR " SA=" MACSTR,
  996. data_stype(WLAN_FC_GET_STYPE(fc)),
  997. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  998. fc & WLAN_FC_ISWEP ? " Prot" : "",
  999. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1000. MAC2STR(hdr->addr3),
  1001. MAC2STR((const u8 *) (hdr + 1)));
  1002. break;
  1003. }
  1004. }