rx_mgmt.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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 "crypto/aes_wrap.h"
  14. #include "wlantest.h"
  15. static const char * mgmt_stype(u16 stype)
  16. {
  17. switch (stype) {
  18. case WLAN_FC_STYPE_ASSOC_REQ:
  19. return "ASSOC-REQ";
  20. case WLAN_FC_STYPE_ASSOC_RESP:
  21. return "ASSOC-RESP";
  22. case WLAN_FC_STYPE_REASSOC_REQ:
  23. return "REASSOC-REQ";
  24. case WLAN_FC_STYPE_REASSOC_RESP:
  25. return "REASSOC-RESP";
  26. case WLAN_FC_STYPE_PROBE_REQ:
  27. return "PROBE-REQ";
  28. case WLAN_FC_STYPE_PROBE_RESP:
  29. return "PROBE-RESP";
  30. case WLAN_FC_STYPE_BEACON:
  31. return "BEACON";
  32. case WLAN_FC_STYPE_ATIM:
  33. return "ATIM";
  34. case WLAN_FC_STYPE_DISASSOC:
  35. return "DISASSOC";
  36. case WLAN_FC_STYPE_AUTH:
  37. return "AUTH";
  38. case WLAN_FC_STYPE_DEAUTH:
  39. return "DEAUTH";
  40. case WLAN_FC_STYPE_ACTION:
  41. return "ACTION";
  42. }
  43. return "??";
  44. }
  45. static void rx_mgmt_beacon(struct wlantest *wt, const u8 *data, size_t len)
  46. {
  47. const struct ieee80211_mgmt *mgmt;
  48. struct wlantest_bss *bss;
  49. struct ieee802_11_elems elems;
  50. size_t offset;
  51. mgmt = (const struct ieee80211_mgmt *) data;
  52. offset = mgmt->u.beacon.variable - data;
  53. if (len < offset)
  54. return;
  55. bss = bss_get(wt, mgmt->bssid);
  56. if (bss == NULL)
  57. return;
  58. if (bss->proberesp_seen)
  59. return; /* do not override with Beacon data */
  60. bss->capab_info = le_to_host16(mgmt->u.beacon.capab_info);
  61. if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - offset,
  62. &elems, 0) == ParseFailed) {
  63. if (bss->parse_error_reported)
  64. return;
  65. add_note(wt, MSG_INFO, "Invalid IEs in a Beacon frame from "
  66. MACSTR, MAC2STR(mgmt->sa));
  67. bss->parse_error_reported = 1;
  68. return;
  69. }
  70. bss_update(wt, bss, &elems);
  71. }
  72. static void rx_mgmt_probe_resp(struct wlantest *wt, const u8 *data, size_t len)
  73. {
  74. const struct ieee80211_mgmt *mgmt;
  75. struct wlantest_bss *bss;
  76. struct ieee802_11_elems elems;
  77. size_t offset;
  78. mgmt = (const struct ieee80211_mgmt *) data;
  79. offset = mgmt->u.probe_resp.variable - data;
  80. if (len < offset)
  81. return;
  82. bss = bss_get(wt, mgmt->bssid);
  83. if (bss == NULL)
  84. return;
  85. bss->counters[WLANTEST_BSS_COUNTER_PROBE_RESPONSE]++;
  86. bss->capab_info = le_to_host16(mgmt->u.probe_resp.capab_info);
  87. if (ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - offset,
  88. &elems, 0) == ParseFailed) {
  89. if (bss->parse_error_reported)
  90. return;
  91. add_note(wt, MSG_INFO, "Invalid IEs in a Probe Response frame "
  92. "from " MACSTR, MAC2STR(mgmt->sa));
  93. bss->parse_error_reported = 1;
  94. return;
  95. }
  96. bss_update(wt, bss, &elems);
  97. }
  98. static void rx_mgmt_auth(struct wlantest *wt, const u8 *data, size_t len)
  99. {
  100. const struct ieee80211_mgmt *mgmt;
  101. struct wlantest_bss *bss;
  102. struct wlantest_sta *sta;
  103. u16 alg, trans, status;
  104. mgmt = (const struct ieee80211_mgmt *) data;
  105. bss = bss_get(wt, mgmt->bssid);
  106. if (bss == NULL)
  107. return;
  108. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  109. sta = sta_get(bss, mgmt->da);
  110. else
  111. sta = sta_get(bss, mgmt->sa);
  112. if (sta == NULL)
  113. return;
  114. if (len < 24 + 6) {
  115. add_note(wt, MSG_INFO, "Too short Authentication frame from "
  116. MACSTR, MAC2STR(mgmt->sa));
  117. return;
  118. }
  119. alg = le_to_host16(mgmt->u.auth.auth_alg);
  120. trans = le_to_host16(mgmt->u.auth.auth_transaction);
  121. status = le_to_host16(mgmt->u.auth.status_code);
  122. wpa_printf(MSG_DEBUG, "AUTH " MACSTR " -> " MACSTR
  123. " (alg=%u trans=%u status=%u)",
  124. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), alg, trans, status);
  125. if (alg == 0 && trans == 2 && status == 0) {
  126. if (sta->state == STATE1) {
  127. add_note(wt, MSG_DEBUG, "STA " MACSTR
  128. " moved to State 2 with " MACSTR,
  129. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  130. sta->state = STATE2;
  131. }
  132. }
  133. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  134. sta->counters[WLANTEST_STA_COUNTER_AUTH_RX]++;
  135. else
  136. sta->counters[WLANTEST_STA_COUNTER_AUTH_TX]++;
  137. }
  138. static void deauth_all_stas(struct wlantest *wt, struct wlantest_bss *bss)
  139. {
  140. struct wlantest_sta *sta;
  141. dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
  142. if (sta->state == STATE1)
  143. continue;
  144. add_note(wt, MSG_DEBUG, "STA " MACSTR
  145. " moved to State 1 with " MACSTR,
  146. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  147. sta->state = STATE1;
  148. }
  149. }
  150. static void tdls_link_down(struct wlantest *wt, struct wlantest_bss *bss,
  151. struct wlantest_sta *sta)
  152. {
  153. struct wlantest_tdls *tdls;
  154. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  155. if ((tdls->init == sta || tdls->resp == sta) && tdls->link_up)
  156. {
  157. add_note(wt, MSG_DEBUG, "TDLS: Set link down based on "
  158. "STA deauth/disassoc");
  159. tdls->link_up = 0;
  160. }
  161. }
  162. }
  163. static void rx_mgmt_deauth(struct wlantest *wt, const u8 *data, size_t len,
  164. int valid)
  165. {
  166. const struct ieee80211_mgmt *mgmt;
  167. struct wlantest_bss *bss;
  168. struct wlantest_sta *sta;
  169. u16 fc, reason;
  170. mgmt = (const struct ieee80211_mgmt *) data;
  171. bss = bss_get(wt, mgmt->bssid);
  172. if (bss == NULL)
  173. return;
  174. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  175. sta = sta_get(bss, mgmt->da);
  176. else
  177. sta = sta_get(bss, mgmt->sa);
  178. if (len < 24 + 2) {
  179. add_note(wt, MSG_INFO, "Too short Deauthentication frame from "
  180. MACSTR, MAC2STR(mgmt->sa));
  181. return;
  182. }
  183. reason = le_to_host16(mgmt->u.deauth.reason_code);
  184. wpa_printf(MSG_DEBUG, "DEAUTH " MACSTR " -> " MACSTR
  185. " (reason=%u) (valid=%d)",
  186. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  187. reason, valid);
  188. wpa_hexdump(MSG_MSGDUMP, "DEAUTH payload", data + 24, len - 24);
  189. if (sta == NULL) {
  190. if (valid && mgmt->da[0] == 0xff)
  191. deauth_all_stas(wt, bss);
  192. return;
  193. }
  194. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  195. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DEAUTH_RX :
  196. WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX]++;
  197. if (sta->pwrmgt && !sta->pspoll)
  198. sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_ASLEEP]++;
  199. else
  200. sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_AWAKE]++;
  201. fc = le_to_host16(mgmt->frame_control);
  202. if (!(fc & WLAN_FC_ISWEP) && reason == 6)
  203. sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_RC6]++;
  204. else if (!(fc & WLAN_FC_ISWEP) && reason == 7)
  205. sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_RC7]++;
  206. } else
  207. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DEAUTH_TX :
  208. WLANTEST_STA_COUNTER_INVALID_DEAUTH_TX]++;
  209. if (!valid) {
  210. add_note(wt, MSG_INFO, "Do not change STA " MACSTR " State "
  211. "since Disassociation frame was not protected "
  212. "correctly", MAC2STR(sta->addr));
  213. return;
  214. }
  215. if (sta->state != STATE1) {
  216. add_note(wt, MSG_DEBUG, "STA " MACSTR
  217. " moved to State 1 with " MACSTR,
  218. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  219. sta->state = STATE1;
  220. }
  221. tdls_link_down(wt, bss, sta);
  222. }
  223. static void rx_mgmt_assoc_req(struct wlantest *wt, const u8 *data, size_t len)
  224. {
  225. const struct ieee80211_mgmt *mgmt;
  226. struct wlantest_bss *bss;
  227. struct wlantest_sta *sta;
  228. struct ieee802_11_elems elems;
  229. mgmt = (const struct ieee80211_mgmt *) data;
  230. bss = bss_get(wt, mgmt->bssid);
  231. if (bss == NULL)
  232. return;
  233. sta = sta_get(bss, mgmt->sa);
  234. if (sta == NULL)
  235. return;
  236. if (len < 24 + 4) {
  237. add_note(wt, MSG_INFO, "Too short Association Request frame "
  238. "from " MACSTR, MAC2STR(mgmt->sa));
  239. return;
  240. }
  241. wpa_printf(MSG_DEBUG, "ASSOCREQ " MACSTR " -> " MACSTR
  242. " (capab=0x%x listen_int=%u)",
  243. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  244. le_to_host16(mgmt->u.assoc_req.capab_info),
  245. le_to_host16(mgmt->u.assoc_req.listen_interval));
  246. sta->counters[WLANTEST_STA_COUNTER_ASSOCREQ_TX]++;
  247. if (ieee802_11_parse_elems(mgmt->u.assoc_req.variable,
  248. len - (mgmt->u.assoc_req.variable - data),
  249. &elems, 0) == ParseFailed) {
  250. add_note(wt, MSG_INFO, "Invalid IEs in Association Request "
  251. "frame from " MACSTR, MAC2STR(mgmt->sa));
  252. return;
  253. }
  254. sta->assocreq_capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  255. sta->assocreq_listen_int =
  256. le_to_host16(mgmt->u.assoc_req.listen_interval);
  257. os_free(sta->assocreq_ies);
  258. sta->assocreq_ies_len = len - (mgmt->u.assoc_req.variable - data);
  259. sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
  260. if (sta->assocreq_ies)
  261. os_memcpy(sta->assocreq_ies, mgmt->u.assoc_req.variable,
  262. sta->assocreq_ies_len);
  263. sta_update_assoc(sta, &elems);
  264. }
  265. static void rx_mgmt_assoc_resp(struct wlantest *wt, const u8 *data, size_t len)
  266. {
  267. const struct ieee80211_mgmt *mgmt;
  268. struct wlantest_bss *bss;
  269. struct wlantest_sta *sta;
  270. u16 capab, status, aid;
  271. mgmt = (const struct ieee80211_mgmt *) data;
  272. bss = bss_get(wt, mgmt->bssid);
  273. if (bss == NULL)
  274. return;
  275. sta = sta_get(bss, mgmt->da);
  276. if (sta == NULL)
  277. return;
  278. if (len < 24 + 6) {
  279. add_note(wt, MSG_INFO, "Too short Association Response frame "
  280. "from " MACSTR, MAC2STR(mgmt->sa));
  281. return;
  282. }
  283. capab = le_to_host16(mgmt->u.assoc_resp.capab_info);
  284. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  285. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  286. wpa_printf(MSG_DEBUG, "ASSOCRESP " MACSTR " -> " MACSTR
  287. " (capab=0x%x status=%u aid=%u)",
  288. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  289. aid & 0x3fff);
  290. if (status == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) {
  291. struct ieee802_11_elems elems;
  292. const u8 *ies = mgmt->u.assoc_resp.variable;
  293. size_t ies_len = len - (mgmt->u.assoc_resp.variable - data);
  294. if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
  295. ParseFailed) {
  296. add_note(wt, MSG_INFO, "Failed to parse IEs in "
  297. "AssocResp from " MACSTR,
  298. MAC2STR(mgmt->sa));
  299. } else if (elems.timeout_int == NULL ||
  300. elems.timeout_int[0] !=
  301. WLAN_TIMEOUT_ASSOC_COMEBACK) {
  302. add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
  303. "with Assoc Comeback time in AssocResp "
  304. "(status=30) from " MACSTR,
  305. MAC2STR(mgmt->sa));
  306. } else {
  307. sta->counters[
  308. WLANTEST_STA_COUNTER_ASSOCRESP_COMEBACK]++;
  309. }
  310. }
  311. if (status)
  312. return;
  313. if ((aid & 0xc000) != 0xc000) {
  314. add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  315. "in Association Response from " MACSTR,
  316. MAC2STR(mgmt->sa));
  317. }
  318. sta->aid = aid & 0xc000;
  319. if (sta->state < STATE2) {
  320. add_note(wt, MSG_DEBUG,
  321. "STA " MACSTR " was not in State 2 when "
  322. "getting associated", MAC2STR(sta->addr));
  323. }
  324. if (sta->state < STATE3) {
  325. add_note(wt, MSG_DEBUG, "STA " MACSTR
  326. " moved to State 3 with " MACSTR,
  327. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  328. sta->state = STATE3;
  329. }
  330. }
  331. static void rx_mgmt_reassoc_req(struct wlantest *wt, const u8 *data,
  332. size_t len)
  333. {
  334. const struct ieee80211_mgmt *mgmt;
  335. struct wlantest_bss *bss;
  336. struct wlantest_sta *sta;
  337. struct ieee802_11_elems elems;
  338. mgmt = (const struct ieee80211_mgmt *) data;
  339. bss = bss_get(wt, mgmt->bssid);
  340. if (bss == NULL)
  341. return;
  342. sta = sta_get(bss, mgmt->sa);
  343. if (sta == NULL)
  344. return;
  345. if (len < 24 + 4 + ETH_ALEN) {
  346. add_note(wt, MSG_INFO, "Too short Reassociation Request frame "
  347. "from " MACSTR, MAC2STR(mgmt->sa));
  348. return;
  349. }
  350. wpa_printf(MSG_DEBUG, "REASSOCREQ " MACSTR " -> " MACSTR
  351. " (capab=0x%x listen_int=%u current_ap=" MACSTR ")",
  352. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  353. le_to_host16(mgmt->u.reassoc_req.capab_info),
  354. le_to_host16(mgmt->u.reassoc_req.listen_interval),
  355. MAC2STR(mgmt->u.reassoc_req.current_ap));
  356. sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX]++;
  357. if (ieee802_11_parse_elems(mgmt->u.reassoc_req.variable,
  358. len - (mgmt->u.reassoc_req.variable - data),
  359. &elems, 0) == ParseFailed) {
  360. add_note(wt, MSG_INFO, "Invalid IEs in Reassociation Request "
  361. "frame from " MACSTR, MAC2STR(mgmt->sa));
  362. return;
  363. }
  364. sta->assocreq_capab_info =
  365. le_to_host16(mgmt->u.reassoc_req.capab_info);
  366. sta->assocreq_listen_int =
  367. le_to_host16(mgmt->u.reassoc_req.listen_interval);
  368. os_free(sta->assocreq_ies);
  369. sta->assocreq_ies_len = len - (mgmt->u.reassoc_req.variable - data);
  370. sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
  371. if (sta->assocreq_ies)
  372. os_memcpy(sta->assocreq_ies, mgmt->u.reassoc_req.variable,
  373. sta->assocreq_ies_len);
  374. sta_update_assoc(sta, &elems);
  375. }
  376. static void rx_mgmt_reassoc_resp(struct wlantest *wt, const u8 *data,
  377. size_t len)
  378. {
  379. const struct ieee80211_mgmt *mgmt;
  380. struct wlantest_bss *bss;
  381. struct wlantest_sta *sta;
  382. u16 capab, status, aid;
  383. mgmt = (const struct ieee80211_mgmt *) data;
  384. bss = bss_get(wt, mgmt->bssid);
  385. if (bss == NULL)
  386. return;
  387. sta = sta_get(bss, mgmt->da);
  388. if (sta == NULL)
  389. return;
  390. if (len < 24 + 6) {
  391. add_note(wt, MSG_INFO, "Too short Reassociation Response frame "
  392. "from " MACSTR, MAC2STR(mgmt->sa));
  393. return;
  394. }
  395. capab = le_to_host16(mgmt->u.reassoc_resp.capab_info);
  396. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  397. aid = le_to_host16(mgmt->u.reassoc_resp.aid);
  398. wpa_printf(MSG_DEBUG, "REASSOCRESP " MACSTR " -> " MACSTR
  399. " (capab=0x%x status=%u aid=%u)",
  400. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  401. aid & 0x3fff);
  402. if (status == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) {
  403. struct ieee802_11_elems elems;
  404. const u8 *ies = mgmt->u.reassoc_resp.variable;
  405. size_t ies_len = len - (mgmt->u.reassoc_resp.variable - data);
  406. if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
  407. ParseFailed) {
  408. add_note(wt, MSG_INFO, "Failed to parse IEs in "
  409. "ReassocResp from " MACSTR,
  410. MAC2STR(mgmt->sa));
  411. } else if (elems.timeout_int == NULL ||
  412. elems.timeout_int[0] !=
  413. WLAN_TIMEOUT_ASSOC_COMEBACK) {
  414. add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
  415. "with Assoc Comeback time in ReassocResp "
  416. "(status=30) from " MACSTR,
  417. MAC2STR(mgmt->sa));
  418. } else {
  419. sta->counters[
  420. WLANTEST_STA_COUNTER_REASSOCRESP_COMEBACK]++;
  421. }
  422. }
  423. if (status)
  424. return;
  425. if ((aid & 0xc000) != 0xc000) {
  426. add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  427. "in Reassociation Response from " MACSTR,
  428. MAC2STR(mgmt->sa));
  429. }
  430. sta->aid = aid & 0xc000;
  431. if (sta->state < STATE2) {
  432. add_note(wt, MSG_DEBUG,
  433. "STA " MACSTR " was not in State 2 when "
  434. "getting associated", MAC2STR(sta->addr));
  435. }
  436. if (sta->state < STATE3) {
  437. add_note(wt, MSG_DEBUG, "STA " MACSTR
  438. " moved to State 3 with " MACSTR,
  439. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  440. sta->state = STATE3;
  441. }
  442. }
  443. static void disassoc_all_stas(struct wlantest *wt, struct wlantest_bss *bss)
  444. {
  445. struct wlantest_sta *sta;
  446. dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
  447. if (sta->state <= STATE2)
  448. continue;
  449. add_note(wt, MSG_DEBUG, "STA " MACSTR
  450. " moved to State 2 with " MACSTR,
  451. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  452. sta->state = STATE2;
  453. }
  454. }
  455. static void rx_mgmt_disassoc(struct wlantest *wt, const u8 *data, size_t len,
  456. int valid)
  457. {
  458. const struct ieee80211_mgmt *mgmt;
  459. struct wlantest_bss *bss;
  460. struct wlantest_sta *sta;
  461. u16 fc, reason;
  462. mgmt = (const struct ieee80211_mgmt *) data;
  463. bss = bss_get(wt, mgmt->bssid);
  464. if (bss == NULL)
  465. return;
  466. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  467. sta = sta_get(bss, mgmt->da);
  468. else
  469. sta = sta_get(bss, mgmt->sa);
  470. if (len < 24 + 2) {
  471. add_note(wt, MSG_INFO, "Too short Disassociation frame from "
  472. MACSTR, MAC2STR(mgmt->sa));
  473. return;
  474. }
  475. reason = le_to_host16(mgmt->u.disassoc.reason_code);
  476. wpa_printf(MSG_DEBUG, "DISASSOC " MACSTR " -> " MACSTR
  477. " (reason=%u) (valid=%d)",
  478. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  479. reason, valid);
  480. wpa_hexdump(MSG_MSGDUMP, "DISASSOC payload", data + 24, len - 24);
  481. if (sta == NULL) {
  482. if (valid && mgmt->da[0] == 0xff)
  483. disassoc_all_stas(wt, bss);
  484. return;
  485. }
  486. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  487. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_RX :
  488. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX]++;
  489. if (sta->pwrmgt && !sta->pspoll)
  490. sta->counters[
  491. WLANTEST_STA_COUNTER_DISASSOC_RX_ASLEEP]++;
  492. else
  493. sta->counters[
  494. WLANTEST_STA_COUNTER_DISASSOC_RX_AWAKE]++;
  495. fc = le_to_host16(mgmt->frame_control);
  496. if (!(fc & WLAN_FC_ISWEP) && reason == 6)
  497. sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC6]++;
  498. else if (!(fc & WLAN_FC_ISWEP) && reason == 7)
  499. sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC7]++;
  500. } else
  501. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_TX :
  502. WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX]++;
  503. if (!valid) {
  504. add_note(wt, MSG_INFO, "Do not change STA " MACSTR " State "
  505. "since Disassociation frame was not protected "
  506. "correctly", MAC2STR(sta->addr));
  507. return;
  508. }
  509. if (sta->state < STATE2) {
  510. add_note(wt, MSG_DEBUG,
  511. "STA " MACSTR " was not in State 2 or 3 "
  512. "when getting disassociated", MAC2STR(sta->addr));
  513. }
  514. if (sta->state > STATE2) {
  515. add_note(wt, MSG_DEBUG, "STA " MACSTR
  516. " moved to State 2 with " MACSTR,
  517. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  518. sta->state = STATE2;
  519. }
  520. tdls_link_down(wt, bss, sta);
  521. }
  522. static void rx_mgmt_action_sa_query_req(struct wlantest *wt,
  523. struct wlantest_sta *sta,
  524. const struct ieee80211_mgmt *mgmt,
  525. size_t len, int valid)
  526. {
  527. const u8 *rx_id;
  528. u8 *id;
  529. rx_id = (const u8 *) mgmt->u.action.u.sa_query_req.trans_id;
  530. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  531. id = sta->ap_sa_query_tr;
  532. else
  533. id = sta->sta_sa_query_tr;
  534. add_note(wt, MSG_INFO, "SA Query Request " MACSTR " -> " MACSTR
  535. " (trans_id=%02x%02x)%s",
  536. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  537. valid ? "" : " (invalid protection)");
  538. os_memcpy(id, mgmt->u.action.u.sa_query_req.trans_id, 2);
  539. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  540. sta->counters[valid ?
  541. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX :
  542. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX]++;
  543. else
  544. sta->counters[valid ?
  545. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX :
  546. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX]++;
  547. }
  548. static void rx_mgmt_action_sa_query_resp(struct wlantest *wt,
  549. struct wlantest_sta *sta,
  550. const struct ieee80211_mgmt *mgmt,
  551. size_t len, int valid)
  552. {
  553. const u8 *rx_id;
  554. u8 *id;
  555. int match;
  556. rx_id = (const u8 *) mgmt->u.action.u.sa_query_resp.trans_id;
  557. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  558. id = sta->sta_sa_query_tr;
  559. else
  560. id = sta->ap_sa_query_tr;
  561. match = os_memcmp(rx_id, id, 2) == 0;
  562. add_note(wt, MSG_INFO, "SA Query Response " MACSTR " -> " MACSTR
  563. " (trans_id=%02x%02x; %s)%s",
  564. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  565. match ? "match" : "mismatch",
  566. valid ? "" : " (invalid protection)");
  567. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  568. sta->counters[(valid && match) ?
  569. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX :
  570. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX]++;
  571. else
  572. sta->counters[(valid && match) ?
  573. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX :
  574. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX]++;
  575. }
  576. static void rx_mgmt_action_sa_query(struct wlantest *wt,
  577. struct wlantest_sta *sta,
  578. const struct ieee80211_mgmt *mgmt,
  579. size_t len, int valid)
  580. {
  581. if (len < 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  582. add_note(wt, MSG_INFO, "Too short SA Query frame from " MACSTR,
  583. MAC2STR(mgmt->sa));
  584. return;
  585. }
  586. if (len > 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  587. size_t elen = len - (24 + 2 + WLAN_SA_QUERY_TR_ID_LEN);
  588. add_note(wt, MSG_INFO, "Unexpected %u octets of extra data at "
  589. "the end of SA Query frame from " MACSTR,
  590. (unsigned) elen, MAC2STR(mgmt->sa));
  591. wpa_hexdump(MSG_INFO, "SA Query extra data",
  592. ((const u8 *) mgmt) + len - elen, elen);
  593. }
  594. switch (mgmt->u.action.u.sa_query_req.action) {
  595. case WLAN_SA_QUERY_REQUEST:
  596. rx_mgmt_action_sa_query_req(wt, sta, mgmt, len, valid);
  597. break;
  598. case WLAN_SA_QUERY_RESPONSE:
  599. rx_mgmt_action_sa_query_resp(wt, sta, mgmt, len, valid);
  600. break;
  601. default:
  602. add_note(wt, MSG_INFO, "Unexpected SA Query action value %u "
  603. "from " MACSTR,
  604. mgmt->u.action.u.sa_query_req.action,
  605. MAC2STR(mgmt->sa));
  606. }
  607. }
  608. static void rx_mgmt_action(struct wlantest *wt, const u8 *data, size_t len,
  609. int valid)
  610. {
  611. const struct ieee80211_mgmt *mgmt;
  612. struct wlantest_bss *bss;
  613. struct wlantest_sta *sta;
  614. mgmt = (const struct ieee80211_mgmt *) data;
  615. if (mgmt->da[0] & 0x01) {
  616. add_note(wt, MSG_DEBUG, "Group addressed Action frame: DA="
  617. MACSTR " SA=" MACSTR " BSSID=" MACSTR
  618. " category=%u",
  619. MAC2STR(mgmt->da), MAC2STR(mgmt->sa),
  620. MAC2STR(mgmt->bssid), mgmt->u.action.category);
  621. return; /* Ignore group addressed Action frames for now */
  622. }
  623. bss = bss_get(wt, mgmt->bssid);
  624. if (bss == NULL)
  625. return;
  626. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  627. sta = sta_get(bss, mgmt->da);
  628. else
  629. sta = sta_get(bss, mgmt->sa);
  630. if (sta == NULL)
  631. return;
  632. if (len < 24 + 1) {
  633. add_note(wt, MSG_INFO, "Too short Action frame from " MACSTR,
  634. MAC2STR(mgmt->sa));
  635. return;
  636. }
  637. wpa_printf(MSG_DEBUG, "ACTION " MACSTR " -> " MACSTR
  638. " (category=%u) (valid=%d)",
  639. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  640. mgmt->u.action.category, valid);
  641. wpa_hexdump(MSG_MSGDUMP, "ACTION payload", data + 24, len - 24);
  642. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  643. sta->state < STATE3) {
  644. add_note(wt, MSG_INFO, "Action frame sent when STA is not in "
  645. "State 3 (SA=" MACSTR " DATA=" MACSTR ")",
  646. MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  647. }
  648. switch (mgmt->u.action.category) {
  649. case WLAN_ACTION_SA_QUERY:
  650. rx_mgmt_action_sa_query(wt, sta, mgmt, len, valid);
  651. break;
  652. }
  653. }
  654. static int check_mmie_mic(unsigned int mgmt_group_cipher,
  655. const u8 *igtk, size_t igtk_len,
  656. const u8 *data, size_t len)
  657. {
  658. u8 *buf;
  659. u8 mic[16];
  660. u16 fc;
  661. const struct ieee80211_hdr *hdr;
  662. int ret, mic_len;
  663. if (!mgmt_group_cipher || igtk_len < 16)
  664. return -1;
  665. mic_len = mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
  666. if (len < 24 || len - 24 < mic_len)
  667. return -1;
  668. buf = os_malloc(len + 20 - 24);
  669. if (buf == NULL)
  670. return -1;
  671. /* BIP AAD: FC(masked) A1 A2 A3 */
  672. hdr = (const struct ieee80211_hdr *) data;
  673. fc = le_to_host16(hdr->frame_control);
  674. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  675. WPA_PUT_LE16(buf, fc);
  676. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  677. /* Frame body with MMIE MIC masked to zero */
  678. os_memcpy(buf + 20, data + 24, len - 24 - mic_len);
  679. os_memset(buf + 20 + len - 24 - mic_len, 0, mic_len);
  680. wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, len + 20 - 24);
  681. /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
  682. if (mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  683. ret = omac1_aes_128(igtk, buf, len + 20 - 24, mic);
  684. } else if (mgmt_group_cipher == WPA_CIPHER_BIP_CMAC_256) {
  685. ret = omac1_aes_256(igtk, buf, len + 20 - 24, mic);
  686. } else if (mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_128 ||
  687. mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_256) {
  688. u8 nonce[12], *npos;
  689. const u8 *ipn;
  690. ipn = data + len - mic_len - 6;
  691. /* Nonce: A2 | IPN */
  692. os_memcpy(nonce, hdr->addr2, ETH_ALEN);
  693. npos = nonce + ETH_ALEN;
  694. *npos++ = ipn[5];
  695. *npos++ = ipn[4];
  696. *npos++ = ipn[3];
  697. *npos++ = ipn[2];
  698. *npos++ = ipn[1];
  699. *npos++ = ipn[0];
  700. ret = aes_gmac(igtk, igtk_len, nonce, sizeof(nonce),
  701. buf, len + 20 - 24, mic);
  702. } else {
  703. ret = -1;
  704. }
  705. if (ret < 0) {
  706. os_free(buf);
  707. return -1;
  708. }
  709. os_free(buf);
  710. if (os_memcmp(data + len - mic_len, mic, mic_len) != 0)
  711. return -1;
  712. return 0;
  713. }
  714. static int check_bip(struct wlantest *wt, const u8 *data, size_t len)
  715. {
  716. const struct ieee80211_mgmt *mgmt;
  717. u16 fc, stype;
  718. const u8 *mmie;
  719. u16 keyid;
  720. struct wlantest_bss *bss;
  721. size_t mic_len;
  722. mgmt = (const struct ieee80211_mgmt *) data;
  723. fc = le_to_host16(mgmt->frame_control);
  724. stype = WLAN_FC_GET_STYPE(fc);
  725. if (stype == WLAN_FC_STYPE_ACTION) {
  726. if (len < 24 + 1)
  727. return 0;
  728. if (mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  729. return 0; /* Not a robust management frame */
  730. }
  731. bss = bss_get(wt, mgmt->bssid);
  732. if (bss == NULL)
  733. return 0; /* No key known yet */
  734. mic_len = bss->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
  735. if (len < 24 + 10 + mic_len ||
  736. data[len - (10 + mic_len)] != WLAN_EID_MMIE ||
  737. data[len - (10 + mic_len - 1)] != 8 + mic_len) {
  738. /* No MMIE */
  739. if (bss->rsn_capab & WPA_CAPABILITY_MFPC) {
  740. add_note(wt, MSG_INFO, "Robust group-addressed "
  741. "management frame sent without BIP by "
  742. MACSTR, MAC2STR(mgmt->sa));
  743. bss->counters[WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE]++;
  744. return -1;
  745. }
  746. return 0;
  747. }
  748. mmie = data + len - (8 + mic_len);
  749. keyid = WPA_GET_LE16(mmie);
  750. if (keyid & 0xf000) {
  751. add_note(wt, MSG_INFO, "MMIE KeyID reserved bits not zero "
  752. "(%04x) from " MACSTR, keyid, MAC2STR(mgmt->sa));
  753. keyid &= 0x0fff;
  754. }
  755. if (keyid < 4 || keyid > 5) {
  756. add_note(wt, MSG_INFO, "Unexpected MMIE KeyID %u from " MACSTR,
  757. keyid, MAC2STR(mgmt->sa));
  758. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  759. return 0;
  760. }
  761. wpa_printf(MSG_DEBUG, "MMIE KeyID %u", keyid);
  762. wpa_hexdump(MSG_MSGDUMP, "MMIE IPN", mmie + 2, 6);
  763. wpa_hexdump(MSG_MSGDUMP, "MMIE MIC", mmie + 8, mic_len);
  764. if (!bss->igtk_len[keyid]) {
  765. add_note(wt, MSG_DEBUG, "No IGTK known to validate BIP frame");
  766. return 0;
  767. }
  768. if (os_memcmp(mmie + 2, bss->ipn[keyid], 6) <= 0) {
  769. add_note(wt, MSG_INFO, "BIP replay detected: SA=" MACSTR,
  770. MAC2STR(mgmt->sa));
  771. wpa_hexdump(MSG_INFO, "RX IPN", mmie + 2, 6);
  772. wpa_hexdump(MSG_INFO, "Last RX IPN", bss->ipn[keyid], 6);
  773. }
  774. if (check_mmie_mic(bss->mgmt_group_cipher, bss->igtk[keyid],
  775. bss->igtk_len[keyid], data, len) < 0) {
  776. add_note(wt, MSG_INFO, "Invalid MMIE MIC in a frame from "
  777. MACSTR, MAC2STR(mgmt->sa));
  778. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  779. return -1;
  780. }
  781. add_note(wt, MSG_DEBUG, "Valid MMIE MIC");
  782. os_memcpy(bss->ipn[keyid], mmie + 2, 6);
  783. bss->counters[WLANTEST_BSS_COUNTER_VALID_BIP_MMIE]++;
  784. if (stype == WLAN_FC_STYPE_DEAUTH)
  785. bss->counters[WLANTEST_BSS_COUNTER_BIP_DEAUTH]++;
  786. else if (stype == WLAN_FC_STYPE_DISASSOC)
  787. bss->counters[WLANTEST_BSS_COUNTER_BIP_DISASSOC]++;
  788. return 0;
  789. }
  790. static u8 * mgmt_ccmp_decrypt(struct wlantest *wt, const u8 *data, size_t len,
  791. size_t *dlen)
  792. {
  793. struct wlantest_bss *bss;
  794. struct wlantest_sta *sta;
  795. const struct ieee80211_hdr *hdr;
  796. int keyid;
  797. u8 *decrypted, *frame = NULL;
  798. u8 pn[6], *rsc;
  799. hdr = (const struct ieee80211_hdr *) data;
  800. bss = bss_get(wt, hdr->addr3);
  801. if (bss == NULL)
  802. return NULL;
  803. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  804. sta = sta_get(bss, hdr->addr2);
  805. else
  806. sta = sta_get(bss, hdr->addr1);
  807. if (sta == NULL || !sta->ptk_set) {
  808. add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
  809. return NULL;
  810. }
  811. if (len < 24 + 4)
  812. return NULL;
  813. if (!(data[24 + 3] & 0x20)) {
  814. add_note(wt, MSG_INFO, "Expected CCMP frame from " MACSTR
  815. " did not have ExtIV bit set to 1",
  816. MAC2STR(hdr->addr2));
  817. return NULL;
  818. }
  819. if (data[24 + 2] != 0 || (data[24 + 3] & 0x1f) != 0) {
  820. add_note(wt, MSG_INFO, "CCMP mgmt frame from " MACSTR " used "
  821. "non-zero reserved bit", MAC2STR(hdr->addr2));
  822. }
  823. keyid = data[24 + 3] >> 6;
  824. if (keyid != 0) {
  825. add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in "
  826. "individually addressed Management frame from "
  827. MACSTR, keyid, MAC2STR(hdr->addr2));
  828. }
  829. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  830. rsc = sta->rsc_tods[16];
  831. else
  832. rsc = sta->rsc_fromds[16];
  833. ccmp_get_pn(pn, data + 24);
  834. if (os_memcmp(pn, rsc, 6) <= 0) {
  835. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  836. add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  837. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
  838. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  839. MAC2STR(hdr->addr3),
  840. WLAN_GET_SEQ_SEQ(seq_ctrl),
  841. WLAN_GET_SEQ_FRAG(seq_ctrl),
  842. (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
  843. " Retry" : "");
  844. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  845. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  846. }
  847. decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data + 24, len - 24, dlen);
  848. if (decrypted) {
  849. os_memcpy(rsc, pn, 6);
  850. frame = os_malloc(24 + *dlen);
  851. if (frame) {
  852. os_memcpy(frame, data, 24);
  853. os_memcpy(frame + 24, decrypted, *dlen);
  854. *dlen += 24;
  855. }
  856. }
  857. os_free(decrypted);
  858. return frame;
  859. }
  860. static int check_mgmt_ccmp(struct wlantest *wt, const u8 *data, size_t len)
  861. {
  862. const struct ieee80211_mgmt *mgmt;
  863. u16 fc;
  864. struct wlantest_bss *bss;
  865. struct wlantest_sta *sta;
  866. mgmt = (const struct ieee80211_mgmt *) data;
  867. fc = le_to_host16(mgmt->frame_control);
  868. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
  869. if (len > 24 &&
  870. mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  871. return 0; /* Not a robust management frame */
  872. }
  873. bss = bss_get(wt, mgmt->bssid);
  874. if (bss == NULL)
  875. return 0;
  876. if (os_memcmp(mgmt->da, mgmt->bssid, ETH_ALEN) == 0)
  877. sta = sta_get(bss, mgmt->sa);
  878. else
  879. sta = sta_get(bss, mgmt->da);
  880. if (sta == NULL)
  881. return 0;
  882. if ((sta->rsn_capab & WPA_CAPABILITY_MFPC) &&
  883. (sta->state == STATE3 ||
  884. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION)) {
  885. add_note(wt, MSG_INFO, "Robust individually-addressed "
  886. "management frame sent without CCMP by "
  887. MACSTR, MAC2STR(mgmt->sa));
  888. return -1;
  889. }
  890. return 0;
  891. }
  892. void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
  893. {
  894. const struct ieee80211_hdr *hdr;
  895. u16 fc, stype;
  896. int valid = 1;
  897. u8 *decrypted = NULL;
  898. size_t dlen;
  899. if (len < 24)
  900. return;
  901. hdr = (const struct ieee80211_hdr *) data;
  902. fc = le_to_host16(hdr->frame_control);
  903. wt->rx_mgmt++;
  904. stype = WLAN_FC_GET_STYPE(fc);
  905. if ((hdr->addr1[0] & 0x01) &&
  906. (stype == WLAN_FC_STYPE_DEAUTH ||
  907. stype == WLAN_FC_STYPE_DISASSOC ||
  908. stype == WLAN_FC_STYPE_ACTION)) {
  909. if (check_bip(wt, data, len) < 0)
  910. valid = 0;
  911. }
  912. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  913. stype == WLAN_FC_STYPE_PROBE_RESP ||
  914. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  915. MSG_EXCESSIVE : MSG_MSGDUMP,
  916. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  917. mgmt_stype(stype),
  918. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  919. fc & WLAN_FC_ISWEP ? " Prot" : "",
  920. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  921. MAC2STR(hdr->addr3));
  922. if ((fc & WLAN_FC_ISWEP) &&
  923. !(hdr->addr1[0] & 0x01) &&
  924. (stype == WLAN_FC_STYPE_DEAUTH ||
  925. stype == WLAN_FC_STYPE_DISASSOC ||
  926. stype == WLAN_FC_STYPE_ACTION)) {
  927. decrypted = mgmt_ccmp_decrypt(wt, data, len, &dlen);
  928. if (decrypted) {
  929. write_pcap_decrypted(wt, decrypted, dlen, NULL, 0);
  930. data = decrypted;
  931. len = dlen;
  932. } else
  933. valid = 0;
  934. }
  935. if (!(fc & WLAN_FC_ISWEP) &&
  936. !(hdr->addr1[0] & 0x01) &&
  937. (stype == WLAN_FC_STYPE_DEAUTH ||
  938. stype == WLAN_FC_STYPE_DISASSOC ||
  939. stype == WLAN_FC_STYPE_ACTION)) {
  940. if (check_mgmt_ccmp(wt, data, len) < 0)
  941. valid = 0;
  942. }
  943. switch (stype) {
  944. case WLAN_FC_STYPE_BEACON:
  945. rx_mgmt_beacon(wt, data, len);
  946. break;
  947. case WLAN_FC_STYPE_PROBE_RESP:
  948. rx_mgmt_probe_resp(wt, data, len);
  949. break;
  950. case WLAN_FC_STYPE_AUTH:
  951. rx_mgmt_auth(wt, data, len);
  952. break;
  953. case WLAN_FC_STYPE_DEAUTH:
  954. rx_mgmt_deauth(wt, data, len, valid);
  955. break;
  956. case WLAN_FC_STYPE_ASSOC_REQ:
  957. rx_mgmt_assoc_req(wt, data, len);
  958. break;
  959. case WLAN_FC_STYPE_ASSOC_RESP:
  960. rx_mgmt_assoc_resp(wt, data, len);
  961. break;
  962. case WLAN_FC_STYPE_REASSOC_REQ:
  963. rx_mgmt_reassoc_req(wt, data, len);
  964. break;
  965. case WLAN_FC_STYPE_REASSOC_RESP:
  966. rx_mgmt_reassoc_resp(wt, data, len);
  967. break;
  968. case WLAN_FC_STYPE_DISASSOC:
  969. rx_mgmt_disassoc(wt, data, len, valid);
  970. break;
  971. case WLAN_FC_STYPE_ACTION:
  972. rx_mgmt_action(wt, data, len, valid);
  973. break;
  974. }
  975. os_free(decrypted);
  976. wt->last_mgmt_valid = valid;
  977. }
  978. static void rx_mgmt_deauth_ack(struct wlantest *wt,
  979. const struct ieee80211_hdr *hdr)
  980. {
  981. const struct ieee80211_mgmt *mgmt;
  982. struct wlantest_bss *bss;
  983. struct wlantest_sta *sta;
  984. mgmt = (const struct ieee80211_mgmt *) hdr;
  985. bss = bss_get(wt, mgmt->bssid);
  986. if (bss == NULL)
  987. return;
  988. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  989. sta = sta_get(bss, mgmt->da);
  990. else
  991. sta = sta_get(bss, mgmt->sa);
  992. if (sta == NULL)
  993. return;
  994. add_note(wt, MSG_DEBUG, "DEAUTH from " MACSTR " acknowledged by "
  995. MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  996. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  997. int c;
  998. c = wt->last_mgmt_valid ?
  999. WLANTEST_STA_COUNTER_VALID_DEAUTH_RX_ACK :
  1000. WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX_ACK;
  1001. sta->counters[c]++;
  1002. }
  1003. }
  1004. static void rx_mgmt_disassoc_ack(struct wlantest *wt,
  1005. const struct ieee80211_hdr *hdr)
  1006. {
  1007. const struct ieee80211_mgmt *mgmt;
  1008. struct wlantest_bss *bss;
  1009. struct wlantest_sta *sta;
  1010. mgmt = (const struct ieee80211_mgmt *) hdr;
  1011. bss = bss_get(wt, mgmt->bssid);
  1012. if (bss == NULL)
  1013. return;
  1014. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  1015. sta = sta_get(bss, mgmt->da);
  1016. else
  1017. sta = sta_get(bss, mgmt->sa);
  1018. if (sta == NULL)
  1019. return;
  1020. add_note(wt, MSG_DEBUG, "DISASSOC from " MACSTR " acknowledged by "
  1021. MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  1022. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  1023. int c;
  1024. c = wt->last_mgmt_valid ?
  1025. WLANTEST_STA_COUNTER_VALID_DISASSOC_RX_ACK :
  1026. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX_ACK;
  1027. sta->counters[c]++;
  1028. }
  1029. }
  1030. void rx_mgmt_ack(struct wlantest *wt, const struct ieee80211_hdr *hdr)
  1031. {
  1032. u16 fc, stype;
  1033. fc = le_to_host16(hdr->frame_control);
  1034. stype = WLAN_FC_GET_STYPE(fc);
  1035. wpa_printf(MSG_MSGDUMP, "MGMT ACK: stype=%u a1=" MACSTR " a2=" MACSTR
  1036. " a3=" MACSTR,
  1037. stype, MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1038. MAC2STR(hdr->addr3));
  1039. switch (stype) {
  1040. case WLAN_FC_STYPE_DEAUTH:
  1041. rx_mgmt_deauth_ack(wt, hdr);
  1042. break;
  1043. case WLAN_FC_STYPE_DISASSOC:
  1044. rx_mgmt_disassoc_ack(wt, hdr);
  1045. break;
  1046. }
  1047. }