process.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * Received 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 "utils/radiotap.h"
  17. #include "utils/radiotap_iter.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/ieee802_11_common.h"
  20. #include "wlantest.h"
  21. static const char * mgmt_stype(u16 stype)
  22. {
  23. switch (stype) {
  24. case WLAN_FC_STYPE_ASSOC_REQ:
  25. return "ASSOC-REQ";
  26. case WLAN_FC_STYPE_ASSOC_RESP:
  27. return "ASSOC-RESP";
  28. case WLAN_FC_STYPE_REASSOC_REQ:
  29. return "REASSOC-REQ";
  30. case WLAN_FC_STYPE_REASSOC_RESP:
  31. return "REASSOC-RESP";
  32. case WLAN_FC_STYPE_PROBE_REQ:
  33. return "PROBE-REQ";
  34. case WLAN_FC_STYPE_PROBE_RESP:
  35. return "PROBE-RESP";
  36. case WLAN_FC_STYPE_BEACON:
  37. return "BEACON";
  38. case WLAN_FC_STYPE_ATIM:
  39. return "ATIM";
  40. case WLAN_FC_STYPE_DISASSOC:
  41. return "DISASSOC";
  42. case WLAN_FC_STYPE_AUTH:
  43. return "AUTH";
  44. case WLAN_FC_STYPE_DEAUTH:
  45. return "DEAUTH";
  46. case WLAN_FC_STYPE_ACTION:
  47. return "ACTION";
  48. }
  49. return "??";
  50. }
  51. static void rx_mgmt_beacon(struct wlantest *wt, const u8 *data, size_t len)
  52. {
  53. const struct ieee80211_mgmt *mgmt;
  54. struct wlantest_bss *bss;
  55. struct ieee802_11_elems elems;
  56. mgmt = (const struct ieee80211_mgmt *) data;
  57. bss = bss_get(wt, mgmt->bssid);
  58. if (bss == NULL)
  59. return;
  60. if (bss->proberesp_seen)
  61. return; /* do not override with Beacon data */
  62. bss->capab_info = le_to_host16(mgmt->u.beacon.capab_info);
  63. if (ieee802_11_parse_elems(mgmt->u.beacon.variable,
  64. len - (mgmt->u.beacon.variable - data),
  65. &elems, 0) == ParseFailed) {
  66. if (bss->parse_error_reported)
  67. return;
  68. wpa_printf(MSG_INFO, "Invalid IEs in a Beacon frame from "
  69. MACSTR, MAC2STR(mgmt->sa));
  70. bss->parse_error_reported = 1;
  71. return;
  72. }
  73. bss_update(bss, &elems);
  74. }
  75. static void rx_mgmt_probe_resp(struct wlantest *wt, const u8 *data, size_t len)
  76. {
  77. const struct ieee80211_mgmt *mgmt;
  78. struct wlantest_bss *bss;
  79. struct ieee802_11_elems elems;
  80. mgmt = (const struct ieee80211_mgmt *) data;
  81. bss = bss_get(wt, mgmt->bssid);
  82. if (bss == NULL)
  83. return;
  84. bss->capab_info = le_to_host16(mgmt->u.probe_resp.capab_info);
  85. if (ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
  86. len - (mgmt->u.probe_resp.variable - data),
  87. &elems, 0) == ParseFailed) {
  88. if (bss->parse_error_reported)
  89. return;
  90. wpa_printf(MSG_INFO, "Invalid IEs in a Probe Response frame "
  91. "from " MACSTR, MAC2STR(mgmt->sa));
  92. bss->parse_error_reported = 1;
  93. return;
  94. }
  95. bss_update(bss, &elems);
  96. }
  97. static void rx_mgmt_auth(struct wlantest *wt, const u8 *data, size_t len)
  98. {
  99. const struct ieee80211_mgmt *mgmt;
  100. struct wlantest_bss *bss;
  101. struct wlantest_sta *sta;
  102. u16 alg, trans, status;
  103. mgmt = (const struct ieee80211_mgmt *) data;
  104. bss = bss_get(wt, mgmt->bssid);
  105. if (bss == NULL)
  106. return;
  107. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  108. sta = sta_get(bss, mgmt->da);
  109. else
  110. sta = sta_get(bss, mgmt->sa);
  111. if (sta == NULL)
  112. return;
  113. if (len < 24 + 6) {
  114. wpa_printf(MSG_INFO, "Too short Authentication frame from "
  115. MACSTR, MAC2STR(mgmt->sa));
  116. return;
  117. }
  118. alg = le_to_host16(mgmt->u.auth.auth_alg);
  119. trans = le_to_host16(mgmt->u.auth.auth_transaction);
  120. status = le_to_host16(mgmt->u.auth.status_code);
  121. wpa_printf(MSG_DEBUG, "AUTH " MACSTR " -> " MACSTR
  122. " (alg=%u trans=%u status=%u)",
  123. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), alg, trans, status);
  124. if (alg == 0 && trans == 2 && status == 0) {
  125. if (sta->state == STATE1) {
  126. wpa_printf(MSG_DEBUG, "STA " MACSTR
  127. " moved to State 2 with " MACSTR,
  128. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  129. sta->state = STATE2;
  130. }
  131. }
  132. }
  133. static void rx_mgmt_deauth(struct wlantest *wt, const u8 *data, size_t len)
  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. if (sta->state != STATE1) {
  158. wpa_printf(MSG_DEBUG, "STA " MACSTR
  159. " moved to State 1 with " MACSTR,
  160. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  161. sta->state = STATE1;
  162. }
  163. }
  164. static void rx_mgmt_assoc_req(struct wlantest *wt, const u8 *data, size_t len)
  165. {
  166. const struct ieee80211_mgmt *mgmt;
  167. struct wlantest_bss *bss;
  168. struct wlantest_sta *sta;
  169. mgmt = (const struct ieee80211_mgmt *) data;
  170. bss = bss_get(wt, mgmt->bssid);
  171. if (bss == NULL)
  172. return;
  173. sta = sta_get(bss, mgmt->sa);
  174. if (sta == NULL)
  175. return;
  176. if (len < 24 + 4) {
  177. wpa_printf(MSG_INFO, "Too short Association Request frame "
  178. "from " MACSTR, MAC2STR(mgmt->sa));
  179. return;
  180. }
  181. wpa_printf(MSG_DEBUG, "ASSOCREQ " MACSTR " -> " MACSTR
  182. " (capab=0x%x listen_int=%u)",
  183. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  184. le_to_host16(mgmt->u.assoc_req.capab_info),
  185. le_to_host16(mgmt->u.assoc_req.listen_interval));
  186. }
  187. static void rx_mgmt_assoc_resp(struct wlantest *wt, const u8 *data, size_t len)
  188. {
  189. const struct ieee80211_mgmt *mgmt;
  190. struct wlantest_bss *bss;
  191. struct wlantest_sta *sta;
  192. u16 capab, status, aid;
  193. mgmt = (const struct ieee80211_mgmt *) data;
  194. bss = bss_get(wt, mgmt->bssid);
  195. if (bss == NULL)
  196. return;
  197. sta = sta_get(bss, mgmt->da);
  198. if (sta == NULL)
  199. return;
  200. if (len < 24 + 6) {
  201. wpa_printf(MSG_INFO, "Too short Association Response frame "
  202. "from " MACSTR, MAC2STR(mgmt->sa));
  203. return;
  204. }
  205. capab = le_to_host16(mgmt->u.assoc_resp.capab_info);
  206. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  207. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  208. wpa_printf(MSG_DEBUG, "ASSOCRESP " MACSTR " -> " MACSTR
  209. " (capab=0x%x status=%u aid=%u)",
  210. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  211. aid & 0x3fff);
  212. if (status)
  213. return;
  214. if ((aid & 0xc000) != 0xc000) {
  215. wpa_printf(MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  216. "in Association Response from " MACSTR,
  217. MAC2STR(mgmt->sa));
  218. }
  219. sta->aid = aid & 0xc000;
  220. if (sta->state < STATE2) {
  221. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 when "
  222. "getting associated", MAC2STR(sta->addr));
  223. }
  224. if (sta->state < STATE3) {
  225. wpa_printf(MSG_DEBUG, "STA " MACSTR
  226. " moved to State 3 with " MACSTR,
  227. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  228. sta->state = STATE3;
  229. }
  230. }
  231. static void rx_mgmt_reassoc_req(struct wlantest *wt, const u8 *data,
  232. size_t len)
  233. {
  234. const struct ieee80211_mgmt *mgmt;
  235. struct wlantest_bss *bss;
  236. struct wlantest_sta *sta;
  237. mgmt = (const struct ieee80211_mgmt *) data;
  238. bss = bss_get(wt, mgmt->bssid);
  239. if (bss == NULL)
  240. return;
  241. sta = sta_get(bss, mgmt->sa);
  242. if (sta == NULL)
  243. return;
  244. if (len < 24 + 4 + ETH_ALEN) {
  245. wpa_printf(MSG_INFO, "Too short Reassociation Request frame "
  246. "from " MACSTR, MAC2STR(mgmt->sa));
  247. return;
  248. }
  249. wpa_printf(MSG_DEBUG, "REASSOCREQ " MACSTR " -> " MACSTR
  250. " (capab=0x%x listen_int=%u current_ap=" MACSTR ")",
  251. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  252. le_to_host16(mgmt->u.reassoc_req.capab_info),
  253. le_to_host16(mgmt->u.reassoc_req.listen_interval),
  254. MAC2STR(mgmt->u.reassoc_req.current_ap));
  255. }
  256. static void rx_mgmt_reassoc_resp(struct wlantest *wt, const u8 *data,
  257. size_t len)
  258. {
  259. const struct ieee80211_mgmt *mgmt;
  260. struct wlantest_bss *bss;
  261. struct wlantest_sta *sta;
  262. u16 capab, status, aid;
  263. mgmt = (const struct ieee80211_mgmt *) data;
  264. bss = bss_get(wt, mgmt->bssid);
  265. if (bss == NULL)
  266. return;
  267. sta = sta_get(bss, mgmt->da);
  268. if (sta == NULL)
  269. return;
  270. if (len < 24 + 6) {
  271. wpa_printf(MSG_INFO, "Too short Reassociation Response frame "
  272. "from " MACSTR, MAC2STR(mgmt->sa));
  273. return;
  274. }
  275. capab = le_to_host16(mgmt->u.reassoc_resp.capab_info);
  276. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  277. aid = le_to_host16(mgmt->u.reassoc_resp.aid);
  278. wpa_printf(MSG_DEBUG, "REASSOCRESP " MACSTR " -> " MACSTR
  279. " (capab=0x%x status=%u aid=%u)",
  280. MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
  281. aid & 0x3fff);
  282. if (status)
  283. return;
  284. if ((aid & 0xc000) != 0xc000) {
  285. wpa_printf(MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
  286. "in Reassociation Response from " MACSTR,
  287. MAC2STR(mgmt->sa));
  288. }
  289. sta->aid = aid & 0xc000;
  290. if (sta->state < STATE2) {
  291. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 when "
  292. "getting associated", MAC2STR(sta->addr));
  293. }
  294. if (sta->state < STATE3) {
  295. wpa_printf(MSG_DEBUG, "STA " MACSTR
  296. " moved to State 3 with " MACSTR,
  297. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  298. sta->state = STATE3;
  299. }
  300. }
  301. static void rx_mgmt_disassoc(struct wlantest *wt, const u8 *data, size_t len)
  302. {
  303. const struct ieee80211_mgmt *mgmt;
  304. struct wlantest_bss *bss;
  305. struct wlantest_sta *sta;
  306. mgmt = (const struct ieee80211_mgmt *) data;
  307. bss = bss_get(wt, mgmt->bssid);
  308. if (bss == NULL)
  309. return;
  310. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  311. sta = sta_get(bss, mgmt->da);
  312. else
  313. sta = sta_get(bss, mgmt->sa);
  314. if (sta == NULL)
  315. return;
  316. if (len < 24 + 2) {
  317. wpa_printf(MSG_INFO, "Too short Disassociation frame from "
  318. MACSTR, MAC2STR(mgmt->sa));
  319. return;
  320. }
  321. wpa_printf(MSG_DEBUG, "DISASSOC " MACSTR " -> " MACSTR
  322. " (reason=%u)",
  323. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  324. le_to_host16(mgmt->u.disassoc.reason_code));
  325. if (sta->state < STATE2) {
  326. wpa_printf(MSG_DEBUG, "STA " MACSTR " was not in State 2 or 3 "
  327. "when getting disassociated", MAC2STR(sta->addr));
  328. }
  329. if (sta->state > STATE2) {
  330. wpa_printf(MSG_DEBUG, "STA " MACSTR
  331. " moved to State 2 with " MACSTR,
  332. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  333. sta->state = STATE2;
  334. }
  335. }
  336. static void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
  337. {
  338. const struct ieee80211_hdr *hdr;
  339. u16 fc, stype;
  340. if (len < 24)
  341. return;
  342. hdr = (const struct ieee80211_hdr *) data;
  343. fc = le_to_host16(hdr->frame_control);
  344. wt->rx_mgmt++;
  345. stype = WLAN_FC_GET_STYPE(fc);
  346. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  347. stype == WLAN_FC_STYPE_PROBE_RESP ||
  348. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  349. MSG_EXCESSIVE : MSG_MSGDUMP,
  350. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  351. mgmt_stype(stype),
  352. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  353. fc & WLAN_FC_ISWEP ? " Prot" : "",
  354. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  355. MAC2STR(hdr->addr3));
  356. switch (stype) {
  357. case WLAN_FC_STYPE_BEACON:
  358. rx_mgmt_beacon(wt, data, len);
  359. break;
  360. case WLAN_FC_STYPE_PROBE_RESP:
  361. rx_mgmt_probe_resp(wt, data, len);
  362. break;
  363. case WLAN_FC_STYPE_AUTH:
  364. rx_mgmt_auth(wt, data, len);
  365. break;
  366. case WLAN_FC_STYPE_DEAUTH:
  367. rx_mgmt_deauth(wt, data, len);
  368. break;
  369. case WLAN_FC_STYPE_ASSOC_REQ:
  370. rx_mgmt_assoc_req(wt, data, len);
  371. break;
  372. case WLAN_FC_STYPE_ASSOC_RESP:
  373. rx_mgmt_assoc_resp(wt, data, len);
  374. break;
  375. case WLAN_FC_STYPE_REASSOC_REQ:
  376. rx_mgmt_reassoc_req(wt, data, len);
  377. break;
  378. case WLAN_FC_STYPE_REASSOC_RESP:
  379. rx_mgmt_reassoc_resp(wt, data, len);
  380. break;
  381. case WLAN_FC_STYPE_DISASSOC:
  382. rx_mgmt_disassoc(wt, data, len);
  383. break;
  384. }
  385. }
  386. static const char * data_stype(u16 stype)
  387. {
  388. switch (stype) {
  389. case WLAN_FC_STYPE_DATA:
  390. return "DATA";
  391. case WLAN_FC_STYPE_DATA_CFACK:
  392. return "DATA-CFACK";
  393. case WLAN_FC_STYPE_DATA_CFPOLL:
  394. return "DATA-CFPOLL";
  395. case WLAN_FC_STYPE_DATA_CFACKPOLL:
  396. return "DATA-CFACKPOLL";
  397. case WLAN_FC_STYPE_NULLFUNC:
  398. return "NULLFUNC";
  399. case WLAN_FC_STYPE_CFACK:
  400. return "CFACK";
  401. case WLAN_FC_STYPE_CFPOLL:
  402. return "CFPOLL";
  403. case WLAN_FC_STYPE_CFACKPOLL:
  404. return "CFACKPOLL";
  405. case WLAN_FC_STYPE_QOS_DATA:
  406. return "QOSDATA";
  407. case WLAN_FC_STYPE_QOS_DATA_CFACK:
  408. return "QOSDATA-CFACK";
  409. case WLAN_FC_STYPE_QOS_DATA_CFPOLL:
  410. return "QOSDATA-CFPOLL";
  411. case WLAN_FC_STYPE_QOS_DATA_CFACKPOLL:
  412. return "QOSDATA-CFACKPOLL";
  413. case WLAN_FC_STYPE_QOS_NULL:
  414. return "QOS-NULL";
  415. case WLAN_FC_STYPE_QOS_CFPOLL:
  416. return "QOS-CFPOLL";
  417. case WLAN_FC_STYPE_QOS_CFACKPOLL:
  418. return "QOS-CFACKPOLL";
  419. }
  420. return "??";
  421. }
  422. static void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  423. {
  424. const struct ieee80211_hdr *hdr;
  425. u16 fc;
  426. if (len < 24)
  427. return;
  428. hdr = (const struct ieee80211_hdr *) data;
  429. fc = le_to_host16(hdr->frame_control);
  430. wt->rx_data++;
  431. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  432. case 0:
  433. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  434. MACSTR " BSSID=" MACSTR,
  435. data_stype(WLAN_FC_GET_STYPE(fc)),
  436. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  437. fc & WLAN_FC_ISWEP ? " Prot" : "",
  438. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  439. MAC2STR(hdr->addr3));
  440. break;
  441. case WLAN_FC_FROMDS:
  442. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  443. " BSSID=" MACSTR " SA=" MACSTR,
  444. data_stype(WLAN_FC_GET_STYPE(fc)),
  445. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  446. fc & WLAN_FC_ISWEP ? " Prot" : "",
  447. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  448. MAC2STR(hdr->addr3));
  449. break;
  450. case WLAN_FC_TODS:
  451. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  452. " SA=" MACSTR " DA=" MACSTR,
  453. data_stype(WLAN_FC_GET_STYPE(fc)),
  454. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  455. fc & WLAN_FC_ISWEP ? " Prot" : "",
  456. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  457. MAC2STR(hdr->addr3));
  458. break;
  459. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  460. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  461. MACSTR " DA=" MACSTR " SA=" MACSTR,
  462. data_stype(WLAN_FC_GET_STYPE(fc)),
  463. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  464. fc & WLAN_FC_ISWEP ? " Prot" : "",
  465. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  466. MAC2STR(hdr->addr3),
  467. MAC2STR((const u8 *) (hdr + 1)));
  468. break;
  469. }
  470. }
  471. static void rx_frame(struct wlantest *wt, const u8 *data, size_t len)
  472. {
  473. const struct ieee80211_hdr *hdr;
  474. u16 fc;
  475. wpa_hexdump(MSG_EXCESSIVE, "RX frame", data, len);
  476. if (len < 2)
  477. return;
  478. hdr = (const struct ieee80211_hdr *) data;
  479. fc = le_to_host16(hdr->frame_control);
  480. if (fc & WLAN_FC_PVER) {
  481. wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected pver=%d",
  482. fc & WLAN_FC_PVER);
  483. return;
  484. }
  485. switch (WLAN_FC_GET_TYPE(fc)) {
  486. case WLAN_FC_TYPE_MGMT:
  487. rx_mgmt(wt, data, len);
  488. break;
  489. case WLAN_FC_TYPE_CTRL:
  490. if (len < 10)
  491. return;
  492. wt->rx_ctrl++;
  493. break;
  494. case WLAN_FC_TYPE_DATA:
  495. rx_data(wt, data, len);
  496. break;
  497. default:
  498. wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected type %d",
  499. WLAN_FC_GET_TYPE(fc));
  500. break;
  501. }
  502. }
  503. static void tx_status(struct wlantest *wt, const u8 *data, size_t len, int ack)
  504. {
  505. wpa_printf(MSG_DEBUG, "TX status: ack=%d", ack);
  506. wpa_hexdump(MSG_EXCESSIVE, "TX status frame", data, len);
  507. }
  508. static int check_fcs(const u8 *frame, size_t frame_len, const u8 *fcs)
  509. {
  510. if (WPA_GET_LE32(fcs) != crc32(frame, frame_len))
  511. return -1;
  512. return 0;
  513. }
  514. void wlantest_process(struct wlantest *wt, const u8 *data, size_t len)
  515. {
  516. struct ieee80211_radiotap_iterator iter;
  517. int ret;
  518. int rxflags = 0, txflags = 0, failed = 0, fcs = 0;
  519. const u8 *frame, *fcspos;
  520. size_t frame_len;
  521. wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
  522. if (ieee80211_radiotap_iterator_init(&iter, (void *) data, len)) {
  523. wpa_printf(MSG_INFO, "Invalid radiotap frame");
  524. return;
  525. }
  526. for (;;) {
  527. ret = ieee80211_radiotap_iterator_next(&iter);
  528. wpa_printf(MSG_EXCESSIVE, "radiotap iter: %d "
  529. "this_arg_index=%d", ret, iter.this_arg_index);
  530. if (ret == -ENOENT)
  531. break;
  532. if (ret) {
  533. wpa_printf(MSG_INFO, "Invalid radiotap header: %d",
  534. ret);
  535. return;
  536. }
  537. switch (iter.this_arg_index) {
  538. case IEEE80211_RADIOTAP_FLAGS:
  539. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  540. fcs = 1;
  541. break;
  542. case IEEE80211_RADIOTAP_RX_FLAGS:
  543. rxflags = 1;
  544. break;
  545. case IEEE80211_RADIOTAP_TX_FLAGS:
  546. txflags = 1;
  547. failed = le_to_host16((*(u16 *) iter.this_arg)) &
  548. IEEE80211_RADIOTAP_F_TX_FAIL;
  549. break;
  550. }
  551. }
  552. frame = data + iter.max_length;
  553. frame_len = len - iter.max_length;
  554. if (fcs && frame_len >= 4) {
  555. frame_len -= 4;
  556. fcspos = frame + frame_len;
  557. if (check_fcs(frame, frame_len, fcspos) < 0) {
  558. wpa_printf(MSG_EXCESSIVE, "Drop RX frame with invalid "
  559. "FCS");
  560. wt->fcs_error++;
  561. return;
  562. }
  563. }
  564. if (rxflags && txflags)
  565. return;
  566. if (!txflags)
  567. rx_frame(wt, frame, frame_len);
  568. else
  569. tx_status(wt, frame, frame_len, !failed);
  570. }