rx_data.c 12 KB

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