2 * wpa_supplicant - TDLS
3 * Copyright (c) 2010-2011, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.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"
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)
36 #define TDLS_TESTING_WRONG_MIC BIT(11)
37 unsigned int tdls_testing
= 0;
38 #endif /* CONFIG_TDLS_TESTING */
40 #define TPK_LIFETIME 43200 /* 12 hours */
41 #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 */
46 #define TDLS_MIC_LEN 16
48 #define TDLS_TIMEOUT_LEN 4
50 struct wpa_tdls_ftie
{
51 u8 ie_type
; /* FTIE */
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 */
60 struct wpa_tdls_timeoutie
{
61 u8 ie_type
; /* Timeout IE */
64 u8 value
[TDLS_TIMEOUT_LEN
];
67 struct wpa_tdls_lnkid
{
68 u8 ie_type
; /* Link Identifier IE */
71 u8 init_sta
[ETH_ALEN
];
72 u8 resp_sta
[ETH_ALEN
];
75 /* TDLS frame headers as per IEEE Std 802.11z-2010 */
76 struct wpa_tdls_frame
{
77 u8 payloadtype
; /* IEEE80211_TDLS_RFTYPE */
78 u8 category
; /* Category */
79 u8 action
; /* Action (enum tdls_frame_type) */
82 static u8
* wpa_add_tdls_timeoutie(u8
*pos
, u8
*ie
, size_t ie_len
, u32 tsecs
);
83 static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx
, void *timeout_ctx
);
84 static void wpa_tdls_peer_free(struct wpa_sm
*sm
, struct wpa_tdls_peer
*peer
);
85 static void wpa_tdls_disable_peer_link(struct wpa_sm
*sm
,
86 struct wpa_tdls_peer
*peer
);
87 static int wpa_tdls_send_teardown(struct wpa_sm
*sm
, const u8
*addr
,
91 #define TDLS_MAX_IE_LEN 80
92 #define IEEE80211_MAX_SUPP_RATES 32
94 struct wpa_tdls_peer
{
95 struct wpa_tdls_peer
*next
;
96 unsigned int reconfig_key
:1;
97 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 */
103 u8 rsnie_p
[TDLS_MAX_IE_LEN
]; /* Peer RSN IE */
106 int cipher
; /* Selected cipher (WPA_CIPHER_*) */
110 u8 kck
[16]; /* TPK-KCK */
111 u8 tk
[16]; /* TPK-TK; assuming only CCMP will be used */
119 int count
; /* Retry Count */
120 int timer
; /* Timeout in milliseconds */
121 u8 action_code
; /* TDLS frame type */
125 int buf_len
; /* length of TPK message for retransmission */
126 u8
*buf
; /* buffer for TPK message */
131 u8 supp_rates
[IEEE80211_MAX_SUPP_RATES
];
132 size_t supp_rates_len
;
134 struct ieee80211_ht_capabilities
*ht_capabilities
;
135 struct ieee80211_vht_capabilities
*vht_capabilities
;
142 size_t ext_capab_len
;
145 size_t supp_channels_len
;
147 u8
*supp_oper_classes
;
148 size_t supp_oper_classes_len
;
154 static int wpa_tdls_get_privacy(struct wpa_sm
*sm
)
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.
161 return sm
->pairwise_cipher
!= WPA_CIPHER_NONE
;
165 static u8
* wpa_add_ie(u8
*pos
, const u8
*ie
, size_t ie_len
)
167 os_memcpy(pos
, ie
, ie_len
);
172 static int wpa_tdls_del_key(struct wpa_sm
*sm
, struct wpa_tdls_peer
*peer
)
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 "
185 static int wpa_tdls_set_key(struct wpa_sm
*sm
, struct wpa_tdls_peer
*peer
)
191 os_memset(rsc
, 0, 6);
193 switch (peer
->cipher
) {
194 case WPA_CIPHER_CCMP
:
198 case WPA_CIPHER_NONE
:
199 wpa_printf(MSG_DEBUG
, "TDLS: Pairwise Cipher Suite: "
200 "NONE - do not use pairwise keys");
203 wpa_printf(MSG_WARNING
, "TDLS: Unsupported pairwise cipher %d",
204 sm
->pairwise_cipher
);
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 "
218 static int wpa_tdls_send_tpk_msg(struct wpa_sm
*sm
, const u8
*dst
,
219 u8 action_code
, u8 dialog_token
,
220 u16 status_code
, u32 peer_capab
,
221 int initiator
, const u8
*buf
, size_t len
)
223 return wpa_sm_send_tdls_mgmt(sm
, dst
, action_code
, dialog_token
,
224 status_code
, peer_capab
, initiator
, buf
,
229 static int wpa_tdls_tpk_send(struct wpa_sm
*sm
, const u8
*dest
, u8 action_code
,
230 u8 dialog_token
, u16 status_code
, u32 peer_capab
,
231 int initiator
, const u8
*msg
, size_t msg_len
)
233 struct wpa_tdls_peer
*peer
;
235 wpa_printf(MSG_DEBUG
, "TDLS: TPK send dest=" MACSTR
" action_code=%u "
236 "dialog_token=%u status_code=%u peer_capab=%u initiator=%d "
238 MAC2STR(dest
), action_code
, dialog_token
, status_code
,
239 peer_capab
, initiator
, (unsigned int) msg_len
);
241 if (wpa_tdls_send_tpk_msg(sm
, dest
, action_code
, dialog_token
,
242 status_code
, peer_capab
, initiator
, msg
,
244 wpa_printf(MSG_INFO
, "TDLS: Failed to send message "
245 "(action_code=%u)", action_code
);
249 if (action_code
== WLAN_TDLS_SETUP_CONFIRM
||
250 action_code
== WLAN_TDLS_TEARDOWN
||
251 action_code
== WLAN_TDLS_DISCOVERY_REQUEST
||
252 action_code
== WLAN_TDLS_DISCOVERY_RESPONSE
)
253 return 0; /* No retries */
255 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
256 if (os_memcmp(peer
->addr
, dest
, ETH_ALEN
) == 0)
261 wpa_printf(MSG_INFO
, "TDLS: No matching entry found for "
262 "retry " MACSTR
, MAC2STR(dest
));
266 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout
, sm
, peer
);
268 if (action_code
== WLAN_TDLS_SETUP_RESPONSE
) {
269 peer
->sm_tmr
.count
= TPK_M2_RETRY_COUNT
;
270 peer
->sm_tmr
.timer
= TPK_M2_TIMEOUT
;
272 peer
->sm_tmr
.count
= TPK_M1_RETRY_COUNT
;
273 peer
->sm_tmr
.timer
= TPK_M1_TIMEOUT
;
276 /* Copy message to resend on timeout */
277 os_memcpy(peer
->sm_tmr
.dest
, dest
, ETH_ALEN
);
278 peer
->sm_tmr
.action_code
= action_code
;
279 peer
->sm_tmr
.dialog_token
= dialog_token
;
280 peer
->sm_tmr
.status_code
= status_code
;
281 peer
->sm_tmr
.peer_capab
= peer_capab
;
282 peer
->sm_tmr
.buf_len
= msg_len
;
283 os_free(peer
->sm_tmr
.buf
);
284 peer
->sm_tmr
.buf
= os_malloc(msg_len
);
285 if (peer
->sm_tmr
.buf
== NULL
)
287 os_memcpy(peer
->sm_tmr
.buf
, msg
, msg_len
);
289 wpa_printf(MSG_DEBUG
, "TDLS: Retry timeout registered "
290 "(action_code=%u)", action_code
);
291 eloop_register_timeout(peer
->sm_tmr
.timer
/ 1000,
292 (peer
->sm_tmr
.timer
% 1000) * 1000,
293 wpa_tdls_tpk_retry_timeout
, sm
, peer
);
298 static int wpa_tdls_do_teardown(struct wpa_sm
*sm
, struct wpa_tdls_peer
*peer
,
303 ret
= wpa_tdls_send_teardown(sm
, peer
->addr
, reason_code
);
304 /* disable the link after teardown was sent */
305 wpa_tdls_disable_peer_link(sm
, peer
);
311 static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx
, void *timeout_ctx
)
314 struct wpa_sm
*sm
= eloop_ctx
;
315 struct wpa_tdls_peer
*peer
= timeout_ctx
;
317 if (peer
->sm_tmr
.count
) {
318 peer
->sm_tmr
.count
--;
320 wpa_printf(MSG_INFO
, "TDLS: Retrying sending of message "
322 peer
->sm_tmr
.action_code
);
324 if (peer
->sm_tmr
.buf
== NULL
) {
325 wpa_printf(MSG_INFO
, "TDLS: No retry buffer available "
326 "for action_code=%u",
327 peer
->sm_tmr
.action_code
);
328 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout
, sm
,
333 /* resend TPK Handshake Message to Peer */
334 if (wpa_tdls_send_tpk_msg(sm
, peer
->sm_tmr
.dest
,
335 peer
->sm_tmr
.action_code
,
336 peer
->sm_tmr
.dialog_token
,
337 peer
->sm_tmr
.status_code
,
338 peer
->sm_tmr
.peer_capab
,
341 peer
->sm_tmr
.buf_len
)) {
342 wpa_printf(MSG_INFO
, "TDLS: Failed to retry "
346 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout
, sm
, peer
);
347 eloop_register_timeout(peer
->sm_tmr
.timer
/ 1000,
348 (peer
->sm_tmr
.timer
% 1000) * 1000,
349 wpa_tdls_tpk_retry_timeout
, sm
, peer
);
351 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout
, sm
, peer
);
353 wpa_printf(MSG_DEBUG
, "TDLS: Sending Teardown Request");
354 wpa_tdls_do_teardown(sm
, peer
,
355 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED
);
360 static void wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm
*sm
,
361 struct wpa_tdls_peer
*peer
,
364 if (action_code
== peer
->sm_tmr
.action_code
) {
365 wpa_printf(MSG_DEBUG
, "TDLS: Retry timeout cancelled for "
366 "action_code=%u", action_code
);
368 /* Cancel Timeout registered */
369 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout
, sm
, peer
);
371 /* free all resources meant for retry */
372 os_free(peer
->sm_tmr
.buf
);
373 peer
->sm_tmr
.buf
= NULL
;
375 peer
->sm_tmr
.count
= 0;
376 peer
->sm_tmr
.timer
= 0;
377 peer
->sm_tmr
.buf_len
= 0;
378 peer
->sm_tmr
.action_code
= 0xff;
380 wpa_printf(MSG_INFO
, "TDLS: Error in cancelling retry timeout "
381 "(Unknown action_code=%u)", action_code
);
386 static void wpa_tdls_generate_tpk(struct wpa_tdls_peer
*peer
,
387 const u8
*own_addr
, const u8
*bssid
)
389 u8 key_input
[SHA256_MAC_LEN
];
392 u8 data
[3 * ETH_ALEN
];
394 /* IEEE Std 802.11z-2010 8.5.9.1:
395 * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
397 len
[0] = WPA_NONCE_LEN
;
398 len
[1] = WPA_NONCE_LEN
;
399 if (os_memcmp(peer
->inonce
, peer
->rnonce
, WPA_NONCE_LEN
) < 0) {
400 nonce
[0] = peer
->inonce
;
401 nonce
[1] = peer
->rnonce
;
403 nonce
[0] = peer
->rnonce
;
404 nonce
[1] = peer
->inonce
;
406 wpa_hexdump(MSG_DEBUG
, "TDLS: min(Nonce)", nonce
[0], WPA_NONCE_LEN
);
407 wpa_hexdump(MSG_DEBUG
, "TDLS: max(Nonce)", nonce
[1], WPA_NONCE_LEN
);
408 sha256_vector(2, nonce
, len
, key_input
);
409 wpa_hexdump_key(MSG_DEBUG
, "TDLS: TPK-Key-Input",
410 key_input
, SHA256_MAC_LEN
);
413 * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
414 * min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
415 * TODO: is N_KEY really included in KDF Context and if so, in which
416 * presentation format (little endian 16-bit?) is it used? It gets
417 * added by the KDF anyway..
420 if (os_memcmp(own_addr
, peer
->addr
, ETH_ALEN
) < 0) {
421 os_memcpy(data
, own_addr
, ETH_ALEN
);
422 os_memcpy(data
+ ETH_ALEN
, peer
->addr
, ETH_ALEN
);
424 os_memcpy(data
, peer
->addr
, ETH_ALEN
);
425 os_memcpy(data
+ ETH_ALEN
, own_addr
, ETH_ALEN
);
427 os_memcpy(data
+ 2 * ETH_ALEN
, bssid
, ETH_ALEN
);
428 wpa_hexdump(MSG_DEBUG
, "TDLS: KDF Context", data
, sizeof(data
));
430 sha256_prf(key_input
, SHA256_MAC_LEN
, "TDLS PMK", data
, sizeof(data
),
431 (u8
*) &peer
->tpk
, sizeof(peer
->tpk
));
432 wpa_hexdump_key(MSG_DEBUG
, "TDLS: TPK-KCK",
433 peer
->tpk
.kck
, sizeof(peer
->tpk
.kck
));
434 wpa_hexdump_key(MSG_DEBUG
, "TDLS: TPK-TK",
435 peer
->tpk
.tk
, sizeof(peer
->tpk
.tk
));
441 * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
443 * @lnkid: Pointer to the beginning of Link Identifier IE
444 * @rsnie: Pointer to the beginning of RSN IE used for handshake
445 * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
446 * @ftie: Pointer to the beginning of FT IE
447 * @mic: Pointer for writing MIC
449 * Calculate MIC for TDLS frame.
451 static int wpa_tdls_ftie_mic(const u8
*kck
, u8 trans_seq
, const u8
*lnkid
,
452 const u8
*rsnie
, const u8
*timeoutie
,
453 const u8
*ftie
, u8
*mic
)
456 struct wpa_tdls_ftie
*_ftie
;
457 const struct wpa_tdls_lnkid
*_lnkid
;
459 int len
= 2 * ETH_ALEN
+ 1 + 2 + lnkid
[1] + 2 + rsnie
[1] +
460 2 + timeoutie
[1] + 2 + ftie
[1];
461 buf
= os_zalloc(len
);
463 wpa_printf(MSG_WARNING
, "TDLS: No memory for MIC calculation");
468 _lnkid
= (const struct wpa_tdls_lnkid
*) lnkid
;
469 /* 1) TDLS initiator STA MAC address */
470 os_memcpy(pos
, _lnkid
->init_sta
, ETH_ALEN
);
472 /* 2) TDLS responder STA MAC address */
473 os_memcpy(pos
, _lnkid
->resp_sta
, ETH_ALEN
);
475 /* 3) Transaction Sequence number */
477 /* 4) Link Identifier IE */
478 os_memcpy(pos
, lnkid
, 2 + lnkid
[1]);
481 os_memcpy(pos
, rsnie
, 2 + rsnie
[1]);
483 /* 6) Timeout Interval IE */
484 os_memcpy(pos
, timeoutie
, 2 + timeoutie
[1]);
485 pos
+= 2 + timeoutie
[1];
486 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
487 os_memcpy(pos
, ftie
, 2 + ftie
[1]);
488 _ftie
= (struct wpa_tdls_ftie
*) pos
;
489 os_memset(_ftie
->mic
, 0, TDLS_MIC_LEN
);
492 wpa_hexdump(MSG_DEBUG
, "TDLS: Data for FTIE MIC", buf
, pos
- buf
);
493 wpa_hexdump_key(MSG_DEBUG
, "TDLS: KCK", kck
, 16);
494 ret
= omac1_aes_128(kck
, buf
, pos
- buf
, mic
);
496 wpa_hexdump(MSG_DEBUG
, "TDLS: FTIE MIC", mic
, 16);
502 * wpa_tdls_key_mic_teardown - Calculate TDLS FTIE MIC for Teardown frame
504 * @trans_seq: Transaction Sequence Number (4 - Teardown)
505 * @rcode: Reason code for Teardown
506 * @dtoken: Dialog Token used for that particular link
507 * @lnkid: Pointer to the beginning of Link Identifier IE
508 * @ftie: Pointer to the beginning of FT IE
509 * @mic: Pointer for writing MIC
511 * Calculate MIC for TDLS frame.
513 static int wpa_tdls_key_mic_teardown(const u8
*kck
, u8 trans_seq
, u16 rcode
,
514 u8 dtoken
, const u8
*lnkid
,
515 const u8
*ftie
, u8
*mic
)
518 struct wpa_tdls_ftie
*_ftie
;
525 len
= 2 + lnkid
[1] + sizeof(rcode
) + sizeof(dtoken
) +
526 sizeof(trans_seq
) + 2 + ftie
[1];
528 buf
= os_zalloc(len
);
530 wpa_printf(MSG_WARNING
, "TDLS: No memory for MIC calculation");
535 /* 1) Link Identifier IE */
536 os_memcpy(pos
, lnkid
, 2 + lnkid
[1]);
539 WPA_PUT_LE16(pos
, rcode
);
540 pos
+= sizeof(rcode
);
541 /* 3) Dialog token */
543 /* 4) Transaction Sequence number */
545 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
546 os_memcpy(pos
, ftie
, 2 + ftie
[1]);
547 _ftie
= (struct wpa_tdls_ftie
*) pos
;
548 os_memset(_ftie
->mic
, 0, TDLS_MIC_LEN
);
551 wpa_hexdump(MSG_DEBUG
, "TDLS: Data for FTIE MIC", buf
, pos
- buf
);
552 wpa_hexdump_key(MSG_DEBUG
, "TDLS: KCK", kck
, 16);
553 ret
= omac1_aes_128(kck
, buf
, pos
- buf
, mic
);
555 wpa_hexdump(MSG_DEBUG
, "TDLS: FTIE MIC", mic
, 16);
560 static int wpa_supplicant_verify_tdls_mic(u8 trans_seq
,
561 struct wpa_tdls_peer
*peer
,
562 const u8
*lnkid
, const u8
*timeoutie
,
563 const struct wpa_tdls_ftie
*ftie
)
568 wpa_tdls_ftie_mic(peer
->tpk
.kck
, trans_seq
, lnkid
,
569 peer
->rsnie_p
, timeoutie
, (u8
*) ftie
,
571 if (os_memcmp_const(mic
, ftie
->mic
, 16) != 0) {
572 wpa_printf(MSG_INFO
, "TDLS: Invalid MIC in FTIE - "
574 wpa_hexdump(MSG_DEBUG
, "TDLS: Received MIC",
576 wpa_hexdump(MSG_DEBUG
, "TDLS: Calculated MIC",
581 wpa_printf(MSG_WARNING
, "TDLS: Could not verify TDLS MIC, "
582 "TPK not set - dropping packet");
589 static int wpa_supplicant_verify_tdls_mic_teardown(
590 u8 trans_seq
, u16 rcode
, u8 dtoken
, struct wpa_tdls_peer
*peer
,
591 const u8
*lnkid
, const struct wpa_tdls_ftie
*ftie
)
596 wpa_tdls_key_mic_teardown(peer
->tpk
.kck
, trans_seq
, rcode
,
597 dtoken
, lnkid
, (u8
*) ftie
, mic
);
598 if (os_memcmp_const(mic
, ftie
->mic
, 16) != 0) {
599 wpa_printf(MSG_INFO
, "TDLS: Invalid MIC in Teardown - "
604 wpa_printf(MSG_INFO
, "TDLS: Could not verify TDLS Teardown "
605 "MIC, TPK not set - dropping packet");
612 static void wpa_tdls_tpk_timeout(void *eloop_ctx
, void *timeout_ctx
)
614 struct wpa_sm
*sm
= eloop_ctx
;
615 struct wpa_tdls_peer
*peer
= timeout_ctx
;
618 * On TPK lifetime expiration, we have an option of either tearing down
619 * the direct link or trying to re-initiate it. The selection of what
620 * to do is not strictly speaking controlled by our role in the expired
621 * link, but for now, use that to select whether to renew or tear down
625 if (peer
->initiator
) {
626 wpa_printf(MSG_DEBUG
, "TDLS: TPK lifetime expired for " MACSTR
627 " - try to renew", MAC2STR(peer
->addr
));
628 wpa_tdls_start(sm
, peer
->addr
);
630 wpa_printf(MSG_DEBUG
, "TDLS: TPK lifetime expired for " MACSTR
631 " - tear down", MAC2STR(peer
->addr
));
632 wpa_tdls_do_teardown(sm
, peer
,
633 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED
);
638 static void wpa_tdls_peer_remove_from_list(struct wpa_sm
*sm
,
639 struct wpa_tdls_peer
*peer
)
641 struct wpa_tdls_peer
*cur
, *prev
;
645 while (cur
&& cur
!= peer
) {
651 wpa_printf(MSG_ERROR
, "TDLS: Could not find peer " MACSTR
652 " to remove it from the list",
653 MAC2STR(peer
->addr
));
658 prev
->next
= peer
->next
;
660 sm
->tdls
= peer
->next
;
664 static void wpa_tdls_peer_clear(struct wpa_sm
*sm
, struct wpa_tdls_peer
*peer
)
666 wpa_printf(MSG_DEBUG
, "TDLS: Clear state for peer " MACSTR
,
667 MAC2STR(peer
->addr
));
668 eloop_cancel_timeout(wpa_tdls_tpk_timeout
, sm
, peer
);
669 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout
, sm
, peer
);
670 peer
->reconfig_key
= 0;
672 peer
->tpk_in_progress
= 0;
673 os_free(peer
->sm_tmr
.buf
);
674 peer
->sm_tmr
.buf
= NULL
;
675 os_free(peer
->ht_capabilities
);
676 peer
->ht_capabilities
= NULL
;
677 os_free(peer
->vht_capabilities
);
678 peer
->vht_capabilities
= NULL
;
679 os_free(peer
->ext_capab
);
680 peer
->ext_capab
= NULL
;
681 os_free(peer
->supp_channels
);
682 peer
->supp_channels
= NULL
;
683 os_free(peer
->supp_oper_classes
);
684 peer
->supp_oper_classes
= NULL
;
685 peer
->rsnie_i_len
= peer
->rsnie_p_len
= 0;
688 peer
->wmm_capable
= 0;
689 peer
->tpk_set
= peer
->tpk_success
= 0;
690 os_memset(&peer
->tpk
, 0, sizeof(peer
->tpk
));
691 os_memset(peer
->inonce
, 0, WPA_NONCE_LEN
);
692 os_memset(peer
->rnonce
, 0, WPA_NONCE_LEN
);
696 static void wpa_tdls_peer_free(struct wpa_sm
*sm
, struct wpa_tdls_peer
*peer
)
698 wpa_tdls_peer_clear(sm
, peer
);
699 wpa_tdls_peer_remove_from_list(sm
, peer
);
704 static void wpa_tdls_linkid(struct wpa_sm
*sm
, struct wpa_tdls_peer
*peer
,
705 struct wpa_tdls_lnkid
*lnkid
)
707 lnkid
->ie_type
= WLAN_EID_LINK_ID
;
708 lnkid
->ie_len
= 3 * ETH_ALEN
;
709 os_memcpy(lnkid
->bssid
, sm
->bssid
, ETH_ALEN
);
710 if (peer
->initiator
) {
711 os_memcpy(lnkid
->init_sta
, sm
->own_addr
, ETH_ALEN
);
712 os_memcpy(lnkid
->resp_sta
, peer
->addr
, ETH_ALEN
);
714 os_memcpy(lnkid
->init_sta
, peer
->addr
, ETH_ALEN
);
715 os_memcpy(lnkid
->resp_sta
, sm
->own_addr
, ETH_ALEN
);
720 static int wpa_tdls_send_teardown(struct wpa_sm
*sm
, const u8
*addr
,
723 struct wpa_tdls_peer
*peer
;
724 struct wpa_tdls_ftie
*ftie
;
725 struct wpa_tdls_lnkid lnkid
;
730 if (sm
->tdls_disabled
|| !sm
->tdls_supported
)
733 /* Find the node and free from the list */
734 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
735 if (os_memcmp(peer
->addr
, addr
, ETH_ALEN
) == 0)
740 wpa_printf(MSG_INFO
, "TDLS: No matching entry found for "
741 "Teardown " MACSTR
, MAC2STR(addr
));
745 dialog_token
= peer
->dtoken
;
747 wpa_printf(MSG_DEBUG
, "TDLS: TDLS Teardown for " MACSTR
,
751 if (wpa_tdls_get_privacy(sm
) && peer
->tpk_set
&& peer
->tpk_success
) {
752 /* To add FTIE for Teardown request and compute MIC */
753 ielen
+= sizeof(*ftie
);
754 #ifdef CONFIG_TDLS_TESTING
755 if (tdls_testing
& TDLS_TESTING_LONG_FRAME
)
757 #endif /* CONFIG_TDLS_TESTING */
760 rbuf
= os_zalloc(ielen
+ 1);
765 if (!wpa_tdls_get_privacy(sm
) || !peer
->tpk_set
|| !peer
->tpk_success
)
768 ftie
= (struct wpa_tdls_ftie
*) pos
;
769 ftie
->ie_type
= WLAN_EID_FAST_BSS_TRANSITION
;
770 /* Using the recent nonce which should be for CONFIRM frame */
771 os_memcpy(ftie
->Anonce
, peer
->rnonce
, WPA_NONCE_LEN
);
772 os_memcpy(ftie
->Snonce
, peer
->inonce
, WPA_NONCE_LEN
);
773 ftie
->ie_len
= sizeof(struct wpa_tdls_ftie
) - 2;
774 pos
= (u8
*) (ftie
+ 1);
775 #ifdef CONFIG_TDLS_TESTING
776 if (tdls_testing
& TDLS_TESTING_LONG_FRAME
) {
777 wpa_printf(MSG_DEBUG
, "TDLS: Testing - add extra subelem to "
780 *pos
++ = 255; /* FTIE subelem */
781 *pos
++ = 168; /* FTIE subelem length */
784 #endif /* CONFIG_TDLS_TESTING */
785 wpa_hexdump(MSG_DEBUG
, "TDLS: FTIE for TDLS Teardown handshake",
786 (u8
*) ftie
, pos
- (u8
*) ftie
);
788 /* compute MIC before sending */
789 wpa_tdls_linkid(sm
, peer
, &lnkid
);
790 wpa_tdls_key_mic_teardown(peer
->tpk
.kck
, 4, reason_code
,
791 dialog_token
, (u8
*) &lnkid
, (u8
*) ftie
,
795 /* TODO: register for a Timeout handler, if Teardown is not received at
796 * the other end, then try again another time */
798 /* request driver to send Teardown using this FTIE */
799 wpa_tdls_tpk_send(sm
, addr
, WLAN_TDLS_TEARDOWN
, 0,
800 reason_code
, 0, peer
->initiator
, rbuf
, pos
- rbuf
);
807 int wpa_tdls_teardown_link(struct wpa_sm
*sm
, const u8
*addr
, u16 reason_code
)
809 struct wpa_tdls_peer
*peer
;
811 if (sm
->tdls_disabled
|| !sm
->tdls_supported
)
814 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
815 if (os_memcmp(peer
->addr
, addr
, ETH_ALEN
) == 0)
820 wpa_printf(MSG_DEBUG
, "TDLS: Could not find peer " MACSTR
821 " for link Teardown", MAC2STR(addr
));
825 if (!peer
->tpk_success
) {
826 wpa_printf(MSG_DEBUG
, "TDLS: Peer " MACSTR
827 " not connected - cannot Teardown link", MAC2STR(addr
));
831 return wpa_tdls_do_teardown(sm
, peer
, reason_code
);
835 static void wpa_tdls_disable_peer_link(struct wpa_sm
*sm
,
836 struct wpa_tdls_peer
*peer
)
838 wpa_sm_tdls_oper(sm
, TDLS_DISABLE_LINK
, peer
->addr
);
839 wpa_tdls_peer_free(sm
, peer
);
843 void wpa_tdls_disable_unreachable_link(struct wpa_sm
*sm
, const u8
*addr
)
845 struct wpa_tdls_peer
*peer
;
847 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
848 if (os_memcmp(peer
->addr
, addr
, ETH_ALEN
) == 0)
852 if (!peer
|| !peer
->tpk_success
) {
853 wpa_printf(MSG_DEBUG
, "TDLS: Peer " MACSTR
854 " not connected - cannot teardown unreachable link",
859 if (wpa_tdls_is_external_setup(sm
)) {
861 * Disable the link, send a teardown packet through the
862 * AP, and then reset link data.
864 wpa_sm_tdls_oper(sm
, TDLS_DISABLE_LINK
, addr
);
865 wpa_tdls_send_teardown(sm
, addr
,
866 WLAN_REASON_TDLS_TEARDOWN_UNREACHABLE
);
867 wpa_tdls_peer_free(sm
, peer
);
869 wpa_tdls_disable_peer_link(sm
, peer
);
874 const char * wpa_tdls_get_link_status(struct wpa_sm
*sm
, const u8
*addr
)
876 struct wpa_tdls_peer
*peer
;
878 if (sm
->tdls_disabled
|| !sm
->tdls_supported
)
881 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
882 if (os_memcmp(peer
->addr
, addr
, ETH_ALEN
) == 0)
887 return "peer does not exist";
889 if (!peer
->tpk_success
)
890 return "peer not connected";
896 static int wpa_tdls_recv_teardown(struct wpa_sm
*sm
, const u8
*src_addr
,
897 const u8
*buf
, size_t len
)
899 struct wpa_tdls_peer
*peer
= NULL
;
900 struct wpa_tdls_ftie
*ftie
;
901 struct wpa_tdls_lnkid
*lnkid
;
902 struct wpa_eapol_ie_parse kde
;
907 /* Find the node and free from the list */
908 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
909 if (os_memcmp(peer
->addr
, src_addr
, ETH_ALEN
) == 0)
914 wpa_printf(MSG_INFO
, "TDLS: No matching entry found for "
915 "Teardown " MACSTR
, MAC2STR(src_addr
));
920 pos
+= 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
922 reason_code
= WPA_GET_LE16(pos
);
925 wpa_printf(MSG_DEBUG
, "TDLS: TDLS Teardown Request from " MACSTR
926 " (reason code %u)", MAC2STR(src_addr
), reason_code
);
928 ielen
= len
- (pos
- buf
); /* start of IE in buf */
929 if (wpa_supplicant_parse_ies((const u8
*) pos
, ielen
, &kde
) < 0) {
930 wpa_printf(MSG_INFO
, "TDLS: Failed to parse IEs in Teardown");
934 if (kde
.lnkid
== NULL
|| kde
.lnkid_len
< 3 * ETH_ALEN
) {
935 wpa_printf(MSG_INFO
, "TDLS: No Link Identifier IE in TDLS "
939 lnkid
= (struct wpa_tdls_lnkid
*) kde
.lnkid
;
941 if (!wpa_tdls_get_privacy(sm
) || !peer
->tpk_set
|| !peer
->tpk_success
)
944 if (kde
.ftie
== NULL
|| kde
.ftie_len
< sizeof(*ftie
)) {
945 wpa_printf(MSG_INFO
, "TDLS: No FTIE in TDLS Teardown");
949 ftie
= (struct wpa_tdls_ftie
*) kde
.ftie
;
951 /* Process MIC check to see if TDLS Teardown is right */
952 if (wpa_supplicant_verify_tdls_mic_teardown(4, reason_code
,
954 (u8
*) lnkid
, ftie
) < 0) {
955 wpa_printf(MSG_DEBUG
, "TDLS: MIC failure for TDLS "
956 "Teardown Request from " MACSTR
, MAC2STR(src_addr
));
962 * Request the driver to disable the direct link and clear associated
965 wpa_tdls_disable_peer_link(sm
, peer
);
971 * wpa_tdls_send_error - To send suitable TDLS status response with
972 * appropriate status code mentioning reason for error/failure.
973 * @dst - MAC addr of Peer station
974 * @tdls_action - TDLS frame type for which error code is sent
975 * @initiator - was this end the initiator of the connection
976 * @status - status code mentioning reason
979 static int wpa_tdls_send_error(struct wpa_sm
*sm
, const u8
*dst
,
980 u8 tdls_action
, u8 dialog_token
, int initiator
,
983 wpa_printf(MSG_DEBUG
, "TDLS: Sending error to " MACSTR
984 " (action=%u status=%u)",
985 MAC2STR(dst
), tdls_action
, status
);
986 return wpa_tdls_tpk_send(sm
, dst
, tdls_action
, dialog_token
, status
,
987 0, initiator
, NULL
, 0);
991 static struct wpa_tdls_peer
*
992 wpa_tdls_add_peer(struct wpa_sm
*sm
, const u8
*addr
, int *existing
)
994 struct wpa_tdls_peer
*peer
;
998 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
999 if (os_memcmp(peer
->addr
, addr
, ETH_ALEN
) == 0) {
1002 return peer
; /* re-use existing entry */
1006 wpa_printf(MSG_INFO
, "TDLS: Creating peer entry for " MACSTR
,
1009 peer
= os_zalloc(sizeof(*peer
));
1013 os_memcpy(peer
->addr
, addr
, ETH_ALEN
);
1014 peer
->next
= sm
->tdls
;
1021 static int wpa_tdls_send_tpk_m1(struct wpa_sm
*sm
,
1022 struct wpa_tdls_peer
*peer
)
1025 struct wpa_tdls_timeoutie timeoutie
;
1027 struct wpa_tdls_ftie
*ftie
;
1028 u8
*rbuf
, *pos
, *count_pos
;
1030 struct rsn_ie_hdr
*hdr
;
1033 if (!wpa_tdls_get_privacy(sm
)) {
1034 wpa_printf(MSG_DEBUG
, "TDLS: No security used on the link");
1035 peer
->rsnie_i_len
= 0;
1040 * TPK Handshake Message 1:
1041 * FTIE: ANonce=0, SNonce=initiator nonce MIC=0, DataKDs=(RSNIE_I,
1042 * Timeout Interval IE))
1045 /* Filling RSN IE */
1046 hdr
= (struct rsn_ie_hdr
*) peer
->rsnie_i
;
1047 hdr
->elem_id
= WLAN_EID_RSN
;
1048 WPA_PUT_LE16(hdr
->version
, RSN_VERSION
);
1050 pos
= (u8
*) (hdr
+ 1);
1051 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED
);
1052 pos
+= RSN_SELECTOR_LEN
;
1059 * AES-CCMP is the default Encryption preferred for TDLS, so
1060 * RSN IE is filled only with CCMP CIPHER
1061 * Note: TKIP is not used to encrypt TDLS link.
1063 * Regardless of the cipher used on the AP connection, select CCMP
1066 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_CCMP
);
1067 pos
+= RSN_SELECTOR_LEN
;
1070 WPA_PUT_LE16(count_pos
, count
);
1072 WPA_PUT_LE16(pos
, 1);
1074 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE
);
1075 pos
+= RSN_SELECTOR_LEN
;
1077 rsn_capab
= WPA_CAPABILITY_PEERKEY_ENABLED
;
1078 rsn_capab
|= RSN_NUM_REPLAY_COUNTERS_16
<< 2;
1079 #ifdef CONFIG_TDLS_TESTING
1080 if (tdls_testing
& TDLS_TESTING_ALT_RSN_IE
) {
1081 wpa_printf(MSG_DEBUG
, "TDLS: Use alternative RSN IE for "
1083 rsn_capab
= WPA_CAPABILITY_PEERKEY_ENABLED
;
1085 #endif /* CONFIG_TDLS_TESTING */
1086 WPA_PUT_LE16(pos
, rsn_capab
);
1088 #ifdef CONFIG_TDLS_TESTING
1089 if (tdls_testing
& TDLS_TESTING_ALT_RSN_IE
) {
1090 /* Number of PMKIDs */
1094 #endif /* CONFIG_TDLS_TESTING */
1096 hdr
->len
= (pos
- peer
->rsnie_i
) - 2;
1097 peer
->rsnie_i_len
= pos
- peer
->rsnie_i
;
1098 wpa_hexdump(MSG_DEBUG
, "TDLS: RSN IE for TPK handshake",
1099 peer
->rsnie_i
, peer
->rsnie_i_len
);
1103 if (wpa_tdls_get_privacy(sm
))
1104 buf_len
+= peer
->rsnie_i_len
+ sizeof(struct wpa_tdls_ftie
) +
1105 sizeof(struct wpa_tdls_timeoutie
);
1106 #ifdef CONFIG_TDLS_TESTING
1107 if (wpa_tdls_get_privacy(sm
) &&
1108 (tdls_testing
& TDLS_TESTING_LONG_FRAME
))
1110 if (tdls_testing
& TDLS_TESTING_DIFF_BSSID
)
1111 buf_len
+= sizeof(struct wpa_tdls_lnkid
);
1112 #endif /* CONFIG_TDLS_TESTING */
1113 rbuf
= os_zalloc(buf_len
+ 1);
1115 wpa_tdls_peer_free(sm
, peer
);
1120 if (!wpa_tdls_get_privacy(sm
))
1123 /* Initiator RSN IE */
1124 pos
= wpa_add_ie(pos
, peer
->rsnie_i
, peer
->rsnie_i_len
);
1126 ftie
= (struct wpa_tdls_ftie
*) pos
;
1127 ftie
->ie_type
= WLAN_EID_FAST_BSS_TRANSITION
;
1128 ftie
->ie_len
= sizeof(struct wpa_tdls_ftie
) - 2;
1130 if (os_get_random(peer
->inonce
, WPA_NONCE_LEN
)) {
1131 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1132 "TDLS: Failed to get random data for initiator Nonce");
1134 wpa_tdls_peer_free(sm
, peer
);
1137 wpa_hexdump(MSG_DEBUG
, "TDLS: Initiator Nonce for TPK handshake",
1138 peer
->inonce
, WPA_NONCE_LEN
);
1139 os_memcpy(ftie
->Snonce
, peer
->inonce
, WPA_NONCE_LEN
);
1141 wpa_hexdump(MSG_DEBUG
, "TDLS: FTIE for TPK Handshake M1",
1142 (u8
*) ftie
, sizeof(struct wpa_tdls_ftie
));
1144 pos
= (u8
*) (ftie
+ 1);
1146 #ifdef CONFIG_TDLS_TESTING
1147 if (tdls_testing
& TDLS_TESTING_LONG_FRAME
) {
1148 wpa_printf(MSG_DEBUG
, "TDLS: Testing - add extra subelem to "
1150 ftie
->ie_len
+= 170;
1151 *pos
++ = 255; /* FTIE subelem */
1152 *pos
++ = 168; /* FTIE subelem length */
1155 #endif /* CONFIG_TDLS_TESTING */
1158 peer
->lifetime
= TPK_LIFETIME
;
1159 #ifdef CONFIG_TDLS_TESTING
1160 if (tdls_testing
& TDLS_TESTING_SHORT_LIFETIME
) {
1161 wpa_printf(MSG_DEBUG
, "TDLS: Testing - use short TPK "
1163 peer
->lifetime
= 301;
1165 if (tdls_testing
& TDLS_TESTING_LONG_LIFETIME
) {
1166 wpa_printf(MSG_DEBUG
, "TDLS: Testing - use long TPK "
1168 peer
->lifetime
= 0xffffffff;
1170 #endif /* CONFIG_TDLS_TESTING */
1171 pos
= wpa_add_tdls_timeoutie(pos
, (u8
*) &timeoutie
,
1172 sizeof(timeoutie
), peer
->lifetime
);
1173 wpa_printf(MSG_DEBUG
, "TDLS: TPK lifetime %u seconds", peer
->lifetime
);
1177 #ifdef CONFIG_TDLS_TESTING
1178 if (tdls_testing
& TDLS_TESTING_DIFF_BSSID
) {
1179 wpa_printf(MSG_DEBUG
, "TDLS: Testing - use incorrect BSSID in "
1181 struct wpa_tdls_lnkid
*l
= (struct wpa_tdls_lnkid
*) pos
;
1182 wpa_tdls_linkid(sm
, peer
, l
);
1183 l
->bssid
[5] ^= 0x01;
1186 #endif /* CONFIG_TDLS_TESTING */
1188 wpa_printf(MSG_DEBUG
, "TDLS: Sending TDLS Setup Request / TPK "
1189 "Handshake Message 1 (peer " MACSTR
")",
1190 MAC2STR(peer
->addr
));
1192 status
= wpa_tdls_tpk_send(sm
, peer
->addr
, WLAN_TDLS_SETUP_REQUEST
,
1193 1, 0, 0, peer
->initiator
, rbuf
, pos
- rbuf
);
1200 static int wpa_tdls_send_tpk_m2(struct wpa_sm
*sm
,
1201 const unsigned char *src_addr
, u8 dtoken
,
1202 struct wpa_tdls_lnkid
*lnkid
,
1203 const struct wpa_tdls_peer
*peer
)
1208 struct wpa_tdls_timeoutie timeoutie
;
1209 struct wpa_tdls_ftie
*ftie
;
1213 if (wpa_tdls_get_privacy(sm
)) {
1214 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1216 buf_len
+= peer
->rsnie_i_len
+ sizeof(struct wpa_tdls_ftie
) +
1217 sizeof(struct wpa_tdls_timeoutie
);
1218 #ifdef CONFIG_TDLS_TESTING
1219 if (tdls_testing
& TDLS_TESTING_LONG_FRAME
)
1221 #endif /* CONFIG_TDLS_TESTING */
1224 rbuf
= os_zalloc(buf_len
+ 1);
1229 if (!wpa_tdls_get_privacy(sm
))
1233 pos
= wpa_add_ie(pos
, peer
->rsnie_p
, peer
->rsnie_p_len
);
1235 ftie
= (struct wpa_tdls_ftie
*) pos
;
1236 ftie
->ie_type
= WLAN_EID_FAST_BSS_TRANSITION
;
1237 /* TODO: ftie->mic_control to set 2-RESPONSE */
1238 os_memcpy(ftie
->Anonce
, peer
->rnonce
, WPA_NONCE_LEN
);
1239 os_memcpy(ftie
->Snonce
, peer
->inonce
, WPA_NONCE_LEN
);
1240 ftie
->ie_len
= sizeof(struct wpa_tdls_ftie
) - 2;
1241 wpa_hexdump(MSG_DEBUG
, "TDLS: FTIE for TPK M2",
1242 (u8
*) ftie
, sizeof(*ftie
));
1244 pos
= (u8
*) (ftie
+ 1);
1246 #ifdef CONFIG_TDLS_TESTING
1247 if (tdls_testing
& TDLS_TESTING_LONG_FRAME
) {
1248 wpa_printf(MSG_DEBUG
, "TDLS: Testing - add extra subelem to "
1250 ftie
->ie_len
+= 170;
1251 *pos
++ = 255; /* FTIE subelem */
1252 *pos
++ = 168; /* FTIE subelem length */
1255 #endif /* CONFIG_TDLS_TESTING */
1258 lifetime
= peer
->lifetime
;
1259 #ifdef CONFIG_TDLS_TESTING
1260 if (tdls_testing
& TDLS_TESTING_WRONG_LIFETIME_RESP
) {
1261 wpa_printf(MSG_DEBUG
, "TDLS: Testing - use wrong TPK "
1262 "lifetime in response");
1265 #endif /* CONFIG_TDLS_TESTING */
1266 pos
= wpa_add_tdls_timeoutie(pos
, (u8
*) &timeoutie
,
1267 sizeof(timeoutie
), lifetime
);
1268 wpa_printf(MSG_DEBUG
, "TDLS: TPK lifetime %u seconds from initiator",
1271 /* compute MIC before sending */
1272 wpa_tdls_ftie_mic(peer
->tpk
.kck
, 2, (u8
*) lnkid
, peer
->rsnie_p
,
1273 (u8
*) &timeoutie
, (u8
*) ftie
, ftie
->mic
);
1274 #ifdef CONFIG_TDLS_TESTING
1275 if (tdls_testing
& TDLS_TESTING_WRONG_MIC
) {
1276 wpa_printf(MSG_DEBUG
, "TDLS: Testing - use wrong MIC");
1277 ftie
->mic
[0] ^= 0x01;
1279 #endif /* CONFIG_TDLS_TESTING */
1282 status
= wpa_tdls_tpk_send(sm
, src_addr
, WLAN_TDLS_SETUP_RESPONSE
,
1283 dtoken
, 0, 0, peer
->initiator
, rbuf
,
1291 static int wpa_tdls_send_tpk_m3(struct wpa_sm
*sm
,
1292 const unsigned char *src_addr
, u8 dtoken
,
1293 struct wpa_tdls_lnkid
*lnkid
,
1294 const struct wpa_tdls_peer
*peer
)
1298 struct wpa_tdls_ftie
*ftie
;
1299 struct wpa_tdls_timeoutie timeoutie
;
1305 if (wpa_tdls_get_privacy(sm
)) {
1306 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1308 buf_len
+= peer
->rsnie_i_len
+ sizeof(struct wpa_tdls_ftie
) +
1309 sizeof(struct wpa_tdls_timeoutie
);
1310 #ifdef CONFIG_TDLS_TESTING
1311 if (tdls_testing
& TDLS_TESTING_LONG_FRAME
)
1313 #endif /* CONFIG_TDLS_TESTING */
1316 rbuf
= os_zalloc(buf_len
+ 1);
1321 if (!wpa_tdls_get_privacy(sm
))
1325 pos
= wpa_add_ie(pos
, peer
->rsnie_p
, peer
->rsnie_p_len
);
1327 ftie
= (struct wpa_tdls_ftie
*) pos
;
1328 ftie
->ie_type
= WLAN_EID_FAST_BSS_TRANSITION
;
1329 /*TODO: ftie->mic_control to set 3-CONFIRM */
1330 os_memcpy(ftie
->Anonce
, peer
->rnonce
, WPA_NONCE_LEN
);
1331 os_memcpy(ftie
->Snonce
, peer
->inonce
, WPA_NONCE_LEN
);
1332 ftie
->ie_len
= sizeof(struct wpa_tdls_ftie
) - 2;
1334 pos
= (u8
*) (ftie
+ 1);
1336 #ifdef CONFIG_TDLS_TESTING
1337 if (tdls_testing
& TDLS_TESTING_LONG_FRAME
) {
1338 wpa_printf(MSG_DEBUG
, "TDLS: Testing - add extra subelem to "
1340 ftie
->ie_len
+= 170;
1341 *pos
++ = 255; /* FTIE subelem */
1342 *pos
++ = 168; /* FTIE subelem length */
1345 #endif /* CONFIG_TDLS_TESTING */
1348 lifetime
= peer
->lifetime
;
1349 #ifdef CONFIG_TDLS_TESTING
1350 if (tdls_testing
& TDLS_TESTING_WRONG_LIFETIME_CONF
) {
1351 wpa_printf(MSG_DEBUG
, "TDLS: Testing - use wrong TPK "
1352 "lifetime in confirm");
1355 #endif /* CONFIG_TDLS_TESTING */
1356 pos
= wpa_add_tdls_timeoutie(pos
, (u8
*) &timeoutie
,
1357 sizeof(timeoutie
), lifetime
);
1358 wpa_printf(MSG_DEBUG
, "TDLS: TPK lifetime %u seconds",
1361 /* compute MIC before sending */
1362 wpa_tdls_ftie_mic(peer
->tpk
.kck
, 3, (u8
*) lnkid
, peer
->rsnie_p
,
1363 (u8
*) &timeoutie
, (u8
*) ftie
, ftie
->mic
);
1364 #ifdef CONFIG_TDLS_TESTING
1365 if (tdls_testing
& TDLS_TESTING_WRONG_MIC
) {
1366 wpa_printf(MSG_DEBUG
, "TDLS: Testing - use wrong MIC");
1367 ftie
->mic
[0] ^= 0x01;
1369 #endif /* CONFIG_TDLS_TESTING */
1373 if (peer
->vht_capabilities
)
1374 peer_capab
|= TDLS_PEER_VHT
;
1375 if (peer
->ht_capabilities
)
1376 peer_capab
|= TDLS_PEER_HT
;
1377 if (peer
->wmm_capable
)
1378 peer_capab
|= TDLS_PEER_WMM
;
1380 status
= wpa_tdls_tpk_send(sm
, src_addr
, WLAN_TDLS_SETUP_CONFIRM
,
1381 dtoken
, 0, peer_capab
, peer
->initiator
,
1389 static int wpa_tdls_send_discovery_response(struct wpa_sm
*sm
,
1390 struct wpa_tdls_peer
*peer
,
1394 struct wpa_tdls_timeoutie timeoutie
;
1396 u8
*rbuf
, *pos
, *count_pos
;
1398 struct rsn_ie_hdr
*hdr
;
1401 wpa_printf(MSG_DEBUG
, "TDLS: Sending TDLS Discovery Response "
1402 "(peer " MACSTR
")", MAC2STR(peer
->addr
));
1403 if (!wpa_tdls_get_privacy(sm
))
1406 /* Filling RSN IE */
1407 hdr
= (struct rsn_ie_hdr
*) peer
->rsnie_i
;
1408 hdr
->elem_id
= WLAN_EID_RSN
;
1409 WPA_PUT_LE16(hdr
->version
, RSN_VERSION
);
1410 pos
= (u8
*) (hdr
+ 1);
1411 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED
);
1412 pos
+= RSN_SELECTOR_LEN
;
1418 * AES-CCMP is the default encryption preferred for TDLS, so
1419 * RSN IE is filled only with CCMP cipher suite.
1420 * Note: TKIP is not used to encrypt TDLS link.
1422 * Regardless of the cipher used on the AP connection, select CCMP
1425 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_CCMP
);
1426 pos
+= RSN_SELECTOR_LEN
;
1428 WPA_PUT_LE16(count_pos
, count
);
1429 WPA_PUT_LE16(pos
, 1);
1431 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE
);
1432 pos
+= RSN_SELECTOR_LEN
;
1434 rsn_capab
= WPA_CAPABILITY_PEERKEY_ENABLED
;
1435 rsn_capab
|= RSN_NUM_REPLAY_COUNTERS_16
<< 2;
1436 WPA_PUT_LE16(pos
, rsn_capab
);
1438 hdr
->len
= (pos
- (u8
*) hdr
) - 2;
1439 peer
->rsnie_i_len
= pos
- peer
->rsnie_i
;
1441 wpa_hexdump(MSG_DEBUG
, "TDLS: RSN IE for Discovery Response",
1442 (u8
*) hdr
, hdr
->len
+ 2);
1445 if (wpa_tdls_get_privacy(sm
)) {
1446 /* Peer RSN IE, Lifetime */
1447 buf_len
+= peer
->rsnie_i_len
+
1448 sizeof(struct wpa_tdls_timeoutie
);
1450 rbuf
= os_zalloc(buf_len
+ 1);
1452 wpa_tdls_peer_free(sm
, peer
);
1457 if (!wpa_tdls_get_privacy(sm
))
1459 /* Initiator RSN IE */
1460 pos
= wpa_add_ie(pos
, peer
->rsnie_i
, peer
->rsnie_i_len
);
1462 peer
->lifetime
= TPK_LIFETIME
;
1463 pos
= wpa_add_tdls_timeoutie(pos
, (u8
*) &timeoutie
,
1464 sizeof(timeoutie
), peer
->lifetime
);
1465 wpa_printf(MSG_DEBUG
, "TDLS: TPK lifetime %u seconds", peer
->lifetime
);
1467 status
= wpa_tdls_tpk_send(sm
, peer
->addr
, WLAN_TDLS_DISCOVERY_RESPONSE
,
1468 dialog_token
, 0, 0, 0, rbuf
, pos
- rbuf
);
1476 wpa_tdls_process_discovery_request(struct wpa_sm
*sm
, const u8
*addr
,
1477 const u8
*buf
, size_t len
)
1479 struct wpa_eapol_ie_parse kde
;
1480 const struct wpa_tdls_lnkid
*lnkid
;
1481 struct wpa_tdls_peer
*peer
;
1482 size_t min_req_len
= sizeof(struct wpa_tdls_frame
) +
1483 1 /* dialog token */ + sizeof(struct wpa_tdls_lnkid
);
1486 wpa_printf(MSG_DEBUG
, "TDLS: Discovery Request from " MACSTR
,
1489 if (len
< min_req_len
) {
1490 wpa_printf(MSG_DEBUG
, "TDLS Discovery Request is too short: "
1495 dialog_token
= buf
[sizeof(struct wpa_tdls_frame
)];
1498 * Some APs will tack on a weird IE to the end of a TDLS
1499 * discovery request packet. This needn't fail the response,
1500 * since the required IE are verified separately.
1502 if (wpa_supplicant_parse_ies(buf
+ sizeof(struct wpa_tdls_frame
) + 1,
1503 len
- (sizeof(struct wpa_tdls_frame
) + 1),
1505 wpa_printf(MSG_DEBUG
,
1506 "TDLS: Failed to parse IEs in Discovery Request - ignore as an interop workaround");
1510 wpa_printf(MSG_DEBUG
, "TDLS: Link ID not found in Discovery "
1515 lnkid
= (const struct wpa_tdls_lnkid
*) kde
.lnkid
;
1517 if (os_memcmp(sm
->bssid
, lnkid
->bssid
, ETH_ALEN
) != 0) {
1518 wpa_printf(MSG_DEBUG
, "TDLS: Discovery Request from different "
1519 " BSS " MACSTR
, MAC2STR(lnkid
->bssid
));
1523 peer
= wpa_tdls_add_peer(sm
, addr
, NULL
);
1527 return wpa_tdls_send_discovery_response(sm
, peer
, dialog_token
);
1531 int wpa_tdls_send_discovery_request(struct wpa_sm
*sm
, const u8
*addr
)
1533 if (sm
->tdls_disabled
|| !sm
->tdls_supported
)
1536 wpa_printf(MSG_DEBUG
, "TDLS: Sending Discovery Request to peer "
1537 MACSTR
, MAC2STR(addr
));
1538 return wpa_tdls_tpk_send(sm
, addr
, WLAN_TDLS_DISCOVERY_REQUEST
,
1539 1, 0, 0, 1, NULL
, 0);
1543 static int copy_supp_rates(const struct wpa_eapol_ie_parse
*kde
,
1544 struct wpa_tdls_peer
*peer
)
1546 if (!kde
->supp_rates
) {
1547 wpa_printf(MSG_DEBUG
, "TDLS: No supported rates received");
1550 peer
->supp_rates_len
= merge_byte_arrays(
1551 peer
->supp_rates
, sizeof(peer
->supp_rates
),
1552 kde
->supp_rates
+ 2, kde
->supp_rates_len
- 2,
1553 kde
->ext_supp_rates
? kde
->ext_supp_rates
+ 2 : NULL
,
1554 kde
->ext_supp_rates_len
- 2);
1559 static int copy_peer_ht_capab(const struct wpa_eapol_ie_parse
*kde
,
1560 struct wpa_tdls_peer
*peer
)
1562 if (!kde
->ht_capabilities
||
1563 kde
->ht_capabilities_len
<
1564 sizeof(struct ieee80211_ht_capabilities
) ) {
1565 wpa_printf(MSG_DEBUG
, "TDLS: No supported ht capabilities "
1570 if (!peer
->ht_capabilities
) {
1571 peer
->ht_capabilities
=
1572 os_zalloc(sizeof(struct ieee80211_ht_capabilities
));
1573 if (peer
->ht_capabilities
== NULL
)
1577 os_memcpy(peer
->ht_capabilities
, kde
->ht_capabilities
,
1578 sizeof(struct ieee80211_ht_capabilities
));
1579 wpa_hexdump(MSG_DEBUG
, "TDLS: Peer HT capabilities",
1580 (u8
*) peer
->ht_capabilities
,
1581 sizeof(struct ieee80211_ht_capabilities
));
1587 static int copy_peer_vht_capab(const struct wpa_eapol_ie_parse
*kde
,
1588 struct wpa_tdls_peer
*peer
)
1590 if (!kde
->vht_capabilities
||
1591 kde
->vht_capabilities_len
<
1592 sizeof(struct ieee80211_vht_capabilities
) ) {
1593 wpa_printf(MSG_DEBUG
, "TDLS: No supported vht capabilities "
1598 if (!peer
->vht_capabilities
) {
1599 peer
->vht_capabilities
=
1600 os_zalloc(sizeof(struct ieee80211_vht_capabilities
));
1601 if (peer
->vht_capabilities
== NULL
)
1605 os_memcpy(peer
->vht_capabilities
, kde
->vht_capabilities
,
1606 sizeof(struct ieee80211_vht_capabilities
));
1607 wpa_hexdump(MSG_DEBUG
, "TDLS: Peer VHT capabilities",
1608 (u8
*) peer
->vht_capabilities
,
1609 sizeof(struct ieee80211_vht_capabilities
));
1615 static int copy_peer_ext_capab(const struct wpa_eapol_ie_parse
*kde
,
1616 struct wpa_tdls_peer
*peer
)
1618 if (!kde
->ext_capab
) {
1619 wpa_printf(MSG_DEBUG
, "TDLS: No extended capabilities "
1624 if (!peer
->ext_capab
|| peer
->ext_capab_len
< kde
->ext_capab_len
- 2) {
1625 /* Need to allocate buffer to fit the new information */
1626 os_free(peer
->ext_capab
);
1627 peer
->ext_capab
= os_zalloc(kde
->ext_capab_len
- 2);
1628 if (peer
->ext_capab
== NULL
)
1632 peer
->ext_capab_len
= kde
->ext_capab_len
- 2;
1633 os_memcpy(peer
->ext_capab
, kde
->ext_capab
+ 2, peer
->ext_capab_len
);
1639 static int copy_peer_wmm_capab(const struct wpa_eapol_ie_parse
*kde
,
1640 struct wpa_tdls_peer
*peer
)
1642 struct wmm_information_element
*wmm
;
1645 wpa_printf(MSG_DEBUG
, "TDLS: No supported WMM capabilities received");
1649 if (kde
->wmm_len
< sizeof(struct wmm_information_element
)) {
1650 wpa_printf(MSG_DEBUG
, "TDLS: Invalid supported WMM capabilities received");
1654 wmm
= (struct wmm_information_element
*) kde
->wmm
;
1655 peer
->qos_info
= wmm
->qos_info
;
1657 peer
->wmm_capable
= 1;
1659 wpa_printf(MSG_DEBUG
, "TDLS: Peer WMM QOS Info 0x%x", peer
->qos_info
);
1664 static int copy_peer_supp_channels(const struct wpa_eapol_ie_parse
*kde
,
1665 struct wpa_tdls_peer
*peer
)
1667 if (!kde
->supp_channels
) {
1668 wpa_printf(MSG_DEBUG
, "TDLS: No supported channels received");
1672 if (!peer
->supp_channels
||
1673 peer
->supp_channels_len
< kde
->supp_channels_len
) {
1674 os_free(peer
->supp_channels
);
1675 peer
->supp_channels
= os_zalloc(kde
->supp_channels_len
);
1676 if (peer
->supp_channels
== NULL
)
1680 peer
->supp_channels_len
= kde
->supp_channels_len
;
1682 os_memcpy(peer
->supp_channels
, kde
->supp_channels
,
1683 peer
->supp_channels_len
);
1684 wpa_hexdump(MSG_DEBUG
, "TDLS: Peer Supported Channels",
1685 (u8
*) peer
->supp_channels
, peer
->supp_channels_len
);
1690 static int copy_peer_supp_oper_classes(const struct wpa_eapol_ie_parse
*kde
,
1691 struct wpa_tdls_peer
*peer
)
1693 if (!kde
->supp_oper_classes
) {
1694 wpa_printf(MSG_DEBUG
, "TDLS: No supported operating classes received");
1698 if (!peer
->supp_oper_classes
||
1699 peer
->supp_oper_classes_len
< kde
->supp_oper_classes_len
) {
1700 os_free(peer
->supp_oper_classes
);
1701 peer
->supp_oper_classes
= os_zalloc(kde
->supp_oper_classes_len
);
1702 if (peer
->supp_oper_classes
== NULL
)
1706 peer
->supp_oper_classes_len
= kde
->supp_oper_classes_len
;
1707 os_memcpy(peer
->supp_oper_classes
, kde
->supp_oper_classes
,
1708 peer
->supp_oper_classes_len
);
1709 wpa_hexdump(MSG_DEBUG
, "TDLS: Peer Supported Operating Classes",
1710 (u8
*) peer
->supp_oper_classes
,
1711 peer
->supp_oper_classes_len
);
1716 static int wpa_tdls_addset_peer(struct wpa_sm
*sm
, struct wpa_tdls_peer
*peer
,
1719 return wpa_sm_tdls_peer_addset(sm
, peer
->addr
, add
, peer
->aid
,
1721 peer
->supp_rates
, peer
->supp_rates_len
,
1722 peer
->ht_capabilities
,
1723 peer
->vht_capabilities
,
1724 peer
->qos_info
, peer
->wmm_capable
,
1725 peer
->ext_capab
, peer
->ext_capab_len
,
1726 peer
->supp_channels
,
1727 peer
->supp_channels_len
,
1728 peer
->supp_oper_classes
,
1729 peer
->supp_oper_classes_len
);
1733 static int wpa_tdls_process_tpk_m1(struct wpa_sm
*sm
, const u8
*src_addr
,
1734 const u8
*buf
, size_t len
)
1736 struct wpa_tdls_peer
*peer
;
1737 struct wpa_eapol_ie_parse kde
;
1738 struct wpa_ie_data ie
;
1741 struct wpa_tdls_ftie
*ftie
= NULL
;
1742 struct wpa_tdls_timeoutie
*timeoutie
;
1743 struct wpa_tdls_lnkid
*lnkid
;
1746 struct rsn_ie_hdr
*hdr
;
1753 u16 status
= WLAN_STATUS_UNSPECIFIED_FAILURE
;
1754 int tdls_prohibited
= sm
->tdls_prohibited
;
1755 int existing_peer
= 0;
1761 cpos
+= 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1763 /* driver had already verified the frame format */
1764 dtoken
= *cpos
++; /* dialog token */
1766 wpa_printf(MSG_INFO
, "TDLS: Dialog Token in TPK M1 %d", dtoken
);
1768 peer
= wpa_tdls_add_peer(sm
, src_addr
, &existing_peer
);
1772 /* If found, use existing entry instead of adding a new one;
1773 * how to handle the case where both ends initiate at the
1775 if (existing_peer
) {
1776 if (peer
->tpk_success
) {
1777 wpa_printf(MSG_DEBUG
, "TDLS: TDLS Setup Request while "
1778 "direct link is enabled - tear down the "
1780 wpa_sm_tdls_oper(sm
, TDLS_DISABLE_LINK
, peer
->addr
);
1781 wpa_tdls_peer_clear(sm
, peer
);
1782 } else if (peer
->initiator
) {
1784 * An entry is already present, so check if we already
1785 * sent a TDLS Setup Request. If so, compare MAC
1786 * addresses and let the STA with the lower MAC address
1787 * continue as the initiator. The other negotiation is
1790 if (os_memcmp(sm
->own_addr
, src_addr
, ETH_ALEN
) < 0) {
1791 wpa_printf(MSG_DEBUG
, "TDLS: Discard request "
1792 "from peer with higher address "
1793 MACSTR
, MAC2STR(src_addr
));
1796 wpa_printf(MSG_DEBUG
, "TDLS: Accept request "
1797 "from peer with lower address "
1798 MACSTR
" (terminate previously "
1799 "initiated negotiation",
1801 wpa_sm_tdls_oper(sm
, TDLS_DISABLE_LINK
,
1803 wpa_tdls_peer_clear(sm
, peer
);
1808 /* capability information */
1809 peer
->capability
= WPA_GET_LE16(cpos
);
1812 ielen
= len
- (cpos
- buf
); /* start of IE in buf */
1813 if (wpa_supplicant_parse_ies(cpos
, ielen
, &kde
) < 0) {
1814 wpa_printf(MSG_INFO
, "TDLS: Failed to parse IEs in TPK M1");
1818 if (kde
.lnkid
== NULL
|| kde
.lnkid_len
< 3 * ETH_ALEN
) {
1819 wpa_printf(MSG_INFO
, "TDLS: No valid Link Identifier IE in "
1823 wpa_hexdump(MSG_DEBUG
, "TDLS: Link ID Received from TPK M1",
1824 kde
.lnkid
, kde
.lnkid_len
);
1825 lnkid
= (struct wpa_tdls_lnkid
*) kde
.lnkid
;
1826 if (os_memcmp(sm
->bssid
, lnkid
->bssid
, ETH_ALEN
) != 0) {
1827 wpa_printf(MSG_INFO
, "TDLS: TPK M1 from diff BSS");
1828 status
= WLAN_STATUS_REQUEST_DECLINED
;
1832 wpa_printf(MSG_DEBUG
, "TDLS: TPK M1 - TPK initiator " MACSTR
,
1835 if (copy_supp_rates(&kde
, peer
) < 0)
1838 if (copy_peer_ht_capab(&kde
, peer
) < 0)
1841 if (copy_peer_vht_capab(&kde
, peer
) < 0)
1844 if (copy_peer_ext_capab(&kde
, peer
) < 0)
1847 if (copy_peer_supp_channels(&kde
, peer
) < 0)
1850 if (copy_peer_supp_oper_classes(&kde
, peer
) < 0)
1853 peer
->qos_info
= kde
.qosinfo
;
1855 /* Overwrite with the qos_info obtained in WMM IE */
1856 if (copy_peer_wmm_capab(&kde
, peer
) < 0)
1859 peer
->aid
= kde
.aid
;
1861 #ifdef CONFIG_TDLS_TESTING
1862 if (tdls_testing
& TDLS_TESTING_CONCURRENT_INIT
) {
1863 peer
= wpa_tdls_add_peer(sm
, src_addr
, NULL
);
1866 wpa_printf(MSG_DEBUG
, "TDLS: Testing concurrent initiation of "
1867 "TDLS setup - send own request");
1868 peer
->initiator
= 1;
1869 wpa_sm_tdls_peer_addset(sm
, peer
->addr
, 1, 0, 0, NULL
, 0, NULL
,
1870 NULL
, 0, 0, NULL
, 0, NULL
, 0, NULL
, 0);
1871 wpa_tdls_send_tpk_m1(sm
, peer
);
1874 if ((tdls_testing
& TDLS_TESTING_IGNORE_AP_PROHIBIT
) &&
1876 wpa_printf(MSG_DEBUG
, "TDLS: Testing - ignore AP prohibition "
1878 tdls_prohibited
= 0;
1880 #endif /* CONFIG_TDLS_TESTING */
1882 if (tdls_prohibited
) {
1883 wpa_printf(MSG_INFO
, "TDLS: TDLS prohibited in this BSS");
1884 status
= WLAN_STATUS_REQUEST_DECLINED
;
1888 if (!wpa_tdls_get_privacy(sm
)) {
1890 wpa_printf(MSG_INFO
, "TDLS: RSN IE in TPK M1 while "
1891 "security is disabled");
1892 status
= WLAN_STATUS_SECURITY_DISABLED
;
1898 if (kde
.ftie
== NULL
|| kde
.ftie_len
< sizeof(*ftie
) ||
1899 kde
.rsn_ie
== NULL
) {
1900 wpa_printf(MSG_INFO
, "TDLS: No FTIE or RSN IE in TPK M1");
1901 status
= WLAN_STATUS_INVALID_PARAMETERS
;
1905 if (kde
.rsn_ie_len
> TDLS_MAX_IE_LEN
) {
1906 wpa_printf(MSG_INFO
, "TDLS: Too long Initiator RSN IE in "
1908 status
= WLAN_STATUS_INVALID_RSNIE
;
1912 if (wpa_parse_wpa_ie_rsn(kde
.rsn_ie
, kde
.rsn_ie_len
, &ie
) < 0) {
1913 wpa_printf(MSG_INFO
, "TDLS: Failed to parse RSN IE in TPK M1");
1914 status
= WLAN_STATUS_INVALID_RSNIE
;
1918 cipher
= ie
.pairwise_cipher
;
1919 if (cipher
& WPA_CIPHER_CCMP
) {
1920 wpa_printf(MSG_DEBUG
, "TDLS: Using CCMP for direct link");
1921 cipher
= WPA_CIPHER_CCMP
;
1923 wpa_printf(MSG_INFO
, "TDLS: No acceptable cipher in TPK M1");
1924 status
= WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID
;
1928 if ((ie
.capabilities
&
1929 (WPA_CAPABILITY_NO_PAIRWISE
| WPA_CAPABILITY_PEERKEY_ENABLED
)) !=
1930 WPA_CAPABILITY_PEERKEY_ENABLED
) {
1931 wpa_printf(MSG_INFO
, "TDLS: Invalid RSN Capabilities in "
1933 status
= WLAN_STATUS_INVALID_RSN_IE_CAPAB
;
1938 if (kde
.key_lifetime
== NULL
) {
1939 wpa_printf(MSG_INFO
, "TDLS: No Key Lifetime IE in TPK M1");
1940 status
= WLAN_STATUS_UNACCEPTABLE_LIFETIME
;
1943 timeoutie
= (struct wpa_tdls_timeoutie
*) kde
.key_lifetime
;
1944 lifetime
= WPA_GET_LE32(timeoutie
->value
);
1945 wpa_printf(MSG_DEBUG
, "TDLS: TPK lifetime %u seconds", lifetime
);
1946 if (lifetime
< 300) {
1947 wpa_printf(MSG_INFO
, "TDLS: Too short TPK lifetime");
1948 status
= WLAN_STATUS_UNACCEPTABLE_LIFETIME
;
1953 #ifdef CONFIG_TDLS_TESTING
1954 if (tdls_testing
& TDLS_TESTING_CONCURRENT_INIT
) {
1955 if (os_memcmp(sm
->own_addr
, peer
->addr
, ETH_ALEN
) < 0) {
1957 * The request frame from us is going to win, so do not
1958 * replace information based on this request frame from
1961 goto skip_rsn_check
;
1964 #endif /* CONFIG_TDLS_TESTING */
1966 peer
->initiator
= 0; /* Need to check */
1967 peer
->dtoken
= dtoken
;
1969 if (!wpa_tdls_get_privacy(sm
)) {
1970 peer
->rsnie_i_len
= 0;
1971 peer
->rsnie_p_len
= 0;
1972 peer
->cipher
= WPA_CIPHER_NONE
;
1973 goto skip_rsn_check
;
1976 ftie
= (struct wpa_tdls_ftie
*) kde
.ftie
;
1977 os_memcpy(peer
->rsnie_i
, kde
.rsn_ie
, kde
.rsn_ie_len
);
1978 peer
->rsnie_i_len
= kde
.rsn_ie_len
;
1979 peer
->cipher
= cipher
;
1981 if (os_memcmp(peer
->inonce
, ftie
->Snonce
, WPA_NONCE_LEN
) != 0) {
1983 * There is no point in updating the RNonce for every obtained
1984 * TPK M1 frame (e.g., retransmission due to timeout) with the
1985 * same INonce (SNonce in FTIE). However, if the TPK M1 is
1986 * retransmitted with a different INonce, update the RNonce
1987 * since this is for a new TDLS session.
1989 wpa_printf(MSG_DEBUG
,
1990 "TDLS: New TPK M1 INonce - generate new RNonce");
1991 os_memcpy(peer
->inonce
, ftie
->Snonce
, WPA_NONCE_LEN
);
1992 if (os_get_random(peer
->rnonce
, WPA_NONCE_LEN
)) {
1993 wpa_msg(sm
->ctx
->ctx
, MSG_WARNING
,
1994 "TDLS: Failed to get random data for responder nonce");
2000 /* get version info from RSNIE received from Peer */
2001 hdr
= (struct rsn_ie_hdr
*) kde
.rsn_ie
;
2002 rsn_ver
= WPA_GET_LE16(hdr
->version
);
2004 /* use min(peer's version, out version) */
2005 if (rsn_ver
> RSN_VERSION
)
2006 rsn_ver
= RSN_VERSION
;
2008 hdr
= (struct rsn_ie_hdr
*) peer
->rsnie_p
;
2010 hdr
->elem_id
= WLAN_EID_RSN
;
2011 WPA_PUT_LE16(hdr
->version
, rsn_ver
);
2012 pos
= (u8
*) (hdr
+ 1);
2014 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED
);
2015 pos
+= RSN_SELECTOR_LEN
;
2016 /* Include only the selected cipher in pairwise cipher suite */
2017 WPA_PUT_LE16(pos
, 1);
2019 if (cipher
== WPA_CIPHER_CCMP
)
2020 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_CCMP
);
2021 pos
+= RSN_SELECTOR_LEN
;
2023 WPA_PUT_LE16(pos
, 1);
2025 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE
);
2026 pos
+= RSN_SELECTOR_LEN
;
2028 rsn_capab
= WPA_CAPABILITY_PEERKEY_ENABLED
;
2029 rsn_capab
|= RSN_NUM_REPLAY_COUNTERS_16
<< 2;
2030 WPA_PUT_LE16(pos
, rsn_capab
);
2033 hdr
->len
= (pos
- peer
->rsnie_p
) - 2;
2034 peer
->rsnie_p_len
= pos
- peer
->rsnie_p
;
2037 /* temp fix: validation of RSNIE later */
2038 os_memcpy(peer
->rsnie_p
, peer
->rsnie_i
, peer
->rsnie_i_len
);
2039 peer
->rsnie_p_len
= peer
->rsnie_i_len
;
2041 wpa_hexdump(MSG_DEBUG
, "TDLS: RSN IE for TPK handshake",
2042 peer
->rsnie_p
, peer
->rsnie_p_len
);
2044 peer
->lifetime
= lifetime
;
2046 wpa_tdls_generate_tpk(peer
, sm
->own_addr
, sm
->bssid
);
2049 #ifdef CONFIG_TDLS_TESTING
2050 if (tdls_testing
& TDLS_TESTING_CONCURRENT_INIT
)
2052 #endif /* CONFIG_TDLS_TESTING */
2054 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2055 if (wpa_tdls_addset_peer(sm
, peer
, 1) < 0)
2058 #ifdef CONFIG_TDLS_TESTING
2060 #endif /* CONFIG_TDLS_TESTING */
2061 peer
->tpk_in_progress
= 1;
2063 wpa_printf(MSG_DEBUG
, "TDLS: Sending TDLS Setup Response / TPK M2");
2064 if (wpa_tdls_send_tpk_m2(sm
, src_addr
, dtoken
, lnkid
, peer
) < 0) {
2065 wpa_sm_tdls_oper(sm
, TDLS_DISABLE_LINK
, peer
->addr
);
2072 wpa_tdls_send_error(sm
, src_addr
, WLAN_TDLS_SETUP_RESPONSE
, dtoken
, 0,
2075 wpa_tdls_peer_free(sm
, peer
);
2080 static int wpa_tdls_enable_link(struct wpa_sm
*sm
, struct wpa_tdls_peer
*peer
)
2082 peer
->tpk_success
= 1;
2083 peer
->tpk_in_progress
= 0;
2084 eloop_cancel_timeout(wpa_tdls_tpk_timeout
, sm
, peer
);
2085 if (wpa_tdls_get_privacy(sm
)) {
2086 u32 lifetime
= peer
->lifetime
;
2088 * Start the initiator process a bit earlier to avoid race
2089 * condition with the responder sending teardown request.
2091 if (lifetime
> 3 && peer
->initiator
)
2093 eloop_register_timeout(lifetime
, 0, wpa_tdls_tpk_timeout
,
2095 #ifdef CONFIG_TDLS_TESTING
2096 if (tdls_testing
& TDLS_TESTING_NO_TPK_EXPIRATION
) {
2097 wpa_printf(MSG_DEBUG
, "TDLS: Testing - disable TPK "
2099 eloop_cancel_timeout(wpa_tdls_tpk_timeout
, sm
, peer
);
2101 #endif /* CONFIG_TDLS_TESTING */
2104 if (peer
->reconfig_key
&& wpa_tdls_set_key(sm
, peer
) < 0) {
2105 wpa_printf(MSG_INFO
, "TDLS: Could not configure key to the "
2109 peer
->reconfig_key
= 0;
2111 return wpa_sm_tdls_oper(sm
, TDLS_ENABLE_LINK
, peer
->addr
);
2115 static int wpa_tdls_process_tpk_m2(struct wpa_sm
*sm
, const u8
*src_addr
,
2116 const u8
*buf
, size_t len
)
2118 struct wpa_tdls_peer
*peer
;
2119 struct wpa_eapol_ie_parse kde
;
2120 struct wpa_ie_data ie
;
2122 struct wpa_tdls_ftie
*ftie
;
2123 struct wpa_tdls_timeoutie
*timeoutie
;
2124 struct wpa_tdls_lnkid
*lnkid
;
2132 wpa_printf(MSG_DEBUG
, "TDLS: Received TDLS Setup Response / TPK M2 "
2133 "(Peer " MACSTR
")", MAC2STR(src_addr
));
2134 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
2135 if (os_memcmp(peer
->addr
, src_addr
, ETH_ALEN
) == 0)
2139 wpa_printf(MSG_INFO
, "TDLS: No matching peer found for "
2140 "TPK M2: " MACSTR
, MAC2STR(src_addr
));
2143 if (!peer
->initiator
) {
2145 * This may happen if both devices try to initiate TDLS at the
2146 * same time and we accept the TPK M1 from the peer in
2147 * wpa_tdls_process_tpk_m1() and clear our previous state.
2149 wpa_printf(MSG_INFO
, "TDLS: We were not the initiator, so "
2150 "ignore TPK M2 from " MACSTR
, MAC2STR(src_addr
));
2153 wpa_tdls_tpk_retry_timeout_cancel(sm
, peer
, WLAN_TDLS_SETUP_REQUEST
);
2155 if (len
< 3 + 2 + 1) {
2156 wpa_tdls_disable_peer_link(sm
, peer
);
2161 pos
+= 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2162 status
= WPA_GET_LE16(pos
);
2163 pos
+= 2 /* status code */;
2165 if (status
!= WLAN_STATUS_SUCCESS
) {
2166 wpa_printf(MSG_INFO
, "TDLS: Status code in TPK M2: %u",
2168 wpa_tdls_disable_peer_link(sm
, peer
);
2172 status
= WLAN_STATUS_UNSPECIFIED_FAILURE
;
2174 /* TODO: need to verify dialog token matches here or in kernel */
2175 dtoken
= *pos
++; /* dialog token */
2177 wpa_printf(MSG_DEBUG
, "TDLS: Dialog Token in TPK M2 %d", dtoken
);
2179 if (len
< 3 + 2 + 1 + 2) {
2180 wpa_tdls_disable_peer_link(sm
, peer
);
2184 /* capability information */
2185 peer
->capability
= WPA_GET_LE16(pos
);
2188 ielen
= len
- (pos
- buf
); /* start of IE in buf */
2189 if (wpa_supplicant_parse_ies(pos
, ielen
, &kde
) < 0) {
2190 wpa_printf(MSG_INFO
, "TDLS: Failed to parse IEs in TPK M2");
2194 #ifdef CONFIG_TDLS_TESTING
2195 if (tdls_testing
& TDLS_TESTING_DECLINE_RESP
) {
2196 wpa_printf(MSG_DEBUG
, "TDLS: Testing - decline response");
2197 status
= WLAN_STATUS_REQUEST_DECLINED
;
2200 #endif /* CONFIG_TDLS_TESTING */
2202 if (kde
.lnkid
== NULL
|| kde
.lnkid_len
< 3 * ETH_ALEN
) {
2203 wpa_printf(MSG_INFO
, "TDLS: No valid Link Identifier IE in "
2207 wpa_hexdump(MSG_DEBUG
, "TDLS: Link ID Received from TPK M2",
2208 kde
.lnkid
, kde
.lnkid_len
);
2209 lnkid
= (struct wpa_tdls_lnkid
*) kde
.lnkid
;
2211 if (os_memcmp(sm
->bssid
, lnkid
->bssid
, ETH_ALEN
) != 0) {
2212 wpa_printf(MSG_INFO
, "TDLS: TPK M2 from different BSS");
2213 status
= WLAN_STATUS_NOT_IN_SAME_BSS
;
2217 if (copy_supp_rates(&kde
, peer
) < 0)
2220 if (copy_peer_ht_capab(&kde
, peer
) < 0)
2223 if (copy_peer_vht_capab(&kde
, peer
) < 0)
2226 if (copy_peer_ext_capab(&kde
, peer
) < 0)
2229 if (copy_peer_supp_channels(&kde
, peer
) < 0)
2232 if (copy_peer_supp_oper_classes(&kde
, peer
) < 0)
2235 peer
->qos_info
= kde
.qosinfo
;
2237 /* Overwrite with the qos_info obtained in WMM IE */
2238 if (copy_peer_wmm_capab(&kde
, peer
) < 0)
2241 peer
->aid
= kde
.aid
;
2243 if (!wpa_tdls_get_privacy(sm
)) {
2244 peer
->rsnie_p_len
= 0;
2245 peer
->cipher
= WPA_CIPHER_NONE
;
2249 if (kde
.ftie
== NULL
|| kde
.ftie_len
< sizeof(*ftie
) ||
2250 kde
.rsn_ie
== NULL
) {
2251 wpa_printf(MSG_INFO
, "TDLS: No FTIE or RSN IE in TPK M2");
2252 status
= WLAN_STATUS_INVALID_PARAMETERS
;
2255 wpa_hexdump(MSG_DEBUG
, "TDLS: RSN IE Received from TPK M2",
2256 kde
.rsn_ie
, kde
.rsn_ie_len
);
2258 if (kde
.rsn_ie_len
> TDLS_MAX_IE_LEN
) {
2259 wpa_printf(MSG_INFO
,
2260 "TDLS: Too long Responder RSN IE in TPK M2");
2261 status
= WLAN_STATUS_INVALID_RSNIE
;
2266 * FIX: bitwise comparison of RSN IE is not the correct way of
2267 * validation this. It can be different, but certain fields must
2268 * match. Since we list only a single pairwise cipher in TPK M1, the
2269 * memcmp is likely to work in most cases, though.
2271 if (kde
.rsn_ie_len
!= peer
->rsnie_i_len
||
2272 os_memcmp(peer
->rsnie_i
, kde
.rsn_ie
, peer
->rsnie_i_len
) != 0) {
2273 wpa_printf(MSG_INFO
, "TDLS: RSN IE in TPK M2 does "
2274 "not match with RSN IE used in TPK M1");
2275 wpa_hexdump(MSG_DEBUG
, "TDLS: RSN IE Sent in TPK M1",
2276 peer
->rsnie_i
, peer
->rsnie_i_len
);
2277 wpa_hexdump(MSG_DEBUG
, "TDLS: RSN IE Received from TPK M2",
2278 kde
.rsn_ie
, kde
.rsn_ie_len
);
2279 status
= WLAN_STATUS_INVALID_RSNIE
;
2283 if (wpa_parse_wpa_ie_rsn(kde
.rsn_ie
, kde
.rsn_ie_len
, &ie
) < 0) {
2284 wpa_printf(MSG_INFO
, "TDLS: Failed to parse RSN IE in TPK M2");
2285 status
= WLAN_STATUS_INVALID_RSNIE
;
2289 cipher
= ie
.pairwise_cipher
;
2290 if (cipher
== WPA_CIPHER_CCMP
) {
2291 wpa_printf(MSG_DEBUG
, "TDLS: Using CCMP for direct link");
2292 cipher
= WPA_CIPHER_CCMP
;
2294 wpa_printf(MSG_INFO
, "TDLS: No acceptable cipher in TPK M2");
2295 status
= WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID
;
2299 wpa_hexdump(MSG_DEBUG
, "TDLS: FTIE Received from TPK M2",
2300 kde
.ftie
, sizeof(*ftie
));
2301 ftie
= (struct wpa_tdls_ftie
*) kde
.ftie
;
2303 if (!os_memcmp(peer
->inonce
, ftie
->Snonce
, WPA_NONCE_LEN
) == 0) {
2304 wpa_printf(MSG_INFO
, "TDLS: FTIE SNonce in TPK M2 does "
2305 "not match with FTIE SNonce used in TPK M1");
2306 /* Silently discard the frame */
2310 /* Responder Nonce and RSN IE */
2311 os_memcpy(peer
->rnonce
, ftie
->Anonce
, WPA_NONCE_LEN
);
2312 os_memcpy(peer
->rsnie_p
, kde
.rsn_ie
, kde
.rsn_ie_len
);
2313 peer
->rsnie_p_len
= kde
.rsn_ie_len
;
2314 peer
->cipher
= cipher
;
2317 if (kde
.key_lifetime
== NULL
) {
2318 wpa_printf(MSG_INFO
, "TDLS: No Key Lifetime IE in TPK M2");
2319 status
= WLAN_STATUS_UNACCEPTABLE_LIFETIME
;
2322 timeoutie
= (struct wpa_tdls_timeoutie
*) kde
.key_lifetime
;
2323 lifetime
= WPA_GET_LE32(timeoutie
->value
);
2324 wpa_printf(MSG_DEBUG
, "TDLS: TPK lifetime %u seconds in TPK M2",
2326 if (lifetime
!= peer
->lifetime
) {
2327 wpa_printf(MSG_INFO
, "TDLS: Unexpected TPK lifetime %u in "
2328 "TPK M2 (expected %u)", lifetime
, peer
->lifetime
);
2329 status
= WLAN_STATUS_UNACCEPTABLE_LIFETIME
;
2333 wpa_tdls_generate_tpk(peer
, sm
->own_addr
, sm
->bssid
);
2335 /* Process MIC check to see if TPK M2 is right */
2336 if (wpa_supplicant_verify_tdls_mic(2, peer
, (u8
*) lnkid
,
2337 (u8
*) timeoutie
, ftie
) < 0) {
2338 /* Discard the frame */
2339 wpa_tdls_del_key(sm
, peer
);
2340 wpa_tdls_disable_peer_link(sm
, peer
);
2344 if (wpa_tdls_set_key(sm
, peer
) < 0) {
2346 * Some drivers may not be able to config the key prior to full
2347 * STA entry having been configured.
2349 wpa_printf(MSG_DEBUG
, "TDLS: Try to configure TPK again after "
2350 "STA entry is complete");
2351 peer
->reconfig_key
= 1;
2355 peer
->dtoken
= dtoken
;
2357 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2358 if (wpa_tdls_addset_peer(sm
, peer
, 0) < 0)
2361 wpa_printf(MSG_DEBUG
, "TDLS: Sending TDLS Setup Confirm / "
2362 "TPK Handshake Message 3");
2363 if (wpa_tdls_send_tpk_m3(sm
, src_addr
, dtoken
, lnkid
, peer
) < 0)
2366 if (!peer
->tpk_success
) {
2368 * Enable Link only when tpk_success is 0, signifying that this
2369 * processing of TPK M2 frame is not because of a retransmission
2370 * during TDLS setup handshake.
2372 ret
= wpa_tdls_enable_link(sm
, peer
);
2374 wpa_printf(MSG_DEBUG
, "TDLS: Could not enable link");
2375 wpa_tdls_do_teardown(
2377 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED
);
2383 wpa_tdls_send_error(sm
, src_addr
, WLAN_TDLS_SETUP_CONFIRM
, dtoken
, 1,
2385 wpa_tdls_disable_peer_link(sm
, peer
);
2390 static int wpa_tdls_process_tpk_m3(struct wpa_sm
*sm
, const u8
*src_addr
,
2391 const u8
*buf
, size_t len
)
2393 struct wpa_tdls_peer
*peer
;
2394 struct wpa_eapol_ie_parse kde
;
2395 struct wpa_tdls_ftie
*ftie
;
2396 struct wpa_tdls_timeoutie
*timeoutie
;
2397 struct wpa_tdls_lnkid
*lnkid
;
2404 wpa_printf(MSG_DEBUG
, "TDLS: Received TDLS Setup Confirm / TPK M3 "
2405 "(Peer " MACSTR
")", MAC2STR(src_addr
));
2406 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
2407 if (os_memcmp(peer
->addr
, src_addr
, ETH_ALEN
) == 0)
2411 wpa_printf(MSG_INFO
, "TDLS: No matching peer found for "
2412 "TPK M3: " MACSTR
, MAC2STR(src_addr
));
2415 wpa_tdls_tpk_retry_timeout_cancel(sm
, peer
, WLAN_TDLS_SETUP_RESPONSE
);
2420 pos
+= 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2422 status
= WPA_GET_LE16(pos
);
2425 wpa_printf(MSG_INFO
, "TDLS: Status code in TPK M3: %u",
2429 pos
+= 2 /* status code */ + 1 /* dialog token */;
2431 ielen
= len
- (pos
- buf
); /* start of IE in buf */
2434 * Don't reject the message if failing to parse IEs. The IEs we need are
2435 * explicitly checked below. Some APs piggy-back broken IEs to the end
2436 * of a TDLS Confirm packet, which will fail the link if we don't ignore
2439 if (wpa_supplicant_parse_ies((const u8
*) pos
, ielen
, &kde
) < 0) {
2440 wpa_printf(MSG_DEBUG
,
2441 "TDLS: Failed to parse KDEs in TPK M3 - ignore as an interop workaround");
2444 if (kde
.lnkid
== NULL
|| kde
.lnkid_len
< 3 * ETH_ALEN
) {
2445 wpa_printf(MSG_INFO
, "TDLS: No Link Identifier IE in TPK M3");
2448 wpa_hexdump(MSG_DEBUG
, "TDLS: Link ID Received from TPK M3",
2449 (u8
*) kde
.lnkid
, kde
.lnkid_len
);
2450 lnkid
= (struct wpa_tdls_lnkid
*) kde
.lnkid
;
2452 if (os_memcmp(sm
->bssid
, lnkid
->bssid
, ETH_ALEN
) != 0) {
2453 wpa_printf(MSG_INFO
, "TDLS: TPK M3 from diff BSS");
2457 if (!wpa_tdls_get_privacy(sm
))
2460 if (kde
.ftie
== NULL
|| kde
.ftie_len
< sizeof(*ftie
)) {
2461 wpa_printf(MSG_INFO
, "TDLS: No FTIE in TPK M3");
2464 wpa_hexdump(MSG_DEBUG
, "TDLS: FTIE Received from TPK M3",
2465 kde
.ftie
, sizeof(*ftie
));
2466 ftie
= (struct wpa_tdls_ftie
*) kde
.ftie
;
2468 if (kde
.rsn_ie
== NULL
) {
2469 wpa_printf(MSG_INFO
, "TDLS: No RSN IE in TPK M3");
2472 wpa_hexdump(MSG_DEBUG
, "TDLS: RSN IE Received from TPK M3",
2473 kde
.rsn_ie
, kde
.rsn_ie_len
);
2474 if (kde
.rsn_ie_len
!= peer
->rsnie_p_len
||
2475 os_memcmp(kde
.rsn_ie
, peer
->rsnie_p
, peer
->rsnie_p_len
) != 0) {
2476 wpa_printf(MSG_INFO
, "TDLS: RSN IE in TPK M3 does not match "
2477 "with the one sent in TPK M2");
2481 if (!os_memcmp(peer
->rnonce
, ftie
->Anonce
, WPA_NONCE_LEN
) == 0) {
2482 wpa_printf(MSG_INFO
, "TDLS: FTIE ANonce in TPK M3 does "
2483 "not match with FTIE ANonce used in TPK M2");
2487 if (!os_memcmp(peer
->inonce
, ftie
->Snonce
, WPA_NONCE_LEN
) == 0) {
2488 wpa_printf(MSG_INFO
, "TDLS: FTIE SNonce in TPK M3 does not "
2489 "match with FTIE SNonce used in TPK M1");
2493 if (kde
.key_lifetime
== NULL
) {
2494 wpa_printf(MSG_INFO
, "TDLS: No Key Lifetime IE in TPK M3");
2497 timeoutie
= (struct wpa_tdls_timeoutie
*) kde
.key_lifetime
;
2498 wpa_hexdump(MSG_DEBUG
, "TDLS: Timeout IE Received from TPK M3",
2499 (u8
*) timeoutie
, sizeof(*timeoutie
));
2500 lifetime
= WPA_GET_LE32(timeoutie
->value
);
2501 wpa_printf(MSG_DEBUG
, "TDLS: TPK lifetime %u seconds in TPK M3",
2503 if (lifetime
!= peer
->lifetime
) {
2504 wpa_printf(MSG_INFO
, "TDLS: Unexpected TPK lifetime %u in "
2505 "TPK M3 (expected %u)", lifetime
, peer
->lifetime
);
2509 if (wpa_supplicant_verify_tdls_mic(3, peer
, (u8
*) lnkid
,
2510 (u8
*) timeoutie
, ftie
) < 0) {
2511 wpa_tdls_del_key(sm
, peer
);
2515 if (wpa_tdls_set_key(sm
, peer
) < 0) {
2517 * Some drivers may not be able to config the key prior to full
2518 * STA entry having been configured.
2520 wpa_printf(MSG_DEBUG
, "TDLS: Try to configure TPK again after "
2521 "STA entry is complete");
2522 peer
->reconfig_key
= 1;
2526 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2527 if (wpa_tdls_addset_peer(sm
, peer
, 0) < 0)
2530 if (!peer
->tpk_success
) {
2532 * Enable Link only when tpk_success is 0, signifying that this
2533 * processing of TPK M3 frame is not because of a retransmission
2534 * during TDLS setup handshake.
2536 ret
= wpa_tdls_enable_link(sm
, peer
);
2538 wpa_printf(MSG_DEBUG
, "TDLS: Could not enable link");
2544 wpa_tdls_do_teardown(sm
, peer
, WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED
);
2549 static u8
* wpa_add_tdls_timeoutie(u8
*pos
, u8
*ie
, size_t ie_len
, u32 tsecs
)
2551 struct wpa_tdls_timeoutie
*lifetime
= (struct wpa_tdls_timeoutie
*) ie
;
2553 os_memset(lifetime
, 0, ie_len
);
2554 lifetime
->ie_type
= WLAN_EID_TIMEOUT_INTERVAL
;
2555 lifetime
->ie_len
= sizeof(struct wpa_tdls_timeoutie
) - 2;
2556 lifetime
->interval_type
= WLAN_TIMEOUT_KEY_LIFETIME
;
2557 WPA_PUT_LE32(lifetime
->value
, tsecs
);
2558 os_memcpy(pos
, ie
, ie_len
);
2559 return pos
+ ie_len
;
2564 * wpa_tdls_start - Initiate TDLS handshake (send TPK Handshake Message 1)
2565 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2566 * @peer: MAC address of the peer STA
2567 * Returns: 0 on success, or -1 on failure
2569 * Send TPK Handshake Message 1 info to driver to start TDLS
2570 * handshake with the peer.
2572 int wpa_tdls_start(struct wpa_sm
*sm
, const u8
*addr
)
2574 struct wpa_tdls_peer
*peer
;
2575 int tdls_prohibited
= sm
->tdls_prohibited
;
2577 if (sm
->tdls_disabled
|| !sm
->tdls_supported
)
2580 #ifdef CONFIG_TDLS_TESTING
2581 if ((tdls_testing
& TDLS_TESTING_IGNORE_AP_PROHIBIT
) &&
2583 wpa_printf(MSG_DEBUG
, "TDLS: Testing - ignore AP prohibition "
2585 tdls_prohibited
= 0;
2587 #endif /* CONFIG_TDLS_TESTING */
2589 if (tdls_prohibited
) {
2590 wpa_printf(MSG_DEBUG
, "TDLS: TDLS is prohibited in this BSS - "
2591 "reject request to start setup");
2595 peer
= wpa_tdls_add_peer(sm
, addr
, NULL
);
2599 if (peer
->tpk_in_progress
) {
2600 wpa_printf(MSG_DEBUG
, "TDLS: Setup is already in progress with the peer");
2604 peer
->initiator
= 1;
2606 /* add the peer to the driver as a "setup in progress" peer */
2607 if (wpa_sm_tdls_peer_addset(sm
, peer
->addr
, 1, 0, 0, NULL
, 0, NULL
,
2608 NULL
, 0, 0, NULL
, 0, NULL
, 0, NULL
, 0)) {
2609 wpa_tdls_disable_peer_link(sm
, peer
);
2613 peer
->tpk_in_progress
= 1;
2615 if (wpa_tdls_send_tpk_m1(sm
, peer
) < 0) {
2616 wpa_tdls_disable_peer_link(sm
, peer
);
2624 void wpa_tdls_remove(struct wpa_sm
*sm
, const u8
*addr
)
2626 struct wpa_tdls_peer
*peer
;
2628 if (sm
->tdls_disabled
|| !sm
->tdls_supported
)
2631 for (peer
= sm
->tdls
; peer
; peer
= peer
->next
) {
2632 if (os_memcmp(peer
->addr
, addr
, ETH_ALEN
) == 0)
2636 if (peer
== NULL
|| !peer
->tpk_success
)
2639 if (sm
->tdls_external_setup
) {
2641 * Disable previous link to allow renegotiation to be completed
2644 wpa_tdls_do_teardown(sm
, peer
,
2645 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED
);
2651 * wpa_supplicant_rx_tdls - Receive TDLS data frame
2653 * This function is called to receive TDLS (ethertype = 0x890d) data frames.
2655 static void wpa_supplicant_rx_tdls(void *ctx
, const u8
*src_addr
,
2656 const u8
*buf
, size_t len
)
2658 struct wpa_sm
*sm
= ctx
;
2659 struct wpa_tdls_frame
*tf
;
2661 wpa_hexdump(MSG_DEBUG
, "TDLS: Received Data frame encapsulation",
2664 if (sm
->tdls_disabled
|| !sm
->tdls_supported
) {
2665 wpa_printf(MSG_DEBUG
, "TDLS: Discard message - TDLS disabled "
2666 "or unsupported by driver");
2670 if (os_memcmp(src_addr
, sm
->own_addr
, ETH_ALEN
) == 0) {
2671 wpa_printf(MSG_DEBUG
, "TDLS: Discard copy of own message");
2675 if (len
< sizeof(*tf
)) {
2676 wpa_printf(MSG_INFO
, "TDLS: Drop too short frame");
2680 /* Check to make sure its a valid encapsulated TDLS frame */
2681 tf
= (struct wpa_tdls_frame
*) buf
;
2682 if (tf
->payloadtype
!= 2 /* TDLS_RFTYPE */ ||
2683 tf
->category
!= WLAN_ACTION_TDLS
) {
2684 wpa_printf(MSG_INFO
, "TDLS: Invalid frame - payloadtype=%u "
2685 "category=%u action=%u",
2686 tf
->payloadtype
, tf
->category
, tf
->action
);
2690 switch (tf
->action
) {
2691 case WLAN_TDLS_SETUP_REQUEST
:
2692 wpa_tdls_process_tpk_m1(sm
, src_addr
, buf
, len
);
2694 case WLAN_TDLS_SETUP_RESPONSE
:
2695 wpa_tdls_process_tpk_m2(sm
, src_addr
, buf
, len
);
2697 case WLAN_TDLS_SETUP_CONFIRM
:
2698 wpa_tdls_process_tpk_m3(sm
, src_addr
, buf
, len
);
2700 case WLAN_TDLS_TEARDOWN
:
2701 wpa_tdls_recv_teardown(sm
, src_addr
, buf
, len
);
2703 case WLAN_TDLS_DISCOVERY_REQUEST
:
2704 wpa_tdls_process_discovery_request(sm
, src_addr
, buf
, len
);
2707 /* Kernel code will process remaining frames */
2708 wpa_printf(MSG_DEBUG
, "TDLS: Ignore TDLS frame action code %u",
2716 * wpa_tdls_init - Initialize driver interface parameters for TDLS
2717 * @wpa_s: Pointer to wpa_supplicant data
2718 * Returns: 0 on success, -1 on failure
2720 * This function is called to initialize driver interface parameters for TDLS.
2721 * wpa_drv_init() must have been called before this function to initialize the
2724 int wpa_tdls_init(struct wpa_sm
*sm
)
2729 sm
->l2_tdls
= l2_packet_init(sm
->bridge_ifname
? sm
->bridge_ifname
:
2732 ETH_P_80211_ENCAP
, wpa_supplicant_rx_tdls
,
2734 if (sm
->l2_tdls
== NULL
) {
2735 wpa_printf(MSG_ERROR
, "TDLS: Failed to open l2_packet "
2741 * Drivers that support TDLS but don't implement the get_capa callback
2742 * are assumed to perform everything internally
2744 if (wpa_sm_tdls_get_capa(sm
, &sm
->tdls_supported
,
2745 &sm
->tdls_external_setup
,
2746 &sm
->tdls_chan_switch
) < 0) {
2747 sm
->tdls_supported
= 1;
2748 sm
->tdls_external_setup
= 0;
2751 wpa_printf(MSG_DEBUG
, "TDLS: TDLS operation%s supported by "
2752 "driver", sm
->tdls_supported
? "" : " not");
2753 wpa_printf(MSG_DEBUG
, "TDLS: Driver uses %s link setup",
2754 sm
->tdls_external_setup
? "external" : "internal");
2755 wpa_printf(MSG_DEBUG
, "TDLS: Driver %s TDLS channel switching",
2756 sm
->tdls_chan_switch
? "supports" : "does not support");
2762 void wpa_tdls_teardown_peers(struct wpa_sm
*sm
)
2764 struct wpa_tdls_peer
*peer
, *tmp
;
2768 wpa_printf(MSG_DEBUG
, "TDLS: Tear down peers");
2772 wpa_printf(MSG_DEBUG
, "TDLS: Tear down peer " MACSTR
,
2773 MAC2STR(peer
->addr
));
2774 if (sm
->tdls_external_setup
)
2775 wpa_tdls_do_teardown(sm
, peer
,
2776 WLAN_REASON_DEAUTH_LEAVING
);
2778 wpa_sm_tdls_oper(sm
, TDLS_TEARDOWN
, peer
->addr
);
2785 static void wpa_tdls_remove_peers(struct wpa_sm
*sm
)
2787 struct wpa_tdls_peer
*peer
, *tmp
;
2794 res
= wpa_sm_tdls_oper(sm
, TDLS_DISABLE_LINK
, peer
->addr
);
2795 wpa_printf(MSG_DEBUG
, "TDLS: Remove peer " MACSTR
" (res=%d)",
2796 MAC2STR(peer
->addr
), res
);
2797 wpa_tdls_peer_free(sm
, peer
);
2804 * wpa_tdls_deinit - Deinitialize driver interface parameters for TDLS
2806 * This function is called to recover driver interface parameters for TDLS
2807 * and frees resources allocated for it.
2809 void wpa_tdls_deinit(struct wpa_sm
*sm
)
2815 l2_packet_deinit(sm
->l2_tdls
);
2818 wpa_tdls_remove_peers(sm
);
2822 void wpa_tdls_assoc(struct wpa_sm
*sm
)
2824 wpa_printf(MSG_DEBUG
, "TDLS: Remove peers on association");
2825 wpa_tdls_remove_peers(sm
);
2829 void wpa_tdls_disassoc(struct wpa_sm
*sm
)
2831 wpa_printf(MSG_DEBUG
, "TDLS: Remove peers on disassociation");
2832 wpa_tdls_remove_peers(sm
);
2836 static int wpa_tdls_prohibited(const u8
*ies
, size_t len
)
2838 struct wpa_eapol_ie_parse elems
;
2843 if (wpa_supplicant_parse_ies(ies
, len
, &elems
) < 0)
2846 if (elems
.ext_capab
== NULL
|| elems
.ext_capab_len
< 2 + 5)
2849 /* bit 38 - TDLS Prohibited */
2850 return !!(elems
.ext_capab
[2 + 4] & 0x40);
2854 void wpa_tdls_ap_ies(struct wpa_sm
*sm
, const u8
*ies
, size_t len
)
2856 sm
->tdls_prohibited
= wpa_tdls_prohibited(ies
, len
);
2857 wpa_printf(MSG_DEBUG
, "TDLS: TDLS is %s in the target BSS",
2858 sm
->tdls_prohibited
? "prohibited" : "allowed");
2862 void wpa_tdls_assoc_resp_ies(struct wpa_sm
*sm
, const u8
*ies
, size_t len
)
2864 if (!sm
->tdls_prohibited
&& wpa_tdls_prohibited(ies
, len
)) {
2865 wpa_printf(MSG_DEBUG
, "TDLS: TDLS prohibited based on "
2866 "(Re)Association Response IEs");
2867 sm
->tdls_prohibited
= 1;
2872 void wpa_tdls_enable(struct wpa_sm
*sm
, int enabled
)
2874 wpa_printf(MSG_DEBUG
, "TDLS: %s", enabled
? "enabled" : "disabled");
2875 sm
->tdls_disabled
= !enabled
;
2879 int wpa_tdls_is_external_setup(struct wpa_sm
*sm
)
2881 return sm
->tdls_external_setup
;