process.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "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(struct wlantest *wt, const u8 *data, size_t len)
  51. {
  52. const struct ieee80211_hdr *hdr;
  53. u16 fc, stype;
  54. if (len < 24)
  55. return;
  56. hdr = (const struct ieee80211_hdr *) data;
  57. fc = le_to_host16(hdr->frame_control);
  58. wt->rx_mgmt++;
  59. stype = WLAN_FC_GET_STYPE(fc);
  60. wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
  61. stype == WLAN_FC_STYPE_PROBE_RESP ||
  62. stype == WLAN_FC_STYPE_PROBE_REQ) ?
  63. MSG_EXCESSIVE : MSG_MSGDUMP,
  64. "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
  65. mgmt_stype(stype),
  66. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  67. fc & WLAN_FC_ISWEP ? " Prot" : "",
  68. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  69. MAC2STR(hdr->addr3));
  70. }
  71. static const char * data_stype(u16 stype)
  72. {
  73. switch (stype) {
  74. case WLAN_FC_STYPE_DATA:
  75. return "DATA";
  76. case WLAN_FC_STYPE_DATA_CFACK:
  77. return "DATA-CFACK";
  78. case WLAN_FC_STYPE_DATA_CFPOLL:
  79. return "DATA-CFPOLL";
  80. case WLAN_FC_STYPE_DATA_CFACKPOLL:
  81. return "DATA-CFACKPOLL";
  82. case WLAN_FC_STYPE_NULLFUNC:
  83. return "NULLFUNC";
  84. case WLAN_FC_STYPE_CFACK:
  85. return "CFACK";
  86. case WLAN_FC_STYPE_CFPOLL:
  87. return "CFPOLL";
  88. case WLAN_FC_STYPE_CFACKPOLL:
  89. return "CFACKPOLL";
  90. case WLAN_FC_STYPE_QOS_DATA:
  91. return "QOSDATA";
  92. case WLAN_FC_STYPE_QOS_DATA_CFACK:
  93. return "QOSDATA-CFACK";
  94. case WLAN_FC_STYPE_QOS_DATA_CFPOLL:
  95. return "QOSDATA-CFPOLL";
  96. case WLAN_FC_STYPE_QOS_DATA_CFACKPOLL:
  97. return "QOSDATA-CFACKPOLL";
  98. case WLAN_FC_STYPE_QOS_NULL:
  99. return "QOS-NULL";
  100. case WLAN_FC_STYPE_QOS_CFPOLL:
  101. return "QOS-CFPOLL";
  102. case WLAN_FC_STYPE_QOS_CFACKPOLL:
  103. return "QOS-CFACKPOLL";
  104. }
  105. return "??";
  106. }
  107. static void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  108. {
  109. const struct ieee80211_hdr *hdr;
  110. u16 fc;
  111. if (len < 24)
  112. return;
  113. hdr = (const struct ieee80211_hdr *) data;
  114. fc = le_to_host16(hdr->frame_control);
  115. wt->rx_data++;
  116. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  117. case 0:
  118. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  119. MACSTR " BSSID=" MACSTR,
  120. data_stype(WLAN_FC_GET_STYPE(fc)),
  121. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  122. fc & WLAN_FC_ISWEP ? " Prot" : "",
  123. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  124. MAC2STR(hdr->addr3));
  125. break;
  126. case WLAN_FC_FROMDS:
  127. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  128. " BSSID=" MACSTR " SA=" MACSTR,
  129. data_stype(WLAN_FC_GET_STYPE(fc)),
  130. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  131. fc & WLAN_FC_ISWEP ? " Prot" : "",
  132. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  133. MAC2STR(hdr->addr3));
  134. break;
  135. case WLAN_FC_TODS:
  136. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  137. " SA=" MACSTR " DA=" MACSTR,
  138. data_stype(WLAN_FC_GET_STYPE(fc)),
  139. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  140. fc & WLAN_FC_ISWEP ? " Prot" : "",
  141. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  142. MAC2STR(hdr->addr3));
  143. break;
  144. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  145. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  146. MACSTR " DA=" MACSTR " SA=" MACSTR,
  147. data_stype(WLAN_FC_GET_STYPE(fc)),
  148. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  149. fc & WLAN_FC_ISWEP ? " Prot" : "",
  150. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  151. MAC2STR(hdr->addr3),
  152. MAC2STR((const u8 *) (hdr + 1)));
  153. break;
  154. }
  155. }
  156. static void rx_frame(struct wlantest *wt, const u8 *data, size_t len)
  157. {
  158. const struct ieee80211_hdr *hdr;
  159. u16 fc;
  160. wpa_hexdump(MSG_EXCESSIVE, "RX frame", data, len);
  161. if (len < 2)
  162. return;
  163. hdr = (const struct ieee80211_hdr *) data;
  164. fc = le_to_host16(hdr->frame_control);
  165. if (fc & WLAN_FC_PVER) {
  166. wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected pver=%d",
  167. fc & WLAN_FC_PVER);
  168. return;
  169. }
  170. switch (WLAN_FC_GET_TYPE(fc)) {
  171. case WLAN_FC_TYPE_MGMT:
  172. rx_mgmt(wt, data, len);
  173. break;
  174. case WLAN_FC_TYPE_CTRL:
  175. if (len < 10)
  176. return;
  177. wt->rx_ctrl++;
  178. break;
  179. case WLAN_FC_TYPE_DATA:
  180. rx_data(wt, data, len);
  181. break;
  182. default:
  183. wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected type %d",
  184. WLAN_FC_GET_TYPE(fc));
  185. break;
  186. }
  187. }
  188. static void tx_status(struct wlantest *wt, const u8 *data, size_t len, int ack)
  189. {
  190. wpa_printf(MSG_DEBUG, "TX status: ack=%d", ack);
  191. wpa_hexdump(MSG_EXCESSIVE, "TX status frame", data, len);
  192. }
  193. static int check_fcs(const u8 *frame, size_t frame_len, const u8 *fcs)
  194. {
  195. if (WPA_GET_LE32(fcs) != crc32(frame, frame_len))
  196. return -1;
  197. return 0;
  198. }
  199. void wlantest_process(struct wlantest *wt, const u8 *data, size_t len)
  200. {
  201. struct ieee80211_radiotap_iterator iter;
  202. int ret;
  203. int rxflags = 0, txflags = 0, failed = 0, fcs = 0;
  204. const u8 *frame, *fcspos;
  205. size_t frame_len;
  206. wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
  207. if (ieee80211_radiotap_iterator_init(&iter, (void *) data, len)) {
  208. wpa_printf(MSG_INFO, "Invalid radiotap frame");
  209. return;
  210. }
  211. for (;;) {
  212. ret = ieee80211_radiotap_iterator_next(&iter);
  213. wpa_printf(MSG_EXCESSIVE, "radiotap iter: %d "
  214. "this_arg_index=%d", ret, iter.this_arg_index);
  215. if (ret == -ENOENT)
  216. break;
  217. if (ret) {
  218. wpa_printf(MSG_INFO, "Invalid radiotap header: %d",
  219. ret);
  220. return;
  221. }
  222. switch (iter.this_arg_index) {
  223. case IEEE80211_RADIOTAP_FLAGS:
  224. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  225. fcs = 1;
  226. break;
  227. case IEEE80211_RADIOTAP_RX_FLAGS:
  228. rxflags = 1;
  229. break;
  230. case IEEE80211_RADIOTAP_TX_FLAGS:
  231. txflags = 1;
  232. failed = le_to_host16((*(u16 *) iter.this_arg)) &
  233. IEEE80211_RADIOTAP_F_TX_FAIL;
  234. break;
  235. }
  236. }
  237. frame = data + iter.max_length;
  238. frame_len = len - iter.max_length;
  239. if (fcs && frame_len >= 4) {
  240. frame_len -= 4;
  241. fcspos = frame + frame_len;
  242. if (check_fcs(frame, frame_len, fcspos) < 0) {
  243. wpa_printf(MSG_EXCESSIVE, "Drop RX frame with invalid "
  244. "FCS");
  245. wt->fcs_error++;
  246. return;
  247. }
  248. }
  249. if (rxflags && txflags)
  250. return;
  251. if (!txflags)
  252. rx_frame(wt, frame, frame_len);
  253. else
  254. tx_status(wt, frame, frame_len, !failed);
  255. }