rx_data.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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, *bss2;
  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. }
  255. if (!sta || !sta->ptk_set) {
  256. bss2 = bss_find(wt, hdr->addr2);
  257. if (bss2) {
  258. sta2 = sta_find(bss2, hdr->addr1);
  259. if (sta2 && (!sta || sta2->ptk_set)) {
  260. bss = bss2;
  261. sta = sta2;
  262. }
  263. }
  264. }
  265. } else {
  266. bss = bss_find(wt, hdr->addr2);
  267. if (!bss)
  268. return;
  269. sta = sta_find(bss, hdr->addr1);
  270. }
  271. } else if (fc & WLAN_FC_TODS) {
  272. bss = bss_get(wt, hdr->addr1);
  273. if (bss == NULL)
  274. return;
  275. sta = sta_get(bss, hdr->addr2);
  276. if (sta)
  277. sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
  278. } else if (fc & WLAN_FC_FROMDS) {
  279. bss = bss_get(wt, hdr->addr2);
  280. if (bss == NULL)
  281. return;
  282. sta = sta_get(bss, hdr->addr1);
  283. } else {
  284. bss = bss_get(wt, hdr->addr3);
  285. if (bss == NULL)
  286. return;
  287. sta = sta_find(bss, hdr->addr2);
  288. sta2 = sta_find(bss, hdr->addr1);
  289. if (sta == NULL || sta2 == NULL)
  290. return;
  291. found = NULL;
  292. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
  293. {
  294. if ((tdls->init == sta && tdls->resp == sta2) ||
  295. (tdls->init == sta2 && tdls->resp == sta)) {
  296. found = tdls;
  297. if (tdls->link_up)
  298. break;
  299. }
  300. }
  301. if (found) {
  302. if (!found->link_up)
  303. add_note(wt, MSG_DEBUG,
  304. "TDLS: Link not up, but Data "
  305. "frame seen");
  306. tk = found->tpk.tk;
  307. tdls = found;
  308. }
  309. }
  310. if ((sta == NULL ||
  311. (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) &&
  312. tk == NULL) {
  313. add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
  314. if (dl_list_empty(&wt->ptk))
  315. return;
  316. try_ptk_iter = 1;
  317. }
  318. if (len < 4) {
  319. add_note(wt, MSG_INFO, "Too short encrypted data frame");
  320. return;
  321. }
  322. if (sta == NULL)
  323. return;
  324. if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
  325. !(data[3] & 0x20)) {
  326. add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
  327. MACSTR " did not have ExtIV bit set to 1",
  328. MAC2STR(src));
  329. return;
  330. }
  331. if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) {
  332. if (data[3] & 0x1f) {
  333. add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
  334. " used non-zero reserved bit",
  335. MAC2STR(hdr->addr2));
  336. }
  337. if (data[1] != ((data[0] | 0x20) & 0x7f)) {
  338. add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
  339. " used incorrect WEPSeed[1] (was 0x%x, "
  340. "expected 0x%x)",
  341. MAC2STR(hdr->addr2), data[1],
  342. (data[0] | 0x20) & 0x7f);
  343. }
  344. } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) {
  345. if (data[2] != 0 || (data[3] & 0x1f) != 0) {
  346. add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
  347. " used non-zero reserved bit",
  348. MAC2STR(hdr->addr2));
  349. }
  350. }
  351. keyid = data[3] >> 6;
  352. if (keyid != 0) {
  353. add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in "
  354. "individually addressed Data frame from " MACSTR,
  355. keyid, MAC2STR(hdr->addr2));
  356. }
  357. if (qos) {
  358. tid = qos[0] & 0x0f;
  359. if (fc & WLAN_FC_TODS)
  360. sta->tx_tid[tid]++;
  361. else
  362. sta->rx_tid[tid]++;
  363. } else {
  364. tid = 0;
  365. if (fc & WLAN_FC_TODS)
  366. sta->tx_tid[16]++;
  367. else
  368. sta->rx_tid[16]++;
  369. }
  370. if (tk) {
  371. if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
  372. rsc = tdls->rsc_init[tid];
  373. else
  374. rsc = tdls->rsc_resp[tid];
  375. } else if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  376. (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  377. if (os_memcmp(sta->addr, hdr->addr2, ETH_ALEN) == 0)
  378. rsc = sta->rsc_tods[tid];
  379. else
  380. rsc = sta->rsc_fromds[tid];
  381. } else if (fc & WLAN_FC_TODS)
  382. rsc = sta->rsc_tods[tid];
  383. else
  384. rsc = sta->rsc_fromds[tid];
  385. if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP)
  386. tkip_get_pn(pn, data);
  387. else if (sta->pairwise_cipher == WPA_CIPHER_WEP40)
  388. goto skip_replay_det;
  389. else
  390. ccmp_get_pn(pn, data);
  391. if (os_memcmp(pn, rsc, 6) <= 0) {
  392. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  393. add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  394. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
  395. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  396. MAC2STR(hdr->addr3),
  397. WLAN_GET_SEQ_SEQ(seq_ctrl),
  398. WLAN_GET_SEQ_FRAG(seq_ctrl),
  399. (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
  400. " Retry" : "");
  401. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  402. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  403. replay = 1;
  404. }
  405. skip_replay_det:
  406. if (tk) {
  407. if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
  408. decrypted = ccmp_256_decrypt(tk, hdr, data, len, &dlen);
  409. else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
  410. sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
  411. decrypted = gcmp_decrypt(tk, sta->ptk.tk_len, hdr, data,
  412. len, &dlen);
  413. else
  414. decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen);
  415. } else if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
  416. decrypted = tkip_decrypt(sta->ptk.tk, hdr, data, len, &dlen);
  417. } else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) {
  418. decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
  419. } else if (sta->ptk_set) {
  420. if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
  421. decrypted = ccmp_256_decrypt(sta->ptk.tk, hdr, data,
  422. len, &dlen);
  423. else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
  424. sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
  425. decrypted = gcmp_decrypt(sta->ptk.tk, sta->ptk.tk_len,
  426. hdr, data, len, &dlen);
  427. else
  428. decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data, len,
  429. &dlen);
  430. } else {
  431. decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data,
  432. len, &dlen);
  433. ptk_iter_done = 1;
  434. }
  435. if (!decrypted && !ptk_iter_done) {
  436. decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data,
  437. len, &dlen);
  438. if (decrypted) {
  439. add_note(wt, MSG_DEBUG, "Current PTK did not work, but found a match from all known PTKs");
  440. }
  441. }
  442. if (decrypted) {
  443. u16 fc = le_to_host16(hdr->frame_control);
  444. const u8 *peer_addr = NULL;
  445. if (!(fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)))
  446. peer_addr = hdr->addr1;
  447. if (!replay)
  448. os_memcpy(rsc, pn, 6);
  449. rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted,
  450. dlen, 1, peer_addr);
  451. write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
  452. decrypted, dlen);
  453. } else if (!try_ptk_iter)
  454. add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
  455. os_free(decrypted);
  456. }
  457. static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  458. size_t hdrlen, const u8 *qos, const u8 *dst,
  459. const u8 *src, const u8 *data, size_t len)
  460. {
  461. u16 fc = le_to_host16(hdr->frame_control);
  462. int prot = !!(fc & WLAN_FC_ISWEP);
  463. if (qos) {
  464. u8 ack = (qos[0] & 0x60) >> 5;
  465. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  466. " len=%u%s tid=%u%s%s",
  467. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  468. prot ? " Prot" : "", qos[0] & 0x0f,
  469. (qos[0] & 0x10) ? " EOSP" : "",
  470. ack == 0 ? "" :
  471. (ack == 1 ? " NoAck" :
  472. (ack == 2 ? " NoExpAck" : " BA")));
  473. } else {
  474. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  475. " len=%u%s",
  476. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  477. prot ? " Prot" : "");
  478. }
  479. if (prot)
  480. rx_data_bss_prot(wt, hdr, hdrlen, qos, dst, src, data, len);
  481. else {
  482. const u8 *bssid, *sta_addr, *peer_addr;
  483. struct wlantest_bss *bss;
  484. if (fc & WLAN_FC_TODS) {
  485. bssid = hdr->addr1;
  486. sta_addr = hdr->addr2;
  487. peer_addr = NULL;
  488. } else if (fc & WLAN_FC_FROMDS) {
  489. bssid = hdr->addr2;
  490. sta_addr = hdr->addr1;
  491. peer_addr = NULL;
  492. } else {
  493. bssid = hdr->addr3;
  494. sta_addr = hdr->addr2;
  495. peer_addr = hdr->addr1;
  496. }
  497. bss = bss_get(wt, bssid);
  498. if (bss) {
  499. struct wlantest_sta *sta = sta_get(bss, sta_addr);
  500. if (sta) {
  501. if (qos) {
  502. int tid = qos[0] & 0x0f;
  503. if (fc & WLAN_FC_TODS)
  504. sta->tx_tid[tid]++;
  505. else
  506. sta->rx_tid[tid]++;
  507. } else {
  508. if (fc & WLAN_FC_TODS)
  509. sta->tx_tid[16]++;
  510. else
  511. sta->rx_tid[16]++;
  512. }
  513. }
  514. }
  515. rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0,
  516. peer_addr);
  517. }
  518. }
  519. static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid,
  520. const u8 *sta1_addr,
  521. const u8 *sta2_addr)
  522. {
  523. struct wlantest_bss *bss;
  524. struct wlantest_sta *sta1, *sta2;
  525. struct wlantest_tdls *tdls, *found = NULL;
  526. bss = bss_find(wt, bssid);
  527. if (bss == NULL)
  528. return NULL;
  529. sta1 = sta_find(bss, sta1_addr);
  530. if (sta1 == NULL)
  531. return NULL;
  532. sta2 = sta_find(bss, sta2_addr);
  533. if (sta2 == NULL)
  534. return NULL;
  535. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  536. if ((tdls->init == sta1 && tdls->resp == sta2) ||
  537. (tdls->init == sta2 && tdls->resp == sta1)) {
  538. found = tdls;
  539. if (tdls->link_up)
  540. break;
  541. }
  542. }
  543. return found;
  544. }
  545. static void add_direct_link(struct wlantest *wt, const u8 *bssid,
  546. const u8 *sta1_addr, const u8 *sta2_addr)
  547. {
  548. struct wlantest_tdls *tdls;
  549. tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
  550. if (tdls == NULL)
  551. return;
  552. if (tdls->link_up)
  553. tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++;
  554. else
  555. tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++;
  556. }
  557. static void add_ap_path(struct wlantest *wt, const u8 *bssid,
  558. const u8 *sta1_addr, const u8 *sta2_addr)
  559. {
  560. struct wlantest_tdls *tdls;
  561. tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
  562. if (tdls == NULL)
  563. return;
  564. if (tdls->link_up)
  565. tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++;
  566. else
  567. tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++;
  568. }
  569. void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  570. {
  571. const struct ieee80211_hdr *hdr;
  572. u16 fc, stype;
  573. size_t hdrlen;
  574. const u8 *qos = NULL;
  575. if (len < 24)
  576. return;
  577. hdr = (const struct ieee80211_hdr *) data;
  578. fc = le_to_host16(hdr->frame_control);
  579. stype = WLAN_FC_GET_STYPE(fc);
  580. hdrlen = 24;
  581. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  582. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  583. hdrlen += ETH_ALEN;
  584. if (stype & 0x08) {
  585. qos = data + hdrlen;
  586. hdrlen += 2;
  587. }
  588. if (len < hdrlen)
  589. return;
  590. wt->rx_data++;
  591. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  592. case 0:
  593. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  594. MACSTR " BSSID=" MACSTR,
  595. data_stype(WLAN_FC_GET_STYPE(fc)),
  596. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  597. fc & WLAN_FC_ISWEP ? " Prot" : "",
  598. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  599. MAC2STR(hdr->addr3));
  600. add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2);
  601. rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
  602. data + hdrlen, len - hdrlen);
  603. break;
  604. case WLAN_FC_FROMDS:
  605. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  606. " BSSID=" MACSTR " SA=" MACSTR,
  607. data_stype(WLAN_FC_GET_STYPE(fc)),
  608. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  609. fc & WLAN_FC_ISWEP ? " Prot" : "",
  610. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  611. MAC2STR(hdr->addr3));
  612. add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3);
  613. rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr3,
  614. data + hdrlen, len - hdrlen);
  615. break;
  616. case WLAN_FC_TODS:
  617. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  618. " SA=" MACSTR " DA=" MACSTR,
  619. data_stype(WLAN_FC_GET_STYPE(fc)),
  620. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  621. fc & WLAN_FC_ISWEP ? " Prot" : "",
  622. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  623. MAC2STR(hdr->addr3));
  624. add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2);
  625. rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr3, hdr->addr2,
  626. data + hdrlen, len - hdrlen);
  627. break;
  628. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  629. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  630. MACSTR " DA=" MACSTR " SA=" MACSTR,
  631. data_stype(WLAN_FC_GET_STYPE(fc)),
  632. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  633. fc & WLAN_FC_ISWEP ? " Prot" : "",
  634. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  635. MAC2STR(hdr->addr3),
  636. MAC2STR((const u8 *) (hdr + 1)));
  637. rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
  638. data + hdrlen, len - hdrlen);
  639. break;
  640. }
  641. }