process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 bss_update(struct wlantest_bss *bss,
  52. struct ieee802_11_elems *elems)
  53. {
  54. if (elems->ssid == NULL || elems->ssid_len > 32) {
  55. wpa_printf(MSG_INFO, "Invalid or missing SSID in a Beacon "
  56. "frame for " MACSTR, MAC2STR(bss->bssid));
  57. bss->parse_error_reported = 1;
  58. return;
  59. }
  60. os_memcpy(bss->ssid, elems->ssid, elems->ssid_len);
  61. bss->ssid_len = elems->ssid_len;
  62. if (elems->rsn_ie == NULL) {
  63. if (bss->rsnie[0]) {
  64. wpa_printf(MSG_INFO, "BSS " MACSTR " - RSN IE removed",
  65. MAC2STR(bss->bssid));
  66. bss->rsnie[0] = 0;
  67. }
  68. } else {
  69. if (bss->rsnie[0] == 0 ||
  70. os_memcmp(bss->rsnie, elems->rsn_ie - 2,
  71. elems->rsn_ie_len + 2) != 0) {
  72. wpa_printf(MSG_INFO, "BSS " MACSTR " - RSN IE "
  73. "stored", MAC2STR(bss->bssid));
  74. wpa_hexdump(MSG_DEBUG, "RSN IE", elems->rsn_ie - 2,
  75. elems->rsn_ie_len + 2);
  76. }
  77. os_memcpy(bss->rsnie, elems->rsn_ie - 2,
  78. elems->rsn_ie_len + 2);
  79. }
  80. if (elems->wpa_ie == NULL) {
  81. if (bss->wpaie[0]) {
  82. wpa_printf(MSG_INFO, "BSS " MACSTR " - WPA IE removed",
  83. MAC2STR(bss->bssid));
  84. bss->wpaie[0] = 0;
  85. }
  86. } else {
  87. if (bss->wpaie[0] == 0 ||
  88. os_memcmp(bss->wpaie, elems->wpa_ie - 2,
  89. elems->wpa_ie_len + 2) != 0) {
  90. wpa_printf(MSG_INFO, "BSS " MACSTR " - WPA IE "
  91. "stored", MAC2STR(bss->bssid));
  92. wpa_hexdump(MSG_DEBUG, "WPA IE", elems->wpa_ie - 2,
  93. elems->wpa_ie_len + 2);
  94. }
  95. os_memcpy(bss->wpaie, elems->wpa_ie - 2,
  96. elems->wpa_ie_len + 2);
  97. }
  98. }
  99. static void rx_mgmt_beacon(struct wlantest *wt, const u8 *data, size_t len)
  100. {
  101. const struct ieee80211_mgmt *mgmt;
  102. struct wlantest_bss *bss;
  103. struct ieee802_11_elems elems;
  104. mgmt = (const struct ieee80211_mgmt *) data;
  105. bss = bss_get(wt, mgmt->bssid);
  106. if (bss == NULL)
  107. return;
  108. if (bss->proberesp_seen)
  109. return; /* do not override with Beacon data */
  110. bss->capab_info = le_to_host16(mgmt->u.beacon.capab_info);
  111. if (ieee802_11_parse_elems(mgmt->u.beacon.variable,
  112. len - (mgmt->u.beacon.variable - data),
  113. &elems, 0) == ParseFailed) {
  114. if (bss->parse_error_reported)
  115. return;
  116. wpa_printf(MSG_INFO, "Invalid IEs in a Beacon frame from "
  117. MACSTR, MAC2STR(mgmt->sa));
  118. bss->parse_error_reported = 1;
  119. return;
  120. }
  121. bss_update(bss, &elems);
  122. }
  123. static void rx_mgmt_probe_resp(struct wlantest *wt, const u8 *data, size_t len)
  124. {
  125. const struct ieee80211_mgmt *mgmt;
  126. struct wlantest_bss *bss;
  127. struct ieee802_11_elems elems;
  128. mgmt = (const struct ieee80211_mgmt *) data;
  129. bss = bss_get(wt, mgmt->bssid);
  130. if (bss == NULL)
  131. return;
  132. bss->capab_info = le_to_host16(mgmt->u.probe_resp.capab_info);
  133. if (ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
  134. len - (mgmt->u.probe_resp.variable - data),
  135. &elems, 0) == ParseFailed) {
  136. if (bss->parse_error_reported)
  137. return;
  138. wpa_printf(MSG_INFO, "Invalid IEs in a Probe Response frame "
  139. "from " MACSTR, MAC2STR(mgmt->sa));
  140. bss->parse_error_reported = 1;
  141. return;
  142. }
  143. bss_update(bss, &elems);
  144. }
  145. static void rx_mgmt_auth(struct wlantest *wt, const u8 *data, size_t len)
  146. {
  147. const struct ieee80211_mgmt *mgmt;
  148. struct wlantest_bss *bss;
  149. struct wlantest_sta *sta;
  150. mgmt = (const struct ieee80211_mgmt *) data;
  151. bss = bss_get(wt, mgmt->bssid);
  152. if (bss == NULL)
  153. return;
  154. if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
  155. sta = sta_get(bss, mgmt->da);
  156. else
  157. sta = sta_get(bss, mgmt->sa);
  158. if (sta == NULL)
  159. return;
  160. if (len < 24 + 6) {
  161. wpa_printf(MSG_INFO, "Too short Authentication frame from "
  162. MACSTR, MAC2STR(mgmt->sa));
  163. return;
  164. }
  165. wpa_printf(MSG_DEBUG, "AUTH " MACSTR " -> " MACSTR
  166. " (alg=%u trans=%u status=%u)",
  167. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  168. le_to_host16(mgmt->u.auth.auth_alg),
  169. le_to_host16(mgmt->u.auth.auth_transaction),
  170. le_to_host16(mgmt->u.auth.status_code));
  171. }
  172. static void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
  173. {
  174. const struct ieee80211_hdr *hdr;
  175. u16 fc, stype;
  176. if (len < 24)
  177. return;
  178. hdr = (const struct ieee80211_hdr *) data;
  179. fc = le_to_host16(hdr->frame_control);
  180. wt->rx_mgmt++;
  181. stype = WLAN_FC_GET_STYPE(fc);
  182. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  183. stype == WLAN_FC_STYPE_PROBE_RESP ||
  184. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  185. MSG_EXCESSIVE : MSG_MSGDUMP,
  186. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  187. mgmt_stype(stype),
  188. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  189. fc & WLAN_FC_ISWEP ? " Prot" : "",
  190. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  191. MAC2STR(hdr->addr3));
  192. switch (stype) {
  193. case WLAN_FC_STYPE_BEACON:
  194. rx_mgmt_beacon(wt, data, len);
  195. break;
  196. case WLAN_FC_STYPE_PROBE_RESP:
  197. rx_mgmt_probe_resp(wt, data, len);
  198. break;
  199. case WLAN_FC_STYPE_AUTH:
  200. rx_mgmt_auth(wt, data, len);
  201. break;
  202. }
  203. }
  204. static const char * data_stype(u16 stype)
  205. {
  206. switch (stype) {
  207. case WLAN_FC_STYPE_DATA:
  208. return "DATA";
  209. case WLAN_FC_STYPE_DATA_CFACK:
  210. return "DATA-CFACK";
  211. case WLAN_FC_STYPE_DATA_CFPOLL:
  212. return "DATA-CFPOLL";
  213. case WLAN_FC_STYPE_DATA_CFACKPOLL:
  214. return "DATA-CFACKPOLL";
  215. case WLAN_FC_STYPE_NULLFUNC:
  216. return "NULLFUNC";
  217. case WLAN_FC_STYPE_CFACK:
  218. return "CFACK";
  219. case WLAN_FC_STYPE_CFPOLL:
  220. return "CFPOLL";
  221. case WLAN_FC_STYPE_CFACKPOLL:
  222. return "CFACKPOLL";
  223. case WLAN_FC_STYPE_QOS_DATA:
  224. return "QOSDATA";
  225. case WLAN_FC_STYPE_QOS_DATA_CFACK:
  226. return "QOSDATA-CFACK";
  227. case WLAN_FC_STYPE_QOS_DATA_CFPOLL:
  228. return "QOSDATA-CFPOLL";
  229. case WLAN_FC_STYPE_QOS_DATA_CFACKPOLL:
  230. return "QOSDATA-CFACKPOLL";
  231. case WLAN_FC_STYPE_QOS_NULL:
  232. return "QOS-NULL";
  233. case WLAN_FC_STYPE_QOS_CFPOLL:
  234. return "QOS-CFPOLL";
  235. case WLAN_FC_STYPE_QOS_CFACKPOLL:
  236. return "QOS-CFACKPOLL";
  237. }
  238. return "??";
  239. }
  240. static void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  241. {
  242. const struct ieee80211_hdr *hdr;
  243. u16 fc;
  244. if (len < 24)
  245. return;
  246. hdr = (const struct ieee80211_hdr *) data;
  247. fc = le_to_host16(hdr->frame_control);
  248. wt->rx_data++;
  249. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  250. case 0:
  251. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  252. MACSTR " BSSID=" MACSTR,
  253. data_stype(WLAN_FC_GET_STYPE(fc)),
  254. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  255. fc & WLAN_FC_ISWEP ? " Prot" : "",
  256. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  257. MAC2STR(hdr->addr3));
  258. break;
  259. case WLAN_FC_FROMDS:
  260. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  261. " BSSID=" MACSTR " SA=" MACSTR,
  262. data_stype(WLAN_FC_GET_STYPE(fc)),
  263. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  264. fc & WLAN_FC_ISWEP ? " Prot" : "",
  265. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  266. MAC2STR(hdr->addr3));
  267. break;
  268. case WLAN_FC_TODS:
  269. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  270. " SA=" MACSTR " DA=" MACSTR,
  271. data_stype(WLAN_FC_GET_STYPE(fc)),
  272. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  273. fc & WLAN_FC_ISWEP ? " Prot" : "",
  274. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  275. MAC2STR(hdr->addr3));
  276. break;
  277. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  278. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  279. MACSTR " DA=" MACSTR " SA=" MACSTR,
  280. data_stype(WLAN_FC_GET_STYPE(fc)),
  281. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  282. fc & WLAN_FC_ISWEP ? " Prot" : "",
  283. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  284. MAC2STR(hdr->addr3),
  285. MAC2STR((const u8 *) (hdr + 1)));
  286. break;
  287. }
  288. }
  289. static void rx_frame(struct wlantest *wt, const u8 *data, size_t len)
  290. {
  291. const struct ieee80211_hdr *hdr;
  292. u16 fc;
  293. wpa_hexdump(MSG_EXCESSIVE, "RX frame", data, len);
  294. if (len < 2)
  295. return;
  296. hdr = (const struct ieee80211_hdr *) data;
  297. fc = le_to_host16(hdr->frame_control);
  298. if (fc & WLAN_FC_PVER) {
  299. wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected pver=%d",
  300. fc & WLAN_FC_PVER);
  301. return;
  302. }
  303. switch (WLAN_FC_GET_TYPE(fc)) {
  304. case WLAN_FC_TYPE_MGMT:
  305. rx_mgmt(wt, data, len);
  306. break;
  307. case WLAN_FC_TYPE_CTRL:
  308. if (len < 10)
  309. return;
  310. wt->rx_ctrl++;
  311. break;
  312. case WLAN_FC_TYPE_DATA:
  313. rx_data(wt, data, len);
  314. break;
  315. default:
  316. wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected type %d",
  317. WLAN_FC_GET_TYPE(fc));
  318. break;
  319. }
  320. }
  321. static void tx_status(struct wlantest *wt, const u8 *data, size_t len, int ack)
  322. {
  323. wpa_printf(MSG_DEBUG, "TX status: ack=%d", ack);
  324. wpa_hexdump(MSG_EXCESSIVE, "TX status frame", data, len);
  325. }
  326. static int check_fcs(const u8 *frame, size_t frame_len, const u8 *fcs)
  327. {
  328. if (WPA_GET_LE32(fcs) != crc32(frame, frame_len))
  329. return -1;
  330. return 0;
  331. }
  332. void wlantest_process(struct wlantest *wt, const u8 *data, size_t len)
  333. {
  334. struct ieee80211_radiotap_iterator iter;
  335. int ret;
  336. int rxflags = 0, txflags = 0, failed = 0, fcs = 0;
  337. const u8 *frame, *fcspos;
  338. size_t frame_len;
  339. wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
  340. if (ieee80211_radiotap_iterator_init(&iter, (void *) data, len)) {
  341. wpa_printf(MSG_INFO, "Invalid radiotap frame");
  342. return;
  343. }
  344. for (;;) {
  345. ret = ieee80211_radiotap_iterator_next(&iter);
  346. wpa_printf(MSG_EXCESSIVE, "radiotap iter: %d "
  347. "this_arg_index=%d", ret, iter.this_arg_index);
  348. if (ret == -ENOENT)
  349. break;
  350. if (ret) {
  351. wpa_printf(MSG_INFO, "Invalid radiotap header: %d",
  352. ret);
  353. return;
  354. }
  355. switch (iter.this_arg_index) {
  356. case IEEE80211_RADIOTAP_FLAGS:
  357. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  358. fcs = 1;
  359. break;
  360. case IEEE80211_RADIOTAP_RX_FLAGS:
  361. rxflags = 1;
  362. break;
  363. case IEEE80211_RADIOTAP_TX_FLAGS:
  364. txflags = 1;
  365. failed = le_to_host16((*(u16 *) iter.this_arg)) &
  366. IEEE80211_RADIOTAP_F_TX_FAIL;
  367. break;
  368. }
  369. }
  370. frame = data + iter.max_length;
  371. frame_len = len - iter.max_length;
  372. if (fcs && frame_len >= 4) {
  373. frame_len -= 4;
  374. fcspos = frame + frame_len;
  375. if (check_fcs(frame, frame_len, fcspos) < 0) {
  376. wpa_printf(MSG_EXCESSIVE, "Drop RX frame with invalid "
  377. "FCS");
  378. wt->fcs_error++;
  379. return;
  380. }
  381. }
  382. if (rxflags && txflags)
  383. return;
  384. if (!txflags)
  385. rx_frame(wt, frame, frame_len);
  386. else
  387. tx_status(wt, frame, frame_len, !failed);
  388. }