2 * wpa_supplicant - TDLS
3 * Copyright (c) 2010-2011, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
20 #include "common/ieee802_11_defs.h"
21 #include "crypto/sha256.h"
22 #include "crypto/crypto.h"
23 #include "crypto/aes_wrap.h"
24 #include "rsn_supp/wpa.h"
25 #include "rsn_supp/wpa_ie.h"
26 #include "rsn_supp/wpa_i.h"
27 #include "drivers/driver.h"
28 #include "l2_packet/l2_packet.h"
30 #ifdef CONFIG_TDLS_TESTING
31 #define TDLS_TESTING_LONG_FRAME BIT(0)
32 #define TDLS_TESTING_ALT_RSN_IE BIT(1)
33 #define TDLS_TESTING_DIFF_BSSID BIT(2)
34 #define TDLS_TESTING_SHORT_LIFETIME BIT(3)
35 #define TDLS_TESTING_WRONG_LIFETIME_RESP BIT(4)
36 #define TDLS_TESTING_WRONG_LIFETIME_CONF BIT(5)
37 unsigned int tdls_testing = 0;
38 #endif /* CONFIG_TDLS_TESTING */
40 #define TPK_LIFETIME 43200 /* 12 hours */
41 #define TPK_RETRY_COUNT 3
42 #define TPK_TIMEOUT 5000 /* in milliseconds */
44 #define TDLS_MIC_LEN 16
46 #define TDLS_TIMEOUT_LEN 4
48 struct wpa_tdls_ftie {
49 u8 ie_type; /* FTIE */
53 u8 Anonce[WPA_NONCE_LEN]; /* Responder Nonce in TDLS */
54 u8 Snonce[WPA_NONCE_LEN]; /* Initiator Nonce in TDLS */
55 /* followed by optional elements */
58 struct wpa_tdls_timeoutie {
59 u8 ie_type; /* Timeout IE */
62 u8 value[TDLS_TIMEOUT_LEN];
65 struct wpa_tdls_lnkid {
66 u8 ie_type; /* Link Identifier IE */
69 u8 init_sta[ETH_ALEN];
70 u8 resp_sta[ETH_ALEN];
73 /* TDLS frame headers as per IEEE Std 802.11z-2010 */
74 struct wpa_tdls_frame {
75 u8 payloadtype; /* IEEE80211_TDLS_RFTYPE */
76 u8 category; /* Category */
77 u8 action; /* Action (enum tdls_frame_type) */
80 static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs);
81 static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx);
82 static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer);
85 #define TDLS_MAX_IE_LEN 80
86 struct wpa_tdls_peer {
87 struct wpa_tdls_peer *next;
88 int initiator; /* whether this end was initiator for TDLS setup */
89 u8 addr[ETH_ALEN]; /* other end MAC address */
90 u8 inonce[WPA_NONCE_LEN]; /* Initiator Nonce */
91 u8 rnonce[WPA_NONCE_LEN]; /* Responder Nonce */
92 u8 rsnie_i[TDLS_MAX_IE_LEN]; /* Initiator RSN IE */
94 u8 rsnie_p[TDLS_MAX_IE_LEN]; /* Peer RSN IE */
97 int cipher; /* Selected cipher (WPA_CIPHER_*) */
99 #define TDLS_LNKID_LEN 20 /* T+L+V(18-octet) */
100 u8 lnkid[TDLS_LNKID_LEN];
104 u8 kck[16]; /* TPK-KCK */
105 u8 tk[16]; /* TPK-TK; assuming only CCMP will be used */
112 int count; /* Retry Count */
113 int timer; /* Timeout in milliseconds */
114 u8 action_code; /* TDLS frame type */
117 int buf_len; /* length of TPK message for retransmission */
118 u8 *buf; /* buffer for TPK message */
123 static int wpa_tdls_get_privacy(struct wpa_sm *sm)
126 * Get info needed from supplicant to check if the current BSS supports
127 * security. Other than OPEN mode, rest are considered secured
128 * WEP/WPA/WPA2 hence TDLS frames are processed for TPK handshake.
130 return sm->pairwise_cipher != WPA_CIPHER_NONE;
134 static u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
136 os_memcpy(pos, ie, ie_len);
141 static int wpa_tdls_del_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
143 if (wpa_sm_set_key(sm, WPA_ALG_NONE, peer->addr,
144 0, 0, NULL, 0, NULL, 0) < 0) {
145 wpa_printf(MSG_WARNING, "TDLS: Failed to delete PTK-TK from "
154 static int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
160 os_memset(rsc, 0, 6);
162 switch (peer->cipher) {
163 case WPA_CIPHER_CCMP:
167 case WPA_CIPHER_NONE:
168 wpa_printf(MSG_DEBUG, "TDLS: Pairwise Cipher Suite: "
169 "NONE - do not use pairwise keys");
172 wpa_printf(MSG_WARNING, "TDLS: Unsupported pairwise cipher %d",
173 sm->pairwise_cipher);
177 if (wpa_sm_set_key(sm, alg, peer->addr, -1, 1,
178 rsc, sizeof(rsc), peer->tpk.tk, key_len) < 0) {
179 wpa_printf(MSG_WARNING, "TDLS: Failed to set TPK to the "
187 static int wpa_tdls_send_tpk_msg(struct wpa_sm *sm, const u8 *dst,
188 u8 action_code, u8 dialog_token,
189 u16 status_code, const u8 *buf, size_t len)
191 return wpa_sm_send_tdls_mgmt(sm, dst, action_code, dialog_token,
192 status_code, buf, len);
196 static int wpa_tdls_tpk_send(struct wpa_sm *sm, const u8 *dest, u8 action_code,
197 u8 dialog_token, u16 status_code,
198 const u8 *msg, size_t msg_len)
200 struct wpa_tdls_peer *peer;
202 wpa_printf(MSG_DEBUG, "TDLS: TPK send dest=" MACSTR " action_code=%u "
203 "dialog_token=%u status_code=%u msg_len=%u",
204 MAC2STR(dest), action_code, dialog_token, status_code,
205 (unsigned int) msg_len);
207 if (wpa_tdls_send_tpk_msg(sm, dest, action_code, dialog_token,
208 status_code, msg, msg_len)) {
209 wpa_printf(MSG_INFO, "TDLS: Failed to send message "
210 "(action_code=%u)", action_code);
214 if (action_code == WLAN_TDLS_SETUP_CONFIRM ||
215 action_code == WLAN_TDLS_TEARDOWN)
216 return 0; /* No retries */
218 for (peer = sm->tdls; peer; peer = peer->next) {
219 if (os_memcmp(peer->addr, dest, ETH_ALEN) == 0)
224 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
225 "retry " MACSTR, MAC2STR(dest));
229 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
231 peer->sm_tmr.count = TPK_RETRY_COUNT;
232 peer->sm_tmr.timer = TPK_TIMEOUT;
234 /* Copy message to resend on timeout */
235 os_memcpy(peer->sm_tmr.dest, dest, ETH_ALEN);
236 peer->sm_tmr.action_code = action_code;
237 peer->sm_tmr.dialog_token = dialog_token;
238 peer->sm_tmr.status_code = status_code;
239 peer->sm_tmr.buf_len = msg_len;
240 os_free(peer->sm_tmr.buf);
241 peer->sm_tmr.buf = os_malloc(msg_len);
242 if (peer->sm_tmr.buf == NULL)
244 os_memcpy(peer->sm_tmr.buf, msg, msg_len);
246 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout registered "
247 "(action_code=%u)", action_code);
248 eloop_register_timeout(peer->sm_tmr.timer / 1000, 0,
249 wpa_tdls_tpk_retry_timeout, sm, peer);
254 static void tdls_clear_peer(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
257 struct wpa_tdls_peer *tmp;
259 os_memcpy(mac, peer->addr, ETH_ALEN);
262 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
263 os_free(peer->sm_tmr.buf);
266 os_memset(peer, 0, sizeof(*peer));
269 os_memcpy(peer->addr, mac, ETH_ALEN);
274 static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx)
277 struct wpa_sm *sm = eloop_ctx;
278 struct wpa_tdls_peer *peer = timeout_ctx;
280 if (peer->sm_tmr.count) {
281 peer->sm_tmr.count--;
282 peer->sm_tmr.timer = TPK_TIMEOUT;
284 wpa_printf(MSG_INFO, "TDLS: Retrying sending of message "
286 peer->sm_tmr.action_code);
288 if (peer->sm_tmr.buf == NULL) {
289 wpa_printf(MSG_INFO, "TDLS: No retry buffer available "
290 "for action_code=%u",
291 peer->sm_tmr.action_code);
292 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm,
297 /* resend TPK Handshake Message to Peer */
298 if (wpa_tdls_send_tpk_msg(sm, peer->sm_tmr.dest,
299 peer->sm_tmr.action_code,
300 peer->sm_tmr.dialog_token,
301 peer->sm_tmr.status_code,
303 peer->sm_tmr.buf_len)) {
304 wpa_printf(MSG_INFO, "TDLS: Failed to retry "
308 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
309 eloop_register_timeout(peer->sm_tmr.timer / 1000, 0,
310 wpa_tdls_tpk_retry_timeout, sm, peer);
312 wpa_printf(MSG_INFO, "Sending Tear_Down Request");
313 wpa_sm_tdls_oper(sm, TDLS_TEARDOWN, peer->addr);
315 wpa_printf(MSG_INFO, "Clearing SM: Peerkey(" MACSTR ")",
316 MAC2STR(peer->addr));
317 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
319 /* clear the Peerkey statemachine */
320 wpa_tdls_peer_free(sm, peer);
325 static void wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm *sm,
326 struct wpa_tdls_peer *peer,
329 if (action_code == peer->sm_tmr.action_code) {
330 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout cancelled for "
331 "action_code=%u", action_code);
333 /* Cancel Timeout registered */
334 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
336 /* free all resources meant for retry */
337 os_free(peer->sm_tmr.buf);
338 peer->sm_tmr.buf = NULL;
340 peer->sm_tmr.count = 0;
341 peer->sm_tmr.timer = 0;
342 peer->sm_tmr.buf_len = 0;
343 peer->sm_tmr.action_code = 0xff;
345 wpa_printf(MSG_INFO, "TDLS: Error in cancelling retry timeout "
346 "(Unknown action_code=%u)", action_code);
351 static void wpa_tdls_generate_tpk(struct wpa_tdls_peer *peer,
352 const u8 *own_addr, const u8 *bssid)
354 u8 key_input[SHA256_MAC_LEN];
357 u8 data[3 * ETH_ALEN];
359 /* IEEE Std 802.11z-2010 8.5.9.1:
360 * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
362 len[0] = WPA_NONCE_LEN;
363 len[1] = WPA_NONCE_LEN;
364 if (os_memcmp(peer->inonce, peer->rnonce, WPA_NONCE_LEN) < 0) {
365 nonce[0] = peer->inonce;
366 nonce[1] = peer->rnonce;
368 nonce[0] = peer->rnonce;
369 nonce[1] = peer->inonce;
371 wpa_hexdump(MSG_DEBUG, "TDLS: min(Nonce)", nonce[0], WPA_NONCE_LEN);
372 wpa_hexdump(MSG_DEBUG, "TDLS: max(Nonce)", nonce[1], WPA_NONCE_LEN);
373 sha256_vector(2, nonce, len, key_input);
374 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-Key-Input",
375 key_input, SHA256_MAC_LEN);
378 * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
379 * min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
380 * TODO: is N_KEY really included in KDF Context and if so, in which
381 * presentation format (little endian 16-bit?) is it used? It gets
382 * added by the KDF anyway..
385 if (os_memcmp(own_addr, peer->addr, ETH_ALEN) < 0) {
386 os_memcpy(data, own_addr, ETH_ALEN);
387 os_memcpy(data + ETH_ALEN, peer->addr, ETH_ALEN);
389 os_memcpy(data, peer->addr, ETH_ALEN);
390 os_memcpy(data + ETH_ALEN, own_addr, ETH_ALEN);
392 os_memcpy(data + 2 * ETH_ALEN, bssid, ETH_ALEN);
393 wpa_hexdump(MSG_DEBUG, "TDLS: KDF Context", data, sizeof(data));
395 sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data),
396 (u8 *) &peer->tpk, sizeof(peer->tpk));
397 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-KCK",
398 peer->tpk.kck, sizeof(peer->tpk.kck));
399 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-TK",
400 peer->tpk.tk, sizeof(peer->tpk.tk));
406 * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
408 * @lnkid: Pointer to the beginning of Link Identifier IE
409 * @rsnie: Pointer to the beginning of RSN IE used for handshake
410 * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
411 * @ftie: Pointer to the beginning of FT IE
412 * @mic: Pointer for writing MIC
414 * Calculate MIC for TDLS frame.
416 static int wpa_tdls_ftie_mic(const u8 *kck, u8 trans_seq, const u8 *lnkid,
417 const u8 *rsnie, const u8 *timeoutie,
418 const u8 *ftie, u8 *mic)
421 struct wpa_tdls_ftie *_ftie;
422 const struct wpa_tdls_lnkid *_lnkid;
424 int len = 2 * ETH_ALEN + 1 + 2 + lnkid[1] + 2 + rsnie[1] +
425 2 + timeoutie[1] + 2 + ftie[1];
426 buf = os_zalloc(len);
428 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
433 _lnkid = (const struct wpa_tdls_lnkid *) lnkid;
434 /* 1) TDLS initiator STA MAC address */
435 os_memcpy(pos, _lnkid->init_sta, ETH_ALEN);
437 /* 2) TDLS responder STA MAC address */
438 os_memcpy(pos, _lnkid->resp_sta, ETH_ALEN);
440 /* 3) Transaction Sequence number */
442 /* 4) Link Identifier IE */
443 os_memcpy(pos, lnkid, 2 + lnkid[1]);
446 os_memcpy(pos, rsnie, 2 + rsnie[1]);
448 /* 6) Timeout Interval IE */
449 os_memcpy(pos, timeoutie, 2 + timeoutie[1]);
450 pos += 2 + timeoutie[1];
451 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
452 os_memcpy(pos, ftie, 2 + ftie[1]);
453 _ftie = (struct wpa_tdls_ftie *) pos;
454 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
457 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
458 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
459 ret = omac1_aes_128(kck, buf, pos - buf, mic);
461 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
467 * wpa_tdls_key_mic_teardown - Calculate TDLS FTIE MIC for Teardown frame
469 * @trans_seq: Transaction Sequence Number (4 - Teardown)
470 * @rcode: Reason code for Teardown
471 * @dtoken: Dialog Token used for that particular link
472 * @lnkid: Pointer to the beginning of Link Identifier IE
473 * @ftie: Pointer to the beginning of FT IE
474 * @mic: Pointer for writing MIC
476 * Calculate MIC for TDLS frame.
478 static int wpa_tdls_key_mic_teardown(const u8 *kck, u8 trans_seq, u16 rcode,
479 u8 dtoken, const u8 *lnkid,
480 const u8 *ftie, u8 *mic)
483 struct wpa_tdls_ftie *_ftie;
490 len = 2 + lnkid[1] + sizeof(rcode) + sizeof(dtoken) +
491 sizeof(trans_seq) + 2 + ftie[1];
493 buf = os_zalloc(len);
495 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
500 /* 1) Link Identifier IE */
501 os_memcpy(pos, lnkid, 2 + lnkid[1]);
504 WPA_PUT_LE16(pos, rcode);
505 pos += sizeof(rcode);
506 /* 3) Dialog token */
508 /* 4) Transaction Sequence number */
510 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
511 os_memcpy(pos, ftie, 2 + ftie[1]);
512 _ftie = (struct wpa_tdls_ftie *) pos;
513 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
516 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
517 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
518 ret = omac1_aes_128(kck, buf, pos - buf, mic);
520 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
525 static int wpa_supplicant_verify_tdls_mic(u8 trans_seq,
526 struct wpa_tdls_peer *peer,
527 const u8 *lnkid, const u8 *timeoutie,
528 const struct wpa_tdls_ftie *ftie)
533 wpa_tdls_ftie_mic(peer->tpk.kck, trans_seq, lnkid,
534 peer->rsnie_p, timeoutie, (u8 *) ftie,
536 if (os_memcmp(mic, ftie->mic, 16) != 0) {
537 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in FTIE - "
539 wpa_hexdump(MSG_DEBUG, "TDLS: Received MIC",
541 wpa_hexdump(MSG_DEBUG, "TDLS: Calculated MIC",
546 wpa_printf(MSG_WARNING, "TDLS: Could not verify TDLS MIC, "
547 "TPK not set - dropping packet");
554 static int wpa_supplicant_verify_tdls_mic_teardown(
555 u8 trans_seq, u16 rcode, u8 dtoken, struct wpa_tdls_peer *peer,
556 const u8 *lnkid, const struct wpa_tdls_ftie *ftie)
561 wpa_tdls_key_mic_teardown(peer->tpk.kck, trans_seq, rcode,
562 dtoken, lnkid, (u8 *) ftie, mic);
563 if (os_memcmp(mic, ftie->mic, 16) != 0) {
564 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in Teardown - "
569 wpa_printf(MSG_INFO, "TDLS: Could not verify TDLS Teardown "
570 "MIC, TPK not set - dropping packet");
577 static void wpa_tdls_tpk_timeout(void *eloop_ctx, void *timeout_ctx)
579 struct wpa_sm *sm = eloop_ctx;
580 struct wpa_tdls_peer *peer = timeout_ctx;
583 * On TPK lifetime expiration, we have an option of either tearing down
584 * the direct link or trying to re-initiate it. The selection of what
585 * to do is not strictly speaking controlled by our role in the expired
586 * link, but for now, use that to select whether to renew or tear down
590 if (peer->initiator) {
591 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
592 " - try to renew", MAC2STR(peer->addr));
593 wpa_tdls_start(sm, peer->addr);
595 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
596 " - tear down", MAC2STR(peer->addr));
597 wpa_sm_tdls_oper(sm, TDLS_TEARDOWN, peer->addr);
602 static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
604 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
606 /* need to clear Peerkey SM */
607 tdls_clear_peer(sm, peer);
612 int wpa_tdls_recv_teardown_notify(struct wpa_sm *sm, const u8 *addr,
615 struct wpa_tdls_peer *peer;
616 struct wpa_tdls_ftie *ftie;
617 struct wpa_tdls_lnkid *lnkid;
622 /* Find the node and free from the list */
623 for (peer = sm->tdls; peer; peer = peer->next) {
624 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
629 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
630 "Teardown " MACSTR, MAC2STR(addr));
634 dialog_token = peer->dtoken;
636 lnkid = (struct wpa_tdls_lnkid *) &peer->lnkid;
638 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown for " MACSTR,
642 if (wpa_tdls_get_privacy(sm) && peer->tpk_set && peer->tpk_success) {
643 /* To add FTIE for Teardown request and compute MIC */
644 ielen += sizeof(*ftie);
645 #ifdef CONFIG_TDLS_TESTING
646 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
648 #endif /* CONFIG_TDLS_TESTING */
651 rbuf = os_zalloc(ielen + 1);
656 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
659 ftie = (struct wpa_tdls_ftie *) pos;
660 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
661 /* Using the recent nonce which should be for CONFIRM frame */
662 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
663 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
664 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
665 pos = (u8 *) (ftie + 1);
666 #ifdef CONFIG_TDLS_TESTING
667 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
668 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
671 *pos++ = 255; /* FTIE subelem */
672 *pos++ = 168; /* FTIE subelem length */
674 #endif /* CONFIG_TDLS_TESTING */
675 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TDLS Teardown handshake",
676 (u8 *) ftie, sizeof(*ftie));
678 /* compute MIC before sending */
679 wpa_tdls_key_mic_teardown(peer->tpk.kck, 4, reason_code,
680 dialog_token, (u8 *) lnkid, (u8 *) ftie,
684 /* TODO: register for a Timeout handler, if Teardown is not received at
685 * the other end, then try again another time */
687 /* request driver to send Teardown using this FTIE */
688 wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_TEARDOWN, 0,
689 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED, rbuf,
693 /* clear the Peerkey statemachine */
694 wpa_tdls_peer_free(sm, peer);
700 static int wpa_tdls_recv_teardown(struct wpa_sm *sm, const u8 *src_addr,
701 const u8 *buf, size_t len)
703 struct wpa_tdls_peer *peer = NULL;
704 struct wpa_tdls_ftie *ftie;
705 struct wpa_tdls_lnkid *lnkid;
706 struct wpa_eapol_ie_parse kde;
711 /* Find the node and free from the list */
712 for (peer = sm->tdls; peer; peer = peer->next) {
713 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
718 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
719 "Teardown " MACSTR, MAC2STR(src_addr));
724 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
726 reason_code = WPA_GET_LE16(pos);
729 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown Request from " MACSTR
730 " (reason code %u)", MAC2STR(src_addr), reason_code);
732 ielen = len - (pos - buf); /* start of IE in buf */
733 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
734 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in Teardown");
738 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
739 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TDLS "
743 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
745 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
748 if (kde.ftie == NULL) {
749 wpa_printf(MSG_INFO, "TDLS: No FTIE in TDLS Teardown");
753 ftie = (struct wpa_tdls_ftie *) kde.ftie;
755 /* Process MIC check to see if TDLS Teardown is right */
756 if (wpa_supplicant_verify_tdls_mic_teardown(4, reason_code,
758 (u8 *) lnkid, ftie) < 0) {
759 wpa_printf(MSG_DEBUG, "TDLS: MIC failure for TDLS "
760 "Teardown Request from " MACSTR, MAC2STR(src_addr));
764 /* TODO: figure out whether this workaround could be disabled
766 wpa_printf(MSG_DEBUG, "TDLS: Workaround - ignore Teardown MIC "
773 * Request the driver to disable the direct link and clear associated
776 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, src_addr);
778 /* clear the Peerkey statemachine */
779 wpa_tdls_peer_free(sm, peer);
786 * wpa_tdls_send_error - To send suitable TDLS status response with
787 * appropriate status code mentioning reason for error/failure.
788 * @dst - MAC addr of Peer station
789 * @tdls_action - TDLS frame type for which error code is sent
790 * @status - status code mentioning reason
793 static int wpa_tdls_send_error(struct wpa_sm *sm, const u8 *dst,
794 u8 tdls_action, u8 dialog_token, u16 status)
796 return wpa_tdls_tpk_send(sm, dst, tdls_action, dialog_token, status,
801 static int wpa_tdls_send_tpk_m1(struct wpa_sm *sm,
802 struct wpa_tdls_peer *peer)
805 struct wpa_tdls_timeoutie timeoutie;
807 struct wpa_tdls_ftie *ftie;
808 u8 *rbuf, *pos, *count_pos;
810 struct rsn_ie_hdr *hdr;
812 if (!wpa_tdls_get_privacy(sm)) {
813 wpa_printf(MSG_DEBUG, "TDLS: No security used on the link");
814 peer->rsnie_i_len = 0;
819 * TPK Handshake Message 1:
820 * FTIE: ANonce=0, SNonce=initiator nonce MIC=0, DataKDs=(RSNIE_I,
821 * Timeout Interval IE))
825 hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
826 hdr->elem_id = WLAN_EID_RSN;
827 WPA_PUT_LE16(hdr->version, RSN_VERSION);
829 pos = (u8 *) (hdr + 1);
830 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
831 pos += RSN_SELECTOR_LEN;
838 * AES-CCMP is the default Encryption preferred for TDLS, so
839 * RSN IE is filled only with CCMP CIPHER
840 * Note: TKIP is not used to encrypt TDLS link.
842 * Regardless of the cipher used on the AP connection, select CCMP
845 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
846 pos += RSN_SELECTOR_LEN;
849 WPA_PUT_LE16(count_pos, count);
851 WPA_PUT_LE16(pos, 1);
853 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
854 pos += RSN_SELECTOR_LEN;
856 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
857 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
858 #ifdef CONFIG_TDLS_TESTING
859 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
860 wpa_printf(MSG_DEBUG, "TDLS: Use alternative RSN IE for "
862 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
864 #endif /* CONFIG_TDLS_TESTING */
865 WPA_PUT_LE16(pos, rsn_capab);
867 #ifdef CONFIG_TDLS_TESTING
868 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
869 /* Number of PMKIDs */
873 #endif /* CONFIG_TDLS_TESTING */
875 hdr->len = (pos - peer->rsnie_i) - 2;
876 peer->rsnie_i_len = pos - peer->rsnie_i;
877 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
878 peer->rsnie_i, peer->rsnie_i_len);
882 if (wpa_tdls_get_privacy(sm))
883 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
884 sizeof(struct wpa_tdls_timeoutie);
885 #ifdef CONFIG_TDLS_TESTING
886 if (wpa_tdls_get_privacy(sm) &&
887 (tdls_testing & TDLS_TESTING_LONG_FRAME))
889 if (tdls_testing & TDLS_TESTING_DIFF_BSSID)
890 buf_len += sizeof(struct wpa_tdls_lnkid);
891 #endif /* CONFIG_TDLS_TESTING */
892 rbuf = os_zalloc(buf_len + 1);
894 wpa_tdls_peer_free(sm, peer);
899 if (!wpa_tdls_get_privacy(sm))
902 /* Initiator RSN IE */
903 pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
905 ftie = (struct wpa_tdls_ftie *) pos;
906 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
907 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
909 if (os_get_random(peer->inonce, WPA_NONCE_LEN)) {
910 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
911 "TDLS: Failed to get random data for initiator Nonce");
913 wpa_tdls_peer_free(sm, peer);
916 wpa_hexdump(MSG_DEBUG, "TDLS: Initiator Nonce for TPK handshake",
917 peer->inonce, WPA_NONCE_LEN);
918 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
920 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK Handshake M1",
921 (u8 *) ftie, sizeof(struct wpa_tdls_ftie));
923 pos = (u8 *) (ftie + 1);
925 #ifdef CONFIG_TDLS_TESTING
926 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
927 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
930 *pos++ = 255; /* FTIE subelem */
931 *pos++ = 168; /* FTIE subelem length */
934 #endif /* CONFIG_TDLS_TESTING */
937 peer->lifetime = TPK_LIFETIME;
938 #ifdef CONFIG_TDLS_TESTING
939 if (tdls_testing & TDLS_TESTING_SHORT_LIFETIME) {
940 wpa_printf(MSG_DEBUG, "TDLS: Testing - use short TPK "
942 peer->lifetime = 301;
944 #endif /* CONFIG_TDLS_TESTING */
945 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
946 sizeof(timeoutie), peer->lifetime);
947 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
951 #ifdef CONFIG_TDLS_TESTING
952 if (tdls_testing & TDLS_TESTING_DIFF_BSSID) {
953 wpa_printf(MSG_DEBUG, "TDLS: Testing - use incorrect BSSID in "
955 struct wpa_tdls_lnkid *l = (struct wpa_tdls_lnkid *) pos;
956 l->ie_type = WLAN_EID_LINK_ID;
957 l->ie_len = 3 * ETH_ALEN;
958 os_memcpy(l->bssid, sm->bssid, ETH_ALEN);
960 os_memcpy(l->init_sta, sm->own_addr, ETH_ALEN);
961 os_memcpy(l->resp_sta, addr, ETH_ALEN);
964 #endif /* CONFIG_TDLS_TESTING */
966 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Request / TPK "
967 "Handshake Message 1 (peer " MACSTR ")",
968 MAC2STR(peer->addr));
970 wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_SETUP_REQUEST, 0, 0,
978 static int wpa_tdls_send_tpk_m2(struct wpa_sm *sm,
979 const unsigned char *src_addr, u8 dtoken,
980 struct wpa_tdls_lnkid *lnkid,
981 const struct wpa_tdls_peer *peer)
986 struct wpa_tdls_timeoutie timeoutie;
987 struct wpa_tdls_ftie *ftie;
990 if (wpa_tdls_get_privacy(sm)) {
991 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
993 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
994 sizeof(struct wpa_tdls_timeoutie);
995 #ifdef CONFIG_TDLS_TESTING
996 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
998 #endif /* CONFIG_TDLS_TESTING */
1001 rbuf = os_zalloc(buf_len + 1);
1006 if (!wpa_tdls_get_privacy(sm))
1010 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1012 ftie = (struct wpa_tdls_ftie *) pos;
1013 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1014 /* TODO: ftie->mic_control to set 2-RESPONSE */
1015 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1016 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1017 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1018 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK M2",
1019 (u8 *) ftie, sizeof(*ftie));
1021 pos = (u8 *) (ftie + 1);
1023 #ifdef CONFIG_TDLS_TESTING
1024 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1025 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1027 ftie->ie_len += 170;
1028 *pos++ = 255; /* FTIE subelem */
1029 *pos++ = 168; /* FTIE subelem length */
1032 #endif /* CONFIG_TDLS_TESTING */
1035 lifetime = peer->lifetime;
1036 #ifdef CONFIG_TDLS_TESTING
1037 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_RESP) {
1038 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1039 "lifetime in response");
1042 #endif /* CONFIG_TDLS_TESTING */
1043 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1044 sizeof(timeoutie), lifetime);
1045 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds from initiator",
1048 /* compute MIC before sending */
1049 wpa_tdls_ftie_mic(peer->tpk.kck, 2, (u8 *) lnkid, peer->rsnie_p,
1050 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
1053 wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE, dtoken, 0,
1061 static int wpa_tdls_send_tpk_m3(struct wpa_sm *sm,
1062 const unsigned char *src_addr, u8 dtoken,
1063 struct wpa_tdls_lnkid *lnkid,
1064 const struct wpa_tdls_peer *peer)
1068 struct wpa_tdls_ftie *ftie;
1069 struct wpa_tdls_timeoutie timeoutie;
1073 if (wpa_tdls_get_privacy(sm)) {
1074 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1076 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1077 sizeof(struct wpa_tdls_timeoutie);
1078 #ifdef CONFIG_TDLS_TESTING
1079 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1081 #endif /* CONFIG_TDLS_TESTING */
1084 rbuf = os_zalloc(buf_len + 1);
1089 if (!wpa_tdls_get_privacy(sm))
1093 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1095 ftie = (struct wpa_tdls_ftie *) pos;
1096 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1097 /*TODO: ftie->mic_control to set 3-CONFIRM */
1098 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1099 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1100 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1102 pos = (u8 *) (ftie + 1);
1104 #ifdef CONFIG_TDLS_TESTING
1105 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1106 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1108 ftie->ie_len += 170;
1109 *pos++ = 255; /* FTIE subelem */
1110 *pos++ = 168; /* FTIE subelem length */
1113 #endif /* CONFIG_TDLS_TESTING */
1116 lifetime = peer->lifetime;
1117 #ifdef CONFIG_TDLS_TESTING
1118 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_CONF) {
1119 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1120 "lifetime in confirm");
1123 #endif /* CONFIG_TDLS_TESTING */
1124 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1125 sizeof(timeoutie), lifetime);
1126 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds",
1129 /* compute MIC before sending */
1130 wpa_tdls_ftie_mic(peer->tpk.kck, 3, (u8 *) lnkid, peer->rsnie_p,
1131 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
1134 wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM, dtoken, 0,
1142 static int wpa_tdls_process_tpk_m1(struct wpa_sm *sm, const u8 *src_addr,
1143 const u8 *buf, size_t len)
1145 struct wpa_tdls_peer *peer;
1146 struct wpa_eapol_ie_parse kde;
1147 struct wpa_ie_data ie;
1150 struct wpa_tdls_ftie *ftie = NULL;
1151 struct wpa_tdls_timeoutie *timeoutie;
1152 struct wpa_tdls_lnkid *lnkid;
1155 struct rsn_ie_hdr *hdr;
1164 cpos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1166 /* driver had already verified the frame format */
1167 dtoken = *cpos++; /* dialog token */
1169 wpa_printf(MSG_INFO, "TDLS: Dialog Token in TPK M1 %d", dtoken);
1171 cpos += 2; /* capability information */
1173 ielen = len - (cpos - buf); /* start of IE in buf */
1174 if (wpa_supplicant_parse_ies(cpos, ielen, &kde) < 0) {
1175 wpa_printf(MSG_INFO, "TDLS: Failed to parse KDEs in TPK M1");
1179 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
1180 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TPK M1");
1183 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
1184 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1185 wpa_printf(MSG_INFO, "TDLS: TPK M1 from diff BSS");
1186 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE,
1187 dtoken, WLAN_STATUS_NOT_IN_SAME_BSS);
1191 wpa_printf(MSG_DEBUG, "TDLS: TPK M1 - TPK initiator " MACSTR,
1194 if (!wpa_tdls_get_privacy(sm))
1197 if (kde.ftie == NULL) {
1198 wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M1");
1201 ftie = (struct wpa_tdls_ftie *) kde.ftie;
1202 if (kde.rsn_ie == NULL) {
1203 wpa_printf(MSG_INFO, "TDLS: No RSN IE in TPK M1");
1207 if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
1208 wpa_printf(MSG_INFO, "TDLS: Too long Initiator RSN IE in "
1213 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
1214 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M1");
1218 cipher = ie.pairwise_cipher;
1219 if (cipher & WPA_CIPHER_CCMP) {
1220 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
1221 cipher = WPA_CIPHER_CCMP;
1223 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M1");
1224 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE,
1226 WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID);
1231 /* Find existing entry and if found, use that instead of adding
1232 * a new one; how to handle the case where both ends initiate at the
1234 for (peer = sm->tdls; peer; peer = peer->next) {
1235 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
1240 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
1241 "peer, creating one for " MACSTR,
1243 peer = os_malloc(sizeof(*peer));
1246 os_memset(peer, 0, sizeof(*peer));
1247 os_memcpy(peer->addr, src_addr, ETH_ALEN);
1248 peer->next = sm->tdls;
1252 * An entry is already present, so check if a TPK_M1 Request
1254 * If so compare MAC address and let
1255 * - greater MAC continue to be initiator
1256 * - other MAC be Peer and process the Req.
1258 if (peer->initiator) {
1259 if (os_memcmp(sm->own_addr, src_addr, ETH_ALEN) > 0) {
1260 wpa_printf(MSG_DEBUG, "TDLS: Dropping Request "
1261 "from peer with smaller address "
1262 MACSTR, MAC2STR(src_addr));
1266 * If smaller node then accept the packet,
1267 * clear values and get ready to process this
1270 wpa_printf(MSG_DEBUG, "TDLS: Accepting "
1271 "Request from peer " MACSTR,
1273 /* clear sm info and preserve the list */
1274 wpa_tdls_peer_free(sm, peer);
1279 peer->initiator = 0; /* Need to check */
1280 peer->dtoken = dtoken;
1281 os_memcpy(peer->lnkid, (u8 *) lnkid, sizeof(struct wpa_tdls_lnkid));
1283 if (!wpa_tdls_get_privacy(sm)) {
1284 peer->rsnie_i_len = 0;
1285 peer->rsnie_p_len = 0;
1286 peer->cipher = WPA_CIPHER_NONE;
1287 goto skip_rsn_check;
1290 os_memcpy(peer->inonce, ftie->Snonce, WPA_NONCE_LEN);
1291 os_memcpy(peer->rsnie_i, kde.rsn_ie, kde.rsn_ie_len);
1292 peer->rsnie_i_len = kde.rsn_ie_len;
1293 peer->cipher = cipher;
1295 if (os_get_random(peer->rnonce, WPA_NONCE_LEN)) {
1296 wpa_msg(sm->ctx->ctx, MSG_WARNING,
1297 "TDLS: Failed to get random data for responder nonce");
1298 wpa_tdls_peer_free(sm, peer);
1303 /* get version info from RSNIE received from Peer */
1304 hdr = (struct rsn_ie_hdr *) kde.rsn_ie;
1305 rsn_ver = WPA_GET_LE16(hdr->version);
1307 /* use min(peer's version, out version) */
1308 if (rsn_ver > RSN_VERSION)
1309 rsn_ver = RSN_VERSION;
1311 hdr = (struct rsn_ie_hdr *) peer->rsnie_p;
1313 hdr->elem_id = WLAN_EID_RSN;
1314 WPA_PUT_LE16(hdr->version, rsn_ver);
1315 pos = (u8 *) (hdr + 1);
1317 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1318 pos += RSN_SELECTOR_LEN;
1319 /* Include only the selected cipher in pairwise cipher suite */
1320 WPA_PUT_LE16(pos, 1);
1322 if (cipher == WPA_CIPHER_CCMP)
1323 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1324 pos += RSN_SELECTOR_LEN;
1326 WPA_PUT_LE16(pos, 1);
1328 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1329 pos += RSN_SELECTOR_LEN;
1331 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1332 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1333 WPA_PUT_LE16(pos, rsn_capab);
1336 hdr->len = (pos - peer->rsnie_p) - 2;
1337 peer->rsnie_p_len = pos - peer->rsnie_p;
1340 /* temp fix: validation of RSNIE later */
1341 os_memcpy(peer->rsnie_p, peer->rsnie_i, peer->rsnie_i_len);
1342 peer->rsnie_p_len = peer->rsnie_i_len;
1344 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for PTK handshake",
1345 peer->rsnie_p, peer->rsnie_p_len);
1348 if (kde.key_lifetime == NULL) {
1349 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M1");
1352 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
1353 lifetime = WPA_GET_LE32(timeoutie->value);
1354 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", lifetime);
1356 lifetime = 300; /* minimum seconds */
1357 peer->lifetime = lifetime;
1359 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
1362 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Response / TPK M2");
1363 wpa_tdls_send_tpk_m2(sm, src_addr, dtoken, lnkid, peer);
1369 static void wpa_tdls_enable_link(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
1371 peer->tpk_success = 1;
1372 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
1373 if (wpa_tdls_get_privacy(sm)) {
1374 eloop_register_timeout(peer->lifetime, 0, wpa_tdls_tpk_timeout,
1377 wpa_sm_tdls_oper(sm, TDLS_ENABLE_LINK, peer->addr);
1381 static int wpa_tdls_process_tpk_m2(struct wpa_sm *sm, const u8 *src_addr,
1382 const u8 *buf, size_t len)
1384 struct wpa_tdls_peer *peer;
1385 struct wpa_eapol_ie_parse kde;
1386 struct wpa_ie_data ie;
1388 struct wpa_tdls_ftie *ftie;
1389 struct wpa_tdls_timeoutie *timeoutie;
1390 struct wpa_tdls_lnkid *lnkid;
1397 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Response / TPK M2 "
1398 "(Peer " MACSTR ")", MAC2STR(src_addr));
1399 for (peer = sm->tdls; peer; peer = peer->next) {
1400 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
1404 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
1405 "TPK M2: " MACSTR, MAC2STR(src_addr));
1408 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_REQUEST);
1410 if (len < 3 + 2 + 1)
1413 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1414 status = WPA_GET_LE16(pos);
1415 pos += 2 /* status code */;
1418 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M2: %u",
1423 /* TODO: need to verify dialog token matches here or in kernel */
1424 dtoken = *pos++; /* dialog token */
1426 wpa_printf(MSG_DEBUG, "TDLS: Dialog Token in TPK M2 %d", dtoken);
1428 if (len < 3 + 2 + 1 + 2)
1430 pos += 2; /* capability information */
1432 ielen = len - (pos - buf); /* start of IE in buf */
1433 if (wpa_supplicant_parse_ies(pos, ielen, &kde) < 0) {
1434 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in TPK M2");
1438 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
1439 wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
1443 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M2",
1444 kde.lnkid, kde.lnkid_len);
1445 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
1447 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1448 wpa_printf(MSG_INFO, "TDLS: TPK M2 from different BSS");
1449 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
1450 dtoken, WLAN_STATUS_NOT_IN_SAME_BSS);
1454 if (!wpa_tdls_get_privacy(sm)) {
1455 peer->rsnie_p_len = 0;
1456 peer->cipher = WPA_CIPHER_NONE;
1460 if (kde.rsn_ie == NULL) {
1461 wpa_printf(MSG_INFO, "TDLS: No RSN IE in TPK M2");
1464 wpa_hexdump(MSG_DEBUG, "TDLS: RSNIE Received from TPK M2",
1465 kde.rsn_ie, kde.rsn_ie_len);
1467 if (kde.rsn_ie_len != peer->rsnie_i_len ||
1468 os_memcmp(peer->rsnie_i, kde.rsn_ie, peer->rsnie_i_len) != 0) {
1469 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M2 does "
1470 "not match with RSN IE used in TPK M1");
1471 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Sent in TPK M1",
1472 peer->rsnie_i, peer->rsnie_i_len);
1473 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
1474 kde.rsn_ie, kde.rsn_ie_len);
1475 wpa_tdls_send_error(sm, src_addr,
1476 WLAN_TDLS_SETUP_CONFIRM, dtoken,
1477 WLAN_STATUS_UNSUPPORTED_RSN_IE_VERSION);
1482 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
1483 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M2");
1487 cipher = ie.pairwise_cipher;
1488 if (cipher & WPA_CIPHER_CCMP) {
1489 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
1490 cipher = WPA_CIPHER_CCMP;
1492 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M2");
1493 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
1495 WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID);
1499 if (kde.ftie == NULL) {
1500 wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M2");
1503 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M2",
1504 kde.ftie, sizeof(*ftie));
1505 ftie = (struct wpa_tdls_ftie *) kde.ftie;
1507 if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
1508 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M2 does "
1509 "not match with FTIE SNonce used in TPK M1");
1513 /* Responder Nonce and RSN IE */
1514 os_memcpy(peer->lnkid, (u8 *) lnkid, sizeof(struct wpa_tdls_lnkid));
1515 os_memcpy(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN);
1516 os_memcpy(peer->rsnie_p, kde.rsn_ie, kde.rsn_ie_len);
1517 peer->rsnie_p_len = kde.rsn_ie_len;
1518 peer->cipher = cipher;
1521 if (kde.key_lifetime == NULL) {
1522 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M2");
1525 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
1526 lifetime = WPA_GET_LE32(timeoutie->value);
1527 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M2",
1529 if (lifetime != peer->lifetime) {
1530 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
1531 "TPK M2 (expected %u)", lifetime, peer->lifetime);
1532 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
1533 dtoken, WLAN_STATUS_UNACCEPTABLE_LIFETIME);
1537 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
1539 /* Process MIC check to see if TPK M2 is right */
1540 if (wpa_supplicant_verify_tdls_mic(2, peer, (u8 *) lnkid,
1541 (u8 *) timeoutie, ftie) < 0) {
1542 wpa_tdls_del_key(sm, peer);
1543 wpa_tdls_peer_free(sm, peer);
1547 wpa_tdls_set_key(sm, peer);
1550 peer->dtoken = dtoken;
1552 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Confirm / "
1553 "TPK Handshake Message 3");
1554 wpa_tdls_send_tpk_m3(sm, src_addr, dtoken, lnkid, peer);
1556 wpa_tdls_enable_link(sm, peer);
1562 static int wpa_tdls_process_tpk_m3(struct wpa_sm *sm, const u8 *src_addr,
1563 const u8 *buf, size_t len)
1565 struct wpa_tdls_peer *peer;
1566 struct wpa_eapol_ie_parse kde;
1567 struct wpa_tdls_ftie *ftie;
1568 struct wpa_tdls_timeoutie *timeoutie;
1569 struct wpa_tdls_lnkid *lnkid;
1575 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Confirm / TPK M3 "
1576 "(Peer " MACSTR ")", MAC2STR(src_addr));
1577 for (peer = sm->tdls; peer; peer = peer->next) {
1578 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
1582 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
1583 "TPK M3: " MACSTR, MAC2STR(src_addr));
1586 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_RESPONSE);
1589 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1591 status = WPA_GET_LE16(pos);
1594 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M3: %u",
1598 pos += 2 /* status code */ + 1 /* dialog token */;
1600 ielen = len - (pos - buf); /* start of IE in buf */
1601 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
1602 wpa_printf(MSG_INFO, "TDLS: Failed to parse KDEs in TPK M3");
1606 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
1607 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TPK M3");
1610 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M3",
1611 (u8 *) kde.lnkid, kde.lnkid_len);
1612 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
1614 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1615 wpa_printf(MSG_INFO, "TDLS: TPK M3 from diff BSS");
1619 if (!wpa_tdls_get_privacy(sm))
1622 if (kde.ftie == NULL) {
1623 wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M3");
1626 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M3",
1627 (u8 *) ftie, sizeof(*ftie));
1628 ftie = (struct wpa_tdls_ftie *) kde.ftie;
1630 if (kde.rsn_ie == NULL) {
1631 wpa_printf(MSG_INFO, "TDLS: No RSN IE KDE in TPK M3");
1634 wpa_hexdump(MSG_DEBUG, "TDLS: RSNIE Received from TPK M3",
1635 kde.rsn_ie, kde.rsn_ie_len);
1637 if (!os_memcmp(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN) == 0) {
1638 wpa_printf(MSG_INFO, "TDLS: FTIE ANonce in TPK M3 does "
1639 "not match with FTIE ANonce used in TPK M2");
1643 if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
1644 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M3 does not "
1645 "match with FTIE SNonce used in TPK M1");
1649 if (kde.key_lifetime == NULL) {
1650 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M3");
1653 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
1654 wpa_hexdump(MSG_DEBUG, "TDLS: Timeout IE Received from TPK M3",
1655 (u8 *) timeoutie, sizeof(*timeoutie));
1656 lifetime = WPA_GET_LE32(timeoutie->value);
1657 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M3",
1659 if (lifetime != peer->lifetime) {
1660 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
1661 "TPK M3 (expected %u)", lifetime, peer->lifetime);
1665 if (wpa_supplicant_verify_tdls_mic(3, peer, (u8 *) lnkid,
1666 (u8 *) timeoutie, ftie) < 0) {
1667 wpa_tdls_del_key(sm, peer);
1668 wpa_tdls_peer_free(sm, peer);
1672 if (wpa_tdls_set_key(sm, peer) < 0)
1676 wpa_tdls_enable_link(sm, peer);
1682 static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs)
1684 struct wpa_tdls_timeoutie *lifetime = (struct wpa_tdls_timeoutie *) ie;
1686 os_memset(lifetime, 0, ie_len);
1687 lifetime->ie_type = WLAN_EID_TIMEOUT_INTERVAL;
1688 lifetime->ie_len = sizeof(struct wpa_tdls_timeoutie) - 2;
1689 lifetime->interval_type = WLAN_TIMEOUT_KEY_LIFETIME;
1690 WPA_PUT_LE32(lifetime->value, tsecs);
1691 os_memcpy(pos, ie, ie_len);
1692 return pos + ie_len;
1697 * wpa_tdls_start - Initiate TDLS handshake (send TPK Handshake Message 1)
1698 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1699 * @peer: MAC address of the peer STA
1700 * Returns: 0 on success, or -1 on failure
1702 * Send TPK Handshake Message 1 info to driver to start TDLS
1703 * handshake with the peer.
1705 int wpa_tdls_start(struct wpa_sm *sm, const u8 *addr)
1707 struct wpa_tdls_peer *peer;
1709 /* Find existing entry and if found, use that instead of adding
1711 for (peer = sm->tdls; peer; peer = peer->next) {
1712 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
1717 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
1718 "peer, creating one for " MACSTR, MAC2STR(addr));
1719 peer = os_malloc(sizeof(*peer));
1722 os_memset(peer, 0, sizeof(*peer));
1723 os_memcpy(peer->addr, addr, ETH_ALEN);
1724 peer->next = sm->tdls;
1728 peer->initiator = 1;
1730 return wpa_tdls_send_tpk_m1(sm, peer);
1735 * wpa_supplicant_rx_tdls - Receive TDLS data frame
1737 * This function is called to receive TDLS (ethertype = 0x890d) data frames.
1739 static void wpa_supplicant_rx_tdls(void *ctx, const u8 *src_addr,
1740 const u8 *buf, size_t len)
1742 struct wpa_sm *sm = ctx;
1743 struct wpa_tdls_frame *tf;
1745 wpa_hexdump(MSG_DEBUG, "TDLS: Received Data frame encapsulation",
1748 if (os_memcmp(src_addr, sm->own_addr, ETH_ALEN) == 0) {
1749 wpa_printf(MSG_DEBUG, "TDLS: Discard copy of own message");
1753 if (len < sizeof(*tf)) {
1754 wpa_printf(MSG_INFO, "TDLS: Drop too short frame");
1758 /* Check to make sure its a valid encapsulated TDLS frame */
1759 tf = (struct wpa_tdls_frame *) buf;
1760 if (tf->payloadtype != 2 /* TDLS_RFTYPE */ ||
1761 tf->category != WLAN_ACTION_TDLS) {
1762 wpa_printf(MSG_INFO, "TDLS: Invalid frame - payloadtype=%u "
1763 "category=%u action=%u",
1764 tf->payloadtype, tf->category, tf->action);
1768 switch (tf->action) {
1769 case WLAN_TDLS_SETUP_REQUEST:
1770 wpa_tdls_process_tpk_m1(sm, src_addr, buf, len);
1772 case WLAN_TDLS_SETUP_RESPONSE:
1773 wpa_tdls_process_tpk_m2(sm, src_addr, buf, len);
1775 case WLAN_TDLS_SETUP_CONFIRM:
1776 wpa_tdls_process_tpk_m3(sm, src_addr, buf, len);
1778 case WLAN_TDLS_TEARDOWN:
1779 wpa_tdls_recv_teardown(sm, src_addr, buf, len);
1782 /* Kernel code will process remaining frames */
1783 wpa_printf(MSG_DEBUG, "TDLS: Ignore TDLS frame action code %u",
1791 * wpa_tdls_init - Initialize driver interface parameters for TDLS
1792 * @wpa_s: Pointer to wpa_supplicant data
1793 * Returns: 0 on success, -1 on failure
1795 * This function is called to initialize driver interface parameters for TDLS.
1796 * wpa_drv_init() must have been called before this function to initialize the
1799 int wpa_tdls_init(struct wpa_sm *sm)
1804 sm->l2_tdls = l2_packet_init(sm->ifname, sm->own_addr,
1805 ETH_P_80211_ENCAP, wpa_supplicant_rx_tdls,
1807 if (sm->l2_tdls == NULL) {
1808 wpa_printf(MSG_ERROR, "TDLS: Failed to open l2_packet "
1817 static void wpa_tdls_remove_peers(struct wpa_sm *sm)
1819 struct wpa_tdls_peer *peer, *tmp;
1827 res = wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
1828 wpa_printf(MSG_DEBUG, "TDLS: Remove peer " MACSTR " (res=%d)",
1829 MAC2STR(peer->addr), res);
1830 wpa_tdls_peer_free(sm, peer);
1838 * wpa_tdls_deinit - Deinitialize driver interface parameters for TDLS
1840 * This function is called to recover driver interface parameters for TDLS
1841 * and frees resources allocated for it.
1843 void wpa_tdls_deinit(struct wpa_sm *sm)
1849 l2_packet_deinit(sm->l2_tdls);
1852 wpa_tdls_remove_peers(sm);
1856 void wpa_tdls_assoc(struct wpa_sm *sm)
1858 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on association");
1859 wpa_tdls_remove_peers(sm);
1863 void wpa_tdls_disassoc(struct wpa_sm *sm)
1865 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on disassociation");
1866 wpa_tdls_remove_peers(sm);