rx_tdls.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Received Data frame processing for TDLS packets
  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 "utils/common.h"
  16. #include "crypto/sha256.h"
  17. #include "crypto/crypto.h"
  18. #include "crypto/aes_wrap.h"
  19. #include "common/ieee802_11_defs.h"
  20. #include "common/ieee802_11_common.h"
  21. #include "wlantest.h"
  22. static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *linkid,
  23. int create_new, const u8 *bssid)
  24. {
  25. struct wlantest_bss *bss;
  26. struct wlantest_sta *init, *resp;
  27. struct wlantest_tdls *tdls;
  28. bss = bss_find(wt, linkid);
  29. if (bss == NULL && bssid) {
  30. bss = bss_find(wt, bssid);
  31. if (bss)
  32. wpa_printf(MSG_INFO, "TDLS: Incorrect BSSID " MACSTR
  33. " in LinkId?! (init=" MACSTR " resp="
  34. MACSTR ")",
  35. MAC2STR(linkid), MAC2STR(linkid + ETH_ALEN),
  36. MAC2STR(linkid + 2 * ETH_ALEN));
  37. }
  38. if (bss == NULL)
  39. return NULL;
  40. init = sta_find(bss, linkid + ETH_ALEN);
  41. if (init == NULL)
  42. return NULL;
  43. resp = sta_find(bss, linkid + 2 * ETH_ALEN);
  44. if (resp == NULL)
  45. return NULL;
  46. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  47. if (tdls->init == init && tdls->resp == resp)
  48. return tdls;
  49. }
  50. if (!create_new)
  51. return NULL;
  52. tdls = os_zalloc(sizeof(*tdls));
  53. if (tdls == NULL)
  54. return NULL;
  55. tdls->init = init;
  56. tdls->resp = resp;
  57. dl_list_add(&bss->tdls, &tdls->list);
  58. return tdls;
  59. }
  60. static int tdls_derive_tpk(struct wlantest_tdls *tdls, const u8 *bssid,
  61. const u8 *ftie, u8 ftie_len)
  62. {
  63. const struct rsn_ftie *f;
  64. u8 key_input[SHA256_MAC_LEN];
  65. const u8 *nonce[2];
  66. size_t len[2];
  67. u8 data[3 * ETH_ALEN];
  68. if (ftie == NULL || ftie_len < sizeof(struct rsn_ftie))
  69. return 0;
  70. f = (const struct rsn_ftie *) ftie;
  71. wpa_hexdump(MSG_DEBUG, "TDLS ANonce", f->anonce, WPA_NONCE_LEN);
  72. wpa_hexdump(MSG_DEBUG, "TDLS SNonce", f->snonce, WPA_NONCE_LEN);
  73. /*
  74. * IEEE Std 802.11z-2010 8.5.9.1:
  75. * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
  76. */
  77. len[0] = WPA_NONCE_LEN;
  78. len[1] = WPA_NONCE_LEN;
  79. if (os_memcmp(f->anonce, f->snonce, WPA_NONCE_LEN) < 0) {
  80. nonce[0] = f->anonce;
  81. nonce[1] = f->snonce;
  82. } else {
  83. nonce[0] = f->snonce;
  84. nonce[1] = f->anonce;
  85. }
  86. sha256_vector(2, nonce, len, key_input);
  87. wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-Key-Input",
  88. key_input, SHA256_MAC_LEN);
  89. /*
  90. * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
  91. * min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
  92. * TODO: is N_KEY really included in KDF Context and if so, in which
  93. * presentation format (little endian 16-bit?) is it used? It gets
  94. * added by the KDF anyway..
  95. */
  96. if (os_memcmp(tdls->init->addr, tdls->resp->addr, ETH_ALEN) < 0) {
  97. os_memcpy(data, tdls->init->addr, ETH_ALEN);
  98. os_memcpy(data + ETH_ALEN, tdls->resp->addr, ETH_ALEN);
  99. } else {
  100. os_memcpy(data, tdls->resp->addr, ETH_ALEN);
  101. os_memcpy(data + ETH_ALEN, tdls->init->addr, ETH_ALEN);
  102. }
  103. os_memcpy(data + 2 * ETH_ALEN, bssid, ETH_ALEN);
  104. wpa_hexdump(MSG_DEBUG, "TDLS: KDF Context", data, sizeof(data));
  105. sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data),
  106. (u8 *) &tdls->tpk, sizeof(tdls->tpk));
  107. wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-KCK",
  108. tdls->tpk.kck, sizeof(tdls->tpk.kck));
  109. wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-TK",
  110. tdls->tpk.tk, sizeof(tdls->tpk.tk));
  111. return 1;
  112. }
  113. static int tdls_verify_mic(struct wlantest_tdls *tdls, u8 trans_seq,
  114. struct ieee802_11_elems *elems)
  115. {
  116. u8 *buf, *pos;
  117. int len;
  118. u8 mic[16];
  119. int ret;
  120. const struct rsn_ftie *rx_ftie;
  121. struct rsn_ftie *tmp_ftie;
  122. if (elems->link_id == NULL || elems->rsn_ie == NULL ||
  123. elems->timeout_int == NULL || elems->ftie == NULL)
  124. return -1;
  125. len = 2 * ETH_ALEN + 1 + 2 + 18 + 2 + elems->rsn_ie_len +
  126. 2 + elems->timeout_int_len + 2 + elems->ftie_len;
  127. buf = os_zalloc(len);
  128. if (buf == NULL)
  129. return -1;
  130. pos = buf;
  131. /* 1) TDLS initiator STA MAC address */
  132. os_memcpy(pos, elems->link_id + ETH_ALEN, ETH_ALEN);
  133. pos += ETH_ALEN;
  134. /* 2) TDLS responder STA MAC address */
  135. os_memcpy(pos, elems->link_id + 2 * ETH_ALEN, ETH_ALEN);
  136. pos += ETH_ALEN;
  137. /* 3) Transaction Sequence number */
  138. *pos++ = trans_seq;
  139. /* 4) Link Identifier IE */
  140. os_memcpy(pos, elems->link_id - 2, 2 + 18);
  141. pos += 2 + 18;
  142. /* 5) RSN IE */
  143. os_memcpy(pos, elems->rsn_ie - 2, 2 + elems->rsn_ie_len);
  144. pos += 2 + elems->rsn_ie_len;
  145. /* 6) Timeout Interval IE */
  146. os_memcpy(pos, elems->timeout_int - 2, 2 + elems->timeout_int_len);
  147. pos += 2 + elems->timeout_int_len;
  148. /* 7) FTIE, with the MIC field of the FTIE set to 0 */
  149. os_memcpy(pos, elems->ftie - 2, 2 + elems->ftie_len);
  150. pos += 2;
  151. tmp_ftie = (struct rsn_ftie *) pos;
  152. os_memset(tmp_ftie->mic, 0, 16);
  153. pos += elems->ftie_len;
  154. wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
  155. wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", tdls->tpk.kck, 16);
  156. ret = omac1_aes_128(tdls->tpk.kck, buf, pos - buf, mic);
  157. os_free(buf);
  158. if (ret)
  159. return -1;
  160. wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
  161. rx_ftie = (const struct rsn_ftie *) elems->ftie;
  162. if (os_memcmp(mic, rx_ftie->mic, 16) == 0) {
  163. wpa_printf(MSG_DEBUG, "TDLS: Valid MIC");
  164. return 0;
  165. }
  166. wpa_printf(MSG_DEBUG, "TDLS: Invalid MIC");
  167. return -1;
  168. }
  169. static void rx_data_tdls_setup_request(struct wlantest *wt, const u8 *bssid,
  170. const u8 *sta_addr, const u8 *dst,
  171. const u8 *src,
  172. const u8 *data, size_t len)
  173. {
  174. struct ieee802_11_elems elems;
  175. struct wlantest_tdls *tdls;
  176. if (len < 3) {
  177. wpa_printf(MSG_INFO, "Too short TDLS Setup Request " MACSTR
  178. " -> " MACSTR, MAC2STR(src), MAC2STR(dst));
  179. return;
  180. }
  181. wpa_printf(MSG_DEBUG, "TDLS Setup Request " MACSTR " -> "
  182. MACSTR, MAC2STR(src), MAC2STR(dst));
  183. if (ieee802_11_parse_elems(data + 3, len - 3, &elems, 1) ==
  184. ParseFailed || elems.link_id == NULL)
  185. return;
  186. wpa_printf(MSG_DEBUG, "TDLS Link Identifier: BSSID " MACSTR
  187. " initiator STA " MACSTR " responder STA " MACSTR,
  188. MAC2STR(elems.link_id), MAC2STR(elems.link_id + ETH_ALEN),
  189. MAC2STR(elems.link_id + 2 * ETH_ALEN));
  190. tdls = get_tdls(wt, elems.link_id, 1, bssid);
  191. if (tdls) {
  192. tdls->counters[WLANTEST_TDLS_COUNTER_SETUP_REQ]++;
  193. tdls->dialog_token = data[0];
  194. }
  195. }
  196. static void rx_data_tdls_setup_response_failure(struct wlantest *wt,
  197. const u8 *bssid,
  198. const u8 *sta_addr,
  199. u8 dialog_token, u16 status)
  200. {
  201. struct wlantest_bss *bss;
  202. struct wlantest_tdls *tdls;
  203. struct wlantest_sta *sta;
  204. if (status == WLAN_STATUS_SUCCESS) {
  205. wpa_printf(MSG_INFO, "TDLS: Invalid TDLS Setup Response from "
  206. MACSTR, MAC2STR(sta_addr));
  207. return;
  208. }
  209. bss = bss_find(wt, bssid);
  210. if (!bss)
  211. return;
  212. sta = sta_find(bss, sta_addr);
  213. if (!sta)
  214. return;
  215. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  216. if (tdls->resp == sta) {
  217. if (dialog_token != tdls->dialog_token) {
  218. wpa_printf(MSG_DEBUG, "TDLS: Dialog token "
  219. "mismatch in TDLS Setup Response "
  220. "(failure)");
  221. break;
  222. }
  223. wpa_printf(MSG_DEBUG, "TDLS: Found matching TDLS "
  224. "setup session based on dialog token");
  225. tdls->counters[
  226. WLANTEST_TDLS_COUNTER_SETUP_RESP_FAIL]++;
  227. break;
  228. }
  229. }
  230. }
  231. static void rx_data_tdls_setup_response(struct wlantest *wt, const u8 *bssid,
  232. const u8 *sta_addr, const u8 *dst,
  233. const u8 *src,
  234. const u8 *data, size_t len)
  235. {
  236. u16 status;
  237. struct ieee802_11_elems elems;
  238. struct wlantest_tdls *tdls;
  239. if (len < 3) {
  240. wpa_printf(MSG_INFO, "Too short TDLS Setup Response " MACSTR
  241. " -> " MACSTR, MAC2STR(src), MAC2STR(dst));
  242. return;
  243. }
  244. status = WPA_GET_LE16(data);
  245. wpa_printf(MSG_DEBUG, "TDLS Setup Response " MACSTR " -> "
  246. MACSTR " (status %d)",
  247. MAC2STR(src), MAC2STR(dst), status);
  248. if (len < 5 && status == 0) {
  249. wpa_printf(MSG_INFO, "Too short TDLS Setup Response " MACSTR
  250. " -> " MACSTR, MAC2STR(src), MAC2STR(dst));
  251. return;
  252. }
  253. if (len < 5 ||
  254. ieee802_11_parse_elems(data + 5, len - 5, &elems, 1) ==
  255. ParseFailed || elems.link_id == NULL) {
  256. /* Need to match TDLS link based on Dialog Token */
  257. rx_data_tdls_setup_response_failure(wt, bssid, sta_addr,
  258. data[2], status);
  259. return;
  260. }
  261. wpa_printf(MSG_DEBUG, "TDLS Link Identifier: BSSID " MACSTR
  262. " initiator STA " MACSTR " responder STA " MACSTR,
  263. MAC2STR(elems.link_id), MAC2STR(elems.link_id + ETH_ALEN),
  264. MAC2STR(elems.link_id + 2 * ETH_ALEN));
  265. tdls = get_tdls(wt, elems.link_id, 1, bssid);
  266. if (!tdls)
  267. return;
  268. if (status)
  269. tdls->counters[WLANTEST_TDLS_COUNTER_SETUP_RESP_FAIL]++;
  270. else
  271. tdls->counters[WLANTEST_TDLS_COUNTER_SETUP_RESP_OK]++;
  272. if (status != WLAN_STATUS_SUCCESS)
  273. return;
  274. if (tdls_derive_tpk(tdls, bssid, elems.ftie, elems.ftie_len) < 1)
  275. return;
  276. if (tdls_verify_mic(tdls, 2, &elems) == 0) {
  277. tdls->dialog_token = data[2];
  278. wpa_printf(MSG_DEBUG, "TDLS: Dialog Token for the link: %u",
  279. tdls->dialog_token);
  280. }
  281. }
  282. static void rx_data_tdls_setup_confirm(struct wlantest *wt, const u8 *bssid,
  283. const u8 *sta_addr, const u8 *dst,
  284. const u8 *src,
  285. const u8 *data, size_t len)
  286. {
  287. u16 status;
  288. struct ieee802_11_elems elems;
  289. struct wlantest_tdls *tdls;
  290. u8 link_id[3 * ETH_ALEN];
  291. if (len < 3) {
  292. wpa_printf(MSG_INFO, "Too short TDLS Setup Confirm " MACSTR
  293. " -> " MACSTR, MAC2STR(src), MAC2STR(dst));
  294. return;
  295. }
  296. status = WPA_GET_LE16(data);
  297. wpa_printf(MSG_DEBUG, "TDLS Setup Confirm " MACSTR " -> "
  298. MACSTR " (status %d)",
  299. MAC2STR(src), MAC2STR(dst), status);
  300. if (ieee802_11_parse_elems(data + 3, len - 3, &elems, 1) ==
  301. ParseFailed || elems.link_id == NULL)
  302. return;
  303. wpa_printf(MSG_DEBUG, "TDLS Link Identifier: BSSID " MACSTR
  304. " initiator STA " MACSTR " responder STA " MACSTR,
  305. MAC2STR(elems.link_id), MAC2STR(elems.link_id + ETH_ALEN),
  306. MAC2STR(elems.link_id + 2 * ETH_ALEN));
  307. tdls = get_tdls(wt, elems.link_id, 1, bssid);
  308. if (tdls == NULL)
  309. return;
  310. if (status)
  311. tdls->counters[WLANTEST_TDLS_COUNTER_SETUP_CONF_FAIL]++;
  312. else
  313. tdls->counters[WLANTEST_TDLS_COUNTER_SETUP_CONF_OK]++;
  314. if (status != WLAN_STATUS_SUCCESS)
  315. return;
  316. tdls->link_up = 1;
  317. if (tdls_derive_tpk(tdls, bssid, elems.ftie, elems.ftie_len) < 1) {
  318. if (elems.ftie == NULL)
  319. goto remove_reverse;
  320. return;
  321. }
  322. if (tdls_verify_mic(tdls, 3, &elems) == 0) {
  323. tdls->dialog_token = data[2];
  324. wpa_printf(MSG_DEBUG, "TDLS: Dialog Token for the link: %u",
  325. tdls->dialog_token);
  326. }
  327. remove_reverse:
  328. /*
  329. * The TDLS link itself is bidirectional, but there is explicit
  330. * initiator/responder roles. Remove the other direction of the link
  331. * (if it exists) to make sure that the link counters are stored for
  332. * the current TDLS entery.
  333. */
  334. os_memcpy(link_id, elems.link_id, ETH_ALEN);
  335. os_memcpy(link_id + ETH_ALEN, elems.link_id + 2 * ETH_ALEN, ETH_ALEN);
  336. os_memcpy(link_id + 2 * ETH_ALEN, elems.link_id + ETH_ALEN, ETH_ALEN);
  337. tdls = get_tdls(wt, link_id, 0, bssid);
  338. if (tdls) {
  339. wpa_printf(MSG_DEBUG, "TDLS: Remove reverse link entry");
  340. tdls_deinit(tdls);
  341. }
  342. }
  343. static int tdls_verify_mic_teardown(struct wlantest_tdls *tdls, u8 trans_seq,
  344. const u8 *reason_code,
  345. struct ieee802_11_elems *elems)
  346. {
  347. u8 *buf, *pos;
  348. int len;
  349. u8 mic[16];
  350. int ret;
  351. const struct rsn_ftie *rx_ftie;
  352. struct rsn_ftie *tmp_ftie;
  353. if (elems->link_id == NULL || elems->ftie == NULL)
  354. return -1;
  355. len = 2 + 18 + 2 + 1 + 1 + 2 + elems->ftie_len;
  356. buf = os_zalloc(len);
  357. if (buf == NULL)
  358. return -1;
  359. pos = buf;
  360. /* 1) Link Identifier IE */
  361. os_memcpy(pos, elems->link_id - 2, 2 + 18);
  362. pos += 2 + 18;
  363. /* 2) Reason Code */
  364. os_memcpy(pos, reason_code, 2);
  365. pos += 2;
  366. /* 3) Dialog token */
  367. *pos++ = tdls->dialog_token;
  368. /* 4) Transaction Sequence number */
  369. *pos++ = trans_seq;
  370. /* 5) FTIE, with the MIC field of the FTIE set to 0 */
  371. os_memcpy(pos, elems->ftie - 2, 2 + elems->ftie_len);
  372. pos += 2;
  373. tmp_ftie = (struct rsn_ftie *) pos;
  374. os_memset(tmp_ftie->mic, 0, 16);
  375. pos += elems->ftie_len;
  376. wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
  377. wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", tdls->tpk.kck, 16);
  378. ret = omac1_aes_128(tdls->tpk.kck, buf, pos - buf, mic);
  379. os_free(buf);
  380. if (ret)
  381. return -1;
  382. wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
  383. rx_ftie = (const struct rsn_ftie *) elems->ftie;
  384. if (os_memcmp(mic, rx_ftie->mic, 16) == 0) {
  385. wpa_printf(MSG_DEBUG, "TDLS: Valid MIC");
  386. return 0;
  387. }
  388. wpa_printf(MSG_DEBUG, "TDLS: Invalid MIC");
  389. return -1;
  390. }
  391. static void rx_data_tdls_teardown(struct wlantest *wt, const u8 *bssid,
  392. const u8 *sta_addr, const u8 *dst,
  393. const u8 *src,
  394. const u8 *data, size_t len)
  395. {
  396. u16 reason;
  397. struct ieee802_11_elems elems;
  398. struct wlantest_tdls *tdls;
  399. if (len < 2)
  400. return;
  401. reason = WPA_GET_LE16(data);
  402. wpa_printf(MSG_DEBUG, "TDLS Teardown " MACSTR " -> "
  403. MACSTR " (reason %d)",
  404. MAC2STR(src), MAC2STR(dst), reason);
  405. if (ieee802_11_parse_elems(data + 2, len - 2, &elems, 1) ==
  406. ParseFailed || elems.link_id == NULL)
  407. return;
  408. wpa_printf(MSG_DEBUG, "TDLS Link Identifier: BSSID " MACSTR
  409. " initiator STA " MACSTR " responder STA " MACSTR,
  410. MAC2STR(elems.link_id), MAC2STR(elems.link_id + ETH_ALEN),
  411. MAC2STR(elems.link_id + 2 * ETH_ALEN));
  412. tdls = get_tdls(wt, elems.link_id, 1, bssid);
  413. if (tdls) {
  414. tdls->link_up = 0;
  415. tdls->counters[WLANTEST_TDLS_COUNTER_TEARDOWN]++;
  416. tdls_verify_mic_teardown(tdls, 4, data, &elems);
  417. }
  418. }
  419. static void rx_data_tdls(struct wlantest *wt, const u8 *bssid,
  420. const u8 *sta_addr, const u8 *dst, const u8 *src,
  421. const u8 *data, size_t len)
  422. {
  423. /* data contains the payload of a TDLS Action frame */
  424. if (len < 2 || data[0] != WLAN_ACTION_TDLS) {
  425. wpa_hexdump(MSG_DEBUG, "Unrecognized encapsulated TDLS frame",
  426. data, len);
  427. return;
  428. }
  429. switch (data[1]) {
  430. case WLAN_TDLS_SETUP_REQUEST:
  431. rx_data_tdls_setup_request(wt, bssid, sta_addr, dst, src,
  432. data + 2, len - 2);
  433. break;
  434. case WLAN_TDLS_SETUP_RESPONSE:
  435. rx_data_tdls_setup_response(wt, bssid, sta_addr, dst, src,
  436. data + 2, len - 2);
  437. break;
  438. case WLAN_TDLS_SETUP_CONFIRM:
  439. rx_data_tdls_setup_confirm(wt, bssid, sta_addr, dst, src,
  440. data + 2, len - 2);
  441. break;
  442. case WLAN_TDLS_TEARDOWN:
  443. rx_data_tdls_teardown(wt, bssid, sta_addr, dst, src, data + 2,
  444. len - 2);
  445. break;
  446. case WLAN_TDLS_DISCOVERY_REQUEST:
  447. wpa_printf(MSG_DEBUG, "TDLS Discovery Request " MACSTR " -> "
  448. MACSTR, MAC2STR(src), MAC2STR(dst));
  449. break;
  450. }
  451. }
  452. void rx_data_80211_encap(struct wlantest *wt, const u8 *bssid,
  453. const u8 *sta_addr, const u8 *dst, const u8 *src,
  454. const u8 *data, size_t len)
  455. {
  456. wpa_hexdump(MSG_EXCESSIVE, "802.11 data encap frame", data, len);
  457. if (len < 1)
  458. return;
  459. if (data[0] == 0x02)
  460. rx_data_tdls(wt, bssid, sta_addr, dst, src, data + 1, len - 1);
  461. }