process.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Received frame processing
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/crc32.h"
  11. #include "utils/radiotap.h"
  12. #include "utils/radiotap_iter.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/qca-vendor.h"
  15. #include "wlantest.h"
  16. static struct wlantest_sta * rx_get_sta(struct wlantest *wt,
  17. const struct ieee80211_hdr *hdr,
  18. size_t len, int *to_ap)
  19. {
  20. u16 fc;
  21. const u8 *sta_addr, *bssid;
  22. struct wlantest_bss *bss;
  23. *to_ap = 0;
  24. if (hdr->addr1[0] & 0x01)
  25. return NULL; /* Ignore group addressed frames */
  26. fc = le_to_host16(hdr->frame_control);
  27. switch (WLAN_FC_GET_TYPE(fc)) {
  28. case WLAN_FC_TYPE_MGMT:
  29. if (len < 24)
  30. return NULL;
  31. bssid = hdr->addr3;
  32. if (os_memcmp(bssid, hdr->addr2, ETH_ALEN) == 0) {
  33. sta_addr = hdr->addr1;
  34. *to_ap = 0;
  35. } else {
  36. if (os_memcmp(bssid, hdr->addr1, ETH_ALEN) != 0)
  37. return NULL; /* Unsupported STA-to-STA frame */
  38. sta_addr = hdr->addr2;
  39. *to_ap = 1;
  40. }
  41. break;
  42. case WLAN_FC_TYPE_DATA:
  43. if (len < 24)
  44. return NULL;
  45. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  46. case 0:
  47. return NULL; /* IBSS not supported */
  48. case WLAN_FC_FROMDS:
  49. sta_addr = hdr->addr1;
  50. bssid = hdr->addr2;
  51. *to_ap = 0;
  52. break;
  53. case WLAN_FC_TODS:
  54. sta_addr = hdr->addr2;
  55. bssid = hdr->addr1;
  56. *to_ap = 1;
  57. break;
  58. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  59. return NULL; /* WDS not supported */
  60. default:
  61. return NULL;
  62. }
  63. break;
  64. case WLAN_FC_TYPE_CTRL:
  65. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PSPOLL &&
  66. len >= 16) {
  67. sta_addr = hdr->addr2;
  68. bssid = hdr->addr1;
  69. *to_ap = 1;
  70. } else
  71. return NULL;
  72. break;
  73. default:
  74. return NULL;
  75. }
  76. bss = bss_find(wt, bssid);
  77. if (bss == NULL)
  78. return NULL;
  79. return sta_find(bss, sta_addr);
  80. }
  81. static void rx_update_ps(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  82. size_t len, struct wlantest_sta *sta, int to_ap)
  83. {
  84. u16 fc, type, stype;
  85. if (sta == NULL)
  86. return;
  87. fc = le_to_host16(hdr->frame_control);
  88. type = WLAN_FC_GET_TYPE(fc);
  89. stype = WLAN_FC_GET_STYPE(fc);
  90. if (!to_ap) {
  91. if (sta->pwrmgt && !sta->pspoll) {
  92. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  93. add_note(wt, MSG_DEBUG, "AP " MACSTR " sent a frame "
  94. "(%u:%u) to a sleeping STA " MACSTR
  95. " (seq=%u)",
  96. MAC2STR(sta->bss->bssid),
  97. type, stype, MAC2STR(sta->addr),
  98. WLAN_GET_SEQ_SEQ(seq_ctrl));
  99. } else
  100. sta->pspoll = 0;
  101. return;
  102. }
  103. sta->pspoll = 0;
  104. if (type == WLAN_FC_TYPE_DATA || type == WLAN_FC_TYPE_MGMT ||
  105. (type == WLAN_FC_TYPE_CTRL && stype == WLAN_FC_STYPE_PSPOLL)) {
  106. /*
  107. * In theory, the PS state changes only at the end of the frame
  108. * exchange that is ACKed by the AP. However, most cases are
  109. * handled with this simpler implementation that does not
  110. * maintain state through the frame exchange.
  111. */
  112. if (sta->pwrmgt && !(fc & WLAN_FC_PWRMGT)) {
  113. add_note(wt, MSG_DEBUG, "STA " MACSTR " woke up from "
  114. "sleep", MAC2STR(sta->addr));
  115. sta->pwrmgt = 0;
  116. } else if (!sta->pwrmgt && (fc & WLAN_FC_PWRMGT)) {
  117. add_note(wt, MSG_DEBUG, "STA " MACSTR " went to sleep",
  118. MAC2STR(sta->addr));
  119. sta->pwrmgt = 1;
  120. }
  121. }
  122. if (type == WLAN_FC_TYPE_CTRL && stype == WLAN_FC_STYPE_PSPOLL)
  123. sta->pspoll = 1;
  124. }
  125. static int rx_duplicate(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  126. size_t len, struct wlantest_sta *sta, int to_ap)
  127. {
  128. u16 fc;
  129. int tid = 16;
  130. le16 *seq_ctrl;
  131. if (sta == NULL)
  132. return 0;
  133. fc = le_to_host16(hdr->frame_control);
  134. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  135. (WLAN_FC_GET_STYPE(fc) & 0x08) && len >= 26) {
  136. const u8 *qos = ((const u8 *) hdr) + 24;
  137. tid = qos[0] & 0x0f;
  138. }
  139. if (to_ap)
  140. seq_ctrl = &sta->seq_ctrl_to_ap[tid];
  141. else
  142. seq_ctrl = &sta->seq_ctrl_to_sta[tid];
  143. if ((fc & WLAN_FC_RETRY) && hdr->seq_ctrl == *seq_ctrl) {
  144. u16 s = le_to_host16(hdr->seq_ctrl);
  145. add_note(wt, MSG_MSGDUMP, "Ignore duplicated frame (seq=%u "
  146. "frag=%u A1=" MACSTR " A2=" MACSTR ")",
  147. WLAN_GET_SEQ_SEQ(s), WLAN_GET_SEQ_FRAG(s),
  148. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2));
  149. return 1;
  150. }
  151. *seq_ctrl = hdr->seq_ctrl;
  152. return 0;
  153. }
  154. static void rx_ack(struct wlantest *wt, const struct ieee80211_hdr *hdr)
  155. {
  156. struct ieee80211_hdr *last = (struct ieee80211_hdr *) wt->last_hdr;
  157. u16 fc;
  158. if (wt->last_len < 24 || (last->addr1[0] & 0x01) ||
  159. os_memcmp(hdr->addr1, last->addr2, ETH_ALEN) != 0) {
  160. add_note(wt, MSG_MSGDUMP, "Unknown Ack frame (previous frame "
  161. "not seen)");
  162. return;
  163. }
  164. /* Ack to the previous frame */
  165. fc = le_to_host16(last->frame_control);
  166. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  167. rx_mgmt_ack(wt, last);
  168. }
  169. static void rx_frame(struct wlantest *wt, const u8 *data, size_t len)
  170. {
  171. const struct ieee80211_hdr *hdr;
  172. u16 fc;
  173. struct wlantest_sta *sta;
  174. int to_ap;
  175. wpa_hexdump(MSG_EXCESSIVE, "RX frame", data, len);
  176. if (len < 2)
  177. return;
  178. hdr = (const struct ieee80211_hdr *) data;
  179. fc = le_to_host16(hdr->frame_control);
  180. if (fc & WLAN_FC_PVER) {
  181. wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected pver=%d",
  182. fc & WLAN_FC_PVER);
  183. return;
  184. }
  185. sta = rx_get_sta(wt, hdr, len, &to_ap);
  186. switch (WLAN_FC_GET_TYPE(fc)) {
  187. case WLAN_FC_TYPE_MGMT:
  188. if (len < 24)
  189. break;
  190. if (rx_duplicate(wt, hdr, len, sta, to_ap))
  191. break;
  192. rx_update_ps(wt, hdr, len, sta, to_ap);
  193. rx_mgmt(wt, data, len);
  194. break;
  195. case WLAN_FC_TYPE_CTRL:
  196. if (len < 10)
  197. break;
  198. wt->rx_ctrl++;
  199. rx_update_ps(wt, hdr, len, sta, to_ap);
  200. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACK)
  201. rx_ack(wt, hdr);
  202. break;
  203. case WLAN_FC_TYPE_DATA:
  204. if (len < 24)
  205. break;
  206. if (rx_duplicate(wt, hdr, len, sta, to_ap))
  207. break;
  208. rx_update_ps(wt, hdr, len, sta, to_ap);
  209. rx_data(wt, data, len);
  210. break;
  211. default:
  212. wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected type %d",
  213. WLAN_FC_GET_TYPE(fc));
  214. break;
  215. }
  216. os_memcpy(wt->last_hdr, data, len > sizeof(wt->last_hdr) ?
  217. sizeof(wt->last_hdr) : len);
  218. wt->last_len = len;
  219. }
  220. static void tx_status(struct wlantest *wt, const u8 *data, size_t len, int ack)
  221. {
  222. wpa_printf(MSG_DEBUG, "TX status: ack=%d", ack);
  223. wpa_hexdump(MSG_EXCESSIVE, "TX status frame", data, len);
  224. }
  225. static int check_fcs(const u8 *frame, size_t frame_len, const u8 *fcs)
  226. {
  227. if (WPA_GET_LE32(fcs) != crc32(frame, frame_len))
  228. return -1;
  229. return 0;
  230. }
  231. void wlantest_process(struct wlantest *wt, const u8 *data, size_t len)
  232. {
  233. struct ieee80211_radiotap_iterator iter;
  234. int ret;
  235. int rxflags = 0, txflags = 0, failed = 0, fcs = 0;
  236. const u8 *frame, *fcspos;
  237. size_t frame_len;
  238. wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
  239. if (ieee80211_radiotap_iterator_init(&iter, (void *) data, len, NULL)) {
  240. add_note(wt, MSG_INFO, "Invalid radiotap frame");
  241. return;
  242. }
  243. for (;;) {
  244. ret = ieee80211_radiotap_iterator_next(&iter);
  245. wpa_printf(MSG_EXCESSIVE, "radiotap iter: %d "
  246. "this_arg_index=%d", ret, iter.this_arg_index);
  247. if (ret == -ENOENT)
  248. break;
  249. if (ret) {
  250. add_note(wt, MSG_INFO, "Invalid radiotap header: %d",
  251. ret);
  252. return;
  253. }
  254. switch (iter.this_arg_index) {
  255. case IEEE80211_RADIOTAP_FLAGS:
  256. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  257. fcs = 1;
  258. break;
  259. case IEEE80211_RADIOTAP_RX_FLAGS:
  260. rxflags = 1;
  261. break;
  262. case IEEE80211_RADIOTAP_TX_FLAGS:
  263. txflags = 1;
  264. failed = le_to_host16((*(u16 *) iter.this_arg)) &
  265. IEEE80211_RADIOTAP_F_TX_FAIL;
  266. break;
  267. case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
  268. if (WPA_GET_BE24(iter.this_arg) == OUI_QCA &&
  269. iter.this_arg[3] == QCA_RADIOTAP_VID_WLANTEST) {
  270. add_note(wt, MSG_DEBUG,
  271. "Skip frame inserted by wlantest");
  272. return;
  273. }
  274. }
  275. }
  276. frame = data + iter._max_length;
  277. frame_len = len - iter._max_length;
  278. if (fcs && frame_len >= 4) {
  279. frame_len -= 4;
  280. fcspos = frame + frame_len;
  281. if (check_fcs(frame, frame_len, fcspos) < 0) {
  282. add_note(wt, MSG_EXCESSIVE, "Drop RX frame with "
  283. "invalid FCS");
  284. wt->fcs_error++;
  285. return;
  286. }
  287. }
  288. if (rxflags && txflags)
  289. return;
  290. if (!txflags)
  291. rx_frame(wt, frame, frame_len);
  292. else {
  293. add_note(wt, MSG_EXCESSIVE, "TX status - process as RX of "
  294. "local frame");
  295. tx_status(wt, frame, frame_len, !failed);
  296. /* Process as RX frame to support local monitor interface */
  297. rx_frame(wt, frame, frame_len);
  298. }
  299. }
  300. void wlantest_process_prism(struct wlantest *wt, const u8 *data, size_t len)
  301. {
  302. int fcs = 0;
  303. const u8 *frame, *fcspos;
  304. size_t frame_len;
  305. u32 hdrlen;
  306. wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
  307. if (len < 8)
  308. return;
  309. hdrlen = WPA_GET_LE32(data + 4);
  310. if (len < hdrlen) {
  311. wpa_printf(MSG_INFO, "Too short frame to include prism "
  312. "header");
  313. return;
  314. }
  315. frame = data + hdrlen;
  316. frame_len = len - hdrlen;
  317. fcs = 1;
  318. if (fcs && frame_len >= 4) {
  319. frame_len -= 4;
  320. fcspos = frame + frame_len;
  321. if (check_fcs(frame, frame_len, fcspos) < 0) {
  322. add_note(wt, MSG_EXCESSIVE, "Drop RX frame with "
  323. "invalid FCS");
  324. wt->fcs_error++;
  325. return;
  326. }
  327. }
  328. rx_frame(wt, frame, frame_len);
  329. }
  330. void wlantest_process_80211(struct wlantest *wt, const u8 *data, size_t len)
  331. {
  332. wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
  333. if (wt->assume_fcs && len >= 4) {
  334. const u8 *fcspos;
  335. len -= 4;
  336. fcspos = data + len;
  337. if (check_fcs(data, len, fcspos) < 0) {
  338. add_note(wt, MSG_EXCESSIVE, "Drop RX frame with "
  339. "invalid FCS");
  340. wt->fcs_error++;
  341. return;
  342. }
  343. }
  344. rx_frame(wt, data, len);
  345. }