rx_mgmt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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_update_assoc(sta, &elems);
  213. }
  214. static void rx_mgmt_assoc_resp(struct wlantest *wt, const u8 *data, size_t len)
  215. {
  216. const struct ieee80211_mgmt *mgmt;
  217. struct wlantest_bss *bss;
  218. struct wlantest_sta *sta;
  219. u16 capab, status, aid;
  220. mgmt = (const struct ieee80211_mgmt *) data;
  221. bss = bss_get(wt, mgmt->bssid);
  222. if (bss == NULL)
  223. return;
  224. sta = sta_get(bss, mgmt->da);
  225. if (sta == NULL)
  226. return;
  227. if (len < 24 + 6) {
  228. wpa_printf(MSG_INFO, "Too short Association Response frame "
  229. "from " MACSTR, MAC2STR(mgmt->sa));
  230. return;
  231. }
  232. capab = le_to_host16(mgmt->u.assoc_resp.capab_info);
  233. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  234. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  235. wpa_printf(MSG_DEBUG, "ASSOCRESP " MACSTR " -> " MACSTR
  236. " (capab=0x%x status=%u aid=%u)",
  237. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  238. aid & 0x3fff);
  239. if (status)
  240. return;
  241. if ((aid & 0xc000) != 0xc000) {
  242. wpa_printf(MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  243. "in Association Response from " MACSTR,
  244. MAC2STR(mgmt->sa));
  245. }
  246. sta->aid = aid & 0xc000;
  247. if (sta->state < STATE2) {
  248. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 when "
  249. "getting associated", MAC2STR(sta->addr));
  250. }
  251. if (sta->state < STATE3) {
  252. wpa_printf(MSG_DEBUG, "STA " MACSTR
  253. " moved to State 3 with " MACSTR,
  254. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  255. sta->state = STATE3;
  256. }
  257. }
  258. static void rx_mgmt_reassoc_req(struct wlantest *wt, const u8 *data,
  259. size_t len)
  260. {
  261. const struct ieee80211_mgmt *mgmt;
  262. struct wlantest_bss *bss;
  263. struct wlantest_sta *sta;
  264. struct ieee802_11_elems elems;
  265. mgmt = (const struct ieee80211_mgmt *) data;
  266. bss = bss_get(wt, mgmt->bssid);
  267. if (bss == NULL)
  268. return;
  269. sta = sta_get(bss, mgmt->sa);
  270. if (sta == NULL)
  271. return;
  272. if (len < 24 + 4 + ETH_ALEN) {
  273. wpa_printf(MSG_INFO, "Too short Reassociation Request frame "
  274. "from " MACSTR, MAC2STR(mgmt->sa));
  275. return;
  276. }
  277. wpa_printf(MSG_DEBUG, "REASSOCREQ " MACSTR " -> " MACSTR
  278. " (capab=0x%x listen_int=%u current_ap=" MACSTR ")",
  279. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  280. le_to_host16(mgmt->u.reassoc_req.capab_info),
  281. le_to_host16(mgmt->u.reassoc_req.listen_interval),
  282. MAC2STR(mgmt->u.reassoc_req.current_ap));
  283. sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX]++;
  284. if (ieee802_11_parse_elems(mgmt->u.reassoc_req.variable,
  285. len - (mgmt->u.reassoc_req.variable - data),
  286. &elems, 0) == ParseFailed) {
  287. wpa_printf(MSG_INFO, "Invalid IEs in Reassociation Request "
  288. "frame from " MACSTR, MAC2STR(mgmt->sa));
  289. return;
  290. }
  291. sta_update_assoc(sta, &elems);
  292. }
  293. static void rx_mgmt_reassoc_resp(struct wlantest *wt, const u8 *data,
  294. size_t len)
  295. {
  296. const struct ieee80211_mgmt *mgmt;
  297. struct wlantest_bss *bss;
  298. struct wlantest_sta *sta;
  299. u16 capab, status, aid;
  300. mgmt = (const struct ieee80211_mgmt *) data;
  301. bss = bss_get(wt, mgmt->bssid);
  302. if (bss == NULL)
  303. return;
  304. sta = sta_get(bss, mgmt->da);
  305. if (sta == NULL)
  306. return;
  307. if (len < 24 + 6) {
  308. wpa_printf(MSG_INFO, "Too short Reassociation Response frame "
  309. "from " MACSTR, MAC2STR(mgmt->sa));
  310. return;
  311. }
  312. capab = le_to_host16(mgmt->u.reassoc_resp.capab_info);
  313. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  314. aid = le_to_host16(mgmt->u.reassoc_resp.aid);
  315. wpa_printf(MSG_DEBUG, "REASSOCRESP " MACSTR " -> " MACSTR
  316. " (capab=0x%x status=%u aid=%u)",
  317. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  318. aid & 0x3fff);
  319. if (status)
  320. return;
  321. if ((aid & 0xc000) != 0xc000) {
  322. wpa_printf(MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  323. "in Reassociation Response from " MACSTR,
  324. MAC2STR(mgmt->sa));
  325. }
  326. sta->aid = aid & 0xc000;
  327. if (sta->state < STATE2) {
  328. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 when "
  329. "getting associated", MAC2STR(sta->addr));
  330. }
  331. if (sta->state < STATE3) {
  332. wpa_printf(MSG_DEBUG, "STA " MACSTR
  333. " moved to State 3 with " MACSTR,
  334. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  335. sta->state = STATE3;
  336. }
  337. }
  338. static void rx_mgmt_disassoc(struct wlantest *wt, const u8 *data, size_t len,
  339. int valid)
  340. {
  341. const struct ieee80211_mgmt *mgmt;
  342. struct wlantest_bss *bss;
  343. struct wlantest_sta *sta;
  344. mgmt = (const struct ieee80211_mgmt *) data;
  345. bss = bss_get(wt, mgmt->bssid);
  346. if (bss == NULL)
  347. return;
  348. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  349. sta = sta_get(bss, mgmt->da);
  350. else
  351. sta = sta_get(bss, mgmt->sa);
  352. if (sta == NULL)
  353. return;
  354. if (len < 24 + 2) {
  355. wpa_printf(MSG_INFO, "Too short Disassociation frame from "
  356. MACSTR, MAC2STR(mgmt->sa));
  357. return;
  358. }
  359. wpa_printf(MSG_DEBUG, "DISASSOC " MACSTR " -> " MACSTR
  360. " (reason=%u)",
  361. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  362. le_to_host16(mgmt->u.disassoc.reason_code));
  363. wpa_hexdump(MSG_MSGDUMP, "DISASSOC payload", data + 24, len - 24);
  364. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  365. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_RX :
  366. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX]++;
  367. else
  368. sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_TX :
  369. WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX]++;
  370. if (!valid) {
  371. wpa_printf(MSG_INFO, "Do not change STA " MACSTR " State "
  372. "since Disassociation frame was not protected "
  373. "correctly", MAC2STR(sta->addr));
  374. return;
  375. }
  376. if (sta->state < STATE2) {
  377. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 or 3 "
  378. "when getting disassociated", MAC2STR(sta->addr));
  379. }
  380. if (sta->state > STATE2) {
  381. wpa_printf(MSG_DEBUG, "STA " MACSTR
  382. " moved to State 2 with " MACSTR,
  383. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  384. sta->state = STATE2;
  385. }
  386. }
  387. static void rx_mgmt_action_sa_query_req(struct wlantest *wt,
  388. struct wlantest_sta *sta,
  389. const struct ieee80211_mgmt *mgmt,
  390. size_t len, int valid)
  391. {
  392. const u8 *rx_id;
  393. u8 *id;
  394. rx_id = (const u8 *) mgmt->u.action.u.sa_query_req.trans_id;
  395. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  396. id = sta->ap_sa_query_tr;
  397. else
  398. id = sta->sta_sa_query_tr;
  399. wpa_printf(MSG_INFO, "SA Query Request " MACSTR " -> " MACSTR
  400. " (trans_id=%02x%02x)%s",
  401. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  402. valid ? "" : " (invalid protection)");
  403. os_memcpy(id, mgmt->u.action.u.sa_query_req.trans_id, 2);
  404. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  405. sta->counters[valid ?
  406. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX :
  407. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX]++;
  408. else
  409. sta->counters[valid ?
  410. WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX :
  411. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX]++;
  412. }
  413. static void rx_mgmt_action_sa_query_resp(struct wlantest *wt,
  414. struct wlantest_sta *sta,
  415. const struct ieee80211_mgmt *mgmt,
  416. size_t len, int valid)
  417. {
  418. const u8 *rx_id;
  419. u8 *id;
  420. int match;
  421. rx_id = (const u8 *) mgmt->u.action.u.sa_query_resp.trans_id;
  422. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  423. id = sta->sta_sa_query_tr;
  424. else
  425. id = sta->ap_sa_query_tr;
  426. match = os_memcmp(rx_id, id, 2) == 0;
  427. wpa_printf(MSG_INFO, "SA Query Response " MACSTR " -> " MACSTR
  428. " (trans_id=%02x%02x; %s)%s",
  429. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
  430. match ? "match" : "mismatch",
  431. valid ? "" : " (invalid protection)");
  432. if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
  433. sta->counters[(valid && match) ?
  434. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX :
  435. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX]++;
  436. else
  437. sta->counters[(valid && match) ?
  438. WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX :
  439. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX]++;
  440. }
  441. static void rx_mgmt_action_sa_query(struct wlantest *wt,
  442. struct wlantest_sta *sta,
  443. const struct ieee80211_mgmt *mgmt,
  444. size_t len, int valid)
  445. {
  446. if (len < 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  447. wpa_printf(MSG_INFO, "Too short SA Query frame from " MACSTR,
  448. MAC2STR(mgmt->sa));
  449. return;
  450. }
  451. if (len > 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  452. size_t elen = len - (24 + 2 + WLAN_SA_QUERY_TR_ID_LEN);
  453. wpa_printf(MSG_INFO, "Unexpected %u octets of extra data at "
  454. "the end of SA Query frame from " MACSTR,
  455. (unsigned) elen, MAC2STR(mgmt->sa));
  456. wpa_hexdump(MSG_INFO, "SA Query extra data",
  457. ((const u8 *) mgmt) + len - elen, elen);
  458. }
  459. switch (mgmt->u.action.u.sa_query_req.action) {
  460. case WLAN_SA_QUERY_REQUEST:
  461. rx_mgmt_action_sa_query_req(wt, sta, mgmt, len, valid);
  462. break;
  463. case WLAN_SA_QUERY_RESPONSE:
  464. rx_mgmt_action_sa_query_resp(wt, sta, mgmt, len, valid);
  465. break;
  466. default:
  467. wpa_printf(MSG_INFO, "Unexpected SA Query action value %u "
  468. "from " MACSTR,
  469. mgmt->u.action.u.sa_query_req.action,
  470. MAC2STR(mgmt->sa));
  471. }
  472. }
  473. static void rx_mgmt_action(struct wlantest *wt, const u8 *data, size_t len,
  474. int valid)
  475. {
  476. const struct ieee80211_mgmt *mgmt;
  477. struct wlantest_bss *bss;
  478. struct wlantest_sta *sta;
  479. mgmt = (const struct ieee80211_mgmt *) data;
  480. if (mgmt->da[0] & 0x01) {
  481. wpa_printf(MSG_DEBUG, "Group addressed Action frame: DA="
  482. MACSTR " SA=" MACSTR " BSSID=" MACSTR
  483. " category=%u",
  484. MAC2STR(mgmt->da), MAC2STR(mgmt->sa),
  485. MAC2STR(mgmt->bssid), mgmt->u.action.category);
  486. return; /* Ignore group addressed Action frames for now */
  487. }
  488. bss = bss_get(wt, mgmt->bssid);
  489. if (bss == NULL)
  490. return;
  491. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  492. sta = sta_get(bss, mgmt->da);
  493. else
  494. sta = sta_get(bss, mgmt->sa);
  495. if (sta == NULL)
  496. return;
  497. if (len < 24 + 1) {
  498. wpa_printf(MSG_INFO, "Too short Action frame from "
  499. MACSTR, MAC2STR(mgmt->sa));
  500. return;
  501. }
  502. wpa_printf(MSG_DEBUG, "ACTION " MACSTR " -> " MACSTR
  503. " (category=%u) (valid=%d)",
  504. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  505. mgmt->u.action.category, valid);
  506. wpa_hexdump(MSG_MSGDUMP, "ACTION payload", data + 24, len - 24);
  507. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  508. sta->state < STATE3) {
  509. wpa_printf(MSG_INFO, "Action frame sent when STA is not in "
  510. "State 3 (SA=" MACSTR " DATA=" MACSTR ")",
  511. MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  512. }
  513. switch (mgmt->u.action.category) {
  514. case WLAN_ACTION_SA_QUERY:
  515. rx_mgmt_action_sa_query(wt, sta, mgmt, len, valid);
  516. break;
  517. }
  518. }
  519. static int check_mmie_mic(const u8 *igtk, const u8 *data, size_t len)
  520. {
  521. u8 *buf;
  522. u8 mic[16];
  523. u16 fc;
  524. const struct ieee80211_hdr *hdr;
  525. buf = os_malloc(len + 20 - 24);
  526. if (buf == NULL)
  527. return -1;
  528. /* BIP AAD: FC(masked) A1 A2 A3 */
  529. hdr = (const struct ieee80211_hdr *) data;
  530. fc = le_to_host16(hdr->frame_control);
  531. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  532. WPA_PUT_LE16(buf, fc);
  533. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  534. /* Frame body with MMIE MIC masked to zero */
  535. os_memcpy(buf + 20, data + 24, len - 24 - 8);
  536. os_memset(buf + 20 + len - 24 - 8, 0, 8);
  537. wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, len + 20 - 24);
  538. /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
  539. if (omac1_aes_128(igtk, buf, len + 20 - 24, mic) < 0) {
  540. os_free(buf);
  541. return -1;
  542. }
  543. os_free(buf);
  544. if (os_memcmp(data + len - 8, mic, 8) != 0)
  545. return -1;
  546. return 0;
  547. }
  548. static int check_bip(struct wlantest *wt, const u8 *data, size_t len)
  549. {
  550. const struct ieee80211_mgmt *mgmt;
  551. u16 fc, stype;
  552. const u8 *mmie;
  553. u16 keyid;
  554. struct wlantest_bss *bss;
  555. mgmt = (const struct ieee80211_mgmt *) data;
  556. fc = le_to_host16(mgmt->frame_control);
  557. stype = WLAN_FC_GET_STYPE(fc);
  558. if (stype == WLAN_FC_STYPE_ACTION) {
  559. if (len < 24 + 1)
  560. return 0;
  561. if (mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  562. return 0; /* Not a robust management frame */
  563. }
  564. bss = bss_get(wt, mgmt->bssid);
  565. if (bss == NULL)
  566. return 0; /* No key known yet */
  567. if (len < 24 + 18 || data[len - 18] != WLAN_EID_MMIE ||
  568. data[len - 17] != 16) {
  569. /* No MMIE */
  570. if (bss->rsn_capab & WPA_CAPABILITY_MFPC) {
  571. wpa_printf(MSG_INFO, "Robust group-addressed "
  572. "management frame sent without BIP by "
  573. MACSTR, MAC2STR(mgmt->sa));
  574. bss->counters[WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE]++;
  575. return -1;
  576. }
  577. return 0;
  578. }
  579. mmie = data + len - 16;
  580. keyid = WPA_GET_LE16(mmie);
  581. if (keyid & 0xf000) {
  582. wpa_printf(MSG_INFO, "MMIE KeyID reserved bits not zero "
  583. "(%04x) from " MACSTR, keyid, MAC2STR(mgmt->sa));
  584. keyid &= 0x0fff;
  585. }
  586. if (keyid < 4 || keyid > 5) {
  587. wpa_printf(MSG_INFO, "Unexpected MMIE KeyID %u from " MACSTR,
  588. keyid, MAC2STR(mgmt->sa));
  589. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  590. return 0;
  591. }
  592. wpa_printf(MSG_DEBUG, "MMIE KeyID %u", keyid);
  593. wpa_hexdump(MSG_MSGDUMP, "MMIE IPN", mmie + 2, 6);
  594. wpa_hexdump(MSG_MSGDUMP, "MMIE MIC", mmie + 8, 8);
  595. if (!bss->igtk_set[keyid]) {
  596. wpa_printf(MSG_DEBUG, "No IGTK known to validate BIP frame");
  597. return 0;
  598. }
  599. if (os_memcmp(mmie + 2, bss->ipn[keyid], 6) <= 0) {
  600. wpa_printf(MSG_INFO, "BIP replay detected: SA=" MACSTR,
  601. MAC2STR(mgmt->sa));
  602. wpa_hexdump(MSG_INFO, "RX IPN", mmie + 2, 6);
  603. wpa_hexdump(MSG_INFO, "Last RX IPN", bss->ipn[keyid], 6);
  604. }
  605. if (check_mmie_mic(bss->igtk[keyid], data, len) < 0) {
  606. wpa_printf(MSG_INFO, "Invalid MMIE MIC in a frame from "
  607. MACSTR, MAC2STR(mgmt->sa));
  608. bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
  609. return -1;
  610. }
  611. wpa_printf(MSG_DEBUG, "Valid MMIE MIC");
  612. os_memcpy(bss->ipn[keyid], mmie + 2, 6);
  613. bss->counters[WLANTEST_BSS_COUNTER_VALID_BIP_MMIE]++;
  614. return 0;
  615. }
  616. static u8 * mgmt_ccmp_decrypt(struct wlantest *wt, const u8 *data, size_t len,
  617. size_t *dlen)
  618. {
  619. struct wlantest_bss *bss;
  620. struct wlantest_sta *sta;
  621. const struct ieee80211_hdr *hdr;
  622. int keyid;
  623. u8 *decrypted, *frame = NULL;
  624. u8 pn[6], *rsc;
  625. hdr = (const struct ieee80211_hdr *) data;
  626. bss = bss_get(wt, hdr->addr3);
  627. if (bss == NULL)
  628. return NULL;
  629. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  630. sta = sta_get(bss, hdr->addr2);
  631. else
  632. sta = sta_get(bss, hdr->addr1);
  633. if (sta == NULL || !sta->ptk_set) {
  634. wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
  635. return NULL;
  636. }
  637. if (len < 24 + 4)
  638. return NULL;
  639. if (!(data[24 + 3] & 0x20)) {
  640. wpa_printf(MSG_INFO, "Expected CCMP frame from " MACSTR
  641. " did not have ExtIV bit set to 1",
  642. MAC2STR(hdr->addr2));
  643. return NULL;
  644. }
  645. if (data[24 + 2] != 0 || (data[24 + 3] & 0x1f) != 0) {
  646. wpa_printf(MSG_INFO, "CCMP mgmt frame from " MACSTR " used "
  647. "non-zero reserved bit", MAC2STR(hdr->addr2));
  648. }
  649. keyid = data[24 + 3] >> 6;
  650. if (keyid != 0) {
  651. wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
  652. "individually addressed Management frame from "
  653. MACSTR, keyid, MAC2STR(hdr->addr2));
  654. }
  655. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  656. rsc = sta->rsc_tods[16];
  657. else
  658. rsc = sta->rsc_fromds[16];
  659. ccmp_get_pn(pn, data + 24);
  660. if (os_memcmp(pn, rsc, 6) <= 0) {
  661. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  662. MAC2STR(hdr->addr2));
  663. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  664. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  665. }
  666. decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data + 24, len - 24, dlen);
  667. if (decrypted) {
  668. os_memcpy(rsc, pn, 6);
  669. frame = os_malloc(24 + *dlen);
  670. if (frame) {
  671. os_memcpy(frame, data, 24);
  672. os_memcpy(frame + 24, decrypted, *dlen);
  673. *dlen += 24;
  674. }
  675. }
  676. os_free(decrypted);
  677. return frame;
  678. }
  679. static int check_mgmt_ccmp(struct wlantest *wt, const u8 *data, size_t len)
  680. {
  681. const struct ieee80211_mgmt *mgmt;
  682. u16 fc;
  683. struct wlantest_bss *bss;
  684. struct wlantest_sta *sta;
  685. mgmt = (const struct ieee80211_mgmt *) data;
  686. fc = le_to_host16(mgmt->frame_control);
  687. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
  688. if (len > 24 &&
  689. mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  690. return 0; /* Not a robust management frame */
  691. }
  692. bss = bss_get(wt, mgmt->bssid);
  693. if (bss == NULL)
  694. return 0;
  695. if (os_memcmp(mgmt->da, mgmt->bssid, ETH_ALEN) == 0)
  696. sta = sta_get(bss, mgmt->sa);
  697. else
  698. sta = sta_get(bss, mgmt->da);
  699. if (sta == NULL)
  700. return 0;
  701. if (sta->rsn_capab & WPA_CAPABILITY_MFPC) {
  702. wpa_printf(MSG_INFO, "Robust individually-addressed "
  703. "management frame sent without CCMP by "
  704. MACSTR, MAC2STR(mgmt->sa));
  705. return -1;
  706. }
  707. return 0;
  708. }
  709. void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
  710. {
  711. const struct ieee80211_hdr *hdr;
  712. u16 fc, stype;
  713. int valid = 1;
  714. u8 *decrypted = NULL;
  715. size_t dlen;
  716. if (len < 24)
  717. return;
  718. hdr = (const struct ieee80211_hdr *) data;
  719. fc = le_to_host16(hdr->frame_control);
  720. wt->rx_mgmt++;
  721. stype = WLAN_FC_GET_STYPE(fc);
  722. if ((hdr->addr1[0] & 0x01) &&
  723. (stype == WLAN_FC_STYPE_DEAUTH ||
  724. stype == WLAN_FC_STYPE_DISASSOC ||
  725. stype == WLAN_FC_STYPE_ACTION)) {
  726. if (check_bip(wt, data, len) < 0)
  727. valid = 0;
  728. }
  729. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  730. stype == WLAN_FC_STYPE_PROBE_RESP ||
  731. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  732. MSG_EXCESSIVE : MSG_MSGDUMP,
  733. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  734. mgmt_stype(stype),
  735. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  736. fc & WLAN_FC_ISWEP ? " Prot" : "",
  737. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  738. MAC2STR(hdr->addr3));
  739. if ((fc & WLAN_FC_ISWEP) &&
  740. !(hdr->addr1[0] & 0x01) &&
  741. (stype == WLAN_FC_STYPE_DEAUTH ||
  742. stype == WLAN_FC_STYPE_DISASSOC ||
  743. stype == WLAN_FC_STYPE_ACTION)) {
  744. decrypted = mgmt_ccmp_decrypt(wt, data, len, &dlen);
  745. if (decrypted) {
  746. write_pcap_decrypted(wt, decrypted, dlen, NULL, 0);
  747. data = decrypted;
  748. len = dlen;
  749. } else
  750. valid = 0;
  751. }
  752. if (!(fc & WLAN_FC_ISWEP) &&
  753. !(hdr->addr1[0] & 0x01) &&
  754. (stype == WLAN_FC_STYPE_DEAUTH ||
  755. stype == WLAN_FC_STYPE_DISASSOC ||
  756. stype == WLAN_FC_STYPE_ACTION)) {
  757. if (check_mgmt_ccmp(wt, data, len) < 0)
  758. valid = 0;
  759. }
  760. switch (stype) {
  761. case WLAN_FC_STYPE_BEACON:
  762. rx_mgmt_beacon(wt, data, len);
  763. break;
  764. case WLAN_FC_STYPE_PROBE_RESP:
  765. rx_mgmt_probe_resp(wt, data, len);
  766. break;
  767. case WLAN_FC_STYPE_AUTH:
  768. rx_mgmt_auth(wt, data, len);
  769. break;
  770. case WLAN_FC_STYPE_DEAUTH:
  771. rx_mgmt_deauth(wt, data, len, valid);
  772. break;
  773. case WLAN_FC_STYPE_ASSOC_REQ:
  774. rx_mgmt_assoc_req(wt, data, len);
  775. break;
  776. case WLAN_FC_STYPE_ASSOC_RESP:
  777. rx_mgmt_assoc_resp(wt, data, len);
  778. break;
  779. case WLAN_FC_STYPE_REASSOC_REQ:
  780. rx_mgmt_reassoc_req(wt, data, len);
  781. break;
  782. case WLAN_FC_STYPE_REASSOC_RESP:
  783. rx_mgmt_reassoc_resp(wt, data, len);
  784. break;
  785. case WLAN_FC_STYPE_DISASSOC:
  786. rx_mgmt_disassoc(wt, data, len, valid);
  787. break;
  788. case WLAN_FC_STYPE_ACTION:
  789. rx_mgmt_action(wt, data, len, valid);
  790. break;
  791. }
  792. os_free(decrypted);
  793. }