rx_data.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 && bss->group_cipher != WPA_CIPHER_WEP40)
  132. {
  133. wpa_printf(MSG_MSGDUMP, "No GTK known to decrypt the frame "
  134. "(A2=" MACSTR " KeyID=%d)",
  135. MAC2STR(hdr->addr2), keyid);
  136. return;
  137. }
  138. if (bss->group_cipher == WPA_CIPHER_TKIP)
  139. tkip_get_pn(pn, data);
  140. else if (bss->group_cipher == WPA_CIPHER_WEP40)
  141. goto skip_replay_det;
  142. else
  143. ccmp_get_pn(pn, data);
  144. if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
  145. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  146. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  147. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  148. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  149. MAC2STR(hdr->addr3),
  150. WLAN_GET_SEQ_SEQ(seq_ctrl),
  151. WLAN_GET_SEQ_FRAG(seq_ctrl));
  152. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  153. wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
  154. }
  155. skip_replay_det:
  156. if (bss->group_cipher == WPA_CIPHER_TKIP)
  157. decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
  158. &dlen);
  159. else if (bss->group_cipher == WPA_CIPHER_WEP40)
  160. decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
  161. else
  162. decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
  163. &dlen);
  164. if (decrypted) {
  165. rx_data_process(wt, bss->bssid, NULL, dst, src, decrypted,
  166. dlen, 1);
  167. os_memcpy(bss->rsc[keyid], pn, 6);
  168. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  169. decrypted, dlen);
  170. }
  171. os_free(decrypted);
  172. }
  173. static void rx_data_bss_prot(struct wlantest *wt,
  174. const struct ieee80211_hdr *hdr, const u8 *qos,
  175. const u8 *dst, const u8 *src, const u8 *data,
  176. size_t len)
  177. {
  178. struct wlantest_bss *bss;
  179. struct wlantest_sta *sta, *sta2;
  180. int keyid;
  181. u16 fc = le_to_host16(hdr->frame_control);
  182. u8 *decrypted;
  183. size_t dlen;
  184. int tid;
  185. u8 pn[6], *rsc;
  186. struct wlantest_tdls *tdls = NULL;
  187. const u8 *tk = NULL;
  188. if (hdr->addr1[0] & 0x01) {
  189. rx_data_bss_prot_group(wt, hdr, qos, dst, src, data, len);
  190. return;
  191. }
  192. if (fc & WLAN_FC_TODS) {
  193. bss = bss_get(wt, hdr->addr1);
  194. if (bss == NULL)
  195. return;
  196. sta = sta_get(bss, hdr->addr2);
  197. if (sta)
  198. sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
  199. } else if (fc & WLAN_FC_FROMDS) {
  200. bss = bss_get(wt, hdr->addr2);
  201. if (bss == NULL)
  202. return;
  203. sta = sta_get(bss, hdr->addr1);
  204. } else {
  205. bss = bss_get(wt, hdr->addr3);
  206. if (bss == NULL)
  207. return;
  208. sta = sta_find(bss, hdr->addr2);
  209. sta2 = sta_find(bss, hdr->addr1);
  210. if (sta == NULL || sta2 == NULL)
  211. return;
  212. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
  213. {
  214. if ((tdls->init == sta && tdls->resp == sta2) ||
  215. (tdls->init == sta2 && tdls->resp == sta)) {
  216. if (!tdls->link_up)
  217. wpa_printf(MSG_DEBUG, "TDLS: Link not "
  218. "up, but Data frame seen");
  219. tk = tdls->tpk.tk;
  220. break;
  221. }
  222. }
  223. }
  224. if ((sta == NULL ||
  225. (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) &&
  226. tk == NULL) {
  227. wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
  228. return;
  229. }
  230. if (len < 4) {
  231. wpa_printf(MSG_INFO, "Too short encrypted data frame");
  232. return;
  233. }
  234. if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
  235. !(data[3] & 0x20)) {
  236. wpa_printf(MSG_INFO, "Expected TKIP/CCMP frame from "
  237. MACSTR " did not have ExtIV bit set to 1",
  238. MAC2STR(src));
  239. return;
  240. }
  241. if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) {
  242. if (data[3] & 0x1f) {
  243. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  244. "non-zero reserved bit",
  245. MAC2STR(hdr->addr2));
  246. }
  247. if (data[1] != ((data[0] | 0x20) & 0x7f)) {
  248. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  249. "incorrect WEPSeed[1] (was 0x%x, expected "
  250. "0x%x)",
  251. MAC2STR(hdr->addr2), data[1],
  252. (data[0] | 0x20) & 0x7f);
  253. }
  254. } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) {
  255. if (data[2] != 0 || (data[3] & 0x1f) != 0) {
  256. wpa_printf(MSG_INFO, "CCMP frame from " MACSTR " used "
  257. "non-zero reserved bit",
  258. MAC2STR(hdr->addr2));
  259. }
  260. }
  261. keyid = data[3] >> 6;
  262. if (keyid != 0) {
  263. wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
  264. "individually addressed Data frame from " MACSTR,
  265. keyid, MAC2STR(hdr->addr2));
  266. }
  267. if (qos)
  268. tid = qos[0] & 0x0f;
  269. else
  270. tid = 0;
  271. if (tk) {
  272. if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
  273. rsc = tdls->rsc_init[tid];
  274. else
  275. rsc = tdls->rsc_resp[tid];
  276. } else if (fc & WLAN_FC_TODS)
  277. rsc = sta->rsc_tods[tid];
  278. else
  279. rsc = sta->rsc_fromds[tid];
  280. if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP)
  281. tkip_get_pn(pn, data);
  282. else if (sta->pairwise_cipher == WPA_CIPHER_WEP40)
  283. goto skip_replay_det;
  284. else
  285. ccmp_get_pn(pn, data);
  286. if (os_memcmp(pn, rsc, 6) <= 0) {
  287. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  288. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
  289. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  290. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  291. MAC2STR(hdr->addr3),
  292. WLAN_GET_SEQ_SEQ(seq_ctrl),
  293. WLAN_GET_SEQ_FRAG(seq_ctrl));
  294. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  295. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  296. }
  297. skip_replay_det:
  298. if (tk)
  299. decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen);
  300. else if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
  301. decrypted = tkip_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  302. else if (sta->pairwise_cipher == WPA_CIPHER_WEP40)
  303. decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
  304. else
  305. decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  306. if (decrypted) {
  307. rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted,
  308. dlen, 1);
  309. os_memcpy(rsc, pn, 6);
  310. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  311. decrypted, dlen);
  312. }
  313. os_free(decrypted);
  314. }
  315. static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  316. const u8 *qos, const u8 *dst, const u8 *src,
  317. const u8 *data, size_t len)
  318. {
  319. u16 fc = le_to_host16(hdr->frame_control);
  320. int prot = !!(fc & WLAN_FC_ISWEP);
  321. if (qos) {
  322. u8 ack = (qos[0] & 0x60) >> 5;
  323. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  324. " len=%u%s tid=%u%s%s",
  325. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  326. prot ? " Prot" : "", qos[0] & 0x0f,
  327. (qos[0] & 0x10) ? " EOSP" : "",
  328. ack == 0 ? "" :
  329. (ack == 1 ? " NoAck" :
  330. (ack == 2 ? " NoExpAck" : " BA")));
  331. } else {
  332. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  333. " len=%u%s",
  334. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  335. prot ? " Prot" : "");
  336. }
  337. if (prot)
  338. rx_data_bss_prot(wt, hdr, qos, dst, src, data, len);
  339. else {
  340. const u8 *bssid, *sta_addr;
  341. if (fc & WLAN_FC_TODS) {
  342. bssid = hdr->addr1;
  343. sta_addr = hdr->addr2;
  344. } else {
  345. bssid = hdr->addr2;
  346. sta_addr = hdr->addr1;
  347. }
  348. rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0);
  349. }
  350. }
  351. static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid,
  352. const u8 *sta1_addr,
  353. const u8 *sta2_addr)
  354. {
  355. struct wlantest_bss *bss;
  356. struct wlantest_sta *sta1, *sta2;
  357. struct wlantest_tdls *tdls;
  358. bss = bss_find(wt, bssid);
  359. if (bss == NULL)
  360. return NULL;
  361. sta1 = sta_find(bss, sta1_addr);
  362. if (sta1 == NULL)
  363. return NULL;
  364. sta2 = sta_find(bss, sta2_addr);
  365. if (sta2 == NULL)
  366. return NULL;
  367. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  368. if ((tdls->init == sta1 && tdls->resp == sta2) ||
  369. (tdls->init == sta2 && tdls->resp == sta1))
  370. return tdls;
  371. }
  372. return NULL;
  373. }
  374. static void add_direct_link(struct wlantest *wt, const u8 *bssid,
  375. const u8 *sta1_addr, const u8 *sta2_addr)
  376. {
  377. struct wlantest_tdls *tdls;
  378. tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
  379. if (tdls == NULL)
  380. return;
  381. if (tdls->link_up)
  382. tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++;
  383. else
  384. tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++;
  385. }
  386. static void add_ap_path(struct wlantest *wt, const u8 *bssid,
  387. const u8 *sta1_addr, const u8 *sta2_addr)
  388. {
  389. struct wlantest_tdls *tdls;
  390. tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
  391. if (tdls == NULL)
  392. return;
  393. if (tdls->link_up)
  394. tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++;
  395. else
  396. tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++;
  397. }
  398. void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  399. {
  400. const struct ieee80211_hdr *hdr;
  401. u16 fc, stype;
  402. size_t hdrlen;
  403. const u8 *qos = NULL;
  404. if (len < 24)
  405. return;
  406. hdr = (const struct ieee80211_hdr *) data;
  407. fc = le_to_host16(hdr->frame_control);
  408. stype = WLAN_FC_GET_STYPE(fc);
  409. hdrlen = 24;
  410. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  411. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  412. hdrlen += ETH_ALEN;
  413. if (stype & 0x08) {
  414. qos = data + hdrlen;
  415. hdrlen += 2;
  416. }
  417. if (len < hdrlen)
  418. return;
  419. wt->rx_data++;
  420. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  421. case 0:
  422. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  423. MACSTR " BSSID=" MACSTR,
  424. data_stype(WLAN_FC_GET_STYPE(fc)),
  425. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  426. fc & WLAN_FC_ISWEP ? " Prot" : "",
  427. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  428. MAC2STR(hdr->addr3));
  429. add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2);
  430. rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
  431. data + hdrlen, len - hdrlen);
  432. break;
  433. case WLAN_FC_FROMDS:
  434. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  435. " BSSID=" MACSTR " SA=" MACSTR,
  436. data_stype(WLAN_FC_GET_STYPE(fc)),
  437. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  438. fc & WLAN_FC_ISWEP ? " Prot" : "",
  439. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  440. MAC2STR(hdr->addr3));
  441. add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3);
  442. rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
  443. data + hdrlen, len - hdrlen);
  444. break;
  445. case WLAN_FC_TODS:
  446. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  447. " SA=" MACSTR " DA=" MACSTR,
  448. data_stype(WLAN_FC_GET_STYPE(fc)),
  449. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  450. fc & WLAN_FC_ISWEP ? " Prot" : "",
  451. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  452. MAC2STR(hdr->addr3));
  453. add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2);
  454. rx_data_bss(wt, hdr, qos, hdr->addr3, hdr->addr2,
  455. data + hdrlen, len - hdrlen);
  456. break;
  457. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  458. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  459. MACSTR " DA=" MACSTR " SA=" MACSTR,
  460. data_stype(WLAN_FC_GET_STYPE(fc)),
  461. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  462. fc & WLAN_FC_ISWEP ? " Prot" : "",
  463. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  464. MAC2STR(hdr->addr3),
  465. MAC2STR((const u8 *) (hdr + 1)));
  466. break;
  467. }
  468. }