rx_data.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. }
  68. }
  69. static void rx_data_process(struct wlantest *wt, const u8 *bssid,
  70. const u8 *sta_addr,
  71. const u8 *dst, const u8 *src,
  72. const u8 *data, size_t len, int prot)
  73. {
  74. if (len == 0)
  75. return;
  76. if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
  77. rx_data_eth(wt, bssid, sta_addr, dst, src,
  78. WPA_GET_BE16(data + 6), data + 8, len - 8, prot);
  79. return;
  80. }
  81. wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
  82. }
  83. static void rx_data_bss_prot_group(struct wlantest *wt,
  84. const struct ieee80211_hdr *hdr,
  85. const u8 *qos, const u8 *dst, const u8 *src,
  86. const u8 *data, size_t len)
  87. {
  88. struct wlantest_bss *bss;
  89. int keyid;
  90. u8 *decrypted;
  91. size_t dlen;
  92. u8 pn[6];
  93. bss = bss_get(wt, hdr->addr2);
  94. if (bss == NULL)
  95. return;
  96. if (len < 4) {
  97. wpa_printf(MSG_INFO, "Too short group addressed data frame");
  98. return;
  99. }
  100. if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
  101. !(data[3] & 0x20)) {
  102. wpa_printf(MSG_INFO, "Expected TKIP/CCMP frame from "
  103. MACSTR " did not have ExtIV bit set to 1",
  104. MAC2STR(bss->bssid));
  105. return;
  106. }
  107. if (bss->group_cipher == WPA_CIPHER_TKIP) {
  108. if (data[3] & 0x1f) {
  109. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  110. "non-zero reserved bit",
  111. MAC2STR(bss->bssid));
  112. }
  113. if (data[1] != ((data[0] | 0x20) & 0x7f)) {
  114. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  115. "incorrect WEPSeed[1] (was 0x%x, expected "
  116. "0x%x)",
  117. MAC2STR(bss->bssid), data[1],
  118. (data[0] | 0x20) & 0x7f);
  119. }
  120. } else if (bss->group_cipher == WPA_CIPHER_CCMP) {
  121. if (data[2] != 0 || (data[3] & 0x1f) != 0) {
  122. wpa_printf(MSG_INFO, "CCMP frame from " MACSTR " used "
  123. "non-zero reserved bit",
  124. MAC2STR(bss->bssid));
  125. }
  126. }
  127. keyid = data[3] >> 6;
  128. if (bss->gtk_len[keyid] == 0) {
  129. wpa_printf(MSG_MSGDUMP, "No GTK known to decrypt the frame "
  130. "(A2=" MACSTR " KeyID=%d)",
  131. MAC2STR(hdr->addr2), keyid);
  132. return;
  133. }
  134. if (bss->group_cipher == WPA_CIPHER_TKIP)
  135. tkip_get_pn(pn, data);
  136. else
  137. ccmp_get_pn(pn, data);
  138. if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
  139. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  140. MAC2STR(hdr->addr2));
  141. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  142. wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
  143. }
  144. if (bss->group_cipher == WPA_CIPHER_TKIP)
  145. decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
  146. &dlen);
  147. else
  148. decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
  149. &dlen);
  150. if (decrypted) {
  151. rx_data_process(wt, bss->bssid, NULL, dst, src, decrypted,
  152. dlen, 1);
  153. os_memcpy(bss->rsc[keyid], pn, 6);
  154. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  155. decrypted, dlen);
  156. }
  157. os_free(decrypted);
  158. }
  159. static void rx_data_bss_prot(struct wlantest *wt,
  160. const struct ieee80211_hdr *hdr, const u8 *qos,
  161. const u8 *dst, const u8 *src, const u8 *data,
  162. size_t len)
  163. {
  164. struct wlantest_bss *bss;
  165. struct wlantest_sta *sta;
  166. int keyid;
  167. u16 fc = le_to_host16(hdr->frame_control);
  168. u8 *decrypted;
  169. size_t dlen;
  170. int tid;
  171. u8 pn[6], *rsc;
  172. if (hdr->addr1[0] & 0x01) {
  173. rx_data_bss_prot_group(wt, hdr, qos, dst, src, data, len);
  174. return;
  175. }
  176. if (fc & WLAN_FC_TODS) {
  177. bss = bss_get(wt, hdr->addr1);
  178. if (bss == NULL)
  179. return;
  180. sta = sta_get(bss, hdr->addr2);
  181. } else {
  182. bss = bss_get(wt, hdr->addr2);
  183. if (bss == NULL)
  184. return;
  185. sta = sta_get(bss, hdr->addr1);
  186. }
  187. if (sta == NULL || !sta->ptk_set) {
  188. wpa_printf(MSG_MSGDUMP, "No PTK known to decrypt the frame");
  189. return;
  190. }
  191. if (len < 4) {
  192. wpa_printf(MSG_INFO, "Too short encrypted data frame");
  193. return;
  194. }
  195. if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
  196. !(data[3] & 0x20)) {
  197. wpa_printf(MSG_INFO, "Expected TKIP/CCMP frame from "
  198. MACSTR " did not have ExtIV bit set to 1",
  199. MAC2STR(src));
  200. return;
  201. }
  202. if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
  203. if (data[3] & 0x1f) {
  204. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  205. "non-zero reserved bit",
  206. MAC2STR(hdr->addr2));
  207. }
  208. if (data[1] != ((data[0] | 0x20) & 0x7f)) {
  209. wpa_printf(MSG_INFO, "TKIP frame from " MACSTR " used "
  210. "incorrect WEPSeed[1] (was 0x%x, expected "
  211. "0x%x)",
  212. MAC2STR(hdr->addr2), data[1],
  213. (data[0] | 0x20) & 0x7f);
  214. }
  215. } else if (sta->pairwise_cipher == WPA_CIPHER_CCMP) {
  216. if (data[2] != 0 || (data[3] & 0x1f) != 0) {
  217. wpa_printf(MSG_INFO, "CCMP frame from " MACSTR " used "
  218. "non-zero reserved bit",
  219. MAC2STR(hdr->addr2));
  220. }
  221. }
  222. keyid = data[3] >> 6;
  223. if (keyid != 0) {
  224. wpa_printf(MSG_INFO, "Unexpected non-zero KeyID %d in "
  225. "individually addressed Data frame from " MACSTR,
  226. keyid, MAC2STR(hdr->addr2));
  227. }
  228. if (qos)
  229. tid = qos[0] & 0x0f;
  230. else
  231. tid = 0;
  232. if (fc & WLAN_FC_TODS)
  233. rsc = sta->rsc_tods[tid];
  234. else
  235. rsc = sta->rsc_fromds[tid];
  236. if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
  237. tkip_get_pn(pn, data);
  238. else
  239. ccmp_get_pn(pn, data);
  240. if (os_memcmp(pn, rsc, 6) <= 0) {
  241. wpa_printf(MSG_INFO, "CCMP/TKIP replay detected: SA=" MACSTR,
  242. MAC2STR(hdr->addr2));
  243. wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
  244. wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
  245. }
  246. if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
  247. decrypted = tkip_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  248. else
  249. decrypted = ccmp_decrypt(sta->ptk.tk1, hdr, data, len, &dlen);
  250. if (decrypted) {
  251. rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted,
  252. dlen, 1);
  253. os_memcpy(rsc, pn, 6);
  254. write_pcap_decrypted(wt, (const u8 *) hdr, 24 + (qos ? 2 : 0),
  255. decrypted, dlen);
  256. }
  257. os_free(decrypted);
  258. }
  259. static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
  260. const u8 *qos, const u8 *dst, const u8 *src,
  261. const u8 *data, size_t len)
  262. {
  263. u16 fc = le_to_host16(hdr->frame_control);
  264. int prot = !!(fc & WLAN_FC_ISWEP);
  265. if (qos) {
  266. u8 ack = (qos[0] & 0x60) >> 5;
  267. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  268. " len=%u%s tid=%u%s%s",
  269. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  270. prot ? " Prot" : "", qos[0] & 0x0f,
  271. (qos[0] & 0x10) ? " EOSP" : "",
  272. ack == 0 ? "" :
  273. (ack == 1 ? " NoAck" :
  274. (ack == 2 ? " NoExpAck" : " BA")));
  275. } else {
  276. wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
  277. " len=%u%s",
  278. MAC2STR(src), MAC2STR(dst), (unsigned int) len,
  279. prot ? " Prot" : "");
  280. }
  281. if (prot)
  282. rx_data_bss_prot(wt, hdr, qos, dst, src, data, len);
  283. else {
  284. const u8 *bssid, *sta_addr;
  285. if (fc & WLAN_FC_TODS) {
  286. bssid = hdr->addr1;
  287. sta_addr = hdr->addr2;
  288. } else {
  289. bssid = hdr->addr2;
  290. sta_addr = hdr->addr1;
  291. }
  292. rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0);
  293. }
  294. }
  295. void rx_data(struct wlantest *wt, const u8 *data, size_t len)
  296. {
  297. const struct ieee80211_hdr *hdr;
  298. u16 fc, stype;
  299. size_t hdrlen;
  300. const u8 *qos = NULL;
  301. if (len < 24)
  302. return;
  303. hdr = (const struct ieee80211_hdr *) data;
  304. fc = le_to_host16(hdr->frame_control);
  305. stype = WLAN_FC_GET_STYPE(fc);
  306. hdrlen = 24;
  307. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  308. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  309. hdrlen += ETH_ALEN;
  310. if (stype & 0x08) {
  311. qos = data + hdrlen;
  312. hdrlen += 2;
  313. }
  314. if (len < hdrlen)
  315. return;
  316. wt->rx_data++;
  317. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  318. case 0:
  319. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
  320. MACSTR " BSSID=" MACSTR,
  321. data_stype(WLAN_FC_GET_STYPE(fc)),
  322. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  323. fc & WLAN_FC_ISWEP ? " Prot" : "",
  324. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  325. MAC2STR(hdr->addr3));
  326. break;
  327. case WLAN_FC_FROMDS:
  328. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
  329. " BSSID=" MACSTR " SA=" MACSTR,
  330. data_stype(WLAN_FC_GET_STYPE(fc)),
  331. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  332. fc & WLAN_FC_ISWEP ? " Prot" : "",
  333. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  334. MAC2STR(hdr->addr3));
  335. rx_data_bss(wt, hdr, qos, hdr->addr1, hdr->addr2,
  336. data + hdrlen, len - hdrlen);
  337. break;
  338. case WLAN_FC_TODS:
  339. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
  340. " SA=" MACSTR " DA=" MACSTR,
  341. data_stype(WLAN_FC_GET_STYPE(fc)),
  342. fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
  343. fc & WLAN_FC_ISWEP ? " Prot" : "",
  344. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  345. MAC2STR(hdr->addr3));
  346. rx_data_bss(wt, hdr, qos, hdr->addr3, hdr->addr2,
  347. data + hdrlen, len - hdrlen);
  348. break;
  349. case WLAN_FC_TODS | WLAN_FC_FROMDS:
  350. wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
  351. MACSTR " DA=" MACSTR " SA=" 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. MAC2STR((const u8 *) (hdr + 1)));
  358. break;
  359. }
  360. }