rx_mgmt.c 33 KB

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