rx_mgmt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. }
  132. static void rx_mgmt_deauth(struct wlantest *wt, const u8 *data, size_t len,
  133. int valid)
  134. {
  135. const struct ieee80211_mgmt *mgmt;
  136. struct wlantest_bss *bss;
  137. struct wlantest_sta *sta;
  138. mgmt = (const struct ieee80211_mgmt *) data;
  139. bss = bss_get(wt, mgmt->bssid);
  140. if (bss == NULL)
  141. return;
  142. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  143. sta = sta_get(bss, mgmt->da);
  144. else
  145. sta = sta_get(bss, mgmt->sa);
  146. if (sta == NULL)
  147. return;
  148. if (len < 24 + 2) {
  149. wpa_printf(MSG_INFO, "Too short Deauthentication frame from "
  150. MACSTR, MAC2STR(mgmt->sa));
  151. return;
  152. }
  153. wpa_printf(MSG_DEBUG, "DEAUTH " MACSTR " -> " MACSTR
  154. " (reason=%u)",
  155. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  156. le_to_host16(mgmt->u.deauth.reason_code));
  157. wpa_hexdump(MSG_MSGDUMP, "DEAUTH payload", data + 24, len - 24);
  158. if (!valid) {
  159. wpa_printf(MSG_INFO, "Do not change STA " MACSTR " State "
  160. "since Disassociation frame was not protected "
  161. "correctly", MAC2STR(sta->addr));
  162. return;
  163. }
  164. if (sta->state != STATE1) {
  165. wpa_printf(MSG_DEBUG, "STA " MACSTR
  166. " moved to State 1 with " MACSTR,
  167. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  168. sta->state = STATE1;
  169. }
  170. }
  171. static void rx_mgmt_assoc_req(struct wlantest *wt, const u8 *data, size_t len)
  172. {
  173. const struct ieee80211_mgmt *mgmt;
  174. struct wlantest_bss *bss;
  175. struct wlantest_sta *sta;
  176. struct ieee802_11_elems elems;
  177. mgmt = (const struct ieee80211_mgmt *) data;
  178. bss = bss_get(wt, mgmt->bssid);
  179. if (bss == NULL)
  180. return;
  181. sta = sta_get(bss, mgmt->sa);
  182. if (sta == NULL)
  183. return;
  184. if (len < 24 + 4) {
  185. wpa_printf(MSG_INFO, "Too short Association Request frame "
  186. "from " MACSTR, MAC2STR(mgmt->sa));
  187. return;
  188. }
  189. wpa_printf(MSG_DEBUG, "ASSOCREQ " MACSTR " -> " MACSTR
  190. " (capab=0x%x listen_int=%u)",
  191. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  192. le_to_host16(mgmt->u.assoc_req.capab_info),
  193. le_to_host16(mgmt->u.assoc_req.listen_interval));
  194. if (ieee802_11_parse_elems(mgmt->u.assoc_req.variable,
  195. len - (mgmt->u.assoc_req.variable - data),
  196. &elems, 0) == ParseFailed) {
  197. wpa_printf(MSG_INFO, "Invalid IEs in Association Request "
  198. "frame from " MACSTR, MAC2STR(mgmt->sa));
  199. return;
  200. }
  201. sta_update_assoc(sta, &elems);
  202. }
  203. static void rx_mgmt_assoc_resp(struct wlantest *wt, const u8 *data, size_t len)
  204. {
  205. const struct ieee80211_mgmt *mgmt;
  206. struct wlantest_bss *bss;
  207. struct wlantest_sta *sta;
  208. u16 capab, status, aid;
  209. mgmt = (const struct ieee80211_mgmt *) data;
  210. bss = bss_get(wt, mgmt->bssid);
  211. if (bss == NULL)
  212. return;
  213. sta = sta_get(bss, mgmt->da);
  214. if (sta == NULL)
  215. return;
  216. if (len < 24 + 6) {
  217. wpa_printf(MSG_INFO, "Too short Association Response frame "
  218. "from " MACSTR, MAC2STR(mgmt->sa));
  219. return;
  220. }
  221. capab = le_to_host16(mgmt->u.assoc_resp.capab_info);
  222. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  223. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  224. wpa_printf(MSG_DEBUG, "ASSOCRESP " MACSTR " -> " MACSTR
  225. " (capab=0x%x status=%u aid=%u)",
  226. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  227. aid & 0x3fff);
  228. if (status)
  229. return;
  230. if ((aid & 0xc000) != 0xc000) {
  231. wpa_printf(MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  232. "in Association Response from " MACSTR,
  233. MAC2STR(mgmt->sa));
  234. }
  235. sta->aid = aid & 0xc000;
  236. if (sta->state < STATE2) {
  237. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 when "
  238. "getting associated", MAC2STR(sta->addr));
  239. }
  240. if (sta->state < STATE3) {
  241. wpa_printf(MSG_DEBUG, "STA " MACSTR
  242. " moved to State 3 with " MACSTR,
  243. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  244. sta->state = STATE3;
  245. }
  246. }
  247. static void rx_mgmt_reassoc_req(struct wlantest *wt, const u8 *data,
  248. size_t len)
  249. {
  250. const struct ieee80211_mgmt *mgmt;
  251. struct wlantest_bss *bss;
  252. struct wlantest_sta *sta;
  253. struct ieee802_11_elems elems;
  254. mgmt = (const struct ieee80211_mgmt *) data;
  255. bss = bss_get(wt, mgmt->bssid);
  256. if (bss == NULL)
  257. return;
  258. sta = sta_get(bss, mgmt->sa);
  259. if (sta == NULL)
  260. return;
  261. if (len < 24 + 4 + ETH_ALEN) {
  262. wpa_printf(MSG_INFO, "Too short Reassociation Request frame "
  263. "from " MACSTR, MAC2STR(mgmt->sa));
  264. return;
  265. }
  266. wpa_printf(MSG_DEBUG, "REASSOCREQ " MACSTR " -> " MACSTR
  267. " (capab=0x%x listen_int=%u current_ap=" MACSTR ")",
  268. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  269. le_to_host16(mgmt->u.reassoc_req.capab_info),
  270. le_to_host16(mgmt->u.reassoc_req.listen_interval),
  271. MAC2STR(mgmt->u.reassoc_req.current_ap));
  272. if (ieee802_11_parse_elems(mgmt->u.reassoc_req.variable,
  273. len - (mgmt->u.reassoc_req.variable - data),
  274. &elems, 0) == ParseFailed) {
  275. wpa_printf(MSG_INFO, "Invalid IEs in Reassociation Request "
  276. "frame from " MACSTR, MAC2STR(mgmt->sa));
  277. return;
  278. }
  279. sta_update_assoc(sta, &elems);
  280. }
  281. static void rx_mgmt_reassoc_resp(struct wlantest *wt, const u8 *data,
  282. size_t len)
  283. {
  284. const struct ieee80211_mgmt *mgmt;
  285. struct wlantest_bss *bss;
  286. struct wlantest_sta *sta;
  287. u16 capab, status, aid;
  288. mgmt = (const struct ieee80211_mgmt *) data;
  289. bss = bss_get(wt, mgmt->bssid);
  290. if (bss == NULL)
  291. return;
  292. sta = sta_get(bss, mgmt->da);
  293. if (sta == NULL)
  294. return;
  295. if (len < 24 + 6) {
  296. wpa_printf(MSG_INFO, "Too short Reassociation Response frame "
  297. "from " MACSTR, MAC2STR(mgmt->sa));
  298. return;
  299. }
  300. capab = le_to_host16(mgmt->u.reassoc_resp.capab_info);
  301. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  302. aid = le_to_host16(mgmt->u.reassoc_resp.aid);
  303. wpa_printf(MSG_DEBUG, "REASSOCRESP " MACSTR " -> " MACSTR
  304. " (capab=0x%x status=%u aid=%u)",
  305. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  306. aid & 0x3fff);
  307. if (status)
  308. return;
  309. if ((aid & 0xc000) != 0xc000) {
  310. wpa_printf(MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  311. "in Reassociation Response from " MACSTR,
  312. MAC2STR(mgmt->sa));
  313. }
  314. sta->aid = aid & 0xc000;
  315. if (sta->state < STATE2) {
  316. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 when "
  317. "getting associated", MAC2STR(sta->addr));
  318. }
  319. if (sta->state < STATE3) {
  320. wpa_printf(MSG_DEBUG, "STA " MACSTR
  321. " moved to State 3 with " MACSTR,
  322. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  323. sta->state = STATE3;
  324. }
  325. }
  326. static void rx_mgmt_disassoc(struct wlantest *wt, const u8 *data, size_t len,
  327. int valid)
  328. {
  329. const struct ieee80211_mgmt *mgmt;
  330. struct wlantest_bss *bss;
  331. struct wlantest_sta *sta;
  332. mgmt = (const struct ieee80211_mgmt *) data;
  333. bss = bss_get(wt, mgmt->bssid);
  334. if (bss == NULL)
  335. return;
  336. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  337. sta = sta_get(bss, mgmt->da);
  338. else
  339. sta = sta_get(bss, mgmt->sa);
  340. if (sta == NULL)
  341. return;
  342. if (len < 24 + 2) {
  343. wpa_printf(MSG_INFO, "Too short Disassociation frame from "
  344. MACSTR, MAC2STR(mgmt->sa));
  345. return;
  346. }
  347. wpa_printf(MSG_DEBUG, "DISASSOC " MACSTR " -> " MACSTR
  348. " (reason=%u)",
  349. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  350. le_to_host16(mgmt->u.disassoc.reason_code));
  351. wpa_hexdump(MSG_MSGDUMP, "DISASSOC payload", data + 24, len - 24);
  352. if (!valid) {
  353. wpa_printf(MSG_INFO, "Do not change STA " MACSTR " State "
  354. "since Disassociation frame was not protected "
  355. "correctly", MAC2STR(sta->addr));
  356. return;
  357. }
  358. if (sta->state < STATE2) {
  359. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 or 3 "
  360. "when getting disassociated", MAC2STR(sta->addr));
  361. }
  362. if (sta->state > STATE2) {
  363. wpa_printf(MSG_DEBUG, "STA " MACSTR
  364. " moved to State 2 with " MACSTR,
  365. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  366. sta->state = STATE2;
  367. }
  368. }
  369. static void rx_mgmt_action_sa_query(struct wlantest *wt,
  370. struct wlantest_sta *sta,
  371. const struct ieee80211_mgmt *mgmt,
  372. size_t len)
  373. {
  374. const u8 *rx_id;
  375. u8 *id;
  376. if (len < 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  377. wpa_printf(MSG_INFO, "Too short SA Query frame from " MACSTR,
  378. MAC2STR(mgmt->sa));
  379. return;
  380. }
  381. if (len > 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
  382. size_t elen = len - (24 + 2 + WLAN_SA_QUERY_TR_ID_LEN);
  383. wpa_printf(MSG_INFO, "Unexpected %u octets of extra data at "
  384. "the end of SA Query frame from " MACSTR,
  385. (unsigned) elen, MAC2STR(mgmt->sa));
  386. wpa_hexdump(MSG_INFO, "SA Query extra data",
  387. ((const u8 *) mgmt) + len - elen, elen);
  388. }
  389. switch (mgmt->u.action.u.sa_query_req.action) {
  390. case WLAN_SA_QUERY_REQUEST:
  391. rx_id = (const u8 *) mgmt->u.action.u.sa_query_req.trans_id;
  392. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  393. id = sta->ap_sa_query_tr;
  394. else
  395. id = sta->sta_sa_query_tr;
  396. wpa_printf(MSG_INFO, "SA Query Request " MACSTR " -> " MACSTR
  397. " (trans_id=%02x%02x)",
  398. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  399. rx_id[0], rx_id[1]);
  400. os_memcpy(id, mgmt->u.action.u.sa_query_req.trans_id, 2);
  401. break;
  402. case WLAN_SA_QUERY_RESPONSE:
  403. rx_id = (const u8 *) mgmt->u.action.u.sa_query_resp.trans_id;
  404. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  405. id = sta->sta_sa_query_tr;
  406. else
  407. id = sta->ap_sa_query_tr;
  408. wpa_printf(MSG_INFO, "SA Query Response " MACSTR " -> " MACSTR
  409. " (trans_id=%02x%02x; %s)",
  410. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  411. rx_id[0], rx_id[1],
  412. os_memcmp(rx_id, id, 2) == 0 ?
  413. "match" : "mismatch");
  414. break;
  415. default:
  416. wpa_printf(MSG_INFO, "Unexpected SA Query action value %u "
  417. "from " MACSTR,
  418. mgmt->u.action.u.sa_query_req.action,
  419. MAC2STR(mgmt->sa));
  420. }
  421. }
  422. static void rx_mgmt_action(struct wlantest *wt, const u8 *data, size_t len)
  423. {
  424. const struct ieee80211_mgmt *mgmt;
  425. struct wlantest_bss *bss;
  426. struct wlantest_sta *sta;
  427. mgmt = (const struct ieee80211_mgmt *) data;
  428. if (mgmt->da[0] & 0x01)
  429. return; /* Ignore group addressed Action frames for now */
  430. bss = bss_get(wt, mgmt->bssid);
  431. if (bss == NULL)
  432. return;
  433. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  434. sta = sta_get(bss, mgmt->da);
  435. else
  436. sta = sta_get(bss, mgmt->sa);
  437. if (sta == NULL)
  438. return;
  439. if (len < 24 + 1) {
  440. wpa_printf(MSG_INFO, "Too short Action frame from "
  441. MACSTR, MAC2STR(mgmt->sa));
  442. return;
  443. }
  444. wpa_printf(MSG_DEBUG, "ACTION " MACSTR " -> " MACSTR
  445. " (category=%u)",
  446. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  447. mgmt->u.action.category);
  448. wpa_hexdump(MSG_MSGDUMP, "ACTION payload", data + 24, len - 24);
  449. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  450. sta->state < STATE3) {
  451. wpa_printf(MSG_INFO, "Action frame sent when STA is not in "
  452. "State 3 (SA=" MACSTR " DATA=" MACSTR ")",
  453. MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  454. }
  455. switch (mgmt->u.action.category) {
  456. case WLAN_ACTION_SA_QUERY:
  457. rx_mgmt_action_sa_query(wt, sta, mgmt, len);
  458. break;
  459. }
  460. }
  461. static int check_mmie_mic(const u8 *igtk, const u8 *data, size_t len)
  462. {
  463. u8 *buf;
  464. u8 mic[16];
  465. u16 fc;
  466. const struct ieee80211_hdr *hdr;
  467. buf = os_malloc(len + 20 - 24);
  468. if (buf == NULL)
  469. return -1;
  470. /* BIP AAD: FC(masked) A1 A2 A3 */
  471. hdr = (const struct ieee80211_hdr *) data;
  472. fc = le_to_host16(hdr->frame_control);
  473. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  474. WPA_PUT_LE16(buf, fc);
  475. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  476. /* Frame body with MMIE MIC masked to zero */
  477. os_memcpy(buf + 20, data + 24, len - 24 - 8);
  478. os_memset(buf + 20 + len - 24 - 8, 0, 8);
  479. wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, len + 20 - 24);
  480. /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
  481. if (omac1_aes_128(igtk, buf, len + 20 - 24, mic) < 0) {
  482. os_free(buf);
  483. return -1;
  484. }
  485. os_free(buf);
  486. if (os_memcmp(data + len - 8, mic, 8) != 0)
  487. return -1;
  488. return 0;
  489. }
  490. static int check_bip(struct wlantest *wt, const u8 *data, size_t len)
  491. {
  492. const struct ieee80211_mgmt *mgmt;
  493. u16 fc, stype;
  494. const u8 *mmie;
  495. int keyid;
  496. struct wlantest_bss *bss;
  497. mgmt = (const struct ieee80211_mgmt *) data;
  498. fc = le_to_host16(mgmt->frame_control);
  499. stype = WLAN_FC_GET_STYPE(fc);
  500. if (stype == WLAN_FC_STYPE_ACTION) {
  501. if (len < 24 + 1)
  502. return 0;
  503. if (mgmt->u.action.category == WLAN_ACTION_PUBLIC)
  504. return 0; /* Not a robust management frame */
  505. }
  506. bss = bss_get(wt, mgmt->bssid);
  507. if (bss == NULL)
  508. return 0; /* No key known yet */
  509. if (len < 24 + 18 || data[len - 18] != WLAN_EID_MMIE ||
  510. data[len - 17] != 16) {
  511. /* No MMIE */
  512. /* TODO: use MFPC flag in RSN IE instead of IGTK flags */
  513. if (bss->igtk_set[4] || bss->igtk_set[5]) {
  514. wpa_printf(MSG_INFO, "Robust group-addressed "
  515. "management frame sent without BIP by "
  516. MACSTR, MAC2STR(mgmt->sa));
  517. return -1;
  518. }
  519. return 0;
  520. }
  521. mmie = data + len - 16;
  522. keyid = WPA_GET_LE16(mmie);
  523. if (keyid < 4 || keyid > 5) {
  524. wpa_printf(MSG_INFO, "Unexpected MMIE KeyID %u from " MACSTR,
  525. keyid, MAC2STR(mgmt->sa));
  526. return 0;
  527. }
  528. wpa_printf(MSG_DEBUG, "MMIE KeyID %u", keyid);
  529. wpa_hexdump(MSG_MSGDUMP, "MMIE IPN", mmie + 2, 6);
  530. wpa_hexdump(MSG_MSGDUMP, "MMIE MIC", mmie + 8, 8);
  531. if (!bss->igtk_set[keyid]) {
  532. wpa_printf(MSG_DEBUG, "No IGTK known to validate BIP frame");
  533. return 0;
  534. }
  535. if (os_memcmp(mmie + 2, bss->ipn[keyid], 6) <= 0) {
  536. wpa_printf(MSG_INFO, "BIP replay detected: SA=" MACSTR,
  537. MAC2STR(mgmt->sa));
  538. wpa_hexdump(MSG_INFO, "RX IPN", mmie + 2, 6);
  539. wpa_hexdump(MSG_INFO, "Last RX IPN", bss->ipn[keyid], 6);
  540. }
  541. if (check_mmie_mic(bss->igtk[keyid], data, len) < 0) {
  542. wpa_printf(MSG_INFO, "Invalid MMIE MIC in a frame from "
  543. MACSTR, MAC2STR(mgmt->sa));
  544. return -1;
  545. }
  546. wpa_printf(MSG_DEBUG, "Valid MMIE MIC");
  547. os_memcpy(bss->ipn[keyid], mmie + 2, 6);
  548. return 0;
  549. }
  550. static u8 * mgmt_ccmp_decrypt(struct wlantest *wt, const u8 *data, size_t len,
  551. size_t *dlen)
  552. {
  553. struct wlantest_bss *bss;
  554. struct wlantest_sta *sta;
  555. const struct ieee80211_hdr *hdr;
  556. int keyid;
  557. u8 *decrypted, *frame;
  558. u8 pn[6], *rsc;
  559. hdr = (const struct ieee80211_hdr *) data;
  560. bss = bss_get(wt, hdr->addr3);
  561. if (bss == NULL)
  562. return NULL;
  563. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  564. sta = sta_get(bss, hdr->addr2);
  565. else
  566. sta = sta_get(bss, hdr->addr1);
  567. if (sta == NULL || !sta->ptk_set) {
  568. wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
  569. return NULL;
  570. }
  571. keyid = data[3] >> 6;
  572. if (keyid != 0) {
  573. wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
  574. "individually addressed Management frame from "
  575. MACSTR, keyid, MAC2STR(hdr->addr2));
  576. }
  577. if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
  578. rsc = sta->rsc_tods[16];
  579. else
  580. rsc = sta->rsc_fromds[16];
  581. ccmp_get_pn(pn, data + 24);
  582. if (os_memcmp(pn, rsc, 6) <= 0) {
  583. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  584. MAC2STR(hdr->addr2));
  585. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  586. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  587. }
  588. decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data + 24, len - 24, dlen);
  589. if (decrypted)
  590. os_memcpy(rsc, pn, 6);
  591. frame = os_malloc(24 + *dlen);
  592. if (frame) {
  593. os_memcpy(frame, data, 24);
  594. os_memcpy(frame + 24, decrypted, *dlen);
  595. *dlen += 24;
  596. }
  597. os_free(decrypted);
  598. return frame;
  599. }
  600. void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
  601. {
  602. const struct ieee80211_hdr *hdr;
  603. u16 fc, stype;
  604. int valid = 1;
  605. u8 *decrypted = NULL;
  606. size_t dlen;
  607. if (len < 24)
  608. return;
  609. hdr = (const struct ieee80211_hdr *) data;
  610. fc = le_to_host16(hdr->frame_control);
  611. wt->rx_mgmt++;
  612. stype = WLAN_FC_GET_STYPE(fc);
  613. if ((hdr->addr1[0] & 0x01) &&
  614. (stype == WLAN_FC_STYPE_DEAUTH ||
  615. stype == WLAN_FC_STYPE_DISASSOC ||
  616. stype == WLAN_FC_STYPE_ACTION))
  617. check_bip(wt, data, len);
  618. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  619. stype == WLAN_FC_STYPE_PROBE_RESP ||
  620. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  621. MSG_EXCESSIVE : MSG_MSGDUMP,
  622. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  623. mgmt_stype(stype),
  624. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  625. fc & WLAN_FC_ISWEP ? " Prot" : "",
  626. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  627. MAC2STR(hdr->addr3));
  628. if ((fc & WLAN_FC_ISWEP) &&
  629. !(hdr->addr1[0] & 0x01) &&
  630. (stype == WLAN_FC_STYPE_DEAUTH ||
  631. stype == WLAN_FC_STYPE_DISASSOC ||
  632. stype == WLAN_FC_STYPE_ACTION)) {
  633. decrypted = mgmt_ccmp_decrypt(wt, data, len, &dlen);
  634. if (decrypted) {
  635. write_pcap_decrypted(wt, decrypted, dlen, NULL, 0);
  636. data = decrypted;
  637. len = dlen;
  638. } else
  639. valid = 0;
  640. }
  641. /* TODO: verify that robust management frames are protected if MFP was
  642. * negotiated */
  643. switch (stype) {
  644. case WLAN_FC_STYPE_BEACON:
  645. rx_mgmt_beacon(wt, data, len);
  646. break;
  647. case WLAN_FC_STYPE_PROBE_RESP:
  648. rx_mgmt_probe_resp(wt, data, len);
  649. break;
  650. case WLAN_FC_STYPE_AUTH:
  651. rx_mgmt_auth(wt, data, len);
  652. break;
  653. case WLAN_FC_STYPE_DEAUTH:
  654. rx_mgmt_deauth(wt, data, len, valid);
  655. break;
  656. case WLAN_FC_STYPE_ASSOC_REQ:
  657. rx_mgmt_assoc_req(wt, data, len);
  658. break;
  659. case WLAN_FC_STYPE_ASSOC_RESP:
  660. rx_mgmt_assoc_resp(wt, data, len);
  661. break;
  662. case WLAN_FC_STYPE_REASSOC_REQ:
  663. rx_mgmt_reassoc_req(wt, data, len);
  664. break;
  665. case WLAN_FC_STYPE_REASSOC_RESP:
  666. rx_mgmt_reassoc_resp(wt, data, len);
  667. break;
  668. case WLAN_FC_STYPE_DISASSOC:
  669. rx_mgmt_disassoc(wt, data, len, valid);
  670. break;
  671. case WLAN_FC_STYPE_ACTION:
  672. if (valid)
  673. rx_mgmt_action(wt, data, len);
  674. break;
  675. }
  676. os_free(decrypted);
  677. }