rx_mgmt.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. /*
  2. * Received Management frame processing
  3. * Copyright (c) 2010-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "common/defs.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "common/ieee802_11_common.h"
  13. #include "common/wpa_common.h"
  14. #include "crypto/aes.h"
  15. #include "crypto/aes_siv.h"
  16. #include "crypto/aes_wrap.h"
  17. #include "wlantest.h"
  18. static const char * mgmt_stype(u16 stype)
  19. {
  20. switch (stype) {
  21. case WLAN_FC_STYPE_ASSOC_REQ:
  22. return "ASSOC-REQ";
  23. case WLAN_FC_STYPE_ASSOC_RESP:
  24. return "ASSOC-RESP";
  25. case WLAN_FC_STYPE_REASSOC_REQ:
  26. return "REASSOC-REQ";
  27. case WLAN_FC_STYPE_REASSOC_RESP:
  28. return "REASSOC-RESP";
  29. case WLAN_FC_STYPE_PROBE_REQ:
  30. return "PROBE-REQ";
  31. case WLAN_FC_STYPE_PROBE_RESP:
  32. return "PROBE-RESP";
  33. case WLAN_FC_STYPE_BEACON:
  34. return "BEACON";
  35. case WLAN_FC_STYPE_ATIM:
  36. return "ATIM";
  37. case WLAN_FC_STYPE_DISASSOC:
  38. return "DISASSOC";
  39. case WLAN_FC_STYPE_AUTH:
  40. return "AUTH";
  41. case WLAN_FC_STYPE_DEAUTH:
  42. return "DEAUTH";
  43. case WLAN_FC_STYPE_ACTION:
  44. return "ACTION";
  45. }
  46. return "??";
  47. }
  48. static void rx_mgmt_beacon(struct wlantest *wt, const u8 *data, size_t len)
  49. {
  50. const struct ieee80211_mgmt *mgmt;
  51. struct wlantest_bss *bss;
  52. struct ieee802_11_elems elems;
  53. size_t offset;
  54. mgmt = (const struct ieee80211_mgmt *) data;
  55. offset = mgmt->u.beacon.variable - data;
  56. if (len < offset)
  57. return;
  58. bss = bss_get(wt, mgmt->bssid);
  59. if (bss == NULL)
  60. return;
  61. if (bss->proberesp_seen)
  62. return; /* do not override with Beacon data */
  63. bss->capab_info = le_to_host16(mgmt->u.beacon.capab_info);
  64. if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - offset,
  65. &elems, 0) == ParseFailed) {
  66. if (bss->parse_error_reported)
  67. return;
  68. add_note(wt, MSG_INFO, "Invalid IEs in a Beacon frame from "
  69. MACSTR, MAC2STR(mgmt->sa));
  70. bss->parse_error_reported = 1;
  71. return;
  72. }
  73. bss_update(wt, bss, &elems);
  74. }
  75. static void rx_mgmt_probe_resp(struct wlantest *wt, const u8 *data, size_t len)
  76. {
  77. const struct ieee80211_mgmt *mgmt;
  78. struct wlantest_bss *bss;
  79. struct ieee802_11_elems elems;
  80. size_t offset;
  81. mgmt = (const struct ieee80211_mgmt *) data;
  82. offset = mgmt->u.probe_resp.variable - data;
  83. if (len < offset)
  84. return;
  85. bss = bss_get(wt, mgmt->bssid);
  86. if (bss == NULL)
  87. return;
  88. bss->counters[WLANTEST_BSS_COUNTER_PROBE_RESPONSE]++;
  89. bss->capab_info = le_to_host16(mgmt->u.probe_resp.capab_info);
  90. if (ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - offset,
  91. &elems, 0) == ParseFailed) {
  92. if (bss->parse_error_reported)
  93. return;
  94. add_note(wt, MSG_INFO, "Invalid IEs in a Probe Response frame "
  95. "from " MACSTR, MAC2STR(mgmt->sa));
  96. bss->parse_error_reported = 1;
  97. return;
  98. }
  99. bss_update(wt, bss, &elems);
  100. }
  101. static void process_fils_auth(struct wlantest *wt, struct wlantest_bss *bss,
  102. struct wlantest_sta *sta,
  103. const struct ieee80211_mgmt *mgmt, size_t len)
  104. {
  105. struct ieee802_11_elems elems;
  106. u16 trans;
  107. struct wpa_ie_data data;
  108. if (sta->auth_alg != WLAN_AUTH_FILS_SK ||
  109. len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth))
  110. return;
  111. trans = le_to_host16(mgmt->u.auth.auth_transaction);
  112. if (ieee802_11_parse_elems(mgmt->u.auth.variable,
  113. len - IEEE80211_HDRLEN -
  114. sizeof(mgmt->u.auth), &elems, 0) ==
  115. ParseFailed)
  116. return;
  117. if (trans == 1) {
  118. if (!elems.rsn_ie) {
  119. add_note(wt, MSG_INFO,
  120. "FILS Authentication frame missing RSNE");
  121. return;
  122. }
  123. if (wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2,
  124. elems.rsn_ie_len + 2, &data) < 0) {
  125. add_note(wt, MSG_INFO,
  126. "Invalid RSNE in FILS Authentication frame");
  127. return;
  128. }
  129. sta->key_mgmt = data.key_mgmt;
  130. sta->pairwise_cipher = data.pairwise_cipher;
  131. }
  132. if (!elems.fils_nonce) {
  133. add_note(wt, MSG_INFO,
  134. "FILS Authentication frame missing nonce");
  135. return;
  136. }
  137. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  138. os_memcpy(sta->anonce, elems.fils_nonce, FILS_NONCE_LEN);
  139. else
  140. os_memcpy(sta->snonce, elems.fils_nonce, FILS_NONCE_LEN);
  141. }
  142. static void rx_mgmt_auth(struct wlantest *wt, const u8 *data, size_t len)
  143. {
  144. const struct ieee80211_mgmt *mgmt;
  145. struct wlantest_bss *bss;
  146. struct wlantest_sta *sta;
  147. u16 alg, trans, status;
  148. mgmt = (const struct ieee80211_mgmt *) data;
  149. bss = bss_get(wt, mgmt->bssid);
  150. if (bss == NULL)
  151. return;
  152. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  153. sta = sta_get(bss, mgmt->da);
  154. else
  155. sta = sta_get(bss, mgmt->sa);
  156. if (sta == NULL)
  157. return;
  158. if (len < 24 + 6) {
  159. add_note(wt, MSG_INFO, "Too short Authentication frame from "
  160. MACSTR, MAC2STR(mgmt->sa));
  161. return;
  162. }
  163. alg = le_to_host16(mgmt->u.auth.auth_alg);
  164. sta->auth_alg = alg;
  165. trans = le_to_host16(mgmt->u.auth.auth_transaction);
  166. status = le_to_host16(mgmt->u.auth.status_code);
  167. wpa_printf(MSG_DEBUG, "AUTH " MACSTR " -> " MACSTR
  168. " (alg=%u trans=%u status=%u)",
  169. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), alg, trans, status);
  170. if (alg == 0 && trans == 2 && status == 0) {
  171. if (sta->state == STATE1) {
  172. add_note(wt, MSG_DEBUG, "STA " MACSTR
  173. " moved to State 2 with " MACSTR,
  174. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  175. sta->state = STATE2;
  176. }
  177. }
  178. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  179. sta->counters[WLANTEST_STA_COUNTER_AUTH_RX]++;
  180. else
  181. sta->counters[WLANTEST_STA_COUNTER_AUTH_TX]++;
  182. process_fils_auth(wt, bss, sta, mgmt, len);
  183. }
  184. static void deauth_all_stas(struct wlantest *wt, struct wlantest_bss *bss)
  185. {
  186. struct wlantest_sta *sta;
  187. dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
  188. if (sta->state == STATE1)
  189. continue;
  190. add_note(wt, MSG_DEBUG, "STA " MACSTR
  191. " moved to State 1 with " MACSTR,
  192. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  193. sta->state = STATE1;
  194. }
  195. }
  196. static void tdls_link_down(struct wlantest *wt, struct wlantest_bss *bss,
  197. struct wlantest_sta *sta)
  198. {
  199. struct wlantest_tdls *tdls;
  200. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  201. if ((tdls->init == sta || tdls->resp == sta) && tdls->link_up)
  202. {
  203. add_note(wt, MSG_DEBUG, "TDLS: Set link down based on "
  204. "STA deauth/disassoc");
  205. tdls->link_up = 0;
  206. }
  207. }
  208. }
  209. static void rx_mgmt_deauth(struct wlantest *wt, const u8 *data, size_t len,
  210. int valid)
  211. {
  212. const struct ieee80211_mgmt *mgmt;
  213. struct wlantest_bss *bss;
  214. struct wlantest_sta *sta;
  215. u16 fc, reason;
  216. mgmt = (const struct ieee80211_mgmt *) data;
  217. bss = bss_get(wt, mgmt->bssid);
  218. if (bss == NULL)
  219. return;
  220. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  221. sta = sta_get(bss, mgmt->da);
  222. else
  223. sta = sta_get(bss, mgmt->sa);
  224. if (len < 24 + 2) {
  225. add_note(wt, MSG_INFO, "Too short Deauthentication frame from "
  226. MACSTR, MAC2STR(mgmt->sa));
  227. return;
  228. }
  229. reason = le_to_host16(mgmt->u.deauth.reason_code);
  230. wpa_printf(MSG_DEBUG, "DEAUTH " MACSTR " -> " MACSTR
  231. " (reason=%u) (valid=%d)",
  232. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  233. reason, valid);
  234. wpa_hexdump(MSG_MSGDUMP, "DEAUTH payload", data + 24, len - 24);
  235. if (sta == NULL) {
  236. if (valid && mgmt->da[0] == 0xff)
  237. deauth_all_stas(wt, bss);
  238. return;
  239. }
  240. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  241. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DEAUTH_RX :
  242. WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX]++;
  243. if (sta->pwrmgt && !sta->pspoll)
  244. sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_ASLEEP]++;
  245. else
  246. sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_AWAKE]++;
  247. fc = le_to_host16(mgmt->frame_control);
  248. if (!(fc & WLAN_FC_ISWEP) && reason == 6)
  249. sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_RC6]++;
  250. else if (!(fc & WLAN_FC_ISWEP) && reason == 7)
  251. sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_RC7]++;
  252. } else
  253. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DEAUTH_TX :
  254. WLANTEST_STA_COUNTER_INVALID_DEAUTH_TX]++;
  255. if (!valid) {
  256. add_note(wt, MSG_INFO, "Do not change STA " MACSTR " State "
  257. "since Disassociation frame was not protected "
  258. "correctly", MAC2STR(sta->addr));
  259. return;
  260. }
  261. if (sta->state != STATE1) {
  262. add_note(wt, MSG_DEBUG, "STA " MACSTR
  263. " moved to State 1 with " MACSTR,
  264. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  265. sta->state = STATE1;
  266. }
  267. tdls_link_down(wt, bss, sta);
  268. }
  269. static const u8 * get_fils_session(const u8 *ies, size_t ies_len)
  270. {
  271. const u8 *ie, *end;
  272. ie = ies;
  273. end = ((const u8 *) ie) + ies_len;
  274. while (ie + 1 < end) {
  275. if (ie + 2 + ie[1] > end)
  276. break;
  277. if (ie[0] == WLAN_EID_EXTENSION &&
  278. ie[1] >= 1 + FILS_SESSION_LEN &&
  279. ie[2] == WLAN_EID_EXT_FILS_SESSION)
  280. return ie;
  281. ie += 2 + ie[1];
  282. }
  283. return NULL;
  284. }
  285. static int try_rmsk(struct wlantest *wt, struct wlantest_bss *bss,
  286. struct wlantest_sta *sta, struct wlantest_pmk *pmk,
  287. const u8 *frame_start, const u8 *frame_ad,
  288. const u8 *frame_ad_end, const u8 *encr_end)
  289. {
  290. size_t pmk_len = 0;
  291. u8 pmk_buf[PMK_LEN_MAX];
  292. struct wpa_ptk ptk;
  293. u8 ick[FILS_ICK_MAX_LEN];
  294. size_t ick_len;
  295. const u8 *aad[5];
  296. size_t aad_len[5];
  297. u8 buf[2000];
  298. if (fils_rmsk_to_pmk(sta->key_mgmt, pmk->pmk, pmk->pmk_len,
  299. sta->snonce, sta->anonce, NULL, 0,
  300. pmk_buf, &pmk_len) < 0)
  301. return -1;
  302. if (fils_pmk_to_ptk(pmk_buf, pmk_len, sta->addr, bss->bssid,
  303. sta->snonce, sta->anonce, &ptk, ick, &ick_len,
  304. sta->key_mgmt, sta->pairwise_cipher,
  305. NULL, NULL) < 0)
  306. return -1;
  307. /* Check AES-SIV decryption with the derived key */
  308. /* AES-SIV AAD vectors */
  309. /* The STA's MAC address */
  310. aad[0] = sta->addr;
  311. aad_len[0] = ETH_ALEN;
  312. /* The AP's BSSID */
  313. aad[1] = bss->bssid;
  314. aad_len[1] = ETH_ALEN;
  315. /* The STA's nonce */
  316. aad[2] = sta->snonce;
  317. aad_len[2] = FILS_NONCE_LEN;
  318. /* The AP's nonce */
  319. aad[3] = sta->anonce;
  320. aad_len[3] = FILS_NONCE_LEN;
  321. /*
  322. * The (Re)Association Request frame from the Capability Information
  323. * field to the FILS Session element (both inclusive).
  324. */
  325. aad[4] = frame_ad;
  326. aad_len[4] = frame_ad_end - frame_ad;
  327. if (encr_end - frame_ad_end < AES_BLOCK_SIZE ||
  328. encr_end - frame_ad_end > sizeof(buf))
  329. return -1;
  330. if (aes_siv_decrypt(ptk.kek, ptk.kek_len,
  331. frame_ad_end, encr_end - frame_ad_end,
  332. 5, aad, aad_len, buf) < 0) {
  333. wpa_printf(MSG_DEBUG,
  334. "FILS: Derived PTK did not match AES-SIV data");
  335. return -1;
  336. }
  337. add_note(wt, MSG_DEBUG, "Derived FILS PTK");
  338. os_memcpy(&sta->ptk, &ptk, sizeof(ptk));
  339. sta->ptk_set = 1;
  340. sta->counters[WLANTEST_STA_COUNTER_PTK_LEARNED]++;
  341. wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
  342. buf, encr_end - frame_ad_end - AES_BLOCK_SIZE);
  343. if (wt->write_pcap_dumper || wt->pcapng) {
  344. write_pcap_decrypted(wt, frame_start,
  345. frame_ad_end - frame_start,
  346. buf,
  347. encr_end - frame_ad_end - AES_BLOCK_SIZE);
  348. }
  349. return 0;
  350. }
  351. static void derive_fils_keys(struct wlantest *wt, struct wlantest_bss *bss,
  352. struct wlantest_sta *sta, const u8 *frame_start,
  353. const u8 *frame_ad, const u8 *frame_ad_end,
  354. const u8 *encr_end)
  355. {
  356. struct wlantest_pmk *pmk;
  357. wpa_printf(MSG_DEBUG, "Trying to derive PTK for " MACSTR
  358. " from FILS rMSK", MAC2STR(sta->addr));
  359. dl_list_for_each(pmk, &bss->pmk, struct wlantest_pmk,
  360. list) {
  361. wpa_printf(MSG_DEBUG, "Try per-BSS PMK");
  362. if (try_rmsk(wt, bss, sta, pmk, frame_start, frame_ad,
  363. frame_ad_end, encr_end) == 0)
  364. return;
  365. }
  366. dl_list_for_each(pmk, &wt->pmk, struct wlantest_pmk, list) {
  367. wpa_printf(MSG_DEBUG, "Try global PMK");
  368. if (try_rmsk(wt, bss, sta, pmk, frame_start, frame_ad,
  369. frame_ad_end, encr_end) == 0)
  370. return;
  371. }
  372. }
  373. static void rx_mgmt_assoc_req(struct wlantest *wt, const u8 *data, size_t len)
  374. {
  375. const struct ieee80211_mgmt *mgmt;
  376. struct wlantest_bss *bss;
  377. struct wlantest_sta *sta;
  378. struct ieee802_11_elems elems;
  379. const u8 *ie;
  380. size_t ie_len;
  381. mgmt = (const struct ieee80211_mgmt *) data;
  382. bss = bss_get(wt, mgmt->bssid);
  383. if (bss == NULL)
  384. return;
  385. sta = sta_get(bss, mgmt->sa);
  386. if (sta == NULL)
  387. return;
  388. if (len < 24 + 4) {
  389. add_note(wt, MSG_INFO, "Too short Association Request frame "
  390. "from " MACSTR, MAC2STR(mgmt->sa));
  391. return;
  392. }
  393. wpa_printf(MSG_DEBUG, "ASSOCREQ " MACSTR " -> " MACSTR
  394. " (capab=0x%x listen_int=%u)",
  395. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  396. le_to_host16(mgmt->u.assoc_req.capab_info),
  397. le_to_host16(mgmt->u.assoc_req.listen_interval));
  398. sta->counters[WLANTEST_STA_COUNTER_ASSOCREQ_TX]++;
  399. ie = mgmt->u.assoc_req.variable;
  400. ie_len = len - (mgmt->u.assoc_req.variable - data);
  401. if (sta->auth_alg == WLAN_AUTH_FILS_SK) {
  402. const u8 *session, *frame_ad, *frame_ad_end, *encr_end;
  403. session = get_fils_session(ie, ie_len);
  404. if (session) {
  405. frame_ad = (const u8 *) &mgmt->u.assoc_req.capab_info;
  406. frame_ad_end = session + 2 + session[1];
  407. encr_end = data + len;
  408. derive_fils_keys(wt, bss, sta, data, frame_ad,
  409. frame_ad_end, encr_end);
  410. ie_len = session - ie;
  411. }
  412. }
  413. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  414. add_note(wt, MSG_INFO, "Invalid IEs in Association Request "
  415. "frame from " MACSTR, MAC2STR(mgmt->sa));
  416. return;
  417. }
  418. sta->assocreq_capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  419. sta->assocreq_listen_int =
  420. le_to_host16(mgmt->u.assoc_req.listen_interval);
  421. os_free(sta->assocreq_ies);
  422. sta->assocreq_ies_len = len - (mgmt->u.assoc_req.variable - data);
  423. sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
  424. if (sta->assocreq_ies)
  425. os_memcpy(sta->assocreq_ies, mgmt->u.assoc_req.variable,
  426. sta->assocreq_ies_len);
  427. sta_update_assoc(sta, &elems);
  428. }
  429. static void decrypt_fils_assoc_resp(struct wlantest *wt,
  430. struct wlantest_bss *bss,
  431. struct wlantest_sta *sta,
  432. const u8 *frame_start, const u8 *frame_ad,
  433. const u8 *frame_ad_end, const u8 *encr_end)
  434. {
  435. const u8 *aad[5];
  436. size_t aad_len[5];
  437. u8 buf[2000];
  438. if (!sta->ptk_set)
  439. return;
  440. /* Check AES-SIV decryption with the derived key */
  441. /* AES-SIV AAD vectors */
  442. /* The AP's BSSID */
  443. aad[0] = bss->bssid;
  444. aad_len[0] = ETH_ALEN;
  445. /* The STA's MAC address */
  446. aad[1] = sta->addr;
  447. aad_len[1] = ETH_ALEN;
  448. /* The AP's nonce */
  449. aad[2] = sta->anonce;
  450. aad_len[2] = FILS_NONCE_LEN;
  451. /* The STA's nonce */
  452. aad[3] = sta->snonce;
  453. aad_len[3] = FILS_NONCE_LEN;
  454. /*
  455. * The (Re)Association Response frame from the Capability Information
  456. * field to the FILS Session element (both inclusive).
  457. */
  458. aad[4] = frame_ad;
  459. aad_len[4] = frame_ad_end - frame_ad;
  460. if (encr_end - frame_ad_end < AES_BLOCK_SIZE ||
  461. encr_end - frame_ad_end > sizeof(buf))
  462. return;
  463. if (aes_siv_decrypt(sta->ptk.kek, sta->ptk.kek_len,
  464. frame_ad_end, encr_end - frame_ad_end,
  465. 5, aad, aad_len, buf) < 0) {
  466. wpa_printf(MSG_DEBUG,
  467. "FILS: Derived PTK did not match AES-SIV data");
  468. return;
  469. }
  470. wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Response elements",
  471. buf, encr_end - frame_ad_end - AES_BLOCK_SIZE);
  472. if (wt->write_pcap_dumper || wt->pcapng) {
  473. write_pcap_decrypted(wt, frame_start,
  474. frame_ad_end - frame_start,
  475. buf,
  476. encr_end - frame_ad_end - AES_BLOCK_SIZE);
  477. }
  478. }
  479. static void rx_mgmt_assoc_resp(struct wlantest *wt, const u8 *data, size_t len)
  480. {
  481. const struct ieee80211_mgmt *mgmt;
  482. struct wlantest_bss *bss;
  483. struct wlantest_sta *sta;
  484. u16 capab, status, aid;
  485. const u8 *ies;
  486. size_t ies_len;
  487. struct wpa_ft_ies parse;
  488. mgmt = (const struct ieee80211_mgmt *) data;
  489. bss = bss_get(wt, mgmt->bssid);
  490. if (bss == NULL)
  491. return;
  492. sta = sta_get(bss, mgmt->da);
  493. if (sta == NULL)
  494. return;
  495. if (len < 24 + 6) {
  496. add_note(wt, MSG_INFO, "Too short Association Response frame "
  497. "from " MACSTR, MAC2STR(mgmt->sa));
  498. return;
  499. }
  500. ies = mgmt->u.assoc_resp.variable;
  501. ies_len = len - (mgmt->u.assoc_resp.variable - data);
  502. capab = le_to_host16(mgmt->u.assoc_resp.capab_info);
  503. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  504. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  505. wpa_printf(MSG_DEBUG, "ASSOCRESP " MACSTR " -> " MACSTR
  506. " (capab=0x%x status=%u aid=%u)",
  507. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  508. aid & 0x3fff);
  509. if (sta->auth_alg == WLAN_AUTH_FILS_SK) {
  510. const u8 *session, *frame_ad, *frame_ad_end, *encr_end;
  511. session = get_fils_session(ies, ies_len);
  512. if (session) {
  513. frame_ad = (const u8 *) &mgmt->u.assoc_resp.capab_info;
  514. frame_ad_end = session + 2 + session[1];
  515. encr_end = data + len;
  516. decrypt_fils_assoc_resp(wt, bss, sta, data, frame_ad,
  517. frame_ad_end, encr_end);
  518. ies_len = session - ies;
  519. }
  520. }
  521. if (status == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) {
  522. struct ieee802_11_elems elems;
  523. if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
  524. ParseFailed) {
  525. add_note(wt, MSG_INFO, "Failed to parse IEs in "
  526. "AssocResp from " MACSTR,
  527. MAC2STR(mgmt->sa));
  528. } else if (elems.timeout_int == NULL ||
  529. elems.timeout_int[0] !=
  530. WLAN_TIMEOUT_ASSOC_COMEBACK) {
  531. add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
  532. "with Assoc Comeback time in AssocResp "
  533. "(status=30) from " MACSTR,
  534. MAC2STR(mgmt->sa));
  535. } else {
  536. sta->counters[
  537. WLANTEST_STA_COUNTER_ASSOCRESP_COMEBACK]++;
  538. }
  539. }
  540. if (status)
  541. return;
  542. if ((aid & 0xc000) != 0xc000) {
  543. add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  544. "in Association Response from " MACSTR,
  545. MAC2STR(mgmt->sa));
  546. }
  547. sta->aid = aid & 0xc000;
  548. if (sta->state < STATE2) {
  549. add_note(wt, MSG_DEBUG,
  550. "STA " MACSTR " was not in State 2 when "
  551. "getting associated", MAC2STR(sta->addr));
  552. }
  553. if (sta->state < STATE3) {
  554. add_note(wt, MSG_DEBUG, "STA " MACSTR
  555. " moved to State 3 with " MACSTR,
  556. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  557. sta->state = STATE3;
  558. }
  559. if (wpa_ft_parse_ies(ies, ies_len, &parse) == 0) {
  560. if (parse.r0kh_id) {
  561. os_memcpy(bss->r0kh_id, parse.r0kh_id,
  562. parse.r0kh_id_len);
  563. bss->r0kh_id_len = parse.r0kh_id_len;
  564. }
  565. if (parse.r1kh_id)
  566. os_memcpy(bss->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
  567. }
  568. }
  569. static void rx_mgmt_reassoc_req(struct wlantest *wt, const u8 *data,
  570. size_t len)
  571. {
  572. const struct ieee80211_mgmt *mgmt;
  573. struct wlantest_bss *bss;
  574. struct wlantest_sta *sta;
  575. struct ieee802_11_elems elems;
  576. const u8 *ie;
  577. size_t ie_len;
  578. mgmt = (const struct ieee80211_mgmt *) data;
  579. bss = bss_get(wt, mgmt->bssid);
  580. if (bss == NULL)
  581. return;
  582. sta = sta_get(bss, mgmt->sa);
  583. if (sta == NULL)
  584. return;
  585. if (len < 24 + 4 + ETH_ALEN) {
  586. add_note(wt, MSG_INFO, "Too short Reassociation Request frame "
  587. "from " MACSTR, MAC2STR(mgmt->sa));
  588. return;
  589. }
  590. wpa_printf(MSG_DEBUG, "REASSOCREQ " MACSTR " -> " MACSTR
  591. " (capab=0x%x listen_int=%u current_ap=" MACSTR ")",
  592. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  593. le_to_host16(mgmt->u.reassoc_req.capab_info),
  594. le_to_host16(mgmt->u.reassoc_req.listen_interval),
  595. MAC2STR(mgmt->u.reassoc_req.current_ap));
  596. sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX]++;
  597. ie = mgmt->u.reassoc_req.variable;
  598. ie_len = len - (mgmt->u.reassoc_req.variable - data);
  599. if (sta->auth_alg == WLAN_AUTH_FILS_SK) {
  600. const u8 *session, *frame_ad, *frame_ad_end, *encr_end;
  601. session = get_fils_session(ie, ie_len);
  602. if (session) {
  603. frame_ad = (const u8 *) &mgmt->u.reassoc_req.capab_info;
  604. frame_ad_end = session + 2 + session[1];
  605. encr_end = data + len;
  606. derive_fils_keys(wt, bss, sta, data, frame_ad,
  607. frame_ad_end, encr_end);
  608. ie_len = session - ie;
  609. }
  610. }
  611. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  612. add_note(wt, MSG_INFO, "Invalid IEs in Reassociation Request "
  613. "frame from " MACSTR, MAC2STR(mgmt->sa));
  614. return;
  615. }
  616. sta->assocreq_capab_info =
  617. le_to_host16(mgmt->u.reassoc_req.capab_info);
  618. sta->assocreq_listen_int =
  619. le_to_host16(mgmt->u.reassoc_req.listen_interval);
  620. os_free(sta->assocreq_ies);
  621. sta->assocreq_ies_len = len - (mgmt->u.reassoc_req.variable - data);
  622. sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
  623. if (sta->assocreq_ies)
  624. os_memcpy(sta->assocreq_ies, mgmt->u.reassoc_req.variable,
  625. sta->assocreq_ies_len);
  626. sta_update_assoc(sta, &elems);
  627. }
  628. static void rx_mgmt_reassoc_resp(struct wlantest *wt, const u8 *data,
  629. size_t len)
  630. {
  631. const struct ieee80211_mgmt *mgmt;
  632. struct wlantest_bss *bss;
  633. struct wlantest_sta *sta;
  634. u16 capab, status, aid;
  635. const u8 *ies;
  636. size_t ies_len;
  637. mgmt = (const struct ieee80211_mgmt *) data;
  638. bss = bss_get(wt, mgmt->bssid);
  639. if (bss == NULL)
  640. return;
  641. sta = sta_get(bss, mgmt->da);
  642. if (sta == NULL)
  643. return;
  644. if (len < 24 + 6) {
  645. add_note(wt, MSG_INFO, "Too short Reassociation Response frame "
  646. "from " MACSTR, MAC2STR(mgmt->sa));
  647. return;
  648. }
  649. ies = mgmt->u.reassoc_resp.variable;
  650. ies_len = len - (mgmt->u.reassoc_resp.variable - data);
  651. capab = le_to_host16(mgmt->u.reassoc_resp.capab_info);
  652. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  653. aid = le_to_host16(mgmt->u.reassoc_resp.aid);
  654. wpa_printf(MSG_DEBUG, "REASSOCRESP " MACSTR " -> " MACSTR
  655. " (capab=0x%x status=%u aid=%u)",
  656. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  657. aid & 0x3fff);
  658. if (sta->auth_alg == WLAN_AUTH_FILS_SK) {
  659. const u8 *session, *frame_ad, *frame_ad_end, *encr_end;
  660. session = get_fils_session(ies, ies_len);
  661. if (session) {
  662. frame_ad = (const u8 *)
  663. &mgmt->u.reassoc_resp.capab_info;
  664. frame_ad_end = session + 2 + session[1];
  665. encr_end = data + len;
  666. decrypt_fils_assoc_resp(wt, bss, sta, data, frame_ad,
  667. frame_ad_end, encr_end);
  668. ies_len = session - ies;
  669. }
  670. }
  671. if (status == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) {
  672. struct ieee802_11_elems elems;
  673. if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
  674. ParseFailed) {
  675. add_note(wt, MSG_INFO, "Failed to parse IEs in "
  676. "ReassocResp from " MACSTR,
  677. MAC2STR(mgmt->sa));
  678. } else if (elems.timeout_int == NULL ||
  679. elems.timeout_int[0] !=
  680. WLAN_TIMEOUT_ASSOC_COMEBACK) {
  681. add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
  682. "with Assoc Comeback time in ReassocResp "
  683. "(status=30) from " MACSTR,
  684. MAC2STR(mgmt->sa));
  685. } else {
  686. sta->counters[
  687. WLANTEST_STA_COUNTER_REASSOCRESP_COMEBACK]++;
  688. }
  689. }
  690. if (status)
  691. return;
  692. if ((aid & 0xc000) != 0xc000) {
  693. add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  694. "in Reassociation Response from " MACSTR,
  695. MAC2STR(mgmt->sa));
  696. }
  697. sta->aid = aid & 0xc000;
  698. if (sta->state < STATE2) {
  699. add_note(wt, MSG_DEBUG,
  700. "STA " MACSTR " was not in State 2 when "
  701. "getting associated", MAC2STR(sta->addr));
  702. }
  703. if (sta->state < STATE3) {
  704. add_note(wt, MSG_DEBUG, "STA " MACSTR
  705. " moved to State 3 with " MACSTR,
  706. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  707. sta->state = STATE3;
  708. }
  709. }
  710. static void disassoc_all_stas(struct wlantest *wt, struct wlantest_bss *bss)
  711. {
  712. struct wlantest_sta *sta;
  713. dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
  714. if (sta->state <= STATE2)
  715. continue;
  716. add_note(wt, MSG_DEBUG, "STA " MACSTR
  717. " moved to State 2 with " MACSTR,
  718. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  719. sta->state = STATE2;
  720. }
  721. }
  722. static void rx_mgmt_disassoc(struct wlantest *wt, const u8 *data, size_t len,
  723. int valid)
  724. {
  725. const struct ieee80211_mgmt *mgmt;
  726. struct wlantest_bss *bss;
  727. struct wlantest_sta *sta;
  728. u16 fc, reason;
  729. mgmt = (const struct ieee80211_mgmt *) data;
  730. bss = bss_get(wt, mgmt->bssid);
  731. if (bss == NULL)
  732. return;
  733. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  734. sta = sta_get(bss, mgmt->da);
  735. else
  736. sta = sta_get(bss, mgmt->sa);
  737. if (len < 24 + 2) {
  738. add_note(wt, MSG_INFO, "Too short Disassociation frame from "
  739. MACSTR, MAC2STR(mgmt->sa));
  740. return;
  741. }
  742. reason = le_to_host16(mgmt->u.disassoc.reason_code);
  743. wpa_printf(MSG_DEBUG, "DISASSOC " MACSTR " -> " MACSTR
  744. " (reason=%u) (valid=%d)",
  745. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  746. reason, valid);
  747. wpa_hexdump(MSG_MSGDUMP, "DISASSOC payload", data + 24, len - 24);
  748. if (sta == NULL) {
  749. if (valid && mgmt->da[0] == 0xff)
  750. disassoc_all_stas(wt, bss);
  751. return;
  752. }
  753. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  754. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_RX :
  755. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX]++;
  756. if (sta->pwrmgt && !sta->pspoll)
  757. sta->counters[
  758. WLANTEST_STA_COUNTER_DISASSOC_RX_ASLEEP]++;
  759. else
  760. sta->counters[
  761. WLANTEST_STA_COUNTER_DISASSOC_RX_AWAKE]++;
  762. fc = le_to_host16(mgmt->frame_control);
  763. if (!(fc & WLAN_FC_ISWEP) && reason == 6)
  764. sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC6]++;
  765. else if (!(fc & WLAN_FC_ISWEP) && reason == 7)
  766. sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC7]++;
  767. } else
  768. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_TX :
  769. WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX]++;
  770. if (!valid) {
  771. add_note(wt, MSG_INFO, "Do not change STA " MACSTR " State "
  772. "since Disassociation frame was not protected "
  773. "correctly", MAC2STR(sta->addr));
  774. return;
  775. }
  776. if (sta->state < STATE2) {
  777. add_note(wt, MSG_DEBUG,
  778. "STA " MACSTR " was not in State 2 or 3 "
  779. "when getting disassociated", MAC2STR(sta->addr));
  780. }
  781. if (sta->state > STATE2) {
  782. add_note(wt, MSG_DEBUG, "STA " MACSTR
  783. " moved to State 2 with " MACSTR,
  784. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  785. sta->state = STATE2;
  786. }
  787. tdls_link_down(wt, bss, sta);
  788. }
  789. static void rx_mgmt_action_sa_query_req(struct wlantest *wt,
  790. struct wlantest_sta *sta,
  791. const struct ieee80211_mgmt *mgmt,
  792. size_t len, int valid)
  793. {
  794. const u8 *rx_id;
  795. u8 *id;
  796. rx_id = (const u8 *) mgmt->u.action.u.sa_query_req.trans_id;
  797. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  798. id = sta->ap_sa_query_tr;
  799. else
  800. id = sta->sta_sa_query_tr;
  801. add_note(wt, MSG_INFO, "SA Query Request " MACSTR " -> " MACSTR
  802. " (trans_id=%02x%02x)%s",
  803. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  804. valid ? "" : " (invalid protection)");
  805. os_memcpy(id, mgmt->u.action.u.sa_query_req.trans_id, 2);
  806. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  807. sta->counters[valid ?
  808. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX :
  809. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX]++;
  810. else
  811. sta->counters[valid ?
  812. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX :
  813. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX]++;
  814. }
  815. static void rx_mgmt_action_sa_query_resp(struct wlantest *wt,
  816. struct wlantest_sta *sta,
  817. const struct ieee80211_mgmt *mgmt,
  818. size_t len, int valid)
  819. {
  820. const u8 *rx_id;
  821. u8 *id;
  822. int match;
  823. rx_id = (const u8 *) mgmt->u.action.u.sa_query_resp.trans_id;
  824. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  825. id = sta->sta_sa_query_tr;
  826. else
  827. id = sta->ap_sa_query_tr;
  828. match = os_memcmp(rx_id, id, 2) == 0;
  829. add_note(wt, MSG_INFO, "SA Query Response " MACSTR " -> " MACSTR
  830. " (trans_id=%02x%02x; %s)%s",
  831. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  832. match ? "match" : "mismatch",
  833. valid ? "" : " (invalid protection)");
  834. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  835. sta->counters[(valid && match) ?
  836. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX :
  837. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX]++;
  838. else
  839. sta->counters[(valid && match) ?
  840. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX :
  841. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX]++;
  842. }
  843. static void rx_mgmt_action_sa_query(struct wlantest *wt,
  844. struct wlantest_sta *sta,
  845. const struct ieee80211_mgmt *mgmt,
  846. size_t len, int valid)
  847. {
  848. if (len < 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  849. add_note(wt, MSG_INFO, "Too short SA Query frame from " MACSTR,
  850. MAC2STR(mgmt->sa));
  851. return;
  852. }
  853. if (len > 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  854. size_t elen = len - (24 + 2 + WLAN_SA_QUERY_TR_ID_LEN);
  855. add_note(wt, MSG_INFO, "Unexpected %u octets of extra data at "
  856. "the end of SA Query frame from " MACSTR,
  857. (unsigned) elen, MAC2STR(mgmt->sa));
  858. wpa_hexdump(MSG_INFO, "SA Query extra data",
  859. ((const u8 *) mgmt) + len - elen, elen);
  860. }
  861. switch (mgmt->u.action.u.sa_query_req.action) {
  862. case WLAN_SA_QUERY_REQUEST:
  863. rx_mgmt_action_sa_query_req(wt, sta, mgmt, len, valid);
  864. break;
  865. case WLAN_SA_QUERY_RESPONSE:
  866. rx_mgmt_action_sa_query_resp(wt, sta, mgmt, len, valid);
  867. break;
  868. default:
  869. add_note(wt, MSG_INFO, "Unexpected SA Query action value %u "
  870. "from " MACSTR,
  871. mgmt->u.action.u.sa_query_req.action,
  872. MAC2STR(mgmt->sa));
  873. }
  874. }
  875. static void rx_mgmt_action(struct wlantest *wt, const u8 *data, size_t len,
  876. int valid)
  877. {
  878. const struct ieee80211_mgmt *mgmt;
  879. struct wlantest_bss *bss;
  880. struct wlantest_sta *sta;
  881. mgmt = (const struct ieee80211_mgmt *) data;
  882. if (mgmt->da[0] & 0x01) {
  883. add_note(wt, MSG_DEBUG, "Group addressed Action frame: DA="
  884. MACSTR " SA=" MACSTR " BSSID=" MACSTR
  885. " category=%u",
  886. MAC2STR(mgmt->da), MAC2STR(mgmt->sa),
  887. MAC2STR(mgmt->bssid), mgmt->u.action.category);
  888. return; /* Ignore group addressed Action frames for now */
  889. }
  890. bss = bss_get(wt, mgmt->bssid);
  891. if (bss == NULL)
  892. return;
  893. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  894. sta = sta_get(bss, mgmt->da);
  895. else
  896. sta = sta_get(bss, mgmt->sa);
  897. if (sta == NULL)
  898. return;
  899. if (len < 24 + 1) {
  900. add_note(wt, MSG_INFO, "Too short Action frame from " MACSTR,
  901. MAC2STR(mgmt->sa));
  902. return;
  903. }
  904. wpa_printf(MSG_DEBUG, "ACTION " MACSTR " -> " MACSTR
  905. " (category=%u) (valid=%d)",
  906. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  907. mgmt->u.action.category, valid);
  908. wpa_hexdump(MSG_MSGDUMP, "ACTION payload", data + 24, len - 24);
  909. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  910. sta->state < STATE3) {
  911. add_note(wt, MSG_INFO, "Action frame sent when STA is not in "
  912. "State 3 (SA=" MACSTR " DATA=" MACSTR ")",
  913. MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  914. }
  915. switch (mgmt->u.action.category) {
  916. case WLAN_ACTION_SA_QUERY:
  917. rx_mgmt_action_sa_query(wt, sta, mgmt, len, valid);
  918. break;
  919. }
  920. }
  921. static int check_mmie_mic(unsigned int mgmt_group_cipher,
  922. const u8 *igtk, size_t igtk_len,
  923. const u8 *data, size_t len)
  924. {
  925. u8 *buf;
  926. u8 mic[16];
  927. u16 fc;
  928. const struct ieee80211_hdr *hdr;
  929. int ret, mic_len;
  930. if (!mgmt_group_cipher || igtk_len < 16)
  931. return -1;
  932. mic_len = mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
  933. if (len < 24 || len - 24 < mic_len)
  934. return -1;
  935. buf = os_malloc(len + 20 - 24);
  936. if (buf == NULL)
  937. return -1;
  938. /* BIP AAD: FC(masked) A1 A2 A3 */
  939. hdr = (const struct ieee80211_hdr *) data;
  940. fc = le_to_host16(hdr->frame_control);
  941. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  942. WPA_PUT_LE16(buf, fc);
  943. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  944. /* Frame body with MMIE MIC masked to zero */
  945. os_memcpy(buf + 20, data + 24, len - 24 - mic_len);
  946. os_memset(buf + 20 + len - 24 - mic_len, 0, mic_len);
  947. wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, len + 20 - 24);
  948. /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
  949. if (mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  950. ret = omac1_aes_128(igtk, buf, len + 20 - 24, mic);
  951. } else if (mgmt_group_cipher == WPA_CIPHER_BIP_CMAC_256) {
  952. ret = omac1_aes_256(igtk, buf, len + 20 - 24, mic);
  953. } else if (mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_128 ||
  954. mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_256) {
  955. u8 nonce[12], *npos;
  956. const u8 *ipn;
  957. ipn = data + len - mic_len - 6;
  958. /* Nonce: A2 | IPN */
  959. os_memcpy(nonce, hdr->addr2, ETH_ALEN);
  960. npos = nonce + ETH_ALEN;
  961. *npos++ = ipn[5];
  962. *npos++ = ipn[4];
  963. *npos++ = ipn[3];
  964. *npos++ = ipn[2];
  965. *npos++ = ipn[1];
  966. *npos++ = ipn[0];
  967. ret = aes_gmac(igtk, igtk_len, nonce, sizeof(nonce),
  968. buf, len + 20 - 24, mic);
  969. } else {
  970. ret = -1;
  971. }
  972. if (ret < 0) {
  973. os_free(buf);
  974. return -1;
  975. }
  976. os_free(buf);
  977. if (os_memcmp(data + len - mic_len, mic, mic_len) != 0)
  978. return -1;
  979. return 0;
  980. }
  981. static int check_bip(struct wlantest *wt, const u8 *data, size_t len)
  982. {
  983. const struct ieee80211_mgmt *mgmt;
  984. u16 fc, stype;
  985. const u8 *mmie;
  986. u16 keyid;
  987. struct wlantest_bss *bss;
  988. size_t mic_len;
  989. mgmt = (const struct ieee80211_mgmt *) data;
  990. fc = le_to_host16(mgmt->frame_control);
  991. stype = WLAN_FC_GET_STYPE(fc);
  992. if (stype == WLAN_FC_STYPE_ACTION) {
  993. if (len < 24 + 1)
  994. return 0;
  995. if (mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  996. return 0; /* Not a robust management frame */
  997. }
  998. bss = bss_get(wt, mgmt->bssid);
  999. if (bss == NULL)
  1000. return 0; /* No key known yet */
  1001. mic_len = bss->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
  1002. if (len < 24 + 10 + mic_len ||
  1003. data[len - (10 + mic_len)] != WLAN_EID_MMIE ||
  1004. data[len - (10 + mic_len - 1)] != 8 + mic_len) {
  1005. /* No MMIE */
  1006. if (bss->rsn_capab & WPA_CAPABILITY_MFPC) {
  1007. add_note(wt, MSG_INFO, "Robust group-addressed "
  1008. "management frame sent without BIP by "
  1009. MACSTR, MAC2STR(mgmt->sa));
  1010. bss->counters[WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE]++;
  1011. return -1;
  1012. }
  1013. return 0;
  1014. }
  1015. mmie = data + len - (8 + mic_len);
  1016. keyid = WPA_GET_LE16(mmie);
  1017. if (keyid & 0xf000) {
  1018. add_note(wt, MSG_INFO, "MMIE KeyID reserved bits not zero "
  1019. "(%04x) from " MACSTR, keyid, MAC2STR(mgmt->sa));
  1020. keyid &= 0x0fff;
  1021. }
  1022. if (keyid < 4 || keyid > 5) {
  1023. add_note(wt, MSG_INFO, "Unexpected MMIE KeyID %u from " MACSTR,
  1024. keyid, MAC2STR(mgmt->sa));
  1025. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  1026. return 0;
  1027. }
  1028. wpa_printf(MSG_DEBUG, "MMIE KeyID %u", keyid);
  1029. wpa_hexdump(MSG_MSGDUMP, "MMIE IPN", mmie + 2, 6);
  1030. wpa_hexdump(MSG_MSGDUMP, "MMIE MIC", mmie + 8, mic_len);
  1031. if (!bss->igtk_len[keyid]) {
  1032. add_note(wt, MSG_DEBUG, "No IGTK known to validate BIP frame");
  1033. return 0;
  1034. }
  1035. if (os_memcmp(mmie + 2, bss->ipn[keyid], 6) <= 0) {
  1036. add_note(wt, MSG_INFO, "BIP replay detected: SA=" MACSTR,
  1037. MAC2STR(mgmt->sa));
  1038. wpa_hexdump(MSG_INFO, "RX IPN", mmie + 2, 6);
  1039. wpa_hexdump(MSG_INFO, "Last RX IPN", bss->ipn[keyid], 6);
  1040. }
  1041. if (check_mmie_mic(bss->mgmt_group_cipher, bss->igtk[keyid],
  1042. bss->igtk_len[keyid], data, len) < 0) {
  1043. add_note(wt, MSG_INFO, "Invalid MMIE MIC in a frame from "
  1044. MACSTR, MAC2STR(mgmt->sa));
  1045. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  1046. return -1;
  1047. }
  1048. add_note(wt, MSG_DEBUG, "Valid MMIE MIC");
  1049. os_memcpy(bss->ipn[keyid], mmie + 2, 6);
  1050. bss->counters[WLANTEST_BSS_COUNTER_VALID_BIP_MMIE]++;
  1051. if (stype == WLAN_FC_STYPE_DEAUTH)
  1052. bss->counters[WLANTEST_BSS_COUNTER_BIP_DEAUTH]++;
  1053. else if (stype == WLAN_FC_STYPE_DISASSOC)
  1054. bss->counters[WLANTEST_BSS_COUNTER_BIP_DISASSOC]++;
  1055. return 0;
  1056. }
  1057. static u8 * mgmt_ccmp_decrypt(struct wlantest *wt, const u8 *data, size_t len,
  1058. size_t *dlen)
  1059. {
  1060. struct wlantest_bss *bss;
  1061. struct wlantest_sta *sta;
  1062. const struct ieee80211_hdr *hdr;
  1063. int keyid;
  1064. u8 *decrypted, *frame = NULL;
  1065. u8 pn[6], *rsc;
  1066. hdr = (const struct ieee80211_hdr *) data;
  1067. bss = bss_get(wt, hdr->addr3);
  1068. if (bss == NULL)
  1069. return NULL;
  1070. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  1071. sta = sta_get(bss, hdr->addr2);
  1072. else
  1073. sta = sta_get(bss, hdr->addr1);
  1074. if (sta == NULL || !sta->ptk_set) {
  1075. add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
  1076. return NULL;
  1077. }
  1078. if (len < 24 + 4)
  1079. return NULL;
  1080. if (!(data[24 + 3] & 0x20)) {
  1081. add_note(wt, MSG_INFO, "Expected CCMP frame from " MACSTR
  1082. " did not have ExtIV bit set to 1",
  1083. MAC2STR(hdr->addr2));
  1084. return NULL;
  1085. }
  1086. if (data[24 + 2] != 0 || (data[24 + 3] & 0x1f) != 0) {
  1087. add_note(wt, MSG_INFO, "CCMP mgmt frame from " MACSTR " used "
  1088. "non-zero reserved bit", MAC2STR(hdr->addr2));
  1089. }
  1090. keyid = data[24 + 3] >> 6;
  1091. if (keyid != 0) {
  1092. add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in "
  1093. "individually addressed Management frame from "
  1094. MACSTR, keyid, MAC2STR(hdr->addr2));
  1095. }
  1096. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  1097. rsc = sta->rsc_tods[16];
  1098. else
  1099. rsc = sta->rsc_fromds[16];
  1100. ccmp_get_pn(pn, data + 24);
  1101. if (os_memcmp(pn, rsc, 6) <= 0) {
  1102. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  1103. add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  1104. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
  1105. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1106. MAC2STR(hdr->addr3),
  1107. WLAN_GET_SEQ_SEQ(seq_ctrl),
  1108. WLAN_GET_SEQ_FRAG(seq_ctrl),
  1109. (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
  1110. " Retry" : "");
  1111. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  1112. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  1113. }
  1114. decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data + 24, len - 24, dlen);
  1115. if (decrypted) {
  1116. os_memcpy(rsc, pn, 6);
  1117. frame = os_malloc(24 + *dlen);
  1118. if (frame) {
  1119. os_memcpy(frame, data, 24);
  1120. os_memcpy(frame + 24, decrypted, *dlen);
  1121. *dlen += 24;
  1122. }
  1123. }
  1124. os_free(decrypted);
  1125. return frame;
  1126. }
  1127. static int check_mgmt_ccmp(struct wlantest *wt, const u8 *data, size_t len)
  1128. {
  1129. const struct ieee80211_mgmt *mgmt;
  1130. u16 fc;
  1131. struct wlantest_bss *bss;
  1132. struct wlantest_sta *sta;
  1133. mgmt = (const struct ieee80211_mgmt *) data;
  1134. fc = le_to_host16(mgmt->frame_control);
  1135. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
  1136. if (len > 24 &&
  1137. mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  1138. return 0; /* Not a robust management frame */
  1139. }
  1140. bss = bss_get(wt, mgmt->bssid);
  1141. if (bss == NULL)
  1142. return 0;
  1143. if (os_memcmp(mgmt->da, mgmt->bssid, ETH_ALEN) == 0)
  1144. sta = sta_get(bss, mgmt->sa);
  1145. else
  1146. sta = sta_get(bss, mgmt->da);
  1147. if (sta == NULL)
  1148. return 0;
  1149. if ((sta->rsn_capab & WPA_CAPABILITY_MFPC) &&
  1150. (sta->state == STATE3 ||
  1151. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION)) {
  1152. add_note(wt, MSG_INFO, "Robust individually-addressed "
  1153. "management frame sent without CCMP by "
  1154. MACSTR, MAC2STR(mgmt->sa));
  1155. return -1;
  1156. }
  1157. return 0;
  1158. }
  1159. void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
  1160. {
  1161. const struct ieee80211_hdr *hdr;
  1162. u16 fc, stype;
  1163. int valid = 1;
  1164. u8 *decrypted = NULL;
  1165. size_t dlen;
  1166. if (len < 24)
  1167. return;
  1168. hdr = (const struct ieee80211_hdr *) data;
  1169. fc = le_to_host16(hdr->frame_control);
  1170. wt->rx_mgmt++;
  1171. stype = WLAN_FC_GET_STYPE(fc);
  1172. if ((hdr->addr1[0] & 0x01) &&
  1173. (stype == WLAN_FC_STYPE_DEAUTH ||
  1174. stype == WLAN_FC_STYPE_DISASSOC ||
  1175. stype == WLAN_FC_STYPE_ACTION)) {
  1176. if (check_bip(wt, data, len) < 0)
  1177. valid = 0;
  1178. }
  1179. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  1180. stype == WLAN_FC_STYPE_PROBE_RESP ||
  1181. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  1182. MSG_EXCESSIVE : MSG_MSGDUMP,
  1183. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  1184. mgmt_stype(stype),
  1185. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  1186. fc & WLAN_FC_ISWEP ? " Prot" : "",
  1187. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1188. MAC2STR(hdr->addr3));
  1189. if ((fc & WLAN_FC_ISWEP) &&
  1190. !(hdr->addr1[0] & 0x01) &&
  1191. (stype == WLAN_FC_STYPE_DEAUTH ||
  1192. stype == WLAN_FC_STYPE_DISASSOC ||
  1193. stype == WLAN_FC_STYPE_ACTION)) {
  1194. decrypted = mgmt_ccmp_decrypt(wt, data, len, &dlen);
  1195. if (decrypted) {
  1196. write_pcap_decrypted(wt, decrypted, dlen, NULL, 0);
  1197. data = decrypted;
  1198. len = dlen;
  1199. } else
  1200. valid = 0;
  1201. }
  1202. if (!(fc & WLAN_FC_ISWEP) &&
  1203. !(hdr->addr1[0] & 0x01) &&
  1204. (stype == WLAN_FC_STYPE_DEAUTH ||
  1205. stype == WLAN_FC_STYPE_DISASSOC ||
  1206. stype == WLAN_FC_STYPE_ACTION)) {
  1207. if (check_mgmt_ccmp(wt, data, len) < 0)
  1208. valid = 0;
  1209. }
  1210. switch (stype) {
  1211. case WLAN_FC_STYPE_BEACON:
  1212. rx_mgmt_beacon(wt, data, len);
  1213. break;
  1214. case WLAN_FC_STYPE_PROBE_RESP:
  1215. rx_mgmt_probe_resp(wt, data, len);
  1216. break;
  1217. case WLAN_FC_STYPE_AUTH:
  1218. rx_mgmt_auth(wt, data, len);
  1219. break;
  1220. case WLAN_FC_STYPE_DEAUTH:
  1221. rx_mgmt_deauth(wt, data, len, valid);
  1222. break;
  1223. case WLAN_FC_STYPE_ASSOC_REQ:
  1224. rx_mgmt_assoc_req(wt, data, len);
  1225. break;
  1226. case WLAN_FC_STYPE_ASSOC_RESP:
  1227. rx_mgmt_assoc_resp(wt, data, len);
  1228. break;
  1229. case WLAN_FC_STYPE_REASSOC_REQ:
  1230. rx_mgmt_reassoc_req(wt, data, len);
  1231. break;
  1232. case WLAN_FC_STYPE_REASSOC_RESP:
  1233. rx_mgmt_reassoc_resp(wt, data, len);
  1234. break;
  1235. case WLAN_FC_STYPE_DISASSOC:
  1236. rx_mgmt_disassoc(wt, data, len, valid);
  1237. break;
  1238. case WLAN_FC_STYPE_ACTION:
  1239. rx_mgmt_action(wt, data, len, valid);
  1240. break;
  1241. }
  1242. os_free(decrypted);
  1243. wt->last_mgmt_valid = valid;
  1244. }
  1245. static void rx_mgmt_deauth_ack(struct wlantest *wt,
  1246. const struct ieee80211_hdr *hdr)
  1247. {
  1248. const struct ieee80211_mgmt *mgmt;
  1249. struct wlantest_bss *bss;
  1250. struct wlantest_sta *sta;
  1251. mgmt = (const struct ieee80211_mgmt *) hdr;
  1252. bss = bss_get(wt, mgmt->bssid);
  1253. if (bss == NULL)
  1254. return;
  1255. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  1256. sta = sta_get(bss, mgmt->da);
  1257. else
  1258. sta = sta_get(bss, mgmt->sa);
  1259. if (sta == NULL)
  1260. return;
  1261. add_note(wt, MSG_DEBUG, "DEAUTH from " MACSTR " acknowledged by "
  1262. MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  1263. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  1264. int c;
  1265. c = wt->last_mgmt_valid ?
  1266. WLANTEST_STA_COUNTER_VALID_DEAUTH_RX_ACK :
  1267. WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX_ACK;
  1268. sta->counters[c]++;
  1269. }
  1270. }
  1271. static void rx_mgmt_disassoc_ack(struct wlantest *wt,
  1272. const struct ieee80211_hdr *hdr)
  1273. {
  1274. const struct ieee80211_mgmt *mgmt;
  1275. struct wlantest_bss *bss;
  1276. struct wlantest_sta *sta;
  1277. mgmt = (const struct ieee80211_mgmt *) hdr;
  1278. bss = bss_get(wt, mgmt->bssid);
  1279. if (bss == NULL)
  1280. return;
  1281. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  1282. sta = sta_get(bss, mgmt->da);
  1283. else
  1284. sta = sta_get(bss, mgmt->sa);
  1285. if (sta == NULL)
  1286. return;
  1287. add_note(wt, MSG_DEBUG, "DISASSOC from " MACSTR " acknowledged by "
  1288. MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  1289. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  1290. int c;
  1291. c = wt->last_mgmt_valid ?
  1292. WLANTEST_STA_COUNTER_VALID_DISASSOC_RX_ACK :
  1293. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX_ACK;
  1294. sta->counters[c]++;
  1295. }
  1296. }
  1297. void rx_mgmt_ack(struct wlantest *wt, const struct ieee80211_hdr *hdr)
  1298. {
  1299. u16 fc, stype;
  1300. fc = le_to_host16(hdr->frame_control);
  1301. stype = WLAN_FC_GET_STYPE(fc);
  1302. wpa_printf(MSG_MSGDUMP, "MGMT ACK: stype=%u a1=" MACSTR " a2=" MACSTR
  1303. " a3=" MACSTR,
  1304. stype, MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1305. MAC2STR(hdr->addr3));
  1306. switch (stype) {
  1307. case WLAN_FC_STYPE_DEAUTH:
  1308. rx_mgmt_deauth_ack(wt, hdr);
  1309. break;
  1310. case WLAN_FC_STYPE_DISASSOC:
  1311. rx_mgmt_disassoc_ack(wt, hdr);
  1312. break;
  1313. }
  1314. }