rx_mgmt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * Received Management frame processing
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "common/ieee802_11_common.h"
  18. #include "crypto/aes_wrap.h"
  19. #include "wlantest.h"
  20. static const char * mgmt_stype(u16 stype)
  21. {
  22. switch (stype) {
  23. case WLAN_FC_STYPE_ASSOC_REQ:
  24. return "ASSOC-REQ";
  25. case WLAN_FC_STYPE_ASSOC_RESP:
  26. return "ASSOC-RESP";
  27. case WLAN_FC_STYPE_REASSOC_REQ:
  28. return "REASSOC-REQ";
  29. case WLAN_FC_STYPE_REASSOC_RESP:
  30. return "REASSOC-RESP";
  31. case WLAN_FC_STYPE_PROBE_REQ:
  32. return "PROBE-REQ";
  33. case WLAN_FC_STYPE_PROBE_RESP:
  34. return "PROBE-RESP";
  35. case WLAN_FC_STYPE_BEACON:
  36. return "BEACON";
  37. case WLAN_FC_STYPE_ATIM:
  38. return "ATIM";
  39. case WLAN_FC_STYPE_DISASSOC:
  40. return "DISASSOC";
  41. case WLAN_FC_STYPE_AUTH:
  42. return "AUTH";
  43. case WLAN_FC_STYPE_DEAUTH:
  44. return "DEAUTH";
  45. case WLAN_FC_STYPE_ACTION:
  46. return "ACTION";
  47. }
  48. return "??";
  49. }
  50. static void rx_mgmt_beacon(struct wlantest *wt, const u8 *data, size_t len)
  51. {
  52. const struct ieee80211_mgmt *mgmt;
  53. struct wlantest_bss *bss;
  54. struct ieee802_11_elems elems;
  55. mgmt = (const struct ieee80211_mgmt *) data;
  56. bss = bss_get(wt, mgmt->bssid);
  57. if (bss == NULL)
  58. return;
  59. if (bss->proberesp_seen)
  60. return; /* do not override with Beacon data */
  61. bss->capab_info = le_to_host16(mgmt->u.beacon.capab_info);
  62. if (ieee802_11_parse_elems(mgmt->u.beacon.variable,
  63. len - (mgmt->u.beacon.variable - data),
  64. &elems, 0) == ParseFailed) {
  65. if (bss->parse_error_reported)
  66. return;
  67. wpa_printf(MSG_INFO, "Invalid IEs in a Beacon frame from "
  68. MACSTR, MAC2STR(mgmt->sa));
  69. bss->parse_error_reported = 1;
  70. return;
  71. }
  72. bss_update(wt, bss, &elems);
  73. }
  74. static void rx_mgmt_probe_resp(struct wlantest *wt, const u8 *data, size_t len)
  75. {
  76. const struct ieee80211_mgmt *mgmt;
  77. struct wlantest_bss *bss;
  78. struct ieee802_11_elems elems;
  79. mgmt = (const struct ieee80211_mgmt *) data;
  80. bss = bss_get(wt, mgmt->bssid);
  81. if (bss == NULL)
  82. return;
  83. bss->capab_info = le_to_host16(mgmt->u.probe_resp.capab_info);
  84. if (ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
  85. len - (mgmt->u.probe_resp.variable - data),
  86. &elems, 0) == ParseFailed) {
  87. if (bss->parse_error_reported)
  88. return;
  89. wpa_printf(MSG_INFO, "Invalid IEs in a Probe Response frame "
  90. "from " MACSTR, MAC2STR(mgmt->sa));
  91. bss->parse_error_reported = 1;
  92. return;
  93. }
  94. bss_update(wt, bss, &elems);
  95. }
  96. static void rx_mgmt_auth(struct wlantest *wt, const u8 *data, size_t len)
  97. {
  98. const struct ieee80211_mgmt *mgmt;
  99. struct wlantest_bss *bss;
  100. struct wlantest_sta *sta;
  101. u16 alg, trans, status;
  102. mgmt = (const struct ieee80211_mgmt *) data;
  103. bss = bss_get(wt, mgmt->bssid);
  104. if (bss == NULL)
  105. return;
  106. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  107. sta = sta_get(bss, mgmt->da);
  108. else
  109. sta = sta_get(bss, mgmt->sa);
  110. if (sta == NULL)
  111. return;
  112. if (len < 24 + 6) {
  113. wpa_printf(MSG_INFO, "Too short Authentication frame from "
  114. MACSTR, MAC2STR(mgmt->sa));
  115. return;
  116. }
  117. alg = le_to_host16(mgmt->u.auth.auth_alg);
  118. trans = le_to_host16(mgmt->u.auth.auth_transaction);
  119. status = le_to_host16(mgmt->u.auth.status_code);
  120. wpa_printf(MSG_DEBUG, "AUTH " MACSTR " -> " MACSTR
  121. " (alg=%u trans=%u status=%u)",
  122. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), alg, trans, status);
  123. if (alg == 0 && trans == 2 && status == 0) {
  124. if (sta->state == STATE1) {
  125. wpa_printf(MSG_DEBUG, "STA " MACSTR
  126. " moved to State 2 with " MACSTR,
  127. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  128. sta->state = STATE2;
  129. }
  130. }
  131. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  132. sta->counters[WLANTEST_STA_COUNTER_AUTH_RX]++;
  133. else
  134. sta->counters[WLANTEST_STA_COUNTER_AUTH_TX]++;
  135. }
  136. static void rx_mgmt_deauth(struct wlantest *wt, const u8 *data, size_t len,
  137. int valid)
  138. {
  139. const struct ieee80211_mgmt *mgmt;
  140. struct wlantest_bss *bss;
  141. struct wlantest_sta *sta;
  142. mgmt = (const struct ieee80211_mgmt *) data;
  143. bss = bss_get(wt, mgmt->bssid);
  144. if (bss == NULL)
  145. return;
  146. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  147. sta = sta_get(bss, mgmt->da);
  148. else
  149. sta = sta_get(bss, mgmt->sa);
  150. if (sta == NULL)
  151. return;
  152. if (len < 24 + 2) {
  153. wpa_printf(MSG_INFO, "Too short Deauthentication frame from "
  154. MACSTR, MAC2STR(mgmt->sa));
  155. return;
  156. }
  157. wpa_printf(MSG_DEBUG, "DEAUTH " MACSTR " -> " MACSTR
  158. " (reason=%u)",
  159. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  160. le_to_host16(mgmt->u.deauth.reason_code));
  161. wpa_hexdump(MSG_MSGDUMP, "DEAUTH payload", data + 24, len - 24);
  162. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  163. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DEAUTH_RX :
  164. WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX]++;
  165. else
  166. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DEAUTH_TX :
  167. WLANTEST_STA_COUNTER_INVALID_DEAUTH_TX]++;
  168. if (!valid) {
  169. wpa_printf(MSG_INFO, "Do not change STA " MACSTR " State "
  170. "since Disassociation frame was not protected "
  171. "correctly", MAC2STR(sta->addr));
  172. return;
  173. }
  174. if (sta->state != STATE1) {
  175. wpa_printf(MSG_DEBUG, "STA " MACSTR
  176. " moved to State 1 with " MACSTR,
  177. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  178. sta->state = STATE1;
  179. }
  180. }
  181. static void rx_mgmt_assoc_req(struct wlantest *wt, const u8 *data, size_t len)
  182. {
  183. const struct ieee80211_mgmt *mgmt;
  184. struct wlantest_bss *bss;
  185. struct wlantest_sta *sta;
  186. struct ieee802_11_elems elems;
  187. mgmt = (const struct ieee80211_mgmt *) data;
  188. bss = bss_get(wt, mgmt->bssid);
  189. if (bss == NULL)
  190. return;
  191. sta = sta_get(bss, mgmt->sa);
  192. if (sta == NULL)
  193. return;
  194. if (len < 24 + 4) {
  195. wpa_printf(MSG_INFO, "Too short Association Request frame "
  196. "from " MACSTR, MAC2STR(mgmt->sa));
  197. return;
  198. }
  199. wpa_printf(MSG_DEBUG, "ASSOCREQ " MACSTR " -> " MACSTR
  200. " (capab=0x%x listen_int=%u)",
  201. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  202. le_to_host16(mgmt->u.assoc_req.capab_info),
  203. le_to_host16(mgmt->u.assoc_req.listen_interval));
  204. sta->counters[WLANTEST_STA_COUNTER_ASSOCREQ_TX]++;
  205. if (ieee802_11_parse_elems(mgmt->u.assoc_req.variable,
  206. len - (mgmt->u.assoc_req.variable - data),
  207. &elems, 0) == ParseFailed) {
  208. wpa_printf(MSG_INFO, "Invalid IEs in Association Request "
  209. "frame from " MACSTR, MAC2STR(mgmt->sa));
  210. return;
  211. }
  212. sta->assocreq_capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  213. sta->assocreq_listen_int =
  214. le_to_host16(mgmt->u.assoc_req.listen_interval);
  215. os_free(sta->assocreq_ies);
  216. sta->assocreq_ies_len = len - (mgmt->u.assoc_req.variable - data);
  217. sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
  218. if (sta->assocreq_ies)
  219. os_memcpy(sta->assocreq_ies, mgmt->u.assoc_req.variable,
  220. sta->assocreq_ies_len);
  221. sta_update_assoc(sta, &elems);
  222. }
  223. static void rx_mgmt_assoc_resp(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. u16 capab, status, aid;
  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->da);
  234. if (sta == NULL)
  235. return;
  236. if (len < 24 + 6) {
  237. wpa_printf(MSG_INFO, "Too short Association Response frame "
  238. "from " MACSTR, MAC2STR(mgmt->sa));
  239. return;
  240. }
  241. capab = le_to_host16(mgmt->u.assoc_resp.capab_info);
  242. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  243. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  244. wpa_printf(MSG_DEBUG, "ASSOCRESP " MACSTR " -> " MACSTR
  245. " (capab=0x%x status=%u aid=%u)",
  246. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  247. aid & 0x3fff);
  248. if (status)
  249. return;
  250. if ((aid & 0xc000) != 0xc000) {
  251. wpa_printf(MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  252. "in Association Response from " MACSTR,
  253. MAC2STR(mgmt->sa));
  254. }
  255. sta->aid = aid & 0xc000;
  256. if (sta->state < STATE2) {
  257. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 when "
  258. "getting associated", MAC2STR(sta->addr));
  259. }
  260. if (sta->state < STATE3) {
  261. wpa_printf(MSG_DEBUG, "STA " MACSTR
  262. " moved to State 3 with " MACSTR,
  263. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  264. sta->state = STATE3;
  265. }
  266. }
  267. static void rx_mgmt_reassoc_req(struct wlantest *wt, const u8 *data,
  268. size_t len)
  269. {
  270. const struct ieee80211_mgmt *mgmt;
  271. struct wlantest_bss *bss;
  272. struct wlantest_sta *sta;
  273. struct ieee802_11_elems elems;
  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->sa);
  279. if (sta == NULL)
  280. return;
  281. if (len < 24 + 4 + ETH_ALEN) {
  282. wpa_printf(MSG_INFO, "Too short Reassociation Request frame "
  283. "from " MACSTR, MAC2STR(mgmt->sa));
  284. return;
  285. }
  286. wpa_printf(MSG_DEBUG, "REASSOCREQ " MACSTR " -> " MACSTR
  287. " (capab=0x%x listen_int=%u current_ap=" MACSTR ")",
  288. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  289. le_to_host16(mgmt->u.reassoc_req.capab_info),
  290. le_to_host16(mgmt->u.reassoc_req.listen_interval),
  291. MAC2STR(mgmt->u.reassoc_req.current_ap));
  292. sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX]++;
  293. if (ieee802_11_parse_elems(mgmt->u.reassoc_req.variable,
  294. len - (mgmt->u.reassoc_req.variable - data),
  295. &elems, 0) == ParseFailed) {
  296. wpa_printf(MSG_INFO, "Invalid IEs in Reassociation Request "
  297. "frame from " MACSTR, MAC2STR(mgmt->sa));
  298. return;
  299. }
  300. sta->assocreq_capab_info =
  301. le_to_host16(mgmt->u.reassoc_req.capab_info);
  302. sta->assocreq_listen_int =
  303. le_to_host16(mgmt->u.reassoc_req.listen_interval);
  304. os_free(sta->assocreq_ies);
  305. sta->assocreq_ies_len = len - (mgmt->u.reassoc_req.variable - data);
  306. sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
  307. if (sta->assocreq_ies)
  308. os_memcpy(sta->assocreq_ies, mgmt->u.reassoc_req.variable,
  309. sta->assocreq_ies_len);
  310. sta_update_assoc(sta, &elems);
  311. }
  312. static void rx_mgmt_reassoc_resp(struct wlantest *wt, const u8 *data,
  313. size_t len)
  314. {
  315. const struct ieee80211_mgmt *mgmt;
  316. struct wlantest_bss *bss;
  317. struct wlantest_sta *sta;
  318. u16 capab, status, aid;
  319. mgmt = (const struct ieee80211_mgmt *) data;
  320. bss = bss_get(wt, mgmt->bssid);
  321. if (bss == NULL)
  322. return;
  323. sta = sta_get(bss, mgmt->da);
  324. if (sta == NULL)
  325. return;
  326. if (len < 24 + 6) {
  327. wpa_printf(MSG_INFO, "Too short Reassociation Response frame "
  328. "from " MACSTR, MAC2STR(mgmt->sa));
  329. return;
  330. }
  331. capab = le_to_host16(mgmt->u.reassoc_resp.capab_info);
  332. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  333. aid = le_to_host16(mgmt->u.reassoc_resp.aid);
  334. wpa_printf(MSG_DEBUG, "REASSOCRESP " MACSTR " -> " MACSTR
  335. " (capab=0x%x status=%u aid=%u)",
  336. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  337. aid & 0x3fff);
  338. if (status)
  339. return;
  340. if ((aid & 0xc000) != 0xc000) {
  341. wpa_printf(MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  342. "in Reassociation Response from " MACSTR,
  343. MAC2STR(mgmt->sa));
  344. }
  345. sta->aid = aid & 0xc000;
  346. if (sta->state < STATE2) {
  347. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 when "
  348. "getting associated", MAC2STR(sta->addr));
  349. }
  350. if (sta->state < STATE3) {
  351. wpa_printf(MSG_DEBUG, "STA " MACSTR
  352. " moved to State 3 with " MACSTR,
  353. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  354. sta->state = STATE3;
  355. }
  356. }
  357. static void rx_mgmt_disassoc(struct wlantest *wt, const u8 *data, size_t len,
  358. int valid)
  359. {
  360. const struct ieee80211_mgmt *mgmt;
  361. struct wlantest_bss *bss;
  362. struct wlantest_sta *sta;
  363. mgmt = (const struct ieee80211_mgmt *) data;
  364. bss = bss_get(wt, mgmt->bssid);
  365. if (bss == NULL)
  366. return;
  367. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  368. sta = sta_get(bss, mgmt->da);
  369. else
  370. sta = sta_get(bss, mgmt->sa);
  371. if (sta == NULL)
  372. return;
  373. if (len < 24 + 2) {
  374. wpa_printf(MSG_INFO, "Too short Disassociation frame from "
  375. MACSTR, MAC2STR(mgmt->sa));
  376. return;
  377. }
  378. wpa_printf(MSG_DEBUG, "DISASSOC " MACSTR " -> " MACSTR
  379. " (reason=%u)",
  380. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  381. le_to_host16(mgmt->u.disassoc.reason_code));
  382. wpa_hexdump(MSG_MSGDUMP, "DISASSOC payload", data + 24, len - 24);
  383. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  384. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_RX :
  385. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX]++;
  386. else
  387. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_TX :
  388. WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX]++;
  389. if (!valid) {
  390. wpa_printf(MSG_INFO, "Do not change STA " MACSTR " State "
  391. "since Disassociation frame was not protected "
  392. "correctly", MAC2STR(sta->addr));
  393. return;
  394. }
  395. if (sta->state < STATE2) {
  396. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 or 3 "
  397. "when getting disassociated", MAC2STR(sta->addr));
  398. }
  399. if (sta->state > STATE2) {
  400. wpa_printf(MSG_DEBUG, "STA " MACSTR
  401. " moved to State 2 with " MACSTR,
  402. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  403. sta->state = STATE2;
  404. }
  405. }
  406. static void rx_mgmt_action_sa_query_req(struct wlantest *wt,
  407. struct wlantest_sta *sta,
  408. const struct ieee80211_mgmt *mgmt,
  409. size_t len, int valid)
  410. {
  411. const u8 *rx_id;
  412. u8 *id;
  413. rx_id = (const u8 *) mgmt->u.action.u.sa_query_req.trans_id;
  414. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  415. id = sta->ap_sa_query_tr;
  416. else
  417. id = sta->sta_sa_query_tr;
  418. wpa_printf(MSG_INFO, "SA Query Request " MACSTR " -> " MACSTR
  419. " (trans_id=%02x%02x)%s",
  420. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  421. valid ? "" : " (invalid protection)");
  422. os_memcpy(id, mgmt->u.action.u.sa_query_req.trans_id, 2);
  423. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  424. sta->counters[valid ?
  425. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX :
  426. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX]++;
  427. else
  428. sta->counters[valid ?
  429. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX :
  430. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX]++;
  431. }
  432. static void rx_mgmt_action_sa_query_resp(struct wlantest *wt,
  433. struct wlantest_sta *sta,
  434. const struct ieee80211_mgmt *mgmt,
  435. size_t len, int valid)
  436. {
  437. const u8 *rx_id;
  438. u8 *id;
  439. int match;
  440. rx_id = (const u8 *) mgmt->u.action.u.sa_query_resp.trans_id;
  441. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  442. id = sta->sta_sa_query_tr;
  443. else
  444. id = sta->ap_sa_query_tr;
  445. match = os_memcmp(rx_id, id, 2) == 0;
  446. wpa_printf(MSG_INFO, "SA Query Response " MACSTR " -> " MACSTR
  447. " (trans_id=%02x%02x; %s)%s",
  448. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  449. match ? "match" : "mismatch",
  450. valid ? "" : " (invalid protection)");
  451. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  452. sta->counters[(valid && match) ?
  453. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX :
  454. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX]++;
  455. else
  456. sta->counters[(valid && match) ?
  457. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX :
  458. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX]++;
  459. }
  460. static void rx_mgmt_action_sa_query(struct wlantest *wt,
  461. struct wlantest_sta *sta,
  462. const struct ieee80211_mgmt *mgmt,
  463. size_t len, int valid)
  464. {
  465. if (len < 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  466. wpa_printf(MSG_INFO, "Too short SA Query frame from " MACSTR,
  467. MAC2STR(mgmt->sa));
  468. return;
  469. }
  470. if (len > 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  471. size_t elen = len - (24 + 2 + WLAN_SA_QUERY_TR_ID_LEN);
  472. wpa_printf(MSG_INFO, "Unexpected %u octets of extra data at "
  473. "the end of SA Query frame from " MACSTR,
  474. (unsigned) elen, MAC2STR(mgmt->sa));
  475. wpa_hexdump(MSG_INFO, "SA Query extra data",
  476. ((const u8 *) mgmt) + len - elen, elen);
  477. }
  478. switch (mgmt->u.action.u.sa_query_req.action) {
  479. case WLAN_SA_QUERY_REQUEST:
  480. rx_mgmt_action_sa_query_req(wt, sta, mgmt, len, valid);
  481. break;
  482. case WLAN_SA_QUERY_RESPONSE:
  483. rx_mgmt_action_sa_query_resp(wt, sta, mgmt, len, valid);
  484. break;
  485. default:
  486. wpa_printf(MSG_INFO, "Unexpected SA Query action value %u "
  487. "from " MACSTR,
  488. mgmt->u.action.u.sa_query_req.action,
  489. MAC2STR(mgmt->sa));
  490. }
  491. }
  492. static void rx_mgmt_action(struct wlantest *wt, const u8 *data, size_t len,
  493. int valid)
  494. {
  495. const struct ieee80211_mgmt *mgmt;
  496. struct wlantest_bss *bss;
  497. struct wlantest_sta *sta;
  498. mgmt = (const struct ieee80211_mgmt *) data;
  499. if (mgmt->da[0] & 0x01) {
  500. wpa_printf(MSG_DEBUG, "Group addressed Action frame: DA="
  501. MACSTR " SA=" MACSTR " BSSID=" MACSTR
  502. " category=%u",
  503. MAC2STR(mgmt->da), MAC2STR(mgmt->sa),
  504. MAC2STR(mgmt->bssid), mgmt->u.action.category);
  505. return; /* Ignore group addressed Action frames for now */
  506. }
  507. bss = bss_get(wt, mgmt->bssid);
  508. if (bss == NULL)
  509. return;
  510. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  511. sta = sta_get(bss, mgmt->da);
  512. else
  513. sta = sta_get(bss, mgmt->sa);
  514. if (sta == NULL)
  515. return;
  516. if (len < 24 + 1) {
  517. wpa_printf(MSG_INFO, "Too short Action frame from "
  518. MACSTR, MAC2STR(mgmt->sa));
  519. return;
  520. }
  521. wpa_printf(MSG_DEBUG, "ACTION " MACSTR " -> " MACSTR
  522. " (category=%u) (valid=%d)",
  523. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  524. mgmt->u.action.category, valid);
  525. wpa_hexdump(MSG_MSGDUMP, "ACTION payload", data + 24, len - 24);
  526. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  527. sta->state < STATE3) {
  528. wpa_printf(MSG_INFO, "Action frame sent when STA is not in "
  529. "State 3 (SA=" MACSTR " DATA=" MACSTR ")",
  530. MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  531. }
  532. switch (mgmt->u.action.category) {
  533. case WLAN_ACTION_SA_QUERY:
  534. rx_mgmt_action_sa_query(wt, sta, mgmt, len, valid);
  535. break;
  536. }
  537. }
  538. static int check_mmie_mic(const u8 *igtk, const u8 *data, size_t len)
  539. {
  540. u8 *buf;
  541. u8 mic[16];
  542. u16 fc;
  543. const struct ieee80211_hdr *hdr;
  544. buf = os_malloc(len + 20 - 24);
  545. if (buf == NULL)
  546. return -1;
  547. /* BIP AAD: FC(masked) A1 A2 A3 */
  548. hdr = (const struct ieee80211_hdr *) data;
  549. fc = le_to_host16(hdr->frame_control);
  550. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  551. WPA_PUT_LE16(buf, fc);
  552. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  553. /* Frame body with MMIE MIC masked to zero */
  554. os_memcpy(buf + 20, data + 24, len - 24 - 8);
  555. os_memset(buf + 20 + len - 24 - 8, 0, 8);
  556. wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, len + 20 - 24);
  557. /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
  558. if (omac1_aes_128(igtk, buf, len + 20 - 24, mic) < 0) {
  559. os_free(buf);
  560. return -1;
  561. }
  562. os_free(buf);
  563. if (os_memcmp(data + len - 8, mic, 8) != 0)
  564. return -1;
  565. return 0;
  566. }
  567. static int check_bip(struct wlantest *wt, const u8 *data, size_t len)
  568. {
  569. const struct ieee80211_mgmt *mgmt;
  570. u16 fc, stype;
  571. const u8 *mmie;
  572. u16 keyid;
  573. struct wlantest_bss *bss;
  574. mgmt = (const struct ieee80211_mgmt *) data;
  575. fc = le_to_host16(mgmt->frame_control);
  576. stype = WLAN_FC_GET_STYPE(fc);
  577. if (stype == WLAN_FC_STYPE_ACTION) {
  578. if (len < 24 + 1)
  579. return 0;
  580. if (mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  581. return 0; /* Not a robust management frame */
  582. }
  583. bss = bss_get(wt, mgmt->bssid);
  584. if (bss == NULL)
  585. return 0; /* No key known yet */
  586. if (len < 24 + 18 || data[len - 18] != WLAN_EID_MMIE ||
  587. data[len - 17] != 16) {
  588. /* No MMIE */
  589. if (bss->rsn_capab & WPA_CAPABILITY_MFPC) {
  590. wpa_printf(MSG_INFO, "Robust group-addressed "
  591. "management frame sent without BIP by "
  592. MACSTR, MAC2STR(mgmt->sa));
  593. bss->counters[WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE]++;
  594. return -1;
  595. }
  596. return 0;
  597. }
  598. mmie = data + len - 16;
  599. keyid = WPA_GET_LE16(mmie);
  600. if (keyid & 0xf000) {
  601. wpa_printf(MSG_INFO, "MMIE KeyID reserved bits not zero "
  602. "(%04x) from " MACSTR, keyid, MAC2STR(mgmt->sa));
  603. keyid &= 0x0fff;
  604. }
  605. if (keyid < 4 || keyid > 5) {
  606. wpa_printf(MSG_INFO, "Unexpected MMIE KeyID %u from " MACSTR,
  607. keyid, MAC2STR(mgmt->sa));
  608. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  609. return 0;
  610. }
  611. wpa_printf(MSG_DEBUG, "MMIE KeyID %u", keyid);
  612. wpa_hexdump(MSG_MSGDUMP, "MMIE IPN", mmie + 2, 6);
  613. wpa_hexdump(MSG_MSGDUMP, "MMIE MIC", mmie + 8, 8);
  614. if (!bss->igtk_set[keyid]) {
  615. wpa_printf(MSG_DEBUG, "No IGTK known to validate BIP frame");
  616. return 0;
  617. }
  618. if (os_memcmp(mmie + 2, bss->ipn[keyid], 6) <= 0) {
  619. wpa_printf(MSG_INFO, "BIP replay detected: SA=" MACSTR,
  620. MAC2STR(mgmt->sa));
  621. wpa_hexdump(MSG_INFO, "RX IPN", mmie + 2, 6);
  622. wpa_hexdump(MSG_INFO, "Last RX IPN", bss->ipn[keyid], 6);
  623. }
  624. if (check_mmie_mic(bss->igtk[keyid], data, len) < 0) {
  625. wpa_printf(MSG_INFO, "Invalid MMIE MIC in a frame from "
  626. MACSTR, MAC2STR(mgmt->sa));
  627. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  628. return -1;
  629. }
  630. wpa_printf(MSG_DEBUG, "Valid MMIE MIC");
  631. os_memcpy(bss->ipn[keyid], mmie + 2, 6);
  632. bss->counters[WLANTEST_BSS_COUNTER_VALID_BIP_MMIE]++;
  633. return 0;
  634. }
  635. static u8 * mgmt_ccmp_decrypt(struct wlantest *wt, const u8 *data, size_t len,
  636. size_t *dlen)
  637. {
  638. struct wlantest_bss *bss;
  639. struct wlantest_sta *sta;
  640. const struct ieee80211_hdr *hdr;
  641. int keyid;
  642. u8 *decrypted, *frame = NULL;
  643. u8 pn[6], *rsc;
  644. hdr = (const struct ieee80211_hdr *) data;
  645. bss = bss_get(wt, hdr->addr3);
  646. if (bss == NULL)
  647. return NULL;
  648. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  649. sta = sta_get(bss, hdr->addr2);
  650. else
  651. sta = sta_get(bss, hdr->addr1);
  652. if (sta == NULL || !sta->ptk_set) {
  653. wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
  654. return NULL;
  655. }
  656. if (len < 24 + 4)
  657. return NULL;
  658. if (!(data[24 + 3] & 0x20)) {
  659. wpa_printf(MSG_INFO, "Expected CCMP frame from " MACSTR
  660. " did not have ExtIV bit set to 1",
  661. MAC2STR(hdr->addr2));
  662. return NULL;
  663. }
  664. if (data[24 + 2] != 0 || (data[24 + 3] & 0x1f) != 0) {
  665. wpa_printf(MSG_INFO, "CCMP mgmt frame from " MACSTR " used "
  666. "non-zero reserved bit", MAC2STR(hdr->addr2));
  667. }
  668. keyid = data[24 + 3] >> 6;
  669. if (keyid != 0) {
  670. wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
  671. "individually addressed Management frame from "
  672. MACSTR, keyid, MAC2STR(hdr->addr2));
  673. }
  674. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  675. rsc = sta->rsc_tods[16];
  676. else
  677. rsc = sta->rsc_fromds[16];
  678. ccmp_get_pn(pn, data + 24);
  679. if (os_memcmp(pn, rsc, 6) <= 0) {
  680. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  681. MAC2STR(hdr->addr2));
  682. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  683. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  684. }
  685. decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data + 24, len - 24, dlen);
  686. if (decrypted) {
  687. os_memcpy(rsc, pn, 6);
  688. frame = os_malloc(24 + *dlen);
  689. if (frame) {
  690. os_memcpy(frame, data, 24);
  691. os_memcpy(frame + 24, decrypted, *dlen);
  692. *dlen += 24;
  693. }
  694. }
  695. os_free(decrypted);
  696. return frame;
  697. }
  698. static int check_mgmt_ccmp(struct wlantest *wt, const u8 *data, size_t len)
  699. {
  700. const struct ieee80211_mgmt *mgmt;
  701. u16 fc;
  702. struct wlantest_bss *bss;
  703. struct wlantest_sta *sta;
  704. mgmt = (const struct ieee80211_mgmt *) data;
  705. fc = le_to_host16(mgmt->frame_control);
  706. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
  707. if (len > 24 &&
  708. mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  709. return 0; /* Not a robust management frame */
  710. }
  711. bss = bss_get(wt, mgmt->bssid);
  712. if (bss == NULL)
  713. return 0;
  714. if (os_memcmp(mgmt->da, mgmt->bssid, ETH_ALEN) == 0)
  715. sta = sta_get(bss, mgmt->sa);
  716. else
  717. sta = sta_get(bss, mgmt->da);
  718. if (sta == NULL)
  719. return 0;
  720. if (sta->rsn_capab & WPA_CAPABILITY_MFPC) {
  721. wpa_printf(MSG_INFO, "Robust individually-addressed "
  722. "management frame sent without CCMP by "
  723. MACSTR, MAC2STR(mgmt->sa));
  724. return -1;
  725. }
  726. return 0;
  727. }
  728. void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
  729. {
  730. const struct ieee80211_hdr *hdr;
  731. u16 fc, stype;
  732. int valid = 1;
  733. u8 *decrypted = NULL;
  734. size_t dlen;
  735. if (len < 24)
  736. return;
  737. hdr = (const struct ieee80211_hdr *) data;
  738. fc = le_to_host16(hdr->frame_control);
  739. wt->rx_mgmt++;
  740. stype = WLAN_FC_GET_STYPE(fc);
  741. if ((hdr->addr1[0] & 0x01) &&
  742. (stype == WLAN_FC_STYPE_DEAUTH ||
  743. stype == WLAN_FC_STYPE_DISASSOC ||
  744. stype == WLAN_FC_STYPE_ACTION)) {
  745. if (check_bip(wt, data, len) < 0)
  746. valid = 0;
  747. }
  748. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  749. stype == WLAN_FC_STYPE_PROBE_RESP ||
  750. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  751. MSG_EXCESSIVE : MSG_MSGDUMP,
  752. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  753. mgmt_stype(stype),
  754. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  755. fc & WLAN_FC_ISWEP ? " Prot" : "",
  756. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  757. MAC2STR(hdr->addr3));
  758. if ((fc & WLAN_FC_ISWEP) &&
  759. !(hdr->addr1[0] & 0x01) &&
  760. (stype == WLAN_FC_STYPE_DEAUTH ||
  761. stype == WLAN_FC_STYPE_DISASSOC ||
  762. stype == WLAN_FC_STYPE_ACTION)) {
  763. decrypted = mgmt_ccmp_decrypt(wt, data, len, &dlen);
  764. if (decrypted) {
  765. write_pcap_decrypted(wt, decrypted, dlen, NULL, 0);
  766. data = decrypted;
  767. len = dlen;
  768. } else
  769. valid = 0;
  770. }
  771. if (!(fc & WLAN_FC_ISWEP) &&
  772. !(hdr->addr1[0] & 0x01) &&
  773. (stype == WLAN_FC_STYPE_DEAUTH ||
  774. stype == WLAN_FC_STYPE_DISASSOC ||
  775. stype == WLAN_FC_STYPE_ACTION)) {
  776. if (check_mgmt_ccmp(wt, data, len) < 0)
  777. valid = 0;
  778. }
  779. switch (stype) {
  780. case WLAN_FC_STYPE_BEACON:
  781. rx_mgmt_beacon(wt, data, len);
  782. break;
  783. case WLAN_FC_STYPE_PROBE_RESP:
  784. rx_mgmt_probe_resp(wt, data, len);
  785. break;
  786. case WLAN_FC_STYPE_AUTH:
  787. rx_mgmt_auth(wt, data, len);
  788. break;
  789. case WLAN_FC_STYPE_DEAUTH:
  790. rx_mgmt_deauth(wt, data, len, valid);
  791. break;
  792. case WLAN_FC_STYPE_ASSOC_REQ:
  793. rx_mgmt_assoc_req(wt, data, len);
  794. break;
  795. case WLAN_FC_STYPE_ASSOC_RESP:
  796. rx_mgmt_assoc_resp(wt, data, len);
  797. break;
  798. case WLAN_FC_STYPE_REASSOC_REQ:
  799. rx_mgmt_reassoc_req(wt, data, len);
  800. break;
  801. case WLAN_FC_STYPE_REASSOC_RESP:
  802. rx_mgmt_reassoc_resp(wt, data, len);
  803. break;
  804. case WLAN_FC_STYPE_DISASSOC:
  805. rx_mgmt_disassoc(wt, data, len, valid);
  806. break;
  807. case WLAN_FC_STYPE_ACTION:
  808. rx_mgmt_action(wt, data, len, valid);
  809. break;
  810. }
  811. os_free(decrypted);
  812. }