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 "common/ieee802_11_common.h"
16 #include "crypto/sha256.h"
17 #include "crypto/crypto.h"
18 #include "crypto/aes_wrap.h"
19 #include "rsn_supp/wpa.h"
20 #include "rsn_supp/wpa_ie.h"
21 #include "rsn_supp/wpa_i.h"
22 #include "drivers/driver.h"
23 #include "l2_packet/l2_packet.h"
25 #ifdef CONFIG_TDLS_TESTING
26 #define TDLS_TESTING_LONG_FRAME BIT(0)
27 #define TDLS_TESTING_ALT_RSN_IE BIT(1)
28 #define TDLS_TESTING_DIFF_BSSID BIT(2)
29 #define TDLS_TESTING_SHORT_LIFETIME BIT(3)
30 #define TDLS_TESTING_WRONG_LIFETIME_RESP BIT(4)
31 #define TDLS_TESTING_WRONG_LIFETIME_CONF BIT(5)
32 #define TDLS_TESTING_LONG_LIFETIME BIT(6)
33 #define TDLS_TESTING_CONCURRENT_INIT BIT(7)
34 #define TDLS_TESTING_NO_TPK_EXPIRATION BIT(8)
35 #define TDLS_TESTING_DECLINE_RESP BIT(9)
36 #define TDLS_TESTING_IGNORE_AP_PROHIBIT BIT(10)
37 #define TDLS_TESTING_WRONG_MIC BIT(11)
38 unsigned int tdls_testing = 0;
39 #endif /* CONFIG_TDLS_TESTING */
41 #define TPK_LIFETIME 43200 /* 12 hours */
42 #define TPK_M1_RETRY_COUNT 3
43 #define TPK_M1_TIMEOUT 5000 /* in milliseconds */
44 #define TPK_M2_RETRY_COUNT 10
45 #define TPK_M2_TIMEOUT 500 /* in milliseconds */
47 #define TDLS_MIC_LEN 16
49 #define TDLS_TIMEOUT_LEN 4
51 struct wpa_tdls_ftie {
52 u8 ie_type; /* FTIE */
56 u8 Anonce[WPA_NONCE_LEN]; /* Responder Nonce in TDLS */
57 u8 Snonce[WPA_NONCE_LEN]; /* Initiator Nonce in TDLS */
58 /* followed by optional elements */
61 struct wpa_tdls_timeoutie {
62 u8 ie_type; /* Timeout IE */
65 u8 value[TDLS_TIMEOUT_LEN];
68 struct wpa_tdls_lnkid {
69 u8 ie_type; /* Link Identifier IE */
72 u8 init_sta[ETH_ALEN];
73 u8 resp_sta[ETH_ALEN];
76 /* TDLS frame headers as per IEEE Std 802.11z-2010 */
77 struct wpa_tdls_frame {
78 u8 payloadtype; /* IEEE80211_TDLS_RFTYPE */
79 u8 category; /* Category */
80 u8 action; /* Action (enum tdls_frame_type) */
83 static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs);
84 static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx);
85 static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer);
86 static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
87 struct wpa_tdls_peer *peer);
88 static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
92 #define TDLS_MAX_IE_LEN 80
93 #define IEEE80211_MAX_SUPP_RATES 32
95 struct wpa_tdls_peer {
96 struct wpa_tdls_peer *next;
97 unsigned int reconfig_key:1;
98 int initiator; /* whether this end was initiator for TDLS setup */
99 u8 addr[ETH_ALEN]; /* other end MAC address */
100 u8 inonce[WPA_NONCE_LEN]; /* Initiator Nonce */
101 u8 rnonce[WPA_NONCE_LEN]; /* Responder Nonce */
102 u8 rsnie_i[TDLS_MAX_IE_LEN]; /* Initiator RSN IE */
104 u8 rsnie_p[TDLS_MAX_IE_LEN]; /* Peer RSN IE */
107 int cipher; /* Selected cipher (WPA_CIPHER_*) */
111 u8 kck[16]; /* TPK-KCK */
112 u8 tk[16]; /* TPK-TK; assuming only CCMP will be used */
120 int count; /* Retry Count */
121 int timer; /* Timeout in milliseconds */
122 u8 action_code; /* TDLS frame type */
126 int buf_len; /* length of TPK message for retransmission */
127 u8 *buf; /* buffer for TPK message */
132 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
133 size_t supp_rates_len;
135 struct ieee80211_ht_capabilities *ht_capabilities;
136 struct ieee80211_vht_capabilities *vht_capabilities;
143 size_t ext_capab_len;
146 size_t supp_channels_len;
148 u8 *supp_oper_classes;
149 size_t supp_oper_classes_len;
153 /* channel switch currently enabled */
154 int chan_switch_enabled;
158 static int wpa_tdls_get_privacy(struct wpa_sm *sm)
161 * Get info needed from supplicant to check if the current BSS supports
162 * security. Other than OPEN mode, rest are considered secured
163 * WEP/WPA/WPA2 hence TDLS frames are processed for TPK handshake.
165 return sm->pairwise_cipher != WPA_CIPHER_NONE;
169 static u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
171 os_memcpy(pos, ie, ie_len);
176 static int wpa_tdls_del_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
178 if (wpa_sm_set_key(sm, WPA_ALG_NONE, peer->addr,
179 0, 0, NULL, 0, NULL, 0) < 0) {
180 wpa_printf(MSG_WARNING, "TDLS: Failed to delete TPK-TK from "
189 static int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
195 os_memset(rsc, 0, 6);
197 switch (peer->cipher) {
198 case WPA_CIPHER_CCMP:
202 case WPA_CIPHER_NONE:
203 wpa_printf(MSG_DEBUG, "TDLS: Pairwise Cipher Suite: "
204 "NONE - do not use pairwise keys");
207 wpa_printf(MSG_WARNING, "TDLS: Unsupported pairwise cipher %d",
208 sm->pairwise_cipher);
212 if (wpa_sm_set_key(sm, alg, peer->addr, -1, 1,
213 rsc, sizeof(rsc), peer->tpk.tk, key_len) < 0) {
214 wpa_printf(MSG_WARNING, "TDLS: Failed to set TPK to the "
222 static int wpa_tdls_send_tpk_msg(struct wpa_sm *sm, const u8 *dst,
223 u8 action_code, u8 dialog_token,
224 u16 status_code, u32 peer_capab,
225 int initiator, const u8 *buf, size_t len)
227 return wpa_sm_send_tdls_mgmt(sm, dst, action_code, dialog_token,
228 status_code, peer_capab, initiator, buf,
233 static int wpa_tdls_tpk_send(struct wpa_sm *sm, const u8 *dest, u8 action_code,
234 u8 dialog_token, u16 status_code, u32 peer_capab,
235 int initiator, const u8 *msg, size_t msg_len)
237 struct wpa_tdls_peer *peer;
239 wpa_printf(MSG_DEBUG, "TDLS: TPK send dest=" MACSTR " action_code=%u "
240 "dialog_token=%u status_code=%u peer_capab=%u initiator=%d "
242 MAC2STR(dest), action_code, dialog_token, status_code,
243 peer_capab, initiator, (unsigned int) msg_len);
245 if (wpa_tdls_send_tpk_msg(sm, dest, action_code, dialog_token,
246 status_code, peer_capab, initiator, msg,
248 wpa_printf(MSG_INFO, "TDLS: Failed to send message "
249 "(action_code=%u)", action_code);
253 if (action_code == WLAN_TDLS_SETUP_CONFIRM ||
254 action_code == WLAN_TDLS_TEARDOWN ||
255 action_code == WLAN_TDLS_DISCOVERY_REQUEST ||
256 action_code == WLAN_TDLS_DISCOVERY_RESPONSE)
257 return 0; /* No retries */
259 for (peer = sm->tdls; peer; peer = peer->next) {
260 if (os_memcmp(peer->addr, dest, ETH_ALEN) == 0)
265 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
266 "retry " MACSTR, MAC2STR(dest));
270 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
272 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
273 peer->sm_tmr.count = TPK_M2_RETRY_COUNT;
274 peer->sm_tmr.timer = TPK_M2_TIMEOUT;
276 peer->sm_tmr.count = TPK_M1_RETRY_COUNT;
277 peer->sm_tmr.timer = TPK_M1_TIMEOUT;
280 /* Copy message to resend on timeout */
281 os_memcpy(peer->sm_tmr.dest, dest, ETH_ALEN);
282 peer->sm_tmr.action_code = action_code;
283 peer->sm_tmr.dialog_token = dialog_token;
284 peer->sm_tmr.status_code = status_code;
285 peer->sm_tmr.peer_capab = peer_capab;
286 peer->sm_tmr.buf_len = msg_len;
287 os_free(peer->sm_tmr.buf);
288 peer->sm_tmr.buf = os_malloc(msg_len);
289 if (peer->sm_tmr.buf == NULL)
291 os_memcpy(peer->sm_tmr.buf, msg, msg_len);
293 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout registered "
294 "(action_code=%u)", action_code);
295 eloop_register_timeout(peer->sm_tmr.timer / 1000,
296 (peer->sm_tmr.timer % 1000) * 1000,
297 wpa_tdls_tpk_retry_timeout, sm, peer);
302 static int wpa_tdls_do_teardown(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
307 ret = wpa_tdls_send_teardown(sm, peer->addr, reason_code);
308 /* disable the link after teardown was sent */
309 wpa_tdls_disable_peer_link(sm, peer);
315 static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx)
318 struct wpa_sm *sm = eloop_ctx;
319 struct wpa_tdls_peer *peer = timeout_ctx;
321 if (peer->sm_tmr.count) {
322 peer->sm_tmr.count--;
324 wpa_printf(MSG_INFO, "TDLS: Retrying sending of message "
326 peer->sm_tmr.action_code);
328 if (peer->sm_tmr.buf == NULL) {
329 wpa_printf(MSG_INFO, "TDLS: No retry buffer available "
330 "for action_code=%u",
331 peer->sm_tmr.action_code);
332 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm,
337 /* resend TPK Handshake Message to Peer */
338 if (wpa_tdls_send_tpk_msg(sm, peer->sm_tmr.dest,
339 peer->sm_tmr.action_code,
340 peer->sm_tmr.dialog_token,
341 peer->sm_tmr.status_code,
342 peer->sm_tmr.peer_capab,
345 peer->sm_tmr.buf_len)) {
346 wpa_printf(MSG_INFO, "TDLS: Failed to retry "
350 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
351 eloop_register_timeout(peer->sm_tmr.timer / 1000,
352 (peer->sm_tmr.timer % 1000) * 1000,
353 wpa_tdls_tpk_retry_timeout, sm, peer);
355 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
357 wpa_printf(MSG_DEBUG, "TDLS: Sending Teardown Request");
358 wpa_tdls_do_teardown(sm, peer,
359 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
364 static void wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm *sm,
365 struct wpa_tdls_peer *peer,
368 if (action_code == peer->sm_tmr.action_code) {
369 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout cancelled for "
370 "action_code=%u", action_code);
372 /* Cancel Timeout registered */
373 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
375 /* free all resources meant for retry */
376 os_free(peer->sm_tmr.buf);
377 peer->sm_tmr.buf = NULL;
379 peer->sm_tmr.count = 0;
380 peer->sm_tmr.timer = 0;
381 peer->sm_tmr.buf_len = 0;
382 peer->sm_tmr.action_code = 0xff;
384 wpa_printf(MSG_INFO, "TDLS: Error in cancelling retry timeout "
385 "(Unknown action_code=%u)", action_code);
390 static void wpa_tdls_generate_tpk(struct wpa_tdls_peer *peer,
391 const u8 *own_addr, const u8 *bssid)
393 u8 key_input[SHA256_MAC_LEN];
396 u8 data[3 * ETH_ALEN];
398 /* IEEE Std 802.11z-2010 8.5.9.1:
399 * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
401 len[0] = WPA_NONCE_LEN;
402 len[1] = WPA_NONCE_LEN;
403 if (os_memcmp(peer->inonce, peer->rnonce, WPA_NONCE_LEN) < 0) {
404 nonce[0] = peer->inonce;
405 nonce[1] = peer->rnonce;
407 nonce[0] = peer->rnonce;
408 nonce[1] = peer->inonce;
410 wpa_hexdump(MSG_DEBUG, "TDLS: min(Nonce)", nonce[0], WPA_NONCE_LEN);
411 wpa_hexdump(MSG_DEBUG, "TDLS: max(Nonce)", nonce[1], WPA_NONCE_LEN);
412 sha256_vector(2, nonce, len, key_input);
413 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-Key-Input",
414 key_input, SHA256_MAC_LEN);
417 * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
418 * min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
419 * TODO: is N_KEY really included in KDF Context and if so, in which
420 * presentation format (little endian 16-bit?) is it used? It gets
421 * added by the KDF anyway..
424 if (os_memcmp(own_addr, peer->addr, ETH_ALEN) < 0) {
425 os_memcpy(data, own_addr, ETH_ALEN);
426 os_memcpy(data + ETH_ALEN, peer->addr, ETH_ALEN);
428 os_memcpy(data, peer->addr, ETH_ALEN);
429 os_memcpy(data + ETH_ALEN, own_addr, ETH_ALEN);
431 os_memcpy(data + 2 * ETH_ALEN, bssid, ETH_ALEN);
432 wpa_hexdump(MSG_DEBUG, "TDLS: KDF Context", data, sizeof(data));
434 sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data),
435 (u8 *) &peer->tpk, sizeof(peer->tpk));
436 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-KCK",
437 peer->tpk.kck, sizeof(peer->tpk.kck));
438 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-TK",
439 peer->tpk.tk, sizeof(peer->tpk.tk));
445 * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
447 * @lnkid: Pointer to the beginning of Link Identifier IE
448 * @rsnie: Pointer to the beginning of RSN IE used for handshake
449 * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
450 * @ftie: Pointer to the beginning of FT IE
451 * @mic: Pointer for writing MIC
453 * Calculate MIC for TDLS frame.
455 static int wpa_tdls_ftie_mic(const u8 *kck, u8 trans_seq, const u8 *lnkid,
456 const u8 *rsnie, const u8 *timeoutie,
457 const u8 *ftie, u8 *mic)
460 struct wpa_tdls_ftie *_ftie;
461 const struct wpa_tdls_lnkid *_lnkid;
463 int len = 2 * ETH_ALEN + 1 + 2 + lnkid[1] + 2 + rsnie[1] +
464 2 + timeoutie[1] + 2 + ftie[1];
465 buf = os_zalloc(len);
467 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
472 _lnkid = (const struct wpa_tdls_lnkid *) lnkid;
473 /* 1) TDLS initiator STA MAC address */
474 os_memcpy(pos, _lnkid->init_sta, ETH_ALEN);
476 /* 2) TDLS responder STA MAC address */
477 os_memcpy(pos, _lnkid->resp_sta, ETH_ALEN);
479 /* 3) Transaction Sequence number */
481 /* 4) Link Identifier IE */
482 os_memcpy(pos, lnkid, 2 + lnkid[1]);
485 os_memcpy(pos, rsnie, 2 + rsnie[1]);
487 /* 6) Timeout Interval IE */
488 os_memcpy(pos, timeoutie, 2 + timeoutie[1]);
489 pos += 2 + timeoutie[1];
490 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
491 os_memcpy(pos, ftie, 2 + ftie[1]);
492 _ftie = (struct wpa_tdls_ftie *) pos;
493 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
496 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
497 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
498 ret = omac1_aes_128(kck, buf, pos - buf, mic);
500 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
506 * wpa_tdls_key_mic_teardown - Calculate TDLS FTIE MIC for Teardown frame
508 * @trans_seq: Transaction Sequence Number (4 - Teardown)
509 * @rcode: Reason code for Teardown
510 * @dtoken: Dialog Token used for that particular link
511 * @lnkid: Pointer to the beginning of Link Identifier IE
512 * @ftie: Pointer to the beginning of FT IE
513 * @mic: Pointer for writing MIC
515 * Calculate MIC for TDLS frame.
517 static int wpa_tdls_key_mic_teardown(const u8 *kck, u8 trans_seq, u16 rcode,
518 u8 dtoken, const u8 *lnkid,
519 const u8 *ftie, u8 *mic)
522 struct wpa_tdls_ftie *_ftie;
529 len = 2 + lnkid[1] + sizeof(rcode) + sizeof(dtoken) +
530 sizeof(trans_seq) + 2 + ftie[1];
532 buf = os_zalloc(len);
534 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
539 /* 1) Link Identifier IE */
540 os_memcpy(pos, lnkid, 2 + lnkid[1]);
543 WPA_PUT_LE16(pos, rcode);
544 pos += sizeof(rcode);
545 /* 3) Dialog token */
547 /* 4) Transaction Sequence number */
549 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
550 os_memcpy(pos, ftie, 2 + ftie[1]);
551 _ftie = (struct wpa_tdls_ftie *) pos;
552 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
555 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
556 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
557 ret = omac1_aes_128(kck, buf, pos - buf, mic);
559 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
564 static int wpa_supplicant_verify_tdls_mic(u8 trans_seq,
565 struct wpa_tdls_peer *peer,
566 const u8 *lnkid, const u8 *timeoutie,
567 const struct wpa_tdls_ftie *ftie)
572 wpa_tdls_ftie_mic(peer->tpk.kck, trans_seq, lnkid,
573 peer->rsnie_p, timeoutie, (u8 *) ftie,
575 if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
576 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in FTIE - "
578 wpa_hexdump(MSG_DEBUG, "TDLS: Received MIC",
580 wpa_hexdump(MSG_DEBUG, "TDLS: Calculated MIC",
585 wpa_printf(MSG_WARNING, "TDLS: Could not verify TDLS MIC, "
586 "TPK not set - dropping packet");
593 static int wpa_supplicant_verify_tdls_mic_teardown(
594 u8 trans_seq, u16 rcode, u8 dtoken, struct wpa_tdls_peer *peer,
595 const u8 *lnkid, const struct wpa_tdls_ftie *ftie)
600 wpa_tdls_key_mic_teardown(peer->tpk.kck, trans_seq, rcode,
601 dtoken, lnkid, (u8 *) ftie, mic);
602 if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
603 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in Teardown - "
608 wpa_printf(MSG_INFO, "TDLS: Could not verify TDLS Teardown "
609 "MIC, TPK not set - dropping packet");
616 static void wpa_tdls_tpk_timeout(void *eloop_ctx, void *timeout_ctx)
618 struct wpa_sm *sm = eloop_ctx;
619 struct wpa_tdls_peer *peer = timeout_ctx;
622 * On TPK lifetime expiration, we have an option of either tearing down
623 * the direct link or trying to re-initiate it. The selection of what
624 * to do is not strictly speaking controlled by our role in the expired
625 * link, but for now, use that to select whether to renew or tear down
629 if (peer->initiator) {
632 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
633 " - try to renew", MAC2STR(peer->addr));
634 /* cache the peer address before do_teardown */
635 os_memcpy(addr, peer->addr, ETH_ALEN);
636 wpa_tdls_do_teardown(sm, peer,
637 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
638 wpa_tdls_start(sm, addr);
640 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
641 " - tear down", MAC2STR(peer->addr));
642 wpa_tdls_do_teardown(sm, peer,
643 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
648 static void wpa_tdls_peer_remove_from_list(struct wpa_sm *sm,
649 struct wpa_tdls_peer *peer)
651 struct wpa_tdls_peer *cur, *prev;
655 while (cur && cur != peer) {
661 wpa_printf(MSG_ERROR, "TDLS: Could not find peer " MACSTR
662 " to remove it from the list",
663 MAC2STR(peer->addr));
668 prev->next = peer->next;
670 sm->tdls = peer->next;
674 static void wpa_tdls_peer_clear(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
676 wpa_printf(MSG_DEBUG, "TDLS: Clear state for peer " MACSTR,
677 MAC2STR(peer->addr));
678 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
679 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
680 peer->reconfig_key = 0;
682 peer->tpk_in_progress = 0;
683 os_free(peer->sm_tmr.buf);
684 peer->sm_tmr.buf = NULL;
685 os_free(peer->ht_capabilities);
686 peer->ht_capabilities = NULL;
687 os_free(peer->vht_capabilities);
688 peer->vht_capabilities = NULL;
689 os_free(peer->ext_capab);
690 peer->ext_capab = NULL;
691 os_free(peer->supp_channels);
692 peer->supp_channels = NULL;
693 os_free(peer->supp_oper_classes);
694 peer->supp_oper_classes = NULL;
695 peer->rsnie_i_len = peer->rsnie_p_len = 0;
698 peer->wmm_capable = 0;
699 peer->tpk_set = peer->tpk_success = 0;
700 peer->chan_switch_enabled = 0;
701 os_memset(&peer->tpk, 0, sizeof(peer->tpk));
702 os_memset(peer->inonce, 0, WPA_NONCE_LEN);
703 os_memset(peer->rnonce, 0, WPA_NONCE_LEN);
707 static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
709 wpa_tdls_peer_clear(sm, peer);
710 wpa_tdls_peer_remove_from_list(sm, peer);
715 static void wpa_tdls_linkid(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
716 struct wpa_tdls_lnkid *lnkid)
718 lnkid->ie_type = WLAN_EID_LINK_ID;
719 lnkid->ie_len = 3 * ETH_ALEN;
720 os_memcpy(lnkid->bssid, sm->bssid, ETH_ALEN);
721 if (peer->initiator) {
722 os_memcpy(lnkid->init_sta, sm->own_addr, ETH_ALEN);
723 os_memcpy(lnkid->resp_sta, peer->addr, ETH_ALEN);
725 os_memcpy(lnkid->init_sta, peer->addr, ETH_ALEN);
726 os_memcpy(lnkid->resp_sta, sm->own_addr, ETH_ALEN);
731 static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
734 struct wpa_tdls_peer *peer;
735 struct wpa_tdls_ftie *ftie;
736 struct wpa_tdls_lnkid lnkid;
741 if (sm->tdls_disabled || !sm->tdls_supported)
744 /* Find the node and free from the list */
745 for (peer = sm->tdls; peer; peer = peer->next) {
746 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
751 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
752 "Teardown " MACSTR, MAC2STR(addr));
756 /* Cancel active channel switch before teardown */
757 if (peer->chan_switch_enabled) {
758 wpa_printf(MSG_DEBUG, "TDLS: First returning link with " MACSTR
759 " to base channel", MAC2STR(addr));
760 wpa_sm_tdls_disable_channel_switch(sm, peer->addr);
763 dialog_token = peer->dtoken;
765 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown for " MACSTR,
769 if (wpa_tdls_get_privacy(sm) && peer->tpk_set && peer->tpk_success) {
770 /* To add FTIE for Teardown request and compute MIC */
771 ielen += sizeof(*ftie);
772 #ifdef CONFIG_TDLS_TESTING
773 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
775 #endif /* CONFIG_TDLS_TESTING */
778 rbuf = os_zalloc(ielen + 1);
783 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
786 ftie = (struct wpa_tdls_ftie *) pos;
787 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
788 /* Using the recent nonce which should be for CONFIRM frame */
789 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
790 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
791 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
792 pos = (u8 *) (ftie + 1);
793 #ifdef CONFIG_TDLS_TESTING
794 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
795 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
798 *pos++ = 255; /* FTIE subelem */
799 *pos++ = 168; /* FTIE subelem length */
802 #endif /* CONFIG_TDLS_TESTING */
803 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TDLS Teardown handshake",
804 (u8 *) ftie, pos - (u8 *) ftie);
806 /* compute MIC before sending */
807 wpa_tdls_linkid(sm, peer, &lnkid);
808 wpa_tdls_key_mic_teardown(peer->tpk.kck, 4, reason_code,
809 dialog_token, (u8 *) &lnkid, (u8 *) ftie,
813 /* TODO: register for a Timeout handler, if Teardown is not received at
814 * the other end, then try again another time */
816 /* request driver to send Teardown using this FTIE */
817 wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_TEARDOWN, 0,
818 reason_code, 0, peer->initiator, rbuf, pos - rbuf);
825 int wpa_tdls_teardown_link(struct wpa_sm *sm, const u8 *addr, u16 reason_code)
827 struct wpa_tdls_peer *peer;
829 if (sm->tdls_disabled || !sm->tdls_supported)
832 for (peer = sm->tdls; peer; peer = peer->next) {
833 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
838 wpa_printf(MSG_DEBUG, "TDLS: Could not find peer " MACSTR
839 " for link Teardown", MAC2STR(addr));
843 if (!peer->tpk_success) {
844 wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
845 " not connected - cannot Teardown link", MAC2STR(addr));
849 return wpa_tdls_do_teardown(sm, peer, reason_code);
853 static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
854 struct wpa_tdls_peer *peer)
856 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
857 wpa_tdls_peer_free(sm, peer);
861 void wpa_tdls_disable_unreachable_link(struct wpa_sm *sm, const u8 *addr)
863 struct wpa_tdls_peer *peer;
865 for (peer = sm->tdls; peer; peer = peer->next) {
866 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
870 if (!peer || !peer->tpk_success) {
871 wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
872 " not connected - cannot teardown unreachable link",
877 if (wpa_tdls_is_external_setup(sm)) {
879 * Get us on the base channel, disable the link, send a
880 * teardown packet through the AP, and then reset link data.
882 if (peer->chan_switch_enabled)
883 wpa_sm_tdls_disable_channel_switch(sm, peer->addr);
884 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, addr);
885 wpa_tdls_send_teardown(sm, addr,
886 WLAN_REASON_TDLS_TEARDOWN_UNREACHABLE);
887 wpa_tdls_peer_free(sm, peer);
889 wpa_tdls_disable_peer_link(sm, peer);
894 const char * wpa_tdls_get_link_status(struct wpa_sm *sm, const u8 *addr)
896 struct wpa_tdls_peer *peer;
898 if (sm->tdls_disabled || !sm->tdls_supported)
901 for (peer = sm->tdls; peer; peer = peer->next) {
902 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
907 return "peer does not exist";
909 if (!peer->tpk_success)
910 return "peer not connected";
916 static int wpa_tdls_recv_teardown(struct wpa_sm *sm, const u8 *src_addr,
917 const u8 *buf, size_t len)
919 struct wpa_tdls_peer *peer = NULL;
920 struct wpa_tdls_ftie *ftie;
921 struct wpa_tdls_lnkid *lnkid;
922 struct wpa_eapol_ie_parse kde;
927 /* Find the node and free from the list */
928 for (peer = sm->tdls; peer; peer = peer->next) {
929 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
934 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
935 "Teardown " MACSTR, MAC2STR(src_addr));
940 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
942 reason_code = WPA_GET_LE16(pos);
945 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown Request from " MACSTR
946 " (reason code %u)", MAC2STR(src_addr), reason_code);
948 ielen = len - (pos - buf); /* start of IE in buf */
951 * Don't reject the message if failing to parse IEs. The IEs we need are
952 * explicitly checked below. Some APs may add arbitrary padding to the
953 * end of short TDLS frames and that would look like invalid IEs.
955 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0)
956 wpa_printf(MSG_DEBUG,
957 "TDLS: Failed to parse IEs in Teardown - ignore as an interop workaround");
959 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
960 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TDLS "
964 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
966 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
969 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
970 wpa_printf(MSG_INFO, "TDLS: No FTIE in TDLS Teardown");
974 ftie = (struct wpa_tdls_ftie *) kde.ftie;
976 /* Process MIC check to see if TDLS Teardown is right */
977 if (wpa_supplicant_verify_tdls_mic_teardown(4, reason_code,
979 (u8 *) lnkid, ftie) < 0) {
980 wpa_printf(MSG_DEBUG, "TDLS: MIC failure for TDLS "
981 "Teardown Request from " MACSTR, MAC2STR(src_addr));
987 * Request the driver to disable the direct link and clear associated
990 wpa_tdls_disable_peer_link(sm, peer);
996 * wpa_tdls_send_error - To send suitable TDLS status response with
997 * appropriate status code mentioning reason for error/failure.
998 * @dst - MAC addr of Peer station
999 * @tdls_action - TDLS frame type for which error code is sent
1000 * @initiator - was this end the initiator of the connection
1001 * @status - status code mentioning reason
1004 static int wpa_tdls_send_error(struct wpa_sm *sm, const u8 *dst,
1005 u8 tdls_action, u8 dialog_token, int initiator,
1008 wpa_printf(MSG_DEBUG, "TDLS: Sending error to " MACSTR
1009 " (action=%u status=%u)",
1010 MAC2STR(dst), tdls_action, status);
1011 return wpa_tdls_tpk_send(sm, dst, tdls_action, dialog_token, status,
1012 0, initiator, NULL, 0);
1016 static struct wpa_tdls_peer *
1017 wpa_tdls_add_peer(struct wpa_sm *sm, const u8 *addr, int *existing)
1019 struct wpa_tdls_peer *peer;
1023 for (peer = sm->tdls; peer; peer = peer->next) {
1024 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0) {
1027 return peer; /* re-use existing entry */
1031 wpa_printf(MSG_INFO, "TDLS: Creating peer entry for " MACSTR,
1034 peer = os_zalloc(sizeof(*peer));
1038 os_memcpy(peer->addr, addr, ETH_ALEN);
1039 peer->next = sm->tdls;
1046 static int wpa_tdls_send_tpk_m1(struct wpa_sm *sm,
1047 struct wpa_tdls_peer *peer)
1050 struct wpa_tdls_timeoutie timeoutie;
1052 struct wpa_tdls_ftie *ftie;
1053 u8 *rbuf, *pos, *count_pos;
1055 struct rsn_ie_hdr *hdr;
1058 if (!wpa_tdls_get_privacy(sm)) {
1059 wpa_printf(MSG_DEBUG, "TDLS: No security used on the link");
1060 peer->rsnie_i_len = 0;
1065 * TPK Handshake Message 1:
1066 * FTIE: ANonce=0, SNonce=initiator nonce MIC=0, DataKDs=(RSNIE_I,
1067 * Timeout Interval IE))
1070 /* Filling RSN IE */
1071 hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
1072 hdr->elem_id = WLAN_EID_RSN;
1073 WPA_PUT_LE16(hdr->version, RSN_VERSION);
1075 pos = (u8 *) (hdr + 1);
1076 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1077 pos += RSN_SELECTOR_LEN;
1084 * AES-CCMP is the default Encryption preferred for TDLS, so
1085 * RSN IE is filled only with CCMP CIPHER
1086 * Note: TKIP is not used to encrypt TDLS link.
1088 * Regardless of the cipher used on the AP connection, select CCMP
1091 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1092 pos += RSN_SELECTOR_LEN;
1095 WPA_PUT_LE16(count_pos, count);
1097 WPA_PUT_LE16(pos, 1);
1099 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1100 pos += RSN_SELECTOR_LEN;
1102 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1103 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1104 #ifdef CONFIG_TDLS_TESTING
1105 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
1106 wpa_printf(MSG_DEBUG, "TDLS: Use alternative RSN IE for "
1108 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1110 #endif /* CONFIG_TDLS_TESTING */
1111 WPA_PUT_LE16(pos, rsn_capab);
1113 #ifdef CONFIG_TDLS_TESTING
1114 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
1115 /* Number of PMKIDs */
1119 #endif /* CONFIG_TDLS_TESTING */
1121 hdr->len = (pos - peer->rsnie_i) - 2;
1122 peer->rsnie_i_len = pos - peer->rsnie_i;
1123 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
1124 peer->rsnie_i, peer->rsnie_i_len);
1128 if (wpa_tdls_get_privacy(sm))
1129 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1130 sizeof(struct wpa_tdls_timeoutie);
1131 #ifdef CONFIG_TDLS_TESTING
1132 if (wpa_tdls_get_privacy(sm) &&
1133 (tdls_testing & TDLS_TESTING_LONG_FRAME))
1135 if (tdls_testing & TDLS_TESTING_DIFF_BSSID)
1136 buf_len += sizeof(struct wpa_tdls_lnkid);
1137 #endif /* CONFIG_TDLS_TESTING */
1138 rbuf = os_zalloc(buf_len + 1);
1140 wpa_tdls_peer_free(sm, peer);
1145 if (!wpa_tdls_get_privacy(sm))
1148 /* Initiator RSN IE */
1149 pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
1151 ftie = (struct wpa_tdls_ftie *) pos;
1152 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1153 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1155 if (os_get_random(peer->inonce, WPA_NONCE_LEN)) {
1156 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1157 "TDLS: Failed to get random data for initiator Nonce");
1159 wpa_tdls_peer_free(sm, peer);
1162 wpa_hexdump(MSG_DEBUG, "TDLS: Initiator Nonce for TPK handshake",
1163 peer->inonce, WPA_NONCE_LEN);
1164 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1166 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK Handshake M1",
1167 (u8 *) ftie, sizeof(struct wpa_tdls_ftie));
1169 pos = (u8 *) (ftie + 1);
1171 #ifdef CONFIG_TDLS_TESTING
1172 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1173 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1175 ftie->ie_len += 170;
1176 *pos++ = 255; /* FTIE subelem */
1177 *pos++ = 168; /* FTIE subelem length */
1180 #endif /* CONFIG_TDLS_TESTING */
1183 peer->lifetime = TPK_LIFETIME;
1184 #ifdef CONFIG_TDLS_TESTING
1185 if (tdls_testing & TDLS_TESTING_SHORT_LIFETIME) {
1186 wpa_printf(MSG_DEBUG, "TDLS: Testing - use short TPK "
1188 peer->lifetime = 301;
1190 if (tdls_testing & TDLS_TESTING_LONG_LIFETIME) {
1191 wpa_printf(MSG_DEBUG, "TDLS: Testing - use long TPK "
1193 peer->lifetime = 0xffffffff;
1195 #endif /* CONFIG_TDLS_TESTING */
1196 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1197 sizeof(timeoutie), peer->lifetime);
1198 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
1202 #ifdef CONFIG_TDLS_TESTING
1203 if (tdls_testing & TDLS_TESTING_DIFF_BSSID) {
1204 wpa_printf(MSG_DEBUG, "TDLS: Testing - use incorrect BSSID in "
1206 struct wpa_tdls_lnkid *l = (struct wpa_tdls_lnkid *) pos;
1207 wpa_tdls_linkid(sm, peer, l);
1208 l->bssid[5] ^= 0x01;
1211 #endif /* CONFIG_TDLS_TESTING */
1213 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Request / TPK "
1214 "Handshake Message 1 (peer " MACSTR ")",
1215 MAC2STR(peer->addr));
1217 status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_SETUP_REQUEST,
1218 1, 0, 0, peer->initiator, rbuf, pos - rbuf);
1225 static int wpa_tdls_send_tpk_m2(struct wpa_sm *sm,
1226 const unsigned char *src_addr, u8 dtoken,
1227 struct wpa_tdls_lnkid *lnkid,
1228 const struct wpa_tdls_peer *peer)
1233 struct wpa_tdls_timeoutie timeoutie;
1234 struct wpa_tdls_ftie *ftie;
1238 if (wpa_tdls_get_privacy(sm)) {
1239 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1241 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1242 sizeof(struct wpa_tdls_timeoutie);
1243 #ifdef CONFIG_TDLS_TESTING
1244 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1246 #endif /* CONFIG_TDLS_TESTING */
1249 rbuf = os_zalloc(buf_len + 1);
1254 if (!wpa_tdls_get_privacy(sm))
1258 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1260 ftie = (struct wpa_tdls_ftie *) pos;
1261 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1262 /* TODO: ftie->mic_control to set 2-RESPONSE */
1263 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1264 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1265 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1266 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK M2",
1267 (u8 *) ftie, sizeof(*ftie));
1269 pos = (u8 *) (ftie + 1);
1271 #ifdef CONFIG_TDLS_TESTING
1272 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1273 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1275 ftie->ie_len += 170;
1276 *pos++ = 255; /* FTIE subelem */
1277 *pos++ = 168; /* FTIE subelem length */
1280 #endif /* CONFIG_TDLS_TESTING */
1283 lifetime = peer->lifetime;
1284 #ifdef CONFIG_TDLS_TESTING
1285 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_RESP) {
1286 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1287 "lifetime in response");
1290 #endif /* CONFIG_TDLS_TESTING */
1291 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1292 sizeof(timeoutie), lifetime);
1293 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds from initiator",
1296 /* compute MIC before sending */
1297 wpa_tdls_ftie_mic(peer->tpk.kck, 2, (u8 *) lnkid, peer->rsnie_p,
1298 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
1299 #ifdef CONFIG_TDLS_TESTING
1300 if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
1301 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
1302 ftie->mic[0] ^= 0x01;
1304 #endif /* CONFIG_TDLS_TESTING */
1307 status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE,
1308 dtoken, 0, 0, peer->initiator, rbuf,
1316 static int wpa_tdls_send_tpk_m3(struct wpa_sm *sm,
1317 const unsigned char *src_addr, u8 dtoken,
1318 struct wpa_tdls_lnkid *lnkid,
1319 const struct wpa_tdls_peer *peer)
1323 struct wpa_tdls_ftie *ftie;
1324 struct wpa_tdls_timeoutie timeoutie;
1330 if (wpa_tdls_get_privacy(sm)) {
1331 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1333 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1334 sizeof(struct wpa_tdls_timeoutie);
1335 #ifdef CONFIG_TDLS_TESTING
1336 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1338 #endif /* CONFIG_TDLS_TESTING */
1341 rbuf = os_zalloc(buf_len + 1);
1346 if (!wpa_tdls_get_privacy(sm))
1350 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1352 ftie = (struct wpa_tdls_ftie *) pos;
1353 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1354 /*TODO: ftie->mic_control to set 3-CONFIRM */
1355 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1356 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1357 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1359 pos = (u8 *) (ftie + 1);
1361 #ifdef CONFIG_TDLS_TESTING
1362 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1363 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1365 ftie->ie_len += 170;
1366 *pos++ = 255; /* FTIE subelem */
1367 *pos++ = 168; /* FTIE subelem length */
1370 #endif /* CONFIG_TDLS_TESTING */
1373 lifetime = peer->lifetime;
1374 #ifdef CONFIG_TDLS_TESTING
1375 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_CONF) {
1376 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1377 "lifetime in confirm");
1380 #endif /* CONFIG_TDLS_TESTING */
1381 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1382 sizeof(timeoutie), lifetime);
1383 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds",
1386 /* compute MIC before sending */
1387 wpa_tdls_ftie_mic(peer->tpk.kck, 3, (u8 *) lnkid, peer->rsnie_p,
1388 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
1389 #ifdef CONFIG_TDLS_TESTING
1390 if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
1391 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
1392 ftie->mic[0] ^= 0x01;
1394 #endif /* CONFIG_TDLS_TESTING */
1398 if (peer->vht_capabilities)
1399 peer_capab |= TDLS_PEER_VHT;
1400 if (peer->ht_capabilities)
1401 peer_capab |= TDLS_PEER_HT;
1402 if (peer->wmm_capable)
1403 peer_capab |= TDLS_PEER_WMM;
1405 status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
1406 dtoken, 0, peer_capab, peer->initiator,
1414 static int wpa_tdls_send_discovery_response(struct wpa_sm *sm,
1415 struct wpa_tdls_peer *peer,
1419 struct wpa_tdls_timeoutie timeoutie;
1421 u8 *rbuf, *pos, *count_pos;
1423 struct rsn_ie_hdr *hdr;
1426 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Discovery Response "
1427 "(peer " MACSTR ")", MAC2STR(peer->addr));
1428 if (!wpa_tdls_get_privacy(sm))
1431 /* Filling RSN IE */
1432 hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
1433 hdr->elem_id = WLAN_EID_RSN;
1434 WPA_PUT_LE16(hdr->version, RSN_VERSION);
1435 pos = (u8 *) (hdr + 1);
1436 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1437 pos += RSN_SELECTOR_LEN;
1443 * AES-CCMP is the default encryption preferred for TDLS, so
1444 * RSN IE is filled only with CCMP cipher suite.
1445 * Note: TKIP is not used to encrypt TDLS link.
1447 * Regardless of the cipher used on the AP connection, select CCMP
1450 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1451 pos += RSN_SELECTOR_LEN;
1453 WPA_PUT_LE16(count_pos, count);
1454 WPA_PUT_LE16(pos, 1);
1456 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1457 pos += RSN_SELECTOR_LEN;
1459 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1460 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1461 WPA_PUT_LE16(pos, rsn_capab);
1463 hdr->len = (pos - (u8 *) hdr) - 2;
1464 peer->rsnie_i_len = pos - peer->rsnie_i;
1466 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for Discovery Response",
1467 (u8 *) hdr, hdr->len + 2);
1470 if (wpa_tdls_get_privacy(sm)) {
1471 /* Peer RSN IE, Lifetime */
1472 buf_len += peer->rsnie_i_len +
1473 sizeof(struct wpa_tdls_timeoutie);
1475 rbuf = os_zalloc(buf_len + 1);
1477 wpa_tdls_peer_free(sm, peer);
1482 if (!wpa_tdls_get_privacy(sm))
1484 /* Initiator RSN IE */
1485 pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
1487 peer->lifetime = TPK_LIFETIME;
1488 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1489 sizeof(timeoutie), peer->lifetime);
1490 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
1492 status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_DISCOVERY_RESPONSE,
1493 dialog_token, 0, 0, 0, rbuf, pos - rbuf);
1501 wpa_tdls_process_discovery_request(struct wpa_sm *sm, const u8 *addr,
1502 const u8 *buf, size_t len)
1504 struct wpa_eapol_ie_parse kde;
1505 const struct wpa_tdls_lnkid *lnkid;
1506 struct wpa_tdls_peer *peer;
1507 size_t min_req_len = sizeof(struct wpa_tdls_frame) +
1508 1 /* dialog token */ + sizeof(struct wpa_tdls_lnkid);
1511 wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from " MACSTR,
1514 if (len < min_req_len) {
1515 wpa_printf(MSG_DEBUG, "TDLS Discovery Request is too short: "
1520 dialog_token = buf[sizeof(struct wpa_tdls_frame)];
1523 * Some APs will tack on a weird IE to the end of a TDLS
1524 * discovery request packet. This needn't fail the response,
1525 * since the required IE are verified separately.
1527 if (wpa_supplicant_parse_ies(buf + sizeof(struct wpa_tdls_frame) + 1,
1528 len - (sizeof(struct wpa_tdls_frame) + 1),
1530 wpa_printf(MSG_DEBUG,
1531 "TDLS: Failed to parse IEs in Discovery Request - ignore as an interop workaround");
1535 wpa_printf(MSG_DEBUG, "TDLS: Link ID not found in Discovery "
1540 lnkid = (const struct wpa_tdls_lnkid *) kde.lnkid;
1542 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1543 wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from different "
1544 " BSS " MACSTR, MAC2STR(lnkid->bssid));
1548 peer = wpa_tdls_add_peer(sm, addr, NULL);
1552 return wpa_tdls_send_discovery_response(sm, peer, dialog_token);
1556 int wpa_tdls_send_discovery_request(struct wpa_sm *sm, const u8 *addr)
1558 if (sm->tdls_disabled || !sm->tdls_supported)
1561 wpa_printf(MSG_DEBUG, "TDLS: Sending Discovery Request to peer "
1562 MACSTR, MAC2STR(addr));
1563 return wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_DISCOVERY_REQUEST,
1564 1, 0, 0, 1, NULL, 0);
1568 static int copy_supp_rates(const struct wpa_eapol_ie_parse *kde,
1569 struct wpa_tdls_peer *peer)
1571 if (!kde->supp_rates) {
1572 wpa_printf(MSG_DEBUG, "TDLS: No supported rates received");
1575 peer->supp_rates_len = merge_byte_arrays(
1576 peer->supp_rates, sizeof(peer->supp_rates),
1577 kde->supp_rates + 2, kde->supp_rates_len - 2,
1578 kde->ext_supp_rates ? kde->ext_supp_rates + 2 : NULL,
1579 kde->ext_supp_rates_len - 2);
1584 static int copy_peer_ht_capab(const struct wpa_eapol_ie_parse *kde,
1585 struct wpa_tdls_peer *peer)
1587 if (!kde->ht_capabilities) {
1588 wpa_printf(MSG_DEBUG, "TDLS: No supported ht capabilities "
1593 if (!peer->ht_capabilities) {
1594 peer->ht_capabilities =
1595 os_zalloc(sizeof(struct ieee80211_ht_capabilities));
1596 if (peer->ht_capabilities == NULL)
1600 os_memcpy(peer->ht_capabilities, kde->ht_capabilities,
1601 sizeof(struct ieee80211_ht_capabilities));
1602 wpa_hexdump(MSG_DEBUG, "TDLS: Peer HT capabilities",
1603 (u8 *) peer->ht_capabilities,
1604 sizeof(struct ieee80211_ht_capabilities));
1610 static int copy_peer_vht_capab(const struct wpa_eapol_ie_parse *kde,
1611 struct wpa_tdls_peer *peer)
1613 if (!kde->vht_capabilities) {
1614 wpa_printf(MSG_DEBUG, "TDLS: No supported vht capabilities "
1619 if (!peer->vht_capabilities) {
1620 peer->vht_capabilities =
1621 os_zalloc(sizeof(struct ieee80211_vht_capabilities));
1622 if (peer->vht_capabilities == NULL)
1626 os_memcpy(peer->vht_capabilities, kde->vht_capabilities,
1627 sizeof(struct ieee80211_vht_capabilities));
1628 wpa_hexdump(MSG_DEBUG, "TDLS: Peer VHT capabilities",
1629 (u8 *) peer->vht_capabilities,
1630 sizeof(struct ieee80211_vht_capabilities));
1636 static int copy_peer_ext_capab(const struct wpa_eapol_ie_parse *kde,
1637 struct wpa_tdls_peer *peer)
1639 if (!kde->ext_capab) {
1640 wpa_printf(MSG_DEBUG, "TDLS: No extended capabilities "
1645 if (!peer->ext_capab || peer->ext_capab_len < kde->ext_capab_len - 2) {
1646 /* Need to allocate buffer to fit the new information */
1647 os_free(peer->ext_capab);
1648 peer->ext_capab = os_zalloc(kde->ext_capab_len - 2);
1649 if (peer->ext_capab == NULL)
1653 peer->ext_capab_len = kde->ext_capab_len - 2;
1654 os_memcpy(peer->ext_capab, kde->ext_capab + 2, peer->ext_capab_len);
1660 static int copy_peer_wmm_capab(const struct wpa_eapol_ie_parse *kde,
1661 struct wpa_tdls_peer *peer)
1663 struct wmm_information_element *wmm;
1666 wpa_printf(MSG_DEBUG, "TDLS: No supported WMM capabilities received");
1670 if (kde->wmm_len < sizeof(struct wmm_information_element)) {
1671 wpa_printf(MSG_DEBUG, "TDLS: Invalid supported WMM capabilities received");
1675 wmm = (struct wmm_information_element *) kde->wmm;
1676 peer->qos_info = wmm->qos_info;
1678 peer->wmm_capable = 1;
1680 wpa_printf(MSG_DEBUG, "TDLS: Peer WMM QOS Info 0x%x", peer->qos_info);
1685 static int copy_peer_supp_channels(const struct wpa_eapol_ie_parse *kde,
1686 struct wpa_tdls_peer *peer)
1688 if (!kde->supp_channels) {
1689 wpa_printf(MSG_DEBUG, "TDLS: No supported channels received");
1693 if (!peer->supp_channels ||
1694 peer->supp_channels_len < kde->supp_channels_len) {
1695 os_free(peer->supp_channels);
1696 peer->supp_channels = os_zalloc(kde->supp_channels_len);
1697 if (peer->supp_channels == NULL)
1701 peer->supp_channels_len = kde->supp_channels_len;
1703 os_memcpy(peer->supp_channels, kde->supp_channels,
1704 peer->supp_channels_len);
1705 wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Channels",
1706 (u8 *) peer->supp_channels, peer->supp_channels_len);
1711 static int copy_peer_supp_oper_classes(const struct wpa_eapol_ie_parse *kde,
1712 struct wpa_tdls_peer *peer)
1714 if (!kde->supp_oper_classes) {
1715 wpa_printf(MSG_DEBUG, "TDLS: No supported operating classes received");
1719 if (!peer->supp_oper_classes ||
1720 peer->supp_oper_classes_len < kde->supp_oper_classes_len) {
1721 os_free(peer->supp_oper_classes);
1722 peer->supp_oper_classes = os_zalloc(kde->supp_oper_classes_len);
1723 if (peer->supp_oper_classes == NULL)
1727 peer->supp_oper_classes_len = kde->supp_oper_classes_len;
1728 os_memcpy(peer->supp_oper_classes, kde->supp_oper_classes,
1729 peer->supp_oper_classes_len);
1730 wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Operating Classes",
1731 (u8 *) peer->supp_oper_classes,
1732 peer->supp_oper_classes_len);
1737 static int wpa_tdls_addset_peer(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
1740 return wpa_sm_tdls_peer_addset(sm, peer->addr, add, peer->aid,
1742 peer->supp_rates, peer->supp_rates_len,
1743 peer->ht_capabilities,
1744 peer->vht_capabilities,
1745 peer->qos_info, peer->wmm_capable,
1746 peer->ext_capab, peer->ext_capab_len,
1747 peer->supp_channels,
1748 peer->supp_channels_len,
1749 peer->supp_oper_classes,
1750 peer->supp_oper_classes_len);
1754 static int wpa_tdls_process_tpk_m1(struct wpa_sm *sm, const u8 *src_addr,
1755 const u8 *buf, size_t len)
1757 struct wpa_tdls_peer *peer;
1758 struct wpa_eapol_ie_parse kde;
1759 struct wpa_ie_data ie;
1762 struct wpa_tdls_ftie *ftie = NULL;
1763 struct wpa_tdls_timeoutie *timeoutie;
1764 struct wpa_tdls_lnkid *lnkid;
1767 struct rsn_ie_hdr *hdr;
1774 u16 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1775 int tdls_prohibited = sm->tdls_prohibited;
1776 int existing_peer = 0;
1782 cpos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1784 /* driver had already verified the frame format */
1785 dtoken = *cpos++; /* dialog token */
1787 wpa_printf(MSG_INFO, "TDLS: Dialog Token in TPK M1 %d", dtoken);
1789 peer = wpa_tdls_add_peer(sm, src_addr, &existing_peer);
1793 /* If found, use existing entry instead of adding a new one;
1794 * how to handle the case where both ends initiate at the
1796 if (existing_peer) {
1797 if (peer->tpk_success) {
1798 wpa_printf(MSG_DEBUG, "TDLS: TDLS Setup Request while "
1799 "direct link is enabled - tear down the "
1801 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
1802 wpa_tdls_peer_clear(sm, peer);
1803 } else if (peer->initiator) {
1805 * An entry is already present, so check if we already
1806 * sent a TDLS Setup Request. If so, compare MAC
1807 * addresses and let the STA with the lower MAC address
1808 * continue as the initiator. The other negotiation is
1811 if (os_memcmp(sm->own_addr, src_addr, ETH_ALEN) < 0) {
1812 wpa_printf(MSG_DEBUG, "TDLS: Discard request "
1813 "from peer with higher address "
1814 MACSTR, MAC2STR(src_addr));
1817 wpa_printf(MSG_DEBUG, "TDLS: Accept request "
1818 "from peer with lower address "
1819 MACSTR " (terminate previously "
1820 "initiated negotiation",
1822 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK,
1824 wpa_tdls_peer_clear(sm, peer);
1829 /* capability information */
1830 peer->capability = WPA_GET_LE16(cpos);
1833 ielen = len - (cpos - buf); /* start of IE in buf */
1836 * Don't reject the message if failing to parse IEs. The IEs we need are
1837 * explicitly checked below. Some APs may add arbitrary padding to the
1838 * end of short TDLS frames and that would look like invalid IEs.
1840 if (wpa_supplicant_parse_ies(cpos, ielen, &kde) < 0)
1841 wpa_printf(MSG_DEBUG,
1842 "TDLS: Failed to parse IEs in TPK M1 - ignore as an interop workaround");
1844 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
1845 wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
1849 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M1",
1850 kde.lnkid, kde.lnkid_len);
1851 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
1852 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1853 wpa_printf(MSG_INFO, "TDLS: TPK M1 from diff BSS");
1854 status = WLAN_STATUS_REQUEST_DECLINED;
1858 wpa_printf(MSG_DEBUG, "TDLS: TPK M1 - TPK initiator " MACSTR,
1861 if (copy_supp_rates(&kde, peer) < 0)
1864 if (copy_peer_ht_capab(&kde, peer) < 0)
1867 if (copy_peer_vht_capab(&kde, peer) < 0)
1870 if (copy_peer_ext_capab(&kde, peer) < 0)
1873 if (copy_peer_supp_channels(&kde, peer) < 0)
1876 if (copy_peer_supp_oper_classes(&kde, peer) < 0)
1879 peer->qos_info = kde.qosinfo;
1881 /* Overwrite with the qos_info obtained in WMM IE */
1882 if (copy_peer_wmm_capab(&kde, peer) < 0)
1885 peer->aid = kde.aid;
1887 #ifdef CONFIG_TDLS_TESTING
1888 if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
1889 peer = wpa_tdls_add_peer(sm, src_addr, NULL);
1892 wpa_printf(MSG_DEBUG, "TDLS: Testing concurrent initiation of "
1893 "TDLS setup - send own request");
1894 peer->initiator = 1;
1895 wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL,
1896 NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0);
1897 wpa_tdls_send_tpk_m1(sm, peer);
1900 if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
1902 wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
1904 tdls_prohibited = 0;
1906 #endif /* CONFIG_TDLS_TESTING */
1908 if (tdls_prohibited) {
1909 wpa_printf(MSG_INFO, "TDLS: TDLS prohibited in this BSS");
1910 status = WLAN_STATUS_REQUEST_DECLINED;
1914 if (!wpa_tdls_get_privacy(sm)) {
1916 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M1 while "
1917 "security is disabled");
1918 status = WLAN_STATUS_SECURITY_DISABLED;
1924 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
1925 kde.rsn_ie == NULL) {
1926 wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M1");
1927 status = WLAN_STATUS_INVALID_PARAMETERS;
1931 if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
1932 wpa_printf(MSG_INFO, "TDLS: Too long Initiator RSN IE in "
1934 status = WLAN_STATUS_INVALID_RSNIE;
1938 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
1939 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M1");
1940 status = WLAN_STATUS_INVALID_RSNIE;
1944 cipher = ie.pairwise_cipher;
1945 if (cipher & WPA_CIPHER_CCMP) {
1946 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
1947 cipher = WPA_CIPHER_CCMP;
1949 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M1");
1950 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1954 if ((ie.capabilities &
1955 (WPA_CAPABILITY_NO_PAIRWISE | WPA_CAPABILITY_PEERKEY_ENABLED)) !=
1956 WPA_CAPABILITY_PEERKEY_ENABLED) {
1957 wpa_printf(MSG_INFO, "TDLS: Invalid RSN Capabilities in "
1959 status = WLAN_STATUS_INVALID_RSN_IE_CAPAB;
1964 if (kde.key_lifetime == NULL) {
1965 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M1");
1966 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
1969 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
1970 lifetime = WPA_GET_LE32(timeoutie->value);
1971 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", lifetime);
1972 if (lifetime < 300) {
1973 wpa_printf(MSG_INFO, "TDLS: Too short TPK lifetime");
1974 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
1979 #ifdef CONFIG_TDLS_TESTING
1980 if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
1981 if (os_memcmp(sm->own_addr, peer->addr, ETH_ALEN) < 0) {
1983 * The request frame from us is going to win, so do not
1984 * replace information based on this request frame from
1987 goto skip_rsn_check;
1990 #endif /* CONFIG_TDLS_TESTING */
1992 peer->initiator = 0; /* Need to check */
1993 peer->dtoken = dtoken;
1995 if (!wpa_tdls_get_privacy(sm)) {
1996 peer->rsnie_i_len = 0;
1997 peer->rsnie_p_len = 0;
1998 peer->cipher = WPA_CIPHER_NONE;
1999 goto skip_rsn_check;
2002 ftie = (struct wpa_tdls_ftie *) kde.ftie;
2003 os_memcpy(peer->rsnie_i, kde.rsn_ie, kde.rsn_ie_len);
2004 peer->rsnie_i_len = kde.rsn_ie_len;
2005 peer->cipher = cipher;
2007 if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
2009 * There is no point in updating the RNonce for every obtained
2010 * TPK M1 frame (e.g., retransmission due to timeout) with the
2011 * same INonce (SNonce in FTIE). However, if the TPK M1 is
2012 * retransmitted with a different INonce, update the RNonce
2013 * since this is for a new TDLS session.
2015 wpa_printf(MSG_DEBUG,
2016 "TDLS: New TPK M1 INonce - generate new RNonce");
2017 os_memcpy(peer->inonce, ftie->Snonce, WPA_NONCE_LEN);
2018 if (os_get_random(peer->rnonce, WPA_NONCE_LEN)) {
2019 wpa_msg(sm->ctx->ctx, MSG_WARNING,
2020 "TDLS: Failed to get random data for responder nonce");
2026 /* get version info from RSNIE received from Peer */
2027 hdr = (struct rsn_ie_hdr *) kde.rsn_ie;
2028 rsn_ver = WPA_GET_LE16(hdr->version);
2030 /* use min(peer's version, out version) */
2031 if (rsn_ver > RSN_VERSION)
2032 rsn_ver = RSN_VERSION;
2034 hdr = (struct rsn_ie_hdr *) peer->rsnie_p;
2036 hdr->elem_id = WLAN_EID_RSN;
2037 WPA_PUT_LE16(hdr->version, rsn_ver);
2038 pos = (u8 *) (hdr + 1);
2040 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
2041 pos += RSN_SELECTOR_LEN;
2042 /* Include only the selected cipher in pairwise cipher suite */
2043 WPA_PUT_LE16(pos, 1);
2045 if (cipher == WPA_CIPHER_CCMP)
2046 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
2047 pos += RSN_SELECTOR_LEN;
2049 WPA_PUT_LE16(pos, 1);
2051 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
2052 pos += RSN_SELECTOR_LEN;
2054 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
2055 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
2056 WPA_PUT_LE16(pos, rsn_capab);
2059 hdr->len = (pos - peer->rsnie_p) - 2;
2060 peer->rsnie_p_len = pos - peer->rsnie_p;
2063 /* temp fix: validation of RSNIE later */
2064 os_memcpy(peer->rsnie_p, peer->rsnie_i, peer->rsnie_i_len);
2065 peer->rsnie_p_len = peer->rsnie_i_len;
2067 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
2068 peer->rsnie_p, peer->rsnie_p_len);
2070 peer->lifetime = lifetime;
2072 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
2075 #ifdef CONFIG_TDLS_TESTING
2076 if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT)
2078 #endif /* CONFIG_TDLS_TESTING */
2080 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2081 if (wpa_tdls_addset_peer(sm, peer, 1) < 0)
2084 #ifdef CONFIG_TDLS_TESTING
2086 #endif /* CONFIG_TDLS_TESTING */
2087 peer->tpk_in_progress = 1;
2089 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Response / TPK M2");
2090 if (wpa_tdls_send_tpk_m2(sm, src_addr, dtoken, lnkid, peer) < 0) {
2091 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
2098 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE, dtoken, 0,
2101 wpa_tdls_peer_free(sm, peer);
2106 static int wpa_tdls_enable_link(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
2108 peer->tpk_success = 1;
2109 peer->tpk_in_progress = 0;
2110 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
2111 if (wpa_tdls_get_privacy(sm)) {
2112 u32 lifetime = peer->lifetime;
2114 * Start the initiator process a bit earlier to avoid race
2115 * condition with the responder sending teardown request.
2117 if (lifetime > 3 && peer->initiator)
2119 eloop_register_timeout(lifetime, 0, wpa_tdls_tpk_timeout,
2121 #ifdef CONFIG_TDLS_TESTING
2122 if (tdls_testing & TDLS_TESTING_NO_TPK_EXPIRATION) {
2123 wpa_printf(MSG_DEBUG, "TDLS: Testing - disable TPK "
2125 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
2127 #endif /* CONFIG_TDLS_TESTING */
2130 if (peer->reconfig_key && wpa_tdls_set_key(sm, peer) < 0) {
2131 wpa_printf(MSG_INFO, "TDLS: Could not configure key to the "
2135 peer->reconfig_key = 0;
2137 return wpa_sm_tdls_oper(sm, TDLS_ENABLE_LINK, peer->addr);
2141 static int wpa_tdls_process_tpk_m2(struct wpa_sm *sm, const u8 *src_addr,
2142 const u8 *buf, size_t len)
2144 struct wpa_tdls_peer *peer;
2145 struct wpa_eapol_ie_parse kde;
2146 struct wpa_ie_data ie;
2148 struct wpa_tdls_ftie *ftie;
2149 struct wpa_tdls_timeoutie *timeoutie;
2150 struct wpa_tdls_lnkid *lnkid;
2158 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Response / TPK M2 "
2159 "(Peer " MACSTR ")", MAC2STR(src_addr));
2160 for (peer = sm->tdls; peer; peer = peer->next) {
2161 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
2165 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
2166 "TPK M2: " MACSTR, MAC2STR(src_addr));
2169 if (!peer->initiator) {
2171 * This may happen if both devices try to initiate TDLS at the
2172 * same time and we accept the TPK M1 from the peer in
2173 * wpa_tdls_process_tpk_m1() and clear our previous state.
2175 wpa_printf(MSG_INFO, "TDLS: We were not the initiator, so "
2176 "ignore TPK M2 from " MACSTR, MAC2STR(src_addr));
2180 if (peer->tpk_success) {
2181 wpa_printf(MSG_INFO, "TDLS: Ignore incoming TPK M2 retry, from "
2182 MACSTR " as TPK M3 was already sent",
2187 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_REQUEST);
2189 if (len < 3 + 2 + 1) {
2190 wpa_tdls_disable_peer_link(sm, peer);
2195 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2196 status = WPA_GET_LE16(pos);
2197 pos += 2 /* status code */;
2199 if (status != WLAN_STATUS_SUCCESS) {
2200 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M2: %u",
2202 wpa_tdls_disable_peer_link(sm, peer);
2206 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2208 /* TODO: need to verify dialog token matches here or in kernel */
2209 dtoken = *pos++; /* dialog token */
2211 wpa_printf(MSG_DEBUG, "TDLS: Dialog Token in TPK M2 %d", dtoken);
2213 if (len < 3 + 2 + 1 + 2) {
2214 wpa_tdls_disable_peer_link(sm, peer);
2218 /* capability information */
2219 peer->capability = WPA_GET_LE16(pos);
2222 ielen = len - (pos - buf); /* start of IE in buf */
2225 * Don't reject the message if failing to parse IEs. The IEs we need are
2226 * explicitly checked below. Some APs may add arbitrary padding to the
2227 * end of short TDLS frames and that would look like invalid IEs.
2229 if (wpa_supplicant_parse_ies(pos, ielen, &kde) < 0)
2230 wpa_printf(MSG_DEBUG,
2231 "TDLS: Failed to parse IEs in TPK M2 - ignore as an interop workaround");
2233 #ifdef CONFIG_TDLS_TESTING
2234 if (tdls_testing & TDLS_TESTING_DECLINE_RESP) {
2235 wpa_printf(MSG_DEBUG, "TDLS: Testing - decline response");
2236 status = WLAN_STATUS_REQUEST_DECLINED;
2239 #endif /* CONFIG_TDLS_TESTING */
2241 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
2242 wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
2246 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M2",
2247 kde.lnkid, kde.lnkid_len);
2248 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
2250 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
2251 wpa_printf(MSG_INFO, "TDLS: TPK M2 from different BSS");
2252 status = WLAN_STATUS_NOT_IN_SAME_BSS;
2256 if (copy_supp_rates(&kde, peer) < 0)
2259 if (copy_peer_ht_capab(&kde, peer) < 0)
2262 if (copy_peer_vht_capab(&kde, peer) < 0)
2265 if (copy_peer_ext_capab(&kde, peer) < 0)
2268 if (copy_peer_supp_channels(&kde, peer) < 0)
2271 if (copy_peer_supp_oper_classes(&kde, peer) < 0)
2274 peer->qos_info = kde.qosinfo;
2276 /* Overwrite with the qos_info obtained in WMM IE */
2277 if (copy_peer_wmm_capab(&kde, peer) < 0)
2280 peer->aid = kde.aid;
2282 if (!wpa_tdls_get_privacy(sm)) {
2283 peer->rsnie_p_len = 0;
2284 peer->cipher = WPA_CIPHER_NONE;
2288 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
2289 kde.rsn_ie == NULL) {
2290 wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M2");
2291 status = WLAN_STATUS_INVALID_PARAMETERS;
2294 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
2295 kde.rsn_ie, kde.rsn_ie_len);
2297 if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
2298 wpa_printf(MSG_INFO,
2299 "TDLS: Too long Responder RSN IE in TPK M2");
2300 status = WLAN_STATUS_INVALID_RSNIE;
2305 * FIX: bitwise comparison of RSN IE is not the correct way of
2306 * validation this. It can be different, but certain fields must
2307 * match. Since we list only a single pairwise cipher in TPK M1, the
2308 * memcmp is likely to work in most cases, though.
2310 if (kde.rsn_ie_len != peer->rsnie_i_len ||
2311 os_memcmp(peer->rsnie_i, kde.rsn_ie, peer->rsnie_i_len) != 0) {
2312 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M2 does "
2313 "not match with RSN IE used in TPK M1");
2314 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Sent in TPK M1",
2315 peer->rsnie_i, peer->rsnie_i_len);
2316 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
2317 kde.rsn_ie, kde.rsn_ie_len);
2318 status = WLAN_STATUS_INVALID_RSNIE;
2322 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
2323 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M2");
2324 status = WLAN_STATUS_INVALID_RSNIE;
2328 cipher = ie.pairwise_cipher;
2329 if (cipher == WPA_CIPHER_CCMP) {
2330 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
2331 cipher = WPA_CIPHER_CCMP;
2333 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M2");
2334 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
2338 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M2",
2339 kde.ftie, sizeof(*ftie));
2340 ftie = (struct wpa_tdls_ftie *) kde.ftie;
2342 if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
2343 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M2 does "
2344 "not match with FTIE SNonce used in TPK M1");
2345 /* Silently discard the frame */
2349 /* Responder Nonce and RSN IE */
2350 os_memcpy(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN);
2351 os_memcpy(peer->rsnie_p, kde.rsn_ie, kde.rsn_ie_len);
2352 peer->rsnie_p_len = kde.rsn_ie_len;
2353 peer->cipher = cipher;
2356 if (kde.key_lifetime == NULL) {
2357 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M2");
2358 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2361 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2362 lifetime = WPA_GET_LE32(timeoutie->value);
2363 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M2",
2365 if (lifetime != peer->lifetime) {
2366 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2367 "TPK M2 (expected %u)", lifetime, peer->lifetime);
2368 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2372 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
2374 /* Process MIC check to see if TPK M2 is right */
2375 if (wpa_supplicant_verify_tdls_mic(2, peer, (u8 *) lnkid,
2376 (u8 *) timeoutie, ftie) < 0) {
2377 /* Discard the frame */
2378 wpa_tdls_del_key(sm, peer);
2379 wpa_tdls_disable_peer_link(sm, peer);
2383 if (wpa_tdls_set_key(sm, peer) < 0) {
2385 * Some drivers may not be able to config the key prior to full
2386 * STA entry having been configured.
2388 wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2389 "STA entry is complete");
2390 peer->reconfig_key = 1;
2394 peer->dtoken = dtoken;
2396 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2397 if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
2400 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Confirm / "
2401 "TPK Handshake Message 3");
2402 if (wpa_tdls_send_tpk_m3(sm, src_addr, dtoken, lnkid, peer) < 0)
2405 if (!peer->tpk_success) {
2407 * Enable Link only when tpk_success is 0, signifying that this
2408 * processing of TPK M2 frame is not because of a retransmission
2409 * during TDLS setup handshake.
2411 ret = wpa_tdls_enable_link(sm, peer);
2413 wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
2414 wpa_tdls_do_teardown(
2416 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
2422 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM, dtoken, 1,
2425 wpa_tdls_disable_peer_link(sm, peer);
2430 static int wpa_tdls_process_tpk_m3(struct wpa_sm *sm, const u8 *src_addr,
2431 const u8 *buf, size_t len)
2433 struct wpa_tdls_peer *peer;
2434 struct wpa_eapol_ie_parse kde;
2435 struct wpa_tdls_ftie *ftie;
2436 struct wpa_tdls_timeoutie *timeoutie;
2437 struct wpa_tdls_lnkid *lnkid;
2444 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Confirm / TPK M3 "
2445 "(Peer " MACSTR ")", MAC2STR(src_addr));
2446 for (peer = sm->tdls; peer; peer = peer->next) {
2447 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
2451 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
2452 "TPK M3: " MACSTR, MAC2STR(src_addr));
2455 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_RESPONSE);
2460 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2462 status = WPA_GET_LE16(pos);
2465 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M3: %u",
2469 pos += 2 /* status code */ + 1 /* dialog token */;
2471 ielen = len - (pos - buf); /* start of IE in buf */
2474 * Don't reject the message if failing to parse IEs. The IEs we need are
2475 * explicitly checked below. Some APs piggy-back broken IEs to the end
2476 * of a TDLS Confirm packet, which will fail the link if we don't ignore
2479 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
2480 wpa_printf(MSG_DEBUG,
2481 "TDLS: Failed to parse KDEs in TPK M3 - ignore as an interop workaround");
2484 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
2485 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TPK M3");
2488 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M3",
2489 (u8 *) kde.lnkid, kde.lnkid_len);
2490 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
2492 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
2493 wpa_printf(MSG_INFO, "TDLS: TPK M3 from diff BSS");
2497 if (!wpa_tdls_get_privacy(sm))
2500 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
2501 wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M3");
2504 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M3",
2505 kde.ftie, sizeof(*ftie));
2506 ftie = (struct wpa_tdls_ftie *) kde.ftie;
2508 if (kde.rsn_ie == NULL) {
2509 wpa_printf(MSG_INFO, "TDLS: No RSN IE in TPK M3");
2512 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M3",
2513 kde.rsn_ie, kde.rsn_ie_len);
2514 if (kde.rsn_ie_len != peer->rsnie_p_len ||
2515 os_memcmp(kde.rsn_ie, peer->rsnie_p, peer->rsnie_p_len) != 0) {
2516 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M3 does not match "
2517 "with the one sent in TPK M2");
2521 if (os_memcmp(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN) != 0) {
2522 wpa_printf(MSG_INFO, "TDLS: FTIE ANonce in TPK M3 does "
2523 "not match with FTIE ANonce used in TPK M2");
2527 if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
2528 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M3 does not "
2529 "match with FTIE SNonce used in TPK M1");
2533 if (kde.key_lifetime == NULL) {
2534 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M3");
2537 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2538 wpa_hexdump(MSG_DEBUG, "TDLS: Timeout IE Received from TPK M3",
2539 (u8 *) timeoutie, sizeof(*timeoutie));
2540 lifetime = WPA_GET_LE32(timeoutie->value);
2541 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M3",
2543 if (lifetime != peer->lifetime) {
2544 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2545 "TPK M3 (expected %u)", lifetime, peer->lifetime);
2549 if (wpa_supplicant_verify_tdls_mic(3, peer, (u8 *) lnkid,
2550 (u8 *) timeoutie, ftie) < 0) {
2551 wpa_tdls_del_key(sm, peer);
2555 if (wpa_tdls_set_key(sm, peer) < 0) {
2557 * Some drivers may not be able to config the key prior to full
2558 * STA entry having been configured.
2560 wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2561 "STA entry is complete");
2562 peer->reconfig_key = 1;
2566 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2567 if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
2570 if (!peer->tpk_success) {
2572 * Enable Link only when tpk_success is 0, signifying that this
2573 * processing of TPK M3 frame is not because of a retransmission
2574 * during TDLS setup handshake.
2576 ret = wpa_tdls_enable_link(sm, peer);
2578 wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
2584 wpa_tdls_do_teardown(sm, peer, WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
2589 static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs)
2591 struct wpa_tdls_timeoutie *lifetime = (struct wpa_tdls_timeoutie *) ie;
2593 os_memset(lifetime, 0, ie_len);
2594 lifetime->ie_type = WLAN_EID_TIMEOUT_INTERVAL;
2595 lifetime->ie_len = sizeof(struct wpa_tdls_timeoutie) - 2;
2596 lifetime->interval_type = WLAN_TIMEOUT_KEY_LIFETIME;
2597 WPA_PUT_LE32(lifetime->value, tsecs);
2598 os_memcpy(pos, ie, ie_len);
2599 return pos + ie_len;
2604 * wpa_tdls_start - Initiate TDLS handshake (send TPK Handshake Message 1)
2605 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2606 * @peer: MAC address of the peer STA
2607 * Returns: 0 on success, or -1 on failure
2609 * Send TPK Handshake Message 1 info to driver to start TDLS
2610 * handshake with the peer.
2612 int wpa_tdls_start(struct wpa_sm *sm, const u8 *addr)
2614 struct wpa_tdls_peer *peer;
2615 int tdls_prohibited = sm->tdls_prohibited;
2617 if (sm->tdls_disabled || !sm->tdls_supported)
2620 #ifdef CONFIG_TDLS_TESTING
2621 if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
2623 wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
2625 tdls_prohibited = 0;
2627 #endif /* CONFIG_TDLS_TESTING */
2629 if (tdls_prohibited) {
2630 wpa_printf(MSG_DEBUG, "TDLS: TDLS is prohibited in this BSS - "
2631 "reject request to start setup");
2635 peer = wpa_tdls_add_peer(sm, addr, NULL);
2639 if (peer->tpk_in_progress) {
2640 wpa_printf(MSG_DEBUG, "TDLS: Setup is already in progress with the peer");
2644 peer->initiator = 1;
2646 /* add the peer to the driver as a "setup in progress" peer */
2647 if (wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL,
2648 NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0)) {
2649 wpa_tdls_disable_peer_link(sm, peer);
2653 peer->tpk_in_progress = 1;
2655 if (wpa_tdls_send_tpk_m1(sm, peer) < 0) {
2656 wpa_tdls_disable_peer_link(sm, peer);
2664 void wpa_tdls_remove(struct wpa_sm *sm, const u8 *addr)
2666 struct wpa_tdls_peer *peer;
2668 if (sm->tdls_disabled || !sm->tdls_supported)
2671 for (peer = sm->tdls; peer; peer = peer->next) {
2672 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
2676 if (peer == NULL || !peer->tpk_success)
2679 if (sm->tdls_external_setup) {
2681 * Disable previous link to allow renegotiation to be completed
2684 wpa_tdls_do_teardown(sm, peer,
2685 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
2691 * wpa_supplicant_rx_tdls - Receive TDLS data frame
2693 * This function is called to receive TDLS (ethertype = 0x890d) data frames.
2695 static void wpa_supplicant_rx_tdls(void *ctx, const u8 *src_addr,
2696 const u8 *buf, size_t len)
2698 struct wpa_sm *sm = ctx;
2699 struct wpa_tdls_frame *tf;
2701 wpa_hexdump(MSG_DEBUG, "TDLS: Received Data frame encapsulation",
2704 if (sm->tdls_disabled || !sm->tdls_supported) {
2705 wpa_printf(MSG_DEBUG, "TDLS: Discard message - TDLS disabled "
2706 "or unsupported by driver");
2710 if (os_memcmp(src_addr, sm->own_addr, ETH_ALEN) == 0) {
2711 wpa_printf(MSG_DEBUG, "TDLS: Discard copy of own message");
2715 if (len < sizeof(*tf)) {
2716 wpa_printf(MSG_INFO, "TDLS: Drop too short frame");
2720 /* Check to make sure its a valid encapsulated TDLS frame */
2721 tf = (struct wpa_tdls_frame *) buf;
2722 if (tf->payloadtype != 2 /* TDLS_RFTYPE */ ||
2723 tf->category != WLAN_ACTION_TDLS) {
2724 wpa_printf(MSG_INFO, "TDLS: Invalid frame - payloadtype=%u "
2725 "category=%u action=%u",
2726 tf->payloadtype, tf->category, tf->action);
2730 switch (tf->action) {
2731 case WLAN_TDLS_SETUP_REQUEST:
2732 wpa_tdls_process_tpk_m1(sm, src_addr, buf, len);
2734 case WLAN_TDLS_SETUP_RESPONSE:
2735 wpa_tdls_process_tpk_m2(sm, src_addr, buf, len);
2737 case WLAN_TDLS_SETUP_CONFIRM:
2738 wpa_tdls_process_tpk_m3(sm, src_addr, buf, len);
2740 case WLAN_TDLS_TEARDOWN:
2741 wpa_tdls_recv_teardown(sm, src_addr, buf, len);
2743 case WLAN_TDLS_DISCOVERY_REQUEST:
2744 wpa_tdls_process_discovery_request(sm, src_addr, buf, len);
2747 /* Kernel code will process remaining frames */
2748 wpa_printf(MSG_DEBUG, "TDLS: Ignore TDLS frame action code %u",
2756 * wpa_tdls_init - Initialize driver interface parameters for TDLS
2757 * @wpa_s: Pointer to wpa_supplicant data
2758 * Returns: 0 on success, -1 on failure
2760 * This function is called to initialize driver interface parameters for TDLS.
2761 * wpa_drv_init() must have been called before this function to initialize the
2764 int wpa_tdls_init(struct wpa_sm *sm)
2769 sm->l2_tdls = l2_packet_init(sm->bridge_ifname ? sm->bridge_ifname :
2772 ETH_P_80211_ENCAP, wpa_supplicant_rx_tdls,
2774 if (sm->l2_tdls == NULL) {
2775 wpa_printf(MSG_ERROR, "TDLS: Failed to open l2_packet "
2781 * Drivers that support TDLS but don't implement the get_capa callback
2782 * are assumed to perform everything internally
2784 if (wpa_sm_tdls_get_capa(sm, &sm->tdls_supported,
2785 &sm->tdls_external_setup,
2786 &sm->tdls_chan_switch) < 0) {
2787 sm->tdls_supported = 1;
2788 sm->tdls_external_setup = 0;
2791 wpa_printf(MSG_DEBUG, "TDLS: TDLS operation%s supported by "
2792 "driver", sm->tdls_supported ? "" : " not");
2793 wpa_printf(MSG_DEBUG, "TDLS: Driver uses %s link setup",
2794 sm->tdls_external_setup ? "external" : "internal");
2795 wpa_printf(MSG_DEBUG, "TDLS: Driver %s TDLS channel switching",
2796 sm->tdls_chan_switch ? "supports" : "does not support");
2802 void wpa_tdls_teardown_peers(struct wpa_sm *sm)
2804 struct wpa_tdls_peer *peer, *tmp;
2810 wpa_printf(MSG_DEBUG, "TDLS: Tear down peers");
2814 wpa_printf(MSG_DEBUG, "TDLS: Tear down peer " MACSTR,
2815 MAC2STR(peer->addr));
2816 if (sm->tdls_external_setup)
2817 wpa_tdls_do_teardown(sm, peer,
2818 WLAN_REASON_DEAUTH_LEAVING);
2820 wpa_sm_tdls_oper(sm, TDLS_TEARDOWN, peer->addr);
2827 static void wpa_tdls_remove_peers(struct wpa_sm *sm)
2829 struct wpa_tdls_peer *peer, *tmp;
2836 res = wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
2837 wpa_printf(MSG_DEBUG, "TDLS: Remove peer " MACSTR " (res=%d)",
2838 MAC2STR(peer->addr), res);
2839 wpa_tdls_peer_free(sm, peer);
2846 * wpa_tdls_deinit - Deinitialize driver interface parameters for TDLS
2848 * This function is called to recover driver interface parameters for TDLS
2849 * and frees resources allocated for it.
2851 void wpa_tdls_deinit(struct wpa_sm *sm)
2857 l2_packet_deinit(sm->l2_tdls);
2860 wpa_tdls_remove_peers(sm);
2864 void wpa_tdls_assoc(struct wpa_sm *sm)
2866 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on association");
2867 wpa_tdls_remove_peers(sm);
2871 void wpa_tdls_disassoc(struct wpa_sm *sm)
2873 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on disassociation");
2874 wpa_tdls_remove_peers(sm);
2878 static int wpa_tdls_prohibited(struct ieee802_11_elems *elems)
2880 /* bit 38 - TDLS Prohibited */
2881 return !!(elems->ext_capab[2 + 4] & 0x40);
2885 static int wpa_tdls_chan_switch_prohibited(struct ieee802_11_elems *elems)
2887 /* bit 39 - TDLS Channel Switch Prohibited */
2888 return !!(elems->ext_capab[2 + 4] & 0x80);
2892 void wpa_tdls_ap_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2894 struct ieee802_11_elems elems;
2896 sm->tdls_prohibited = 0;
2897 sm->tdls_chan_switch_prohibited = 0;
2900 ieee802_11_parse_elems(ies, len, &elems, 0) == ParseFailed ||
2901 elems.ext_capab == NULL || elems.ext_capab_len < 2 + 5)
2904 sm->tdls_prohibited = wpa_tdls_prohibited(&elems);
2905 wpa_printf(MSG_DEBUG, "TDLS: TDLS is %s in the target BSS",
2906 sm->tdls_prohibited ? "prohibited" : "allowed");
2907 sm->tdls_chan_switch_prohibited =
2908 wpa_tdls_chan_switch_prohibited(&elems);
2909 wpa_printf(MSG_DEBUG, "TDLS: TDLS channel switch %s in the target BSS",
2910 sm->tdls_chan_switch_prohibited ? "prohibited" : "allowed");
2914 void wpa_tdls_assoc_resp_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2916 struct ieee802_11_elems elems;
2919 ieee802_11_parse_elems(ies, len, &elems, 0) == ParseFailed ||
2920 elems.ext_capab == NULL || elems.ext_capab_len < 2 + 5)
2923 if (!sm->tdls_prohibited && wpa_tdls_prohibited(&elems)) {
2924 wpa_printf(MSG_DEBUG, "TDLS: TDLS prohibited based on "
2925 "(Re)Association Response IEs");
2926 sm->tdls_prohibited = 1;
2929 if (!sm->tdls_chan_switch_prohibited &&
2930 wpa_tdls_chan_switch_prohibited(&elems)) {
2931 wpa_printf(MSG_DEBUG,
2932 "TDLS: TDLS channel switch prohibited based on (Re)Association Response IEs");
2933 sm->tdls_chan_switch_prohibited = 1;
2938 void wpa_tdls_enable(struct wpa_sm *sm, int enabled)
2940 wpa_printf(MSG_DEBUG, "TDLS: %s", enabled ? "enabled" : "disabled");
2941 sm->tdls_disabled = !enabled;
2945 int wpa_tdls_is_external_setup(struct wpa_sm *sm)
2947 return sm->tdls_external_setup;
2951 int wpa_tdls_enable_chan_switch(struct wpa_sm *sm, const u8 *addr,
2953 struct hostapd_freq_params *freq_params)
2955 struct wpa_tdls_peer *peer;
2958 if (sm->tdls_disabled || !sm->tdls_supported)
2961 if (!sm->tdls_chan_switch) {
2962 wpa_printf(MSG_DEBUG,
2963 "TDLS: Channel switching not supported by the driver");
2967 if (sm->tdls_chan_switch_prohibited) {
2968 wpa_printf(MSG_DEBUG,
2969 "TDLS: Channel switching is prohibited in this BSS - reject request to switch channel");
2973 for (peer = sm->tdls; peer; peer = peer->next) {
2974 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
2978 if (peer == NULL || !peer->tpk_success) {
2979 wpa_printf(MSG_ERROR, "TDLS: Peer " MACSTR
2980 " not found for channel switching", MAC2STR(addr));
2984 if (peer->chan_switch_enabled) {
2985 wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
2986 " already has channel switching enabled",
2991 ret = wpa_sm_tdls_enable_channel_switch(sm, peer->addr,
2992 oper_class, freq_params);
2994 peer->chan_switch_enabled = 1;
3000 int wpa_tdls_disable_chan_switch(struct wpa_sm *sm, const u8 *addr)
3002 struct wpa_tdls_peer *peer;
3004 if (sm->tdls_disabled || !sm->tdls_supported)
3007 for (peer = sm->tdls; peer; peer = peer->next) {
3008 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
3012 if (!peer || !peer->chan_switch_enabled) {
3013 wpa_printf(MSG_ERROR, "TDLS: Channel switching not enabled for "
3014 MACSTR, MAC2STR(addr));
3018 /* ignore the return value */
3019 wpa_sm_tdls_disable_channel_switch(sm, peer->addr);
3021 peer->chan_switch_enabled = 0;