rx_data.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Received Data frame processing
  3. * Copyright (c) 2010-2015, 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 "common/defs.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "wlantest.h"
  13. static const char * data_stype(u16 stype)
  14. {
  15. switch (stype) {
  16. case WLAN_FC_STYPE_DATA:
  17. return "DATA";
  18. case WLAN_FC_STYPE_DATA_CFACK:
  19. return "DATA-CFACK";
  20. case WLAN_FC_STYPE_DATA_CFPOLL:
  21. return "DATA-CFPOLL";
  22. case WLAN_FC_STYPE_DATA_CFACKPOLL:
  23. return "DATA-CFACKPOLL";
  24. case WLAN_FC_STYPE_NULLFUNC:
  25. return "NULLFUNC";
  26. case WLAN_FC_STYPE_CFACK:
  27. return "CFACK";
  28. case WLAN_FC_STYPE_CFPOLL:
  29. return "CFPOLL";
  30. case WLAN_FC_STYPE_CFACKPOLL:
  31. return "CFACKPOLL";
  32. case WLAN_FC_STYPE_QOS_DATA:
  33. return "QOSDATA";
  34. case WLAN_FC_STYPE_QOS_DATA_CFACK:
  35. return "QOSDATA-CFACK";
  36. case WLAN_FC_STYPE_QOS_DATA_CFPOLL:
  37. return "QOSDATA-CFPOLL";
  38. case WLAN_FC_STYPE_QOS_DATA_CFACKPOLL:
  39. return "QOSDATA-CFACKPOLL";
  40. case WLAN_FC_STYPE_QOS_NULL:
  41. return "QOS-NULL";
  42. case WLAN_FC_STYPE_QOS_CFPOLL:
  43. return "QOS-CFPOLL";
  44. case WLAN_FC_STYPE_QOS_CFACKPOLL:
  45. return "QOS-CFACKPOLL";
  46. }
  47. return "??";
  48. }
  49. static void rx_data_eth(struct wlantest *wt, const u8 *bssid,
  50. const u8 *sta_addr, const u8 *dst, const u8 *src,
  51. u16 ethertype, const u8 *data, size_t len, int prot,
  52. const u8 *peer_addr)
  53. {
  54. switch (ethertype) {
  55. case ETH_P_PAE:
  56. rx_data_eapol(wt, bssid, sta_addr, dst, src, data, len, prot);
  57. break;
  58. case ETH_P_IP:
  59. rx_data_ip(wt, bssid, sta_addr, dst, src, data, len,
  60. peer_addr);
  61. break;
  62. case 0x890d:
  63. rx_data_80211_encap(wt, bssid, sta_addr, dst, src, data, len);
  64. break;
  65. }
  66. }
  67. static void rx_data_process(struct wlantest *wt, const u8 *bssid,
  68. const u8 *sta_addr,
  69. const u8 *dst, const u8 *src,
  70. const u8 *data, size_t len, int prot,
  71. const u8 *peer_addr)
  72. {
  73. if (len == 0)
  74. return;
  75. if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
  76. rx_data_eth(wt, bssid, sta_addr, dst, src,
  77. WPA_GET_BE16(data + 6), data + 8, len - 8, prot,
  78. peer_addr);
  79. return;
  80. }
  81. wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
  82. }
  83. static u8 * try_all_ptk(struct wlantest *wt, int pairwise_cipher,
  84. const struct ieee80211_hdr *hdr,
  85. const u8 *data, size_t data_len, size_t *decrypted_len)
  86. {
  87. struct wlantest_ptk *ptk;
  88. u8 *decrypted;
  89. int prev_level = wpa_debug_level;
  90. wpa_debug_level = MSG_WARNING;
  91. dl_list_for_each(ptk, &wt->ptk, struct wlantest_ptk, list) {
  92. unsigned int tk_len = ptk->ptk_len - 32;
  93. decrypted = NULL;
  94. if ((pairwise_cipher == WPA_CIPHER_CCMP ||
  95. pairwise_cipher == 0) && tk_len == 16) {
  96. decrypted = ccmp_decrypt(ptk->ptk.tk, hdr, data,
  97. data_len, decrypted_len);
  98. } else if ((pairwise_cipher == WPA_CIPHER_CCMP_256 ||
  99. pairwise_cipher == 0) && tk_len == 32) {
  100. decrypted = ccmp_256_decrypt(ptk->ptk.tk, hdr, data,
  101. data_len, decrypted_len);
  102. } else if ((pairwise_cipher == WPA_CIPHER_GCMP ||
  103. pairwise_cipher == WPA_CIPHER_GCMP_256 ||
  104. pairwise_cipher == 0) &&
  105. (tk_len == 16 || tk_len == 32)) {
  106. decrypted = gcmp_decrypt(ptk->ptk.tk, tk_len, hdr,
  107. data, data_len, decrypted_len);
  108. } else if ((pairwise_cipher == WPA_CIPHER_TKIP ||
  109. pairwise_cipher == 0) && tk_len == 32) {
  110. decrypted = tkip_decrypt(ptk->ptk.tk, hdr, data,
  111. data_len, decrypted_len);
  112. }
  113. if (decrypted) {
  114. wpa_debug_level = prev_level;
  115. add_note(wt, MSG_DEBUG, "Found PTK match from list of all known PTKs");
  116. return decrypted;
  117. }
  118. }
  119. wpa_debug_level = prev_level;
  120. return NULL;
  121. }
  122. static void rx_data_bss_prot_group(struct wlantest *wt,
  123. const struct ieee80211_hdr *hdr,
  124. size_t hdrlen,
  125. const u8 *qos, const u8 *dst, const u8 *src,
  126. const u8 *data, size_t len)
  127. {
  128. struct wlantest_bss *bss;
  129. int keyid;
  130. u8 *decrypted = NULL;
  131. size_t dlen;
  132. u8 pn[6];
  133. int replay = 0;
  134. bss = bss_get(wt, hdr->addr2);
  135. if (bss == NULL)
  136. return;
  137. if (len < 4) {
  138. add_note(wt, MSG_INFO, "Too short group addressed data frame");
  139. return;
  140. }
  141. if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
  142. !(data[3] & 0x20)) {
  143. add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
  144. MACSTR " did not have ExtIV bit set to 1",
  145. MAC2STR(bss->bssid));
  146. return;
  147. }
  148. if (bss->group_cipher == WPA_CIPHER_TKIP) {
  149. if (data[3] & 0x1f) {
  150. add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
  151. " used non-zero reserved bit",
  152. MAC2STR(bss->bssid));
  153. }
  154. if (data[1] != ((data[0] | 0x20) & 0x7f)) {
  155. add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
  156. " used incorrect WEPSeed[1] (was 0x%x, "
  157. "expected 0x%x)",
  158. MAC2STR(bss->bssid), data[1],
  159. (data[0] | 0x20) & 0x7f);
  160. }
  161. } else if (bss->group_cipher == WPA_CIPHER_CCMP) {
  162. if (data[2] != 0 || (data[3] & 0x1f) != 0) {
  163. add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
  164. " used non-zero reserved bit",
  165. MAC2STR(bss->bssid));
  166. }
  167. }
  168. keyid = data[3] >> 6;
  169. if (bss->gtk_len[keyid] == 0 && bss->group_cipher != WPA_CIPHER_WEP40)
  170. {
  171. add_note(wt, MSG_MSGDUMP, "No GTK known to decrypt the frame "
  172. "(A2=" MACSTR " KeyID=%d)",
  173. MAC2STR(hdr->addr2), keyid);
  174. return;
  175. }
  176. if (bss->group_cipher == WPA_CIPHER_TKIP)
  177. tkip_get_pn(pn, data);
  178. else if (bss->group_cipher == WPA_CIPHER_WEP40)
  179. goto skip_replay_det;
  180. else
  181. ccmp_get_pn(pn, data);
  182. if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
  183. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  184. add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  185. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
  186. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  187. MAC2STR(hdr->addr3),
  188. WLAN_GET_SEQ_SEQ(seq_ctrl),
  189. WLAN_GET_SEQ_FRAG(seq_ctrl),
  190. (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
  191. " Retry" : "");
  192. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  193. wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
  194. replay = 1;
  195. }
  196. skip_replay_det:
  197. if (bss->group_cipher == WPA_CIPHER_TKIP)
  198. decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
  199. &dlen);
  200. else if (bss->group_cipher == WPA_CIPHER_WEP40)
  201. decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
  202. else if (bss->group_cipher == WPA_CIPHER_CCMP)
  203. decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
  204. &dlen);
  205. else if (bss->group_cipher == WPA_CIPHER_CCMP_256)
  206. decrypted = ccmp_256_decrypt(bss->gtk[keyid], hdr, data, len,
  207. &dlen);
  208. else if (bss->group_cipher == WPA_CIPHER_GCMP ||
  209. bss->group_cipher == WPA_CIPHER_GCMP_256)
  210. decrypted = gcmp_decrypt(bss->gtk[keyid], bss->gtk_len[keyid],
  211. hdr, data, len, &dlen);
  212. if (decrypted) {
  213. rx_data_process(wt, bss->bssid, NULL, dst, src, decrypted,
  214. dlen, 1, NULL);
  215. if (!replay)
  216. os_memcpy(bss->rsc[keyid], pn, 6);
  217. write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
  218. decrypted, dlen);
  219. } else
  220. add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
  221. os_free(decrypted);
  222. }
  223. static void rx_data_bss_prot(struct wlantest *wt,
  224. const struct ieee80211_hdr *hdr, size_t hdrlen,
  225. const u8 *qos, const u8 *dst, const u8 *src,
  226. const u8 *data, size_t len)
  227. {
  228. struct wlantest_bss *bss;
  229. struct wlantest_sta *sta, *sta2;
  230. int keyid;
  231. u16 fc = le_to_host16(hdr->frame_control);
  232. u8 *decrypted;
  233. size_t dlen;
  234. int tid;
  235. u8 pn[6], *rsc;
  236. struct wlantest_tdls *tdls = NULL, *found;
  237. const u8 *tk = NULL;
  238. int ptk_iter_done = 0;
  239. int try_ptk_iter = 0;
  240. int replay = 0;
  241. if (hdr->addr1[0] & 0x01) {
  242. rx_data_bss_prot_group(wt, hdr, hdrlen, qos, dst, src,
  243. data, len);
  244. return;
  245. }
  246. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  247. (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  248. bss = bss_find(wt, hdr->addr1);
  249. if (bss) {
  250. sta = sta_find(bss, hdr->addr2);
  251. if (sta)
  252. sta->counters[
  253. WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
  254. } else {
  255. bss = bss_find(wt, hdr->addr2);
  256. if (!bss)
  257. return;
  258. sta = sta_find(bss, hdr->addr1);
  259. }
  260. } else if (fc & WLAN_FC_TODS) {
  261. bss = bss_get(wt, hdr->addr1);
  262. if (bss == NULL)
  263. return;
  264. sta = sta_get(bss, hdr->addr2);
  265. if (sta)
  266. sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
  267. } else if (fc & WLAN_FC_FROMDS) {
  268. bss = bss_get(wt, hdr->addr2);
  269. if (bss == NULL)
  270. return;
  271. sta = sta_get(bss, hdr->addr1);
  272. } else {
  273. bss = bss_get(wt, hdr->addr3);
  274. if (bss == NULL)
  275. return;
  276. sta = sta_find(bss, hdr->addr2);
  277. sta2 = sta_find(bss, hdr->addr1);
  278. if (sta == NULL || sta2 == NULL)
  279. return;
  280. found = NULL;
  281. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
  282. {
  283. if ((tdls->init == sta && tdls->resp == sta2) ||
  284. (tdls->init == sta2 && tdls->resp == sta)) {
  285. found = tdls;
  286. if (tdls->link_up)
  287. break;
  288. }
  289. }
  290. if (found) {
  291. if (!found->link_up)
  292. add_note(wt, MSG_DEBUG,
  293. "TDLS: Link not up, but Data "
  294. "frame seen");
  295. tk = found->tpk.tk;
  296. tdls = found;
  297. }
  298. }
  299. if ((sta == NULL ||
  300. (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) &&
  301. tk == NULL) {
  302. add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
  303. if (dl_list_empty(&wt->ptk))
  304. return;
  305. try_ptk_iter = 1;
  306. }
  307. if (len < 4) {
  308. add_note(wt, MSG_INFO, "Too short encrypted data frame");
  309. return;
  310. }
  311. if (sta == NULL)
  312. return;
  313. if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
  314. !(data[3] & 0x20)) {
  315. add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
  316. MACSTR " did not have ExtIV bit set to 1",
  317. MAC2STR(src));
  318. return;
  319. }
  320. if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) {
  321. if (data[3] & 0x1f) {
  322. add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
  323. " used non-zero reserved bit",
  324. MAC2STR(hdr->addr2));
  325. }
  326. if (data[1] != ((data[0] | 0x20) & 0x7f)) {
  327. add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
  328. " used incorrect WEPSeed[1] (was 0x%x, "
  329. "expected 0x%x)",
  330. MAC2STR(hdr->addr2), data[1],
  331. (data[0] | 0x20) & 0x7f);
  332. }
  333. } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) {
  334. if (data[2] != 0 || (data[3] & 0x1f) != 0) {
  335. add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
  336. " used non-zero reserved bit",
  337. MAC2STR(hdr->addr2));
  338. }
  339. }
  340. keyid = data[3] >> 6;
  341. if (keyid != 0) {
  342. add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in "
  343. "individually addressed Data frame from " MACSTR,
  344. keyid, MAC2STR(hdr->addr2));
  345. }
  346. if (qos) {
  347. tid = qos[0] & 0x0f;
  348. if (fc & WLAN_FC_TODS)
  349. sta->tx_tid[tid]++;
  350. else
  351. sta->rx_tid[tid]++;
  352. } else {
  353. tid = 0;
  354. if (fc & WLAN_FC_TODS)
  355. sta->tx_tid[16]++;
  356. else
  357. sta->rx_tid[16]++;
  358. }
  359. if (tk) {
  360. if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
  361. rsc = tdls->rsc_init[tid];
  362. else
  363. rsc = tdls->rsc_resp[tid];
  364. } else if (fc & WLAN_FC_TODS)
  365. rsc = sta->rsc_tods[tid];
  366. else
  367. rsc = sta->rsc_fromds[tid];
  368. if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP)
  369. tkip_get_pn(pn, data);
  370. else if (sta->pairwise_cipher == WPA_CIPHER_WEP40)
  371. goto skip_replay_det;
  372. else
  373. ccmp_get_pn(pn, data);
  374. if (os_memcmp(pn, rsc, 6) <= 0) {
  375. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  376. add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  377. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
  378. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  379. MAC2STR(hdr->addr3),
  380. WLAN_GET_SEQ_SEQ(seq_ctrl),
  381. WLAN_GET_SEQ_FRAG(seq_ctrl),
  382. (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
  383. " Retry" : "");
  384. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  385. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  386. replay = 1;
  387. }
  388. skip_replay_det:
  389. if (tk) {
  390. if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
  391. decrypted = ccmp_256_decrypt(tk, hdr, data, len, &dlen);
  392. else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
  393. sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
  394. decrypted = gcmp_decrypt(tk, sta->tk_len, hdr, data,
  395. len, &dlen);
  396. else
  397. decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen);
  398. } else if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
  399. decrypted = tkip_decrypt(sta->ptk.tk, hdr, data, len, &dlen);
  400. } else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) {
  401. decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
  402. } else if (sta->ptk_set) {
  403. if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
  404. decrypted = ccmp_256_decrypt(sta->ptk.tk, hdr, data,
  405. len, &dlen);
  406. else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
  407. sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
  408. decrypted = gcmp_decrypt(sta->ptk.tk, sta->tk_len,
  409. hdr, data, len, &dlen);
  410. else
  411. decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data, len,
  412. &dlen);
  413. } else {
  414. decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data,
  415. len, &dlen);
  416. ptk_iter_done = 1;
  417. }
  418. if (!decrypted && !ptk_iter_done) {
  419. decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data,
  420. len, &dlen);
  421. if (decrypted) {
  422. add_note(wt, MSG_DEBUG, "Current PTK did not work, but found a match from all known PTKs");
  423. }
  424. }
  425. if (decrypted) {
  426. u16 fc = le_to_host16(hdr->frame_control);
  427. const u8 *peer_addr = NULL;
  428. if (!(fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)))
  429. peer_addr = hdr->addr1;
  430. if (!replay)
  431. os_memcpy(rsc, pn, 6);
  432. rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted,
  433. dlen, 1, peer_addr);
  434. write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
  435. decrypted, dlen);
  436. } else if (!try_ptk_iter)
  437. add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
  438. os_free(decrypted);
  439. }
  440. static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  441. size_t hdrlen, const u8 *qos, const u8 *dst,
  442. const u8 *src, const u8 *data, size_t len)
  443. {
  444. u16 fc = le_to_host16(hdr->frame_control);
  445. int prot = !!(fc & WLAN_FC_ISWEP);
  446. if (qos) {
  447. u8 ack = (qos[0] & 0x60) >> 5;
  448. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  449. " len=%u%s tid=%u%s%s",
  450. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  451. prot ? " Prot" : "", qos[0] & 0x0f,
  452. (qos[0] & 0x10) ? " EOSP" : "",
  453. ack == 0 ? "" :
  454. (ack == 1 ? " NoAck" :
  455. (ack == 2 ? " NoExpAck" : " BA")));
  456. } else {
  457. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  458. " len=%u%s",
  459. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  460. prot ? " Prot" : "");
  461. }
  462. if (prot)
  463. rx_data_bss_prot(wt, hdr, hdrlen, qos, dst, src, data, len);
  464. else {
  465. const u8 *bssid, *sta_addr, *peer_addr;
  466. struct wlantest_bss *bss;
  467. if (fc & WLAN_FC_TODS) {
  468. bssid = hdr->addr1;
  469. sta_addr = hdr->addr2;
  470. peer_addr = NULL;
  471. } else if (fc & WLAN_FC_FROMDS) {
  472. bssid = hdr->addr2;
  473. sta_addr = hdr->addr1;
  474. peer_addr = NULL;
  475. } else {
  476. bssid = hdr->addr3;
  477. sta_addr = hdr->addr2;
  478. peer_addr = hdr->addr1;
  479. }
  480. bss = bss_get(wt, bssid);
  481. if (bss) {
  482. struct wlantest_sta *sta = sta_get(bss, sta_addr);
  483. if (sta) {
  484. if (qos) {
  485. int tid = qos[0] & 0x0f;
  486. if (fc & WLAN_FC_TODS)
  487. sta->tx_tid[tid]++;
  488. else
  489. sta->rx_tid[tid]++;
  490. } else {
  491. if (fc & WLAN_FC_TODS)
  492. sta->tx_tid[16]++;
  493. else
  494. sta->rx_tid[16]++;
  495. }
  496. }
  497. }
  498. rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0,
  499. peer_addr);
  500. }
  501. }
  502. static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid,
  503. const u8 *sta1_addr,
  504. const u8 *sta2_addr)
  505. {
  506. struct wlantest_bss *bss;
  507. struct wlantest_sta *sta1, *sta2;
  508. struct wlantest_tdls *tdls, *found = NULL;
  509. bss = bss_find(wt, bssid);
  510. if (bss == NULL)
  511. return NULL;
  512. sta1 = sta_find(bss, sta1_addr);
  513. if (sta1 == NULL)
  514. return NULL;
  515. sta2 = sta_find(bss, sta2_addr);
  516. if (sta2 == NULL)
  517. return NULL;
  518. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  519. if ((tdls->init == sta1 && tdls->resp == sta2) ||
  520. (tdls->init == sta2 && tdls->resp == sta1)) {
  521. found = tdls;
  522. if (tdls->link_up)
  523. break;
  524. }
  525. }
  526. return found;
  527. }
  528. static void add_direct_link(struct wlantest *wt, const u8 *bssid,
  529. const u8 *sta1_addr, const u8 *sta2_addr)
  530. {
  531. struct wlantest_tdls *tdls;
  532. tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
  533. if (tdls == NULL)
  534. return;
  535. if (tdls->link_up)
  536. tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++;
  537. else
  538. tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++;
  539. }
  540. static void add_ap_path(struct wlantest *wt, const u8 *bssid,
  541. const u8 *sta1_addr, const u8 *sta2_addr)
  542. {
  543. struct wlantest_tdls *tdls;
  544. tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
  545. if (tdls == NULL)
  546. return;
  547. if (tdls->link_up)
  548. tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++;
  549. else
  550. tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++;
  551. }
  552. void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  553. {
  554. const struct ieee80211_hdr *hdr;
  555. u16 fc, stype;
  556. size_t hdrlen;
  557. const u8 *qos = NULL;
  558. if (len < 24)
  559. return;
  560. hdr = (const struct ieee80211_hdr *) data;
  561. fc = le_to_host16(hdr->frame_control);
  562. stype = WLAN_FC_GET_STYPE(fc);
  563. hdrlen = 24;
  564. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  565. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  566. hdrlen += ETH_ALEN;
  567. if (stype & 0x08) {
  568. qos = data + hdrlen;
  569. hdrlen += 2;
  570. }
  571. if (len < hdrlen)
  572. return;
  573. wt->rx_data++;
  574. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  575. case 0:
  576. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  577. MACSTR " BSSID=" MACSTR,
  578. data_stype(WLAN_FC_GET_STYPE(fc)),
  579. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  580. fc & WLAN_FC_ISWEP ? " Prot" : "",
  581. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  582. MAC2STR(hdr->addr3));
  583. add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2);
  584. rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
  585. data + hdrlen, len - hdrlen);
  586. break;
  587. case WLAN_FC_FROMDS:
  588. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  589. " BSSID=" MACSTR " SA=" MACSTR,
  590. data_stype(WLAN_FC_GET_STYPE(fc)),
  591. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  592. fc & WLAN_FC_ISWEP ? " Prot" : "",
  593. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  594. MAC2STR(hdr->addr3));
  595. add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3);
  596. rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr3,
  597. data + hdrlen, len - hdrlen);
  598. break;
  599. case WLAN_FC_TODS:
  600. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  601. " SA=" MACSTR " DA=" MACSTR,
  602. data_stype(WLAN_FC_GET_STYPE(fc)),
  603. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  604. fc & WLAN_FC_ISWEP ? " Prot" : "",
  605. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  606. MAC2STR(hdr->addr3));
  607. add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2);
  608. rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr3, hdr->addr2,
  609. data + hdrlen, len - hdrlen);
  610. break;
  611. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  612. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  613. MACSTR " DA=" MACSTR " SA=" MACSTR,
  614. data_stype(WLAN_FC_GET_STYPE(fc)),
  615. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  616. fc & WLAN_FC_ISWEP ? " Prot" : "",
  617. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  618. MAC2STR(hdr->addr3),
  619. MAC2STR((const u8 *) (hdr + 1)));
  620. rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
  621. data + hdrlen, len - hdrlen);
  622. break;
  623. }
  624. }