18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/*
28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_supplicant - TDLS
38d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2010-2011, Atheros Communications
48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license.
6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details.
78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/includes.h"
108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/common.h"
128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/eloop.h"
138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/os.h"
148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common/ieee802_11_defs.h"
158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "crypto/sha256.h"
168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "crypto/crypto.h"
178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "crypto/aes_wrap.h"
188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "rsn_supp/wpa.h"
198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "rsn_supp/wpa_ie.h"
208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "rsn_supp/wpa_i.h"
218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "drivers/driver.h"
228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "l2_packet/l2_packet.h"
238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_LONG_FRAME BIT(0)
268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_ALT_RSN_IE BIT(1)
278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_DIFF_BSSID BIT(2)
288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_SHORT_LIFETIME BIT(3)
298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_WRONG_LIFETIME_RESP BIT(4)
308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_WRONG_LIFETIME_CONF BIT(5)
318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_LONG_LIFETIME BIT(6)
328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_CONCURRENT_INIT BIT(7)
338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_NO_TPK_EXPIRATION BIT(8)
348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_DECLINE_RESP BIT(9)
358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TESTING_IGNORE_AP_PROHIBIT BIT(10)
368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtunsigned int tdls_testing = 0;
378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TPK_LIFETIME 43200 /* 12 hours */
40f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt#define TPK_M1_RETRY_COUNT 3
41f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt#define TPK_M1_TIMEOUT 5000 /* in milliseconds */
42f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt#define TPK_M2_RETRY_COUNT 10
43f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt#define TPK_M2_TIMEOUT 500 /* in milliseconds */
448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_MIC_LEN		16
468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_TIMEOUT_LEN	4
488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_tdls_ftie {
508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 ie_type; /* FTIE */
518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 ie_len;
528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 mic_ctrl[2];
538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 mic[TDLS_MIC_LEN];
548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 Anonce[WPA_NONCE_LEN]; /* Responder Nonce in TDLS */
558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 Snonce[WPA_NONCE_LEN]; /* Initiator Nonce in TDLS */
568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* followed by optional elements */
578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} STRUCT_PACKED;
588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_tdls_timeoutie {
608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 ie_type; /* Timeout IE */
618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 ie_len;
628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 interval_type;
638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 value[TDLS_TIMEOUT_LEN];
648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} STRUCT_PACKED;
658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_tdls_lnkid {
678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 ie_type; /* Link Identifier IE */
688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 ie_len;
698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 bssid[ETH_ALEN];
708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 init_sta[ETH_ALEN];
718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 resp_sta[ETH_ALEN];
728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} STRUCT_PACKED;
738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* TDLS frame headers as per IEEE Std 802.11z-2010 */
758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_tdls_frame {
768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 payloadtype; /* IEEE80211_TDLS_RFTYPE */
778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 category; /* Category */
788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 action; /* Action (enum tdls_frame_type) */
798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} STRUCT_PACKED;
808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs);
828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx);
838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer);
84d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Duttstatic void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
85d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt				       struct wpa_tdls_peer *peer);
868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define TDLS_MAX_IE_LEN 80
891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#define IEEE80211_MAX_SUPP_RATES 32
901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_tdls_peer {
928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *next;
938da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	unsigned int reconfig_key:1;
948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int initiator; /* whether this end was initiator for TDLS setup */
958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 addr[ETH_ALEN]; /* other end MAC address */
968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 inonce[WPA_NONCE_LEN]; /* Initiator Nonce */
978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 rnonce[WPA_NONCE_LEN]; /* Responder Nonce */
988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 rsnie_i[TDLS_MAX_IE_LEN]; /* Initiator RSN IE */
998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t rsnie_i_len;
1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 rsnie_p[TDLS_MAX_IE_LEN]; /* Peer RSN IE */
1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t rsnie_p_len;
1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 lifetime;
1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int cipher; /* Selected cipher (WPA_CIPHER_*) */
1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 dtoken;
1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct tpk {
1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u8 kck[16]; /* TPK-KCK */
1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u8 tk[16]; /* TPK-TK; assuming only CCMP will be used */
1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} tpk;
1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int tpk_set;
1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int tpk_success;
11273b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt	int tpk_in_progress;
1138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct tpk_timer {
1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u8 dest[ETH_ALEN];
1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		int count;      /* Retry Count */
1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		int timer;      /* Timeout in milliseconds */
1188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u8 action_code; /* TDLS frame type */
1198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u8 dialog_token;
1208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u16 status_code;
1218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		int buf_len;    /* length of TPK message for retransmission */
1228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u8 *buf;        /* buffer for TPK message */
1238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} sm_tmr;
1241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u16 capability;
1261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
1281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	size_t supp_rates_len;
129f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
130f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	struct ieee80211_ht_capabilities *ht_capabilities;
13133e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	struct ieee80211_vht_capabilities *vht_capabilities;
132f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
133f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	u8 qos_info;
134f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
13551b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt	u16 aid;
13651b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt
137f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	u8 *ext_capab;
138f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	size_t ext_capab_len;
1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_get_privacy(struct wpa_sm *sm)
1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
1458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Get info needed from supplicant to check if the current BSS supports
1468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * security. Other than OPEN mode, rest are considered secured
1478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * WEP/WPA/WPA2 hence TDLS frames are processed for TPK handshake.
1488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
1498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return sm->pairwise_cipher != WPA_CIPHER_NONE;
1508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, ie, ie_len);
1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return pos + ie_len;
1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_del_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_sm_set_key(sm, WPA_ALG_NONE, peer->addr,
1638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   0, 0, NULL, 0, NULL, 0) < 0) {
1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_WARNING, "TDLS: Failed to delete TPK-TK from "
1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "the driver");
1668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
1708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 key_len;
1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 rsc[6];
1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	enum wpa_alg alg;
1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(rsc, 0, 6);
1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (peer->cipher) {
1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPA_CIPHER_CCMP:
1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		alg = WPA_ALG_CCMP;
1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		key_len = 16;
1858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPA_CIPHER_NONE:
1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Pairwise Cipher Suite: "
1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "NONE - do not use pairwise keys");
1898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
1908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
1918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_WARNING, "TDLS: Unsupported pairwise cipher %d",
1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   sm->pairwise_cipher);
1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
1948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
1958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_sm_set_key(sm, alg, peer->addr, -1, 1,
1978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   rsc, sizeof(rsc), peer->tpk.tk, key_len) < 0) {
1988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_WARNING, "TDLS: Failed to set TPK to the "
1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "driver");
2008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_send_tpk_msg(struct wpa_sm *sm, const u8 *dst,
2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 u8 action_code, u8 dialog_token,
2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 u16 status_code, const u8 *buf, size_t len)
2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wpa_sm_send_tdls_mgmt(sm, dst, action_code, dialog_token,
2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     status_code, buf, len);
2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_tpk_send(struct wpa_sm *sm, const u8 *dest, u8 action_code,
2168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     u8 dialog_token, u16 status_code,
2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     const u8 *msg, size_t msg_len)
2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer;
2208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TPK send dest=" MACSTR " action_code=%u "
2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "dialog_token=%u status_code=%u msg_len=%u",
2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(dest), action_code, dialog_token, status_code,
2248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (unsigned int) msg_len);
2258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_tdls_send_tpk_msg(sm, dest, action_code, dialog_token,
2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  status_code, msg, msg_len)) {
2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Failed to send message "
2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(action_code=%u)", action_code);
2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (action_code == WLAN_TDLS_SETUP_CONFIRM ||
2341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    action_code == WLAN_TDLS_TEARDOWN ||
2351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    action_code == WLAN_TDLS_DISCOVERY_REQUEST ||
2361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    action_code == WLAN_TDLS_DISCOVERY_RESPONSE)
2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0; /* No retries */
2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (peer = sm->tdls; peer; peer = peer->next) {
2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(peer->addr, dest, ETH_ALEN) == 0)
2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer == NULL) {
2458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
2468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "retry " MACSTR, MAC2STR(dest));
2478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
2488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
2518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
252f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt	if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
253f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		peer->sm_tmr.count = TPK_M2_RETRY_COUNT;
254f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		peer->sm_tmr.timer = TPK_M2_TIMEOUT;
255f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt	} else {
256f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		peer->sm_tmr.count = TPK_M1_RETRY_COUNT;
257f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		peer->sm_tmr.timer = TPK_M1_TIMEOUT;
258f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt	}
2598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Copy message to resend on timeout */
2618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(peer->sm_tmr.dest, dest, ETH_ALEN);
2628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->sm_tmr.action_code = action_code;
2638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->sm_tmr.dialog_token = dialog_token;
2648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->sm_tmr.status_code = status_code;
2658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->sm_tmr.buf_len = msg_len;
2668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(peer->sm_tmr.buf);
2678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->sm_tmr.buf = os_malloc(msg_len);
2688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer->sm_tmr.buf == NULL)
2698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
2708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(peer->sm_tmr.buf, msg, msg_len);
2718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Retry timeout registered "
2738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "(action_code=%u)", action_code);
274f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt	eloop_register_timeout(peer->sm_tmr.timer / 1000,
275f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt			       (peer->sm_tmr.timer % 1000) * 1000,
2768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       wpa_tdls_tpk_retry_timeout, sm, peer);
2778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
2788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_tdls_do_teardown(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
2826a9f522e56dae5b1fff00642e27b82e82772dcf7Sunil Dutt				u16 reason_code)
2831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
2841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ret;
2851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2866a9f522e56dae5b1fff00642e27b82e82772dcf7Sunil Dutt	ret = wpa_tdls_send_teardown(sm, peer->addr, reason_code);
2876a9f522e56dae5b1fff00642e27b82e82772dcf7Sunil Dutt	/* disable the link after teardown was sent */
288d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt	wpa_tdls_disable_peer_link(sm, peer);
2891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return ret;
2911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
2921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx)
2958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_sm *sm = eloop_ctx;
2988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer = timeout_ctx;
2998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer->sm_tmr.count) {
3018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->sm_tmr.count--;
3028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Retrying sending of message "
3048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(action_code=%u)",
3058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   peer->sm_tmr.action_code);
3068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (peer->sm_tmr.buf == NULL) {
3088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "TDLS: No retry buffer available "
3098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "for action_code=%u",
3108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   peer->sm_tmr.action_code);
3118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm,
3128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     peer);
3138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return;
3148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
3158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* resend TPK Handshake Message to Peer */
3178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wpa_tdls_send_tpk_msg(sm, peer->sm_tmr.dest,
3188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  peer->sm_tmr.action_code,
3198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  peer->sm_tmr.dialog_token,
3208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  peer->sm_tmr.status_code,
3218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  peer->sm_tmr.buf,
3228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  peer->sm_tmr.buf_len)) {
3238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "TDLS: Failed to retry "
3248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "transmission");
3258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
3268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
328f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		eloop_register_timeout(peer->sm_tmr.timer / 1000,
329f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt				       (peer->sm_tmr.timer % 1000) * 1000,
3308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       wpa_tdls_tpk_retry_timeout, sm, peer);
3318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
3328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
3338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Sending Teardown Request");
3351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_tdls_do_teardown(sm, peer,
3366a9f522e56dae5b1fff00642e27b82e82772dcf7Sunil Dutt				     WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
3378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm *sm,
3428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					      struct wpa_tdls_peer *peer,
3438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					      u8 action_code)
3448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (action_code == peer->sm_tmr.action_code) {
3468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Retry timeout cancelled for "
3478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "action_code=%u", action_code);
3488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Cancel Timeout registered */
3508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
3518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* free all resources meant for retry */
3538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(peer->sm_tmr.buf);
3548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->sm_tmr.buf = NULL;
3558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->sm_tmr.count = 0;
3578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->sm_tmr.timer = 0;
3588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->sm_tmr.buf_len = 0;
3598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->sm_tmr.action_code = 0xff;
3608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
3618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Error in cancelling retry timeout "
3628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(Unknown action_code=%u)", action_code);
3638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_tdls_generate_tpk(struct wpa_tdls_peer *peer,
3688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  const u8 *own_addr, const u8 *bssid)
3698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 key_input[SHA256_MAC_LEN];
3718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *nonce[2];
3728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t len[2];
3738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 data[3 * ETH_ALEN];
3748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* IEEE Std 802.11z-2010 8.5.9.1:
3768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
3778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
3788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[0] = WPA_NONCE_LEN;
3798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[1] = WPA_NONCE_LEN;
3808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(peer->inonce, peer->rnonce, WPA_NONCE_LEN) < 0) {
3818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nonce[0] = peer->inonce;
3828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nonce[1] = peer->rnonce;
3838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
3848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nonce[0] = peer->rnonce;
3858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nonce[1] = peer->inonce;
3868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: min(Nonce)", nonce[0], WPA_NONCE_LEN);
3888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: max(Nonce)", nonce[1], WPA_NONCE_LEN);
3898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sha256_vector(2, nonce, len, key_input);
3908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-Key-Input",
3918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			key_input, SHA256_MAC_LEN);
3928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
3948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
3958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 *	min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
3968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * TODO: is N_KEY really included in KDF Context and if so, in which
3978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * presentation format (little endian 16-bit?) is it used? It gets
3988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * added by the KDF anyway..
3998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
4008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(own_addr, peer->addr, ETH_ALEN) < 0) {
4028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data, own_addr, ETH_ALEN);
4038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data + ETH_ALEN, peer->addr, ETH_ALEN);
4048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
4058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data, peer->addr, ETH_ALEN);
4068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data + ETH_ALEN, own_addr, ETH_ALEN);
4078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
4088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(data + 2 * ETH_ALEN, bssid, ETH_ALEN);
4098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: KDF Context", data, sizeof(data));
4108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data),
4128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (u8 *) &peer->tpk, sizeof(peer->tpk));
4138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-KCK",
4148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			peer->tpk.kck, sizeof(peer->tpk.kck));
4158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-TK",
4168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			peer->tpk.tk, sizeof(peer->tpk.tk));
4178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->tpk_set = 1;
4188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
4228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
4238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @kck: TPK-KCK
4248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @lnkid: Pointer to the beginning of Link Identifier IE
4258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @rsnie: Pointer to the beginning of RSN IE used for handshake
4268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
4278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @ftie: Pointer to the beginning of FT IE
4288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @mic: Pointer for writing MIC
4298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
4308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Calculate MIC for TDLS frame.
4318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
4328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_ftie_mic(const u8 *kck, u8 trans_seq, const u8 *lnkid,
4338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     const u8 *rsnie, const u8 *timeoutie,
4348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     const u8 *ftie, u8 *mic)
4358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *buf, *pos;
4378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *_ftie;
4388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const struct wpa_tdls_lnkid *_lnkid;
4398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
4408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int len = 2 * ETH_ALEN + 1 + 2 + lnkid[1] + 2 + rsnie[1] +
4418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		2 + timeoutie[1] + 2 + ftie[1];
4428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	buf = os_zalloc(len);
4438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!buf) {
4448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
4458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
4468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
4478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = buf;
4498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	_lnkid = (const struct wpa_tdls_lnkid *) lnkid;
4508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 1) TDLS initiator STA MAC address */
4518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, _lnkid->init_sta, ETH_ALEN);
4528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += ETH_ALEN;
4538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 2) TDLS responder STA MAC address */
4548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, _lnkid->resp_sta, ETH_ALEN);
4558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += ETH_ALEN;
4568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 3) Transaction Sequence number */
4578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*pos++ = trans_seq;
4588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 4) Link Identifier IE */
4598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, lnkid, 2 + lnkid[1]);
4608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2 + lnkid[1];
4618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 5) RSN IE */
4628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, rsnie, 2 + rsnie[1]);
4638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2 + rsnie[1];
4648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 6) Timeout Interval IE */
4658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, timeoutie, 2 + timeoutie[1]);
4668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2 + timeoutie[1];
4678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 7) FTIE, with the MIC field of the FTIE set to 0 */
4688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, ftie, 2 + ftie[1]);
4698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	_ftie = (struct wpa_tdls_ftie *) pos;
4708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
4718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2 + ftie[1];
4728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
4748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
4758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = omac1_aes_128(kck, buf, pos - buf, mic);
4768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(buf);
4778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
4788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
4798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
4838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_tdls_key_mic_teardown - Calculate TDLS FTIE MIC for Teardown frame
4848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @kck: TPK-KCK
4858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @trans_seq: Transaction Sequence Number (4 - Teardown)
4868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @rcode: Reason code for Teardown
4878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @dtoken: Dialog Token used for that particular link
4888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @lnkid: Pointer to the beginning of Link Identifier IE
4898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @ftie: Pointer to the beginning of FT IE
4908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @mic: Pointer for writing MIC
4918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
4928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Calculate MIC for TDLS frame.
4938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
4948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_key_mic_teardown(const u8 *kck, u8 trans_seq, u16 rcode,
4958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     u8 dtoken, const u8 *lnkid,
4968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     const u8 *ftie, u8 *mic)
4978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *buf, *pos;
4998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *_ftie;
5008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
5018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int len;
5028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (lnkid == NULL)
5048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
5058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len = 2 + lnkid[1] + sizeof(rcode) + sizeof(dtoken) +
5078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		sizeof(trans_seq) + 2 + ftie[1];
5088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	buf = os_zalloc(len);
5108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!buf) {
5118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
5128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
5138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = buf;
5168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 1) Link Identifier IE */
5178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, lnkid, 2 + lnkid[1]);
5188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2 + lnkid[1];
5198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 2) Reason Code */
5208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE16(pos, rcode);
5218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += sizeof(rcode);
5228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 3) Dialog token */
5238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*pos++ = dtoken;
5248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 4) Transaction Sequence number */
5258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*pos++ = trans_seq;
5268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* 7) FTIE, with the MIC field of the FTIE set to 0 */
5278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, ftie, 2 + ftie[1]);
5288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	_ftie = (struct wpa_tdls_ftie *) pos;
5298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
5308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2 + ftie[1];
5318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
5338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
5348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = omac1_aes_128(kck, buf, pos - buf, mic);
5358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(buf);
5368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
5378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
5388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_supplicant_verify_tdls_mic(u8 trans_seq,
5428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  struct wpa_tdls_peer *peer,
5438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  const u8 *lnkid, const u8 *timeoutie,
5448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  const struct wpa_tdls_ftie *ftie)
5458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 mic[16];
5478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer->tpk_set) {
5498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_ftie_mic(peer->tpk.kck, trans_seq, lnkid,
5508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  peer->rsnie_p, timeoutie, (u8 *) ftie,
5518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  mic);
5528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(mic, ftie->mic, 16) != 0) {
5538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "TDLS: Invalid MIC in FTIE - "
5548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "dropping packet");
5558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_hexdump(MSG_DEBUG, "TDLS: Received MIC",
5568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    ftie->mic, 16);
5578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_hexdump(MSG_DEBUG, "TDLS: Calculated MIC",
5588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    mic, 16);
5598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
5608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
5618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
5628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_WARNING, "TDLS: Could not verify TDLS MIC, "
5638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TPK not set - dropping packet");
5648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
5658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
5678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_supplicant_verify_tdls_mic_teardown(
5718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 trans_seq, u16 rcode, u8 dtoken, struct wpa_tdls_peer *peer,
5728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *lnkid, const struct wpa_tdls_ftie *ftie)
5738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 mic[16];
5758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer->tpk_set) {
5778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_key_mic_teardown(peer->tpk.kck, trans_seq, rcode,
5788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  dtoken, lnkid, (u8 *) ftie, mic);
5798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(mic, ftie->mic, 16) != 0) {
5808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "TDLS: Invalid MIC in Teardown - "
5818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "dropping packet");
5828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
5838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
5848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
5858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Could not verify TDLS Teardown "
5868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "MIC, TPK not set - dropping packet");
5878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
5888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
5908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_tdls_tpk_timeout(void *eloop_ctx, void *timeout_ctx)
5948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_sm *sm = eloop_ctx;
5968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer = timeout_ctx;
5978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
5998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * On TPK lifetime expiration, we have an option of either tearing down
6008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * the direct link or trying to re-initiate it. The selection of what
6018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * to do is not strictly speaking controlled by our role in the expired
6028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * link, but for now, use that to select whether to renew or tear down
6038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * the link.
6048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
6058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer->initiator) {
6078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
6088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   " - try to renew", MAC2STR(peer->addr));
6098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_start(sm, peer->addr);
6108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
6118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
6128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   " - tear down", MAC2STR(peer->addr));
6131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_tdls_do_teardown(sm, peer,
6146a9f522e56dae5b1fff00642e27b82e82772dcf7Sunil Dutt				     WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
6158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
6208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Clear state for peer " MACSTR,
6228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(peer->addr));
6238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
6248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
6258da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	peer->reconfig_key = 0;
6268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->initiator = 0;
62773b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt	peer->tpk_in_progress = 0;
6288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(peer->sm_tmr.buf);
6298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->sm_tmr.buf = NULL;
630f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	os_free(peer->ht_capabilities);
631f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	peer->ht_capabilities = NULL;
63233e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	os_free(peer->vht_capabilities);
63333e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	peer->vht_capabilities = NULL;
634f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	os_free(peer->ext_capab);
635f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	peer->ext_capab = NULL;
6368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->rsnie_i_len = peer->rsnie_p_len = 0;
6378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->cipher = 0;
6388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->tpk_set = peer->tpk_success = 0;
6398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&peer->tpk, 0, sizeof(peer->tpk));
6408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(peer->inonce, 0, WPA_NONCE_LEN);
6418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(peer->rnonce, 0, WPA_NONCE_LEN);
6428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_tdls_linkid(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
6468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    struct wpa_tdls_lnkid *lnkid)
6478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lnkid->ie_type = WLAN_EID_LINK_ID;
6498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lnkid->ie_len = 3 * ETH_ALEN;
6508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(lnkid->bssid, sm->bssid, ETH_ALEN);
6518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer->initiator) {
6528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(lnkid->init_sta, sm->own_addr, ETH_ALEN);
6538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(lnkid->resp_sta, peer->addr, ETH_ALEN);
6548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
6558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(lnkid->init_sta, peer->addr, ETH_ALEN);
6568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(lnkid->resp_sta, sm->own_addr, ETH_ALEN);
6578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtint wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr, u16 reason_code)
6628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer;
6648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *ftie;
6658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_lnkid lnkid;
6668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 dialog_token;
6678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *rbuf, *pos;
6688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ielen;
6698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (sm->tdls_disabled || !sm->tdls_supported)
6718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
6728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Find the node and free from the list */
6748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (peer = sm->tdls; peer; peer = peer->next) {
6758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
6768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
6778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer == NULL) {
6808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
6818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Teardown " MACSTR, MAC2STR(addr));
6828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
6838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dialog_token = peer->dtoken;
6868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown for " MACSTR,
6888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(addr));
6898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ielen = 0;
6918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_tdls_get_privacy(sm) && peer->tpk_set && peer->tpk_success) {
6928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* To add FTIE for Teardown request and compute MIC */
6938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ielen += sizeof(*ftie);
6948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
6958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (tdls_testing & TDLS_TESTING_LONG_FRAME)
6968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			ielen += 170;
6978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
6988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rbuf = os_zalloc(ielen + 1);
7018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (rbuf == NULL)
7028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
7038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = rbuf;
7048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
70551b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt	if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
7068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_ies;
7078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie = (struct wpa_tdls_ftie *) pos;
7098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
7108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Using the recent nonce which should be for CONFIRM frame */
7118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
7128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
7138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
7148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = (u8 *) (ftie + 1);
7158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
7168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
7178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
7188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "FTIE");
7198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ftie->ie_len += 170;
7208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 255; /* FTIE subelem */
7218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 168; /* FTIE subelem length */
7221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		pos += 168;
7238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
7248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
7258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TDLS Teardown handshake",
7261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    (u8 *) ftie, pos - (u8 *) ftie);
7278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* compute MIC before sending */
7298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_linkid(sm, peer, &lnkid);
7308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_key_mic_teardown(peer->tpk.kck, 4, reason_code,
7318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  dialog_token, (u8 *) &lnkid, (u8 *) ftie,
7328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  ftie->mic);
7338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_ies:
7358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* TODO: register for a Timeout handler, if Teardown is not received at
7368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * the other end, then try again another time */
7378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* request driver to send Teardown using this FTIE */
7398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_TEARDOWN, 0,
7404b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt			  reason_code, rbuf, pos - rbuf);
7418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(rbuf);
7428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* clear the Peerkey statemachine */
7448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_peer_free(sm, peer);
7458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
7478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
7488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtint wpa_tdls_teardown_link(struct wpa_sm *sm, const u8 *addr, u16 reason_code)
7511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
7521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_tdls_peer *peer;
7531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (sm->tdls_disabled || !sm->tdls_supported)
7551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
7561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	for (peer = sm->tdls; peer; peer = peer->next) {
7581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
7591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			break;
7601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
7611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (peer == NULL) {
7631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Could not find peer " MACSTR
7641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   " for link Teardown", MAC2STR(addr));
7651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
7661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
7671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!peer->tpk_success) {
7691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
7701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   " not connected - cannot Teardown link", MAC2STR(addr));
7711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
7721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
7731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7746a9f522e56dae5b1fff00642e27b82e82772dcf7Sunil Dutt	return wpa_tdls_do_teardown(sm, peer, reason_code);
7751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
7761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
77838ffd8879e7990491033bf20568212e8c3472b48Sunil Duttstatic void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
77938ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt				       struct wpa_tdls_peer *peer)
78038ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt{
78138ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt	wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
78238ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt	wpa_tdls_peer_free(sm, peer);
78338ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt}
78438ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt
78538ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt
7861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtvoid wpa_tdls_disable_link(struct wpa_sm *sm, const u8 *addr)
7871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
7881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_tdls_peer *peer;
7891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	for (peer = sm->tdls; peer; peer = peer->next) {
7911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
7921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			break;
7931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
7941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
79538ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt	if (peer)
79638ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt		wpa_tdls_disable_peer_link(sm, peer);
7971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
7981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_recv_teardown(struct wpa_sm *sm, const u8 *src_addr,
8018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  const u8 *buf, size_t len)
8028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
8038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer = NULL;
8048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *ftie;
8058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_lnkid *lnkid;
8068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_eapol_ie_parse kde;
8078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 reason_code;
8088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *pos;
8098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ielen;
8108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Find the node and free from the list */
8128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (peer = sm->tdls; peer; peer = peer->next) {
8138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
8148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
8158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer == NULL) {
8188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
8198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Teardown " MACSTR, MAC2STR(src_addr));
8208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
8218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = buf;
8248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
8258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reason_code = WPA_GET_LE16(pos);
8278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2;
8288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown Request from " MACSTR
8308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   " (reason code %u)", MAC2STR(src_addr), reason_code);
8318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ielen = len - (pos - buf); /* start of IE in buf */
8338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
8348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in Teardown");
8358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
8368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
8398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TDLS "
8408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Teardown");
8418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
8428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
8448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
8468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_ftie;
8478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
8498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No FTIE in TDLS Teardown");
8508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
8518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie = (struct wpa_tdls_ftie *) kde.ftie;
8548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Process MIC check to see if TDLS Teardown is right */
8568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_supplicant_verify_tdls_mic_teardown(4, reason_code,
8578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						    peer->dtoken, peer,
8588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						    (u8 *) lnkid, ftie) < 0) {
8598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: MIC failure for TDLS "
8608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Teardown Request from " MACSTR, MAC2STR(src_addr));
8618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
8628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_ftie:
8658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
8668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Request the driver to disable the direct link and clear associated
8678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * keys.
8688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
86938ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt	wpa_tdls_disable_peer_link(sm, peer);
8708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
8718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
8728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
8758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_tdls_send_error - To send suitable TDLS status response with
8768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *	appropriate status code mentioning reason for error/failure.
8778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @dst 	- MAC addr of Peer station
8788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @tdls_action - TDLS frame type for which error code is sent
8798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @status 	- status code mentioning reason
8808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
8818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_send_error(struct wpa_sm *sm, const u8 *dst,
8838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       u8 tdls_action, u8 dialog_token, u16 status)
8848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
8858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Sending error to " MACSTR
8868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   " (action=%u status=%u)",
8878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(dst), tdls_action, status);
8888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wpa_tdls_tpk_send(sm, dst, tdls_action, dialog_token, status,
8898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 NULL, 0);
8908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
8918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic struct wpa_tdls_peer *
8944b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtwpa_tdls_add_peer(struct wpa_sm *sm, const u8 *addr, int *existing)
8951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
8961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_tdls_peer *peer;
8971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8984b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	if (existing)
8994b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		*existing = 0;
9004b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	for (peer = sm->tdls; peer; peer = peer->next) {
9014b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0) {
9024b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt			if (existing)
9034b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				*existing = 1;
9044b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt			return peer; /* re-use existing entry */
9054b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		}
9064b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	}
9074b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
9081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_INFO, "TDLS: Creating peer entry for " MACSTR,
9091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   MAC2STR(addr));
9101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	peer = os_zalloc(sizeof(*peer));
9121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (peer == NULL)
9131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
9141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memcpy(peer->addr, addr, ETH_ALEN);
9161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	peer->next = sm->tdls;
9171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	sm->tdls = peer;
9181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return peer;
9201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
9211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_send_tpk_m1(struct wpa_sm *sm,
9248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				struct wpa_tdls_peer *peer)
9258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
9268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t buf_len;
9278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_timeoutie timeoutie;
9288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 rsn_capab;
9298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *ftie;
9308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *rbuf, *pos, *count_pos;
9318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 count;
9328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_ie_hdr *hdr;
933d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	int status;
9348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wpa_tdls_get_privacy(sm)) {
9368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: No security used on the link");
9378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->rsnie_i_len = 0;
9388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_rsnie;
9398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
9408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
9428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * TPK Handshake Message 1:
9438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * FTIE: ANonce=0, SNonce=initiator nonce MIC=0, DataKDs=(RSNIE_I,
9448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Timeout Interval IE))
9458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
9468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Filling RSN IE */
9488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
9498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->elem_id = WLAN_EID_RSN;
9508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE16(hdr->version, RSN_VERSION);
9518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = (u8 *) (hdr + 1);
9538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
9548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += RSN_SELECTOR_LEN;
9558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	count_pos = pos;
9568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2;
9578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	count = 0;
9598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
9618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * AES-CCMP is the default Encryption preferred for TDLS, so
9628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * RSN IE is filled only with CCMP CIPHER
9638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Note: TKIP is not used to encrypt TDLS link.
9648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 *
9658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Regardless of the cipher used on the AP connection, select CCMP
9668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * here.
9678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
9688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
9698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += RSN_SELECTOR_LEN;
9708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	count++;
9718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE16(count_pos, count);
9738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE16(pos, 1);
9758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2;
9768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
9778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += RSN_SELECTOR_LEN;
9788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
9808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
9818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
9828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
9838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Use alternative RSN IE for "
9848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "testing");
9858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
9868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
9878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
9888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE16(pos, rsn_capab);
9898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2;
9908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
9918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
9928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Number of PMKIDs */
9938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 0x00;
9948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 0x00;
9958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
9968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
9978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->len = (pos - peer->rsnie_i) - 2;
9998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->rsnie_i_len = pos - peer->rsnie_i;
10008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
10018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    peer->rsnie_i, peer->rsnie_i_len);
10028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_rsnie:
10048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	buf_len = 0;
10058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_tdls_get_privacy(sm))
10068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
10078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			sizeof(struct wpa_tdls_timeoutie);
10088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
10098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_tdls_get_privacy(sm) &&
10108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (tdls_testing & TDLS_TESTING_LONG_FRAME))
10118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		buf_len += 170;
10128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_DIFF_BSSID)
10138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		buf_len += sizeof(struct wpa_tdls_lnkid);
10148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
10158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rbuf = os_zalloc(buf_len + 1);
10168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (rbuf == NULL) {
10178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_peer_free(sm, peer);
10188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
10198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
10208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = rbuf;
10218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wpa_tdls_get_privacy(sm))
10238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_ies;
10248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Initiator RSN IE */
10268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
10278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie = (struct wpa_tdls_ftie *) pos;
10298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
10308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
10318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_get_random(peer->inonce, WPA_NONCE_LEN)) {
10338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
10348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			"TDLS: Failed to get random data for initiator Nonce");
10358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(rbuf);
10368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_peer_free(sm, peer);
10378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
10388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
10398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Initiator Nonce for TPK handshake",
10408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    peer->inonce, WPA_NONCE_LEN);
10418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
10428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK Handshake M1",
10448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    (u8 *) ftie, sizeof(struct wpa_tdls_ftie));
10458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = (u8 *) (ftie + 1);
10478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
10498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
10508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
10518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "FTIE");
10528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ftie->ie_len += 170;
10538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 255; /* FTIE subelem */
10548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 168; /* FTIE subelem length */
10558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += 168;
10568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
10578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
10588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Lifetime */
10608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->lifetime = TPK_LIFETIME;
10618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
10628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_SHORT_LIFETIME) {
10638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - use short TPK "
10648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "lifetime");
10658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->lifetime = 301;
10668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
10678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_LONG_LIFETIME) {
10688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - use long TPK "
10698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "lifetime");
10708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->lifetime = 0xffffffff;
10718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
10728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
10738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
10748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     sizeof(timeoutie), peer->lifetime);
10758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
10768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_ies:
10788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
10808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_DIFF_BSSID) {
10818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - use incorrect BSSID in "
10828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Link Identifier");
10838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct wpa_tdls_lnkid *l = (struct wpa_tdls_lnkid *) pos;
10848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_linkid(sm, peer, l);
10858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		l->bssid[5] ^= 0x01;
10868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += sizeof(*l);
10878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
10888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
10898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Request / TPK "
10918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "Handshake Message 1 (peer " MACSTR ")",
10928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(peer->addr));
10938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1094d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_SETUP_REQUEST,
1095d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt				   1, 0, rbuf, pos - rbuf);
10968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(rbuf);
10978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1098d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	return status;
10998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
11008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_send_tpk_m2(struct wpa_sm *sm,
11038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				const unsigned char *src_addr, u8 dtoken,
11048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				struct wpa_tdls_lnkid *lnkid,
11058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				const struct wpa_tdls_peer *peer)
11068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
11078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *rbuf, *pos;
11088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t buf_len;
11098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 lifetime;
11108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_timeoutie timeoutie;
11118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *ftie;
1112d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	int status;
11138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	buf_len = 0;
11158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_tdls_get_privacy(sm)) {
11168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
11178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Lifetime */
11188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
11198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			sizeof(struct wpa_tdls_timeoutie);
11208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
11218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (tdls_testing & TDLS_TESTING_LONG_FRAME)
11228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			buf_len += 170;
11238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
11248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rbuf = os_zalloc(buf_len + 1);
11278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (rbuf == NULL)
11288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
11298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = rbuf;
11308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wpa_tdls_get_privacy(sm))
11328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_ies;
11338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Peer RSN IE */
11358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
11368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie = (struct wpa_tdls_ftie *) pos;
11388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
11398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* TODO: ftie->mic_control to set 2-RESPONSE */
11408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
11418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
11428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
11438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK M2",
11448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    (u8 *) ftie, sizeof(*ftie));
11458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = (u8 *) (ftie + 1);
11478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
11498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
11508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
11518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "FTIE");
11528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ftie->ie_len += 170;
11538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 255; /* FTIE subelem */
11548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 168; /* FTIE subelem length */
11558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += 168;
11568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
11588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Lifetime */
11608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lifetime = peer->lifetime;
11618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
11628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_RESP) {
11638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
11648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "lifetime in response");
11658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		lifetime++;
11668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
11688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
11698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     sizeof(timeoutie), lifetime);
11708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds from initiator",
11718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   lifetime);
11728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* compute MIC before sending */
11748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_ftie_mic(peer->tpk.kck, 2, (u8 *) lnkid, peer->rsnie_p,
11758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
11768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_ies:
1178d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE,
1179d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt				   dtoken, 0, rbuf, pos - rbuf);
11808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(rbuf);
11818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1182d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	return status;
11838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
11848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_send_tpk_m3(struct wpa_sm *sm,
11878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				const unsigned char *src_addr, u8 dtoken,
11888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				struct wpa_tdls_lnkid *lnkid,
11898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				const struct wpa_tdls_peer *peer)
11908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
11918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *rbuf, *pos;
11928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t buf_len;
11938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *ftie;
11948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_timeoutie timeoutie;
11958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 lifetime;
1196d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	int status;
11978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	buf_len = 0;
11998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_tdls_get_privacy(sm)) {
12008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
12018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Lifetime */
12028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
12038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			sizeof(struct wpa_tdls_timeoutie);
12048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
12058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (tdls_testing & TDLS_TESTING_LONG_FRAME)
12068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			buf_len += 170;
12078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
12088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rbuf = os_zalloc(buf_len + 1);
12118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (rbuf == NULL)
12128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
12138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = rbuf;
12148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wpa_tdls_get_privacy(sm))
12168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_ies;
12178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Peer RSN IE */
12198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
12208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie = (struct wpa_tdls_ftie *) pos;
12228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
12238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*TODO: ftie->mic_control to set 3-CONFIRM */
12248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
12258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
12268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
12278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = (u8 *) (ftie + 1);
12298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
12318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
12328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
12338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "FTIE");
12348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ftie->ie_len += 170;
12358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 255; /* FTIE subelem */
12368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*pos++ = 168; /* FTIE subelem length */
12378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += 168;
12388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
12408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Lifetime */
12428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lifetime = peer->lifetime;
12438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
12448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_CONF) {
12458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
12468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "lifetime in confirm");
12478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		lifetime++;
12488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
12508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
12518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     sizeof(timeoutie), lifetime);
12528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds",
12538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   lifetime);
12548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* compute MIC before sending */
12568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_ftie_mic(peer->tpk.kck, 3, (u8 *) lnkid, peer->rsnie_p,
12578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
12588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_ies:
1260d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
1261d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt				   dtoken, 0, rbuf, pos - rbuf);
12628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(rbuf);
12638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1264d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	return status;
12658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
12668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_tdls_send_discovery_response(struct wpa_sm *sm,
12691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					    struct wpa_tdls_peer *peer,
12701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					    u8 dialog_token)
12711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
12721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Discovery Response "
12731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "(peer " MACSTR ")", MAC2STR(peer->addr));
12741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
12751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_DISCOVERY_RESPONSE,
12761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				 dialog_token, 0, NULL, 0);
12771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
12781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
12791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
12801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int
12811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtwpa_tdls_process_discovery_request(struct wpa_sm *sm, const u8 *addr,
12821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   const u8 *buf, size_t len)
12831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
12841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_eapol_ie_parse kde;
12851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	const struct wpa_tdls_lnkid *lnkid;
12861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_tdls_peer *peer;
12871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	size_t min_req_len = sizeof(struct wpa_tdls_frame) +
12881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		1 /* dialog token */ + sizeof(struct wpa_tdls_lnkid);
12891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u8 dialog_token;
12901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
12911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from " MACSTR,
12921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   MAC2STR(addr));
12931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
12941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (len < min_req_len) {
12951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS Discovery Request is too short: "
12961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "%d", (int) len);
12971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
12981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
12991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dialog_token = buf[sizeof(struct wpa_tdls_frame)];
13011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (wpa_supplicant_parse_ies(buf + sizeof(struct wpa_tdls_frame) + 1,
13031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     len - (sizeof(struct wpa_tdls_frame) + 1),
13041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     &kde) < 0)
13051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
13061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!kde.lnkid) {
13081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Link ID not found in Discovery "
13091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "Request");
13101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
13111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
13121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	lnkid = (const struct wpa_tdls_lnkid *) kde.lnkid;
13141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
13161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from different "
13171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   " BSS " MACSTR, MAC2STR(lnkid->bssid));
13181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
13191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
13201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13214b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	peer = wpa_tdls_add_peer(sm, addr, NULL);
13221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (peer == NULL)
13231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
13241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return wpa_tdls_send_discovery_response(sm, peer, dialog_token);
13261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
13271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtint wpa_tdls_send_discovery_request(struct wpa_sm *sm, const u8 *addr)
13301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
13311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (sm->tdls_disabled || !sm->tdls_supported)
13321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
13331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Sending Discovery Request to peer "
13351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   MACSTR, MAC2STR(addr));
13361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_DISCOVERY_REQUEST,
13371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				 1, 0, NULL, 0);
13381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
13391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int copy_supp_rates(const struct wpa_eapol_ie_parse *kde,
13421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   struct wpa_tdls_peer *peer)
13431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
13441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!kde->supp_rates) {
13451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: No supported rates received");
13461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
13471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
1348a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	peer->supp_rates_len = merge_byte_arrays(
1349a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		peer->supp_rates, sizeof(peer->supp_rates),
1350a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		kde->supp_rates + 2, kde->supp_rates_len - 2,
135134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		kde->ext_supp_rates ? kde->ext_supp_rates + 2 : NULL,
135234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		kde->ext_supp_rates_len - 2);
13531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
13541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
13551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
13561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1357f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidtstatic int copy_peer_ht_capab(const struct wpa_eapol_ie_parse *kde,
1358f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			      struct wpa_tdls_peer *peer)
1359f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt{
1360f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (!kde->ht_capabilities ||
1361f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	    kde->ht_capabilities_len <
1362f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	    sizeof(struct ieee80211_ht_capabilities) ) {
1363f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: No supported ht capabilities "
1364f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			   "received");
1365f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		return 0;
1366f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	}
1367f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1368f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (!peer->ht_capabilities) {
1369f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		peer->ht_capabilities =
1370f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt                        os_zalloc(sizeof(struct ieee80211_ht_capabilities));
1371f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		if (peer->ht_capabilities == NULL)
1372f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt                        return -1;
1373f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	}
1374f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1375f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	os_memcpy(peer->ht_capabilities, kde->ht_capabilities,
1376f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt                  sizeof(struct ieee80211_ht_capabilities));
1377f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Peer HT capabilities",
1378f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		    (u8 *) peer->ht_capabilities,
1379f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		    sizeof(struct ieee80211_ht_capabilities));
1380f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1381f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	return 0;
1382f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt}
1383f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1384f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
138533e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidtstatic int copy_peer_vht_capab(const struct wpa_eapol_ie_parse *kde,
138633e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt			      struct wpa_tdls_peer *peer)
138733e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt{
138833e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	if (!kde->vht_capabilities ||
138933e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	    kde->vht_capabilities_len <
139033e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	    sizeof(struct ieee80211_vht_capabilities) ) {
139133e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: No supported vht capabilities "
139233e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt			   "received");
139333e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt		return 0;
139433e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	}
139533e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt
139633e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	if (!peer->vht_capabilities) {
139733e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt		peer->vht_capabilities =
139833e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt                        os_zalloc(sizeof(struct ieee80211_vht_capabilities));
139933e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt		if (peer->vht_capabilities == NULL)
140033e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt                        return -1;
140133e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	}
140233e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt
140333e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	os_memcpy(peer->vht_capabilities, kde->vht_capabilities,
140433e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt                  sizeof(struct ieee80211_vht_capabilities));
140533e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Peer VHT capabilities",
140633e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt		    (u8 *) peer->vht_capabilities,
140733e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt		    sizeof(struct ieee80211_vht_capabilities));
140833e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt
140933e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	return 0;
141033e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt}
141133e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt
141233e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt
1413f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidtstatic int copy_peer_ext_capab(const struct wpa_eapol_ie_parse *kde,
1414f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			       struct wpa_tdls_peer *peer)
1415f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt{
1416f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (!kde->ext_capab) {
1417f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: No extended capabilities "
1418f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			   "received");
1419f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		return 0;
1420f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	}
1421f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1422f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (!peer->ext_capab || peer->ext_capab_len < kde->ext_capab_len - 2) {
1423f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		/* Need to allocate buffer to fit the new information */
1424f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		os_free(peer->ext_capab);
1425f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		peer->ext_capab = os_zalloc(kde->ext_capab_len - 2);
1426f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		if (peer->ext_capab == NULL)
1427f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			return -1;
1428f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	}
1429f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1430f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	peer->ext_capab_len = kde->ext_capab_len - 2;
1431f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	os_memcpy(peer->ext_capab, kde->ext_capab + 2, peer->ext_capab_len);
1432f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1433f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	return 0;
1434f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt}
1435f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1436f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
14378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_process_tpk_m1(struct wpa_sm *sm, const u8 *src_addr,
14388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   const u8 *buf, size_t len)
14398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer;
14418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_eapol_ie_parse kde;
14428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_ie_data ie;
14438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int cipher;
14448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *cpos;
14458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *ftie = NULL;
14468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_timeoutie *timeoutie;
14478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_lnkid *lnkid;
14488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 lifetime = 0;
14498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#if 0
14508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_ie_hdr *hdr;
14518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *pos;
14528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 rsn_capab;
14538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 rsn_ver;
14548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif
14558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 dtoken;
14568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 ielen;
14578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
14588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int tdls_prohibited = sm->tdls_prohibited;
14591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int existing_peer = 0;
14608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len < 3 + 3)
14628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
14638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	cpos = buf;
14658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	cpos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
14668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* driver had already verified the frame format */
14688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dtoken = *cpos++; /* dialog token */
14698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_INFO, "TDLS: Dialog Token in TPK M1 %d", dtoken);
14718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14724b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	peer = wpa_tdls_add_peer(sm, src_addr, &existing_peer);
14734b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	if (peer == NULL)
14744b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		goto error;
14751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
14760ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	/* If found, use existing entry instead of adding a new one;
14770ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	 * how to handle the case where both ends initiate at the
14780ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	 * same time? */
14790ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	if (existing_peer) {
14800ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		if (peer->tpk_success) {
14810ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			wpa_printf(MSG_DEBUG, "TDLS: TDLS Setup Request while "
14820ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				   "direct link is enabled - tear down the "
14830ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				   "old link first");
14848b43e82b5aa16d167d3e2696b7827c412b0e0a00Sunil Dutt			wpa_tdls_disable_peer_link(sm, peer);
14850ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		}
14860ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt
14870ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		/*
14880ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		 * An entry is already present, so check if we already sent a
14890ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		 * TDLS Setup Request. If so, compare MAC addresses and let the
14900ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		 * STA with the lower MAC address continue as the initiator.
14910ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		 * The other negotiation is terminated.
14920ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		 */
14930ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		if (peer->initiator) {
14940ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			if (os_memcmp(sm->own_addr, src_addr, ETH_ALEN) < 0) {
14950ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				wpa_printf(MSG_DEBUG, "TDLS: Discard request "
14960ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt					   "from peer with higher address "
14970ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt					   MACSTR, MAC2STR(src_addr));
14980ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				return -1;
14990ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			} else {
15000ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				wpa_printf(MSG_DEBUG, "TDLS: Accept request "
15010ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt					   "from peer with lower address "
15020ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt					   MACSTR " (terminate previously "
15030ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt					   "initiated negotiation",
15040ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt					   MAC2STR(src_addr));
15058b43e82b5aa16d167d3e2696b7827c412b0e0a00Sunil Dutt				wpa_tdls_disable_peer_link(sm, peer);
15060ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			}
15070ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		}
15080ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	}
15090ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt
15101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* capability information */
15111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	peer->capability = WPA_GET_LE16(cpos);
15121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	cpos += 2;
15138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ielen = len - (cpos - buf); /* start of IE in buf */
15158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_supplicant_parse_ies(cpos, ielen, &kde) < 0) {
15168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in TPK M1");
15178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
15188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
15218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
15228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TPK M1");
15238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
15248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M1",
15268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    kde.lnkid, kde.lnkid_len);
15278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
15288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
15298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: TPK M1 from diff BSS");
15308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_NOT_IN_SAME_BSS;
15318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
15328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TPK M1 - TPK initiator " MACSTR,
15358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(src_addr));
15368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (copy_supp_rates(&kde, peer) < 0)
15381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto error;
15391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1540f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (copy_peer_ht_capab(&kde, peer) < 0)
1541f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		goto error;
1542f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
154333e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	if (copy_peer_vht_capab(&kde, peer) < 0)
154433e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt		goto error;
154533e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt
1546f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (copy_peer_ext_capab(&kde, peer) < 0)
1547f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		goto error;
1548f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1549f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	peer->qos_info = kde.qosinfo;
1550f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
155151b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt	peer->aid = kde.aid;
155251b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt
15538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
15548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
15554b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		peer = wpa_tdls_add_peer(sm, src_addr, NULL);
15564b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		if (peer == NULL)
15574b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt			goto error;
15588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing concurrent initiation of "
15598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TDLS setup - send own request");
15608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->initiator = 1;
15618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_send_tpk_m1(sm, peer);
15628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
15658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    tdls_prohibited) {
15668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
15678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "on TDLS");
15688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		tdls_prohibited = 0;
15698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
15718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_prohibited) {
15738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: TDLS prohibited in this BSS");
15748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_REQUEST_DECLINED;
15758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
15768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wpa_tdls_get_privacy(sm)) {
15798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (kde.rsn_ie) {
15808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M1 while "
15818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "security is disabled");
15828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			status = WLAN_STATUS_SECURITY_DISABLED;
15838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto error;
15848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
15858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_rsn;
15868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
15898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    kde.rsn_ie == NULL) {
15908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M1");
15918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_INVALID_PARAMETERS;
15928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
15938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
15968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Too long Initiator RSN IE in "
15978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TPK M1");
15988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_INVALID_RSNIE;
15998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
16008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
16038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M1");
16048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_INVALID_RSNIE;
16058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
16068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	cipher = ie.pairwise_cipher;
16098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cipher & WPA_CIPHER_CCMP) {
16108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
16118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cipher = WPA_CIPHER_CCMP;
16128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
16138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M1");
16148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
16158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
16168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if ((ie.capabilities &
16198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	     (WPA_CAPABILITY_NO_PAIRWISE | WPA_CAPABILITY_PEERKEY_ENABLED)) !=
16208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    WPA_CAPABILITY_PEERKEY_ENABLED) {
16218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Invalid RSN Capabilities in "
16228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TPK M1");
16238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_INVALID_RSN_IE_CAPAB;
16248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
16258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Lifetime */
16288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.key_lifetime == NULL) {
16298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M1");
16308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
16318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
16328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
16348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lifetime = WPA_GET_LE32(timeoutie->value);
16358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", lifetime);
16368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (lifetime < 300) {
16378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Too short TPK lifetime");
16388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
16398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
16408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_rsn:
16431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef CONFIG_TDLS_TESTING
16441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
16451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (os_memcmp(sm->own_addr, peer->addr, ETH_ALEN) < 0) {
16461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			/*
16471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * The request frame from us is going to win, so do not
16481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * replace information based on this request frame from
16491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * the peer.
16501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 */
16511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			goto skip_rsn_check;
16521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
16531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
16541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
16551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
16568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->initiator = 0; /* Need to check */
16578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->dtoken = dtoken;
16588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wpa_tdls_get_privacy(sm)) {
16608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->rsnie_i_len = 0;
16618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->rsnie_p_len = 0;
16628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->cipher = WPA_CIPHER_NONE;
16638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_rsn_check;
16648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie = (struct wpa_tdls_ftie *) kde.ftie;
16678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(peer->rsnie_i, kde.rsn_ie, kde.rsn_ie_len);
16688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->rsnie_i_len = kde.rsn_ie_len;
16698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->cipher = cipher;
16708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16716102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt	if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
16726102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		/*
16736102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		 * There is no point in updating the RNonce for every obtained
16746102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		 * TPK M1 frame (e.g., retransmission due to timeout) with the
16756102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		 * same INonce (SNonce in FTIE). However, if the TPK M1 is
16766102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		 * retransmitted with a different INonce, update the RNonce
16776102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		 * since this is for a new TDLS session.
16786102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		 */
16796102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		wpa_printf(MSG_DEBUG,
16806102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt			   "TDLS: New TPK M1 INonce - generate new RNonce");
16816102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		os_memcpy(peer->inonce, ftie->Snonce, WPA_NONCE_LEN);
16826102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		if (os_get_random(peer->rnonce, WPA_NONCE_LEN)) {
16836102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt			wpa_msg(sm->ctx->ctx, MSG_WARNING,
16846102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt				"TDLS: Failed to get random data for responder nonce");
16856102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt			wpa_tdls_peer_free(sm, peer);
16866102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt			goto error;
16876102472da23e48cb1406b87ec767f4fd9a40a85dSunil Dutt		}
16888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#if 0
16918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* get version info from RSNIE received from Peer */
16928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr = (struct rsn_ie_hdr *) kde.rsn_ie;
16938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rsn_ver = WPA_GET_LE16(hdr->version);
16948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* use min(peer's version, out version) */
16968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (rsn_ver > RSN_VERSION)
16978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		rsn_ver = RSN_VERSION;
16988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr = (struct rsn_ie_hdr *) peer->rsnie_p;
17008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->elem_id = WLAN_EID_RSN;
17028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE16(hdr->version, rsn_ver);
17038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = (u8 *) (hdr + 1);
17048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
17068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += RSN_SELECTOR_LEN;
17078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Include only the selected cipher in pairwise cipher suite */
17088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE16(pos, 1);
17098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2;
17108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cipher == WPA_CIPHER_CCMP)
17118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
17128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += RSN_SELECTOR_LEN;
17138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE16(pos, 1);
17158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2;
17168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
17178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += RSN_SELECTOR_LEN;
17188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
17208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
17218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE16(pos, rsn_capab);
17228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2;
17238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->len = (pos - peer->rsnie_p) - 2;
17258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->rsnie_p_len = pos - peer->rsnie_p;
17268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif
17278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* temp fix: validation of RSNIE later */
17298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(peer->rsnie_p, peer->rsnie_i, peer->rsnie_i_len);
17308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->rsnie_p_len = peer->rsnie_i_len;
17318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
17338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    peer->rsnie_p, peer->rsnie_p_len);
17348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->lifetime = lifetime;
17368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
17388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_rsn_check:
17401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* add the peer to the driver as a "setup in progress" peer */
174151b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt	wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL, NULL, 0,
1742f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt				NULL, 0);
174373b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt	peer->tpk_in_progress = 1;
17441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Response / TPK M2");
17461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (wpa_tdls_send_tpk_m2(sm, src_addr, dtoken, lnkid, peer) < 0) {
1747d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt		wpa_tdls_disable_peer_link(sm, peer);
17481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto error;
17491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
17508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
17528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidterror:
17548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE, dtoken,
17558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    status);
17568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
17578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
17588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17608da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidtstatic int wpa_tdls_enable_link(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
17618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
17628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->tpk_success = 1;
176373b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt	peer->tpk_in_progress = 0;
17648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
17658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_tdls_get_privacy(sm)) {
17668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u32 lifetime = peer->lifetime;
17678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
17688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Start the initiator process a bit earlier to avoid race
17698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * condition with the responder sending teardown request.
17708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
17718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (lifetime > 3 && peer->initiator)
17728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			lifetime -= 3;
17738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_register_timeout(lifetime, 0, wpa_tdls_tpk_timeout,
17748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       sm, peer);
17758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
17768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_NO_TPK_EXPIRATION) {
17778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - disable TPK "
17788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "expiration");
17798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
17808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
17818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
17828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
17831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1784f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	/* add supported rates, capabilities, and qos_info to the TDLS peer */
178551b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt	if (wpa_sm_tdls_peer_addset(sm, peer->addr, 0, peer->aid,
178651b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt				    peer->capability,
17878da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt				    peer->supp_rates, peer->supp_rates_len,
17888da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt				    peer->ht_capabilities,
17898da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt				    peer->vht_capabilities,
17908da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt				    peer->qos_info, peer->ext_capab,
17918da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt				    peer->ext_capab_len) < 0)
17928da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		return -1;
17931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17948da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	if (peer->reconfig_key && wpa_tdls_set_key(sm, peer) < 0) {
17958da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Could not configure key to the "
17968da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			   "driver");
17978da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		return -1;
17988da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	}
17998da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	peer->reconfig_key = 0;
18008da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt
18018da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	return wpa_sm_tdls_oper(sm, TDLS_ENABLE_LINK, peer->addr);
18028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
18038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_process_tpk_m2(struct wpa_sm *sm, const u8 *src_addr,
18068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   const u8 *buf, size_t len)
18078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
18088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer;
18098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_eapol_ie_parse kde;
18108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_ie_data ie;
18118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int cipher;
18128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *ftie;
18138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_timeoutie *timeoutie;
18148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_lnkid *lnkid;
18158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 lifetime;
18168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 dtoken;
18178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ielen;
18188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 status;
18198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *pos;
18208da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	int ret;
18218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Response / TPK M2 "
18238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "(Peer " MACSTR ")", MAC2STR(src_addr));
18248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (peer = sm->tdls; peer; peer = peer->next) {
18258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
18268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
18278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer == NULL) {
18298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
18308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TPK M2: " MACSTR, MAC2STR(src_addr));
18318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
18328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18330ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	if (!peer->initiator) {
18340ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		/*
18350ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		 * This may happen if both devices try to initiate TDLS at the
18360ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		 * same time and we accept the TPK M1 from the peer in
18370ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		 * wpa_tdls_process_tpk_m1() and clear our previous state.
18380ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		 */
18390ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: We were not the initiator, so "
18400ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			   "ignore TPK M2 from " MACSTR, MAC2STR(src_addr));
18410ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		return -1;
18420ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	}
18438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_REQUEST);
18448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1845adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt	if (len < 3 + 2 + 1) {
1846d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt		wpa_tdls_disable_peer_link(sm, peer);
18478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
1848adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt	}
1849adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt
18508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = buf;
18518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
18528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	status = WPA_GET_LE16(pos);
18538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2 /* status code */;
18548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (status != WLAN_STATUS_SUCCESS) {
18568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Status code in TPK M2: %u",
18578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   status);
1858d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt		wpa_tdls_disable_peer_link(sm, peer);
18598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
18608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	status = WLAN_STATUS_UNSPECIFIED_FAILURE;
18638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* TODO: need to verify dialog token matches here or in kernel */
18658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dtoken = *pos++; /* dialog token */
18668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Dialog Token in TPK M2 %d", dtoken);
18688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1869adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt	if (len < 3 + 2 + 1 + 2) {
1870d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt		wpa_tdls_disable_peer_link(sm, peer);
18718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
1872adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt	}
18731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
18741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* capability information */
18751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	peer->capability = WPA_GET_LE16(pos);
18761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	pos += 2;
18778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ielen = len - (pos - buf); /* start of IE in buf */
18798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_supplicant_parse_ies(pos, ielen, &kde) < 0) {
18808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in TPK M2");
18818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
18828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
18858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_testing & TDLS_TESTING_DECLINE_RESP) {
18868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - decline response");
18878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_REQUEST_DECLINED;
18888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
18898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
18918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
18938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
18948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TPK M2");
18958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
18968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M2",
18988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    kde.lnkid, kde.lnkid_len);
18998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
19008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
19028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: TPK M2 from different BSS");
19038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_NOT_IN_SAME_BSS;
19048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
19058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (copy_supp_rates(&kde, peer) < 0)
19081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto error;
19091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1910f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (copy_peer_ht_capab(&kde, peer) < 0)
1911f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		goto error;
1912f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
191333e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	if (copy_peer_vht_capab(&kde, peer) < 0)
191433e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt		goto error;
191533e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt
1916f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (copy_peer_ext_capab(&kde, peer) < 0)
1917f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		goto error;
1918f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
1919f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	peer->qos_info = kde.qosinfo;
1920f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
192151b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt	peer->aid = kde.aid;
192251b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt
19238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wpa_tdls_get_privacy(sm)) {
19248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->rsnie_p_len = 0;
19258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer->cipher = WPA_CIPHER_NONE;
19268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_rsn;
19278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
19308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    kde.rsn_ie == NULL) {
19318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M2");
19328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_INVALID_PARAMETERS;
19338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
19348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
19368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    kde.rsn_ie, kde.rsn_ie_len);
19378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
19398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * FIX: bitwise comparison of RSN IE is not the correct way of
19408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * validation this. It can be different, but certain fields must
19418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * match. Since we list only a single pairwise cipher in TPK M1, the
19428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * memcmp is likely to work in most cases, though.
19438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
19448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.rsn_ie_len != peer->rsnie_i_len ||
19458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    os_memcmp(peer->rsnie_i, kde.rsn_ie, peer->rsnie_i_len) != 0) {
19468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M2 does "
19478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "not match with RSN IE used in TPK M1");
19488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Sent in TPK M1",
19498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    peer->rsnie_i, peer->rsnie_i_len);
19508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
19518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    kde.rsn_ie, kde.rsn_ie_len);
19528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_INVALID_RSNIE;
19538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
19548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
19578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M2");
19588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_INVALID_RSNIE;
19598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
19608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	cipher = ie.pairwise_cipher;
19638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cipher == WPA_CIPHER_CCMP) {
19648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
19658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cipher = WPA_CIPHER_CCMP;
19668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
19678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M2");
19688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
19698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
19708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M2",
19738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    kde.ftie, sizeof(*ftie));
19748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie = (struct wpa_tdls_ftie *) kde.ftie;
19758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
19778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M2 does "
19788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "not match with FTIE SNonce used in TPK M1");
19798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Silently discard the frame */
19808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
19818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Responder Nonce and RSN IE */
19848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN);
19858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(peer->rsnie_p, kde.rsn_ie, kde.rsn_ie_len);
19868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->rsnie_p_len = kde.rsn_ie_len;
19878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->cipher = cipher;
19888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Lifetime */
19908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.key_lifetime == NULL) {
19918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M2");
19928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
19938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
19948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
19968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lifetime = WPA_GET_LE32(timeoutie->value);
19978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M2",
19988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   lifetime);
19998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (lifetime != peer->lifetime) {
20008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
20018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TPK M2 (expected %u)", lifetime, peer->lifetime);
20028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
20038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
20048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
20078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Process MIC check to see if TPK M2 is right */
20098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_supplicant_verify_tdls_mic(2, peer, (u8 *) lnkid,
20108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   (u8 *) timeoutie, ftie) < 0) {
20118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Discard the frame */
20128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_del_key(sm, peer);
201338ffd8879e7990491033bf20568212e8c3472b48Sunil Dutt		wpa_tdls_disable_peer_link(sm, peer);
20148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
20158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20178da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	if (wpa_tdls_set_key(sm, peer) < 0) {
20188da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		/*
20198da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		 * Some drivers may not be able to config the key prior to full
20208da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		 * STA entry having been configured.
20218da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		 */
20228da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
20238da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			   "STA entry is complete");
20248da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		peer->reconfig_key = 1;
20258da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	}
20268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_rsn:
20288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->dtoken = dtoken;
20298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Confirm / "
20318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "TPK Handshake Message 3");
2032d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	if (wpa_tdls_send_tpk_m3(sm, src_addr, dtoken, lnkid, peer) < 0) {
2033d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt		wpa_tdls_disable_peer_link(sm, peer);
2034d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt		return -1;
2035d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	}
20368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20378da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	ret = wpa_tdls_enable_link(sm, peer);
20388da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	if (ret < 0) {
20398da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
20408da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		wpa_tdls_do_teardown(sm, peer,
20416a9f522e56dae5b1fff00642e27b82e82772dcf7Sunil Dutt				     WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
20428da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	}
20438da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	return ret;
20448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidterror:
20468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM, dtoken,
20478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    status);
2048d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt	wpa_tdls_disable_peer_link(sm, peer);
20498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
20508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
20518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_process_tpk_m3(struct wpa_sm *sm, const u8 *src_addr,
20548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   const u8 *buf, size_t len)
20558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
20568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer;
20578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_eapol_ie_parse kde;
20588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_ftie *ftie;
20598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_timeoutie *timeoutie;
20608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_lnkid *lnkid;
20618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ielen;
20628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 status;
20638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *pos;
20648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 lifetime;
20658da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	int ret;
20668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Confirm / TPK M3 "
20688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "(Peer " MACSTR ")", MAC2STR(src_addr));
20698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (peer = sm->tdls; peer; peer = peer->next) {
20708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
20718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
20728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer == NULL) {
20748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
20758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TPK M3: " MACSTR, MAC2STR(src_addr));
20768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
20778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_RESPONSE);
20798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len < 3 + 3)
2081adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
20828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = buf;
20838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
20848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	status = WPA_GET_LE16(pos);
20868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (status != 0) {
20888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Status code in TPK M3: %u",
20898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   status);
2090adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
20918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2 /* status code */ + 1 /* dialog token */;
20938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ielen = len - (pos - buf); /* start of IE in buf */
20958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
20968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Failed to parse KDEs in TPK M3");
2097adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
20988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
21018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TPK M3");
2102adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M3",
21058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    (u8 *) kde.lnkid, kde.lnkid_len);
21068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
21078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
21098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: TPK M3 from diff BSS");
2110adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wpa_tdls_get_privacy(sm))
21148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_rsn;
21158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
21178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M3");
2118adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M3",
21218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    kde.ftie, sizeof(*ftie));
21228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ftie = (struct wpa_tdls_ftie *) kde.ftie;
21238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.rsn_ie == NULL) {
21258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No RSN IE in TPK M3");
2126adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M3",
21298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    kde.rsn_ie, kde.rsn_ie_len);
21308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.rsn_ie_len != peer->rsnie_p_len ||
21318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    os_memcmp(kde.rsn_ie, peer->rsnie_p, peer->rsnie_p_len) != 0) {
21328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M3 does not match "
21338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "with the one sent in TPK M2");
2134adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!os_memcmp(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN) == 0) {
21388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: FTIE ANonce in TPK M3 does "
21398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "not match with FTIE ANonce used in TPK M2");
2140adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
21448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M3 does not "
21458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "match with FTIE SNonce used in TPK M1");
2146adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (kde.key_lifetime == NULL) {
21508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M3");
2151adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
21548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Timeout IE Received from TPK M3",
21558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    (u8 *) timeoutie, sizeof(*timeoutie));
21568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lifetime = WPA_GET_LE32(timeoutie->value);
21578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M3",
21588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   lifetime);
21598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (lifetime != peer->lifetime) {
21608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
21618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TPK M3 (expected %u)", lifetime, peer->lifetime);
2162adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_supplicant_verify_tdls_mic(3, peer, (u8 *) lnkid,
21668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   (u8 *) timeoutie, ftie) < 0) {
21678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_del_key(sm, peer);
2168adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt		goto error;
21698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21718da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	if (wpa_tdls_set_key(sm, peer) < 0) {
21728da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		/*
21738da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		 * Some drivers may not be able to config the key prior to full
21748da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		 * STA entry having been configured.
21758da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		 */
21768da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
21778da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			   "STA entry is complete");
21788da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		peer->reconfig_key = 1;
21798da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	}
21808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_rsn:
21828da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	ret = wpa_tdls_enable_link(sm, peer);
21838da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	if (ret < 0) {
21848da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
21858da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		wpa_tdls_do_teardown(sm, peer,
21866a9f522e56dae5b1fff00642e27b82e82772dcf7Sunil Dutt				     WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
21878da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	}
21888da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	return ret;
2189adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutterror:
2190d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt	wpa_tdls_disable_peer_link(sm, peer);
2191adce9cf73c2237aff40a49fc3d0b315711f291d8Sunil Dutt	return -1;
21928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
21938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs)
21968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
21978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_timeoutie *lifetime = (struct wpa_tdls_timeoutie *) ie;
21988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(lifetime, 0, ie_len);
22008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lifetime->ie_type = WLAN_EID_TIMEOUT_INTERVAL;
22018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lifetime->ie_len = sizeof(struct wpa_tdls_timeoutie) - 2;
22028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	lifetime->interval_type = WLAN_TIMEOUT_KEY_LIFETIME;
22038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_LE32(lifetime->value, tsecs);
22048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, ie, ie_len);
22058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return pos + ie_len;
22068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
22078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
22108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_tdls_start - Initiate TDLS handshake (send TPK Handshake Message 1)
22118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @sm: Pointer to WPA state machine data from wpa_sm_init()
22128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @peer: MAC address of the peer STA
22138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 on success, or -1 on failure
22148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
22158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Send TPK Handshake Message 1 info to driver to start TDLS
22168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * handshake with the peer.
22178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
22188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_tdls_start(struct wpa_sm *sm, const u8 *addr)
22198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
22208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer;
22218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int tdls_prohibited = sm->tdls_prohibited;
22228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (sm->tdls_disabled || !sm->tdls_supported)
22248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
22258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_TDLS_TESTING
22278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
22288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    tdls_prohibited) {
22298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
22308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "on TDLS");
22318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		tdls_prohibited = 0;
22328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_TDLS_TESTING */
22348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tdls_prohibited) {
22368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: TDLS is prohibited in this BSS - "
22378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "reject request to start setup");
22388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
22398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22414b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	peer = wpa_tdls_add_peer(sm, addr, NULL);
22424b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	if (peer == NULL)
22434b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		return -1;
22448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
224573b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt	if (peer->tpk_in_progress) {
224673b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt		wpa_printf(MSG_DEBUG, "TDLS: Setup is already in progress with the peer");
224773b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt		return 0;
224873b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt	}
224973b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt
22508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer->initiator = 1;
22518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* add the peer to the driver as a "setup in progress" peer */
225351b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt	wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL, NULL, 0,
2254f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt				NULL, 0);
22551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
225673b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt	peer->tpk_in_progress = 1;
225773b28cc4bd1d5efa3534ad9e28365fea9a62ffd3Sunil Dutt
22581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (wpa_tdls_send_tpk_m1(sm, peer) < 0) {
2259d0ef38b52c069e34102b38dafbbc1fe055364ae8Sunil Dutt		wpa_tdls_disable_peer_link(sm, peer);
22601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
22611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
22621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
22631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
22648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
22658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22674b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtvoid wpa_tdls_remove(struct wpa_sm *sm, const u8 *addr)
22688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
22698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer;
22708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (sm->tdls_disabled || !sm->tdls_supported)
22724b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		return;
22738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (peer = sm->tdls; peer; peer = peer->next) {
22758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
22768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
22778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (peer == NULL || !peer->tpk_success)
22804b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		return;
22818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (sm->tdls_external_setup) {
22831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/*
22841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * Disable previous link to allow renegotiation to be completed
22851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * on AP path.
22861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 */
22871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
22881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
22898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
22908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
22938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_supplicant_rx_tdls - Receive TDLS data frame
22948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
22958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is called to receive TDLS (ethertype = 0x890d) data frames.
22968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
22978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_supplicant_rx_tdls(void *ctx, const u8 *src_addr,
22988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   const u8 *buf, size_t len)
22998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
23008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_sm *sm = ctx;
23018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_frame *tf;
23028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "TDLS: Received Data frame encapsulation",
23048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    buf, len);
23058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (sm->tdls_disabled || !sm->tdls_supported) {
23071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Discard message - TDLS disabled "
23081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "or unsupported by driver");
23098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
23108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(src_addr, sm->own_addr, ETH_ALEN) == 0) {
23138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Discard copy of own message");
23148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
23158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len < sizeof(*tf)) {
23188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Drop too short frame");
23198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
23208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Check to make sure its a valid encapsulated TDLS frame */
23238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	tf = (struct wpa_tdls_frame *) buf;
23248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tf->payloadtype != 2 /* TDLS_RFTYPE */ ||
23258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    tf->category != WLAN_ACTION_TDLS) {
23268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "TDLS: Invalid frame - payloadtype=%u "
23278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "category=%u action=%u",
23288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   tf->payloadtype, tf->category, tf->action);
23298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
23308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (tf->action) {
23338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WLAN_TDLS_SETUP_REQUEST:
23348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_process_tpk_m1(sm, src_addr, buf, len);
23358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
23368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WLAN_TDLS_SETUP_RESPONSE:
23378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_process_tpk_m2(sm, src_addr, buf, len);
23388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
23398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WLAN_TDLS_SETUP_CONFIRM:
23408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_process_tpk_m3(sm, src_addr, buf, len);
23418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
23428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WLAN_TDLS_TEARDOWN:
23438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_recv_teardown(sm, src_addr, buf, len);
23448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
23451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case WLAN_TDLS_DISCOVERY_REQUEST:
23461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_tdls_process_discovery_request(sm, src_addr, buf, len);
23471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
23488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
23498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Kernel code will process remaining frames */
23508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Ignore TDLS frame action code %u",
23518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   tf->action);
23528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
23538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
23558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
23588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_tdls_init - Initialize driver interface parameters for TDLS
23598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @wpa_s: Pointer to wpa_supplicant data
23608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 on success, -1 on failure
23618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
23628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is called to initialize driver interface parameters for TDLS.
23638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_drv_init() must have been called before this function to initialize the
23648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * driver interface.
23658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
23668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_tdls_init(struct wpa_sm *sm)
23678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
23688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sm == NULL)
23698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
23708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
237104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	sm->l2_tdls = l2_packet_init(sm->bridge_ifname ? sm->bridge_ifname :
237204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				     sm->ifname,
237304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				     sm->own_addr,
23748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     ETH_P_80211_ENCAP, wpa_supplicant_rx_tdls,
23758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     sm, 0);
23768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sm->l2_tdls == NULL) {
23778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "TDLS: Failed to open l2_packet "
23788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "connection");
23798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
23808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/*
23831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * Drivers that support TDLS but don't implement the get_capa callback
23841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * are assumed to perform everything internally
23851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 */
23861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (wpa_sm_tdls_get_capa(sm, &sm->tdls_supported,
23871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				 &sm->tdls_external_setup) < 0) {
23881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		sm->tdls_supported = 1;
23891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		sm->tdls_external_setup = 0;
23901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
23911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TDLS operation%s supported by "
23931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "driver", sm->tdls_supported ? "" : " not");
23941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Driver uses %s link setup",
23951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   sm->tdls_external_setup ? "external" : "internal");
23961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
23988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
23998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2401f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidtvoid wpa_tdls_teardown_peers(struct wpa_sm *sm)
2402f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt{
2403f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	struct wpa_tdls_peer *peer;
2404f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2405f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	peer = sm->tdls;
2406f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2407f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Tear down peers");
2408f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2409f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	while (peer) {
2410f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Tear down peer " MACSTR,
2411f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			   MAC2STR(peer->addr));
2412f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		if (sm->tdls_external_setup)
2413f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			wpa_tdls_send_teardown(sm, peer->addr,
2414f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt					       WLAN_REASON_DEAUTH_LEAVING);
2415f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		else
2416f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			wpa_sm_tdls_oper(sm, TDLS_TEARDOWN, peer->addr);
2417f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2418f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		peer = peer->next;
2419f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	}
2420f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt}
2421f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2422f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
24238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_tdls_remove_peers(struct wpa_sm *sm)
24248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_tdls_peer *peer, *tmp;
24268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	peer = sm->tdls;
24288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sm->tdls = NULL;
24298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (peer) {
24318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		int res;
24328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		tmp = peer->next;
24338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		res = wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
24348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: Remove peer " MACSTR " (res=%d)",
24358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MAC2STR(peer->addr), res);
24368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_tdls_peer_free(sm, peer);
24378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(peer);
24388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		peer = tmp;
24398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
24408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
24448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_tdls_deinit - Deinitialize driver interface parameters for TDLS
24458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
24468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is called to recover driver interface parameters for TDLS
24478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * and frees resources allocated for it.
24488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
24498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wpa_tdls_deinit(struct wpa_sm *sm)
24508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sm == NULL)
24528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
24538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sm->l2_tdls)
24558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		l2_packet_deinit(sm->l2_tdls);
24568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sm->l2_tdls = NULL;
24578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_remove_peers(sm);
24598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wpa_tdls_assoc(struct wpa_sm *sm)
24638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Remove peers on association");
24658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_remove_peers(sm);
24668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wpa_tdls_disassoc(struct wpa_sm *sm)
24708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: Remove peers on disassociation");
24728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_tdls_remove_peers(sm);
24738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_tdls_prohibited(const u8 *ies, size_t len)
24778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_eapol_ie_parse elems;
24798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ies == NULL)
24818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
24828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_supplicant_parse_ies(ies, len, &elems) < 0)
24848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
24858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (elems.ext_capab == NULL || elems.ext_capab_len < 2 + 5)
24878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
24888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 /* bit 38 - TDLS Prohibited */
24908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return !!(elems.ext_capab[2 + 4] & 0x40);
24918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wpa_tdls_ap_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
24958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sm->tdls_prohibited = wpa_tdls_prohibited(ies, len);
24978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: TDLS is %s in the target BSS",
24988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   sm->tdls_prohibited ? "prohibited" : "allowed");
24998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
25008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wpa_tdls_assoc_resp_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
25038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
25048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!sm->tdls_prohibited && wpa_tdls_prohibited(ies, len)) {
25058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "TDLS: TDLS prohibited based on "
25068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(Re)Association Response IEs");
25078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		sm->tdls_prohibited = 1;
25088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
25098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
25108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wpa_tdls_enable(struct wpa_sm *sm, int enabled)
25138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
25148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "TDLS: %s", enabled ? "enabled" : "disabled");
25158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sm->tdls_disabled = !enabled;
25168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
25171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtint wpa_tdls_is_external_setup(struct wpa_sm *sm)
25201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
25211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return sm->tdls_external_setup;
25221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
2523