rx_data.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Received Data 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 <linux/if_ether.h>
  16. #include "utils/common.h"
  17. #include "common/defs.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "wlantest.h"
  20. static const char * data_stype(u16 stype)
  21. {
  22. switch (stype) {
  23. case WLAN_FC_STYPE_DATA:
  24. return "DATA";
  25. case WLAN_FC_STYPE_DATA_CFACK:
  26. return "DATA-CFACK";
  27. case WLAN_FC_STYPE_DATA_CFPOLL:
  28. return "DATA-CFPOLL";
  29. case WLAN_FC_STYPE_DATA_CFACKPOLL:
  30. return "DATA-CFACKPOLL";
  31. case WLAN_FC_STYPE_NULLFUNC:
  32. return "NULLFUNC";
  33. case WLAN_FC_STYPE_CFACK:
  34. return "CFACK";
  35. case WLAN_FC_STYPE_CFPOLL:
  36. return "CFPOLL";
  37. case WLAN_FC_STYPE_CFACKPOLL:
  38. return "CFACKPOLL";
  39. case WLAN_FC_STYPE_QOS_DATA:
  40. return "QOSDATA";
  41. case WLAN_FC_STYPE_QOS_DATA_CFACK:
  42. return "QOSDATA-CFACK";
  43. case WLAN_FC_STYPE_QOS_DATA_CFPOLL:
  44. return "QOSDATA-CFPOLL";
  45. case WLAN_FC_STYPE_QOS_DATA_CFACKPOLL:
  46. return "QOSDATA-CFACKPOLL";
  47. case WLAN_FC_STYPE_QOS_NULL:
  48. return "QOS-NULL";
  49. case WLAN_FC_STYPE_QOS_CFPOLL:
  50. return "QOS-CFPOLL";
  51. case WLAN_FC_STYPE_QOS_CFACKPOLL:
  52. return "QOS-CFACKPOLL";
  53. }
  54. return "??";
  55. }
  56. static void rx_data_eth(struct wlantest *wt, const u8 *bssid,
  57. const u8 *sta_addr, const u8 *dst, const u8 *src,
  58. u16 ethertype, const u8 *data, size_t len, int prot)
  59. {
  60. switch (ethertype) {
  61. case ETH_P_PAE:
  62. rx_data_eapol(wt, dst, src, data, len, prot);
  63. break;
  64. case ETH_P_IP:
  65. rx_data_ip(wt, bssid, sta_addr, dst, src, data, len);
  66. break;
  67. case 0x890d:
  68. rx_data_80211_encap(wt, bssid, sta_addr, dst, src, data, len);
  69. break;
  70. }
  71. }
  72. static void rx_data_process(struct wlantest *wt, const u8 *bssid,
  73. const u8 *sta_addr,
  74. const u8 *dst, const u8 *src,
  75. const u8 *data, size_t len, int prot)
  76. {
  77. if (len == 0)
  78. return;
  79. if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
  80. rx_data_eth(wt, bssid, sta_addr, dst, src,
  81. WPA_GET_BE16(data + 6), data + 8, len - 8, prot);
  82. return;
  83. }
  84. wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
  85. }
  86. static void rx_data_bss_prot_group(struct wlantest *wt,
  87. const struct ieee80211_hdr *hdr,
  88. const u8 *qos, const u8 *dst, const u8 *src,
  89. const u8 *data, size_t len)
  90. {
  91. struct wlantest_bss *bss;
  92. int keyid;
  93. u8 *decrypted;
  94. size_t dlen;
  95. u8 pn[6];
  96. bss = bss_get(wt, hdr->addr2);
  97. if (bss == NULL)
  98. return;
  99. if (len < 4) {
  100. wpa_printf(MSG_INFO, "Too short group addressed data frame");
  101. return;
  102. }
  103. if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
  104. !(data[3] & 0x20)) {
  105. wpa_printf(MSG_INFO, "Expected TKIP/CCMP frame from "
  106. MACSTR " did not have ExtIV bit set to 1",
  107. MAC2STR(bss->bssid));
  108. return;
  109. }
  110. if (bss->group_cipher == WPA_CIPHER_TKIP) {
  111. if (data[3] & 0x1f) {
  112. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  113. "non-zero reserved bit",
  114. MAC2STR(bss->bssid));
  115. }
  116. if (data[1] != ((data[0] | 0x20) & 0x7f)) {
  117. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  118. "incorrect WEPSeed[1] (was 0x%x, expected "
  119. "0x%x)",
  120. MAC2STR(bss->bssid), data[1],
  121. (data[0] | 0x20) & 0x7f);
  122. }
  123. } else if (bss->group_cipher == WPA_CIPHER_CCMP) {
  124. if (data[2] != 0 || (data[3] & 0x1f) != 0) {
  125. wpa_printf(MSG_INFO, "CCMP frame from " MACSTR " used "
  126. "non-zero reserved bit",
  127. MAC2STR(bss->bssid));
  128. }
  129. }
  130. keyid = data[3] >> 6;
  131. if (bss->gtk_len[keyid] == 0) {
  132. wpa_printf(MSG_MSGDUMP, "No GTK known to decrypt the frame "
  133. "(A2=" MACSTR " KeyID=%d)",
  134. MAC2STR(hdr->addr2), keyid);
  135. return;
  136. }
  137. if (bss->group_cipher == WPA_CIPHER_TKIP)
  138. tkip_get_pn(pn, data);
  139. else
  140. ccmp_get_pn(pn, data);
  141. if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
  142. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  143. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  144. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  145. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  146. MAC2STR(hdr->addr3),
  147. WLAN_GET_SEQ_SEQ(seq_ctrl),
  148. WLAN_GET_SEQ_FRAG(seq_ctrl));
  149. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  150. wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
  151. }
  152. if (bss->group_cipher == WPA_CIPHER_TKIP)
  153. decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
  154. &dlen);
  155. else
  156. decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
  157. &dlen);
  158. if (decrypted) {
  159. rx_data_process(wt, bss->bssid, NULL, dst, src, decrypted,
  160. dlen, 1);
  161. os_memcpy(bss->rsc[keyid], pn, 6);
  162. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  163. decrypted, dlen);
  164. }
  165. os_free(decrypted);
  166. }
  167. static void rx_data_bss_prot(struct wlantest *wt,
  168. const struct ieee80211_hdr *hdr, const u8 *qos,
  169. const u8 *dst, const u8 *src, const u8 *data,
  170. size_t len)
  171. {
  172. struct wlantest_bss *bss;
  173. struct wlantest_sta *sta, *sta2;
  174. int keyid;
  175. u16 fc = le_to_host16(hdr->frame_control);
  176. u8 *decrypted;
  177. size_t dlen;
  178. int tid;
  179. u8 pn[6], *rsc;
  180. struct wlantest_tdls *tdls = NULL;
  181. const u8 *tk = NULL;
  182. if (hdr->addr1[0] & 0x01) {
  183. rx_data_bss_prot_group(wt, hdr, qos, dst, src, data, len);
  184. return;
  185. }
  186. if (fc & WLAN_FC_TODS) {
  187. bss = bss_get(wt, hdr->addr1);
  188. if (bss == NULL)
  189. return;
  190. sta = sta_get(bss, hdr->addr2);
  191. } else if (fc & WLAN_FC_FROMDS) {
  192. bss = bss_get(wt, hdr->addr2);
  193. if (bss == NULL)
  194. return;
  195. sta = sta_get(bss, hdr->addr1);
  196. } else {
  197. bss = bss_get(wt, hdr->addr3);
  198. if (bss == NULL)
  199. return;
  200. sta = sta_find(bss, hdr->addr2);
  201. sta2 = sta_find(bss, hdr->addr1);
  202. if (sta == NULL || sta2 == NULL)
  203. return;
  204. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
  205. {
  206. if ((tdls->init == sta && tdls->resp == sta2) ||
  207. (tdls->init == sta2 && tdls->resp == sta)) {
  208. if (!tdls->link_up)
  209. wpa_printf(MSG_DEBUG, "TDLS: Link not "
  210. "up, but Data frame seen");
  211. tk = tdls->tpk.tk;
  212. break;
  213. }
  214. }
  215. }
  216. if ((sta == NULL || !sta->ptk_set) && tk == NULL) {
  217. wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
  218. return;
  219. }
  220. if (len < 4) {
  221. wpa_printf(MSG_INFO, "Too short encrypted data frame");
  222. return;
  223. }
  224. if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
  225. !(data[3] & 0x20)) {
  226. wpa_printf(MSG_INFO, "Expected TKIP/CCMP frame from "
  227. MACSTR " did not have ExtIV bit set to 1",
  228. MAC2STR(src));
  229. return;
  230. }
  231. if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) {
  232. if (data[3] & 0x1f) {
  233. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  234. "non-zero reserved bit",
  235. MAC2STR(hdr->addr2));
  236. }
  237. if (data[1] != ((data[0] | 0x20) & 0x7f)) {
  238. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  239. "incorrect WEPSeed[1] (was 0x%x, expected "
  240. "0x%x)",
  241. MAC2STR(hdr->addr2), data[1],
  242. (data[0] | 0x20) & 0x7f);
  243. }
  244. } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) {
  245. if (data[2] != 0 || (data[3] & 0x1f) != 0) {
  246. wpa_printf(MSG_INFO, "CCMP frame from " MACSTR " used "
  247. "non-zero reserved bit",
  248. MAC2STR(hdr->addr2));
  249. }
  250. }
  251. keyid = data[3] >> 6;
  252. if (keyid != 0) {
  253. wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
  254. "individually addressed Data frame from " MACSTR,
  255. keyid, MAC2STR(hdr->addr2));
  256. }
  257. if (qos)
  258. tid = qos[0] & 0x0f;
  259. else
  260. tid = 0;
  261. if (tk) {
  262. if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
  263. rsc = tdls->rsc_init[tid];
  264. else
  265. rsc = tdls->rsc_resp[tid];
  266. } else if (fc & WLAN_FC_TODS)
  267. rsc = sta->rsc_tods[tid];
  268. else
  269. rsc = sta->rsc_fromds[tid];
  270. if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP)
  271. tkip_get_pn(pn, data);
  272. else
  273. ccmp_get_pn(pn, data);
  274. if (os_memcmp(pn, rsc, 6) <= 0) {
  275. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  276. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  277. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  278. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  279. MAC2STR(hdr->addr3),
  280. WLAN_GET_SEQ_SEQ(seq_ctrl),
  281. WLAN_GET_SEQ_FRAG(seq_ctrl));
  282. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  283. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  284. }
  285. if (tk)
  286. decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen);
  287. else if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
  288. decrypted = tkip_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  289. else
  290. decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  291. if (decrypted) {
  292. rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted,
  293. dlen, 1);
  294. os_memcpy(rsc, pn, 6);
  295. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  296. decrypted, dlen);
  297. }
  298. os_free(decrypted);
  299. }
  300. static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  301. const u8 *qos, const u8 *dst, const u8 *src,
  302. const u8 *data, size_t len)
  303. {
  304. u16 fc = le_to_host16(hdr->frame_control);
  305. int prot = !!(fc & WLAN_FC_ISWEP);
  306. if (qos) {
  307. u8 ack = (qos[0] & 0x60) >> 5;
  308. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  309. " len=%u%s tid=%u%s%s",
  310. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  311. prot ? " Prot" : "", qos[0] & 0x0f,
  312. (qos[0] & 0x10) ? " EOSP" : "",
  313. ack == 0 ? "" :
  314. (ack == 1 ? " NoAck" :
  315. (ack == 2 ? " NoExpAck" : " BA")));
  316. } else {
  317. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  318. " len=%u%s",
  319. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  320. prot ? " Prot" : "");
  321. }
  322. if (prot)
  323. rx_data_bss_prot(wt, hdr, qos, dst, src, data, len);
  324. else {
  325. const u8 *bssid, *sta_addr;
  326. if (fc & WLAN_FC_TODS) {
  327. bssid = hdr->addr1;
  328. sta_addr = hdr->addr2;
  329. } else {
  330. bssid = hdr->addr2;
  331. sta_addr = hdr->addr1;
  332. }
  333. rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0);
  334. }
  335. }
  336. static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid,
  337. const u8 *sta1_addr,
  338. const u8 *sta2_addr)
  339. {
  340. struct wlantest_bss *bss;
  341. struct wlantest_sta *sta1, *sta2;
  342. struct wlantest_tdls *tdls;
  343. bss = bss_find(wt, bssid);
  344. if (bss == NULL)
  345. return NULL;
  346. sta1 = sta_find(bss, sta1_addr);
  347. if (sta1 == NULL)
  348. return NULL;
  349. sta2 = sta_find(bss, sta2_addr);
  350. if (sta2 == NULL)
  351. return NULL;
  352. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  353. if ((tdls->init == sta1 && tdls->resp == sta2) ||
  354. (tdls->init == sta2 && tdls->resp == sta1))
  355. return tdls;
  356. }
  357. return NULL;
  358. }
  359. static void add_direct_link(struct wlantest *wt, const u8 *bssid,
  360. const u8 *sta1_addr, const u8 *sta2_addr)
  361. {
  362. struct wlantest_tdls *tdls;
  363. tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
  364. if (tdls == NULL)
  365. return;
  366. if (tdls->link_up)
  367. tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++;
  368. else
  369. tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++;
  370. }
  371. static void add_ap_path(struct wlantest *wt, const u8 *bssid,
  372. const u8 *sta1_addr, const u8 *sta2_addr)
  373. {
  374. struct wlantest_tdls *tdls;
  375. tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
  376. if (tdls == NULL)
  377. return;
  378. if (tdls->link_up)
  379. tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++;
  380. else
  381. tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++;
  382. }
  383. void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  384. {
  385. const struct ieee80211_hdr *hdr;
  386. u16 fc, stype;
  387. size_t hdrlen;
  388. const u8 *qos = NULL;
  389. if (len < 24)
  390. return;
  391. hdr = (const struct ieee80211_hdr *) data;
  392. fc = le_to_host16(hdr->frame_control);
  393. stype = WLAN_FC_GET_STYPE(fc);
  394. hdrlen = 24;
  395. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  396. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  397. hdrlen += ETH_ALEN;
  398. if (stype & 0x08) {
  399. qos = data + hdrlen;
  400. hdrlen += 2;
  401. }
  402. if (len < hdrlen)
  403. return;
  404. wt->rx_data++;
  405. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  406. case 0:
  407. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  408. MACSTR " BSSID=" MACSTR,
  409. data_stype(WLAN_FC_GET_STYPE(fc)),
  410. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  411. fc & WLAN_FC_ISWEP ? " Prot" : "",
  412. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  413. MAC2STR(hdr->addr3));
  414. add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2);
  415. rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
  416. data + hdrlen, len - hdrlen);
  417. break;
  418. case WLAN_FC_FROMDS:
  419. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  420. " BSSID=" MACSTR " SA=" MACSTR,
  421. data_stype(WLAN_FC_GET_STYPE(fc)),
  422. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  423. fc & WLAN_FC_ISWEP ? " Prot" : "",
  424. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  425. MAC2STR(hdr->addr3));
  426. add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3);
  427. rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
  428. data + hdrlen, len - hdrlen);
  429. break;
  430. case WLAN_FC_TODS:
  431. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  432. " SA=" MACSTR " DA=" MACSTR,
  433. data_stype(WLAN_FC_GET_STYPE(fc)),
  434. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  435. fc & WLAN_FC_ISWEP ? " Prot" : "",
  436. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  437. MAC2STR(hdr->addr3));
  438. add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2);
  439. rx_data_bss(wt, hdr, qos, hdr->addr3, hdr->addr2,
  440. data + hdrlen, len - hdrlen);
  441. break;
  442. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  443. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  444. MACSTR " DA=" MACSTR " SA=" MACSTR,
  445. data_stype(WLAN_FC_GET_STYPE(fc)),
  446. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  447. fc & WLAN_FC_ISWEP ? " Prot" : "",
  448. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  449. MAC2STR(hdr->addr3),
  450. MAC2STR((const u8 *) (hdr + 1)));
  451. break;
  452. }
  453. }