blob: ceccda9d37477da23b7b3df248c05d4563a9c7f6 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - TDLS
3 * Copyright (c) 2010-2011, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "utils/os.h"
14#include "common/ieee802_11_defs.h"
15#include "crypto/sha256.h"
16#include "crypto/crypto.h"
17#include "crypto/aes_wrap.h"
18#include "rsn_supp/wpa.h"
19#include "rsn_supp/wpa_ie.h"
20#include "rsn_supp/wpa_i.h"
21#include "drivers/driver.h"
22#include "l2_packet/l2_packet.h"
23
24#ifdef CONFIG_TDLS_TESTING
25#define TDLS_TESTING_LONG_FRAME BIT(0)
26#define TDLS_TESTING_ALT_RSN_IE BIT(1)
27#define TDLS_TESTING_DIFF_BSSID BIT(2)
28#define TDLS_TESTING_SHORT_LIFETIME BIT(3)
29#define TDLS_TESTING_WRONG_LIFETIME_RESP BIT(4)
30#define TDLS_TESTING_WRONG_LIFETIME_CONF BIT(5)
31#define TDLS_TESTING_LONG_LIFETIME BIT(6)
32#define TDLS_TESTING_CONCURRENT_INIT BIT(7)
33#define TDLS_TESTING_NO_TPK_EXPIRATION BIT(8)
34#define TDLS_TESTING_DECLINE_RESP BIT(9)
35#define TDLS_TESTING_IGNORE_AP_PROHIBIT BIT(10)
Dmitry Shmidt21de2142014-04-08 10:50:52 -070036#define TDLS_TESTING_WRONG_MIC BIT(11)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037unsigned int tdls_testing = 0;
38#endif /* CONFIG_TDLS_TESTING */
39
40#define TPK_LIFETIME 43200 /* 12 hours */
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070041#define TPK_M1_RETRY_COUNT 3
42#define TPK_M1_TIMEOUT 5000 /* in milliseconds */
43#define TPK_M2_RETRY_COUNT 10
44#define TPK_M2_TIMEOUT 500 /* in milliseconds */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045
46#define TDLS_MIC_LEN 16
47
48#define TDLS_TIMEOUT_LEN 4
49
50struct wpa_tdls_ftie {
51 u8 ie_type; /* FTIE */
52 u8 ie_len;
53 u8 mic_ctrl[2];
54 u8 mic[TDLS_MIC_LEN];
55 u8 Anonce[WPA_NONCE_LEN]; /* Responder Nonce in TDLS */
56 u8 Snonce[WPA_NONCE_LEN]; /* Initiator Nonce in TDLS */
57 /* followed by optional elements */
58} STRUCT_PACKED;
59
60struct wpa_tdls_timeoutie {
61 u8 ie_type; /* Timeout IE */
62 u8 ie_len;
63 u8 interval_type;
64 u8 value[TDLS_TIMEOUT_LEN];
65} STRUCT_PACKED;
66
67struct wpa_tdls_lnkid {
68 u8 ie_type; /* Link Identifier IE */
69 u8 ie_len;
70 u8 bssid[ETH_ALEN];
71 u8 init_sta[ETH_ALEN];
72 u8 resp_sta[ETH_ALEN];
73} STRUCT_PACKED;
74
75/* TDLS frame headers as per IEEE Std 802.11z-2010 */
76struct wpa_tdls_frame {
77 u8 payloadtype; /* IEEE80211_TDLS_RFTYPE */
78 u8 category; /* Category */
79 u8 action; /* Action (enum tdls_frame_type) */
80} STRUCT_PACKED;
81
82static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs);
83static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx);
84static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer);
Sunil Duttd0ef38b2013-09-30 17:34:13 +030085static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
86 struct wpa_tdls_peer *peer);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070087static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
88 u16 reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089
90
91#define TDLS_MAX_IE_LEN 80
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080092#define IEEE80211_MAX_SUPP_RATES 32
93
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094struct wpa_tdls_peer {
95 struct wpa_tdls_peer *next;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070096 unsigned int reconfig_key:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 int initiator; /* whether this end was initiator for TDLS setup */
98 u8 addr[ETH_ALEN]; /* other end MAC address */
99 u8 inonce[WPA_NONCE_LEN]; /* Initiator Nonce */
100 u8 rnonce[WPA_NONCE_LEN]; /* Responder Nonce */
101 u8 rsnie_i[TDLS_MAX_IE_LEN]; /* Initiator RSN IE */
102 size_t rsnie_i_len;
103 u8 rsnie_p[TDLS_MAX_IE_LEN]; /* Peer RSN IE */
104 size_t rsnie_p_len;
105 u32 lifetime;
106 int cipher; /* Selected cipher (WPA_CIPHER_*) */
107 u8 dtoken;
108
109 struct tpk {
110 u8 kck[16]; /* TPK-KCK */
111 u8 tk[16]; /* TPK-TK; assuming only CCMP will be used */
112 } tpk;
113 int tpk_set;
114 int tpk_success;
Sunil Dutt73b28cc2013-09-30 17:38:41 +0300115 int tpk_in_progress;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116
117 struct tpk_timer {
118 u8 dest[ETH_ALEN];
119 int count; /* Retry Count */
120 int timer; /* Timeout in milliseconds */
121 u8 action_code; /* TDLS frame type */
122 u8 dialog_token;
123 u16 status_code;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700124 u32 peer_capab;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 int buf_len; /* length of TPK message for retransmission */
126 u8 *buf; /* buffer for TPK message */
127 } sm_tmr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800128
129 u16 capability;
130
131 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
132 size_t supp_rates_len;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800133
134 struct ieee80211_ht_capabilities *ht_capabilities;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800135 struct ieee80211_vht_capabilities *vht_capabilities;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800136
137 u8 qos_info;
138
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700139 u16 aid;
140
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800141 u8 *ext_capab;
142 size_t ext_capab_len;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800143
144 u8 *supp_channels;
145 size_t supp_channels_len;
146
147 u8 *supp_oper_classes;
148 size_t supp_oper_classes_len;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700149
150 u8 wmm_capable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151};
152
153
154static int wpa_tdls_get_privacy(struct wpa_sm *sm)
155{
156 /*
157 * Get info needed from supplicant to check if the current BSS supports
158 * security. Other than OPEN mode, rest are considered secured
159 * WEP/WPA/WPA2 hence TDLS frames are processed for TPK handshake.
160 */
161 return sm->pairwise_cipher != WPA_CIPHER_NONE;
162}
163
164
165static u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
166{
167 os_memcpy(pos, ie, ie_len);
168 return pos + ie_len;
169}
170
171
172static int wpa_tdls_del_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
173{
174 if (wpa_sm_set_key(sm, WPA_ALG_NONE, peer->addr,
175 0, 0, NULL, 0, NULL, 0) < 0) {
176 wpa_printf(MSG_WARNING, "TDLS: Failed to delete TPK-TK from "
177 "the driver");
178 return -1;
179 }
180
181 return 0;
182}
183
184
185static int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
186{
187 u8 key_len;
188 u8 rsc[6];
189 enum wpa_alg alg;
190
191 os_memset(rsc, 0, 6);
192
193 switch (peer->cipher) {
194 case WPA_CIPHER_CCMP:
195 alg = WPA_ALG_CCMP;
196 key_len = 16;
197 break;
198 case WPA_CIPHER_NONE:
199 wpa_printf(MSG_DEBUG, "TDLS: Pairwise Cipher Suite: "
200 "NONE - do not use pairwise keys");
201 return -1;
202 default:
203 wpa_printf(MSG_WARNING, "TDLS: Unsupported pairwise cipher %d",
204 sm->pairwise_cipher);
205 return -1;
206 }
207
208 if (wpa_sm_set_key(sm, alg, peer->addr, -1, 1,
209 rsc, sizeof(rsc), peer->tpk.tk, key_len) < 0) {
210 wpa_printf(MSG_WARNING, "TDLS: Failed to set TPK to the "
211 "driver");
212 return -1;
213 }
214 return 0;
215}
216
217
218static int wpa_tdls_send_tpk_msg(struct wpa_sm *sm, const u8 *dst,
219 u8 action_code, u8 dialog_token,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700220 u16 status_code, u32 peer_capab,
221 const u8 *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222{
223 return wpa_sm_send_tdls_mgmt(sm, dst, action_code, dialog_token,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700224 status_code, peer_capab, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225}
226
227
228static int wpa_tdls_tpk_send(struct wpa_sm *sm, const u8 *dest, u8 action_code,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700229 u8 dialog_token, u16 status_code, u32 peer_capab,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 const u8 *msg, size_t msg_len)
231{
232 struct wpa_tdls_peer *peer;
233
234 wpa_printf(MSG_DEBUG, "TDLS: TPK send dest=" MACSTR " action_code=%u "
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700235 "dialog_token=%u status_code=%u peer_capab=%u msg_len=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 MAC2STR(dest), action_code, dialog_token, status_code,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700237 peer_capab, (unsigned int) msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238
239 if (wpa_tdls_send_tpk_msg(sm, dest, action_code, dialog_token,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700240 status_code, peer_capab, msg, msg_len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 wpa_printf(MSG_INFO, "TDLS: Failed to send message "
242 "(action_code=%u)", action_code);
243 return -1;
244 }
245
246 if (action_code == WLAN_TDLS_SETUP_CONFIRM ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247 action_code == WLAN_TDLS_TEARDOWN ||
248 action_code == WLAN_TDLS_DISCOVERY_REQUEST ||
249 action_code == WLAN_TDLS_DISCOVERY_RESPONSE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 return 0; /* No retries */
251
252 for (peer = sm->tdls; peer; peer = peer->next) {
253 if (os_memcmp(peer->addr, dest, ETH_ALEN) == 0)
254 break;
255 }
256
257 if (peer == NULL) {
258 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
259 "retry " MACSTR, MAC2STR(dest));
260 return 0;
261 }
262
263 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
264
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700265 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
266 peer->sm_tmr.count = TPK_M2_RETRY_COUNT;
267 peer->sm_tmr.timer = TPK_M2_TIMEOUT;
268 } else {
269 peer->sm_tmr.count = TPK_M1_RETRY_COUNT;
270 peer->sm_tmr.timer = TPK_M1_TIMEOUT;
271 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272
273 /* Copy message to resend on timeout */
274 os_memcpy(peer->sm_tmr.dest, dest, ETH_ALEN);
275 peer->sm_tmr.action_code = action_code;
276 peer->sm_tmr.dialog_token = dialog_token;
277 peer->sm_tmr.status_code = status_code;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700278 peer->sm_tmr.peer_capab = peer_capab;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 peer->sm_tmr.buf_len = msg_len;
280 os_free(peer->sm_tmr.buf);
281 peer->sm_tmr.buf = os_malloc(msg_len);
282 if (peer->sm_tmr.buf == NULL)
283 return -1;
284 os_memcpy(peer->sm_tmr.buf, msg, msg_len);
285
286 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout registered "
287 "(action_code=%u)", action_code);
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700288 eloop_register_timeout(peer->sm_tmr.timer / 1000,
289 (peer->sm_tmr.timer % 1000) * 1000,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 wpa_tdls_tpk_retry_timeout, sm, peer);
291 return 0;
292}
293
294
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800295static int wpa_tdls_do_teardown(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300296 u16 reason_code)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800297{
298 int ret;
299
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300300 ret = wpa_tdls_send_teardown(sm, peer->addr, reason_code);
301 /* disable the link after teardown was sent */
Sunil Duttd0ef38b2013-09-30 17:34:13 +0300302 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303
304 return ret;
305}
306
307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx)
309{
310
311 struct wpa_sm *sm = eloop_ctx;
312 struct wpa_tdls_peer *peer = timeout_ctx;
313
314 if (peer->sm_tmr.count) {
315 peer->sm_tmr.count--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316
317 wpa_printf(MSG_INFO, "TDLS: Retrying sending of message "
318 "(action_code=%u)",
319 peer->sm_tmr.action_code);
320
321 if (peer->sm_tmr.buf == NULL) {
322 wpa_printf(MSG_INFO, "TDLS: No retry buffer available "
323 "for action_code=%u",
324 peer->sm_tmr.action_code);
325 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm,
326 peer);
327 return;
328 }
329
330 /* resend TPK Handshake Message to Peer */
331 if (wpa_tdls_send_tpk_msg(sm, peer->sm_tmr.dest,
332 peer->sm_tmr.action_code,
333 peer->sm_tmr.dialog_token,
334 peer->sm_tmr.status_code,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700335 peer->sm_tmr.peer_capab,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 peer->sm_tmr.buf,
337 peer->sm_tmr.buf_len)) {
338 wpa_printf(MSG_INFO, "TDLS: Failed to retry "
339 "transmission");
340 }
341
342 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700343 eloop_register_timeout(peer->sm_tmr.timer / 1000,
344 (peer->sm_tmr.timer % 1000) * 1000,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 wpa_tdls_tpk_retry_timeout, sm, peer);
346 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
348
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800349 wpa_printf(MSG_DEBUG, "TDLS: Sending Teardown Request");
350 wpa_tdls_do_teardown(sm, peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300351 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 }
353}
354
355
356static void wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm *sm,
357 struct wpa_tdls_peer *peer,
358 u8 action_code)
359{
360 if (action_code == peer->sm_tmr.action_code) {
361 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout cancelled for "
362 "action_code=%u", action_code);
363
364 /* Cancel Timeout registered */
365 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
366
367 /* free all resources meant for retry */
368 os_free(peer->sm_tmr.buf);
369 peer->sm_tmr.buf = NULL;
370
371 peer->sm_tmr.count = 0;
372 peer->sm_tmr.timer = 0;
373 peer->sm_tmr.buf_len = 0;
374 peer->sm_tmr.action_code = 0xff;
375 } else {
376 wpa_printf(MSG_INFO, "TDLS: Error in cancelling retry timeout "
377 "(Unknown action_code=%u)", action_code);
378 }
379}
380
381
382static void wpa_tdls_generate_tpk(struct wpa_tdls_peer *peer,
383 const u8 *own_addr, const u8 *bssid)
384{
385 u8 key_input[SHA256_MAC_LEN];
386 const u8 *nonce[2];
387 size_t len[2];
388 u8 data[3 * ETH_ALEN];
389
390 /* IEEE Std 802.11z-2010 8.5.9.1:
391 * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
392 */
393 len[0] = WPA_NONCE_LEN;
394 len[1] = WPA_NONCE_LEN;
395 if (os_memcmp(peer->inonce, peer->rnonce, WPA_NONCE_LEN) < 0) {
396 nonce[0] = peer->inonce;
397 nonce[1] = peer->rnonce;
398 } else {
399 nonce[0] = peer->rnonce;
400 nonce[1] = peer->inonce;
401 }
402 wpa_hexdump(MSG_DEBUG, "TDLS: min(Nonce)", nonce[0], WPA_NONCE_LEN);
403 wpa_hexdump(MSG_DEBUG, "TDLS: max(Nonce)", nonce[1], WPA_NONCE_LEN);
404 sha256_vector(2, nonce, len, key_input);
405 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-Key-Input",
406 key_input, SHA256_MAC_LEN);
407
408 /*
409 * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
410 * min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
411 * TODO: is N_KEY really included in KDF Context and if so, in which
412 * presentation format (little endian 16-bit?) is it used? It gets
413 * added by the KDF anyway..
414 */
415
416 if (os_memcmp(own_addr, peer->addr, ETH_ALEN) < 0) {
417 os_memcpy(data, own_addr, ETH_ALEN);
418 os_memcpy(data + ETH_ALEN, peer->addr, ETH_ALEN);
419 } else {
420 os_memcpy(data, peer->addr, ETH_ALEN);
421 os_memcpy(data + ETH_ALEN, own_addr, ETH_ALEN);
422 }
423 os_memcpy(data + 2 * ETH_ALEN, bssid, ETH_ALEN);
424 wpa_hexdump(MSG_DEBUG, "TDLS: KDF Context", data, sizeof(data));
425
426 sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data),
427 (u8 *) &peer->tpk, sizeof(peer->tpk));
428 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-KCK",
429 peer->tpk.kck, sizeof(peer->tpk.kck));
430 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-TK",
431 peer->tpk.tk, sizeof(peer->tpk.tk));
432 peer->tpk_set = 1;
433}
434
435
436/**
437 * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
438 * @kck: TPK-KCK
439 * @lnkid: Pointer to the beginning of Link Identifier IE
440 * @rsnie: Pointer to the beginning of RSN IE used for handshake
441 * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
442 * @ftie: Pointer to the beginning of FT IE
443 * @mic: Pointer for writing MIC
444 *
445 * Calculate MIC for TDLS frame.
446 */
447static int wpa_tdls_ftie_mic(const u8 *kck, u8 trans_seq, const u8 *lnkid,
448 const u8 *rsnie, const u8 *timeoutie,
449 const u8 *ftie, u8 *mic)
450{
451 u8 *buf, *pos;
452 struct wpa_tdls_ftie *_ftie;
453 const struct wpa_tdls_lnkid *_lnkid;
454 int ret;
455 int len = 2 * ETH_ALEN + 1 + 2 + lnkid[1] + 2 + rsnie[1] +
456 2 + timeoutie[1] + 2 + ftie[1];
457 buf = os_zalloc(len);
458 if (!buf) {
459 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
460 return -1;
461 }
462
463 pos = buf;
464 _lnkid = (const struct wpa_tdls_lnkid *) lnkid;
465 /* 1) TDLS initiator STA MAC address */
466 os_memcpy(pos, _lnkid->init_sta, ETH_ALEN);
467 pos += ETH_ALEN;
468 /* 2) TDLS responder STA MAC address */
469 os_memcpy(pos, _lnkid->resp_sta, ETH_ALEN);
470 pos += ETH_ALEN;
471 /* 3) Transaction Sequence number */
472 *pos++ = trans_seq;
473 /* 4) Link Identifier IE */
474 os_memcpy(pos, lnkid, 2 + lnkid[1]);
475 pos += 2 + lnkid[1];
476 /* 5) RSN IE */
477 os_memcpy(pos, rsnie, 2 + rsnie[1]);
478 pos += 2 + rsnie[1];
479 /* 6) Timeout Interval IE */
480 os_memcpy(pos, timeoutie, 2 + timeoutie[1]);
481 pos += 2 + timeoutie[1];
482 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
483 os_memcpy(pos, ftie, 2 + ftie[1]);
484 _ftie = (struct wpa_tdls_ftie *) pos;
485 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
486 pos += 2 + ftie[1];
487
488 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
489 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
490 ret = omac1_aes_128(kck, buf, pos - buf, mic);
491 os_free(buf);
492 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
493 return ret;
494}
495
496
497/**
498 * wpa_tdls_key_mic_teardown - Calculate TDLS FTIE MIC for Teardown frame
499 * @kck: TPK-KCK
500 * @trans_seq: Transaction Sequence Number (4 - Teardown)
501 * @rcode: Reason code for Teardown
502 * @dtoken: Dialog Token used for that particular link
503 * @lnkid: Pointer to the beginning of Link Identifier IE
504 * @ftie: Pointer to the beginning of FT IE
505 * @mic: Pointer for writing MIC
506 *
507 * Calculate MIC for TDLS frame.
508 */
509static int wpa_tdls_key_mic_teardown(const u8 *kck, u8 trans_seq, u16 rcode,
510 u8 dtoken, const u8 *lnkid,
511 const u8 *ftie, u8 *mic)
512{
513 u8 *buf, *pos;
514 struct wpa_tdls_ftie *_ftie;
515 int ret;
516 int len;
517
518 if (lnkid == NULL)
519 return -1;
520
521 len = 2 + lnkid[1] + sizeof(rcode) + sizeof(dtoken) +
522 sizeof(trans_seq) + 2 + ftie[1];
523
524 buf = os_zalloc(len);
525 if (!buf) {
526 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
527 return -1;
528 }
529
530 pos = buf;
531 /* 1) Link Identifier IE */
532 os_memcpy(pos, lnkid, 2 + lnkid[1]);
533 pos += 2 + lnkid[1];
534 /* 2) Reason Code */
535 WPA_PUT_LE16(pos, rcode);
536 pos += sizeof(rcode);
537 /* 3) Dialog token */
538 *pos++ = dtoken;
539 /* 4) Transaction Sequence number */
540 *pos++ = trans_seq;
541 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
542 os_memcpy(pos, ftie, 2 + ftie[1]);
543 _ftie = (struct wpa_tdls_ftie *) pos;
544 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
545 pos += 2 + ftie[1];
546
547 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
548 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
549 ret = omac1_aes_128(kck, buf, pos - buf, mic);
550 os_free(buf);
551 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
552 return ret;
553}
554
555
556static int wpa_supplicant_verify_tdls_mic(u8 trans_seq,
557 struct wpa_tdls_peer *peer,
558 const u8 *lnkid, const u8 *timeoutie,
559 const struct wpa_tdls_ftie *ftie)
560{
561 u8 mic[16];
562
563 if (peer->tpk_set) {
564 wpa_tdls_ftie_mic(peer->tpk.kck, trans_seq, lnkid,
565 peer->rsnie_p, timeoutie, (u8 *) ftie,
566 mic);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700567 if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in FTIE - "
569 "dropping packet");
570 wpa_hexdump(MSG_DEBUG, "TDLS: Received MIC",
571 ftie->mic, 16);
572 wpa_hexdump(MSG_DEBUG, "TDLS: Calculated MIC",
573 mic, 16);
574 return -1;
575 }
576 } else {
577 wpa_printf(MSG_WARNING, "TDLS: Could not verify TDLS MIC, "
578 "TPK not set - dropping packet");
579 return -1;
580 }
581 return 0;
582}
583
584
585static int wpa_supplicant_verify_tdls_mic_teardown(
586 u8 trans_seq, u16 rcode, u8 dtoken, struct wpa_tdls_peer *peer,
587 const u8 *lnkid, const struct wpa_tdls_ftie *ftie)
588{
589 u8 mic[16];
590
591 if (peer->tpk_set) {
592 wpa_tdls_key_mic_teardown(peer->tpk.kck, trans_seq, rcode,
593 dtoken, lnkid, (u8 *) ftie, mic);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700594 if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in Teardown - "
596 "dropping packet");
597 return -1;
598 }
599 } else {
600 wpa_printf(MSG_INFO, "TDLS: Could not verify TDLS Teardown "
601 "MIC, TPK not set - dropping packet");
602 return -1;
603 }
604 return 0;
605}
606
607
608static void wpa_tdls_tpk_timeout(void *eloop_ctx, void *timeout_ctx)
609{
610 struct wpa_sm *sm = eloop_ctx;
611 struct wpa_tdls_peer *peer = timeout_ctx;
612
613 /*
614 * On TPK lifetime expiration, we have an option of either tearing down
615 * the direct link or trying to re-initiate it. The selection of what
616 * to do is not strictly speaking controlled by our role in the expired
617 * link, but for now, use that to select whether to renew or tear down
618 * the link.
619 */
620
621 if (peer->initiator) {
622 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
623 " - try to renew", MAC2STR(peer->addr));
624 wpa_tdls_start(sm, peer->addr);
625 } else {
626 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
627 " - tear down", MAC2STR(peer->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628 wpa_tdls_do_teardown(sm, peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300629 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 }
631}
632
633
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700634static void wpa_tdls_peer_remove_from_list(struct wpa_sm *sm,
635 struct wpa_tdls_peer *peer)
636{
637 struct wpa_tdls_peer *cur, *prev;
638
639 cur = sm->tdls;
640 prev = NULL;
641 while (cur && cur != peer) {
642 prev = cur;
643 cur = cur->next;
644 }
645
646 if (cur != peer) {
647 wpa_printf(MSG_ERROR, "TDLS: Could not find peer " MACSTR
648 " to remove it from the list",
649 MAC2STR(peer->addr));
650 return;
651 }
652
653 if (prev)
654 prev->next = peer->next;
655 else
656 sm->tdls = peer->next;
657}
658
659
660static void wpa_tdls_peer_clear(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661{
662 wpa_printf(MSG_DEBUG, "TDLS: Clear state for peer " MACSTR,
663 MAC2STR(peer->addr));
664 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
665 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700666 peer->reconfig_key = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 peer->initiator = 0;
Sunil Dutt73b28cc2013-09-30 17:38:41 +0300668 peer->tpk_in_progress = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 os_free(peer->sm_tmr.buf);
670 peer->sm_tmr.buf = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800671 os_free(peer->ht_capabilities);
672 peer->ht_capabilities = NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800673 os_free(peer->vht_capabilities);
674 peer->vht_capabilities = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800675 os_free(peer->ext_capab);
676 peer->ext_capab = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800677 os_free(peer->supp_channels);
678 peer->supp_channels = NULL;
679 os_free(peer->supp_oper_classes);
680 peer->supp_oper_classes = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 peer->rsnie_i_len = peer->rsnie_p_len = 0;
682 peer->cipher = 0;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700683 peer->qos_info = 0;
684 peer->wmm_capable = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 peer->tpk_set = peer->tpk_success = 0;
686 os_memset(&peer->tpk, 0, sizeof(peer->tpk));
687 os_memset(peer->inonce, 0, WPA_NONCE_LEN);
688 os_memset(peer->rnonce, 0, WPA_NONCE_LEN);
689}
690
691
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700692static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
693{
694 wpa_tdls_peer_clear(sm, peer);
695 wpa_tdls_peer_remove_from_list(sm, peer);
696 os_free(peer);
697}
698
699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700static void wpa_tdls_linkid(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
701 struct wpa_tdls_lnkid *lnkid)
702{
703 lnkid->ie_type = WLAN_EID_LINK_ID;
704 lnkid->ie_len = 3 * ETH_ALEN;
705 os_memcpy(lnkid->bssid, sm->bssid, ETH_ALEN);
706 if (peer->initiator) {
707 os_memcpy(lnkid->init_sta, sm->own_addr, ETH_ALEN);
708 os_memcpy(lnkid->resp_sta, peer->addr, ETH_ALEN);
709 } else {
710 os_memcpy(lnkid->init_sta, peer->addr, ETH_ALEN);
711 os_memcpy(lnkid->resp_sta, sm->own_addr, ETH_ALEN);
712 }
713}
714
715
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700716static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
717 u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718{
719 struct wpa_tdls_peer *peer;
720 struct wpa_tdls_ftie *ftie;
721 struct wpa_tdls_lnkid lnkid;
722 u8 dialog_token;
723 u8 *rbuf, *pos;
724 int ielen;
725
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800726 if (sm->tdls_disabled || !sm->tdls_supported)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727 return -1;
728
729 /* Find the node and free from the list */
730 for (peer = sm->tdls; peer; peer = peer->next) {
731 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
732 break;
733 }
734
735 if (peer == NULL) {
736 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
737 "Teardown " MACSTR, MAC2STR(addr));
738 return 0;
739 }
740
741 dialog_token = peer->dtoken;
742
743 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown for " MACSTR,
744 MAC2STR(addr));
745
746 ielen = 0;
747 if (wpa_tdls_get_privacy(sm) && peer->tpk_set && peer->tpk_success) {
748 /* To add FTIE for Teardown request and compute MIC */
749 ielen += sizeof(*ftie);
750#ifdef CONFIG_TDLS_TESTING
751 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
752 ielen += 170;
753#endif /* CONFIG_TDLS_TESTING */
754 }
755
756 rbuf = os_zalloc(ielen + 1);
757 if (rbuf == NULL)
758 return -1;
759 pos = rbuf;
760
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700761 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 goto skip_ies;
763
764 ftie = (struct wpa_tdls_ftie *) pos;
765 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
766 /* Using the recent nonce which should be for CONFIRM frame */
767 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
768 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
769 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
770 pos = (u8 *) (ftie + 1);
771#ifdef CONFIG_TDLS_TESTING
772 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
773 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
774 "FTIE");
775 ftie->ie_len += 170;
776 *pos++ = 255; /* FTIE subelem */
777 *pos++ = 168; /* FTIE subelem length */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800778 pos += 168;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 }
780#endif /* CONFIG_TDLS_TESTING */
781 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TDLS Teardown handshake",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 (u8 *) ftie, pos - (u8 *) ftie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783
784 /* compute MIC before sending */
785 wpa_tdls_linkid(sm, peer, &lnkid);
786 wpa_tdls_key_mic_teardown(peer->tpk.kck, 4, reason_code,
787 dialog_token, (u8 *) &lnkid, (u8 *) ftie,
788 ftie->mic);
789
790skip_ies:
791 /* TODO: register for a Timeout handler, if Teardown is not received at
792 * the other end, then try again another time */
793
794 /* request driver to send Teardown using this FTIE */
795 wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_TEARDOWN, 0,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700796 reason_code, 0, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 os_free(rbuf);
798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 return 0;
800}
801
802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803int wpa_tdls_teardown_link(struct wpa_sm *sm, const u8 *addr, u16 reason_code)
804{
805 struct wpa_tdls_peer *peer;
806
807 if (sm->tdls_disabled || !sm->tdls_supported)
808 return -1;
809
810 for (peer = sm->tdls; peer; peer = peer->next) {
811 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
812 break;
813 }
814
815 if (peer == NULL) {
816 wpa_printf(MSG_DEBUG, "TDLS: Could not find peer " MACSTR
817 " for link Teardown", MAC2STR(addr));
818 return -1;
819 }
820
821 if (!peer->tpk_success) {
822 wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
823 " not connected - cannot Teardown link", MAC2STR(addr));
824 return -1;
825 }
826
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300827 return wpa_tdls_do_teardown(sm, peer, reason_code);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800828}
829
830
Sunil Dutt38ffd882013-09-30 17:23:23 +0300831static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
832 struct wpa_tdls_peer *peer)
833{
834 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
835 wpa_tdls_peer_free(sm, peer);
836}
837
838
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700839void wpa_tdls_disable_unreachable_link(struct wpa_sm *sm, const u8 *addr)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800840{
841 struct wpa_tdls_peer *peer;
842
843 for (peer = sm->tdls; peer; peer = peer->next) {
844 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
845 break;
846 }
847
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700848 if (!peer || !peer->tpk_success) {
849 wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
850 " not connected - cannot teardown unreachable link",
851 MAC2STR(addr));
852 return;
853 }
854
855 if (wpa_tdls_is_external_setup(sm)) {
856 /*
857 * Disable the link, send a teardown packet through the
858 * AP, and then reset link data.
859 */
860 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, addr);
861 wpa_tdls_send_teardown(sm, addr,
862 WLAN_REASON_TDLS_TEARDOWN_UNREACHABLE);
863 wpa_tdls_peer_free(sm, peer);
864 } else {
Sunil Dutt38ffd882013-09-30 17:23:23 +0300865 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700866 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867}
868
869
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800870const char * wpa_tdls_get_link_status(struct wpa_sm *sm, const u8 *addr)
871{
872 struct wpa_tdls_peer *peer;
873
874 if (sm->tdls_disabled || !sm->tdls_supported)
875 return "disabled";
876
877 for (peer = sm->tdls; peer; peer = peer->next) {
878 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
879 break;
880 }
881
882 if (peer == NULL)
883 return "peer does not exist";
884
885 if (!peer->tpk_success)
886 return "peer not connected";
887
888 return "connected";
889}
890
891
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892static int wpa_tdls_recv_teardown(struct wpa_sm *sm, const u8 *src_addr,
893 const u8 *buf, size_t len)
894{
895 struct wpa_tdls_peer *peer = NULL;
896 struct wpa_tdls_ftie *ftie;
897 struct wpa_tdls_lnkid *lnkid;
898 struct wpa_eapol_ie_parse kde;
899 u16 reason_code;
900 const u8 *pos;
901 int ielen;
902
903 /* Find the node and free from the list */
904 for (peer = sm->tdls; peer; peer = peer->next) {
905 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
906 break;
907 }
908
909 if (peer == NULL) {
910 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
911 "Teardown " MACSTR, MAC2STR(src_addr));
912 return 0;
913 }
914
915 pos = buf;
916 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
917
918 reason_code = WPA_GET_LE16(pos);
919 pos += 2;
920
921 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown Request from " MACSTR
922 " (reason code %u)", MAC2STR(src_addr), reason_code);
923
924 ielen = len - (pos - buf); /* start of IE in buf */
925 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
926 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in Teardown");
927 return -1;
928 }
929
930 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
931 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TDLS "
932 "Teardown");
933 return -1;
934 }
935 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
936
937 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
938 goto skip_ftie;
939
940 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
941 wpa_printf(MSG_INFO, "TDLS: No FTIE in TDLS Teardown");
942 return -1;
943 }
944
945 ftie = (struct wpa_tdls_ftie *) kde.ftie;
946
947 /* Process MIC check to see if TDLS Teardown is right */
948 if (wpa_supplicant_verify_tdls_mic_teardown(4, reason_code,
949 peer->dtoken, peer,
950 (u8 *) lnkid, ftie) < 0) {
951 wpa_printf(MSG_DEBUG, "TDLS: MIC failure for TDLS "
952 "Teardown Request from " MACSTR, MAC2STR(src_addr));
953 return -1;
954 }
955
956skip_ftie:
957 /*
958 * Request the driver to disable the direct link and clear associated
959 * keys.
960 */
Sunil Dutt38ffd882013-09-30 17:23:23 +0300961 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 return 0;
963}
964
965
966/**
967 * wpa_tdls_send_error - To send suitable TDLS status response with
968 * appropriate status code mentioning reason for error/failure.
969 * @dst - MAC addr of Peer station
970 * @tdls_action - TDLS frame type for which error code is sent
971 * @status - status code mentioning reason
972 */
973
974static int wpa_tdls_send_error(struct wpa_sm *sm, const u8 *dst,
975 u8 tdls_action, u8 dialog_token, u16 status)
976{
977 wpa_printf(MSG_DEBUG, "TDLS: Sending error to " MACSTR
978 " (action=%u status=%u)",
979 MAC2STR(dst), tdls_action, status);
980 return wpa_tdls_tpk_send(sm, dst, tdls_action, dialog_token, status,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700981 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982}
983
984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800985static struct wpa_tdls_peer *
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800986wpa_tdls_add_peer(struct wpa_sm *sm, const u8 *addr, int *existing)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800987{
988 struct wpa_tdls_peer *peer;
989
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800990 if (existing)
991 *existing = 0;
992 for (peer = sm->tdls; peer; peer = peer->next) {
993 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0) {
994 if (existing)
995 *existing = 1;
996 return peer; /* re-use existing entry */
997 }
998 }
999
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000 wpa_printf(MSG_INFO, "TDLS: Creating peer entry for " MACSTR,
1001 MAC2STR(addr));
1002
1003 peer = os_zalloc(sizeof(*peer));
1004 if (peer == NULL)
1005 return NULL;
1006
1007 os_memcpy(peer->addr, addr, ETH_ALEN);
1008 peer->next = sm->tdls;
1009 sm->tdls = peer;
1010
1011 return peer;
1012}
1013
1014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015static int wpa_tdls_send_tpk_m1(struct wpa_sm *sm,
1016 struct wpa_tdls_peer *peer)
1017{
1018 size_t buf_len;
1019 struct wpa_tdls_timeoutie timeoutie;
1020 u16 rsn_capab;
1021 struct wpa_tdls_ftie *ftie;
1022 u8 *rbuf, *pos, *count_pos;
1023 u16 count;
1024 struct rsn_ie_hdr *hdr;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001025 int status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026
1027 if (!wpa_tdls_get_privacy(sm)) {
1028 wpa_printf(MSG_DEBUG, "TDLS: No security used on the link");
1029 peer->rsnie_i_len = 0;
1030 goto skip_rsnie;
1031 }
1032
1033 /*
1034 * TPK Handshake Message 1:
1035 * FTIE: ANonce=0, SNonce=initiator nonce MIC=0, DataKDs=(RSNIE_I,
1036 * Timeout Interval IE))
1037 */
1038
1039 /* Filling RSN IE */
1040 hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
1041 hdr->elem_id = WLAN_EID_RSN;
1042 WPA_PUT_LE16(hdr->version, RSN_VERSION);
1043
1044 pos = (u8 *) (hdr + 1);
1045 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1046 pos += RSN_SELECTOR_LEN;
1047 count_pos = pos;
1048 pos += 2;
1049
1050 count = 0;
1051
1052 /*
1053 * AES-CCMP is the default Encryption preferred for TDLS, so
1054 * RSN IE is filled only with CCMP CIPHER
1055 * Note: TKIP is not used to encrypt TDLS link.
1056 *
1057 * Regardless of the cipher used on the AP connection, select CCMP
1058 * here.
1059 */
1060 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1061 pos += RSN_SELECTOR_LEN;
1062 count++;
1063
1064 WPA_PUT_LE16(count_pos, count);
1065
1066 WPA_PUT_LE16(pos, 1);
1067 pos += 2;
1068 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1069 pos += RSN_SELECTOR_LEN;
1070
1071 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1072 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1073#ifdef CONFIG_TDLS_TESTING
1074 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
1075 wpa_printf(MSG_DEBUG, "TDLS: Use alternative RSN IE for "
1076 "testing");
1077 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1078 }
1079#endif /* CONFIG_TDLS_TESTING */
1080 WPA_PUT_LE16(pos, rsn_capab);
1081 pos += 2;
1082#ifdef CONFIG_TDLS_TESTING
1083 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
1084 /* Number of PMKIDs */
1085 *pos++ = 0x00;
1086 *pos++ = 0x00;
1087 }
1088#endif /* CONFIG_TDLS_TESTING */
1089
1090 hdr->len = (pos - peer->rsnie_i) - 2;
1091 peer->rsnie_i_len = pos - peer->rsnie_i;
1092 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
1093 peer->rsnie_i, peer->rsnie_i_len);
1094
1095skip_rsnie:
1096 buf_len = 0;
1097 if (wpa_tdls_get_privacy(sm))
1098 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1099 sizeof(struct wpa_tdls_timeoutie);
1100#ifdef CONFIG_TDLS_TESTING
1101 if (wpa_tdls_get_privacy(sm) &&
1102 (tdls_testing & TDLS_TESTING_LONG_FRAME))
1103 buf_len += 170;
1104 if (tdls_testing & TDLS_TESTING_DIFF_BSSID)
1105 buf_len += sizeof(struct wpa_tdls_lnkid);
1106#endif /* CONFIG_TDLS_TESTING */
1107 rbuf = os_zalloc(buf_len + 1);
1108 if (rbuf == NULL) {
1109 wpa_tdls_peer_free(sm, peer);
1110 return -1;
1111 }
1112 pos = rbuf;
1113
1114 if (!wpa_tdls_get_privacy(sm))
1115 goto skip_ies;
1116
1117 /* Initiator RSN IE */
1118 pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
1119
1120 ftie = (struct wpa_tdls_ftie *) pos;
1121 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1122 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1123
1124 if (os_get_random(peer->inonce, WPA_NONCE_LEN)) {
1125 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1126 "TDLS: Failed to get random data for initiator Nonce");
1127 os_free(rbuf);
1128 wpa_tdls_peer_free(sm, peer);
1129 return -1;
1130 }
1131 wpa_hexdump(MSG_DEBUG, "TDLS: Initiator Nonce for TPK handshake",
1132 peer->inonce, WPA_NONCE_LEN);
1133 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1134
1135 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK Handshake M1",
1136 (u8 *) ftie, sizeof(struct wpa_tdls_ftie));
1137
1138 pos = (u8 *) (ftie + 1);
1139
1140#ifdef CONFIG_TDLS_TESTING
1141 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1142 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1143 "FTIE");
1144 ftie->ie_len += 170;
1145 *pos++ = 255; /* FTIE subelem */
1146 *pos++ = 168; /* FTIE subelem length */
1147 pos += 168;
1148 }
1149#endif /* CONFIG_TDLS_TESTING */
1150
1151 /* Lifetime */
1152 peer->lifetime = TPK_LIFETIME;
1153#ifdef CONFIG_TDLS_TESTING
1154 if (tdls_testing & TDLS_TESTING_SHORT_LIFETIME) {
1155 wpa_printf(MSG_DEBUG, "TDLS: Testing - use short TPK "
1156 "lifetime");
1157 peer->lifetime = 301;
1158 }
1159 if (tdls_testing & TDLS_TESTING_LONG_LIFETIME) {
1160 wpa_printf(MSG_DEBUG, "TDLS: Testing - use long TPK "
1161 "lifetime");
1162 peer->lifetime = 0xffffffff;
1163 }
1164#endif /* CONFIG_TDLS_TESTING */
1165 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1166 sizeof(timeoutie), peer->lifetime);
1167 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
1168
1169skip_ies:
1170
1171#ifdef CONFIG_TDLS_TESTING
1172 if (tdls_testing & TDLS_TESTING_DIFF_BSSID) {
1173 wpa_printf(MSG_DEBUG, "TDLS: Testing - use incorrect BSSID in "
1174 "Link Identifier");
1175 struct wpa_tdls_lnkid *l = (struct wpa_tdls_lnkid *) pos;
1176 wpa_tdls_linkid(sm, peer, l);
1177 l->bssid[5] ^= 0x01;
1178 pos += sizeof(*l);
1179 }
1180#endif /* CONFIG_TDLS_TESTING */
1181
1182 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Request / TPK "
1183 "Handshake Message 1 (peer " MACSTR ")",
1184 MAC2STR(peer->addr));
1185
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001186 status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_SETUP_REQUEST,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001187 1, 0, 0, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001188 os_free(rbuf);
1189
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001190 return status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191}
1192
1193
1194static int wpa_tdls_send_tpk_m2(struct wpa_sm *sm,
1195 const unsigned char *src_addr, u8 dtoken,
1196 struct wpa_tdls_lnkid *lnkid,
1197 const struct wpa_tdls_peer *peer)
1198{
1199 u8 *rbuf, *pos;
1200 size_t buf_len;
1201 u32 lifetime;
1202 struct wpa_tdls_timeoutie timeoutie;
1203 struct wpa_tdls_ftie *ftie;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001204 int status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205
1206 buf_len = 0;
1207 if (wpa_tdls_get_privacy(sm)) {
1208 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1209 * Lifetime */
1210 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1211 sizeof(struct wpa_tdls_timeoutie);
1212#ifdef CONFIG_TDLS_TESTING
1213 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1214 buf_len += 170;
1215#endif /* CONFIG_TDLS_TESTING */
1216 }
1217
1218 rbuf = os_zalloc(buf_len + 1);
1219 if (rbuf == NULL)
1220 return -1;
1221 pos = rbuf;
1222
1223 if (!wpa_tdls_get_privacy(sm))
1224 goto skip_ies;
1225
1226 /* Peer RSN IE */
1227 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1228
1229 ftie = (struct wpa_tdls_ftie *) pos;
1230 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1231 /* TODO: ftie->mic_control to set 2-RESPONSE */
1232 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1233 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1234 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1235 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK M2",
1236 (u8 *) ftie, sizeof(*ftie));
1237
1238 pos = (u8 *) (ftie + 1);
1239
1240#ifdef CONFIG_TDLS_TESTING
1241 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1242 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1243 "FTIE");
1244 ftie->ie_len += 170;
1245 *pos++ = 255; /* FTIE subelem */
1246 *pos++ = 168; /* FTIE subelem length */
1247 pos += 168;
1248 }
1249#endif /* CONFIG_TDLS_TESTING */
1250
1251 /* Lifetime */
1252 lifetime = peer->lifetime;
1253#ifdef CONFIG_TDLS_TESTING
1254 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_RESP) {
1255 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1256 "lifetime in response");
1257 lifetime++;
1258 }
1259#endif /* CONFIG_TDLS_TESTING */
1260 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1261 sizeof(timeoutie), lifetime);
1262 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds from initiator",
1263 lifetime);
1264
1265 /* compute MIC before sending */
1266 wpa_tdls_ftie_mic(peer->tpk.kck, 2, (u8 *) lnkid, peer->rsnie_p,
1267 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001268#ifdef CONFIG_TDLS_TESTING
1269 if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
1270 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
1271 ftie->mic[0] ^= 0x01;
1272 }
1273#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274
1275skip_ies:
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001276 status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001277 dtoken, 0, 0, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278 os_free(rbuf);
1279
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001280 return status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281}
1282
1283
1284static int wpa_tdls_send_tpk_m3(struct wpa_sm *sm,
1285 const unsigned char *src_addr, u8 dtoken,
1286 struct wpa_tdls_lnkid *lnkid,
1287 const struct wpa_tdls_peer *peer)
1288{
1289 u8 *rbuf, *pos;
1290 size_t buf_len;
1291 struct wpa_tdls_ftie *ftie;
1292 struct wpa_tdls_timeoutie timeoutie;
1293 u32 lifetime;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001294 int status;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001295 u32 peer_capab = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296
1297 buf_len = 0;
1298 if (wpa_tdls_get_privacy(sm)) {
1299 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1300 * Lifetime */
1301 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1302 sizeof(struct wpa_tdls_timeoutie);
1303#ifdef CONFIG_TDLS_TESTING
1304 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1305 buf_len += 170;
1306#endif /* CONFIG_TDLS_TESTING */
1307 }
1308
1309 rbuf = os_zalloc(buf_len + 1);
1310 if (rbuf == NULL)
1311 return -1;
1312 pos = rbuf;
1313
1314 if (!wpa_tdls_get_privacy(sm))
1315 goto skip_ies;
1316
1317 /* Peer RSN IE */
1318 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1319
1320 ftie = (struct wpa_tdls_ftie *) pos;
1321 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1322 /*TODO: ftie->mic_control to set 3-CONFIRM */
1323 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1324 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1325 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1326
1327 pos = (u8 *) (ftie + 1);
1328
1329#ifdef CONFIG_TDLS_TESTING
1330 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1331 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1332 "FTIE");
1333 ftie->ie_len += 170;
1334 *pos++ = 255; /* FTIE subelem */
1335 *pos++ = 168; /* FTIE subelem length */
1336 pos += 168;
1337 }
1338#endif /* CONFIG_TDLS_TESTING */
1339
1340 /* Lifetime */
1341 lifetime = peer->lifetime;
1342#ifdef CONFIG_TDLS_TESTING
1343 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_CONF) {
1344 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1345 "lifetime in confirm");
1346 lifetime++;
1347 }
1348#endif /* CONFIG_TDLS_TESTING */
1349 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1350 sizeof(timeoutie), lifetime);
1351 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds",
1352 lifetime);
1353
1354 /* compute MIC before sending */
1355 wpa_tdls_ftie_mic(peer->tpk.kck, 3, (u8 *) lnkid, peer->rsnie_p,
1356 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001357#ifdef CONFIG_TDLS_TESTING
1358 if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
1359 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
1360 ftie->mic[0] ^= 0x01;
1361 }
1362#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07001364skip_ies:
1365
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001366 if (peer->vht_capabilities)
1367 peer_capab |= TDLS_PEER_VHT;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07001368 if (peer->ht_capabilities)
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001369 peer_capab |= TDLS_PEER_HT;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07001370 if (peer->wmm_capable)
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001371 peer_capab |= TDLS_PEER_WMM;
1372
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001373 status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001374 dtoken, 0, peer_capab, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375 os_free(rbuf);
1376
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001377 return status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378}
1379
1380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001381static int wpa_tdls_send_discovery_response(struct wpa_sm *sm,
1382 struct wpa_tdls_peer *peer,
1383 u8 dialog_token)
1384{
1385 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Discovery Response "
1386 "(peer " MACSTR ")", MAC2STR(peer->addr));
1387
1388 return wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_DISCOVERY_RESPONSE,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001389 dialog_token, 0, 0, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001390}
1391
1392
1393static int
1394wpa_tdls_process_discovery_request(struct wpa_sm *sm, const u8 *addr,
1395 const u8 *buf, size_t len)
1396{
1397 struct wpa_eapol_ie_parse kde;
1398 const struct wpa_tdls_lnkid *lnkid;
1399 struct wpa_tdls_peer *peer;
1400 size_t min_req_len = sizeof(struct wpa_tdls_frame) +
1401 1 /* dialog token */ + sizeof(struct wpa_tdls_lnkid);
1402 u8 dialog_token;
1403
1404 wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from " MACSTR,
1405 MAC2STR(addr));
1406
1407 if (len < min_req_len) {
1408 wpa_printf(MSG_DEBUG, "TDLS Discovery Request is too short: "
1409 "%d", (int) len);
1410 return -1;
1411 }
1412
1413 dialog_token = buf[sizeof(struct wpa_tdls_frame)];
1414
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07001415 /*
1416 * Some APs will tack on a weird IE to the end of a TDLS
1417 * discovery request packet. This needn't fail the response,
1418 * since the required IE are verified separately.
1419 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001420 if (wpa_supplicant_parse_ies(buf + sizeof(struct wpa_tdls_frame) + 1,
1421 len - (sizeof(struct wpa_tdls_frame) + 1),
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07001422 &kde) < 0) {
1423 wpa_printf(MSG_DEBUG,
1424 "TDLS: Failed to parse IEs in Discovery Request - ignore as an interop workaround");
1425 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001426
1427 if (!kde.lnkid) {
1428 wpa_printf(MSG_DEBUG, "TDLS: Link ID not found in Discovery "
1429 "Request");
1430 return -1;
1431 }
1432
1433 lnkid = (const struct wpa_tdls_lnkid *) kde.lnkid;
1434
1435 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1436 wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from different "
1437 " BSS " MACSTR, MAC2STR(lnkid->bssid));
1438 return -1;
1439 }
1440
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001441 peer = wpa_tdls_add_peer(sm, addr, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001442 if (peer == NULL)
1443 return -1;
1444
1445 return wpa_tdls_send_discovery_response(sm, peer, dialog_token);
1446}
1447
1448
1449int wpa_tdls_send_discovery_request(struct wpa_sm *sm, const u8 *addr)
1450{
1451 if (sm->tdls_disabled || !sm->tdls_supported)
1452 return -1;
1453
1454 wpa_printf(MSG_DEBUG, "TDLS: Sending Discovery Request to peer "
1455 MACSTR, MAC2STR(addr));
1456 return wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_DISCOVERY_REQUEST,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001457 1, 0, 0, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001458}
1459
1460
1461static int copy_supp_rates(const struct wpa_eapol_ie_parse *kde,
1462 struct wpa_tdls_peer *peer)
1463{
1464 if (!kde->supp_rates) {
1465 wpa_printf(MSG_DEBUG, "TDLS: No supported rates received");
1466 return -1;
1467 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001468 peer->supp_rates_len = merge_byte_arrays(
1469 peer->supp_rates, sizeof(peer->supp_rates),
1470 kde->supp_rates + 2, kde->supp_rates_len - 2,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001471 kde->ext_supp_rates ? kde->ext_supp_rates + 2 : NULL,
1472 kde->ext_supp_rates_len - 2);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001473 return 0;
1474}
1475
1476
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001477static int copy_peer_ht_capab(const struct wpa_eapol_ie_parse *kde,
1478 struct wpa_tdls_peer *peer)
1479{
1480 if (!kde->ht_capabilities ||
1481 kde->ht_capabilities_len <
1482 sizeof(struct ieee80211_ht_capabilities) ) {
1483 wpa_printf(MSG_DEBUG, "TDLS: No supported ht capabilities "
1484 "received");
1485 return 0;
1486 }
1487
1488 if (!peer->ht_capabilities) {
1489 peer->ht_capabilities =
1490 os_zalloc(sizeof(struct ieee80211_ht_capabilities));
1491 if (peer->ht_capabilities == NULL)
1492 return -1;
1493 }
1494
1495 os_memcpy(peer->ht_capabilities, kde->ht_capabilities,
1496 sizeof(struct ieee80211_ht_capabilities));
1497 wpa_hexdump(MSG_DEBUG, "TDLS: Peer HT capabilities",
1498 (u8 *) peer->ht_capabilities,
1499 sizeof(struct ieee80211_ht_capabilities));
1500
1501 return 0;
1502}
1503
1504
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001505static int copy_peer_vht_capab(const struct wpa_eapol_ie_parse *kde,
1506 struct wpa_tdls_peer *peer)
1507{
1508 if (!kde->vht_capabilities ||
1509 kde->vht_capabilities_len <
1510 sizeof(struct ieee80211_vht_capabilities) ) {
1511 wpa_printf(MSG_DEBUG, "TDLS: No supported vht capabilities "
1512 "received");
1513 return 0;
1514 }
1515
1516 if (!peer->vht_capabilities) {
1517 peer->vht_capabilities =
1518 os_zalloc(sizeof(struct ieee80211_vht_capabilities));
1519 if (peer->vht_capabilities == NULL)
1520 return -1;
1521 }
1522
1523 os_memcpy(peer->vht_capabilities, kde->vht_capabilities,
1524 sizeof(struct ieee80211_vht_capabilities));
1525 wpa_hexdump(MSG_DEBUG, "TDLS: Peer VHT capabilities",
1526 (u8 *) peer->vht_capabilities,
1527 sizeof(struct ieee80211_vht_capabilities));
1528
1529 return 0;
1530}
1531
1532
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001533static int copy_peer_ext_capab(const struct wpa_eapol_ie_parse *kde,
1534 struct wpa_tdls_peer *peer)
1535{
1536 if (!kde->ext_capab) {
1537 wpa_printf(MSG_DEBUG, "TDLS: No extended capabilities "
1538 "received");
1539 return 0;
1540 }
1541
1542 if (!peer->ext_capab || peer->ext_capab_len < kde->ext_capab_len - 2) {
1543 /* Need to allocate buffer to fit the new information */
1544 os_free(peer->ext_capab);
1545 peer->ext_capab = os_zalloc(kde->ext_capab_len - 2);
1546 if (peer->ext_capab == NULL)
1547 return -1;
1548 }
1549
1550 peer->ext_capab_len = kde->ext_capab_len - 2;
1551 os_memcpy(peer->ext_capab, kde->ext_capab + 2, peer->ext_capab_len);
1552
1553 return 0;
1554}
1555
1556
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001557static int copy_peer_wmm_capab(const struct wpa_eapol_ie_parse *kde,
1558 struct wpa_tdls_peer *peer)
1559{
1560 struct wmm_information_element *wmm;
1561
1562 if (!kde->wmm) {
1563 wpa_printf(MSG_DEBUG, "TDLS: No supported WMM capabilities received");
1564 return 0;
1565 }
1566
1567 if (kde->wmm_len < sizeof(struct wmm_information_element)) {
1568 wpa_printf(MSG_DEBUG, "TDLS: Invalid supported WMM capabilities received");
1569 return -1;
1570 }
1571
1572 wmm = (struct wmm_information_element *) kde->wmm;
1573 peer->qos_info = wmm->qos_info;
1574
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001575 peer->wmm_capable = 1;
1576
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001577 wpa_printf(MSG_DEBUG, "TDLS: Peer WMM QOS Info 0x%x", peer->qos_info);
1578 return 0;
1579}
1580
1581
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001582static int copy_peer_supp_channels(const struct wpa_eapol_ie_parse *kde,
1583 struct wpa_tdls_peer *peer)
1584{
1585 if (!kde->supp_channels) {
1586 wpa_printf(MSG_DEBUG, "TDLS: No supported channels received");
1587 return 0;
1588 }
1589
1590 if (!peer->supp_channels ||
1591 peer->supp_channels_len < kde->supp_channels_len) {
1592 os_free(peer->supp_channels);
1593 peer->supp_channels = os_zalloc(kde->supp_channels_len);
1594 if (peer->supp_channels == NULL)
1595 return -1;
1596 }
1597
1598 peer->supp_channels_len = kde->supp_channels_len;
1599
1600 os_memcpy(peer->supp_channels, kde->supp_channels,
1601 peer->supp_channels_len);
1602 wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Channels",
1603 (u8 *) peer->supp_channels, peer->supp_channels_len);
1604 return 0;
1605}
1606
1607
1608static int copy_peer_supp_oper_classes(const struct wpa_eapol_ie_parse *kde,
1609 struct wpa_tdls_peer *peer)
1610{
1611 if (!kde->supp_oper_classes) {
1612 wpa_printf(MSG_DEBUG, "TDLS: No supported operating classes received");
1613 return 0;
1614 }
1615
1616 if (!peer->supp_oper_classes ||
1617 peer->supp_oper_classes_len < kde->supp_oper_classes_len) {
1618 os_free(peer->supp_oper_classes);
1619 peer->supp_oper_classes = os_zalloc(kde->supp_oper_classes_len);
1620 if (peer->supp_oper_classes == NULL)
1621 return -1;
1622 }
1623
1624 peer->supp_oper_classes_len = kde->supp_oper_classes_len;
1625 os_memcpy(peer->supp_oper_classes, kde->supp_oper_classes,
1626 peer->supp_oper_classes_len);
1627 wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Operating Classes",
1628 (u8 *) peer->supp_oper_classes,
1629 peer->supp_oper_classes_len);
1630 return 0;
1631}
1632
1633
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001634static int wpa_tdls_addset_peer(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
1635 int add)
1636{
1637 return wpa_sm_tdls_peer_addset(sm, peer->addr, add, peer->aid,
1638 peer->capability,
1639 peer->supp_rates, peer->supp_rates_len,
1640 peer->ht_capabilities,
1641 peer->vht_capabilities,
1642 peer->qos_info, peer->ext_capab,
1643 peer->ext_capab_len,
1644 peer->supp_channels,
1645 peer->supp_channels_len,
1646 peer->supp_oper_classes,
1647 peer->supp_oper_classes_len);
1648}
1649
1650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651static int wpa_tdls_process_tpk_m1(struct wpa_sm *sm, const u8 *src_addr,
1652 const u8 *buf, size_t len)
1653{
1654 struct wpa_tdls_peer *peer;
1655 struct wpa_eapol_ie_parse kde;
1656 struct wpa_ie_data ie;
1657 int cipher;
1658 const u8 *cpos;
1659 struct wpa_tdls_ftie *ftie = NULL;
1660 struct wpa_tdls_timeoutie *timeoutie;
1661 struct wpa_tdls_lnkid *lnkid;
1662 u32 lifetime = 0;
1663#if 0
1664 struct rsn_ie_hdr *hdr;
1665 u8 *pos;
1666 u16 rsn_capab;
1667 u16 rsn_ver;
1668#endif
1669 u8 dtoken;
1670 u16 ielen;
1671 u16 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1672 int tdls_prohibited = sm->tdls_prohibited;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001673 int existing_peer = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674
1675 if (len < 3 + 3)
1676 return -1;
1677
1678 cpos = buf;
1679 cpos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1680
1681 /* driver had already verified the frame format */
1682 dtoken = *cpos++; /* dialog token */
1683
1684 wpa_printf(MSG_INFO, "TDLS: Dialog Token in TPK M1 %d", dtoken);
1685
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001686 peer = wpa_tdls_add_peer(sm, src_addr, &existing_peer);
1687 if (peer == NULL)
1688 goto error;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001689
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001690 /* If found, use existing entry instead of adding a new one;
1691 * how to handle the case where both ends initiate at the
1692 * same time? */
1693 if (existing_peer) {
1694 if (peer->tpk_success) {
1695 wpa_printf(MSG_DEBUG, "TDLS: TDLS Setup Request while "
1696 "direct link is enabled - tear down the "
1697 "old link first");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001698 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
1699 wpa_tdls_peer_clear(sm, peer);
1700 } else if (peer->initiator) {
1701 /*
1702 * An entry is already present, so check if we already
1703 * sent a TDLS Setup Request. If so, compare MAC
1704 * addresses and let the STA with the lower MAC address
1705 * continue as the initiator. The other negotiation is
1706 * terminated.
1707 */
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001708 if (os_memcmp(sm->own_addr, src_addr, ETH_ALEN) < 0) {
1709 wpa_printf(MSG_DEBUG, "TDLS: Discard request "
1710 "from peer with higher address "
1711 MACSTR, MAC2STR(src_addr));
1712 return -1;
1713 } else {
1714 wpa_printf(MSG_DEBUG, "TDLS: Accept request "
1715 "from peer with lower address "
1716 MACSTR " (terminate previously "
1717 "initiated negotiation",
1718 MAC2STR(src_addr));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001719 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK,
1720 peer->addr);
1721 wpa_tdls_peer_clear(sm, peer);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001722 }
1723 }
1724 }
1725
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001726 /* capability information */
1727 peer->capability = WPA_GET_LE16(cpos);
1728 cpos += 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729
1730 ielen = len - (cpos - buf); /* start of IE in buf */
1731 if (wpa_supplicant_parse_ies(cpos, ielen, &kde) < 0) {
1732 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in TPK M1");
1733 goto error;
1734 }
1735
1736 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
1737 wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
1738 "TPK M1");
1739 goto error;
1740 }
1741 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M1",
1742 kde.lnkid, kde.lnkid_len);
1743 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
1744 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1745 wpa_printf(MSG_INFO, "TDLS: TPK M1 from diff BSS");
1746 status = WLAN_STATUS_NOT_IN_SAME_BSS;
1747 goto error;
1748 }
1749
1750 wpa_printf(MSG_DEBUG, "TDLS: TPK M1 - TPK initiator " MACSTR,
1751 MAC2STR(src_addr));
1752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001753 if (copy_supp_rates(&kde, peer) < 0)
1754 goto error;
1755
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001756 if (copy_peer_ht_capab(&kde, peer) < 0)
1757 goto error;
1758
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001759 if (copy_peer_vht_capab(&kde, peer) < 0)
1760 goto error;
1761
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001762 if (copy_peer_ext_capab(&kde, peer) < 0)
1763 goto error;
1764
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001765 if (copy_peer_supp_channels(&kde, peer) < 0)
1766 goto error;
1767
1768 if (copy_peer_supp_oper_classes(&kde, peer) < 0)
1769 goto error;
1770
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001771 peer->qos_info = kde.qosinfo;
1772
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001773 /* Overwrite with the qos_info obtained in WMM IE */
1774 if (copy_peer_wmm_capab(&kde, peer) < 0)
1775 goto error;
1776
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001777 peer->aid = kde.aid;
1778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779#ifdef CONFIG_TDLS_TESTING
1780 if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001781 peer = wpa_tdls_add_peer(sm, src_addr, NULL);
1782 if (peer == NULL)
1783 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 wpa_printf(MSG_DEBUG, "TDLS: Testing concurrent initiation of "
1785 "TDLS setup - send own request");
1786 peer->initiator = 1;
1787 wpa_tdls_send_tpk_m1(sm, peer);
1788 }
1789
1790 if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
1791 tdls_prohibited) {
1792 wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
1793 "on TDLS");
1794 tdls_prohibited = 0;
1795 }
1796#endif /* CONFIG_TDLS_TESTING */
1797
1798 if (tdls_prohibited) {
1799 wpa_printf(MSG_INFO, "TDLS: TDLS prohibited in this BSS");
1800 status = WLAN_STATUS_REQUEST_DECLINED;
1801 goto error;
1802 }
1803
1804 if (!wpa_tdls_get_privacy(sm)) {
1805 if (kde.rsn_ie) {
1806 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M1 while "
1807 "security is disabled");
1808 status = WLAN_STATUS_SECURITY_DISABLED;
1809 goto error;
1810 }
1811 goto skip_rsn;
1812 }
1813
1814 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
1815 kde.rsn_ie == NULL) {
1816 wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M1");
1817 status = WLAN_STATUS_INVALID_PARAMETERS;
1818 goto error;
1819 }
1820
1821 if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
1822 wpa_printf(MSG_INFO, "TDLS: Too long Initiator RSN IE in "
1823 "TPK M1");
1824 status = WLAN_STATUS_INVALID_RSNIE;
1825 goto error;
1826 }
1827
1828 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
1829 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M1");
1830 status = WLAN_STATUS_INVALID_RSNIE;
1831 goto error;
1832 }
1833
1834 cipher = ie.pairwise_cipher;
1835 if (cipher & WPA_CIPHER_CCMP) {
1836 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
1837 cipher = WPA_CIPHER_CCMP;
1838 } else {
1839 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M1");
1840 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1841 goto error;
1842 }
1843
1844 if ((ie.capabilities &
1845 (WPA_CAPABILITY_NO_PAIRWISE | WPA_CAPABILITY_PEERKEY_ENABLED)) !=
1846 WPA_CAPABILITY_PEERKEY_ENABLED) {
1847 wpa_printf(MSG_INFO, "TDLS: Invalid RSN Capabilities in "
1848 "TPK M1");
1849 status = WLAN_STATUS_INVALID_RSN_IE_CAPAB;
1850 goto error;
1851 }
1852
1853 /* Lifetime */
1854 if (kde.key_lifetime == NULL) {
1855 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M1");
1856 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
1857 goto error;
1858 }
1859 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
1860 lifetime = WPA_GET_LE32(timeoutie->value);
1861 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", lifetime);
1862 if (lifetime < 300) {
1863 wpa_printf(MSG_INFO, "TDLS: Too short TPK lifetime");
1864 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
1865 goto error;
1866 }
1867
1868skip_rsn:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001869#ifdef CONFIG_TDLS_TESTING
1870 if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
1871 if (os_memcmp(sm->own_addr, peer->addr, ETH_ALEN) < 0) {
1872 /*
1873 * The request frame from us is going to win, so do not
1874 * replace information based on this request frame from
1875 * the peer.
1876 */
1877 goto skip_rsn_check;
1878 }
1879 }
1880#endif /* CONFIG_TDLS_TESTING */
1881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 peer->initiator = 0; /* Need to check */
1883 peer->dtoken = dtoken;
1884
1885 if (!wpa_tdls_get_privacy(sm)) {
1886 peer->rsnie_i_len = 0;
1887 peer->rsnie_p_len = 0;
1888 peer->cipher = WPA_CIPHER_NONE;
1889 goto skip_rsn_check;
1890 }
1891
1892 ftie = (struct wpa_tdls_ftie *) kde.ftie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 os_memcpy(peer->rsnie_i, kde.rsn_ie, kde.rsn_ie_len);
1894 peer->rsnie_i_len = kde.rsn_ie_len;
1895 peer->cipher = cipher;
1896
Sunil Dutt61024722013-09-15 12:09:40 -07001897 if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
1898 /*
1899 * There is no point in updating the RNonce for every obtained
1900 * TPK M1 frame (e.g., retransmission due to timeout) with the
1901 * same INonce (SNonce in FTIE). However, if the TPK M1 is
1902 * retransmitted with a different INonce, update the RNonce
1903 * since this is for a new TDLS session.
1904 */
1905 wpa_printf(MSG_DEBUG,
1906 "TDLS: New TPK M1 INonce - generate new RNonce");
1907 os_memcpy(peer->inonce, ftie->Snonce, WPA_NONCE_LEN);
1908 if (os_get_random(peer->rnonce, WPA_NONCE_LEN)) {
1909 wpa_msg(sm->ctx->ctx, MSG_WARNING,
1910 "TDLS: Failed to get random data for responder nonce");
Sunil Dutt61024722013-09-15 12:09:40 -07001911 goto error;
1912 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 }
1914
1915#if 0
1916 /* get version info from RSNIE received from Peer */
1917 hdr = (struct rsn_ie_hdr *) kde.rsn_ie;
1918 rsn_ver = WPA_GET_LE16(hdr->version);
1919
1920 /* use min(peer's version, out version) */
1921 if (rsn_ver > RSN_VERSION)
1922 rsn_ver = RSN_VERSION;
1923
1924 hdr = (struct rsn_ie_hdr *) peer->rsnie_p;
1925
1926 hdr->elem_id = WLAN_EID_RSN;
1927 WPA_PUT_LE16(hdr->version, rsn_ver);
1928 pos = (u8 *) (hdr + 1);
1929
1930 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1931 pos += RSN_SELECTOR_LEN;
1932 /* Include only the selected cipher in pairwise cipher suite */
1933 WPA_PUT_LE16(pos, 1);
1934 pos += 2;
1935 if (cipher == WPA_CIPHER_CCMP)
1936 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1937 pos += RSN_SELECTOR_LEN;
1938
1939 WPA_PUT_LE16(pos, 1);
1940 pos += 2;
1941 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1942 pos += RSN_SELECTOR_LEN;
1943
1944 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1945 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1946 WPA_PUT_LE16(pos, rsn_capab);
1947 pos += 2;
1948
1949 hdr->len = (pos - peer->rsnie_p) - 2;
1950 peer->rsnie_p_len = pos - peer->rsnie_p;
1951#endif
1952
1953 /* temp fix: validation of RSNIE later */
1954 os_memcpy(peer->rsnie_p, peer->rsnie_i, peer->rsnie_i_len);
1955 peer->rsnie_p_len = peer->rsnie_i_len;
1956
1957 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
1958 peer->rsnie_p, peer->rsnie_p_len);
1959
1960 peer->lifetime = lifetime;
1961
1962 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
1963
1964skip_rsn_check:
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001965 /* add supported rates, capabilities, and qos_info to the TDLS peer */
1966 if (wpa_tdls_addset_peer(sm, peer, 1) < 0)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001967 goto error;
1968
Sunil Dutt73b28cc2013-09-30 17:38:41 +03001969 peer->tpk_in_progress = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Response / TPK M2");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001972 if (wpa_tdls_send_tpk_m2(sm, src_addr, dtoken, lnkid, peer) < 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001973 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001974 goto error;
1975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976
1977 return 0;
1978
1979error:
1980 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE, dtoken,
1981 status);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001982 if (peer)
1983 wpa_tdls_peer_free(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 return -1;
1985}
1986
1987
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001988static int wpa_tdls_enable_link(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989{
1990 peer->tpk_success = 1;
Sunil Dutt73b28cc2013-09-30 17:38:41 +03001991 peer->tpk_in_progress = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
1993 if (wpa_tdls_get_privacy(sm)) {
1994 u32 lifetime = peer->lifetime;
1995 /*
1996 * Start the initiator process a bit earlier to avoid race
1997 * condition with the responder sending teardown request.
1998 */
1999 if (lifetime > 3 && peer->initiator)
2000 lifetime -= 3;
2001 eloop_register_timeout(lifetime, 0, wpa_tdls_tpk_timeout,
2002 sm, peer);
2003#ifdef CONFIG_TDLS_TESTING
2004 if (tdls_testing & TDLS_TESTING_NO_TPK_EXPIRATION) {
2005 wpa_printf(MSG_DEBUG, "TDLS: Testing - disable TPK "
2006 "expiration");
2007 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
2008 }
2009#endif /* CONFIG_TDLS_TESTING */
2010 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002011
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002012 if (peer->reconfig_key && wpa_tdls_set_key(sm, peer) < 0) {
2013 wpa_printf(MSG_INFO, "TDLS: Could not configure key to the "
2014 "driver");
2015 return -1;
2016 }
2017 peer->reconfig_key = 0;
2018
2019 return wpa_sm_tdls_oper(sm, TDLS_ENABLE_LINK, peer->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020}
2021
2022
2023static int wpa_tdls_process_tpk_m2(struct wpa_sm *sm, const u8 *src_addr,
2024 const u8 *buf, size_t len)
2025{
2026 struct wpa_tdls_peer *peer;
2027 struct wpa_eapol_ie_parse kde;
2028 struct wpa_ie_data ie;
2029 int cipher;
2030 struct wpa_tdls_ftie *ftie;
2031 struct wpa_tdls_timeoutie *timeoutie;
2032 struct wpa_tdls_lnkid *lnkid;
2033 u32 lifetime;
2034 u8 dtoken;
2035 int ielen;
2036 u16 status;
2037 const u8 *pos;
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002038 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039
2040 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Response / TPK M2 "
2041 "(Peer " MACSTR ")", MAC2STR(src_addr));
2042 for (peer = sm->tdls; peer; peer = peer->next) {
2043 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
2044 break;
2045 }
2046 if (peer == NULL) {
2047 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
2048 "TPK M2: " MACSTR, MAC2STR(src_addr));
2049 return -1;
2050 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002051 if (!peer->initiator) {
2052 /*
2053 * This may happen if both devices try to initiate TDLS at the
2054 * same time and we accept the TPK M1 from the peer in
2055 * wpa_tdls_process_tpk_m1() and clear our previous state.
2056 */
2057 wpa_printf(MSG_INFO, "TDLS: We were not the initiator, so "
2058 "ignore TPK M2 from " MACSTR, MAC2STR(src_addr));
2059 return -1;
2060 }
Arik Nemtsova7baabb2015-12-10 12:56:07 +02002061
2062 if (peer->tpk_success) {
2063 wpa_printf(MSG_INFO, "TDLS: Ignore incoming TPK M2 retry, from "
2064 MACSTR " as TPK M3 was already sent",
2065 MAC2STR(src_addr));
2066 return 0;
2067 }
2068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_REQUEST);
2070
Sunil Duttadce9cf2013-09-15 11:51:00 -07002071 if (len < 3 + 2 + 1) {
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002072 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 return -1;
Sunil Duttadce9cf2013-09-15 11:51:00 -07002074 }
2075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 pos = buf;
2077 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2078 status = WPA_GET_LE16(pos);
2079 pos += 2 /* status code */;
2080
2081 if (status != WLAN_STATUS_SUCCESS) {
2082 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M2: %u",
2083 status);
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002084 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 return -1;
2086 }
2087
2088 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2089
2090 /* TODO: need to verify dialog token matches here or in kernel */
2091 dtoken = *pos++; /* dialog token */
2092
2093 wpa_printf(MSG_DEBUG, "TDLS: Dialog Token in TPK M2 %d", dtoken);
2094
Sunil Duttadce9cf2013-09-15 11:51:00 -07002095 if (len < 3 + 2 + 1 + 2) {
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002096 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 return -1;
Sunil Duttadce9cf2013-09-15 11:51:00 -07002098 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002099
2100 /* capability information */
2101 peer->capability = WPA_GET_LE16(pos);
2102 pos += 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103
2104 ielen = len - (pos - buf); /* start of IE in buf */
2105 if (wpa_supplicant_parse_ies(pos, ielen, &kde) < 0) {
2106 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in TPK M2");
2107 goto error;
2108 }
2109
2110#ifdef CONFIG_TDLS_TESTING
2111 if (tdls_testing & TDLS_TESTING_DECLINE_RESP) {
2112 wpa_printf(MSG_DEBUG, "TDLS: Testing - decline response");
2113 status = WLAN_STATUS_REQUEST_DECLINED;
2114 goto error;
2115 }
2116#endif /* CONFIG_TDLS_TESTING */
2117
2118 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
2119 wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
2120 "TPK M2");
2121 goto error;
2122 }
2123 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M2",
2124 kde.lnkid, kde.lnkid_len);
2125 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
2126
2127 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
2128 wpa_printf(MSG_INFO, "TDLS: TPK M2 from different BSS");
2129 status = WLAN_STATUS_NOT_IN_SAME_BSS;
2130 goto error;
2131 }
2132
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002133 if (copy_supp_rates(&kde, peer) < 0)
2134 goto error;
2135
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002136 if (copy_peer_ht_capab(&kde, peer) < 0)
2137 goto error;
2138
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002139 if (copy_peer_vht_capab(&kde, peer) < 0)
2140 goto error;
2141
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002142 if (copy_peer_ext_capab(&kde, peer) < 0)
2143 goto error;
2144
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002145 if (copy_peer_supp_channels(&kde, peer) < 0)
2146 goto error;
2147
2148 if (copy_peer_supp_oper_classes(&kde, peer) < 0)
2149 goto error;
2150
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002151 peer->qos_info = kde.qosinfo;
2152
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002153 /* Overwrite with the qos_info obtained in WMM IE */
2154 if (copy_peer_wmm_capab(&kde, peer) < 0)
2155 goto error;
2156
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002157 peer->aid = kde.aid;
2158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 if (!wpa_tdls_get_privacy(sm)) {
2160 peer->rsnie_p_len = 0;
2161 peer->cipher = WPA_CIPHER_NONE;
2162 goto skip_rsn;
2163 }
2164
2165 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
2166 kde.rsn_ie == NULL) {
2167 wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M2");
2168 status = WLAN_STATUS_INVALID_PARAMETERS;
2169 goto error;
2170 }
2171 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
2172 kde.rsn_ie, kde.rsn_ie_len);
2173
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002174 if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
2175 wpa_printf(MSG_INFO,
2176 "TDLS: Too long Responder RSN IE in TPK M2");
2177 status = WLAN_STATUS_INVALID_RSNIE;
2178 goto error;
2179 }
2180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 /*
2182 * FIX: bitwise comparison of RSN IE is not the correct way of
2183 * validation this. It can be different, but certain fields must
2184 * match. Since we list only a single pairwise cipher in TPK M1, the
2185 * memcmp is likely to work in most cases, though.
2186 */
2187 if (kde.rsn_ie_len != peer->rsnie_i_len ||
2188 os_memcmp(peer->rsnie_i, kde.rsn_ie, peer->rsnie_i_len) != 0) {
2189 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M2 does "
2190 "not match with RSN IE used in TPK M1");
2191 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Sent in TPK M1",
2192 peer->rsnie_i, peer->rsnie_i_len);
2193 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
2194 kde.rsn_ie, kde.rsn_ie_len);
2195 status = WLAN_STATUS_INVALID_RSNIE;
2196 goto error;
2197 }
2198
2199 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
2200 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M2");
2201 status = WLAN_STATUS_INVALID_RSNIE;
2202 goto error;
2203 }
2204
2205 cipher = ie.pairwise_cipher;
2206 if (cipher == WPA_CIPHER_CCMP) {
2207 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
2208 cipher = WPA_CIPHER_CCMP;
2209 } else {
2210 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M2");
2211 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
2212 goto error;
2213 }
2214
2215 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M2",
2216 kde.ftie, sizeof(*ftie));
2217 ftie = (struct wpa_tdls_ftie *) kde.ftie;
2218
2219 if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
2220 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M2 does "
2221 "not match with FTIE SNonce used in TPK M1");
2222 /* Silently discard the frame */
2223 return -1;
2224 }
2225
2226 /* Responder Nonce and RSN IE */
2227 os_memcpy(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN);
2228 os_memcpy(peer->rsnie_p, kde.rsn_ie, kde.rsn_ie_len);
2229 peer->rsnie_p_len = kde.rsn_ie_len;
2230 peer->cipher = cipher;
2231
2232 /* Lifetime */
2233 if (kde.key_lifetime == NULL) {
2234 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M2");
2235 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2236 goto error;
2237 }
2238 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2239 lifetime = WPA_GET_LE32(timeoutie->value);
2240 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M2",
2241 lifetime);
2242 if (lifetime != peer->lifetime) {
2243 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2244 "TPK M2 (expected %u)", lifetime, peer->lifetime);
2245 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2246 goto error;
2247 }
2248
2249 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
2250
2251 /* Process MIC check to see if TPK M2 is right */
2252 if (wpa_supplicant_verify_tdls_mic(2, peer, (u8 *) lnkid,
2253 (u8 *) timeoutie, ftie) < 0) {
2254 /* Discard the frame */
2255 wpa_tdls_del_key(sm, peer);
Sunil Dutt38ffd882013-09-30 17:23:23 +03002256 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 return -1;
2258 }
2259
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002260 if (wpa_tdls_set_key(sm, peer) < 0) {
2261 /*
2262 * Some drivers may not be able to config the key prior to full
2263 * STA entry having been configured.
2264 */
2265 wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2266 "STA entry is complete");
2267 peer->reconfig_key = 1;
2268 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269
2270skip_rsn:
2271 peer->dtoken = dtoken;
2272
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002273 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2274 if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
2275 goto error;
2276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002277 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Confirm / "
2278 "TPK Handshake Message 3");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002279 if (wpa_tdls_send_tpk_m3(sm, src_addr, dtoken, lnkid, peer) < 0)
2280 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002282 if (!peer->tpk_success) {
2283 /*
2284 * Enable Link only when tpk_success is 0, signifying that this
2285 * processing of TPK M2 frame is not because of a retransmission
2286 * during TDLS setup handshake.
2287 */
2288 ret = wpa_tdls_enable_link(sm, peer);
2289 if (ret < 0) {
2290 wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
2291 wpa_tdls_do_teardown(
2292 sm, peer,
2293 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
2294 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002295 }
2296 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297
2298error:
2299 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM, dtoken,
2300 status);
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002301 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 return -1;
2303}
2304
2305
2306static int wpa_tdls_process_tpk_m3(struct wpa_sm *sm, const u8 *src_addr,
2307 const u8 *buf, size_t len)
2308{
2309 struct wpa_tdls_peer *peer;
2310 struct wpa_eapol_ie_parse kde;
2311 struct wpa_tdls_ftie *ftie;
2312 struct wpa_tdls_timeoutie *timeoutie;
2313 struct wpa_tdls_lnkid *lnkid;
2314 int ielen;
2315 u16 status;
2316 const u8 *pos;
2317 u32 lifetime;
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002318 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319
2320 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Confirm / TPK M3 "
2321 "(Peer " MACSTR ")", MAC2STR(src_addr));
2322 for (peer = sm->tdls; peer; peer = peer->next) {
2323 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
2324 break;
2325 }
2326 if (peer == NULL) {
2327 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
2328 "TPK M3: " MACSTR, MAC2STR(src_addr));
2329 return -1;
2330 }
2331 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_RESPONSE);
2332
2333 if (len < 3 + 3)
Sunil Duttadce9cf2013-09-15 11:51:00 -07002334 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 pos = buf;
2336 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2337
2338 status = WPA_GET_LE16(pos);
2339
2340 if (status != 0) {
2341 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M3: %u",
2342 status);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002343 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 }
2345 pos += 2 /* status code */ + 1 /* dialog token */;
2346
2347 ielen = len - (pos - buf); /* start of IE in buf */
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002348
2349 /*
2350 * Don't reject the message if failing to parse IEs. The IEs we need are
2351 * explicitly checked below. Some APs piggy-back broken IEs to the end
2352 * of a TDLS Confirm packet, which will fail the link if we don't ignore
2353 * this error.
2354 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002356 wpa_printf(MSG_DEBUG,
2357 "TDLS: Failed to parse KDEs in TPK M3 - ignore as an interop workaround");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 }
2359
2360 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
2361 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002362 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 }
2364 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M3",
2365 (u8 *) kde.lnkid, kde.lnkid_len);
2366 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
2367
2368 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
2369 wpa_printf(MSG_INFO, "TDLS: TPK M3 from diff BSS");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002370 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371 }
2372
2373 if (!wpa_tdls_get_privacy(sm))
2374 goto skip_rsn;
2375
2376 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
2377 wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002378 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379 }
2380 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M3",
2381 kde.ftie, sizeof(*ftie));
2382 ftie = (struct wpa_tdls_ftie *) kde.ftie;
2383
2384 if (kde.rsn_ie == NULL) {
2385 wpa_printf(MSG_INFO, "TDLS: No RSN IE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002386 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 }
2388 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M3",
2389 kde.rsn_ie, kde.rsn_ie_len);
2390 if (kde.rsn_ie_len != peer->rsnie_p_len ||
2391 os_memcmp(kde.rsn_ie, peer->rsnie_p, peer->rsnie_p_len) != 0) {
2392 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M3 does not match "
2393 "with the one sent in TPK M2");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002394 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395 }
2396
2397 if (!os_memcmp(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN) == 0) {
2398 wpa_printf(MSG_INFO, "TDLS: FTIE ANonce in TPK M3 does "
2399 "not match with FTIE ANonce used in TPK M2");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002400 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401 }
2402
2403 if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
2404 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M3 does not "
2405 "match with FTIE SNonce used in TPK M1");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002406 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407 }
2408
2409 if (kde.key_lifetime == NULL) {
2410 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002411 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412 }
2413 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2414 wpa_hexdump(MSG_DEBUG, "TDLS: Timeout IE Received from TPK M3",
2415 (u8 *) timeoutie, sizeof(*timeoutie));
2416 lifetime = WPA_GET_LE32(timeoutie->value);
2417 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M3",
2418 lifetime);
2419 if (lifetime != peer->lifetime) {
2420 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2421 "TPK M3 (expected %u)", lifetime, peer->lifetime);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002422 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 }
2424
2425 if (wpa_supplicant_verify_tdls_mic(3, peer, (u8 *) lnkid,
2426 (u8 *) timeoutie, ftie) < 0) {
2427 wpa_tdls_del_key(sm, peer);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002428 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429 }
2430
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002431 if (wpa_tdls_set_key(sm, peer) < 0) {
2432 /*
2433 * Some drivers may not be able to config the key prior to full
2434 * STA entry having been configured.
2435 */
2436 wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2437 "STA entry is complete");
2438 peer->reconfig_key = 1;
2439 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440
2441skip_rsn:
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002442 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2443 if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
2444 goto error;
2445
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002446 if (!peer->tpk_success) {
2447 /*
2448 * Enable Link only when tpk_success is 0, signifying that this
2449 * processing of TPK M3 frame is not because of a retransmission
2450 * during TDLS setup handshake.
2451 */
2452 ret = wpa_tdls_enable_link(sm, peer);
2453 if (ret < 0) {
2454 wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002455 goto error;
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002456 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002457 }
2458 return ret;
Sunil Duttadce9cf2013-09-15 11:51:00 -07002459error:
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002460 wpa_tdls_do_teardown(sm, peer, WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002461 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462}
2463
2464
2465static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs)
2466{
2467 struct wpa_tdls_timeoutie *lifetime = (struct wpa_tdls_timeoutie *) ie;
2468
2469 os_memset(lifetime, 0, ie_len);
2470 lifetime->ie_type = WLAN_EID_TIMEOUT_INTERVAL;
2471 lifetime->ie_len = sizeof(struct wpa_tdls_timeoutie) - 2;
2472 lifetime->interval_type = WLAN_TIMEOUT_KEY_LIFETIME;
2473 WPA_PUT_LE32(lifetime->value, tsecs);
2474 os_memcpy(pos, ie, ie_len);
2475 return pos + ie_len;
2476}
2477
2478
2479/**
2480 * wpa_tdls_start - Initiate TDLS handshake (send TPK Handshake Message 1)
2481 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2482 * @peer: MAC address of the peer STA
2483 * Returns: 0 on success, or -1 on failure
2484 *
2485 * Send TPK Handshake Message 1 info to driver to start TDLS
2486 * handshake with the peer.
2487 */
2488int wpa_tdls_start(struct wpa_sm *sm, const u8 *addr)
2489{
2490 struct wpa_tdls_peer *peer;
2491 int tdls_prohibited = sm->tdls_prohibited;
2492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002493 if (sm->tdls_disabled || !sm->tdls_supported)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 return -1;
2495
2496#ifdef CONFIG_TDLS_TESTING
2497 if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
2498 tdls_prohibited) {
2499 wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
2500 "on TDLS");
2501 tdls_prohibited = 0;
2502 }
2503#endif /* CONFIG_TDLS_TESTING */
2504
2505 if (tdls_prohibited) {
2506 wpa_printf(MSG_DEBUG, "TDLS: TDLS is prohibited in this BSS - "
2507 "reject request to start setup");
2508 return -1;
2509 }
2510
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002511 peer = wpa_tdls_add_peer(sm, addr, NULL);
2512 if (peer == NULL)
2513 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514
Sunil Dutt73b28cc2013-09-30 17:38:41 +03002515 if (peer->tpk_in_progress) {
2516 wpa_printf(MSG_DEBUG, "TDLS: Setup is already in progress with the peer");
2517 return 0;
2518 }
2519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 peer->initiator = 1;
2521
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002522 /* add the peer to the driver as a "setup in progress" peer */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002523 if (wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL,
2524 NULL, 0, NULL, 0, NULL, 0, NULL, 0)) {
2525 wpa_tdls_disable_peer_link(sm, peer);
2526 return -1;
2527 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002528
Sunil Dutt73b28cc2013-09-30 17:38:41 +03002529 peer->tpk_in_progress = 1;
2530
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002531 if (wpa_tdls_send_tpk_m1(sm, peer) < 0) {
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002532 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002533 return -1;
2534 }
2535
2536 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537}
2538
2539
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002540void wpa_tdls_remove(struct wpa_sm *sm, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541{
2542 struct wpa_tdls_peer *peer;
2543
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002544 if (sm->tdls_disabled || !sm->tdls_supported)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002545 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546
2547 for (peer = sm->tdls; peer; peer = peer->next) {
2548 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
2549 break;
2550 }
2551
2552 if (peer == NULL || !peer->tpk_success)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002553 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002555 if (sm->tdls_external_setup) {
2556 /*
2557 * Disable previous link to allow renegotiation to be completed
2558 * on AP path.
2559 */
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002560 wpa_tdls_do_teardown(sm, peer,
2561 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002562 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563}
2564
2565
2566/**
2567 * wpa_supplicant_rx_tdls - Receive TDLS data frame
2568 *
2569 * This function is called to receive TDLS (ethertype = 0x890d) data frames.
2570 */
2571static void wpa_supplicant_rx_tdls(void *ctx, const u8 *src_addr,
2572 const u8 *buf, size_t len)
2573{
2574 struct wpa_sm *sm = ctx;
2575 struct wpa_tdls_frame *tf;
2576
2577 wpa_hexdump(MSG_DEBUG, "TDLS: Received Data frame encapsulation",
2578 buf, len);
2579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002580 if (sm->tdls_disabled || !sm->tdls_supported) {
2581 wpa_printf(MSG_DEBUG, "TDLS: Discard message - TDLS disabled "
2582 "or unsupported by driver");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 return;
2584 }
2585
2586 if (os_memcmp(src_addr, sm->own_addr, ETH_ALEN) == 0) {
2587 wpa_printf(MSG_DEBUG, "TDLS: Discard copy of own message");
2588 return;
2589 }
2590
2591 if (len < sizeof(*tf)) {
2592 wpa_printf(MSG_INFO, "TDLS: Drop too short frame");
2593 return;
2594 }
2595
2596 /* Check to make sure its a valid encapsulated TDLS frame */
2597 tf = (struct wpa_tdls_frame *) buf;
2598 if (tf->payloadtype != 2 /* TDLS_RFTYPE */ ||
2599 tf->category != WLAN_ACTION_TDLS) {
2600 wpa_printf(MSG_INFO, "TDLS: Invalid frame - payloadtype=%u "
2601 "category=%u action=%u",
2602 tf->payloadtype, tf->category, tf->action);
2603 return;
2604 }
2605
2606 switch (tf->action) {
2607 case WLAN_TDLS_SETUP_REQUEST:
2608 wpa_tdls_process_tpk_m1(sm, src_addr, buf, len);
2609 break;
2610 case WLAN_TDLS_SETUP_RESPONSE:
2611 wpa_tdls_process_tpk_m2(sm, src_addr, buf, len);
2612 break;
2613 case WLAN_TDLS_SETUP_CONFIRM:
2614 wpa_tdls_process_tpk_m3(sm, src_addr, buf, len);
2615 break;
2616 case WLAN_TDLS_TEARDOWN:
2617 wpa_tdls_recv_teardown(sm, src_addr, buf, len);
2618 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002619 case WLAN_TDLS_DISCOVERY_REQUEST:
2620 wpa_tdls_process_discovery_request(sm, src_addr, buf, len);
2621 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 default:
2623 /* Kernel code will process remaining frames */
2624 wpa_printf(MSG_DEBUG, "TDLS: Ignore TDLS frame action code %u",
2625 tf->action);
2626 break;
2627 }
2628}
2629
2630
2631/**
2632 * wpa_tdls_init - Initialize driver interface parameters for TDLS
2633 * @wpa_s: Pointer to wpa_supplicant data
2634 * Returns: 0 on success, -1 on failure
2635 *
2636 * This function is called to initialize driver interface parameters for TDLS.
2637 * wpa_drv_init() must have been called before this function to initialize the
2638 * driver interface.
2639 */
2640int wpa_tdls_init(struct wpa_sm *sm)
2641{
2642 if (sm == NULL)
2643 return -1;
2644
Dmitry Shmidt04949592012-07-19 12:16:46 -07002645 sm->l2_tdls = l2_packet_init(sm->bridge_ifname ? sm->bridge_ifname :
2646 sm->ifname,
2647 sm->own_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 ETH_P_80211_ENCAP, wpa_supplicant_rx_tdls,
2649 sm, 0);
2650 if (sm->l2_tdls == NULL) {
2651 wpa_printf(MSG_ERROR, "TDLS: Failed to open l2_packet "
2652 "connection");
2653 return -1;
2654 }
2655
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002656 /*
2657 * Drivers that support TDLS but don't implement the get_capa callback
2658 * are assumed to perform everything internally
2659 */
2660 if (wpa_sm_tdls_get_capa(sm, &sm->tdls_supported,
2661 &sm->tdls_external_setup) < 0) {
2662 sm->tdls_supported = 1;
2663 sm->tdls_external_setup = 0;
2664 }
2665
2666 wpa_printf(MSG_DEBUG, "TDLS: TDLS operation%s supported by "
2667 "driver", sm->tdls_supported ? "" : " not");
2668 wpa_printf(MSG_DEBUG, "TDLS: Driver uses %s link setup",
2669 sm->tdls_external_setup ? "external" : "internal");
2670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 return 0;
2672}
2673
2674
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002675void wpa_tdls_teardown_peers(struct wpa_sm *sm)
2676{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002677 struct wpa_tdls_peer *peer, *tmp;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002678
2679 peer = sm->tdls;
2680
2681 wpa_printf(MSG_DEBUG, "TDLS: Tear down peers");
2682
2683 while (peer) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002684 tmp = peer->next;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002685 wpa_printf(MSG_DEBUG, "TDLS: Tear down peer " MACSTR,
2686 MAC2STR(peer->addr));
2687 if (sm->tdls_external_setup)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002688 wpa_tdls_do_teardown(sm, peer,
2689 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002690 else
2691 wpa_sm_tdls_oper(sm, TDLS_TEARDOWN, peer->addr);
2692
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002693 peer = tmp;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002694 }
2695}
2696
2697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698static void wpa_tdls_remove_peers(struct wpa_sm *sm)
2699{
2700 struct wpa_tdls_peer *peer, *tmp;
2701
2702 peer = sm->tdls;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703
2704 while (peer) {
2705 int res;
2706 tmp = peer->next;
2707 res = wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
2708 wpa_printf(MSG_DEBUG, "TDLS: Remove peer " MACSTR " (res=%d)",
2709 MAC2STR(peer->addr), res);
2710 wpa_tdls_peer_free(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 peer = tmp;
2712 }
2713}
2714
2715
2716/**
2717 * wpa_tdls_deinit - Deinitialize driver interface parameters for TDLS
2718 *
2719 * This function is called to recover driver interface parameters for TDLS
2720 * and frees resources allocated for it.
2721 */
2722void wpa_tdls_deinit(struct wpa_sm *sm)
2723{
2724 if (sm == NULL)
2725 return;
2726
2727 if (sm->l2_tdls)
2728 l2_packet_deinit(sm->l2_tdls);
2729 sm->l2_tdls = NULL;
2730
2731 wpa_tdls_remove_peers(sm);
2732}
2733
2734
2735void wpa_tdls_assoc(struct wpa_sm *sm)
2736{
2737 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on association");
2738 wpa_tdls_remove_peers(sm);
2739}
2740
2741
2742void wpa_tdls_disassoc(struct wpa_sm *sm)
2743{
2744 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on disassociation");
2745 wpa_tdls_remove_peers(sm);
2746}
2747
2748
2749static int wpa_tdls_prohibited(const u8 *ies, size_t len)
2750{
2751 struct wpa_eapol_ie_parse elems;
2752
2753 if (ies == NULL)
2754 return 0;
2755
2756 if (wpa_supplicant_parse_ies(ies, len, &elems) < 0)
2757 return 0;
2758
2759 if (elems.ext_capab == NULL || elems.ext_capab_len < 2 + 5)
2760 return 0;
2761
2762 /* bit 38 - TDLS Prohibited */
2763 return !!(elems.ext_capab[2 + 4] & 0x40);
2764}
2765
2766
2767void wpa_tdls_ap_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2768{
2769 sm->tdls_prohibited = wpa_tdls_prohibited(ies, len);
2770 wpa_printf(MSG_DEBUG, "TDLS: TDLS is %s in the target BSS",
2771 sm->tdls_prohibited ? "prohibited" : "allowed");
2772}
2773
2774
2775void wpa_tdls_assoc_resp_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2776{
2777 if (!sm->tdls_prohibited && wpa_tdls_prohibited(ies, len)) {
2778 wpa_printf(MSG_DEBUG, "TDLS: TDLS prohibited based on "
2779 "(Re)Association Response IEs");
2780 sm->tdls_prohibited = 1;
2781 }
2782}
2783
2784
2785void wpa_tdls_enable(struct wpa_sm *sm, int enabled)
2786{
2787 wpa_printf(MSG_DEBUG, "TDLS: %s", enabled ? "enabled" : "disabled");
2788 sm->tdls_disabled = !enabled;
2789}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002790
2791
2792int wpa_tdls_is_external_setup(struct wpa_sm *sm)
2793{
2794 return sm->tdls_external_setup;
2795}