rx_mgmt.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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_len != 5 ||
  301. elems.timeout_int[0] !=
  302. WLAN_TIMEOUT_ASSOC_COMEBACK) {
  303. add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
  304. "with Assoc Comeback time in AssocResp "
  305. "(status=30) from " MACSTR,
  306. MAC2STR(mgmt->sa));
  307. } else {
  308. sta->counters[
  309. WLANTEST_STA_COUNTER_ASSOCRESP_COMEBACK]++;
  310. }
  311. }
  312. if (status)
  313. return;
  314. if ((aid & 0xc000) != 0xc000) {
  315. add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  316. "in Association Response from " MACSTR,
  317. MAC2STR(mgmt->sa));
  318. }
  319. sta->aid = aid & 0xc000;
  320. if (sta->state < STATE2) {
  321. add_note(wt, MSG_DEBUG,
  322. "STA " MACSTR " was not in State 2 when "
  323. "getting associated", MAC2STR(sta->addr));
  324. }
  325. if (sta->state < STATE3) {
  326. add_note(wt, MSG_DEBUG, "STA " MACSTR
  327. " moved to State 3 with " MACSTR,
  328. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  329. sta->state = STATE3;
  330. }
  331. }
  332. static void rx_mgmt_reassoc_req(struct wlantest *wt, const u8 *data,
  333. size_t len)
  334. {
  335. const struct ieee80211_mgmt *mgmt;
  336. struct wlantest_bss *bss;
  337. struct wlantest_sta *sta;
  338. struct ieee802_11_elems elems;
  339. mgmt = (const struct ieee80211_mgmt *) data;
  340. bss = bss_get(wt, mgmt->bssid);
  341. if (bss == NULL)
  342. return;
  343. sta = sta_get(bss, mgmt->sa);
  344. if (sta == NULL)
  345. return;
  346. if (len < 24 + 4 + ETH_ALEN) {
  347. add_note(wt, MSG_INFO, "Too short Reassociation Request frame "
  348. "from " MACSTR, MAC2STR(mgmt->sa));
  349. return;
  350. }
  351. wpa_printf(MSG_DEBUG, "REASSOCREQ " MACSTR " -> " MACSTR
  352. " (capab=0x%x listen_int=%u current_ap=" MACSTR ")",
  353. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  354. le_to_host16(mgmt->u.reassoc_req.capab_info),
  355. le_to_host16(mgmt->u.reassoc_req.listen_interval),
  356. MAC2STR(mgmt->u.reassoc_req.current_ap));
  357. sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX]++;
  358. if (ieee802_11_parse_elems(mgmt->u.reassoc_req.variable,
  359. len - (mgmt->u.reassoc_req.variable - data),
  360. &elems, 0) == ParseFailed) {
  361. add_note(wt, MSG_INFO, "Invalid IEs in Reassociation Request "
  362. "frame from " MACSTR, MAC2STR(mgmt->sa));
  363. return;
  364. }
  365. sta->assocreq_capab_info =
  366. le_to_host16(mgmt->u.reassoc_req.capab_info);
  367. sta->assocreq_listen_int =
  368. le_to_host16(mgmt->u.reassoc_req.listen_interval);
  369. os_free(sta->assocreq_ies);
  370. sta->assocreq_ies_len = len - (mgmt->u.reassoc_req.variable - data);
  371. sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
  372. if (sta->assocreq_ies)
  373. os_memcpy(sta->assocreq_ies, mgmt->u.reassoc_req.variable,
  374. sta->assocreq_ies_len);
  375. sta_update_assoc(sta, &elems);
  376. }
  377. static void rx_mgmt_reassoc_resp(struct wlantest *wt, const u8 *data,
  378. size_t len)
  379. {
  380. const struct ieee80211_mgmt *mgmt;
  381. struct wlantest_bss *bss;
  382. struct wlantest_sta *sta;
  383. u16 capab, status, aid;
  384. mgmt = (const struct ieee80211_mgmt *) data;
  385. bss = bss_get(wt, mgmt->bssid);
  386. if (bss == NULL)
  387. return;
  388. sta = sta_get(bss, mgmt->da);
  389. if (sta == NULL)
  390. return;
  391. if (len < 24 + 6) {
  392. add_note(wt, MSG_INFO, "Too short Reassociation Response frame "
  393. "from " MACSTR, MAC2STR(mgmt->sa));
  394. return;
  395. }
  396. capab = le_to_host16(mgmt->u.reassoc_resp.capab_info);
  397. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  398. aid = le_to_host16(mgmt->u.reassoc_resp.aid);
  399. wpa_printf(MSG_DEBUG, "REASSOCRESP " MACSTR " -> " MACSTR
  400. " (capab=0x%x status=%u aid=%u)",
  401. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  402. aid & 0x3fff);
  403. if (status == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) {
  404. struct ieee802_11_elems elems;
  405. const u8 *ies = mgmt->u.reassoc_resp.variable;
  406. size_t ies_len = len - (mgmt->u.reassoc_resp.variable - data);
  407. if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
  408. ParseFailed) {
  409. add_note(wt, MSG_INFO, "Failed to parse IEs in "
  410. "ReassocResp from " MACSTR,
  411. MAC2STR(mgmt->sa));
  412. } else if (elems.timeout_int == NULL ||
  413. elems.timeout_int_len != 5 ||
  414. elems.timeout_int[0] !=
  415. WLAN_TIMEOUT_ASSOC_COMEBACK) {
  416. add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
  417. "with Assoc Comeback time in ReassocResp "
  418. "(status=30) from " MACSTR,
  419. MAC2STR(mgmt->sa));
  420. } else {
  421. sta->counters[
  422. WLANTEST_STA_COUNTER_REASSOCRESP_COMEBACK]++;
  423. }
  424. }
  425. if (status)
  426. return;
  427. if ((aid & 0xc000) != 0xc000) {
  428. add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  429. "in Reassociation Response from " MACSTR,
  430. MAC2STR(mgmt->sa));
  431. }
  432. sta->aid = aid & 0xc000;
  433. if (sta->state < STATE2) {
  434. add_note(wt, MSG_DEBUG,
  435. "STA " MACSTR " was not in State 2 when "
  436. "getting associated", MAC2STR(sta->addr));
  437. }
  438. if (sta->state < STATE3) {
  439. add_note(wt, MSG_DEBUG, "STA " MACSTR
  440. " moved to State 3 with " MACSTR,
  441. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  442. sta->state = STATE3;
  443. }
  444. }
  445. static void disassoc_all_stas(struct wlantest *wt, struct wlantest_bss *bss)
  446. {
  447. struct wlantest_sta *sta;
  448. dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
  449. if (sta->state <= STATE2)
  450. continue;
  451. add_note(wt, MSG_DEBUG, "STA " MACSTR
  452. " moved to State 2 with " MACSTR,
  453. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  454. sta->state = STATE2;
  455. }
  456. }
  457. static void rx_mgmt_disassoc(struct wlantest *wt, const u8 *data, size_t len,
  458. int valid)
  459. {
  460. const struct ieee80211_mgmt *mgmt;
  461. struct wlantest_bss *bss;
  462. struct wlantest_sta *sta;
  463. u16 fc, reason;
  464. mgmt = (const struct ieee80211_mgmt *) data;
  465. bss = bss_get(wt, mgmt->bssid);
  466. if (bss == NULL)
  467. return;
  468. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  469. sta = sta_get(bss, mgmt->da);
  470. else
  471. sta = sta_get(bss, mgmt->sa);
  472. if (len < 24 + 2) {
  473. add_note(wt, MSG_INFO, "Too short Disassociation frame from "
  474. MACSTR, MAC2STR(mgmt->sa));
  475. return;
  476. }
  477. reason = le_to_host16(mgmt->u.disassoc.reason_code);
  478. wpa_printf(MSG_DEBUG, "DISASSOC " MACSTR " -> " MACSTR
  479. " (reason=%u) (valid=%d)",
  480. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  481. reason, valid);
  482. wpa_hexdump(MSG_MSGDUMP, "DISASSOC payload", data + 24, len - 24);
  483. if (sta == NULL) {
  484. if (valid && mgmt->da[0] == 0xff)
  485. disassoc_all_stas(wt, bss);
  486. return;
  487. }
  488. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  489. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_RX :
  490. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX]++;
  491. if (sta->pwrmgt && !sta->pspoll)
  492. sta->counters[
  493. WLANTEST_STA_COUNTER_DISASSOC_RX_ASLEEP]++;
  494. else
  495. sta->counters[
  496. WLANTEST_STA_COUNTER_DISASSOC_RX_AWAKE]++;
  497. fc = le_to_host16(mgmt->frame_control);
  498. if (!(fc & WLAN_FC_ISWEP) && reason == 6)
  499. sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC6]++;
  500. else if (!(fc & WLAN_FC_ISWEP) && reason == 7)
  501. sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC7]++;
  502. } else
  503. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_TX :
  504. WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX]++;
  505. if (!valid) {
  506. add_note(wt, MSG_INFO, "Do not change STA " MACSTR " State "
  507. "since Disassociation frame was not protected "
  508. "correctly", MAC2STR(sta->addr));
  509. return;
  510. }
  511. if (sta->state < STATE2) {
  512. add_note(wt, MSG_DEBUG,
  513. "STA " MACSTR " was not in State 2 or 3 "
  514. "when getting disassociated", MAC2STR(sta->addr));
  515. }
  516. if (sta->state > STATE2) {
  517. add_note(wt, MSG_DEBUG, "STA " MACSTR
  518. " moved to State 2 with " MACSTR,
  519. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  520. sta->state = STATE2;
  521. }
  522. tdls_link_down(wt, bss, sta);
  523. }
  524. static void rx_mgmt_action_sa_query_req(struct wlantest *wt,
  525. struct wlantest_sta *sta,
  526. const struct ieee80211_mgmt *mgmt,
  527. size_t len, int valid)
  528. {
  529. const u8 *rx_id;
  530. u8 *id;
  531. rx_id = (const u8 *) mgmt->u.action.u.sa_query_req.trans_id;
  532. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  533. id = sta->ap_sa_query_tr;
  534. else
  535. id = sta->sta_sa_query_tr;
  536. add_note(wt, MSG_INFO, "SA Query Request " MACSTR " -> " MACSTR
  537. " (trans_id=%02x%02x)%s",
  538. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  539. valid ? "" : " (invalid protection)");
  540. os_memcpy(id, mgmt->u.action.u.sa_query_req.trans_id, 2);
  541. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  542. sta->counters[valid ?
  543. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX :
  544. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX]++;
  545. else
  546. sta->counters[valid ?
  547. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX :
  548. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX]++;
  549. }
  550. static void rx_mgmt_action_sa_query_resp(struct wlantest *wt,
  551. struct wlantest_sta *sta,
  552. const struct ieee80211_mgmt *mgmt,
  553. size_t len, int valid)
  554. {
  555. const u8 *rx_id;
  556. u8 *id;
  557. int match;
  558. rx_id = (const u8 *) mgmt->u.action.u.sa_query_resp.trans_id;
  559. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  560. id = sta->sta_sa_query_tr;
  561. else
  562. id = sta->ap_sa_query_tr;
  563. match = os_memcmp(rx_id, id, 2) == 0;
  564. add_note(wt, MSG_INFO, "SA Query Response " MACSTR " -> " MACSTR
  565. " (trans_id=%02x%02x; %s)%s",
  566. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  567. match ? "match" : "mismatch",
  568. valid ? "" : " (invalid protection)");
  569. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  570. sta->counters[(valid && match) ?
  571. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX :
  572. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX]++;
  573. else
  574. sta->counters[(valid && match) ?
  575. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX :
  576. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX]++;
  577. }
  578. static void rx_mgmt_action_sa_query(struct wlantest *wt,
  579. struct wlantest_sta *sta,
  580. const struct ieee80211_mgmt *mgmt,
  581. size_t len, int valid)
  582. {
  583. if (len < 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  584. add_note(wt, MSG_INFO, "Too short SA Query frame from " MACSTR,
  585. MAC2STR(mgmt->sa));
  586. return;
  587. }
  588. if (len > 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  589. size_t elen = len - (24 + 2 + WLAN_SA_QUERY_TR_ID_LEN);
  590. add_note(wt, MSG_INFO, "Unexpected %u octets of extra data at "
  591. "the end of SA Query frame from " MACSTR,
  592. (unsigned) elen, MAC2STR(mgmt->sa));
  593. wpa_hexdump(MSG_INFO, "SA Query extra data",
  594. ((const u8 *) mgmt) + len - elen, elen);
  595. }
  596. switch (mgmt->u.action.u.sa_query_req.action) {
  597. case WLAN_SA_QUERY_REQUEST:
  598. rx_mgmt_action_sa_query_req(wt, sta, mgmt, len, valid);
  599. break;
  600. case WLAN_SA_QUERY_RESPONSE:
  601. rx_mgmt_action_sa_query_resp(wt, sta, mgmt, len, valid);
  602. break;
  603. default:
  604. add_note(wt, MSG_INFO, "Unexpected SA Query action value %u "
  605. "from " MACSTR,
  606. mgmt->u.action.u.sa_query_req.action,
  607. MAC2STR(mgmt->sa));
  608. }
  609. }
  610. static void rx_mgmt_action(struct wlantest *wt, const u8 *data, size_t len,
  611. int valid)
  612. {
  613. const struct ieee80211_mgmt *mgmt;
  614. struct wlantest_bss *bss;
  615. struct wlantest_sta *sta;
  616. mgmt = (const struct ieee80211_mgmt *) data;
  617. if (mgmt->da[0] & 0x01) {
  618. add_note(wt, MSG_DEBUG, "Group addressed Action frame: DA="
  619. MACSTR " SA=" MACSTR " BSSID=" MACSTR
  620. " category=%u",
  621. MAC2STR(mgmt->da), MAC2STR(mgmt->sa),
  622. MAC2STR(mgmt->bssid), mgmt->u.action.category);
  623. return; /* Ignore group addressed Action frames for now */
  624. }
  625. bss = bss_get(wt, mgmt->bssid);
  626. if (bss == NULL)
  627. return;
  628. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  629. sta = sta_get(bss, mgmt->da);
  630. else
  631. sta = sta_get(bss, mgmt->sa);
  632. if (sta == NULL)
  633. return;
  634. if (len < 24 + 1) {
  635. add_note(wt, MSG_INFO, "Too short Action frame from " MACSTR,
  636. MAC2STR(mgmt->sa));
  637. return;
  638. }
  639. wpa_printf(MSG_DEBUG, "ACTION " MACSTR " -> " MACSTR
  640. " (category=%u) (valid=%d)",
  641. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  642. mgmt->u.action.category, valid);
  643. wpa_hexdump(MSG_MSGDUMP, "ACTION payload", data + 24, len - 24);
  644. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  645. sta->state < STATE3) {
  646. add_note(wt, MSG_INFO, "Action frame sent when STA is not in "
  647. "State 3 (SA=" MACSTR " DATA=" MACSTR ")",
  648. MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  649. }
  650. switch (mgmt->u.action.category) {
  651. case WLAN_ACTION_SA_QUERY:
  652. rx_mgmt_action_sa_query(wt, sta, mgmt, len, valid);
  653. break;
  654. }
  655. }
  656. static int check_mmie_mic(unsigned int mgmt_group_cipher,
  657. const u8 *igtk, size_t igtk_len,
  658. const u8 *data, size_t len)
  659. {
  660. u8 *buf;
  661. u8 mic[16];
  662. u16 fc;
  663. const struct ieee80211_hdr *hdr;
  664. int ret, mic_len;
  665. if (!mgmt_group_cipher || igtk_len < 16)
  666. return -1;
  667. mic_len = mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
  668. if (len < 24 || len - 24 < mic_len)
  669. return -1;
  670. buf = os_malloc(len + 20 - 24);
  671. if (buf == NULL)
  672. return -1;
  673. /* BIP AAD: FC(masked) A1 A2 A3 */
  674. hdr = (const struct ieee80211_hdr *) data;
  675. fc = le_to_host16(hdr->frame_control);
  676. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  677. WPA_PUT_LE16(buf, fc);
  678. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  679. /* Frame body with MMIE MIC masked to zero */
  680. os_memcpy(buf + 20, data + 24, len - 24 - mic_len);
  681. os_memset(buf + 20 + len - 24 - mic_len, 0, mic_len);
  682. wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, len + 20 - 24);
  683. /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
  684. if (mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  685. ret = omac1_aes_128(igtk, buf, len + 20 - 24, mic);
  686. } else if (mgmt_group_cipher == WPA_CIPHER_BIP_CMAC_256) {
  687. ret = omac1_aes_256(igtk, buf, len + 20 - 24, mic);
  688. } else if (mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_128 ||
  689. mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_256) {
  690. u8 nonce[12], *npos;
  691. const u8 *ipn;
  692. ipn = data + len - mic_len - 6;
  693. /* Nonce: A2 | IPN */
  694. os_memcpy(nonce, hdr->addr2, ETH_ALEN);
  695. npos = nonce + ETH_ALEN;
  696. *npos++ = ipn[5];
  697. *npos++ = ipn[4];
  698. *npos++ = ipn[3];
  699. *npos++ = ipn[2];
  700. *npos++ = ipn[1];
  701. *npos++ = ipn[0];
  702. ret = aes_gmac(igtk, igtk_len, nonce, sizeof(nonce),
  703. buf, len + 20 - 24, mic);
  704. } else {
  705. ret = -1;
  706. }
  707. if (ret < 0) {
  708. os_free(buf);
  709. return -1;
  710. }
  711. os_free(buf);
  712. if (os_memcmp(data + len - mic_len, mic, mic_len) != 0)
  713. return -1;
  714. return 0;
  715. }
  716. static int check_bip(struct wlantest *wt, const u8 *data, size_t len)
  717. {
  718. const struct ieee80211_mgmt *mgmt;
  719. u16 fc, stype;
  720. const u8 *mmie;
  721. u16 keyid;
  722. struct wlantest_bss *bss;
  723. size_t mic_len;
  724. mgmt = (const struct ieee80211_mgmt *) data;
  725. fc = le_to_host16(mgmt->frame_control);
  726. stype = WLAN_FC_GET_STYPE(fc);
  727. if (stype == WLAN_FC_STYPE_ACTION) {
  728. if (len < 24 + 1)
  729. return 0;
  730. if (mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  731. return 0; /* Not a robust management frame */
  732. }
  733. bss = bss_get(wt, mgmt->bssid);
  734. if (bss == NULL)
  735. return 0; /* No key known yet */
  736. mic_len = bss->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
  737. if (len < 24 + 10 + mic_len ||
  738. data[len - (10 + mic_len)] != WLAN_EID_MMIE ||
  739. data[len - (10 + mic_len - 1)] != 8 + mic_len) {
  740. /* No MMIE */
  741. if (bss->rsn_capab & WPA_CAPABILITY_MFPC) {
  742. add_note(wt, MSG_INFO, "Robust group-addressed "
  743. "management frame sent without BIP by "
  744. MACSTR, MAC2STR(mgmt->sa));
  745. bss->counters[WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE]++;
  746. return -1;
  747. }
  748. return 0;
  749. }
  750. mmie = data + len - (8 + mic_len);
  751. keyid = WPA_GET_LE16(mmie);
  752. if (keyid & 0xf000) {
  753. add_note(wt, MSG_INFO, "MMIE KeyID reserved bits not zero "
  754. "(%04x) from " MACSTR, keyid, MAC2STR(mgmt->sa));
  755. keyid &= 0x0fff;
  756. }
  757. if (keyid < 4 || keyid > 5) {
  758. add_note(wt, MSG_INFO, "Unexpected MMIE KeyID %u from " MACSTR,
  759. keyid, MAC2STR(mgmt->sa));
  760. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  761. return 0;
  762. }
  763. wpa_printf(MSG_DEBUG, "MMIE KeyID %u", keyid);
  764. wpa_hexdump(MSG_MSGDUMP, "MMIE IPN", mmie + 2, 6);
  765. wpa_hexdump(MSG_MSGDUMP, "MMIE MIC", mmie + 8, mic_len);
  766. if (!bss->igtk_len[keyid]) {
  767. add_note(wt, MSG_DEBUG, "No IGTK known to validate BIP frame");
  768. return 0;
  769. }
  770. if (os_memcmp(mmie + 2, bss->ipn[keyid], 6) <= 0) {
  771. add_note(wt, MSG_INFO, "BIP replay detected: SA=" MACSTR,
  772. MAC2STR(mgmt->sa));
  773. wpa_hexdump(MSG_INFO, "RX IPN", mmie + 2, 6);
  774. wpa_hexdump(MSG_INFO, "Last RX IPN", bss->ipn[keyid], 6);
  775. }
  776. if (check_mmie_mic(bss->mgmt_group_cipher, bss->igtk[keyid],
  777. bss->igtk_len[keyid], data, len) < 0) {
  778. add_note(wt, MSG_INFO, "Invalid MMIE MIC in a frame from "
  779. MACSTR, MAC2STR(mgmt->sa));
  780. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  781. return -1;
  782. }
  783. add_note(wt, MSG_DEBUG, "Valid MMIE MIC");
  784. os_memcpy(bss->ipn[keyid], mmie + 2, 6);
  785. bss->counters[WLANTEST_BSS_COUNTER_VALID_BIP_MMIE]++;
  786. if (stype == WLAN_FC_STYPE_DEAUTH)
  787. bss->counters[WLANTEST_BSS_COUNTER_BIP_DEAUTH]++;
  788. else if (stype == WLAN_FC_STYPE_DISASSOC)
  789. bss->counters[WLANTEST_BSS_COUNTER_BIP_DISASSOC]++;
  790. return 0;
  791. }
  792. static u8 * mgmt_ccmp_decrypt(struct wlantest *wt, const u8 *data, size_t len,
  793. size_t *dlen)
  794. {
  795. struct wlantest_bss *bss;
  796. struct wlantest_sta *sta;
  797. const struct ieee80211_hdr *hdr;
  798. int keyid;
  799. u8 *decrypted, *frame = NULL;
  800. u8 pn[6], *rsc;
  801. hdr = (const struct ieee80211_hdr *) data;
  802. bss = bss_get(wt, hdr->addr3);
  803. if (bss == NULL)
  804. return NULL;
  805. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  806. sta = sta_get(bss, hdr->addr2);
  807. else
  808. sta = sta_get(bss, hdr->addr1);
  809. if (sta == NULL || !sta->ptk_set) {
  810. add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
  811. return NULL;
  812. }
  813. if (len < 24 + 4)
  814. return NULL;
  815. if (!(data[24 + 3] & 0x20)) {
  816. add_note(wt, MSG_INFO, "Expected CCMP frame from " MACSTR
  817. " did not have ExtIV bit set to 1",
  818. MAC2STR(hdr->addr2));
  819. return NULL;
  820. }
  821. if (data[24 + 2] != 0 || (data[24 + 3] & 0x1f) != 0) {
  822. add_note(wt, MSG_INFO, "CCMP mgmt frame from " MACSTR " used "
  823. "non-zero reserved bit", MAC2STR(hdr->addr2));
  824. }
  825. keyid = data[24 + 3] >> 6;
  826. if (keyid != 0) {
  827. add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in "
  828. "individually addressed Management frame from "
  829. MACSTR, keyid, MAC2STR(hdr->addr2));
  830. }
  831. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  832. rsc = sta->rsc_tods[16];
  833. else
  834. rsc = sta->rsc_fromds[16];
  835. ccmp_get_pn(pn, data + 24);
  836. if (os_memcmp(pn, rsc, 6) <= 0) {
  837. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  838. add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  839. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
  840. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  841. MAC2STR(hdr->addr3),
  842. WLAN_GET_SEQ_SEQ(seq_ctrl),
  843. WLAN_GET_SEQ_FRAG(seq_ctrl),
  844. (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
  845. " Retry" : "");
  846. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  847. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  848. }
  849. decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data + 24, len - 24, dlen);
  850. if (decrypted) {
  851. os_memcpy(rsc, pn, 6);
  852. frame = os_malloc(24 + *dlen);
  853. if (frame) {
  854. os_memcpy(frame, data, 24);
  855. os_memcpy(frame + 24, decrypted, *dlen);
  856. *dlen += 24;
  857. }
  858. }
  859. os_free(decrypted);
  860. return frame;
  861. }
  862. static int check_mgmt_ccmp(struct wlantest *wt, const u8 *data, size_t len)
  863. {
  864. const struct ieee80211_mgmt *mgmt;
  865. u16 fc;
  866. struct wlantest_bss *bss;
  867. struct wlantest_sta *sta;
  868. mgmt = (const struct ieee80211_mgmt *) data;
  869. fc = le_to_host16(mgmt->frame_control);
  870. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
  871. if (len > 24 &&
  872. mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  873. return 0; /* Not a robust management frame */
  874. }
  875. bss = bss_get(wt, mgmt->bssid);
  876. if (bss == NULL)
  877. return 0;
  878. if (os_memcmp(mgmt->da, mgmt->bssid, ETH_ALEN) == 0)
  879. sta = sta_get(bss, mgmt->sa);
  880. else
  881. sta = sta_get(bss, mgmt->da);
  882. if (sta == NULL)
  883. return 0;
  884. if ((sta->rsn_capab & WPA_CAPABILITY_MFPC) &&
  885. (sta->state == STATE3 ||
  886. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION)) {
  887. add_note(wt, MSG_INFO, "Robust individually-addressed "
  888. "management frame sent without CCMP by "
  889. MACSTR, MAC2STR(mgmt->sa));
  890. return -1;
  891. }
  892. return 0;
  893. }
  894. void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
  895. {
  896. const struct ieee80211_hdr *hdr;
  897. u16 fc, stype;
  898. int valid = 1;
  899. u8 *decrypted = NULL;
  900. size_t dlen;
  901. if (len < 24)
  902. return;
  903. hdr = (const struct ieee80211_hdr *) data;
  904. fc = le_to_host16(hdr->frame_control);
  905. wt->rx_mgmt++;
  906. stype = WLAN_FC_GET_STYPE(fc);
  907. if ((hdr->addr1[0] & 0x01) &&
  908. (stype == WLAN_FC_STYPE_DEAUTH ||
  909. stype == WLAN_FC_STYPE_DISASSOC ||
  910. stype == WLAN_FC_STYPE_ACTION)) {
  911. if (check_bip(wt, data, len) < 0)
  912. valid = 0;
  913. }
  914. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  915. stype == WLAN_FC_STYPE_PROBE_RESP ||
  916. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  917. MSG_EXCESSIVE : MSG_MSGDUMP,
  918. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  919. mgmt_stype(stype),
  920. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  921. fc & WLAN_FC_ISWEP ? " Prot" : "",
  922. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  923. MAC2STR(hdr->addr3));
  924. if ((fc & WLAN_FC_ISWEP) &&
  925. !(hdr->addr1[0] & 0x01) &&
  926. (stype == WLAN_FC_STYPE_DEAUTH ||
  927. stype == WLAN_FC_STYPE_DISASSOC ||
  928. stype == WLAN_FC_STYPE_ACTION)) {
  929. decrypted = mgmt_ccmp_decrypt(wt, data, len, &dlen);
  930. if (decrypted) {
  931. write_pcap_decrypted(wt, decrypted, dlen, NULL, 0);
  932. data = decrypted;
  933. len = dlen;
  934. } else
  935. valid = 0;
  936. }
  937. if (!(fc & WLAN_FC_ISWEP) &&
  938. !(hdr->addr1[0] & 0x01) &&
  939. (stype == WLAN_FC_STYPE_DEAUTH ||
  940. stype == WLAN_FC_STYPE_DISASSOC ||
  941. stype == WLAN_FC_STYPE_ACTION)) {
  942. if (check_mgmt_ccmp(wt, data, len) < 0)
  943. valid = 0;
  944. }
  945. switch (stype) {
  946. case WLAN_FC_STYPE_BEACON:
  947. rx_mgmt_beacon(wt, data, len);
  948. break;
  949. case WLAN_FC_STYPE_PROBE_RESP:
  950. rx_mgmt_probe_resp(wt, data, len);
  951. break;
  952. case WLAN_FC_STYPE_AUTH:
  953. rx_mgmt_auth(wt, data, len);
  954. break;
  955. case WLAN_FC_STYPE_DEAUTH:
  956. rx_mgmt_deauth(wt, data, len, valid);
  957. break;
  958. case WLAN_FC_STYPE_ASSOC_REQ:
  959. rx_mgmt_assoc_req(wt, data, len);
  960. break;
  961. case WLAN_FC_STYPE_ASSOC_RESP:
  962. rx_mgmt_assoc_resp(wt, data, len);
  963. break;
  964. case WLAN_FC_STYPE_REASSOC_REQ:
  965. rx_mgmt_reassoc_req(wt, data, len);
  966. break;
  967. case WLAN_FC_STYPE_REASSOC_RESP:
  968. rx_mgmt_reassoc_resp(wt, data, len);
  969. break;
  970. case WLAN_FC_STYPE_DISASSOC:
  971. rx_mgmt_disassoc(wt, data, len, valid);
  972. break;
  973. case WLAN_FC_STYPE_ACTION:
  974. rx_mgmt_action(wt, data, len, valid);
  975. break;
  976. }
  977. os_free(decrypted);
  978. wt->last_mgmt_valid = valid;
  979. }
  980. static void rx_mgmt_deauth_ack(struct wlantest *wt,
  981. const struct ieee80211_hdr *hdr)
  982. {
  983. const struct ieee80211_mgmt *mgmt;
  984. struct wlantest_bss *bss;
  985. struct wlantest_sta *sta;
  986. mgmt = (const struct ieee80211_mgmt *) hdr;
  987. bss = bss_get(wt, mgmt->bssid);
  988. if (bss == NULL)
  989. return;
  990. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  991. sta = sta_get(bss, mgmt->da);
  992. else
  993. sta = sta_get(bss, mgmt->sa);
  994. if (sta == NULL)
  995. return;
  996. add_note(wt, MSG_DEBUG, "DEAUTH from " MACSTR " acknowledged by "
  997. MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  998. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  999. int c;
  1000. c = wt->last_mgmt_valid ?
  1001. WLANTEST_STA_COUNTER_VALID_DEAUTH_RX_ACK :
  1002. WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX_ACK;
  1003. sta->counters[c]++;
  1004. }
  1005. }
  1006. static void rx_mgmt_disassoc_ack(struct wlantest *wt,
  1007. const struct ieee80211_hdr *hdr)
  1008. {
  1009. const struct ieee80211_mgmt *mgmt;
  1010. struct wlantest_bss *bss;
  1011. struct wlantest_sta *sta;
  1012. mgmt = (const struct ieee80211_mgmt *) hdr;
  1013. bss = bss_get(wt, mgmt->bssid);
  1014. if (bss == NULL)
  1015. return;
  1016. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  1017. sta = sta_get(bss, mgmt->da);
  1018. else
  1019. sta = sta_get(bss, mgmt->sa);
  1020. if (sta == NULL)
  1021. return;
  1022. add_note(wt, MSG_DEBUG, "DISASSOC from " MACSTR " acknowledged by "
  1023. MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  1024. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
  1025. int c;
  1026. c = wt->last_mgmt_valid ?
  1027. WLANTEST_STA_COUNTER_VALID_DISASSOC_RX_ACK :
  1028. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX_ACK;
  1029. sta->counters[c]++;
  1030. }
  1031. }
  1032. void rx_mgmt_ack(struct wlantest *wt, const struct ieee80211_hdr *hdr)
  1033. {
  1034. u16 fc, stype;
  1035. fc = le_to_host16(hdr->frame_control);
  1036. stype = WLAN_FC_GET_STYPE(fc);
  1037. wpa_printf(MSG_MSGDUMP, "MGMT ACK: stype=%u a1=" MACSTR " a2=" MACSTR
  1038. " a3=" MACSTR,
  1039. stype, MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  1040. MAC2STR(hdr->addr3));
  1041. switch (stype) {
  1042. case WLAN_FC_STYPE_DEAUTH:
  1043. rx_mgmt_deauth_ack(wt, hdr);
  1044. break;
  1045. case WLAN_FC_STYPE_DISASSOC:
  1046. rx_mgmt_disassoc_ack(wt, hdr);
  1047. break;
  1048. }
  1049. }