blob: adca12d6cac58ebaf94ad7d797c52b9be4521908 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - TDLS
3 * Copyright (c) 2010-2011, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "utils/os.h"
14#include "common/ieee802_11_defs.h"
15#include "crypto/sha256.h"
16#include "crypto/crypto.h"
17#include "crypto/aes_wrap.h"
18#include "rsn_supp/wpa.h"
19#include "rsn_supp/wpa_ie.h"
20#include "rsn_supp/wpa_i.h"
21#include "drivers/driver.h"
22#include "l2_packet/l2_packet.h"
23
24#ifdef CONFIG_TDLS_TESTING
25#define TDLS_TESTING_LONG_FRAME BIT(0)
26#define TDLS_TESTING_ALT_RSN_IE BIT(1)
27#define TDLS_TESTING_DIFF_BSSID BIT(2)
28#define TDLS_TESTING_SHORT_LIFETIME BIT(3)
29#define TDLS_TESTING_WRONG_LIFETIME_RESP BIT(4)
30#define TDLS_TESTING_WRONG_LIFETIME_CONF BIT(5)
31#define TDLS_TESTING_LONG_LIFETIME BIT(6)
32#define TDLS_TESTING_CONCURRENT_INIT BIT(7)
33#define TDLS_TESTING_NO_TPK_EXPIRATION BIT(8)
34#define TDLS_TESTING_DECLINE_RESP BIT(9)
35#define TDLS_TESTING_IGNORE_AP_PROHIBIT BIT(10)
Dmitry Shmidt21de2142014-04-08 10:50:52 -070036#define TDLS_TESTING_WRONG_MIC BIT(11)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037unsigned int tdls_testing = 0;
38#endif /* CONFIG_TDLS_TESTING */
39
40#define TPK_LIFETIME 43200 /* 12 hours */
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070041#define TPK_M1_RETRY_COUNT 3
42#define TPK_M1_TIMEOUT 5000 /* in milliseconds */
43#define TPK_M2_RETRY_COUNT 10
44#define TPK_M2_TIMEOUT 500 /* in milliseconds */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045
46#define TDLS_MIC_LEN 16
47
48#define TDLS_TIMEOUT_LEN 4
49
50struct wpa_tdls_ftie {
51 u8 ie_type; /* FTIE */
52 u8 ie_len;
53 u8 mic_ctrl[2];
54 u8 mic[TDLS_MIC_LEN];
55 u8 Anonce[WPA_NONCE_LEN]; /* Responder Nonce in TDLS */
56 u8 Snonce[WPA_NONCE_LEN]; /* Initiator Nonce in TDLS */
57 /* followed by optional elements */
58} STRUCT_PACKED;
59
60struct wpa_tdls_timeoutie {
61 u8 ie_type; /* Timeout IE */
62 u8 ie_len;
63 u8 interval_type;
64 u8 value[TDLS_TIMEOUT_LEN];
65} STRUCT_PACKED;
66
67struct wpa_tdls_lnkid {
68 u8 ie_type; /* Link Identifier IE */
69 u8 ie_len;
70 u8 bssid[ETH_ALEN];
71 u8 init_sta[ETH_ALEN];
72 u8 resp_sta[ETH_ALEN];
73} STRUCT_PACKED;
74
75/* TDLS frame headers as per IEEE Std 802.11z-2010 */
76struct wpa_tdls_frame {
77 u8 payloadtype; /* IEEE80211_TDLS_RFTYPE */
78 u8 category; /* Category */
79 u8 action; /* Action (enum tdls_frame_type) */
80} STRUCT_PACKED;
81
82static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs);
83static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx);
84static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer);
Sunil Duttd0ef38b2013-09-30 17:34:13 +030085static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
86 struct wpa_tdls_peer *peer);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070087static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
88 u16 reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089
90
91#define TDLS_MAX_IE_LEN 80
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080092#define IEEE80211_MAX_SUPP_RATES 32
93
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094struct wpa_tdls_peer {
95 struct wpa_tdls_peer *next;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070096 unsigned int reconfig_key:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 int initiator; /* whether this end was initiator for TDLS setup */
98 u8 addr[ETH_ALEN]; /* other end MAC address */
99 u8 inonce[WPA_NONCE_LEN]; /* Initiator Nonce */
100 u8 rnonce[WPA_NONCE_LEN]; /* Responder Nonce */
101 u8 rsnie_i[TDLS_MAX_IE_LEN]; /* Initiator RSN IE */
102 size_t rsnie_i_len;
103 u8 rsnie_p[TDLS_MAX_IE_LEN]; /* Peer RSN IE */
104 size_t rsnie_p_len;
105 u32 lifetime;
106 int cipher; /* Selected cipher (WPA_CIPHER_*) */
107 u8 dtoken;
108
109 struct tpk {
110 u8 kck[16]; /* TPK-KCK */
111 u8 tk[16]; /* TPK-TK; assuming only CCMP will be used */
112 } tpk;
113 int tpk_set;
Jouni Malinen4c527402017-09-22 11:03:15 +0300114 int tk_set; /* TPK-TK configured to the driver */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115 int tpk_success;
Sunil Dutt73b28cc2013-09-30 17:38:41 +0300116 int tpk_in_progress;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117
118 struct tpk_timer {
119 u8 dest[ETH_ALEN];
120 int count; /* Retry Count */
121 int timer; /* Timeout in milliseconds */
122 u8 action_code; /* TDLS frame type */
123 u8 dialog_token;
124 u16 status_code;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700125 u32 peer_capab;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 int buf_len; /* length of TPK message for retransmission */
127 u8 *buf; /* buffer for TPK message */
128 } sm_tmr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800129
130 u16 capability;
131
132 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
133 size_t supp_rates_len;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800134
135 struct ieee80211_ht_capabilities *ht_capabilities;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800136 struct ieee80211_vht_capabilities *vht_capabilities;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800137
138 u8 qos_info;
139
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700140 u16 aid;
141
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800142 u8 *ext_capab;
143 size_t ext_capab_len;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800144
145 u8 *supp_channels;
146 size_t supp_channels_len;
147
148 u8 *supp_oper_classes;
149 size_t supp_oper_classes_len;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700150
151 u8 wmm_capable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152};
153
154
155static int wpa_tdls_get_privacy(struct wpa_sm *sm)
156{
157 /*
158 * Get info needed from supplicant to check if the current BSS supports
159 * security. Other than OPEN mode, rest are considered secured
160 * WEP/WPA/WPA2 hence TDLS frames are processed for TPK handshake.
161 */
162 return sm->pairwise_cipher != WPA_CIPHER_NONE;
163}
164
165
166static u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
167{
168 os_memcpy(pos, ie, ie_len);
169 return pos + ie_len;
170}
171
172
173static int wpa_tdls_del_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
174{
175 if (wpa_sm_set_key(sm, WPA_ALG_NONE, peer->addr,
176 0, 0, NULL, 0, NULL, 0) < 0) {
177 wpa_printf(MSG_WARNING, "TDLS: Failed to delete TPK-TK from "
178 "the driver");
179 return -1;
180 }
181
182 return 0;
183}
184
185
186static int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
187{
188 u8 key_len;
189 u8 rsc[6];
190 enum wpa_alg alg;
191
Jouni Malinen4c527402017-09-22 11:03:15 +0300192 if (peer->tk_set) {
193 /*
194 * This same TPK-TK has already been configured to the driver
195 * and this new configuration attempt (likely due to an
196 * unexpected retransmitted frame) would result in clearing
197 * the TX/RX sequence number which can break security, so must
198 * not allow that to happen.
199 */
200 wpa_printf(MSG_INFO, "TDLS: TPK-TK for the peer " MACSTR
201 " has already been configured to the driver - do not reconfigure",
202 MAC2STR(peer->addr));
203 return -1;
204 }
205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 os_memset(rsc, 0, 6);
207
208 switch (peer->cipher) {
209 case WPA_CIPHER_CCMP:
210 alg = WPA_ALG_CCMP;
211 key_len = 16;
212 break;
213 case WPA_CIPHER_NONE:
214 wpa_printf(MSG_DEBUG, "TDLS: Pairwise Cipher Suite: "
215 "NONE - do not use pairwise keys");
216 return -1;
217 default:
218 wpa_printf(MSG_WARNING, "TDLS: Unsupported pairwise cipher %d",
219 sm->pairwise_cipher);
220 return -1;
221 }
222
Jouni Malinen4c527402017-09-22 11:03:15 +0300223 wpa_printf(MSG_DEBUG, "TDLS: Configure pairwise key for peer " MACSTR,
224 MAC2STR(peer->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 if (wpa_sm_set_key(sm, alg, peer->addr, -1, 1,
226 rsc, sizeof(rsc), peer->tpk.tk, key_len) < 0) {
227 wpa_printf(MSG_WARNING, "TDLS: Failed to set TPK to the "
228 "driver");
229 return -1;
230 }
Jouni Malinen4c527402017-09-22 11:03:15 +0300231 peer->tk_set = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 return 0;
233}
234
235
236static int wpa_tdls_send_tpk_msg(struct wpa_sm *sm, const u8 *dst,
237 u8 action_code, u8 dialog_token,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700238 u16 status_code, u32 peer_capab,
239 const u8 *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240{
241 return wpa_sm_send_tdls_mgmt(sm, dst, action_code, dialog_token,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700242 status_code, peer_capab, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243}
244
245
246static int wpa_tdls_tpk_send(struct wpa_sm *sm, const u8 *dest, u8 action_code,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700247 u8 dialog_token, u16 status_code, u32 peer_capab,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 const u8 *msg, size_t msg_len)
249{
250 struct wpa_tdls_peer *peer;
251
252 wpa_printf(MSG_DEBUG, "TDLS: TPK send dest=" MACSTR " action_code=%u "
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700253 "dialog_token=%u status_code=%u peer_capab=%u msg_len=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 MAC2STR(dest), action_code, dialog_token, status_code,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700255 peer_capab, (unsigned int) msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256
257 if (wpa_tdls_send_tpk_msg(sm, dest, action_code, dialog_token,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700258 status_code, peer_capab, msg, msg_len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259 wpa_printf(MSG_INFO, "TDLS: Failed to send message "
260 "(action_code=%u)", action_code);
261 return -1;
262 }
263
264 if (action_code == WLAN_TDLS_SETUP_CONFIRM ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800265 action_code == WLAN_TDLS_TEARDOWN ||
266 action_code == WLAN_TDLS_DISCOVERY_REQUEST ||
267 action_code == WLAN_TDLS_DISCOVERY_RESPONSE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 return 0; /* No retries */
269
270 for (peer = sm->tdls; peer; peer = peer->next) {
271 if (os_memcmp(peer->addr, dest, ETH_ALEN) == 0)
272 break;
273 }
274
275 if (peer == NULL) {
276 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
277 "retry " MACSTR, MAC2STR(dest));
278 return 0;
279 }
280
281 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
282
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700283 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
284 peer->sm_tmr.count = TPK_M2_RETRY_COUNT;
285 peer->sm_tmr.timer = TPK_M2_TIMEOUT;
286 } else {
287 peer->sm_tmr.count = TPK_M1_RETRY_COUNT;
288 peer->sm_tmr.timer = TPK_M1_TIMEOUT;
289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290
291 /* Copy message to resend on timeout */
292 os_memcpy(peer->sm_tmr.dest, dest, ETH_ALEN);
293 peer->sm_tmr.action_code = action_code;
294 peer->sm_tmr.dialog_token = dialog_token;
295 peer->sm_tmr.status_code = status_code;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700296 peer->sm_tmr.peer_capab = peer_capab;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 peer->sm_tmr.buf_len = msg_len;
298 os_free(peer->sm_tmr.buf);
299 peer->sm_tmr.buf = os_malloc(msg_len);
300 if (peer->sm_tmr.buf == NULL)
301 return -1;
302 os_memcpy(peer->sm_tmr.buf, msg, msg_len);
303
304 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout registered "
305 "(action_code=%u)", action_code);
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700306 eloop_register_timeout(peer->sm_tmr.timer / 1000,
307 (peer->sm_tmr.timer % 1000) * 1000,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 wpa_tdls_tpk_retry_timeout, sm, peer);
309 return 0;
310}
311
312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313static int wpa_tdls_do_teardown(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300314 u16 reason_code)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800315{
316 int ret;
317
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300318 ret = wpa_tdls_send_teardown(sm, peer->addr, reason_code);
319 /* disable the link after teardown was sent */
Sunil Duttd0ef38b2013-09-30 17:34:13 +0300320 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321
322 return ret;
323}
324
325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx)
327{
328
329 struct wpa_sm *sm = eloop_ctx;
330 struct wpa_tdls_peer *peer = timeout_ctx;
331
332 if (peer->sm_tmr.count) {
333 peer->sm_tmr.count--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334
335 wpa_printf(MSG_INFO, "TDLS: Retrying sending of message "
336 "(action_code=%u)",
337 peer->sm_tmr.action_code);
338
339 if (peer->sm_tmr.buf == NULL) {
340 wpa_printf(MSG_INFO, "TDLS: No retry buffer available "
341 "for action_code=%u",
342 peer->sm_tmr.action_code);
343 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm,
344 peer);
345 return;
346 }
347
348 /* resend TPK Handshake Message to Peer */
349 if (wpa_tdls_send_tpk_msg(sm, peer->sm_tmr.dest,
350 peer->sm_tmr.action_code,
351 peer->sm_tmr.dialog_token,
352 peer->sm_tmr.status_code,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700353 peer->sm_tmr.peer_capab,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 peer->sm_tmr.buf,
355 peer->sm_tmr.buf_len)) {
356 wpa_printf(MSG_INFO, "TDLS: Failed to retry "
357 "transmission");
358 }
359
360 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700361 eloop_register_timeout(peer->sm_tmr.timer / 1000,
362 (peer->sm_tmr.timer % 1000) * 1000,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 wpa_tdls_tpk_retry_timeout, sm, peer);
364 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
366
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800367 wpa_printf(MSG_DEBUG, "TDLS: Sending Teardown Request");
368 wpa_tdls_do_teardown(sm, peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300369 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370 }
371}
372
373
374static void wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm *sm,
375 struct wpa_tdls_peer *peer,
376 u8 action_code)
377{
378 if (action_code == peer->sm_tmr.action_code) {
379 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout cancelled for "
380 "action_code=%u", action_code);
381
382 /* Cancel Timeout registered */
383 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
384
385 /* free all resources meant for retry */
386 os_free(peer->sm_tmr.buf);
387 peer->sm_tmr.buf = NULL;
388
389 peer->sm_tmr.count = 0;
390 peer->sm_tmr.timer = 0;
391 peer->sm_tmr.buf_len = 0;
392 peer->sm_tmr.action_code = 0xff;
393 } else {
394 wpa_printf(MSG_INFO, "TDLS: Error in cancelling retry timeout "
395 "(Unknown action_code=%u)", action_code);
396 }
397}
398
399
400static void wpa_tdls_generate_tpk(struct wpa_tdls_peer *peer,
401 const u8 *own_addr, const u8 *bssid)
402{
403 u8 key_input[SHA256_MAC_LEN];
404 const u8 *nonce[2];
405 size_t len[2];
406 u8 data[3 * ETH_ALEN];
407
408 /* IEEE Std 802.11z-2010 8.5.9.1:
409 * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
410 */
411 len[0] = WPA_NONCE_LEN;
412 len[1] = WPA_NONCE_LEN;
413 if (os_memcmp(peer->inonce, peer->rnonce, WPA_NONCE_LEN) < 0) {
414 nonce[0] = peer->inonce;
415 nonce[1] = peer->rnonce;
416 } else {
417 nonce[0] = peer->rnonce;
418 nonce[1] = peer->inonce;
419 }
420 wpa_hexdump(MSG_DEBUG, "TDLS: min(Nonce)", nonce[0], WPA_NONCE_LEN);
421 wpa_hexdump(MSG_DEBUG, "TDLS: max(Nonce)", nonce[1], WPA_NONCE_LEN);
422 sha256_vector(2, nonce, len, key_input);
423 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-Key-Input",
424 key_input, SHA256_MAC_LEN);
425
426 /*
427 * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
428 * min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
429 * TODO: is N_KEY really included in KDF Context and if so, in which
430 * presentation format (little endian 16-bit?) is it used? It gets
431 * added by the KDF anyway..
432 */
433
434 if (os_memcmp(own_addr, peer->addr, ETH_ALEN) < 0) {
435 os_memcpy(data, own_addr, ETH_ALEN);
436 os_memcpy(data + ETH_ALEN, peer->addr, ETH_ALEN);
437 } else {
438 os_memcpy(data, peer->addr, ETH_ALEN);
439 os_memcpy(data + ETH_ALEN, own_addr, ETH_ALEN);
440 }
441 os_memcpy(data + 2 * ETH_ALEN, bssid, ETH_ALEN);
442 wpa_hexdump(MSG_DEBUG, "TDLS: KDF Context", data, sizeof(data));
443
444 sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data),
445 (u8 *) &peer->tpk, sizeof(peer->tpk));
446 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-KCK",
447 peer->tpk.kck, sizeof(peer->tpk.kck));
448 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-TK",
449 peer->tpk.tk, sizeof(peer->tpk.tk));
450 peer->tpk_set = 1;
451}
452
453
454/**
455 * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
456 * @kck: TPK-KCK
457 * @lnkid: Pointer to the beginning of Link Identifier IE
458 * @rsnie: Pointer to the beginning of RSN IE used for handshake
459 * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
460 * @ftie: Pointer to the beginning of FT IE
461 * @mic: Pointer for writing MIC
462 *
463 * Calculate MIC for TDLS frame.
464 */
465static int wpa_tdls_ftie_mic(const u8 *kck, u8 trans_seq, const u8 *lnkid,
466 const u8 *rsnie, const u8 *timeoutie,
467 const u8 *ftie, u8 *mic)
468{
469 u8 *buf, *pos;
470 struct wpa_tdls_ftie *_ftie;
471 const struct wpa_tdls_lnkid *_lnkid;
472 int ret;
473 int len = 2 * ETH_ALEN + 1 + 2 + lnkid[1] + 2 + rsnie[1] +
474 2 + timeoutie[1] + 2 + ftie[1];
475 buf = os_zalloc(len);
476 if (!buf) {
477 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
478 return -1;
479 }
480
481 pos = buf;
482 _lnkid = (const struct wpa_tdls_lnkid *) lnkid;
483 /* 1) TDLS initiator STA MAC address */
484 os_memcpy(pos, _lnkid->init_sta, ETH_ALEN);
485 pos += ETH_ALEN;
486 /* 2) TDLS responder STA MAC address */
487 os_memcpy(pos, _lnkid->resp_sta, ETH_ALEN);
488 pos += ETH_ALEN;
489 /* 3) Transaction Sequence number */
490 *pos++ = trans_seq;
491 /* 4) Link Identifier IE */
492 os_memcpy(pos, lnkid, 2 + lnkid[1]);
493 pos += 2 + lnkid[1];
494 /* 5) RSN IE */
495 os_memcpy(pos, rsnie, 2 + rsnie[1]);
496 pos += 2 + rsnie[1];
497 /* 6) Timeout Interval IE */
498 os_memcpy(pos, timeoutie, 2 + timeoutie[1]);
499 pos += 2 + timeoutie[1];
500 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
501 os_memcpy(pos, ftie, 2 + ftie[1]);
502 _ftie = (struct wpa_tdls_ftie *) pos;
503 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
504 pos += 2 + ftie[1];
505
506 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
507 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
508 ret = omac1_aes_128(kck, buf, pos - buf, mic);
509 os_free(buf);
510 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
511 return ret;
512}
513
514
515/**
516 * wpa_tdls_key_mic_teardown - Calculate TDLS FTIE MIC for Teardown frame
517 * @kck: TPK-KCK
518 * @trans_seq: Transaction Sequence Number (4 - Teardown)
519 * @rcode: Reason code for Teardown
520 * @dtoken: Dialog Token used for that particular link
521 * @lnkid: Pointer to the beginning of Link Identifier IE
522 * @ftie: Pointer to the beginning of FT IE
523 * @mic: Pointer for writing MIC
524 *
525 * Calculate MIC for TDLS frame.
526 */
527static int wpa_tdls_key_mic_teardown(const u8 *kck, u8 trans_seq, u16 rcode,
528 u8 dtoken, const u8 *lnkid,
529 const u8 *ftie, u8 *mic)
530{
531 u8 *buf, *pos;
532 struct wpa_tdls_ftie *_ftie;
533 int ret;
534 int len;
535
536 if (lnkid == NULL)
537 return -1;
538
539 len = 2 + lnkid[1] + sizeof(rcode) + sizeof(dtoken) +
540 sizeof(trans_seq) + 2 + ftie[1];
541
542 buf = os_zalloc(len);
543 if (!buf) {
544 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
545 return -1;
546 }
547
548 pos = buf;
549 /* 1) Link Identifier IE */
550 os_memcpy(pos, lnkid, 2 + lnkid[1]);
551 pos += 2 + lnkid[1];
552 /* 2) Reason Code */
553 WPA_PUT_LE16(pos, rcode);
554 pos += sizeof(rcode);
555 /* 3) Dialog token */
556 *pos++ = dtoken;
557 /* 4) Transaction Sequence number */
558 *pos++ = trans_seq;
559 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
560 os_memcpy(pos, ftie, 2 + ftie[1]);
561 _ftie = (struct wpa_tdls_ftie *) pos;
562 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
563 pos += 2 + ftie[1];
564
565 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
566 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
567 ret = omac1_aes_128(kck, buf, pos - buf, mic);
568 os_free(buf);
569 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
570 return ret;
571}
572
573
574static int wpa_supplicant_verify_tdls_mic(u8 trans_seq,
575 struct wpa_tdls_peer *peer,
576 const u8 *lnkid, const u8 *timeoutie,
577 const struct wpa_tdls_ftie *ftie)
578{
579 u8 mic[16];
580
581 if (peer->tpk_set) {
582 wpa_tdls_ftie_mic(peer->tpk.kck, trans_seq, lnkid,
583 peer->rsnie_p, timeoutie, (u8 *) ftie,
584 mic);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700585 if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in FTIE - "
587 "dropping packet");
588 wpa_hexdump(MSG_DEBUG, "TDLS: Received MIC",
589 ftie->mic, 16);
590 wpa_hexdump(MSG_DEBUG, "TDLS: Calculated MIC",
591 mic, 16);
592 return -1;
593 }
594 } else {
595 wpa_printf(MSG_WARNING, "TDLS: Could not verify TDLS MIC, "
596 "TPK not set - dropping packet");
597 return -1;
598 }
599 return 0;
600}
601
602
603static int wpa_supplicant_verify_tdls_mic_teardown(
604 u8 trans_seq, u16 rcode, u8 dtoken, struct wpa_tdls_peer *peer,
605 const u8 *lnkid, const struct wpa_tdls_ftie *ftie)
606{
607 u8 mic[16];
608
609 if (peer->tpk_set) {
610 wpa_tdls_key_mic_teardown(peer->tpk.kck, trans_seq, rcode,
611 dtoken, lnkid, (u8 *) ftie, mic);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700612 if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in Teardown - "
614 "dropping packet");
615 return -1;
616 }
617 } else {
618 wpa_printf(MSG_INFO, "TDLS: Could not verify TDLS Teardown "
619 "MIC, TPK not set - dropping packet");
620 return -1;
621 }
622 return 0;
623}
624
625
626static void wpa_tdls_tpk_timeout(void *eloop_ctx, void *timeout_ctx)
627{
628 struct wpa_sm *sm = eloop_ctx;
629 struct wpa_tdls_peer *peer = timeout_ctx;
630
631 /*
632 * On TPK lifetime expiration, we have an option of either tearing down
633 * the direct link or trying to re-initiate it. The selection of what
634 * to do is not strictly speaking controlled by our role in the expired
635 * link, but for now, use that to select whether to renew or tear down
636 * the link.
637 */
638
639 if (peer->initiator) {
640 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
641 " - try to renew", MAC2STR(peer->addr));
642 wpa_tdls_start(sm, peer->addr);
643 } else {
644 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
645 " - tear down", MAC2STR(peer->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800646 wpa_tdls_do_teardown(sm, peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300647 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648 }
649}
650
651
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700652static void wpa_tdls_peer_remove_from_list(struct wpa_sm *sm,
653 struct wpa_tdls_peer *peer)
654{
655 struct wpa_tdls_peer *cur, *prev;
656
657 cur = sm->tdls;
658 prev = NULL;
659 while (cur && cur != peer) {
660 prev = cur;
661 cur = cur->next;
662 }
663
664 if (cur != peer) {
665 wpa_printf(MSG_ERROR, "TDLS: Could not find peer " MACSTR
666 " to remove it from the list",
667 MAC2STR(peer->addr));
668 return;
669 }
670
671 if (prev)
672 prev->next = peer->next;
673 else
674 sm->tdls = peer->next;
675}
676
677
678static void wpa_tdls_peer_clear(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679{
680 wpa_printf(MSG_DEBUG, "TDLS: Clear state for peer " MACSTR,
681 MAC2STR(peer->addr));
682 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
683 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700684 peer->reconfig_key = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 peer->initiator = 0;
Sunil Dutt73b28cc2013-09-30 17:38:41 +0300686 peer->tpk_in_progress = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 os_free(peer->sm_tmr.buf);
688 peer->sm_tmr.buf = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800689 os_free(peer->ht_capabilities);
690 peer->ht_capabilities = NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800691 os_free(peer->vht_capabilities);
692 peer->vht_capabilities = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800693 os_free(peer->ext_capab);
694 peer->ext_capab = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800695 os_free(peer->supp_channels);
696 peer->supp_channels = NULL;
697 os_free(peer->supp_oper_classes);
698 peer->supp_oper_classes = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699 peer->rsnie_i_len = peer->rsnie_p_len = 0;
700 peer->cipher = 0;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700701 peer->qos_info = 0;
702 peer->wmm_capable = 0;
Jouni Malinen4c527402017-09-22 11:03:15 +0300703 peer->tk_set = peer->tpk_set = peer->tpk_success = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 os_memset(&peer->tpk, 0, sizeof(peer->tpk));
705 os_memset(peer->inonce, 0, WPA_NONCE_LEN);
706 os_memset(peer->rnonce, 0, WPA_NONCE_LEN);
707}
708
709
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700710static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
711{
712 wpa_tdls_peer_clear(sm, peer);
713 wpa_tdls_peer_remove_from_list(sm, peer);
714 os_free(peer);
715}
716
717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718static void wpa_tdls_linkid(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
719 struct wpa_tdls_lnkid *lnkid)
720{
721 lnkid->ie_type = WLAN_EID_LINK_ID;
722 lnkid->ie_len = 3 * ETH_ALEN;
723 os_memcpy(lnkid->bssid, sm->bssid, ETH_ALEN);
724 if (peer->initiator) {
725 os_memcpy(lnkid->init_sta, sm->own_addr, ETH_ALEN);
726 os_memcpy(lnkid->resp_sta, peer->addr, ETH_ALEN);
727 } else {
728 os_memcpy(lnkid->init_sta, peer->addr, ETH_ALEN);
729 os_memcpy(lnkid->resp_sta, sm->own_addr, ETH_ALEN);
730 }
731}
732
733
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700734static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
735 u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736{
737 struct wpa_tdls_peer *peer;
738 struct wpa_tdls_ftie *ftie;
739 struct wpa_tdls_lnkid lnkid;
740 u8 dialog_token;
741 u8 *rbuf, *pos;
742 int ielen;
743
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800744 if (sm->tdls_disabled || !sm->tdls_supported)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700745 return -1;
746
747 /* Find the node and free from the list */
748 for (peer = sm->tdls; peer; peer = peer->next) {
749 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
750 break;
751 }
752
753 if (peer == NULL) {
754 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
755 "Teardown " MACSTR, MAC2STR(addr));
756 return 0;
757 }
758
759 dialog_token = peer->dtoken;
760
761 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown for " MACSTR,
762 MAC2STR(addr));
763
764 ielen = 0;
765 if (wpa_tdls_get_privacy(sm) && peer->tpk_set && peer->tpk_success) {
766 /* To add FTIE for Teardown request and compute MIC */
767 ielen += sizeof(*ftie);
768#ifdef CONFIG_TDLS_TESTING
769 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
770 ielen += 170;
771#endif /* CONFIG_TDLS_TESTING */
772 }
773
774 rbuf = os_zalloc(ielen + 1);
775 if (rbuf == NULL)
776 return -1;
777 pos = rbuf;
778
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700779 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 goto skip_ies;
781
782 ftie = (struct wpa_tdls_ftie *) pos;
783 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
784 /* Using the recent nonce which should be for CONFIRM frame */
785 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
786 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
787 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
788 pos = (u8 *) (ftie + 1);
789#ifdef CONFIG_TDLS_TESTING
790 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
791 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
792 "FTIE");
793 ftie->ie_len += 170;
794 *pos++ = 255; /* FTIE subelem */
795 *pos++ = 168; /* FTIE subelem length */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796 pos += 168;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 }
798#endif /* CONFIG_TDLS_TESTING */
799 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TDLS Teardown handshake",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 (u8 *) ftie, pos - (u8 *) ftie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801
802 /* compute MIC before sending */
803 wpa_tdls_linkid(sm, peer, &lnkid);
804 wpa_tdls_key_mic_teardown(peer->tpk.kck, 4, reason_code,
805 dialog_token, (u8 *) &lnkid, (u8 *) ftie,
806 ftie->mic);
807
808skip_ies:
809 /* TODO: register for a Timeout handler, if Teardown is not received at
810 * the other end, then try again another time */
811
812 /* request driver to send Teardown using this FTIE */
813 wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_TEARDOWN, 0,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700814 reason_code, 0, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 os_free(rbuf);
816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 return 0;
818}
819
820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800821int wpa_tdls_teardown_link(struct wpa_sm *sm, const u8 *addr, u16 reason_code)
822{
823 struct wpa_tdls_peer *peer;
824
825 if (sm->tdls_disabled || !sm->tdls_supported)
826 return -1;
827
828 for (peer = sm->tdls; peer; peer = peer->next) {
829 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
830 break;
831 }
832
833 if (peer == NULL) {
834 wpa_printf(MSG_DEBUG, "TDLS: Could not find peer " MACSTR
835 " for link Teardown", MAC2STR(addr));
836 return -1;
837 }
838
839 if (!peer->tpk_success) {
840 wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
841 " not connected - cannot Teardown link", MAC2STR(addr));
842 return -1;
843 }
844
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300845 return wpa_tdls_do_teardown(sm, peer, reason_code);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800846}
847
848
Sunil Dutt38ffd882013-09-30 17:23:23 +0300849static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
850 struct wpa_tdls_peer *peer)
851{
852 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
853 wpa_tdls_peer_free(sm, peer);
854}
855
856
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700857void wpa_tdls_disable_unreachable_link(struct wpa_sm *sm, const u8 *addr)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858{
859 struct wpa_tdls_peer *peer;
860
861 for (peer = sm->tdls; peer; peer = peer->next) {
862 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
863 break;
864 }
865
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700866 if (!peer || !peer->tpk_success) {
867 wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
868 " not connected - cannot teardown unreachable link",
869 MAC2STR(addr));
870 return;
871 }
872
873 if (wpa_tdls_is_external_setup(sm)) {
874 /*
875 * Disable the link, send a teardown packet through the
876 * AP, and then reset link data.
877 */
878 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, addr);
879 wpa_tdls_send_teardown(sm, addr,
880 WLAN_REASON_TDLS_TEARDOWN_UNREACHABLE);
881 wpa_tdls_peer_free(sm, peer);
882 } else {
Sunil Dutt38ffd882013-09-30 17:23:23 +0300883 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700884 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800885}
886
887
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800888const char * wpa_tdls_get_link_status(struct wpa_sm *sm, const u8 *addr)
889{
890 struct wpa_tdls_peer *peer;
891
892 if (sm->tdls_disabled || !sm->tdls_supported)
893 return "disabled";
894
895 for (peer = sm->tdls; peer; peer = peer->next) {
896 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
897 break;
898 }
899
900 if (peer == NULL)
901 return "peer does not exist";
902
903 if (!peer->tpk_success)
904 return "peer not connected";
905
906 return "connected";
907}
908
909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910static int wpa_tdls_recv_teardown(struct wpa_sm *sm, const u8 *src_addr,
911 const u8 *buf, size_t len)
912{
913 struct wpa_tdls_peer *peer = NULL;
914 struct wpa_tdls_ftie *ftie;
915 struct wpa_tdls_lnkid *lnkid;
916 struct wpa_eapol_ie_parse kde;
917 u16 reason_code;
918 const u8 *pos;
919 int ielen;
920
921 /* Find the node and free from the list */
922 for (peer = sm->tdls; peer; peer = peer->next) {
923 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
924 break;
925 }
926
927 if (peer == NULL) {
928 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
929 "Teardown " MACSTR, MAC2STR(src_addr));
930 return 0;
931 }
932
933 pos = buf;
934 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
935
936 reason_code = WPA_GET_LE16(pos);
937 pos += 2;
938
939 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown Request from " MACSTR
940 " (reason code %u)", MAC2STR(src_addr), reason_code);
941
942 ielen = len - (pos - buf); /* start of IE in buf */
943 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
944 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in Teardown");
945 return -1;
946 }
947
948 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
949 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TDLS "
950 "Teardown");
951 return -1;
952 }
953 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
954
955 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
956 goto skip_ftie;
957
958 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
959 wpa_printf(MSG_INFO, "TDLS: No FTIE in TDLS Teardown");
960 return -1;
961 }
962
963 ftie = (struct wpa_tdls_ftie *) kde.ftie;
964
965 /* Process MIC check to see if TDLS Teardown is right */
966 if (wpa_supplicant_verify_tdls_mic_teardown(4, reason_code,
967 peer->dtoken, peer,
968 (u8 *) lnkid, ftie) < 0) {
969 wpa_printf(MSG_DEBUG, "TDLS: MIC failure for TDLS "
970 "Teardown Request from " MACSTR, MAC2STR(src_addr));
971 return -1;
972 }
973
974skip_ftie:
975 /*
976 * Request the driver to disable the direct link and clear associated
977 * keys.
978 */
Sunil Dutt38ffd882013-09-30 17:23:23 +0300979 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 return 0;
981}
982
983
984/**
985 * wpa_tdls_send_error - To send suitable TDLS status response with
986 * appropriate status code mentioning reason for error/failure.
987 * @dst - MAC addr of Peer station
988 * @tdls_action - TDLS frame type for which error code is sent
989 * @status - status code mentioning reason
990 */
991
992static int wpa_tdls_send_error(struct wpa_sm *sm, const u8 *dst,
993 u8 tdls_action, u8 dialog_token, u16 status)
994{
995 wpa_printf(MSG_DEBUG, "TDLS: Sending error to " MACSTR
996 " (action=%u status=%u)",
997 MAC2STR(dst), tdls_action, status);
998 return wpa_tdls_tpk_send(sm, dst, tdls_action, dialog_token, status,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700999 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000}
1001
1002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001003static struct wpa_tdls_peer *
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001004wpa_tdls_add_peer(struct wpa_sm *sm, const u8 *addr, int *existing)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001005{
1006 struct wpa_tdls_peer *peer;
1007
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001008 if (existing)
1009 *existing = 0;
1010 for (peer = sm->tdls; peer; peer = peer->next) {
1011 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0) {
1012 if (existing)
1013 *existing = 1;
1014 return peer; /* re-use existing entry */
1015 }
1016 }
1017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001018 wpa_printf(MSG_INFO, "TDLS: Creating peer entry for " MACSTR,
1019 MAC2STR(addr));
1020
1021 peer = os_zalloc(sizeof(*peer));
1022 if (peer == NULL)
1023 return NULL;
1024
1025 os_memcpy(peer->addr, addr, ETH_ALEN);
1026 peer->next = sm->tdls;
1027 sm->tdls = peer;
1028
1029 return peer;
1030}
1031
1032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033static int wpa_tdls_send_tpk_m1(struct wpa_sm *sm,
1034 struct wpa_tdls_peer *peer)
1035{
1036 size_t buf_len;
1037 struct wpa_tdls_timeoutie timeoutie;
1038 u16 rsn_capab;
1039 struct wpa_tdls_ftie *ftie;
1040 u8 *rbuf, *pos, *count_pos;
1041 u16 count;
1042 struct rsn_ie_hdr *hdr;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001043 int status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044
1045 if (!wpa_tdls_get_privacy(sm)) {
1046 wpa_printf(MSG_DEBUG, "TDLS: No security used on the link");
1047 peer->rsnie_i_len = 0;
1048 goto skip_rsnie;
1049 }
1050
1051 /*
1052 * TPK Handshake Message 1:
1053 * FTIE: ANonce=0, SNonce=initiator nonce MIC=0, DataKDs=(RSNIE_I,
1054 * Timeout Interval IE))
1055 */
1056
1057 /* Filling RSN IE */
1058 hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
1059 hdr->elem_id = WLAN_EID_RSN;
1060 WPA_PUT_LE16(hdr->version, RSN_VERSION);
1061
1062 pos = (u8 *) (hdr + 1);
1063 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1064 pos += RSN_SELECTOR_LEN;
1065 count_pos = pos;
1066 pos += 2;
1067
1068 count = 0;
1069
1070 /*
1071 * AES-CCMP is the default Encryption preferred for TDLS, so
1072 * RSN IE is filled only with CCMP CIPHER
1073 * Note: TKIP is not used to encrypt TDLS link.
1074 *
1075 * Regardless of the cipher used on the AP connection, select CCMP
1076 * here.
1077 */
1078 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1079 pos += RSN_SELECTOR_LEN;
1080 count++;
1081
1082 WPA_PUT_LE16(count_pos, count);
1083
1084 WPA_PUT_LE16(pos, 1);
1085 pos += 2;
1086 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1087 pos += RSN_SELECTOR_LEN;
1088
1089 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1090 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1091#ifdef CONFIG_TDLS_TESTING
1092 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
1093 wpa_printf(MSG_DEBUG, "TDLS: Use alternative RSN IE for "
1094 "testing");
1095 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1096 }
1097#endif /* CONFIG_TDLS_TESTING */
1098 WPA_PUT_LE16(pos, rsn_capab);
1099 pos += 2;
1100#ifdef CONFIG_TDLS_TESTING
1101 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
1102 /* Number of PMKIDs */
1103 *pos++ = 0x00;
1104 *pos++ = 0x00;
1105 }
1106#endif /* CONFIG_TDLS_TESTING */
1107
1108 hdr->len = (pos - peer->rsnie_i) - 2;
1109 peer->rsnie_i_len = pos - peer->rsnie_i;
1110 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
1111 peer->rsnie_i, peer->rsnie_i_len);
1112
1113skip_rsnie:
1114 buf_len = 0;
1115 if (wpa_tdls_get_privacy(sm))
1116 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1117 sizeof(struct wpa_tdls_timeoutie);
1118#ifdef CONFIG_TDLS_TESTING
1119 if (wpa_tdls_get_privacy(sm) &&
1120 (tdls_testing & TDLS_TESTING_LONG_FRAME))
1121 buf_len += 170;
1122 if (tdls_testing & TDLS_TESTING_DIFF_BSSID)
1123 buf_len += sizeof(struct wpa_tdls_lnkid);
1124#endif /* CONFIG_TDLS_TESTING */
1125 rbuf = os_zalloc(buf_len + 1);
1126 if (rbuf == NULL) {
1127 wpa_tdls_peer_free(sm, peer);
1128 return -1;
1129 }
1130 pos = rbuf;
1131
1132 if (!wpa_tdls_get_privacy(sm))
1133 goto skip_ies;
1134
1135 /* Initiator RSN IE */
1136 pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
1137
1138 ftie = (struct wpa_tdls_ftie *) pos;
1139 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1140 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1141
1142 if (os_get_random(peer->inonce, WPA_NONCE_LEN)) {
1143 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1144 "TDLS: Failed to get random data for initiator Nonce");
1145 os_free(rbuf);
1146 wpa_tdls_peer_free(sm, peer);
1147 return -1;
1148 }
Jouni Malinen4c527402017-09-22 11:03:15 +03001149 peer->tk_set = 0; /* A new nonce results in a new TK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 wpa_hexdump(MSG_DEBUG, "TDLS: Initiator Nonce for TPK handshake",
1151 peer->inonce, WPA_NONCE_LEN);
1152 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1153
1154 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK Handshake M1",
1155 (u8 *) ftie, sizeof(struct wpa_tdls_ftie));
1156
1157 pos = (u8 *) (ftie + 1);
1158
1159#ifdef CONFIG_TDLS_TESTING
1160 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1161 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1162 "FTIE");
1163 ftie->ie_len += 170;
1164 *pos++ = 255; /* FTIE subelem */
1165 *pos++ = 168; /* FTIE subelem length */
1166 pos += 168;
1167 }
1168#endif /* CONFIG_TDLS_TESTING */
1169
1170 /* Lifetime */
1171 peer->lifetime = TPK_LIFETIME;
1172#ifdef CONFIG_TDLS_TESTING
1173 if (tdls_testing & TDLS_TESTING_SHORT_LIFETIME) {
1174 wpa_printf(MSG_DEBUG, "TDLS: Testing - use short TPK "
1175 "lifetime");
1176 peer->lifetime = 301;
1177 }
1178 if (tdls_testing & TDLS_TESTING_LONG_LIFETIME) {
1179 wpa_printf(MSG_DEBUG, "TDLS: Testing - use long TPK "
1180 "lifetime");
1181 peer->lifetime = 0xffffffff;
1182 }
1183#endif /* CONFIG_TDLS_TESTING */
1184 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1185 sizeof(timeoutie), peer->lifetime);
1186 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
1187
1188skip_ies:
1189
1190#ifdef CONFIG_TDLS_TESTING
1191 if (tdls_testing & TDLS_TESTING_DIFF_BSSID) {
1192 wpa_printf(MSG_DEBUG, "TDLS: Testing - use incorrect BSSID in "
1193 "Link Identifier");
1194 struct wpa_tdls_lnkid *l = (struct wpa_tdls_lnkid *) pos;
1195 wpa_tdls_linkid(sm, peer, l);
1196 l->bssid[5] ^= 0x01;
1197 pos += sizeof(*l);
1198 }
1199#endif /* CONFIG_TDLS_TESTING */
1200
1201 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Request / TPK "
1202 "Handshake Message 1 (peer " MACSTR ")",
1203 MAC2STR(peer->addr));
1204
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001205 status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_SETUP_REQUEST,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001206 1, 0, 0, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 os_free(rbuf);
1208
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001209 return status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210}
1211
1212
1213static int wpa_tdls_send_tpk_m2(struct wpa_sm *sm,
1214 const unsigned char *src_addr, u8 dtoken,
1215 struct wpa_tdls_lnkid *lnkid,
1216 const struct wpa_tdls_peer *peer)
1217{
1218 u8 *rbuf, *pos;
1219 size_t buf_len;
1220 u32 lifetime;
1221 struct wpa_tdls_timeoutie timeoutie;
1222 struct wpa_tdls_ftie *ftie;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001223 int status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224
1225 buf_len = 0;
1226 if (wpa_tdls_get_privacy(sm)) {
1227 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1228 * Lifetime */
1229 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1230 sizeof(struct wpa_tdls_timeoutie);
1231#ifdef CONFIG_TDLS_TESTING
1232 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1233 buf_len += 170;
1234#endif /* CONFIG_TDLS_TESTING */
1235 }
1236
1237 rbuf = os_zalloc(buf_len + 1);
1238 if (rbuf == NULL)
1239 return -1;
1240 pos = rbuf;
1241
1242 if (!wpa_tdls_get_privacy(sm))
1243 goto skip_ies;
1244
1245 /* Peer RSN IE */
1246 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1247
1248 ftie = (struct wpa_tdls_ftie *) pos;
1249 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1250 /* TODO: ftie->mic_control to set 2-RESPONSE */
1251 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1252 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1253 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1254 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK M2",
1255 (u8 *) ftie, sizeof(*ftie));
1256
1257 pos = (u8 *) (ftie + 1);
1258
1259#ifdef CONFIG_TDLS_TESTING
1260 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1261 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1262 "FTIE");
1263 ftie->ie_len += 170;
1264 *pos++ = 255; /* FTIE subelem */
1265 *pos++ = 168; /* FTIE subelem length */
1266 pos += 168;
1267 }
1268#endif /* CONFIG_TDLS_TESTING */
1269
1270 /* Lifetime */
1271 lifetime = peer->lifetime;
1272#ifdef CONFIG_TDLS_TESTING
1273 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_RESP) {
1274 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1275 "lifetime in response");
1276 lifetime++;
1277 }
1278#endif /* CONFIG_TDLS_TESTING */
1279 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1280 sizeof(timeoutie), lifetime);
1281 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds from initiator",
1282 lifetime);
1283
1284 /* compute MIC before sending */
1285 wpa_tdls_ftie_mic(peer->tpk.kck, 2, (u8 *) lnkid, peer->rsnie_p,
1286 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001287#ifdef CONFIG_TDLS_TESTING
1288 if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
1289 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
1290 ftie->mic[0] ^= 0x01;
1291 }
1292#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293
1294skip_ies:
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001295 status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001296 dtoken, 0, 0, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 os_free(rbuf);
1298
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001299 return status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300}
1301
1302
1303static int wpa_tdls_send_tpk_m3(struct wpa_sm *sm,
1304 const unsigned char *src_addr, u8 dtoken,
1305 struct wpa_tdls_lnkid *lnkid,
1306 const struct wpa_tdls_peer *peer)
1307{
1308 u8 *rbuf, *pos;
1309 size_t buf_len;
1310 struct wpa_tdls_ftie *ftie;
1311 struct wpa_tdls_timeoutie timeoutie;
1312 u32 lifetime;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001313 int status;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001314 u32 peer_capab = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315
1316 buf_len = 0;
1317 if (wpa_tdls_get_privacy(sm)) {
1318 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1319 * Lifetime */
1320 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1321 sizeof(struct wpa_tdls_timeoutie);
1322#ifdef CONFIG_TDLS_TESTING
1323 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1324 buf_len += 170;
1325#endif /* CONFIG_TDLS_TESTING */
1326 }
1327
1328 rbuf = os_zalloc(buf_len + 1);
1329 if (rbuf == NULL)
1330 return -1;
1331 pos = rbuf;
1332
1333 if (!wpa_tdls_get_privacy(sm))
1334 goto skip_ies;
1335
1336 /* Peer RSN IE */
1337 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1338
1339 ftie = (struct wpa_tdls_ftie *) pos;
1340 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1341 /*TODO: ftie->mic_control to set 3-CONFIRM */
1342 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1343 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1344 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1345
1346 pos = (u8 *) (ftie + 1);
1347
1348#ifdef CONFIG_TDLS_TESTING
1349 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1350 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1351 "FTIE");
1352 ftie->ie_len += 170;
1353 *pos++ = 255; /* FTIE subelem */
1354 *pos++ = 168; /* FTIE subelem length */
1355 pos += 168;
1356 }
1357#endif /* CONFIG_TDLS_TESTING */
1358
1359 /* Lifetime */
1360 lifetime = peer->lifetime;
1361#ifdef CONFIG_TDLS_TESTING
1362 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_CONF) {
1363 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1364 "lifetime in confirm");
1365 lifetime++;
1366 }
1367#endif /* CONFIG_TDLS_TESTING */
1368 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1369 sizeof(timeoutie), lifetime);
1370 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds",
1371 lifetime);
1372
1373 /* compute MIC before sending */
1374 wpa_tdls_ftie_mic(peer->tpk.kck, 3, (u8 *) lnkid, peer->rsnie_p,
1375 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001376#ifdef CONFIG_TDLS_TESTING
1377 if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
1378 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
1379 ftie->mic[0] ^= 0x01;
1380 }
1381#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07001383skip_ies:
1384
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001385 if (peer->vht_capabilities)
1386 peer_capab |= TDLS_PEER_VHT;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07001387 if (peer->ht_capabilities)
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001388 peer_capab |= TDLS_PEER_HT;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07001389 if (peer->wmm_capable)
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001390 peer_capab |= TDLS_PEER_WMM;
1391
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001392 status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001393 dtoken, 0, peer_capab, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 os_free(rbuf);
1395
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001396 return status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397}
1398
1399
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001400static int wpa_tdls_send_discovery_response(struct wpa_sm *sm,
1401 struct wpa_tdls_peer *peer,
1402 u8 dialog_token)
1403{
1404 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Discovery Response "
1405 "(peer " MACSTR ")", MAC2STR(peer->addr));
1406
1407 return wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_DISCOVERY_RESPONSE,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001408 dialog_token, 0, 0, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001409}
1410
1411
1412static int
1413wpa_tdls_process_discovery_request(struct wpa_sm *sm, const u8 *addr,
1414 const u8 *buf, size_t len)
1415{
1416 struct wpa_eapol_ie_parse kde;
1417 const struct wpa_tdls_lnkid *lnkid;
1418 struct wpa_tdls_peer *peer;
1419 size_t min_req_len = sizeof(struct wpa_tdls_frame) +
1420 1 /* dialog token */ + sizeof(struct wpa_tdls_lnkid);
1421 u8 dialog_token;
1422
1423 wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from " MACSTR,
1424 MAC2STR(addr));
1425
1426 if (len < min_req_len) {
1427 wpa_printf(MSG_DEBUG, "TDLS Discovery Request is too short: "
1428 "%d", (int) len);
1429 return -1;
1430 }
1431
1432 dialog_token = buf[sizeof(struct wpa_tdls_frame)];
1433
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07001434 /*
1435 * Some APs will tack on a weird IE to the end of a TDLS
1436 * discovery request packet. This needn't fail the response,
1437 * since the required IE are verified separately.
1438 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001439 if (wpa_supplicant_parse_ies(buf + sizeof(struct wpa_tdls_frame) + 1,
1440 len - (sizeof(struct wpa_tdls_frame) + 1),
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07001441 &kde) < 0) {
1442 wpa_printf(MSG_DEBUG,
1443 "TDLS: Failed to parse IEs in Discovery Request - ignore as an interop workaround");
1444 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001445
1446 if (!kde.lnkid) {
1447 wpa_printf(MSG_DEBUG, "TDLS: Link ID not found in Discovery "
1448 "Request");
1449 return -1;
1450 }
1451
1452 lnkid = (const struct wpa_tdls_lnkid *) kde.lnkid;
1453
1454 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1455 wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from different "
1456 " BSS " MACSTR, MAC2STR(lnkid->bssid));
1457 return -1;
1458 }
1459
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001460 peer = wpa_tdls_add_peer(sm, addr, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001461 if (peer == NULL)
1462 return -1;
1463
1464 return wpa_tdls_send_discovery_response(sm, peer, dialog_token);
1465}
1466
1467
1468int wpa_tdls_send_discovery_request(struct wpa_sm *sm, const u8 *addr)
1469{
1470 if (sm->tdls_disabled || !sm->tdls_supported)
1471 return -1;
1472
1473 wpa_printf(MSG_DEBUG, "TDLS: Sending Discovery Request to peer "
1474 MACSTR, MAC2STR(addr));
1475 return wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_DISCOVERY_REQUEST,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001476 1, 0, 0, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001477}
1478
1479
1480static int copy_supp_rates(const struct wpa_eapol_ie_parse *kde,
1481 struct wpa_tdls_peer *peer)
1482{
1483 if (!kde->supp_rates) {
1484 wpa_printf(MSG_DEBUG, "TDLS: No supported rates received");
1485 return -1;
1486 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001487 peer->supp_rates_len = merge_byte_arrays(
1488 peer->supp_rates, sizeof(peer->supp_rates),
1489 kde->supp_rates + 2, kde->supp_rates_len - 2,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001490 kde->ext_supp_rates ? kde->ext_supp_rates + 2 : NULL,
1491 kde->ext_supp_rates_len - 2);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492 return 0;
1493}
1494
1495
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001496static int copy_peer_ht_capab(const struct wpa_eapol_ie_parse *kde,
1497 struct wpa_tdls_peer *peer)
1498{
1499 if (!kde->ht_capabilities ||
1500 kde->ht_capabilities_len <
1501 sizeof(struct ieee80211_ht_capabilities) ) {
1502 wpa_printf(MSG_DEBUG, "TDLS: No supported ht capabilities "
1503 "received");
1504 return 0;
1505 }
1506
1507 if (!peer->ht_capabilities) {
1508 peer->ht_capabilities =
1509 os_zalloc(sizeof(struct ieee80211_ht_capabilities));
1510 if (peer->ht_capabilities == NULL)
1511 return -1;
1512 }
1513
1514 os_memcpy(peer->ht_capabilities, kde->ht_capabilities,
1515 sizeof(struct ieee80211_ht_capabilities));
1516 wpa_hexdump(MSG_DEBUG, "TDLS: Peer HT capabilities",
1517 (u8 *) peer->ht_capabilities,
1518 sizeof(struct ieee80211_ht_capabilities));
1519
1520 return 0;
1521}
1522
1523
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001524static int copy_peer_vht_capab(const struct wpa_eapol_ie_parse *kde,
1525 struct wpa_tdls_peer *peer)
1526{
1527 if (!kde->vht_capabilities ||
1528 kde->vht_capabilities_len <
1529 sizeof(struct ieee80211_vht_capabilities) ) {
1530 wpa_printf(MSG_DEBUG, "TDLS: No supported vht capabilities "
1531 "received");
1532 return 0;
1533 }
1534
1535 if (!peer->vht_capabilities) {
1536 peer->vht_capabilities =
1537 os_zalloc(sizeof(struct ieee80211_vht_capabilities));
1538 if (peer->vht_capabilities == NULL)
1539 return -1;
1540 }
1541
1542 os_memcpy(peer->vht_capabilities, kde->vht_capabilities,
1543 sizeof(struct ieee80211_vht_capabilities));
1544 wpa_hexdump(MSG_DEBUG, "TDLS: Peer VHT capabilities",
1545 (u8 *) peer->vht_capabilities,
1546 sizeof(struct ieee80211_vht_capabilities));
1547
1548 return 0;
1549}
1550
1551
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001552static int copy_peer_ext_capab(const struct wpa_eapol_ie_parse *kde,
1553 struct wpa_tdls_peer *peer)
1554{
1555 if (!kde->ext_capab) {
1556 wpa_printf(MSG_DEBUG, "TDLS: No extended capabilities "
1557 "received");
1558 return 0;
1559 }
1560
1561 if (!peer->ext_capab || peer->ext_capab_len < kde->ext_capab_len - 2) {
1562 /* Need to allocate buffer to fit the new information */
1563 os_free(peer->ext_capab);
1564 peer->ext_capab = os_zalloc(kde->ext_capab_len - 2);
1565 if (peer->ext_capab == NULL)
1566 return -1;
1567 }
1568
1569 peer->ext_capab_len = kde->ext_capab_len - 2;
1570 os_memcpy(peer->ext_capab, kde->ext_capab + 2, peer->ext_capab_len);
1571
1572 return 0;
1573}
1574
1575
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001576static int copy_peer_wmm_capab(const struct wpa_eapol_ie_parse *kde,
1577 struct wpa_tdls_peer *peer)
1578{
1579 struct wmm_information_element *wmm;
1580
1581 if (!kde->wmm) {
1582 wpa_printf(MSG_DEBUG, "TDLS: No supported WMM capabilities received");
1583 return 0;
1584 }
1585
1586 if (kde->wmm_len < sizeof(struct wmm_information_element)) {
1587 wpa_printf(MSG_DEBUG, "TDLS: Invalid supported WMM capabilities received");
1588 return -1;
1589 }
1590
1591 wmm = (struct wmm_information_element *) kde->wmm;
1592 peer->qos_info = wmm->qos_info;
1593
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001594 peer->wmm_capable = 1;
1595
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001596 wpa_printf(MSG_DEBUG, "TDLS: Peer WMM QOS Info 0x%x", peer->qos_info);
1597 return 0;
1598}
1599
1600
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001601static int copy_peer_supp_channels(const struct wpa_eapol_ie_parse *kde,
1602 struct wpa_tdls_peer *peer)
1603{
1604 if (!kde->supp_channels) {
1605 wpa_printf(MSG_DEBUG, "TDLS: No supported channels received");
1606 return 0;
1607 }
1608
1609 if (!peer->supp_channels ||
1610 peer->supp_channels_len < kde->supp_channels_len) {
1611 os_free(peer->supp_channels);
1612 peer->supp_channels = os_zalloc(kde->supp_channels_len);
1613 if (peer->supp_channels == NULL)
1614 return -1;
1615 }
1616
1617 peer->supp_channels_len = kde->supp_channels_len;
1618
1619 os_memcpy(peer->supp_channels, kde->supp_channels,
1620 peer->supp_channels_len);
1621 wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Channels",
1622 (u8 *) peer->supp_channels, peer->supp_channels_len);
1623 return 0;
1624}
1625
1626
1627static int copy_peer_supp_oper_classes(const struct wpa_eapol_ie_parse *kde,
1628 struct wpa_tdls_peer *peer)
1629{
1630 if (!kde->supp_oper_classes) {
1631 wpa_printf(MSG_DEBUG, "TDLS: No supported operating classes received");
1632 return 0;
1633 }
1634
1635 if (!peer->supp_oper_classes ||
1636 peer->supp_oper_classes_len < kde->supp_oper_classes_len) {
1637 os_free(peer->supp_oper_classes);
1638 peer->supp_oper_classes = os_zalloc(kde->supp_oper_classes_len);
1639 if (peer->supp_oper_classes == NULL)
1640 return -1;
1641 }
1642
1643 peer->supp_oper_classes_len = kde->supp_oper_classes_len;
1644 os_memcpy(peer->supp_oper_classes, kde->supp_oper_classes,
1645 peer->supp_oper_classes_len);
1646 wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Operating Classes",
1647 (u8 *) peer->supp_oper_classes,
1648 peer->supp_oper_classes_len);
1649 return 0;
1650}
1651
1652
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001653static int wpa_tdls_addset_peer(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
1654 int add)
1655{
1656 return wpa_sm_tdls_peer_addset(sm, peer->addr, add, peer->aid,
1657 peer->capability,
1658 peer->supp_rates, peer->supp_rates_len,
1659 peer->ht_capabilities,
1660 peer->vht_capabilities,
1661 peer->qos_info, peer->ext_capab,
1662 peer->ext_capab_len,
1663 peer->supp_channels,
1664 peer->supp_channels_len,
1665 peer->supp_oper_classes,
1666 peer->supp_oper_classes_len);
1667}
1668
1669
Jouni Malinen4c527402017-09-22 11:03:15 +03001670static int tdls_nonce_set(const u8 *nonce)
1671{
1672 int i;
1673
1674 for (i = 0; i < WPA_NONCE_LEN; i++) {
1675 if (nonce[i])
1676 return 1;
1677 }
1678
1679 return 0;
1680}
1681
1682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683static int wpa_tdls_process_tpk_m1(struct wpa_sm *sm, const u8 *src_addr,
1684 const u8 *buf, size_t len)
1685{
1686 struct wpa_tdls_peer *peer;
1687 struct wpa_eapol_ie_parse kde;
1688 struct wpa_ie_data ie;
1689 int cipher;
1690 const u8 *cpos;
1691 struct wpa_tdls_ftie *ftie = NULL;
1692 struct wpa_tdls_timeoutie *timeoutie;
1693 struct wpa_tdls_lnkid *lnkid;
1694 u32 lifetime = 0;
1695#if 0
1696 struct rsn_ie_hdr *hdr;
1697 u8 *pos;
1698 u16 rsn_capab;
1699 u16 rsn_ver;
1700#endif
1701 u8 dtoken;
1702 u16 ielen;
1703 u16 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1704 int tdls_prohibited = sm->tdls_prohibited;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001705 int existing_peer = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706
1707 if (len < 3 + 3)
1708 return -1;
1709
1710 cpos = buf;
1711 cpos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1712
1713 /* driver had already verified the frame format */
1714 dtoken = *cpos++; /* dialog token */
1715
1716 wpa_printf(MSG_INFO, "TDLS: Dialog Token in TPK M1 %d", dtoken);
1717
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001718 peer = wpa_tdls_add_peer(sm, src_addr, &existing_peer);
1719 if (peer == NULL)
1720 goto error;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001721
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001722 /* If found, use existing entry instead of adding a new one;
1723 * how to handle the case where both ends initiate at the
1724 * same time? */
1725 if (existing_peer) {
1726 if (peer->tpk_success) {
1727 wpa_printf(MSG_DEBUG, "TDLS: TDLS Setup Request while "
1728 "direct link is enabled - tear down the "
1729 "old link first");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001730 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
1731 wpa_tdls_peer_clear(sm, peer);
1732 } else if (peer->initiator) {
1733 /*
1734 * An entry is already present, so check if we already
1735 * sent a TDLS Setup Request. If so, compare MAC
1736 * addresses and let the STA with the lower MAC address
1737 * continue as the initiator. The other negotiation is
1738 * terminated.
1739 */
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001740 if (os_memcmp(sm->own_addr, src_addr, ETH_ALEN) < 0) {
1741 wpa_printf(MSG_DEBUG, "TDLS: Discard request "
1742 "from peer with higher address "
1743 MACSTR, MAC2STR(src_addr));
1744 return -1;
1745 } else {
1746 wpa_printf(MSG_DEBUG, "TDLS: Accept request "
1747 "from peer with lower address "
1748 MACSTR " (terminate previously "
1749 "initiated negotiation",
1750 MAC2STR(src_addr));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001751 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK,
1752 peer->addr);
1753 wpa_tdls_peer_clear(sm, peer);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001754 }
1755 }
1756 }
1757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001758 /* capability information */
1759 peer->capability = WPA_GET_LE16(cpos);
1760 cpos += 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761
1762 ielen = len - (cpos - buf); /* start of IE in buf */
1763 if (wpa_supplicant_parse_ies(cpos, ielen, &kde) < 0) {
1764 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in TPK M1");
1765 goto error;
1766 }
1767
1768 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
1769 wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
1770 "TPK M1");
1771 goto error;
1772 }
1773 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M1",
1774 kde.lnkid, kde.lnkid_len);
1775 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
1776 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1777 wpa_printf(MSG_INFO, "TDLS: TPK M1 from diff BSS");
1778 status = WLAN_STATUS_NOT_IN_SAME_BSS;
1779 goto error;
1780 }
1781
1782 wpa_printf(MSG_DEBUG, "TDLS: TPK M1 - TPK initiator " MACSTR,
1783 MAC2STR(src_addr));
1784
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001785 if (copy_supp_rates(&kde, peer) < 0)
1786 goto error;
1787
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001788 if (copy_peer_ht_capab(&kde, peer) < 0)
1789 goto error;
1790
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001791 if (copy_peer_vht_capab(&kde, peer) < 0)
1792 goto error;
1793
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001794 if (copy_peer_ext_capab(&kde, peer) < 0)
1795 goto error;
1796
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001797 if (copy_peer_supp_channels(&kde, peer) < 0)
1798 goto error;
1799
1800 if (copy_peer_supp_oper_classes(&kde, peer) < 0)
1801 goto error;
1802
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001803 peer->qos_info = kde.qosinfo;
1804
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001805 /* Overwrite with the qos_info obtained in WMM IE */
1806 if (copy_peer_wmm_capab(&kde, peer) < 0)
1807 goto error;
1808
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001809 peer->aid = kde.aid;
1810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811#ifdef CONFIG_TDLS_TESTING
1812 if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001813 peer = wpa_tdls_add_peer(sm, src_addr, NULL);
1814 if (peer == NULL)
1815 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 wpa_printf(MSG_DEBUG, "TDLS: Testing concurrent initiation of "
1817 "TDLS setup - send own request");
1818 peer->initiator = 1;
1819 wpa_tdls_send_tpk_m1(sm, peer);
1820 }
1821
1822 if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
1823 tdls_prohibited) {
1824 wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
1825 "on TDLS");
1826 tdls_prohibited = 0;
1827 }
1828#endif /* CONFIG_TDLS_TESTING */
1829
1830 if (tdls_prohibited) {
1831 wpa_printf(MSG_INFO, "TDLS: TDLS prohibited in this BSS");
1832 status = WLAN_STATUS_REQUEST_DECLINED;
1833 goto error;
1834 }
1835
1836 if (!wpa_tdls_get_privacy(sm)) {
1837 if (kde.rsn_ie) {
1838 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M1 while "
1839 "security is disabled");
1840 status = WLAN_STATUS_SECURITY_DISABLED;
1841 goto error;
1842 }
1843 goto skip_rsn;
1844 }
1845
1846 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
1847 kde.rsn_ie == NULL) {
1848 wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M1");
1849 status = WLAN_STATUS_INVALID_PARAMETERS;
1850 goto error;
1851 }
1852
1853 if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
1854 wpa_printf(MSG_INFO, "TDLS: Too long Initiator RSN IE in "
1855 "TPK M1");
1856 status = WLAN_STATUS_INVALID_RSNIE;
1857 goto error;
1858 }
1859
1860 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
1861 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M1");
1862 status = WLAN_STATUS_INVALID_RSNIE;
1863 goto error;
1864 }
1865
1866 cipher = ie.pairwise_cipher;
1867 if (cipher & WPA_CIPHER_CCMP) {
1868 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
1869 cipher = WPA_CIPHER_CCMP;
1870 } else {
1871 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M1");
1872 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1873 goto error;
1874 }
1875
1876 if ((ie.capabilities &
1877 (WPA_CAPABILITY_NO_PAIRWISE | WPA_CAPABILITY_PEERKEY_ENABLED)) !=
1878 WPA_CAPABILITY_PEERKEY_ENABLED) {
1879 wpa_printf(MSG_INFO, "TDLS: Invalid RSN Capabilities in "
1880 "TPK M1");
1881 status = WLAN_STATUS_INVALID_RSN_IE_CAPAB;
1882 goto error;
1883 }
1884
1885 /* Lifetime */
1886 if (kde.key_lifetime == NULL) {
1887 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M1");
1888 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
1889 goto error;
1890 }
1891 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
1892 lifetime = WPA_GET_LE32(timeoutie->value);
1893 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", lifetime);
1894 if (lifetime < 300) {
1895 wpa_printf(MSG_INFO, "TDLS: Too short TPK lifetime");
1896 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
1897 goto error;
1898 }
1899
1900skip_rsn:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001901#ifdef CONFIG_TDLS_TESTING
1902 if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
1903 if (os_memcmp(sm->own_addr, peer->addr, ETH_ALEN) < 0) {
1904 /*
1905 * The request frame from us is going to win, so do not
1906 * replace information based on this request frame from
1907 * the peer.
1908 */
1909 goto skip_rsn_check;
1910 }
1911 }
1912#endif /* CONFIG_TDLS_TESTING */
1913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914 peer->initiator = 0; /* Need to check */
1915 peer->dtoken = dtoken;
1916
1917 if (!wpa_tdls_get_privacy(sm)) {
1918 peer->rsnie_i_len = 0;
1919 peer->rsnie_p_len = 0;
1920 peer->cipher = WPA_CIPHER_NONE;
1921 goto skip_rsn_check;
1922 }
1923
1924 ftie = (struct wpa_tdls_ftie *) kde.ftie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 os_memcpy(peer->rsnie_i, kde.rsn_ie, kde.rsn_ie_len);
1926 peer->rsnie_i_len = kde.rsn_ie_len;
1927 peer->cipher = cipher;
1928
Jouni Malinen4c527402017-09-22 11:03:15 +03001929 if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0 ||
1930 !tdls_nonce_set(peer->inonce)) {
Sunil Dutt61024722013-09-15 12:09:40 -07001931 /*
1932 * There is no point in updating the RNonce for every obtained
1933 * TPK M1 frame (e.g., retransmission due to timeout) with the
1934 * same INonce (SNonce in FTIE). However, if the TPK M1 is
1935 * retransmitted with a different INonce, update the RNonce
1936 * since this is for a new TDLS session.
1937 */
1938 wpa_printf(MSG_DEBUG,
1939 "TDLS: New TPK M1 INonce - generate new RNonce");
1940 os_memcpy(peer->inonce, ftie->Snonce, WPA_NONCE_LEN);
1941 if (os_get_random(peer->rnonce, WPA_NONCE_LEN)) {
1942 wpa_msg(sm->ctx->ctx, MSG_WARNING,
1943 "TDLS: Failed to get random data for responder nonce");
Sunil Dutt61024722013-09-15 12:09:40 -07001944 goto error;
1945 }
Jouni Malinen4c527402017-09-22 11:03:15 +03001946 peer->tk_set = 0; /* A new nonce results in a new TK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 }
1948
1949#if 0
1950 /* get version info from RSNIE received from Peer */
1951 hdr = (struct rsn_ie_hdr *) kde.rsn_ie;
1952 rsn_ver = WPA_GET_LE16(hdr->version);
1953
1954 /* use min(peer's version, out version) */
1955 if (rsn_ver > RSN_VERSION)
1956 rsn_ver = RSN_VERSION;
1957
1958 hdr = (struct rsn_ie_hdr *) peer->rsnie_p;
1959
1960 hdr->elem_id = WLAN_EID_RSN;
1961 WPA_PUT_LE16(hdr->version, rsn_ver);
1962 pos = (u8 *) (hdr + 1);
1963
1964 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1965 pos += RSN_SELECTOR_LEN;
1966 /* Include only the selected cipher in pairwise cipher suite */
1967 WPA_PUT_LE16(pos, 1);
1968 pos += 2;
1969 if (cipher == WPA_CIPHER_CCMP)
1970 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1971 pos += RSN_SELECTOR_LEN;
1972
1973 WPA_PUT_LE16(pos, 1);
1974 pos += 2;
1975 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1976 pos += RSN_SELECTOR_LEN;
1977
1978 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1979 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1980 WPA_PUT_LE16(pos, rsn_capab);
1981 pos += 2;
1982
1983 hdr->len = (pos - peer->rsnie_p) - 2;
1984 peer->rsnie_p_len = pos - peer->rsnie_p;
1985#endif
1986
1987 /* temp fix: validation of RSNIE later */
1988 os_memcpy(peer->rsnie_p, peer->rsnie_i, peer->rsnie_i_len);
1989 peer->rsnie_p_len = peer->rsnie_i_len;
1990
1991 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
1992 peer->rsnie_p, peer->rsnie_p_len);
1993
1994 peer->lifetime = lifetime;
1995
1996 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
1997
1998skip_rsn_check:
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001999 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2000 if (wpa_tdls_addset_peer(sm, peer, 1) < 0)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002001 goto error;
2002
Sunil Dutt73b28cc2013-09-30 17:38:41 +03002003 peer->tpk_in_progress = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Response / TPK M2");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002006 if (wpa_tdls_send_tpk_m2(sm, src_addr, dtoken, lnkid, peer) < 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002007 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002008 goto error;
2009 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002010
2011 return 0;
2012
2013error:
2014 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE, dtoken,
2015 status);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002016 if (peer)
2017 wpa_tdls_peer_free(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018 return -1;
2019}
2020
2021
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002022static int wpa_tdls_enable_link(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023{
2024 peer->tpk_success = 1;
Sunil Dutt73b28cc2013-09-30 17:38:41 +03002025 peer->tpk_in_progress = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
2027 if (wpa_tdls_get_privacy(sm)) {
2028 u32 lifetime = peer->lifetime;
2029 /*
2030 * Start the initiator process a bit earlier to avoid race
2031 * condition with the responder sending teardown request.
2032 */
2033 if (lifetime > 3 && peer->initiator)
2034 lifetime -= 3;
2035 eloop_register_timeout(lifetime, 0, wpa_tdls_tpk_timeout,
2036 sm, peer);
2037#ifdef CONFIG_TDLS_TESTING
2038 if (tdls_testing & TDLS_TESTING_NO_TPK_EXPIRATION) {
2039 wpa_printf(MSG_DEBUG, "TDLS: Testing - disable TPK "
2040 "expiration");
2041 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
2042 }
2043#endif /* CONFIG_TDLS_TESTING */
2044 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002045
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002046 if (peer->reconfig_key && wpa_tdls_set_key(sm, peer) < 0) {
2047 wpa_printf(MSG_INFO, "TDLS: Could not configure key to the "
2048 "driver");
2049 return -1;
2050 }
2051 peer->reconfig_key = 0;
2052
2053 return wpa_sm_tdls_oper(sm, TDLS_ENABLE_LINK, peer->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054}
2055
2056
2057static int wpa_tdls_process_tpk_m2(struct wpa_sm *sm, const u8 *src_addr,
2058 const u8 *buf, size_t len)
2059{
2060 struct wpa_tdls_peer *peer;
2061 struct wpa_eapol_ie_parse kde;
2062 struct wpa_ie_data ie;
2063 int cipher;
2064 struct wpa_tdls_ftie *ftie;
2065 struct wpa_tdls_timeoutie *timeoutie;
2066 struct wpa_tdls_lnkid *lnkid;
2067 u32 lifetime;
2068 u8 dtoken;
2069 int ielen;
2070 u16 status;
2071 const u8 *pos;
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002072 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073
2074 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Response / TPK M2 "
2075 "(Peer " MACSTR ")", MAC2STR(src_addr));
2076 for (peer = sm->tdls; peer; peer = peer->next) {
2077 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
2078 break;
2079 }
2080 if (peer == NULL) {
2081 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
2082 "TPK M2: " MACSTR, MAC2STR(src_addr));
2083 return -1;
2084 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002085 if (!peer->initiator) {
2086 /*
2087 * This may happen if both devices try to initiate TDLS at the
2088 * same time and we accept the TPK M1 from the peer in
2089 * wpa_tdls_process_tpk_m1() and clear our previous state.
2090 */
2091 wpa_printf(MSG_INFO, "TDLS: We were not the initiator, so "
2092 "ignore TPK M2 from " MACSTR, MAC2STR(src_addr));
2093 return -1;
2094 }
Arik Nemtsova7baabb2015-12-10 12:56:07 +02002095
2096 if (peer->tpk_success) {
2097 wpa_printf(MSG_INFO, "TDLS: Ignore incoming TPK M2 retry, from "
2098 MACSTR " as TPK M3 was already sent",
2099 MAC2STR(src_addr));
2100 return 0;
2101 }
2102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_REQUEST);
2104
Sunil Duttadce9cf2013-09-15 11:51:00 -07002105 if (len < 3 + 2 + 1) {
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002106 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 return -1;
Sunil Duttadce9cf2013-09-15 11:51:00 -07002108 }
2109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 pos = buf;
2111 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2112 status = WPA_GET_LE16(pos);
2113 pos += 2 /* status code */;
2114
2115 if (status != WLAN_STATUS_SUCCESS) {
2116 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M2: %u",
2117 status);
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002118 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 return -1;
2120 }
2121
2122 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2123
2124 /* TODO: need to verify dialog token matches here or in kernel */
2125 dtoken = *pos++; /* dialog token */
2126
2127 wpa_printf(MSG_DEBUG, "TDLS: Dialog Token in TPK M2 %d", dtoken);
2128
Sunil Duttadce9cf2013-09-15 11:51:00 -07002129 if (len < 3 + 2 + 1 + 2) {
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002130 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002131 return -1;
Sunil Duttadce9cf2013-09-15 11:51:00 -07002132 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002133
2134 /* capability information */
2135 peer->capability = WPA_GET_LE16(pos);
2136 pos += 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137
2138 ielen = len - (pos - buf); /* start of IE in buf */
2139 if (wpa_supplicant_parse_ies(pos, ielen, &kde) < 0) {
2140 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in TPK M2");
2141 goto error;
2142 }
2143
2144#ifdef CONFIG_TDLS_TESTING
2145 if (tdls_testing & TDLS_TESTING_DECLINE_RESP) {
2146 wpa_printf(MSG_DEBUG, "TDLS: Testing - decline response");
2147 status = WLAN_STATUS_REQUEST_DECLINED;
2148 goto error;
2149 }
2150#endif /* CONFIG_TDLS_TESTING */
2151
2152 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
2153 wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
2154 "TPK M2");
2155 goto error;
2156 }
2157 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M2",
2158 kde.lnkid, kde.lnkid_len);
2159 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
2160
2161 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
2162 wpa_printf(MSG_INFO, "TDLS: TPK M2 from different BSS");
2163 status = WLAN_STATUS_NOT_IN_SAME_BSS;
2164 goto error;
2165 }
2166
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002167 if (copy_supp_rates(&kde, peer) < 0)
2168 goto error;
2169
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002170 if (copy_peer_ht_capab(&kde, peer) < 0)
2171 goto error;
2172
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002173 if (copy_peer_vht_capab(&kde, peer) < 0)
2174 goto error;
2175
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002176 if (copy_peer_ext_capab(&kde, peer) < 0)
2177 goto error;
2178
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002179 if (copy_peer_supp_channels(&kde, peer) < 0)
2180 goto error;
2181
2182 if (copy_peer_supp_oper_classes(&kde, peer) < 0)
2183 goto error;
2184
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002185 peer->qos_info = kde.qosinfo;
2186
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002187 /* Overwrite with the qos_info obtained in WMM IE */
2188 if (copy_peer_wmm_capab(&kde, peer) < 0)
2189 goto error;
2190
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002191 peer->aid = kde.aid;
2192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 if (!wpa_tdls_get_privacy(sm)) {
2194 peer->rsnie_p_len = 0;
2195 peer->cipher = WPA_CIPHER_NONE;
2196 goto skip_rsn;
2197 }
2198
2199 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
2200 kde.rsn_ie == NULL) {
2201 wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M2");
2202 status = WLAN_STATUS_INVALID_PARAMETERS;
2203 goto error;
2204 }
2205 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
2206 kde.rsn_ie, kde.rsn_ie_len);
2207
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002208 if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
2209 wpa_printf(MSG_INFO,
2210 "TDLS: Too long Responder RSN IE in TPK M2");
2211 status = WLAN_STATUS_INVALID_RSNIE;
2212 goto error;
2213 }
2214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 /*
2216 * FIX: bitwise comparison of RSN IE is not the correct way of
2217 * validation this. It can be different, but certain fields must
2218 * match. Since we list only a single pairwise cipher in TPK M1, the
2219 * memcmp is likely to work in most cases, though.
2220 */
2221 if (kde.rsn_ie_len != peer->rsnie_i_len ||
2222 os_memcmp(peer->rsnie_i, kde.rsn_ie, peer->rsnie_i_len) != 0) {
2223 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M2 does "
2224 "not match with RSN IE used in TPK M1");
2225 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Sent in TPK M1",
2226 peer->rsnie_i, peer->rsnie_i_len);
2227 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
2228 kde.rsn_ie, kde.rsn_ie_len);
2229 status = WLAN_STATUS_INVALID_RSNIE;
2230 goto error;
2231 }
2232
2233 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
2234 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M2");
2235 status = WLAN_STATUS_INVALID_RSNIE;
2236 goto error;
2237 }
2238
2239 cipher = ie.pairwise_cipher;
2240 if (cipher == WPA_CIPHER_CCMP) {
2241 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
2242 cipher = WPA_CIPHER_CCMP;
2243 } else {
2244 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M2");
2245 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
2246 goto error;
2247 }
2248
2249 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M2",
2250 kde.ftie, sizeof(*ftie));
2251 ftie = (struct wpa_tdls_ftie *) kde.ftie;
2252
2253 if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
2254 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M2 does "
2255 "not match with FTIE SNonce used in TPK M1");
2256 /* Silently discard the frame */
2257 return -1;
2258 }
2259
2260 /* Responder Nonce and RSN IE */
2261 os_memcpy(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN);
2262 os_memcpy(peer->rsnie_p, kde.rsn_ie, kde.rsn_ie_len);
2263 peer->rsnie_p_len = kde.rsn_ie_len;
2264 peer->cipher = cipher;
2265
2266 /* Lifetime */
2267 if (kde.key_lifetime == NULL) {
2268 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M2");
2269 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2270 goto error;
2271 }
2272 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2273 lifetime = WPA_GET_LE32(timeoutie->value);
2274 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M2",
2275 lifetime);
2276 if (lifetime != peer->lifetime) {
2277 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2278 "TPK M2 (expected %u)", lifetime, peer->lifetime);
2279 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2280 goto error;
2281 }
2282
2283 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
2284
2285 /* Process MIC check to see if TPK M2 is right */
2286 if (wpa_supplicant_verify_tdls_mic(2, peer, (u8 *) lnkid,
2287 (u8 *) timeoutie, ftie) < 0) {
2288 /* Discard the frame */
2289 wpa_tdls_del_key(sm, peer);
Sunil Dutt38ffd882013-09-30 17:23:23 +03002290 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291 return -1;
2292 }
2293
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002294 if (wpa_tdls_set_key(sm, peer) < 0) {
2295 /*
2296 * Some drivers may not be able to config the key prior to full
2297 * STA entry having been configured.
2298 */
2299 wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2300 "STA entry is complete");
2301 peer->reconfig_key = 1;
2302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303
2304skip_rsn:
2305 peer->dtoken = dtoken;
2306
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002307 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2308 if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
2309 goto error;
2310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Confirm / "
2312 "TPK Handshake Message 3");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002313 if (wpa_tdls_send_tpk_m3(sm, src_addr, dtoken, lnkid, peer) < 0)
2314 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002316 if (!peer->tpk_success) {
2317 /*
2318 * Enable Link only when tpk_success is 0, signifying that this
2319 * processing of TPK M2 frame is not because of a retransmission
2320 * during TDLS setup handshake.
2321 */
2322 ret = wpa_tdls_enable_link(sm, peer);
2323 if (ret < 0) {
2324 wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
2325 wpa_tdls_do_teardown(
2326 sm, peer,
2327 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
2328 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002329 }
2330 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331
2332error:
2333 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM, dtoken,
2334 status);
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002335 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 return -1;
2337}
2338
2339
2340static int wpa_tdls_process_tpk_m3(struct wpa_sm *sm, const u8 *src_addr,
2341 const u8 *buf, size_t len)
2342{
2343 struct wpa_tdls_peer *peer;
2344 struct wpa_eapol_ie_parse kde;
2345 struct wpa_tdls_ftie *ftie;
2346 struct wpa_tdls_timeoutie *timeoutie;
2347 struct wpa_tdls_lnkid *lnkid;
2348 int ielen;
2349 u16 status;
2350 const u8 *pos;
2351 u32 lifetime;
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002352 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353
2354 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Confirm / TPK M3 "
2355 "(Peer " MACSTR ")", MAC2STR(src_addr));
2356 for (peer = sm->tdls; peer; peer = peer->next) {
2357 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
2358 break;
2359 }
2360 if (peer == NULL) {
2361 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
2362 "TPK M3: " MACSTR, MAC2STR(src_addr));
2363 return -1;
2364 }
2365 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_RESPONSE);
2366
2367 if (len < 3 + 3)
Sunil Duttadce9cf2013-09-15 11:51:00 -07002368 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369 pos = buf;
2370 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2371
2372 status = WPA_GET_LE16(pos);
2373
2374 if (status != 0) {
2375 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M3: %u",
2376 status);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002377 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378 }
2379 pos += 2 /* status code */ + 1 /* dialog token */;
2380
2381 ielen = len - (pos - buf); /* start of IE in buf */
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002382
2383 /*
2384 * Don't reject the message if failing to parse IEs. The IEs we need are
2385 * explicitly checked below. Some APs piggy-back broken IEs to the end
2386 * of a TDLS Confirm packet, which will fail the link if we don't ignore
2387 * this error.
2388 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002390 wpa_printf(MSG_DEBUG,
2391 "TDLS: Failed to parse KDEs in TPK M3 - ignore as an interop workaround");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392 }
2393
2394 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
2395 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002396 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397 }
2398 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M3",
2399 (u8 *) kde.lnkid, kde.lnkid_len);
2400 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
2401
2402 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
2403 wpa_printf(MSG_INFO, "TDLS: TPK M3 from diff BSS");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002404 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405 }
2406
2407 if (!wpa_tdls_get_privacy(sm))
2408 goto skip_rsn;
2409
2410 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
2411 wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002412 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 }
2414 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M3",
2415 kde.ftie, sizeof(*ftie));
2416 ftie = (struct wpa_tdls_ftie *) kde.ftie;
2417
2418 if (kde.rsn_ie == NULL) {
2419 wpa_printf(MSG_INFO, "TDLS: No RSN IE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002420 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421 }
2422 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M3",
2423 kde.rsn_ie, kde.rsn_ie_len);
2424 if (kde.rsn_ie_len != peer->rsnie_p_len ||
2425 os_memcmp(kde.rsn_ie, peer->rsnie_p, peer->rsnie_p_len) != 0) {
2426 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M3 does not match "
2427 "with the one sent in TPK M2");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002428 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429 }
2430
2431 if (!os_memcmp(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN) == 0) {
2432 wpa_printf(MSG_INFO, "TDLS: FTIE ANonce in TPK M3 does "
2433 "not match with FTIE ANonce used in TPK M2");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002434 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002435 }
2436
2437 if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
2438 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M3 does not "
2439 "match with FTIE SNonce used in TPK M1");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002440 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441 }
2442
2443 if (kde.key_lifetime == NULL) {
2444 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002445 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002446 }
2447 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2448 wpa_hexdump(MSG_DEBUG, "TDLS: Timeout IE Received from TPK M3",
2449 (u8 *) timeoutie, sizeof(*timeoutie));
2450 lifetime = WPA_GET_LE32(timeoutie->value);
2451 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M3",
2452 lifetime);
2453 if (lifetime != peer->lifetime) {
2454 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2455 "TPK M3 (expected %u)", lifetime, peer->lifetime);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002456 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457 }
2458
2459 if (wpa_supplicant_verify_tdls_mic(3, peer, (u8 *) lnkid,
2460 (u8 *) timeoutie, ftie) < 0) {
2461 wpa_tdls_del_key(sm, peer);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002462 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 }
2464
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002465 if (wpa_tdls_set_key(sm, peer) < 0) {
2466 /*
2467 * Some drivers may not be able to config the key prior to full
2468 * STA entry having been configured.
2469 */
2470 wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2471 "STA entry is complete");
2472 peer->reconfig_key = 1;
2473 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474
2475skip_rsn:
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002476 /* add supported rates, capabilities, and qos_info to the TDLS peer */
2477 if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
2478 goto error;
2479
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002480 if (!peer->tpk_success) {
2481 /*
2482 * Enable Link only when tpk_success is 0, signifying that this
2483 * processing of TPK M3 frame is not because of a retransmission
2484 * during TDLS setup handshake.
2485 */
2486 ret = wpa_tdls_enable_link(sm, peer);
2487 if (ret < 0) {
2488 wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002489 goto error;
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08002490 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002491 }
2492 return ret;
Sunil Duttadce9cf2013-09-15 11:51:00 -07002493error:
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002494 wpa_tdls_do_teardown(sm, peer, WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002495 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496}
2497
2498
2499static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs)
2500{
2501 struct wpa_tdls_timeoutie *lifetime = (struct wpa_tdls_timeoutie *) ie;
2502
2503 os_memset(lifetime, 0, ie_len);
2504 lifetime->ie_type = WLAN_EID_TIMEOUT_INTERVAL;
2505 lifetime->ie_len = sizeof(struct wpa_tdls_timeoutie) - 2;
2506 lifetime->interval_type = WLAN_TIMEOUT_KEY_LIFETIME;
2507 WPA_PUT_LE32(lifetime->value, tsecs);
2508 os_memcpy(pos, ie, ie_len);
2509 return pos + ie_len;
2510}
2511
2512
2513/**
2514 * wpa_tdls_start - Initiate TDLS handshake (send TPK Handshake Message 1)
2515 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2516 * @peer: MAC address of the peer STA
2517 * Returns: 0 on success, or -1 on failure
2518 *
2519 * Send TPK Handshake Message 1 info to driver to start TDLS
2520 * handshake with the peer.
2521 */
2522int wpa_tdls_start(struct wpa_sm *sm, const u8 *addr)
2523{
2524 struct wpa_tdls_peer *peer;
2525 int tdls_prohibited = sm->tdls_prohibited;
2526
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002527 if (sm->tdls_disabled || !sm->tdls_supported)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 return -1;
2529
2530#ifdef CONFIG_TDLS_TESTING
2531 if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
2532 tdls_prohibited) {
2533 wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
2534 "on TDLS");
2535 tdls_prohibited = 0;
2536 }
2537#endif /* CONFIG_TDLS_TESTING */
2538
2539 if (tdls_prohibited) {
2540 wpa_printf(MSG_DEBUG, "TDLS: TDLS is prohibited in this BSS - "
2541 "reject request to start setup");
2542 return -1;
2543 }
2544
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002545 peer = wpa_tdls_add_peer(sm, addr, NULL);
2546 if (peer == NULL)
2547 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548
Sunil Dutt73b28cc2013-09-30 17:38:41 +03002549 if (peer->tpk_in_progress) {
2550 wpa_printf(MSG_DEBUG, "TDLS: Setup is already in progress with the peer");
2551 return 0;
2552 }
2553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554 peer->initiator = 1;
2555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002556 /* add the peer to the driver as a "setup in progress" peer */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002557 if (wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL,
2558 NULL, 0, NULL, 0, NULL, 0, NULL, 0)) {
2559 wpa_tdls_disable_peer_link(sm, peer);
2560 return -1;
2561 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002562
Sunil Dutt73b28cc2013-09-30 17:38:41 +03002563 peer->tpk_in_progress = 1;
2564
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002565 if (wpa_tdls_send_tpk_m1(sm, peer) < 0) {
Sunil Duttd0ef38b2013-09-30 17:34:13 +03002566 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002567 return -1;
2568 }
2569
2570 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571}
2572
2573
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002574void wpa_tdls_remove(struct wpa_sm *sm, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575{
2576 struct wpa_tdls_peer *peer;
2577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002578 if (sm->tdls_disabled || !sm->tdls_supported)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002579 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580
2581 for (peer = sm->tdls; peer; peer = peer->next) {
2582 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
2583 break;
2584 }
2585
2586 if (peer == NULL || !peer->tpk_success)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002587 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002589 if (sm->tdls_external_setup) {
2590 /*
2591 * Disable previous link to allow renegotiation to be completed
2592 * on AP path.
2593 */
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002594 wpa_tdls_do_teardown(sm, peer,
2595 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002596 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597}
2598
2599
2600/**
2601 * wpa_supplicant_rx_tdls - Receive TDLS data frame
2602 *
2603 * This function is called to receive TDLS (ethertype = 0x890d) data frames.
2604 */
2605static void wpa_supplicant_rx_tdls(void *ctx, const u8 *src_addr,
2606 const u8 *buf, size_t len)
2607{
2608 struct wpa_sm *sm = ctx;
2609 struct wpa_tdls_frame *tf;
2610
2611 wpa_hexdump(MSG_DEBUG, "TDLS: Received Data frame encapsulation",
2612 buf, len);
2613
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002614 if (sm->tdls_disabled || !sm->tdls_supported) {
2615 wpa_printf(MSG_DEBUG, "TDLS: Discard message - TDLS disabled "
2616 "or unsupported by driver");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002617 return;
2618 }
2619
2620 if (os_memcmp(src_addr, sm->own_addr, ETH_ALEN) == 0) {
2621 wpa_printf(MSG_DEBUG, "TDLS: Discard copy of own message");
2622 return;
2623 }
2624
2625 if (len < sizeof(*tf)) {
2626 wpa_printf(MSG_INFO, "TDLS: Drop too short frame");
2627 return;
2628 }
2629
2630 /* Check to make sure its a valid encapsulated TDLS frame */
2631 tf = (struct wpa_tdls_frame *) buf;
2632 if (tf->payloadtype != 2 /* TDLS_RFTYPE */ ||
2633 tf->category != WLAN_ACTION_TDLS) {
2634 wpa_printf(MSG_INFO, "TDLS: Invalid frame - payloadtype=%u "
2635 "category=%u action=%u",
2636 tf->payloadtype, tf->category, tf->action);
2637 return;
2638 }
2639
2640 switch (tf->action) {
2641 case WLAN_TDLS_SETUP_REQUEST:
2642 wpa_tdls_process_tpk_m1(sm, src_addr, buf, len);
2643 break;
2644 case WLAN_TDLS_SETUP_RESPONSE:
2645 wpa_tdls_process_tpk_m2(sm, src_addr, buf, len);
2646 break;
2647 case WLAN_TDLS_SETUP_CONFIRM:
2648 wpa_tdls_process_tpk_m3(sm, src_addr, buf, len);
2649 break;
2650 case WLAN_TDLS_TEARDOWN:
2651 wpa_tdls_recv_teardown(sm, src_addr, buf, len);
2652 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002653 case WLAN_TDLS_DISCOVERY_REQUEST:
2654 wpa_tdls_process_discovery_request(sm, src_addr, buf, len);
2655 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 default:
2657 /* Kernel code will process remaining frames */
2658 wpa_printf(MSG_DEBUG, "TDLS: Ignore TDLS frame action code %u",
2659 tf->action);
2660 break;
2661 }
2662}
2663
2664
2665/**
2666 * wpa_tdls_init - Initialize driver interface parameters for TDLS
2667 * @wpa_s: Pointer to wpa_supplicant data
2668 * Returns: 0 on success, -1 on failure
2669 *
2670 * This function is called to initialize driver interface parameters for TDLS.
2671 * wpa_drv_init() must have been called before this function to initialize the
2672 * driver interface.
2673 */
2674int wpa_tdls_init(struct wpa_sm *sm)
2675{
2676 if (sm == NULL)
2677 return -1;
2678
Dmitry Shmidt04949592012-07-19 12:16:46 -07002679 sm->l2_tdls = l2_packet_init(sm->bridge_ifname ? sm->bridge_ifname :
2680 sm->ifname,
2681 sm->own_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682 ETH_P_80211_ENCAP, wpa_supplicant_rx_tdls,
2683 sm, 0);
2684 if (sm->l2_tdls == NULL) {
2685 wpa_printf(MSG_ERROR, "TDLS: Failed to open l2_packet "
2686 "connection");
2687 return -1;
2688 }
2689
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002690 /*
2691 * Drivers that support TDLS but don't implement the get_capa callback
2692 * are assumed to perform everything internally
2693 */
2694 if (wpa_sm_tdls_get_capa(sm, &sm->tdls_supported,
2695 &sm->tdls_external_setup) < 0) {
2696 sm->tdls_supported = 1;
2697 sm->tdls_external_setup = 0;
2698 }
2699
2700 wpa_printf(MSG_DEBUG, "TDLS: TDLS operation%s supported by "
2701 "driver", sm->tdls_supported ? "" : " not");
2702 wpa_printf(MSG_DEBUG, "TDLS: Driver uses %s link setup",
2703 sm->tdls_external_setup ? "external" : "internal");
2704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705 return 0;
2706}
2707
2708
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002709void wpa_tdls_teardown_peers(struct wpa_sm *sm)
2710{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002711 struct wpa_tdls_peer *peer, *tmp;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002712
2713 peer = sm->tdls;
2714
2715 wpa_printf(MSG_DEBUG, "TDLS: Tear down peers");
2716
2717 while (peer) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002718 tmp = peer->next;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002719 wpa_printf(MSG_DEBUG, "TDLS: Tear down peer " MACSTR,
2720 MAC2STR(peer->addr));
2721 if (sm->tdls_external_setup)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002722 wpa_tdls_do_teardown(sm, peer,
2723 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002724 else
2725 wpa_sm_tdls_oper(sm, TDLS_TEARDOWN, peer->addr);
2726
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002727 peer = tmp;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002728 }
2729}
2730
2731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732static void wpa_tdls_remove_peers(struct wpa_sm *sm)
2733{
2734 struct wpa_tdls_peer *peer, *tmp;
2735
2736 peer = sm->tdls;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737
2738 while (peer) {
2739 int res;
2740 tmp = peer->next;
2741 res = wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
2742 wpa_printf(MSG_DEBUG, "TDLS: Remove peer " MACSTR " (res=%d)",
2743 MAC2STR(peer->addr), res);
2744 wpa_tdls_peer_free(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002745 peer = tmp;
2746 }
2747}
2748
2749
2750/**
2751 * wpa_tdls_deinit - Deinitialize driver interface parameters for TDLS
2752 *
2753 * This function is called to recover driver interface parameters for TDLS
2754 * and frees resources allocated for it.
2755 */
2756void wpa_tdls_deinit(struct wpa_sm *sm)
2757{
2758 if (sm == NULL)
2759 return;
2760
2761 if (sm->l2_tdls)
2762 l2_packet_deinit(sm->l2_tdls);
2763 sm->l2_tdls = NULL;
2764
2765 wpa_tdls_remove_peers(sm);
2766}
2767
2768
2769void wpa_tdls_assoc(struct wpa_sm *sm)
2770{
2771 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on association");
2772 wpa_tdls_remove_peers(sm);
2773}
2774
2775
2776void wpa_tdls_disassoc(struct wpa_sm *sm)
2777{
2778 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on disassociation");
2779 wpa_tdls_remove_peers(sm);
2780}
2781
2782
2783static int wpa_tdls_prohibited(const u8 *ies, size_t len)
2784{
2785 struct wpa_eapol_ie_parse elems;
2786
2787 if (ies == NULL)
2788 return 0;
2789
2790 if (wpa_supplicant_parse_ies(ies, len, &elems) < 0)
2791 return 0;
2792
2793 if (elems.ext_capab == NULL || elems.ext_capab_len < 2 + 5)
2794 return 0;
2795
2796 /* bit 38 - TDLS Prohibited */
2797 return !!(elems.ext_capab[2 + 4] & 0x40);
2798}
2799
2800
2801void wpa_tdls_ap_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2802{
2803 sm->tdls_prohibited = wpa_tdls_prohibited(ies, len);
2804 wpa_printf(MSG_DEBUG, "TDLS: TDLS is %s in the target BSS",
2805 sm->tdls_prohibited ? "prohibited" : "allowed");
2806}
2807
2808
2809void wpa_tdls_assoc_resp_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2810{
2811 if (!sm->tdls_prohibited && wpa_tdls_prohibited(ies, len)) {
2812 wpa_printf(MSG_DEBUG, "TDLS: TDLS prohibited based on "
2813 "(Re)Association Response IEs");
2814 sm->tdls_prohibited = 1;
2815 }
2816}
2817
2818
2819void wpa_tdls_enable(struct wpa_sm *sm, int enabled)
2820{
2821 wpa_printf(MSG_DEBUG, "TDLS: %s", enabled ? "enabled" : "disabled");
2822 sm->tdls_disabled = !enabled;
2823}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002824
2825
2826int wpa_tdls_is_external_setup(struct wpa_sm *sm)
2827{
2828 return sm->tdls_external_setup;
2829}