rx_mgmt.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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, NULL, 0,
  304. &ptk, ick, &ick_len,
  305. sta->key_mgmt, sta->pairwise_cipher,
  306. NULL, NULL) < 0)
  307. return -1;
  308. /* Check AES-SIV decryption with the derived key */
  309. /* AES-SIV AAD vectors */
  310. /* The STA's MAC address */
  311. aad[0] = sta->addr;
  312. aad_len[0] = ETH_ALEN;
  313. /* The AP's BSSID */
  314. aad[1] = bss->bssid;
  315. aad_len[1] = ETH_ALEN;
  316. /* The STA's nonce */
  317. aad[2] = sta->snonce;
  318. aad_len[2] = FILS_NONCE_LEN;
  319. /* The AP's nonce */
  320. aad[3] = sta->anonce;
  321. aad_len[3] = FILS_NONCE_LEN;
  322. /*
  323. * The (Re)Association Request frame from the Capability Information
  324. * field to the FILS Session element (both inclusive).
  325. */
  326. aad[4] = frame_ad;
  327. aad_len[4] = frame_ad_end - frame_ad;
  328. if (encr_end - frame_ad_end < AES_BLOCK_SIZE ||
  329. encr_end - frame_ad_end > sizeof(buf))
  330. return -1;
  331. if (aes_siv_decrypt(ptk.kek, ptk.kek_len,
  332. frame_ad_end, encr_end - frame_ad_end,
  333. 5, aad, aad_len, buf) < 0) {
  334. wpa_printf(MSG_DEBUG,
  335. "FILS: Derived PTK did not match AES-SIV data");
  336. return -1;
  337. }
  338. add_note(wt, MSG_DEBUG, "Derived FILS PTK");
  339. os_memcpy(&sta->ptk, &ptk, sizeof(ptk));
  340. sta->ptk_set = 1;
  341. sta->counters[WLANTEST_STA_COUNTER_PTK_LEARNED]++;
  342. wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
  343. buf, encr_end - frame_ad_end - AES_BLOCK_SIZE);
  344. if (wt->write_pcap_dumper || wt->pcapng) {
  345. write_pcap_decrypted(wt, frame_start,
  346. frame_ad_end - frame_start,
  347. buf,
  348. encr_end - frame_ad_end - AES_BLOCK_SIZE);
  349. }
  350. return 0;
  351. }
  352. static void derive_fils_keys(struct wlantest *wt, struct wlantest_bss *bss,
  353. struct wlantest_sta *sta, const u8 *frame_start,
  354. const u8 *frame_ad, const u8 *frame_ad_end,
  355. const u8 *encr_end)
  356. {
  357. struct wlantest_pmk *pmk;
  358. wpa_printf(MSG_DEBUG, "Trying to derive PTK for " MACSTR
  359. " from FILS rMSK", MAC2STR(sta->addr));
  360. dl_list_for_each(pmk, &bss->pmk, struct wlantest_pmk,
  361. list) {
  362. wpa_printf(MSG_DEBUG, "Try per-BSS PMK");
  363. if (try_rmsk(wt, bss, sta, pmk, frame_start, frame_ad,
  364. frame_ad_end, encr_end) == 0)
  365. return;
  366. }
  367. dl_list_for_each(pmk, &wt->pmk, struct wlantest_pmk, list) {
  368. wpa_printf(MSG_DEBUG, "Try global PMK");
  369. if (try_rmsk(wt, bss, sta, pmk, frame_start, frame_ad,
  370. frame_ad_end, encr_end) == 0)
  371. return;
  372. }
  373. }
  374. static void rx_mgmt_assoc_req(struct wlantest *wt, const u8 *data, size_t len)
  375. {
  376. const struct ieee80211_mgmt *mgmt;
  377. struct wlantest_bss *bss;
  378. struct wlantest_sta *sta;
  379. struct ieee802_11_elems elems;
  380. const u8 *ie;
  381. size_t ie_len;
  382. mgmt = (const struct ieee80211_mgmt *) data;
  383. bss = bss_get(wt, mgmt->bssid);
  384. if (bss == NULL)
  385. return;
  386. sta = sta_get(bss, mgmt->sa);
  387. if (sta == NULL)
  388. return;
  389. if (len < 24 + 4) {
  390. add_note(wt, MSG_INFO, "Too short Association Request frame "
  391. "from " MACSTR, MAC2STR(mgmt->sa));
  392. return;
  393. }
  394. wpa_printf(MSG_DEBUG, "ASSOCREQ " MACSTR " -> " MACSTR
  395. " (capab=0x%x listen_int=%u)",
  396. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  397. le_to_host16(mgmt->u.assoc_req.capab_info),
  398. le_to_host16(mgmt->u.assoc_req.listen_interval));
  399. sta->counters[WLANTEST_STA_COUNTER_ASSOCREQ_TX]++;
  400. ie = mgmt->u.assoc_req.variable;
  401. ie_len = len - (mgmt->u.assoc_req.variable - data);
  402. if (sta->auth_alg == WLAN_AUTH_FILS_SK) {
  403. const u8 *session, *frame_ad, *frame_ad_end, *encr_end;
  404. session = get_fils_session(ie, ie_len);
  405. if (session) {
  406. frame_ad = (const u8 *) &mgmt->u.assoc_req.capab_info;
  407. frame_ad_end = session + 2 + session[1];
  408. encr_end = data + len;
  409. derive_fils_keys(wt, bss, sta, data, frame_ad,
  410. frame_ad_end, encr_end);
  411. ie_len = session - ie;
  412. }
  413. }
  414. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  415. add_note(wt, MSG_INFO, "Invalid IEs in Association Request "
  416. "frame from " MACSTR, MAC2STR(mgmt->sa));
  417. return;
  418. }
  419. sta->assocreq_capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  420. sta->assocreq_listen_int =
  421. le_to_host16(mgmt->u.assoc_req.listen_interval);
  422. os_free(sta->assocreq_ies);
  423. sta->assocreq_ies_len = len - (mgmt->u.assoc_req.variable - data);
  424. sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
  425. if (sta->assocreq_ies)
  426. os_memcpy(sta->assocreq_ies, mgmt->u.assoc_req.variable,
  427. sta->assocreq_ies_len);
  428. sta_update_assoc(sta, &elems);
  429. }
  430. static void decrypt_fils_assoc_resp(struct wlantest *wt,
  431. struct wlantest_bss *bss,
  432. struct wlantest_sta *sta,
  433. const u8 *frame_start, const u8 *frame_ad,
  434. const u8 *frame_ad_end, const u8 *encr_end)
  435. {
  436. const u8 *aad[5];
  437. size_t aad_len[5];
  438. u8 buf[2000];
  439. if (!sta->ptk_set)
  440. return;
  441. /* Check AES-SIV decryption with the derived key */
  442. /* AES-SIV AAD vectors */
  443. /* The AP's BSSID */
  444. aad[0] = bss->bssid;
  445. aad_len[0] = ETH_ALEN;
  446. /* The STA's MAC address */
  447. aad[1] = sta->addr;
  448. aad_len[1] = ETH_ALEN;
  449. /* The AP's nonce */
  450. aad[2] = sta->anonce;
  451. aad_len[2] = FILS_NONCE_LEN;
  452. /* The STA's nonce */
  453. aad[3] = sta->snonce;
  454. aad_len[3] = FILS_NONCE_LEN;
  455. /*
  456. * The (Re)Association Response frame from the Capability Information
  457. * field to the FILS Session element (both inclusive).
  458. */
  459. aad[4] = frame_ad;
  460. aad_len[4] = frame_ad_end - frame_ad;
  461. if (encr_end - frame_ad_end < AES_BLOCK_SIZE ||
  462. encr_end - frame_ad_end > sizeof(buf))
  463. return;
  464. if (aes_siv_decrypt(sta->ptk.kek, sta->ptk.kek_len,
  465. frame_ad_end, encr_end - frame_ad_end,
  466. 5, aad, aad_len, buf) < 0) {
  467. wpa_printf(MSG_DEBUG,
  468. "FILS: Derived PTK did not match AES-SIV data");
  469. return;
  470. }
  471. wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Response elements",
  472. buf, encr_end - frame_ad_end - AES_BLOCK_SIZE);
  473. if (wt->write_pcap_dumper || wt->pcapng) {
  474. write_pcap_decrypted(wt, frame_start,
  475. frame_ad_end - frame_start,
  476. buf,
  477. encr_end - frame_ad_end - AES_BLOCK_SIZE);
  478. }
  479. }
  480. static void rx_mgmt_assoc_resp(struct wlantest *wt, const u8 *data, size_t len)
  481. {
  482. const struct ieee80211_mgmt *mgmt;
  483. struct wlantest_bss *bss;
  484. struct wlantest_sta *sta;
  485. u16 capab, status, aid;
  486. const u8 *ies;
  487. size_t ies_len;
  488. struct wpa_ft_ies parse;
  489. mgmt = (const struct ieee80211_mgmt *) data;
  490. bss = bss_get(wt, mgmt->bssid);
  491. if (bss == NULL)
  492. return;
  493. sta = sta_get(bss, mgmt->da);
  494. if (sta == NULL)
  495. return;
  496. if (len < 24 + 6) {
  497. add_note(wt, MSG_INFO, "Too short Association Response frame "
  498. "from " MACSTR, MAC2STR(mgmt->sa));
  499. return;
  500. }
  501. ies = mgmt->u.assoc_resp.variable;
  502. ies_len = len - (mgmt->u.assoc_resp.variable - data);
  503. capab = le_to_host16(mgmt->u.assoc_resp.capab_info);
  504. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  505. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  506. wpa_printf(MSG_DEBUG, "ASSOCRESP " MACSTR " -> " MACSTR
  507. " (capab=0x%x status=%u aid=%u)",
  508. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  509. aid & 0x3fff);
  510. if (sta->auth_alg == WLAN_AUTH_FILS_SK) {
  511. const u8 *session, *frame_ad, *frame_ad_end, *encr_end;
  512. session = get_fils_session(ies, ies_len);
  513. if (session) {
  514. frame_ad = (const u8 *) &mgmt->u.assoc_resp.capab_info;
  515. frame_ad_end = session + 2 + session[1];
  516. encr_end = data + len;
  517. decrypt_fils_assoc_resp(wt, bss, sta, data, frame_ad,
  518. frame_ad_end, encr_end);
  519. ies_len = session - ies;
  520. }
  521. }
  522. if (status == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) {
  523. struct ieee802_11_elems elems;
  524. if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
  525. ParseFailed) {
  526. add_note(wt, MSG_INFO, "Failed to parse IEs in "
  527. "AssocResp from " MACSTR,
  528. MAC2STR(mgmt->sa));
  529. } else if (elems.timeout_int == NULL ||
  530. elems.timeout_int[0] !=
  531. WLAN_TIMEOUT_ASSOC_COMEBACK) {
  532. add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
  533. "with Assoc Comeback time in AssocResp "
  534. "(status=30) from " MACSTR,
  535. MAC2STR(mgmt->sa));
  536. } else {
  537. sta->counters[
  538. WLANTEST_STA_COUNTER_ASSOCRESP_COMEBACK]++;
  539. }
  540. }
  541. if (status)
  542. return;
  543. if ((aid & 0xc000) != 0xc000) {
  544. add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  545. "in Association Response from " MACSTR,
  546. MAC2STR(mgmt->sa));
  547. }
  548. sta->aid = aid & 0xc000;
  549. if (sta->state < STATE2) {
  550. add_note(wt, MSG_DEBUG,
  551. "STA " MACSTR " was not in State 2 when "
  552. "getting associated", MAC2STR(sta->addr));
  553. }
  554. if (sta->state < STATE3) {
  555. add_note(wt, MSG_DEBUG, "STA " MACSTR
  556. " moved to State 3 with " MACSTR,
  557. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  558. sta->state = STATE3;
  559. }
  560. if (wpa_ft_parse_ies(ies, ies_len, &parse) == 0) {
  561. if (parse.r0kh_id) {
  562. os_memcpy(bss->r0kh_id, parse.r0kh_id,
  563. parse.r0kh_id_len);
  564. bss->r0kh_id_len = parse.r0kh_id_len;
  565. }
  566. if (parse.r1kh_id)
  567. os_memcpy(bss->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
  568. }
  569. }
  570. static void rx_mgmt_reassoc_req(struct wlantest *wt, const u8 *data,
  571. size_t len)
  572. {
  573. const struct ieee80211_mgmt *mgmt;
  574. struct wlantest_bss *bss;
  575. struct wlantest_sta *sta;
  576. struct ieee802_11_elems elems;
  577. const u8 *ie;
  578. size_t ie_len;
  579. mgmt = (const struct ieee80211_mgmt *) data;
  580. bss = bss_get(wt, mgmt->bssid);
  581. if (bss == NULL)
  582. return;
  583. sta = sta_get(bss, mgmt->sa);
  584. if (sta == NULL)
  585. return;
  586. if (len < 24 + 4 + ETH_ALEN) {
  587. add_note(wt, MSG_INFO, "Too short Reassociation Request frame "
  588. "from " MACSTR, MAC2STR(mgmt->sa));
  589. return;
  590. }
  591. wpa_printf(MSG_DEBUG, "REASSOCREQ " MACSTR " -> " MACSTR
  592. " (capab=0x%x listen_int=%u current_ap=" MACSTR ")",
  593. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  594. le_to_host16(mgmt->u.reassoc_req.capab_info),
  595. le_to_host16(mgmt->u.reassoc_req.listen_interval),
  596. MAC2STR(mgmt->u.reassoc_req.current_ap));
  597. sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX]++;
  598. ie = mgmt->u.reassoc_req.variable;
  599. ie_len = len - (mgmt->u.reassoc_req.variable - data);
  600. if (sta->auth_alg == WLAN_AUTH_FILS_SK) {
  601. const u8 *session, *frame_ad, *frame_ad_end, *encr_end;
  602. session = get_fils_session(ie, ie_len);
  603. if (session) {
  604. frame_ad = (const u8 *) &mgmt->u.reassoc_req.capab_info;
  605. frame_ad_end = session + 2 + session[1];
  606. encr_end = data + len;
  607. derive_fils_keys(wt, bss, sta, data, frame_ad,
  608. frame_ad_end, encr_end);
  609. ie_len = session - ie;
  610. }
  611. }
  612. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  613. add_note(wt, MSG_INFO, "Invalid IEs in Reassociation Request "
  614. "frame from " MACSTR, MAC2STR(mgmt->sa));
  615. return;
  616. }
  617. sta->assocreq_capab_info =
  618. le_to_host16(mgmt->u.reassoc_req.capab_info);
  619. sta->assocreq_listen_int =
  620. le_to_host16(mgmt->u.reassoc_req.listen_interval);
  621. os_free(sta->assocreq_ies);
  622. sta->assocreq_ies_len = len - (mgmt->u.reassoc_req.variable - data);
  623. sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
  624. if (sta->assocreq_ies)
  625. os_memcpy(sta->assocreq_ies, mgmt->u.reassoc_req.variable,
  626. sta->assocreq_ies_len);
  627. sta_update_assoc(sta, &elems);
  628. }
  629. static void rx_mgmt_reassoc_resp(struct wlantest *wt, const u8 *data,
  630. size_t len)
  631. {
  632. const struct ieee80211_mgmt *mgmt;
  633. struct wlantest_bss *bss;
  634. struct wlantest_sta *sta;
  635. u16 capab, status, aid;
  636. const u8 *ies;
  637. size_t ies_len;
  638. mgmt = (const struct ieee80211_mgmt *) data;
  639. bss = bss_get(wt, mgmt->bssid);
  640. if (bss == NULL)
  641. return;
  642. sta = sta_get(bss, mgmt->da);
  643. if (sta == NULL)
  644. return;
  645. if (len < 24 + 6) {
  646. add_note(wt, MSG_INFO, "Too short Reassociation Response frame "
  647. "from " MACSTR, MAC2STR(mgmt->sa));
  648. return;
  649. }
  650. ies = mgmt->u.reassoc_resp.variable;
  651. ies_len = len - (mgmt->u.reassoc_resp.variable - data);
  652. capab = le_to_host16(mgmt->u.reassoc_resp.capab_info);
  653. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  654. aid = le_to_host16(mgmt->u.reassoc_resp.aid);
  655. wpa_printf(MSG_DEBUG, "REASSOCRESP " MACSTR " -> " MACSTR
  656. " (capab=0x%x status=%u aid=%u)",
  657. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  658. aid & 0x3fff);
  659. if (sta->auth_alg == WLAN_AUTH_FILS_SK) {
  660. const u8 *session, *frame_ad, *frame_ad_end, *encr_end;
  661. session = get_fils_session(ies, ies_len);
  662. if (session) {
  663. frame_ad = (const u8 *)
  664. &mgmt->u.reassoc_resp.capab_info;
  665. frame_ad_end = session + 2 + session[1];
  666. encr_end = data + len;
  667. decrypt_fils_assoc_resp(wt, bss, sta, data, frame_ad,
  668. frame_ad_end, encr_end);
  669. ies_len = session - ies;
  670. }
  671. }
  672. if (status == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) {
  673. struct ieee802_11_elems elems;
  674. if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
  675. ParseFailed) {
  676. add_note(wt, MSG_INFO, "Failed to parse IEs in "
  677. "ReassocResp from " MACSTR,
  678. MAC2STR(mgmt->sa));
  679. } else if (elems.timeout_int == NULL ||
  680. elems.timeout_int[0] !=
  681. WLAN_TIMEOUT_ASSOC_COMEBACK) {
  682. add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
  683. "with Assoc Comeback time in ReassocResp "
  684. "(status=30) from " MACSTR,
  685. MAC2STR(mgmt->sa));
  686. } else {
  687. sta->counters[
  688. WLANTEST_STA_COUNTER_REASSOCRESP_COMEBACK]++;
  689. }
  690. }
  691. if (status)
  692. return;
  693. if ((aid & 0xc000) != 0xc000) {
  694. add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  695. "in Reassociation Response from " MACSTR,
  696. MAC2STR(mgmt->sa));
  697. }
  698. sta->aid = aid & 0xc000;
  699. if (sta->state < STATE2) {
  700. add_note(wt, MSG_DEBUG,
  701. "STA " MACSTR " was not in State 2 when "
  702. "getting associated", MAC2STR(sta->addr));
  703. }
  704. if (sta->state < STATE3) {
  705. add_note(wt, MSG_DEBUG, "STA " MACSTR
  706. " moved to State 3 with " MACSTR,
  707. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  708. sta->state = STATE3;
  709. }
  710. }
  711. static void disassoc_all_stas(struct wlantest *wt, struct wlantest_bss *bss)
  712. {
  713. struct wlantest_sta *sta;
  714. dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
  715. if (sta->state <= STATE2)
  716. continue;
  717. add_note(wt, MSG_DEBUG, "STA " MACSTR
  718. " moved to State 2 with " MACSTR,
  719. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  720. sta->state = STATE2;
  721. }
  722. }
  723. static void rx_mgmt_disassoc(struct wlantest *wt, const u8 *data, size_t len,
  724. int valid)
  725. {
  726. const struct ieee80211_mgmt *mgmt;
  727. struct wlantest_bss *bss;
  728. struct wlantest_sta *sta;
  729. u16 fc, reason;
  730. mgmt = (const struct ieee80211_mgmt *) data;
  731. bss = bss_get(wt, mgmt->bssid);
  732. if (bss == NULL)
  733. return;
  734. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  735. sta = sta_get(bss, mgmt->da);
  736. else
  737. sta = sta_get(bss, mgmt->sa);
  738. if (len < 24 + 2) {
  739. add_note(wt, MSG_INFO, "Too short Disassociation frame from "
  740. MACSTR, MAC2STR(mgmt->sa));
  741. return;
  742. }
  743. reason = le_to_host16(mgmt->u.disassoc.reason_code);
  744. wpa_printf(MSG_DEBUG, "DISASSOC " MACSTR " -> " MACSTR
  745. " (reason=%u) (valid=%d)",
  746. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  747. reason, valid);
  748. wpa_hexdump(MSG_MSGDUMP, "DISASSOC payload", data + 24, len - 24);
  749. if (sta == NULL) {
  750. if (valid && mgmt->da[0] == 0xff)
  751. disassoc_all_stas(wt, bss);
  752. return;
  753. }
  754. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  755. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_RX :
  756. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX]++;
  757. if (sta->pwrmgt && !sta->pspoll)
  758. sta->counters[
  759. WLANTEST_STA_COUNTER_DISASSOC_RX_ASLEEP]++;
  760. else
  761. sta->counters[
  762. WLANTEST_STA_COUNTER_DISASSOC_RX_AWAKE]++;
  763. fc = le_to_host16(mgmt->frame_control);
  764. if (!(fc & WLAN_FC_ISWEP) && reason == 6)
  765. sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC6]++;
  766. else if (!(fc & WLAN_FC_ISWEP) && reason == 7)
  767. sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC7]++;
  768. } else
  769. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_TX :
  770. WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX]++;
  771. if (!valid) {
  772. add_note(wt, MSG_INFO, "Do not change STA " MACSTR " State "
  773. "since Disassociation frame was not protected "
  774. "correctly", MAC2STR(sta->addr));
  775. return;
  776. }
  777. if (sta->state < STATE2) {
  778. add_note(wt, MSG_DEBUG,
  779. "STA " MACSTR " was not in State 2 or 3 "
  780. "when getting disassociated", MAC2STR(sta->addr));
  781. }
  782. if (sta->state > STATE2) {
  783. add_note(wt, MSG_DEBUG, "STA " MACSTR
  784. " moved to State 2 with " MACSTR,
  785. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  786. sta->state = STATE2;
  787. }
  788. tdls_link_down(wt, bss, sta);
  789. }
  790. static void rx_mgmt_action_sa_query_req(struct wlantest *wt,
  791. struct wlantest_sta *sta,
  792. const struct ieee80211_mgmt *mgmt,
  793. size_t len, int valid)
  794. {
  795. const u8 *rx_id;
  796. u8 *id;
  797. rx_id = (const u8 *) mgmt->u.action.u.sa_query_req.trans_id;
  798. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  799. id = sta->ap_sa_query_tr;
  800. else
  801. id = sta->sta_sa_query_tr;
  802. add_note(wt, MSG_INFO, "SA Query Request " MACSTR " -> " MACSTR
  803. " (trans_id=%02x%02x)%s",
  804. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  805. valid ? "" : " (invalid protection)");
  806. os_memcpy(id, mgmt->u.action.u.sa_query_req.trans_id, 2);
  807. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  808. sta->counters[valid ?
  809. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX :
  810. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX]++;
  811. else
  812. sta->counters[valid ?
  813. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX :
  814. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX]++;
  815. }
  816. static void rx_mgmt_action_sa_query_resp(struct wlantest *wt,
  817. struct wlantest_sta *sta,
  818. const struct ieee80211_mgmt *mgmt,
  819. size_t len, int valid)
  820. {
  821. const u8 *rx_id;
  822. u8 *id;
  823. int match;
  824. rx_id = (const u8 *) mgmt->u.action.u.sa_query_resp.trans_id;
  825. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  826. id = sta->sta_sa_query_tr;
  827. else
  828. id = sta->ap_sa_query_tr;
  829. match = os_memcmp(rx_id, id, 2) == 0;
  830. add_note(wt, MSG_INFO, "SA Query Response " MACSTR " -> " MACSTR
  831. " (trans_id=%02x%02x; %s)%s",
  832. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  833. match ? "match" : "mismatch",
  834. valid ? "" : " (invalid protection)");
  835. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  836. sta->counters[(valid && match) ?
  837. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX :
  838. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX]++;
  839. else
  840. sta->counters[(valid && match) ?
  841. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX :
  842. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX]++;
  843. }
  844. static void rx_mgmt_action_sa_query(struct wlantest *wt,
  845. struct wlantest_sta *sta,
  846. const struct ieee80211_mgmt *mgmt,
  847. size_t len, int valid)
  848. {
  849. if (len < 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  850. add_note(wt, MSG_INFO, "Too short SA Query frame from " MACSTR,
  851. MAC2STR(mgmt->sa));
  852. return;
  853. }
  854. if (len > 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  855. size_t elen = len - (24 + 2 + WLAN_SA_QUERY_TR_ID_LEN);
  856. add_note(wt, MSG_INFO, "Unexpected %u octets of extra data at "
  857. "the end of SA Query frame from " MACSTR,
  858. (unsigned) elen, MAC2STR(mgmt->sa));
  859. wpa_hexdump(MSG_INFO, "SA Query extra data",
  860. ((const u8 *) mgmt) + len - elen, elen);
  861. }
  862. switch (mgmt->u.action.u.sa_query_req.action) {
  863. case WLAN_SA_QUERY_REQUEST:
  864. rx_mgmt_action_sa_query_req(wt, sta, mgmt, len, valid);
  865. break;
  866. case WLAN_SA_QUERY_RESPONSE:
  867. rx_mgmt_action_sa_query_resp(wt, sta, mgmt, len, valid);
  868. break;
  869. default:
  870. add_note(wt, MSG_INFO, "Unexpected SA Query action value %u "
  871. "from " MACSTR,
  872. mgmt->u.action.u.sa_query_req.action,
  873. MAC2STR(mgmt->sa));
  874. }
  875. }
  876. static void rx_mgmt_action(struct wlantest *wt, const u8 *data, size_t len,
  877. int valid)
  878. {
  879. const struct ieee80211_mgmt *mgmt;
  880. struct wlantest_bss *bss;
  881. struct wlantest_sta *sta;
  882. mgmt = (const struct ieee80211_mgmt *) data;
  883. if (mgmt->da[0] & 0x01) {
  884. add_note(wt, MSG_DEBUG, "Group addressed Action frame: DA="
  885. MACSTR " SA=" MACSTR " BSSID=" MACSTR
  886. " category=%u",
  887. MAC2STR(mgmt->da), MAC2STR(mgmt->sa),
  888. MAC2STR(mgmt->bssid), mgmt->u.action.category);
  889. return; /* Ignore group addressed Action frames for now */
  890. }
  891. bss = bss_get(wt, mgmt->bssid);
  892. if (bss == NULL)
  893. return;
  894. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  895. sta = sta_get(bss, mgmt->da);
  896. else
  897. sta = sta_get(bss, mgmt->sa);
  898. if (sta == NULL)
  899. return;
  900. if (len < 24 + 1) {
  901. add_note(wt, MSG_INFO, "Too short Action frame from " MACSTR,
  902. MAC2STR(mgmt->sa));
  903. return;
  904. }
  905. wpa_printf(MSG_DEBUG, "ACTION " MACSTR " -> " MACSTR
  906. " (category=%u) (valid=%d)",
  907. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  908. mgmt->u.action.category, valid);
  909. wpa_hexdump(MSG_MSGDUMP, "ACTION payload", data + 24, len - 24);
  910. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  911. sta->state < STATE3) {
  912. add_note(wt, MSG_INFO, "Action frame sent when STA is not in "
  913. "State 3 (SA=" MACSTR " DATA=" MACSTR ")",
  914. MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  915. }
  916. switch (mgmt->u.action.category) {
  917. case WLAN_ACTION_SA_QUERY:
  918. rx_mgmt_action_sa_query(wt, sta, mgmt, len, valid);
  919. break;
  920. }
  921. }
  922. static int check_mmie_mic(unsigned int mgmt_group_cipher,
  923. const u8 *igtk, size_t igtk_len,
  924. const u8 *data, size_t len)
  925. {
  926. u8 *buf;
  927. u8 mic[16];
  928. u16 fc;
  929. const struct ieee80211_hdr *hdr;
  930. int ret, mic_len;
  931. if (!mgmt_group_cipher || igtk_len < 16)
  932. return -1;
  933. mic_len = mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
  934. if (len < 24 || len - 24 < mic_len)
  935. return -1;
  936. buf = os_malloc(len + 20 - 24);
  937. if (buf == NULL)
  938. return -1;
  939. /* BIP AAD: FC(masked) A1 A2 A3 */
  940. hdr = (const struct ieee80211_hdr *) data;
  941. fc = le_to_host16(hdr->frame_control);
  942. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  943. WPA_PUT_LE16(buf, fc);
  944. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  945. /* Frame body with MMIE MIC masked to zero */
  946. os_memcpy(buf + 20, data + 24, len - 24 - mic_len);
  947. os_memset(buf + 20 + len - 24 - mic_len, 0, mic_len);
  948. wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, len + 20 - 24);
  949. /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
  950. if (mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  951. ret = omac1_aes_128(igtk, buf, len + 20 - 24, mic);
  952. } else if (mgmt_group_cipher == WPA_CIPHER_BIP_CMAC_256) {
  953. ret = omac1_aes_256(igtk, buf, len + 20 - 24, mic);
  954. } else if (mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_128 ||
  955. mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_256) {
  956. u8 nonce[12], *npos;
  957. const u8 *ipn;
  958. ipn = data + len - mic_len - 6;
  959. /* Nonce: A2 | IPN */
  960. os_memcpy(nonce, hdr->addr2, ETH_ALEN);
  961. npos = nonce + ETH_ALEN;
  962. *npos++ = ipn[5];
  963. *npos++ = ipn[4];
  964. *npos++ = ipn[3];
  965. *npos++ = ipn[2];
  966. *npos++ = ipn[1];
  967. *npos++ = ipn[0];
  968. ret = aes_gmac(igtk, igtk_len, nonce, sizeof(nonce),
  969. buf, len + 20 - 24, mic);
  970. } else {
  971. ret = -1;
  972. }
  973. if (ret < 0) {
  974. os_free(buf);
  975. return -1;
  976. }
  977. os_free(buf);
  978. if (os_memcmp(data + len - mic_len, mic, mic_len) != 0)
  979. return -1;
  980. return 0;
  981. }
  982. static int check_bip(struct wlantest *wt, const u8 *data, size_t len)
  983. {
  984. const struct ieee80211_mgmt *mgmt;
  985. u16 fc, stype;
  986. const u8 *mmie;
  987. u16 keyid;
  988. struct wlantest_bss *bss;
  989. size_t mic_len;
  990. mgmt = (const struct ieee80211_mgmt *) data;
  991. fc = le_to_host16(mgmt->frame_control);
  992. stype = WLAN_FC_GET_STYPE(fc);
  993. if (stype == WLAN_FC_STYPE_ACTION) {
  994. if (len < 24 + 1)
  995. return 0;
  996. if (mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  997. return 0; /* Not a robust management frame */
  998. }
  999. bss = bss_get(wt, mgmt->bssid);
  1000. if (bss == NULL)
  1001. return 0; /* No key known yet */
  1002. mic_len = bss->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
  1003. if (len < 24 + 10 + mic_len ||
  1004. data[len - (10 + mic_len)] != WLAN_EID_MMIE ||
  1005. data[len - (10 + mic_len - 1)] != 8 + mic_len) {
  1006. /* No MMIE */
  1007. if (bss->rsn_capab & WPA_CAPABILITY_MFPC) {
  1008. add_note(wt, MSG_INFO, "Robust group-addressed "
  1009. "management frame sent without BIP by "
  1010. MACSTR, MAC2STR(mgmt->sa));
  1011. bss->counters[WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE]++;
  1012. return -1;
  1013. }
  1014. return 0;
  1015. }
  1016. mmie = data + len - (8 + mic_len);
  1017. keyid = WPA_GET_LE16(mmie);
  1018. if (keyid & 0xf000) {
  1019. add_note(wt, MSG_INFO, "MMIE KeyID reserved bits not zero "
  1020. "(%04x) from " MACSTR, keyid, MAC2STR(mgmt->sa));
  1021. keyid &= 0x0fff;
  1022. }
  1023. if (keyid < 4 || keyid > 5) {
  1024. add_note(wt, MSG_INFO, "Unexpected MMIE KeyID %u from " MACSTR,
  1025. keyid, MAC2STR(mgmt->sa));
  1026. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  1027. return 0;
  1028. }
  1029. wpa_printf(MSG_DEBUG, "MMIE KeyID %u", keyid);
  1030. wpa_hexdump(MSG_MSGDUMP, "MMIE IPN", mmie + 2, 6);
  1031. wpa_hexdump(MSG_MSGDUMP, "MMIE MIC", mmie + 8, mic_len);
  1032. if (!bss->igtk_len[keyid]) {
  1033. add_note(wt, MSG_DEBUG, "No IGTK known to validate BIP frame");
  1034. return 0;
  1035. }
  1036. if (os_memcmp(mmie + 2, bss->ipn[keyid], 6) <= 0) {
  1037. add_note(wt, MSG_INFO, "BIP replay detected: SA=" MACSTR,
  1038. MAC2STR(mgmt->sa));
  1039. wpa_hexdump(MSG_INFO, "RX IPN", mmie + 2, 6);
  1040. wpa_hexdump(MSG_INFO, "Last RX IPN", bss->ipn[keyid], 6);
  1041. }
  1042. if (check_mmie_mic(bss->mgmt_group_cipher, bss->igtk[keyid],
  1043. bss->igtk_len[keyid], data, len) < 0) {
  1044. add_note(wt, MSG_INFO, "Invalid MMIE MIC in a frame from "
  1045. MACSTR, MAC2STR(mgmt->sa));
  1046. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  1047. return -1;
  1048. }
  1049. add_note(wt, MSG_DEBUG, "Valid MMIE MIC");
  1050. os_memcpy(bss->ipn[keyid], mmie + 2, 6);
  1051. bss->counters[WLANTEST_BSS_COUNTER_VALID_BIP_MMIE]++;
  1052. if (stype == WLAN_FC_STYPE_DEAUTH)
  1053. bss->counters[WLANTEST_BSS_COUNTER_BIP_DEAUTH]++;
  1054. else if (stype == WLAN_FC_STYPE_DISASSOC)
  1055. bss->counters[WLANTEST_BSS_COUNTER_BIP_DISASSOC]++;
  1056. return 0;
  1057. }
  1058. static u8 * mgmt_ccmp_decrypt(struct wlantest *wt, const u8 *data, size_t len,
  1059. size_t *dlen)
  1060. {
  1061. struct wlantest_bss *bss;
  1062. struct wlantest_sta *sta;
  1063. const struct ieee80211_hdr *hdr;
  1064. int keyid;
  1065. u8 *decrypted, *frame = NULL;
  1066. u8 pn[6], *rsc;
  1067. hdr = (const struct ieee80211_hdr *) data;
  1068. bss = bss_get(wt, hdr->addr3);
  1069. if (bss == NULL)
  1070. return NULL;
  1071. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  1072. sta = sta_get(bss, hdr->addr2);
  1073. else
  1074. sta = sta_get(bss, hdr->addr1);
  1075. if (sta == NULL || !sta->ptk_set) {
  1076. add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
  1077. return NULL;
  1078. }
  1079. if (len < 24 + 4)
  1080. return NULL;
  1081. if (!(data[24 + 3] & 0x20)) {
  1082. add_note(wt, MSG_INFO, "Expected CCMP frame from " MACSTR
  1083. " did not have ExtIV bit set to 1",
  1084. MAC2STR(hdr->addr2));
  1085. return NULL;
  1086. }
  1087. if (data[24 + 2] != 0 || (data[24 + 3] & 0x1f) != 0) {
  1088. add_note(wt, MSG_INFO, "CCMP mgmt frame from " MACSTR " used "
  1089. "non-zero reserved bit", MAC2STR(hdr->addr2));
  1090. }
  1091. keyid = data[24 + 3] >> 6;
  1092. if (keyid != 0) {
  1093. add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in "
  1094. "individually addressed Management frame from "
  1095. MACSTR, keyid, MAC2STR(hdr->addr2));
  1096. }
  1097. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  1098. rsc = sta->rsc_tods[16];
  1099. else
  1100. rsc = sta->rsc_fromds[16];
  1101. ccmp_get_pn(pn, data + 24);
  1102. if (os_memcmp(pn, rsc, 6) <= 0) {
  1103. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  1104. add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  1105. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
  1106. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1107. MAC2STR(hdr->addr3),
  1108. WLAN_GET_SEQ_SEQ(seq_ctrl),
  1109. WLAN_GET_SEQ_FRAG(seq_ctrl),
  1110. (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
  1111. " Retry" : "");
  1112. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  1113. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  1114. }
  1115. decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data + 24, len - 24, dlen);
  1116. if (decrypted) {
  1117. os_memcpy(rsc, pn, 6);
  1118. frame = os_malloc(24 + *dlen);
  1119. if (frame) {
  1120. os_memcpy(frame, data, 24);
  1121. os_memcpy(frame + 24, decrypted, *dlen);
  1122. *dlen += 24;
  1123. }
  1124. }
  1125. os_free(decrypted);
  1126. return frame;
  1127. }
  1128. static int check_mgmt_ccmp(struct wlantest *wt, const u8 *data, size_t len)
  1129. {
  1130. const struct ieee80211_mgmt *mgmt;
  1131. u16 fc;
  1132. struct wlantest_bss *bss;
  1133. struct wlantest_sta *sta;
  1134. mgmt = (const struct ieee80211_mgmt *) data;
  1135. fc = le_to_host16(mgmt->frame_control);
  1136. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
  1137. if (len > 24 &&
  1138. mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  1139. return 0; /* Not a robust management frame */
  1140. }
  1141. bss = bss_get(wt, mgmt->bssid);
  1142. if (bss == NULL)
  1143. return 0;
  1144. if (os_memcmp(mgmt->da, mgmt->bssid, ETH_ALEN) == 0)
  1145. sta = sta_get(bss, mgmt->sa);
  1146. else
  1147. sta = sta_get(bss, mgmt->da);
  1148. if (sta == NULL)
  1149. return 0;
  1150. if ((sta->rsn_capab & WPA_CAPABILITY_MFPC) &&
  1151. (sta->state == STATE3 ||
  1152. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION)) {
  1153. add_note(wt, MSG_INFO, "Robust individually-addressed "
  1154. "management frame sent without CCMP by "
  1155. MACSTR, MAC2STR(mgmt->sa));
  1156. return -1;
  1157. }
  1158. return 0;
  1159. }
  1160. void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
  1161. {
  1162. const struct ieee80211_hdr *hdr;
  1163. u16 fc, stype;
  1164. int valid = 1;
  1165. u8 *decrypted = NULL;
  1166. size_t dlen;
  1167. if (len < 24)
  1168. return;
  1169. hdr = (const struct ieee80211_hdr *) data;
  1170. fc = le_to_host16(hdr->frame_control);
  1171. wt->rx_mgmt++;
  1172. stype = WLAN_FC_GET_STYPE(fc);
  1173. if ((hdr->addr1[0] & 0x01) &&
  1174. (stype == WLAN_FC_STYPE_DEAUTH ||
  1175. stype == WLAN_FC_STYPE_DISASSOC ||
  1176. stype == WLAN_FC_STYPE_ACTION)) {
  1177. if (check_bip(wt, data, len) < 0)
  1178. valid = 0;
  1179. }
  1180. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  1181. stype == WLAN_FC_STYPE_PROBE_RESP ||
  1182. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  1183. MSG_EXCESSIVE : MSG_MSGDUMP,
  1184. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  1185. mgmt_stype(stype),
  1186. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  1187. fc & WLAN_FC_ISWEP ? " Prot" : "",
  1188. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1189. MAC2STR(hdr->addr3));
  1190. if ((fc & WLAN_FC_ISWEP) &&
  1191. !(hdr->addr1[0] & 0x01) &&
  1192. (stype == WLAN_FC_STYPE_DEAUTH ||
  1193. stype == WLAN_FC_STYPE_DISASSOC ||
  1194. stype == WLAN_FC_STYPE_ACTION)) {
  1195. decrypted = mgmt_ccmp_decrypt(wt, data, len, &dlen);
  1196. if (decrypted) {
  1197. write_pcap_decrypted(wt, decrypted, dlen, NULL, 0);
  1198. data = decrypted;
  1199. len = dlen;
  1200. } else
  1201. valid = 0;
  1202. }
  1203. if (!(fc & WLAN_FC_ISWEP) &&
  1204. !(hdr->addr1[0] & 0x01) &&
  1205. (stype == WLAN_FC_STYPE_DEAUTH ||
  1206. stype == WLAN_FC_STYPE_DISASSOC ||
  1207. stype == WLAN_FC_STYPE_ACTION)) {
  1208. if (check_mgmt_ccmp(wt, data, len) < 0)
  1209. valid = 0;
  1210. }
  1211. switch (stype) {
  1212. case WLAN_FC_STYPE_BEACON:
  1213. rx_mgmt_beacon(wt, data, len);
  1214. break;
  1215. case WLAN_FC_STYPE_PROBE_RESP:
  1216. rx_mgmt_probe_resp(wt, data, len);
  1217. break;
  1218. case WLAN_FC_STYPE_AUTH:
  1219. rx_mgmt_auth(wt, data, len);
  1220. break;
  1221. case WLAN_FC_STYPE_DEAUTH:
  1222. rx_mgmt_deauth(wt, data, len, valid);
  1223. break;
  1224. case WLAN_FC_STYPE_ASSOC_REQ:
  1225. rx_mgmt_assoc_req(wt, data, len);
  1226. break;
  1227. case WLAN_FC_STYPE_ASSOC_RESP:
  1228. rx_mgmt_assoc_resp(wt, data, len);
  1229. break;
  1230. case WLAN_FC_STYPE_REASSOC_REQ:
  1231. rx_mgmt_reassoc_req(wt, data, len);
  1232. break;
  1233. case WLAN_FC_STYPE_REASSOC_RESP:
  1234. rx_mgmt_reassoc_resp(wt, data, len);
  1235. break;
  1236. case WLAN_FC_STYPE_DISASSOC:
  1237. rx_mgmt_disassoc(wt, data, len, valid);
  1238. break;
  1239. case WLAN_FC_STYPE_ACTION:
  1240. rx_mgmt_action(wt, data, len, valid);
  1241. break;
  1242. }
  1243. os_free(decrypted);
  1244. wt->last_mgmt_valid = valid;
  1245. }
  1246. static void rx_mgmt_deauth_ack(struct wlantest *wt,
  1247. const struct ieee80211_hdr *hdr)
  1248. {
  1249. const struct ieee80211_mgmt *mgmt;
  1250. struct wlantest_bss *bss;
  1251. struct wlantest_sta *sta;
  1252. mgmt = (const struct ieee80211_mgmt *) hdr;
  1253. bss = bss_get(wt, mgmt->bssid);
  1254. if (bss == NULL)
  1255. return;
  1256. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  1257. sta = sta_get(bss, mgmt->da);
  1258. else
  1259. sta = sta_get(bss, mgmt->sa);
  1260. if (sta == NULL)
  1261. return;
  1262. add_note(wt, MSG_DEBUG, "DEAUTH from " MACSTR " acknowledged by "
  1263. MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  1264. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  1265. int c;
  1266. c = wt->last_mgmt_valid ?
  1267. WLANTEST_STA_COUNTER_VALID_DEAUTH_RX_ACK :
  1268. WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX_ACK;
  1269. sta->counters[c]++;
  1270. }
  1271. }
  1272. static void rx_mgmt_disassoc_ack(struct wlantest *wt,
  1273. const struct ieee80211_hdr *hdr)
  1274. {
  1275. const struct ieee80211_mgmt *mgmt;
  1276. struct wlantest_bss *bss;
  1277. struct wlantest_sta *sta;
  1278. mgmt = (const struct ieee80211_mgmt *) hdr;
  1279. bss = bss_get(wt, mgmt->bssid);
  1280. if (bss == NULL)
  1281. return;
  1282. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  1283. sta = sta_get(bss, mgmt->da);
  1284. else
  1285. sta = sta_get(bss, mgmt->sa);
  1286. if (sta == NULL)
  1287. return;
  1288. add_note(wt, MSG_DEBUG, "DISASSOC from " MACSTR " acknowledged by "
  1289. MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  1290. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  1291. int c;
  1292. c = wt->last_mgmt_valid ?
  1293. WLANTEST_STA_COUNTER_VALID_DISASSOC_RX_ACK :
  1294. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX_ACK;
  1295. sta->counters[c]++;
  1296. }
  1297. }
  1298. void rx_mgmt_ack(struct wlantest *wt, const struct ieee80211_hdr *hdr)
  1299. {
  1300. u16 fc, stype;
  1301. fc = le_to_host16(hdr->frame_control);
  1302. stype = WLAN_FC_GET_STYPE(fc);
  1303. wpa_printf(MSG_MSGDUMP, "MGMT ACK: stype=%u a1=" MACSTR " a2=" MACSTR
  1304. " a3=" MACSTR,
  1305. stype, MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1306. MAC2STR(hdr->addr3));
  1307. switch (stype) {
  1308. case WLAN_FC_STYPE_DEAUTH:
  1309. rx_mgmt_deauth_ack(wt, hdr);
  1310. break;
  1311. case WLAN_FC_STYPE_DISASSOC:
  1312. rx_mgmt_disassoc_ack(wt, hdr);
  1313. break;
  1314. }
  1315. }