rx_data.c 31 KB

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