blob: 74c83fa976a2b57fdb3301e7e36c498784890565 [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)
36unsigned int tdls_testing = 0;
37#endif /* CONFIG_TDLS_TESTING */
38
39#define TPK_LIFETIME 43200 /* 12 hours */
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070040#define TPK_M1_RETRY_COUNT 3
41#define TPK_M1_TIMEOUT 5000 /* in milliseconds */
42#define TPK_M2_RETRY_COUNT 10
43#define TPK_M2_TIMEOUT 500 /* in milliseconds */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
45#define TDLS_MIC_LEN 16
46
47#define TDLS_TIMEOUT_LEN 4
48
49struct wpa_tdls_ftie {
50 u8 ie_type; /* FTIE */
51 u8 ie_len;
52 u8 mic_ctrl[2];
53 u8 mic[TDLS_MIC_LEN];
54 u8 Anonce[WPA_NONCE_LEN]; /* Responder Nonce in TDLS */
55 u8 Snonce[WPA_NONCE_LEN]; /* Initiator Nonce in TDLS */
56 /* followed by optional elements */
57} STRUCT_PACKED;
58
59struct wpa_tdls_timeoutie {
60 u8 ie_type; /* Timeout IE */
61 u8 ie_len;
62 u8 interval_type;
63 u8 value[TDLS_TIMEOUT_LEN];
64} STRUCT_PACKED;
65
66struct wpa_tdls_lnkid {
67 u8 ie_type; /* Link Identifier IE */
68 u8 ie_len;
69 u8 bssid[ETH_ALEN];
70 u8 init_sta[ETH_ALEN];
71 u8 resp_sta[ETH_ALEN];
72} STRUCT_PACKED;
73
74/* TDLS frame headers as per IEEE Std 802.11z-2010 */
75struct wpa_tdls_frame {
76 u8 payloadtype; /* IEEE80211_TDLS_RFTYPE */
77 u8 category; /* Category */
78 u8 action; /* Action (enum tdls_frame_type) */
79} STRUCT_PACKED;
80
81static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs);
82static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx);
83static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer);
84
85
86#define TDLS_MAX_IE_LEN 80
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080087#define IEEE80211_MAX_SUPP_RATES 32
88
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089struct wpa_tdls_peer {
90 struct wpa_tdls_peer *next;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070091 unsigned int reconfig_key:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092 int initiator; /* whether this end was initiator for TDLS setup */
93 u8 addr[ETH_ALEN]; /* other end MAC address */
94 u8 inonce[WPA_NONCE_LEN]; /* Initiator Nonce */
95 u8 rnonce[WPA_NONCE_LEN]; /* Responder Nonce */
96 u8 rsnie_i[TDLS_MAX_IE_LEN]; /* Initiator RSN IE */
97 size_t rsnie_i_len;
98 u8 rsnie_p[TDLS_MAX_IE_LEN]; /* Peer RSN IE */
99 size_t rsnie_p_len;
100 u32 lifetime;
101 int cipher; /* Selected cipher (WPA_CIPHER_*) */
102 u8 dtoken;
103
104 struct tpk {
105 u8 kck[16]; /* TPK-KCK */
106 u8 tk[16]; /* TPK-TK; assuming only CCMP will be used */
107 } tpk;
108 int tpk_set;
109 int tpk_success;
110
111 struct tpk_timer {
112 u8 dest[ETH_ALEN];
113 int count; /* Retry Count */
114 int timer; /* Timeout in milliseconds */
115 u8 action_code; /* TDLS frame type */
116 u8 dialog_token;
117 u16 status_code;
118 int buf_len; /* length of TPK message for retransmission */
119 u8 *buf; /* buffer for TPK message */
120 } sm_tmr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800121
122 u16 capability;
123
124 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
125 size_t supp_rates_len;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800126
127 struct ieee80211_ht_capabilities *ht_capabilities;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800128 struct ieee80211_vht_capabilities *vht_capabilities;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800129
130 u8 qos_info;
131
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700132 u16 aid;
133
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800134 u8 *ext_capab;
135 size_t ext_capab_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136};
137
138
139static int wpa_tdls_get_privacy(struct wpa_sm *sm)
140{
141 /*
142 * Get info needed from supplicant to check if the current BSS supports
143 * security. Other than OPEN mode, rest are considered secured
144 * WEP/WPA/WPA2 hence TDLS frames are processed for TPK handshake.
145 */
146 return sm->pairwise_cipher != WPA_CIPHER_NONE;
147}
148
149
150static u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
151{
152 os_memcpy(pos, ie, ie_len);
153 return pos + ie_len;
154}
155
156
157static int wpa_tdls_del_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
158{
159 if (wpa_sm_set_key(sm, WPA_ALG_NONE, peer->addr,
160 0, 0, NULL, 0, NULL, 0) < 0) {
161 wpa_printf(MSG_WARNING, "TDLS: Failed to delete TPK-TK from "
162 "the driver");
163 return -1;
164 }
165
166 return 0;
167}
168
169
170static int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
171{
172 u8 key_len;
173 u8 rsc[6];
174 enum wpa_alg alg;
175
176 os_memset(rsc, 0, 6);
177
178 switch (peer->cipher) {
179 case WPA_CIPHER_CCMP:
180 alg = WPA_ALG_CCMP;
181 key_len = 16;
182 break;
183 case WPA_CIPHER_NONE:
184 wpa_printf(MSG_DEBUG, "TDLS: Pairwise Cipher Suite: "
185 "NONE - do not use pairwise keys");
186 return -1;
187 default:
188 wpa_printf(MSG_WARNING, "TDLS: Unsupported pairwise cipher %d",
189 sm->pairwise_cipher);
190 return -1;
191 }
192
193 if (wpa_sm_set_key(sm, alg, peer->addr, -1, 1,
194 rsc, sizeof(rsc), peer->tpk.tk, key_len) < 0) {
195 wpa_printf(MSG_WARNING, "TDLS: Failed to set TPK to the "
196 "driver");
197 return -1;
198 }
199 return 0;
200}
201
202
203static int wpa_tdls_send_tpk_msg(struct wpa_sm *sm, const u8 *dst,
204 u8 action_code, u8 dialog_token,
205 u16 status_code, const u8 *buf, size_t len)
206{
207 return wpa_sm_send_tdls_mgmt(sm, dst, action_code, dialog_token,
208 status_code, buf, len);
209}
210
211
212static int wpa_tdls_tpk_send(struct wpa_sm *sm, const u8 *dest, u8 action_code,
213 u8 dialog_token, u16 status_code,
214 const u8 *msg, size_t msg_len)
215{
216 struct wpa_tdls_peer *peer;
217
218 wpa_printf(MSG_DEBUG, "TDLS: TPK send dest=" MACSTR " action_code=%u "
219 "dialog_token=%u status_code=%u msg_len=%u",
220 MAC2STR(dest), action_code, dialog_token, status_code,
221 (unsigned int) msg_len);
222
223 if (wpa_tdls_send_tpk_msg(sm, dest, action_code, dialog_token,
224 status_code, msg, msg_len)) {
225 wpa_printf(MSG_INFO, "TDLS: Failed to send message "
226 "(action_code=%u)", action_code);
227 return -1;
228 }
229
230 if (action_code == WLAN_TDLS_SETUP_CONFIRM ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800231 action_code == WLAN_TDLS_TEARDOWN ||
232 action_code == WLAN_TDLS_DISCOVERY_REQUEST ||
233 action_code == WLAN_TDLS_DISCOVERY_RESPONSE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 return 0; /* No retries */
235
236 for (peer = sm->tdls; peer; peer = peer->next) {
237 if (os_memcmp(peer->addr, dest, ETH_ALEN) == 0)
238 break;
239 }
240
241 if (peer == NULL) {
242 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
243 "retry " MACSTR, MAC2STR(dest));
244 return 0;
245 }
246
247 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
248
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700249 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
250 peer->sm_tmr.count = TPK_M2_RETRY_COUNT;
251 peer->sm_tmr.timer = TPK_M2_TIMEOUT;
252 } else {
253 peer->sm_tmr.count = TPK_M1_RETRY_COUNT;
254 peer->sm_tmr.timer = TPK_M1_TIMEOUT;
255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256
257 /* Copy message to resend on timeout */
258 os_memcpy(peer->sm_tmr.dest, dest, ETH_ALEN);
259 peer->sm_tmr.action_code = action_code;
260 peer->sm_tmr.dialog_token = dialog_token;
261 peer->sm_tmr.status_code = status_code;
262 peer->sm_tmr.buf_len = msg_len;
263 os_free(peer->sm_tmr.buf);
264 peer->sm_tmr.buf = os_malloc(msg_len);
265 if (peer->sm_tmr.buf == NULL)
266 return -1;
267 os_memcpy(peer->sm_tmr.buf, msg, msg_len);
268
269 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout registered "
270 "(action_code=%u)", action_code);
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700271 eloop_register_timeout(peer->sm_tmr.timer / 1000,
272 (peer->sm_tmr.timer % 1000) * 1000,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273 wpa_tdls_tpk_retry_timeout, sm, peer);
274 return 0;
275}
276
277
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800278static int wpa_tdls_do_teardown(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300279 u16 reason_code)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800280{
281 int ret;
282
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300283 ret = wpa_tdls_send_teardown(sm, peer->addr, reason_code);
284 /* disable the link after teardown was sent */
285 wpa_tdls_disable_link(sm, peer->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800286
287 return ret;
288}
289
290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx)
292{
293
294 struct wpa_sm *sm = eloop_ctx;
295 struct wpa_tdls_peer *peer = timeout_ctx;
296
297 if (peer->sm_tmr.count) {
298 peer->sm_tmr.count--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299
300 wpa_printf(MSG_INFO, "TDLS: Retrying sending of message "
301 "(action_code=%u)",
302 peer->sm_tmr.action_code);
303
304 if (peer->sm_tmr.buf == NULL) {
305 wpa_printf(MSG_INFO, "TDLS: No retry buffer available "
306 "for action_code=%u",
307 peer->sm_tmr.action_code);
308 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm,
309 peer);
310 return;
311 }
312
313 /* resend TPK Handshake Message to Peer */
314 if (wpa_tdls_send_tpk_msg(sm, peer->sm_tmr.dest,
315 peer->sm_tmr.action_code,
316 peer->sm_tmr.dialog_token,
317 peer->sm_tmr.status_code,
318 peer->sm_tmr.buf,
319 peer->sm_tmr.buf_len)) {
320 wpa_printf(MSG_INFO, "TDLS: Failed to retry "
321 "transmission");
322 }
323
324 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700325 eloop_register_timeout(peer->sm_tmr.timer / 1000,
326 (peer->sm_tmr.timer % 1000) * 1000,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 wpa_tdls_tpk_retry_timeout, sm, peer);
328 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800331 wpa_printf(MSG_DEBUG, "TDLS: Sending Teardown Request");
332 wpa_tdls_do_teardown(sm, peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300333 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334 }
335}
336
337
338static void wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm *sm,
339 struct wpa_tdls_peer *peer,
340 u8 action_code)
341{
342 if (action_code == peer->sm_tmr.action_code) {
343 wpa_printf(MSG_DEBUG, "TDLS: Retry timeout cancelled for "
344 "action_code=%u", action_code);
345
346 /* Cancel Timeout registered */
347 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
348
349 /* free all resources meant for retry */
350 os_free(peer->sm_tmr.buf);
351 peer->sm_tmr.buf = NULL;
352
353 peer->sm_tmr.count = 0;
354 peer->sm_tmr.timer = 0;
355 peer->sm_tmr.buf_len = 0;
356 peer->sm_tmr.action_code = 0xff;
357 } else {
358 wpa_printf(MSG_INFO, "TDLS: Error in cancelling retry timeout "
359 "(Unknown action_code=%u)", action_code);
360 }
361}
362
363
364static void wpa_tdls_generate_tpk(struct wpa_tdls_peer *peer,
365 const u8 *own_addr, const u8 *bssid)
366{
367 u8 key_input[SHA256_MAC_LEN];
368 const u8 *nonce[2];
369 size_t len[2];
370 u8 data[3 * ETH_ALEN];
371
372 /* IEEE Std 802.11z-2010 8.5.9.1:
373 * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
374 */
375 len[0] = WPA_NONCE_LEN;
376 len[1] = WPA_NONCE_LEN;
377 if (os_memcmp(peer->inonce, peer->rnonce, WPA_NONCE_LEN) < 0) {
378 nonce[0] = peer->inonce;
379 nonce[1] = peer->rnonce;
380 } else {
381 nonce[0] = peer->rnonce;
382 nonce[1] = peer->inonce;
383 }
384 wpa_hexdump(MSG_DEBUG, "TDLS: min(Nonce)", nonce[0], WPA_NONCE_LEN);
385 wpa_hexdump(MSG_DEBUG, "TDLS: max(Nonce)", nonce[1], WPA_NONCE_LEN);
386 sha256_vector(2, nonce, len, key_input);
387 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-Key-Input",
388 key_input, SHA256_MAC_LEN);
389
390 /*
391 * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
392 * min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
393 * TODO: is N_KEY really included in KDF Context and if so, in which
394 * presentation format (little endian 16-bit?) is it used? It gets
395 * added by the KDF anyway..
396 */
397
398 if (os_memcmp(own_addr, peer->addr, ETH_ALEN) < 0) {
399 os_memcpy(data, own_addr, ETH_ALEN);
400 os_memcpy(data + ETH_ALEN, peer->addr, ETH_ALEN);
401 } else {
402 os_memcpy(data, peer->addr, ETH_ALEN);
403 os_memcpy(data + ETH_ALEN, own_addr, ETH_ALEN);
404 }
405 os_memcpy(data + 2 * ETH_ALEN, bssid, ETH_ALEN);
406 wpa_hexdump(MSG_DEBUG, "TDLS: KDF Context", data, sizeof(data));
407
408 sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data),
409 (u8 *) &peer->tpk, sizeof(peer->tpk));
410 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-KCK",
411 peer->tpk.kck, sizeof(peer->tpk.kck));
412 wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-TK",
413 peer->tpk.tk, sizeof(peer->tpk.tk));
414 peer->tpk_set = 1;
415}
416
417
418/**
419 * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
420 * @kck: TPK-KCK
421 * @lnkid: Pointer to the beginning of Link Identifier IE
422 * @rsnie: Pointer to the beginning of RSN IE used for handshake
423 * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
424 * @ftie: Pointer to the beginning of FT IE
425 * @mic: Pointer for writing MIC
426 *
427 * Calculate MIC for TDLS frame.
428 */
429static int wpa_tdls_ftie_mic(const u8 *kck, u8 trans_seq, const u8 *lnkid,
430 const u8 *rsnie, const u8 *timeoutie,
431 const u8 *ftie, u8 *mic)
432{
433 u8 *buf, *pos;
434 struct wpa_tdls_ftie *_ftie;
435 const struct wpa_tdls_lnkid *_lnkid;
436 int ret;
437 int len = 2 * ETH_ALEN + 1 + 2 + lnkid[1] + 2 + rsnie[1] +
438 2 + timeoutie[1] + 2 + ftie[1];
439 buf = os_zalloc(len);
440 if (!buf) {
441 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
442 return -1;
443 }
444
445 pos = buf;
446 _lnkid = (const struct wpa_tdls_lnkid *) lnkid;
447 /* 1) TDLS initiator STA MAC address */
448 os_memcpy(pos, _lnkid->init_sta, ETH_ALEN);
449 pos += ETH_ALEN;
450 /* 2) TDLS responder STA MAC address */
451 os_memcpy(pos, _lnkid->resp_sta, ETH_ALEN);
452 pos += ETH_ALEN;
453 /* 3) Transaction Sequence number */
454 *pos++ = trans_seq;
455 /* 4) Link Identifier IE */
456 os_memcpy(pos, lnkid, 2 + lnkid[1]);
457 pos += 2 + lnkid[1];
458 /* 5) RSN IE */
459 os_memcpy(pos, rsnie, 2 + rsnie[1]);
460 pos += 2 + rsnie[1];
461 /* 6) Timeout Interval IE */
462 os_memcpy(pos, timeoutie, 2 + timeoutie[1]);
463 pos += 2 + timeoutie[1];
464 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
465 os_memcpy(pos, ftie, 2 + ftie[1]);
466 _ftie = (struct wpa_tdls_ftie *) pos;
467 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
468 pos += 2 + ftie[1];
469
470 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
471 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
472 ret = omac1_aes_128(kck, buf, pos - buf, mic);
473 os_free(buf);
474 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
475 return ret;
476}
477
478
479/**
480 * wpa_tdls_key_mic_teardown - Calculate TDLS FTIE MIC for Teardown frame
481 * @kck: TPK-KCK
482 * @trans_seq: Transaction Sequence Number (4 - Teardown)
483 * @rcode: Reason code for Teardown
484 * @dtoken: Dialog Token used for that particular link
485 * @lnkid: Pointer to the beginning of Link Identifier IE
486 * @ftie: Pointer to the beginning of FT IE
487 * @mic: Pointer for writing MIC
488 *
489 * Calculate MIC for TDLS frame.
490 */
491static int wpa_tdls_key_mic_teardown(const u8 *kck, u8 trans_seq, u16 rcode,
492 u8 dtoken, const u8 *lnkid,
493 const u8 *ftie, u8 *mic)
494{
495 u8 *buf, *pos;
496 struct wpa_tdls_ftie *_ftie;
497 int ret;
498 int len;
499
500 if (lnkid == NULL)
501 return -1;
502
503 len = 2 + lnkid[1] + sizeof(rcode) + sizeof(dtoken) +
504 sizeof(trans_seq) + 2 + ftie[1];
505
506 buf = os_zalloc(len);
507 if (!buf) {
508 wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
509 return -1;
510 }
511
512 pos = buf;
513 /* 1) Link Identifier IE */
514 os_memcpy(pos, lnkid, 2 + lnkid[1]);
515 pos += 2 + lnkid[1];
516 /* 2) Reason Code */
517 WPA_PUT_LE16(pos, rcode);
518 pos += sizeof(rcode);
519 /* 3) Dialog token */
520 *pos++ = dtoken;
521 /* 4) Transaction Sequence number */
522 *pos++ = trans_seq;
523 /* 7) FTIE, with the MIC field of the FTIE set to 0 */
524 os_memcpy(pos, ftie, 2 + ftie[1]);
525 _ftie = (struct wpa_tdls_ftie *) pos;
526 os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
527 pos += 2 + ftie[1];
528
529 wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
530 wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
531 ret = omac1_aes_128(kck, buf, pos - buf, mic);
532 os_free(buf);
533 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
534 return ret;
535}
536
537
538static int wpa_supplicant_verify_tdls_mic(u8 trans_seq,
539 struct wpa_tdls_peer *peer,
540 const u8 *lnkid, const u8 *timeoutie,
541 const struct wpa_tdls_ftie *ftie)
542{
543 u8 mic[16];
544
545 if (peer->tpk_set) {
546 wpa_tdls_ftie_mic(peer->tpk.kck, trans_seq, lnkid,
547 peer->rsnie_p, timeoutie, (u8 *) ftie,
548 mic);
549 if (os_memcmp(mic, ftie->mic, 16) != 0) {
550 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in FTIE - "
551 "dropping packet");
552 wpa_hexdump(MSG_DEBUG, "TDLS: Received MIC",
553 ftie->mic, 16);
554 wpa_hexdump(MSG_DEBUG, "TDLS: Calculated MIC",
555 mic, 16);
556 return -1;
557 }
558 } else {
559 wpa_printf(MSG_WARNING, "TDLS: Could not verify TDLS MIC, "
560 "TPK not set - dropping packet");
561 return -1;
562 }
563 return 0;
564}
565
566
567static int wpa_supplicant_verify_tdls_mic_teardown(
568 u8 trans_seq, u16 rcode, u8 dtoken, struct wpa_tdls_peer *peer,
569 const u8 *lnkid, const struct wpa_tdls_ftie *ftie)
570{
571 u8 mic[16];
572
573 if (peer->tpk_set) {
574 wpa_tdls_key_mic_teardown(peer->tpk.kck, trans_seq, rcode,
575 dtoken, lnkid, (u8 *) ftie, mic);
576 if (os_memcmp(mic, ftie->mic, 16) != 0) {
577 wpa_printf(MSG_INFO, "TDLS: Invalid MIC in Teardown - "
578 "dropping packet");
579 return -1;
580 }
581 } else {
582 wpa_printf(MSG_INFO, "TDLS: Could not verify TDLS Teardown "
583 "MIC, TPK not set - dropping packet");
584 return -1;
585 }
586 return 0;
587}
588
589
590static void wpa_tdls_tpk_timeout(void *eloop_ctx, void *timeout_ctx)
591{
592 struct wpa_sm *sm = eloop_ctx;
593 struct wpa_tdls_peer *peer = timeout_ctx;
594
595 /*
596 * On TPK lifetime expiration, we have an option of either tearing down
597 * the direct link or trying to re-initiate it. The selection of what
598 * to do is not strictly speaking controlled by our role in the expired
599 * link, but for now, use that to select whether to renew or tear down
600 * the link.
601 */
602
603 if (peer->initiator) {
604 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
605 " - try to renew", MAC2STR(peer->addr));
606 wpa_tdls_start(sm, peer->addr);
607 } else {
608 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
609 " - tear down", MAC2STR(peer->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800610 wpa_tdls_do_teardown(sm, peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300611 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 }
613}
614
615
616static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
617{
618 wpa_printf(MSG_DEBUG, "TDLS: Clear state for peer " MACSTR,
619 MAC2STR(peer->addr));
620 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
621 eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700622 peer->reconfig_key = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 peer->initiator = 0;
624 os_free(peer->sm_tmr.buf);
625 peer->sm_tmr.buf = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800626 os_free(peer->ht_capabilities);
627 peer->ht_capabilities = NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800628 os_free(peer->vht_capabilities);
629 peer->vht_capabilities = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800630 os_free(peer->ext_capab);
631 peer->ext_capab = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632 peer->rsnie_i_len = peer->rsnie_p_len = 0;
633 peer->cipher = 0;
634 peer->tpk_set = peer->tpk_success = 0;
635 os_memset(&peer->tpk, 0, sizeof(peer->tpk));
636 os_memset(peer->inonce, 0, WPA_NONCE_LEN);
637 os_memset(peer->rnonce, 0, WPA_NONCE_LEN);
638}
639
640
641static void wpa_tdls_linkid(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
642 struct wpa_tdls_lnkid *lnkid)
643{
644 lnkid->ie_type = WLAN_EID_LINK_ID;
645 lnkid->ie_len = 3 * ETH_ALEN;
646 os_memcpy(lnkid->bssid, sm->bssid, ETH_ALEN);
647 if (peer->initiator) {
648 os_memcpy(lnkid->init_sta, sm->own_addr, ETH_ALEN);
649 os_memcpy(lnkid->resp_sta, peer->addr, ETH_ALEN);
650 } else {
651 os_memcpy(lnkid->init_sta, peer->addr, ETH_ALEN);
652 os_memcpy(lnkid->resp_sta, sm->own_addr, ETH_ALEN);
653 }
654}
655
656
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800657int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700658{
659 struct wpa_tdls_peer *peer;
660 struct wpa_tdls_ftie *ftie;
661 struct wpa_tdls_lnkid lnkid;
662 u8 dialog_token;
663 u8 *rbuf, *pos;
664 int ielen;
665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800666 if (sm->tdls_disabled || !sm->tdls_supported)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 return -1;
668
669 /* Find the node and free from the list */
670 for (peer = sm->tdls; peer; peer = peer->next) {
671 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
672 break;
673 }
674
675 if (peer == NULL) {
676 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
677 "Teardown " MACSTR, MAC2STR(addr));
678 return 0;
679 }
680
681 dialog_token = peer->dtoken;
682
683 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown for " MACSTR,
684 MAC2STR(addr));
685
686 ielen = 0;
687 if (wpa_tdls_get_privacy(sm) && peer->tpk_set && peer->tpk_success) {
688 /* To add FTIE for Teardown request and compute MIC */
689 ielen += sizeof(*ftie);
690#ifdef CONFIG_TDLS_TESTING
691 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
692 ielen += 170;
693#endif /* CONFIG_TDLS_TESTING */
694 }
695
696 rbuf = os_zalloc(ielen + 1);
697 if (rbuf == NULL)
698 return -1;
699 pos = rbuf;
700
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700701 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702 goto skip_ies;
703
704 ftie = (struct wpa_tdls_ftie *) pos;
705 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
706 /* Using the recent nonce which should be for CONFIRM frame */
707 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
708 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
709 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
710 pos = (u8 *) (ftie + 1);
711#ifdef CONFIG_TDLS_TESTING
712 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
713 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
714 "FTIE");
715 ftie->ie_len += 170;
716 *pos++ = 255; /* FTIE subelem */
717 *pos++ = 168; /* FTIE subelem length */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800718 pos += 168;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719 }
720#endif /* CONFIG_TDLS_TESTING */
721 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TDLS Teardown handshake",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800722 (u8 *) ftie, pos - (u8 *) ftie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723
724 /* compute MIC before sending */
725 wpa_tdls_linkid(sm, peer, &lnkid);
726 wpa_tdls_key_mic_teardown(peer->tpk.kck, 4, reason_code,
727 dialog_token, (u8 *) &lnkid, (u8 *) ftie,
728 ftie->mic);
729
730skip_ies:
731 /* TODO: register for a Timeout handler, if Teardown is not received at
732 * the other end, then try again another time */
733
734 /* request driver to send Teardown using this FTIE */
735 wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_TEARDOWN, 0,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800736 reason_code, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 os_free(rbuf);
738
739 /* clear the Peerkey statemachine */
740 wpa_tdls_peer_free(sm, peer);
741
742 return 0;
743}
744
745
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800746int wpa_tdls_teardown_link(struct wpa_sm *sm, const u8 *addr, u16 reason_code)
747{
748 struct wpa_tdls_peer *peer;
749
750 if (sm->tdls_disabled || !sm->tdls_supported)
751 return -1;
752
753 for (peer = sm->tdls; peer; peer = peer->next) {
754 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
755 break;
756 }
757
758 if (peer == NULL) {
759 wpa_printf(MSG_DEBUG, "TDLS: Could not find peer " MACSTR
760 " for link Teardown", MAC2STR(addr));
761 return -1;
762 }
763
764 if (!peer->tpk_success) {
765 wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
766 " not connected - cannot Teardown link", MAC2STR(addr));
767 return -1;
768 }
769
Sunil Dutt6a9f5222013-09-30 17:10:18 +0300770 return wpa_tdls_do_teardown(sm, peer, reason_code);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800771}
772
773
Sunil Dutt38ffd882013-09-30 17:23:23 +0300774static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
775 struct wpa_tdls_peer *peer)
776{
777 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
778 wpa_tdls_peer_free(sm, peer);
779}
780
781
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782void wpa_tdls_disable_link(struct wpa_sm *sm, const u8 *addr)
783{
784 struct wpa_tdls_peer *peer;
785
786 for (peer = sm->tdls; peer; peer = peer->next) {
787 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
788 break;
789 }
790
Sunil Dutt38ffd882013-09-30 17:23:23 +0300791 if (peer)
792 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800793}
794
795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796static int wpa_tdls_recv_teardown(struct wpa_sm *sm, const u8 *src_addr,
797 const u8 *buf, size_t len)
798{
799 struct wpa_tdls_peer *peer = NULL;
800 struct wpa_tdls_ftie *ftie;
801 struct wpa_tdls_lnkid *lnkid;
802 struct wpa_eapol_ie_parse kde;
803 u16 reason_code;
804 const u8 *pos;
805 int ielen;
806
807 /* Find the node and free from the list */
808 for (peer = sm->tdls; peer; peer = peer->next) {
809 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
810 break;
811 }
812
813 if (peer == NULL) {
814 wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
815 "Teardown " MACSTR, MAC2STR(src_addr));
816 return 0;
817 }
818
819 pos = buf;
820 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
821
822 reason_code = WPA_GET_LE16(pos);
823 pos += 2;
824
825 wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown Request from " MACSTR
826 " (reason code %u)", MAC2STR(src_addr), reason_code);
827
828 ielen = len - (pos - buf); /* start of IE in buf */
829 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
830 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in Teardown");
831 return -1;
832 }
833
834 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
835 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TDLS "
836 "Teardown");
837 return -1;
838 }
839 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
840
841 if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
842 goto skip_ftie;
843
844 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
845 wpa_printf(MSG_INFO, "TDLS: No FTIE in TDLS Teardown");
846 return -1;
847 }
848
849 ftie = (struct wpa_tdls_ftie *) kde.ftie;
850
851 /* Process MIC check to see if TDLS Teardown is right */
852 if (wpa_supplicant_verify_tdls_mic_teardown(4, reason_code,
853 peer->dtoken, peer,
854 (u8 *) lnkid, ftie) < 0) {
855 wpa_printf(MSG_DEBUG, "TDLS: MIC failure for TDLS "
856 "Teardown Request from " MACSTR, MAC2STR(src_addr));
857 return -1;
858 }
859
860skip_ftie:
861 /*
862 * Request the driver to disable the direct link and clear associated
863 * keys.
864 */
Sunil Dutt38ffd882013-09-30 17:23:23 +0300865 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 return 0;
867}
868
869
870/**
871 * wpa_tdls_send_error - To send suitable TDLS status response with
872 * appropriate status code mentioning reason for error/failure.
873 * @dst - MAC addr of Peer station
874 * @tdls_action - TDLS frame type for which error code is sent
875 * @status - status code mentioning reason
876 */
877
878static int wpa_tdls_send_error(struct wpa_sm *sm, const u8 *dst,
879 u8 tdls_action, u8 dialog_token, u16 status)
880{
881 wpa_printf(MSG_DEBUG, "TDLS: Sending error to " MACSTR
882 " (action=%u status=%u)",
883 MAC2STR(dst), tdls_action, status);
884 return wpa_tdls_tpk_send(sm, dst, tdls_action, dialog_token, status,
885 NULL, 0);
886}
887
888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800889static struct wpa_tdls_peer *
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800890wpa_tdls_add_peer(struct wpa_sm *sm, const u8 *addr, int *existing)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800891{
892 struct wpa_tdls_peer *peer;
893
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800894 if (existing)
895 *existing = 0;
896 for (peer = sm->tdls; peer; peer = peer->next) {
897 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0) {
898 if (existing)
899 *existing = 1;
900 return peer; /* re-use existing entry */
901 }
902 }
903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800904 wpa_printf(MSG_INFO, "TDLS: Creating peer entry for " MACSTR,
905 MAC2STR(addr));
906
907 peer = os_zalloc(sizeof(*peer));
908 if (peer == NULL)
909 return NULL;
910
911 os_memcpy(peer->addr, addr, ETH_ALEN);
912 peer->next = sm->tdls;
913 sm->tdls = peer;
914
915 return peer;
916}
917
918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919static int wpa_tdls_send_tpk_m1(struct wpa_sm *sm,
920 struct wpa_tdls_peer *peer)
921{
922 size_t buf_len;
923 struct wpa_tdls_timeoutie timeoutie;
924 u16 rsn_capab;
925 struct wpa_tdls_ftie *ftie;
926 u8 *rbuf, *pos, *count_pos;
927 u16 count;
928 struct rsn_ie_hdr *hdr;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -0700929 int status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930
931 if (!wpa_tdls_get_privacy(sm)) {
932 wpa_printf(MSG_DEBUG, "TDLS: No security used on the link");
933 peer->rsnie_i_len = 0;
934 goto skip_rsnie;
935 }
936
937 /*
938 * TPK Handshake Message 1:
939 * FTIE: ANonce=0, SNonce=initiator nonce MIC=0, DataKDs=(RSNIE_I,
940 * Timeout Interval IE))
941 */
942
943 /* Filling RSN IE */
944 hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
945 hdr->elem_id = WLAN_EID_RSN;
946 WPA_PUT_LE16(hdr->version, RSN_VERSION);
947
948 pos = (u8 *) (hdr + 1);
949 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
950 pos += RSN_SELECTOR_LEN;
951 count_pos = pos;
952 pos += 2;
953
954 count = 0;
955
956 /*
957 * AES-CCMP is the default Encryption preferred for TDLS, so
958 * RSN IE is filled only with CCMP CIPHER
959 * Note: TKIP is not used to encrypt TDLS link.
960 *
961 * Regardless of the cipher used on the AP connection, select CCMP
962 * here.
963 */
964 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
965 pos += RSN_SELECTOR_LEN;
966 count++;
967
968 WPA_PUT_LE16(count_pos, count);
969
970 WPA_PUT_LE16(pos, 1);
971 pos += 2;
972 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
973 pos += RSN_SELECTOR_LEN;
974
975 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
976 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
977#ifdef CONFIG_TDLS_TESTING
978 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
979 wpa_printf(MSG_DEBUG, "TDLS: Use alternative RSN IE for "
980 "testing");
981 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
982 }
983#endif /* CONFIG_TDLS_TESTING */
984 WPA_PUT_LE16(pos, rsn_capab);
985 pos += 2;
986#ifdef CONFIG_TDLS_TESTING
987 if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
988 /* Number of PMKIDs */
989 *pos++ = 0x00;
990 *pos++ = 0x00;
991 }
992#endif /* CONFIG_TDLS_TESTING */
993
994 hdr->len = (pos - peer->rsnie_i) - 2;
995 peer->rsnie_i_len = pos - peer->rsnie_i;
996 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
997 peer->rsnie_i, peer->rsnie_i_len);
998
999skip_rsnie:
1000 buf_len = 0;
1001 if (wpa_tdls_get_privacy(sm))
1002 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1003 sizeof(struct wpa_tdls_timeoutie);
1004#ifdef CONFIG_TDLS_TESTING
1005 if (wpa_tdls_get_privacy(sm) &&
1006 (tdls_testing & TDLS_TESTING_LONG_FRAME))
1007 buf_len += 170;
1008 if (tdls_testing & TDLS_TESTING_DIFF_BSSID)
1009 buf_len += sizeof(struct wpa_tdls_lnkid);
1010#endif /* CONFIG_TDLS_TESTING */
1011 rbuf = os_zalloc(buf_len + 1);
1012 if (rbuf == NULL) {
1013 wpa_tdls_peer_free(sm, peer);
1014 return -1;
1015 }
1016 pos = rbuf;
1017
1018 if (!wpa_tdls_get_privacy(sm))
1019 goto skip_ies;
1020
1021 /* Initiator RSN IE */
1022 pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
1023
1024 ftie = (struct wpa_tdls_ftie *) pos;
1025 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1026 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1027
1028 if (os_get_random(peer->inonce, WPA_NONCE_LEN)) {
1029 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1030 "TDLS: Failed to get random data for initiator Nonce");
1031 os_free(rbuf);
1032 wpa_tdls_peer_free(sm, peer);
1033 return -1;
1034 }
1035 wpa_hexdump(MSG_DEBUG, "TDLS: Initiator Nonce for TPK handshake",
1036 peer->inonce, WPA_NONCE_LEN);
1037 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1038
1039 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK Handshake M1",
1040 (u8 *) ftie, sizeof(struct wpa_tdls_ftie));
1041
1042 pos = (u8 *) (ftie + 1);
1043
1044#ifdef CONFIG_TDLS_TESTING
1045 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1046 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1047 "FTIE");
1048 ftie->ie_len += 170;
1049 *pos++ = 255; /* FTIE subelem */
1050 *pos++ = 168; /* FTIE subelem length */
1051 pos += 168;
1052 }
1053#endif /* CONFIG_TDLS_TESTING */
1054
1055 /* Lifetime */
1056 peer->lifetime = TPK_LIFETIME;
1057#ifdef CONFIG_TDLS_TESTING
1058 if (tdls_testing & TDLS_TESTING_SHORT_LIFETIME) {
1059 wpa_printf(MSG_DEBUG, "TDLS: Testing - use short TPK "
1060 "lifetime");
1061 peer->lifetime = 301;
1062 }
1063 if (tdls_testing & TDLS_TESTING_LONG_LIFETIME) {
1064 wpa_printf(MSG_DEBUG, "TDLS: Testing - use long TPK "
1065 "lifetime");
1066 peer->lifetime = 0xffffffff;
1067 }
1068#endif /* CONFIG_TDLS_TESTING */
1069 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1070 sizeof(timeoutie), peer->lifetime);
1071 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
1072
1073skip_ies:
1074
1075#ifdef CONFIG_TDLS_TESTING
1076 if (tdls_testing & TDLS_TESTING_DIFF_BSSID) {
1077 wpa_printf(MSG_DEBUG, "TDLS: Testing - use incorrect BSSID in "
1078 "Link Identifier");
1079 struct wpa_tdls_lnkid *l = (struct wpa_tdls_lnkid *) pos;
1080 wpa_tdls_linkid(sm, peer, l);
1081 l->bssid[5] ^= 0x01;
1082 pos += sizeof(*l);
1083 }
1084#endif /* CONFIG_TDLS_TESTING */
1085
1086 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Request / TPK "
1087 "Handshake Message 1 (peer " MACSTR ")",
1088 MAC2STR(peer->addr));
1089
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001090 status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_SETUP_REQUEST,
1091 1, 0, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 os_free(rbuf);
1093
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001094 return status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095}
1096
1097
1098static int wpa_tdls_send_tpk_m2(struct wpa_sm *sm,
1099 const unsigned char *src_addr, u8 dtoken,
1100 struct wpa_tdls_lnkid *lnkid,
1101 const struct wpa_tdls_peer *peer)
1102{
1103 u8 *rbuf, *pos;
1104 size_t buf_len;
1105 u32 lifetime;
1106 struct wpa_tdls_timeoutie timeoutie;
1107 struct wpa_tdls_ftie *ftie;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001108 int status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109
1110 buf_len = 0;
1111 if (wpa_tdls_get_privacy(sm)) {
1112 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1113 * Lifetime */
1114 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1115 sizeof(struct wpa_tdls_timeoutie);
1116#ifdef CONFIG_TDLS_TESTING
1117 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1118 buf_len += 170;
1119#endif /* CONFIG_TDLS_TESTING */
1120 }
1121
1122 rbuf = os_zalloc(buf_len + 1);
1123 if (rbuf == NULL)
1124 return -1;
1125 pos = rbuf;
1126
1127 if (!wpa_tdls_get_privacy(sm))
1128 goto skip_ies;
1129
1130 /* Peer RSN IE */
1131 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1132
1133 ftie = (struct wpa_tdls_ftie *) pos;
1134 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1135 /* TODO: ftie->mic_control to set 2-RESPONSE */
1136 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1137 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1138 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1139 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK M2",
1140 (u8 *) ftie, sizeof(*ftie));
1141
1142 pos = (u8 *) (ftie + 1);
1143
1144#ifdef CONFIG_TDLS_TESTING
1145 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1146 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1147 "FTIE");
1148 ftie->ie_len += 170;
1149 *pos++ = 255; /* FTIE subelem */
1150 *pos++ = 168; /* FTIE subelem length */
1151 pos += 168;
1152 }
1153#endif /* CONFIG_TDLS_TESTING */
1154
1155 /* Lifetime */
1156 lifetime = peer->lifetime;
1157#ifdef CONFIG_TDLS_TESTING
1158 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_RESP) {
1159 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1160 "lifetime in response");
1161 lifetime++;
1162 }
1163#endif /* CONFIG_TDLS_TESTING */
1164 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1165 sizeof(timeoutie), lifetime);
1166 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds from initiator",
1167 lifetime);
1168
1169 /* compute MIC before sending */
1170 wpa_tdls_ftie_mic(peer->tpk.kck, 2, (u8 *) lnkid, peer->rsnie_p,
1171 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
1172
1173skip_ies:
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001174 status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE,
1175 dtoken, 0, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176 os_free(rbuf);
1177
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001178 return status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179}
1180
1181
1182static int wpa_tdls_send_tpk_m3(struct wpa_sm *sm,
1183 const unsigned char *src_addr, u8 dtoken,
1184 struct wpa_tdls_lnkid *lnkid,
1185 const struct wpa_tdls_peer *peer)
1186{
1187 u8 *rbuf, *pos;
1188 size_t buf_len;
1189 struct wpa_tdls_ftie *ftie;
1190 struct wpa_tdls_timeoutie timeoutie;
1191 u32 lifetime;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001192 int status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193
1194 buf_len = 0;
1195 if (wpa_tdls_get_privacy(sm)) {
1196 /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1197 * Lifetime */
1198 buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1199 sizeof(struct wpa_tdls_timeoutie);
1200#ifdef CONFIG_TDLS_TESTING
1201 if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1202 buf_len += 170;
1203#endif /* CONFIG_TDLS_TESTING */
1204 }
1205
1206 rbuf = os_zalloc(buf_len + 1);
1207 if (rbuf == NULL)
1208 return -1;
1209 pos = rbuf;
1210
1211 if (!wpa_tdls_get_privacy(sm))
1212 goto skip_ies;
1213
1214 /* Peer RSN IE */
1215 pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1216
1217 ftie = (struct wpa_tdls_ftie *) pos;
1218 ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1219 /*TODO: ftie->mic_control to set 3-CONFIRM */
1220 os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1221 os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1222 ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1223
1224 pos = (u8 *) (ftie + 1);
1225
1226#ifdef CONFIG_TDLS_TESTING
1227 if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1228 wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1229 "FTIE");
1230 ftie->ie_len += 170;
1231 *pos++ = 255; /* FTIE subelem */
1232 *pos++ = 168; /* FTIE subelem length */
1233 pos += 168;
1234 }
1235#endif /* CONFIG_TDLS_TESTING */
1236
1237 /* Lifetime */
1238 lifetime = peer->lifetime;
1239#ifdef CONFIG_TDLS_TESTING
1240 if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_CONF) {
1241 wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1242 "lifetime in confirm");
1243 lifetime++;
1244 }
1245#endif /* CONFIG_TDLS_TESTING */
1246 pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1247 sizeof(timeoutie), lifetime);
1248 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds",
1249 lifetime);
1250
1251 /* compute MIC before sending */
1252 wpa_tdls_ftie_mic(peer->tpk.kck, 3, (u8 *) lnkid, peer->rsnie_p,
1253 (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
1254
1255skip_ies:
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001256 status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
1257 dtoken, 0, rbuf, pos - rbuf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 os_free(rbuf);
1259
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001260 return status;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261}
1262
1263
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001264static int wpa_tdls_send_discovery_response(struct wpa_sm *sm,
1265 struct wpa_tdls_peer *peer,
1266 u8 dialog_token)
1267{
1268 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Discovery Response "
1269 "(peer " MACSTR ")", MAC2STR(peer->addr));
1270
1271 return wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_DISCOVERY_RESPONSE,
1272 dialog_token, 0, NULL, 0);
1273}
1274
1275
1276static int
1277wpa_tdls_process_discovery_request(struct wpa_sm *sm, const u8 *addr,
1278 const u8 *buf, size_t len)
1279{
1280 struct wpa_eapol_ie_parse kde;
1281 const struct wpa_tdls_lnkid *lnkid;
1282 struct wpa_tdls_peer *peer;
1283 size_t min_req_len = sizeof(struct wpa_tdls_frame) +
1284 1 /* dialog token */ + sizeof(struct wpa_tdls_lnkid);
1285 u8 dialog_token;
1286
1287 wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from " MACSTR,
1288 MAC2STR(addr));
1289
1290 if (len < min_req_len) {
1291 wpa_printf(MSG_DEBUG, "TDLS Discovery Request is too short: "
1292 "%d", (int) len);
1293 return -1;
1294 }
1295
1296 dialog_token = buf[sizeof(struct wpa_tdls_frame)];
1297
1298 if (wpa_supplicant_parse_ies(buf + sizeof(struct wpa_tdls_frame) + 1,
1299 len - (sizeof(struct wpa_tdls_frame) + 1),
1300 &kde) < 0)
1301 return -1;
1302
1303 if (!kde.lnkid) {
1304 wpa_printf(MSG_DEBUG, "TDLS: Link ID not found in Discovery "
1305 "Request");
1306 return -1;
1307 }
1308
1309 lnkid = (const struct wpa_tdls_lnkid *) kde.lnkid;
1310
1311 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1312 wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from different "
1313 " BSS " MACSTR, MAC2STR(lnkid->bssid));
1314 return -1;
1315 }
1316
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001317 peer = wpa_tdls_add_peer(sm, addr, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001318 if (peer == NULL)
1319 return -1;
1320
1321 return wpa_tdls_send_discovery_response(sm, peer, dialog_token);
1322}
1323
1324
1325int wpa_tdls_send_discovery_request(struct wpa_sm *sm, const u8 *addr)
1326{
1327 if (sm->tdls_disabled || !sm->tdls_supported)
1328 return -1;
1329
1330 wpa_printf(MSG_DEBUG, "TDLS: Sending Discovery Request to peer "
1331 MACSTR, MAC2STR(addr));
1332 return wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_DISCOVERY_REQUEST,
1333 1, 0, NULL, 0);
1334}
1335
1336
1337static int copy_supp_rates(const struct wpa_eapol_ie_parse *kde,
1338 struct wpa_tdls_peer *peer)
1339{
1340 if (!kde->supp_rates) {
1341 wpa_printf(MSG_DEBUG, "TDLS: No supported rates received");
1342 return -1;
1343 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001344 peer->supp_rates_len = merge_byte_arrays(
1345 peer->supp_rates, sizeof(peer->supp_rates),
1346 kde->supp_rates + 2, kde->supp_rates_len - 2,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001347 kde->ext_supp_rates ? kde->ext_supp_rates + 2 : NULL,
1348 kde->ext_supp_rates_len - 2);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001349 return 0;
1350}
1351
1352
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001353static int copy_peer_ht_capab(const struct wpa_eapol_ie_parse *kde,
1354 struct wpa_tdls_peer *peer)
1355{
1356 if (!kde->ht_capabilities ||
1357 kde->ht_capabilities_len <
1358 sizeof(struct ieee80211_ht_capabilities) ) {
1359 wpa_printf(MSG_DEBUG, "TDLS: No supported ht capabilities "
1360 "received");
1361 return 0;
1362 }
1363
1364 if (!peer->ht_capabilities) {
1365 peer->ht_capabilities =
1366 os_zalloc(sizeof(struct ieee80211_ht_capabilities));
1367 if (peer->ht_capabilities == NULL)
1368 return -1;
1369 }
1370
1371 os_memcpy(peer->ht_capabilities, kde->ht_capabilities,
1372 sizeof(struct ieee80211_ht_capabilities));
1373 wpa_hexdump(MSG_DEBUG, "TDLS: Peer HT capabilities",
1374 (u8 *) peer->ht_capabilities,
1375 sizeof(struct ieee80211_ht_capabilities));
1376
1377 return 0;
1378}
1379
1380
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001381static int copy_peer_vht_capab(const struct wpa_eapol_ie_parse *kde,
1382 struct wpa_tdls_peer *peer)
1383{
1384 if (!kde->vht_capabilities ||
1385 kde->vht_capabilities_len <
1386 sizeof(struct ieee80211_vht_capabilities) ) {
1387 wpa_printf(MSG_DEBUG, "TDLS: No supported vht capabilities "
1388 "received");
1389 return 0;
1390 }
1391
1392 if (!peer->vht_capabilities) {
1393 peer->vht_capabilities =
1394 os_zalloc(sizeof(struct ieee80211_vht_capabilities));
1395 if (peer->vht_capabilities == NULL)
1396 return -1;
1397 }
1398
1399 os_memcpy(peer->vht_capabilities, kde->vht_capabilities,
1400 sizeof(struct ieee80211_vht_capabilities));
1401 wpa_hexdump(MSG_DEBUG, "TDLS: Peer VHT capabilities",
1402 (u8 *) peer->vht_capabilities,
1403 sizeof(struct ieee80211_vht_capabilities));
1404
1405 return 0;
1406}
1407
1408
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001409static int copy_peer_ext_capab(const struct wpa_eapol_ie_parse *kde,
1410 struct wpa_tdls_peer *peer)
1411{
1412 if (!kde->ext_capab) {
1413 wpa_printf(MSG_DEBUG, "TDLS: No extended capabilities "
1414 "received");
1415 return 0;
1416 }
1417
1418 if (!peer->ext_capab || peer->ext_capab_len < kde->ext_capab_len - 2) {
1419 /* Need to allocate buffer to fit the new information */
1420 os_free(peer->ext_capab);
1421 peer->ext_capab = os_zalloc(kde->ext_capab_len - 2);
1422 if (peer->ext_capab == NULL)
1423 return -1;
1424 }
1425
1426 peer->ext_capab_len = kde->ext_capab_len - 2;
1427 os_memcpy(peer->ext_capab, kde->ext_capab + 2, peer->ext_capab_len);
1428
1429 return 0;
1430}
1431
1432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433static int wpa_tdls_process_tpk_m1(struct wpa_sm *sm, const u8 *src_addr,
1434 const u8 *buf, size_t len)
1435{
1436 struct wpa_tdls_peer *peer;
1437 struct wpa_eapol_ie_parse kde;
1438 struct wpa_ie_data ie;
1439 int cipher;
1440 const u8 *cpos;
1441 struct wpa_tdls_ftie *ftie = NULL;
1442 struct wpa_tdls_timeoutie *timeoutie;
1443 struct wpa_tdls_lnkid *lnkid;
1444 u32 lifetime = 0;
1445#if 0
1446 struct rsn_ie_hdr *hdr;
1447 u8 *pos;
1448 u16 rsn_capab;
1449 u16 rsn_ver;
1450#endif
1451 u8 dtoken;
1452 u16 ielen;
1453 u16 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1454 int tdls_prohibited = sm->tdls_prohibited;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001455 int existing_peer = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456
1457 if (len < 3 + 3)
1458 return -1;
1459
1460 cpos = buf;
1461 cpos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1462
1463 /* driver had already verified the frame format */
1464 dtoken = *cpos++; /* dialog token */
1465
1466 wpa_printf(MSG_INFO, "TDLS: Dialog Token in TPK M1 %d", dtoken);
1467
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001468 peer = wpa_tdls_add_peer(sm, src_addr, &existing_peer);
1469 if (peer == NULL)
1470 goto error;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001471
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001472 /* If found, use existing entry instead of adding a new one;
1473 * how to handle the case where both ends initiate at the
1474 * same time? */
1475 if (existing_peer) {
1476 if (peer->tpk_success) {
1477 wpa_printf(MSG_DEBUG, "TDLS: TDLS Setup Request while "
1478 "direct link is enabled - tear down the "
1479 "old link first");
1480#if 0
1481 /* TODO: Disabling the link would be more proper
1482 * operation here, but it seems to trigger a race with
1483 * some drivers handling the new request frame. */
1484 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, src_addr);
1485#else
1486 if (sm->tdls_external_setup)
1487 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK,
1488 src_addr);
1489 else
1490 wpa_tdls_del_key(sm, peer);
1491#endif
1492 wpa_tdls_peer_free(sm, peer);
1493 }
1494
1495 /*
1496 * An entry is already present, so check if we already sent a
1497 * TDLS Setup Request. If so, compare MAC addresses and let the
1498 * STA with the lower MAC address continue as the initiator.
1499 * The other negotiation is terminated.
1500 */
1501 if (peer->initiator) {
1502 if (os_memcmp(sm->own_addr, src_addr, ETH_ALEN) < 0) {
1503 wpa_printf(MSG_DEBUG, "TDLS: Discard request "
1504 "from peer with higher address "
1505 MACSTR, MAC2STR(src_addr));
1506 return -1;
1507 } else {
1508 wpa_printf(MSG_DEBUG, "TDLS: Accept request "
1509 "from peer with lower address "
1510 MACSTR " (terminate previously "
1511 "initiated negotiation",
1512 MAC2STR(src_addr));
1513 if (sm->tdls_external_setup)
1514 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK,
1515 src_addr);
1516 else
1517 wpa_tdls_del_key(sm, peer);
1518 wpa_tdls_peer_free(sm, peer);
1519 }
1520 }
1521 }
1522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001523 /* capability information */
1524 peer->capability = WPA_GET_LE16(cpos);
1525 cpos += 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526
1527 ielen = len - (cpos - buf); /* start of IE in buf */
1528 if (wpa_supplicant_parse_ies(cpos, ielen, &kde) < 0) {
1529 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in TPK M1");
1530 goto error;
1531 }
1532
1533 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
1534 wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
1535 "TPK M1");
1536 goto error;
1537 }
1538 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M1",
1539 kde.lnkid, kde.lnkid_len);
1540 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
1541 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1542 wpa_printf(MSG_INFO, "TDLS: TPK M1 from diff BSS");
1543 status = WLAN_STATUS_NOT_IN_SAME_BSS;
1544 goto error;
1545 }
1546
1547 wpa_printf(MSG_DEBUG, "TDLS: TPK M1 - TPK initiator " MACSTR,
1548 MAC2STR(src_addr));
1549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001550 if (copy_supp_rates(&kde, peer) < 0)
1551 goto error;
1552
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001553 if (copy_peer_ht_capab(&kde, peer) < 0)
1554 goto error;
1555
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001556 if (copy_peer_vht_capab(&kde, peer) < 0)
1557 goto error;
1558
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001559 if (copy_peer_ext_capab(&kde, peer) < 0)
1560 goto error;
1561
1562 peer->qos_info = kde.qosinfo;
1563
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001564 peer->aid = kde.aid;
1565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566#ifdef CONFIG_TDLS_TESTING
1567 if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001568 peer = wpa_tdls_add_peer(sm, src_addr, NULL);
1569 if (peer == NULL)
1570 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571 wpa_printf(MSG_DEBUG, "TDLS: Testing concurrent initiation of "
1572 "TDLS setup - send own request");
1573 peer->initiator = 1;
1574 wpa_tdls_send_tpk_m1(sm, peer);
1575 }
1576
1577 if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
1578 tdls_prohibited) {
1579 wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
1580 "on TDLS");
1581 tdls_prohibited = 0;
1582 }
1583#endif /* CONFIG_TDLS_TESTING */
1584
1585 if (tdls_prohibited) {
1586 wpa_printf(MSG_INFO, "TDLS: TDLS prohibited in this BSS");
1587 status = WLAN_STATUS_REQUEST_DECLINED;
1588 goto error;
1589 }
1590
1591 if (!wpa_tdls_get_privacy(sm)) {
1592 if (kde.rsn_ie) {
1593 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M1 while "
1594 "security is disabled");
1595 status = WLAN_STATUS_SECURITY_DISABLED;
1596 goto error;
1597 }
1598 goto skip_rsn;
1599 }
1600
1601 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
1602 kde.rsn_ie == NULL) {
1603 wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M1");
1604 status = WLAN_STATUS_INVALID_PARAMETERS;
1605 goto error;
1606 }
1607
1608 if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
1609 wpa_printf(MSG_INFO, "TDLS: Too long Initiator RSN IE in "
1610 "TPK M1");
1611 status = WLAN_STATUS_INVALID_RSNIE;
1612 goto error;
1613 }
1614
1615 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
1616 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M1");
1617 status = WLAN_STATUS_INVALID_RSNIE;
1618 goto error;
1619 }
1620
1621 cipher = ie.pairwise_cipher;
1622 if (cipher & WPA_CIPHER_CCMP) {
1623 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
1624 cipher = WPA_CIPHER_CCMP;
1625 } else {
1626 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M1");
1627 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1628 goto error;
1629 }
1630
1631 if ((ie.capabilities &
1632 (WPA_CAPABILITY_NO_PAIRWISE | WPA_CAPABILITY_PEERKEY_ENABLED)) !=
1633 WPA_CAPABILITY_PEERKEY_ENABLED) {
1634 wpa_printf(MSG_INFO, "TDLS: Invalid RSN Capabilities in "
1635 "TPK M1");
1636 status = WLAN_STATUS_INVALID_RSN_IE_CAPAB;
1637 goto error;
1638 }
1639
1640 /* Lifetime */
1641 if (kde.key_lifetime == NULL) {
1642 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M1");
1643 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
1644 goto error;
1645 }
1646 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
1647 lifetime = WPA_GET_LE32(timeoutie->value);
1648 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", lifetime);
1649 if (lifetime < 300) {
1650 wpa_printf(MSG_INFO, "TDLS: Too short TPK lifetime");
1651 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
1652 goto error;
1653 }
1654
1655skip_rsn:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001656#ifdef CONFIG_TDLS_TESTING
1657 if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
1658 if (os_memcmp(sm->own_addr, peer->addr, ETH_ALEN) < 0) {
1659 /*
1660 * The request frame from us is going to win, so do not
1661 * replace information based on this request frame from
1662 * the peer.
1663 */
1664 goto skip_rsn_check;
1665 }
1666 }
1667#endif /* CONFIG_TDLS_TESTING */
1668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 peer->initiator = 0; /* Need to check */
1670 peer->dtoken = dtoken;
1671
1672 if (!wpa_tdls_get_privacy(sm)) {
1673 peer->rsnie_i_len = 0;
1674 peer->rsnie_p_len = 0;
1675 peer->cipher = WPA_CIPHER_NONE;
1676 goto skip_rsn_check;
1677 }
1678
1679 ftie = (struct wpa_tdls_ftie *) kde.ftie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680 os_memcpy(peer->rsnie_i, kde.rsn_ie, kde.rsn_ie_len);
1681 peer->rsnie_i_len = kde.rsn_ie_len;
1682 peer->cipher = cipher;
1683
Sunil Dutt61024722013-09-15 12:09:40 -07001684 if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
1685 /*
1686 * There is no point in updating the RNonce for every obtained
1687 * TPK M1 frame (e.g., retransmission due to timeout) with the
1688 * same INonce (SNonce in FTIE). However, if the TPK M1 is
1689 * retransmitted with a different INonce, update the RNonce
1690 * since this is for a new TDLS session.
1691 */
1692 wpa_printf(MSG_DEBUG,
1693 "TDLS: New TPK M1 INonce - generate new RNonce");
1694 os_memcpy(peer->inonce, ftie->Snonce, WPA_NONCE_LEN);
1695 if (os_get_random(peer->rnonce, WPA_NONCE_LEN)) {
1696 wpa_msg(sm->ctx->ctx, MSG_WARNING,
1697 "TDLS: Failed to get random data for responder nonce");
1698 wpa_tdls_peer_free(sm, peer);
1699 goto error;
1700 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 }
1702
1703#if 0
1704 /* get version info from RSNIE received from Peer */
1705 hdr = (struct rsn_ie_hdr *) kde.rsn_ie;
1706 rsn_ver = WPA_GET_LE16(hdr->version);
1707
1708 /* use min(peer's version, out version) */
1709 if (rsn_ver > RSN_VERSION)
1710 rsn_ver = RSN_VERSION;
1711
1712 hdr = (struct rsn_ie_hdr *) peer->rsnie_p;
1713
1714 hdr->elem_id = WLAN_EID_RSN;
1715 WPA_PUT_LE16(hdr->version, rsn_ver);
1716 pos = (u8 *) (hdr + 1);
1717
1718 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1719 pos += RSN_SELECTOR_LEN;
1720 /* Include only the selected cipher in pairwise cipher suite */
1721 WPA_PUT_LE16(pos, 1);
1722 pos += 2;
1723 if (cipher == WPA_CIPHER_CCMP)
1724 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1725 pos += RSN_SELECTOR_LEN;
1726
1727 WPA_PUT_LE16(pos, 1);
1728 pos += 2;
1729 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1730 pos += RSN_SELECTOR_LEN;
1731
1732 rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1733 rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1734 WPA_PUT_LE16(pos, rsn_capab);
1735 pos += 2;
1736
1737 hdr->len = (pos - peer->rsnie_p) - 2;
1738 peer->rsnie_p_len = pos - peer->rsnie_p;
1739#endif
1740
1741 /* temp fix: validation of RSNIE later */
1742 os_memcpy(peer->rsnie_p, peer->rsnie_i, peer->rsnie_i_len);
1743 peer->rsnie_p_len = peer->rsnie_i_len;
1744
1745 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
1746 peer->rsnie_p, peer->rsnie_p_len);
1747
1748 peer->lifetime = lifetime;
1749
1750 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
1751
1752skip_rsn_check:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001753 /* add the peer to the driver as a "setup in progress" peer */
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001754 wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL, NULL, 0,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001755 NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Response / TPK M2");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001758 if (wpa_tdls_send_tpk_m2(sm, src_addr, dtoken, lnkid, peer) < 0) {
1759 wpa_tdls_disable_link(sm, peer->addr);
1760 goto error;
1761 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762
1763 return 0;
1764
1765error:
1766 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE, dtoken,
1767 status);
1768 return -1;
1769}
1770
1771
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001772static int wpa_tdls_enable_link(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773{
1774 peer->tpk_success = 1;
1775 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
1776 if (wpa_tdls_get_privacy(sm)) {
1777 u32 lifetime = peer->lifetime;
1778 /*
1779 * Start the initiator process a bit earlier to avoid race
1780 * condition with the responder sending teardown request.
1781 */
1782 if (lifetime > 3 && peer->initiator)
1783 lifetime -= 3;
1784 eloop_register_timeout(lifetime, 0, wpa_tdls_tpk_timeout,
1785 sm, peer);
1786#ifdef CONFIG_TDLS_TESTING
1787 if (tdls_testing & TDLS_TESTING_NO_TPK_EXPIRATION) {
1788 wpa_printf(MSG_DEBUG, "TDLS: Testing - disable TPK "
1789 "expiration");
1790 eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
1791 }
1792#endif /* CONFIG_TDLS_TESTING */
1793 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001794
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001795 /* add supported rates, capabilities, and qos_info to the TDLS peer */
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001796 if (wpa_sm_tdls_peer_addset(sm, peer->addr, 0, peer->aid,
1797 peer->capability,
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001798 peer->supp_rates, peer->supp_rates_len,
1799 peer->ht_capabilities,
1800 peer->vht_capabilities,
1801 peer->qos_info, peer->ext_capab,
1802 peer->ext_capab_len) < 0)
1803 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001804
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001805 if (peer->reconfig_key && wpa_tdls_set_key(sm, peer) < 0) {
1806 wpa_printf(MSG_INFO, "TDLS: Could not configure key to the "
1807 "driver");
1808 return -1;
1809 }
1810 peer->reconfig_key = 0;
1811
1812 return wpa_sm_tdls_oper(sm, TDLS_ENABLE_LINK, peer->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813}
1814
1815
1816static int wpa_tdls_process_tpk_m2(struct wpa_sm *sm, const u8 *src_addr,
1817 const u8 *buf, size_t len)
1818{
1819 struct wpa_tdls_peer *peer;
1820 struct wpa_eapol_ie_parse kde;
1821 struct wpa_ie_data ie;
1822 int cipher;
1823 struct wpa_tdls_ftie *ftie;
1824 struct wpa_tdls_timeoutie *timeoutie;
1825 struct wpa_tdls_lnkid *lnkid;
1826 u32 lifetime;
1827 u8 dtoken;
1828 int ielen;
1829 u16 status;
1830 const u8 *pos;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001831 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832
1833 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Response / TPK M2 "
1834 "(Peer " MACSTR ")", MAC2STR(src_addr));
1835 for (peer = sm->tdls; peer; peer = peer->next) {
1836 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
1837 break;
1838 }
1839 if (peer == NULL) {
1840 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
1841 "TPK M2: " MACSTR, MAC2STR(src_addr));
1842 return -1;
1843 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001844 if (!peer->initiator) {
1845 /*
1846 * This may happen if both devices try to initiate TDLS at the
1847 * same time and we accept the TPK M1 from the peer in
1848 * wpa_tdls_process_tpk_m1() and clear our previous state.
1849 */
1850 wpa_printf(MSG_INFO, "TDLS: We were not the initiator, so "
1851 "ignore TPK M2 from " MACSTR, MAC2STR(src_addr));
1852 return -1;
1853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_REQUEST);
1855
Sunil Duttadce9cf2013-09-15 11:51:00 -07001856 if (len < 3 + 2 + 1) {
1857 wpa_tdls_disable_link(sm, src_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001858 return -1;
Sunil Duttadce9cf2013-09-15 11:51:00 -07001859 }
1860
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861 pos = buf;
1862 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1863 status = WPA_GET_LE16(pos);
1864 pos += 2 /* status code */;
1865
1866 if (status != WLAN_STATUS_SUCCESS) {
1867 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M2: %u",
1868 status);
Sunil Duttadce9cf2013-09-15 11:51:00 -07001869 wpa_tdls_disable_link(sm, src_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 return -1;
1871 }
1872
1873 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1874
1875 /* TODO: need to verify dialog token matches here or in kernel */
1876 dtoken = *pos++; /* dialog token */
1877
1878 wpa_printf(MSG_DEBUG, "TDLS: Dialog Token in TPK M2 %d", dtoken);
1879
Sunil Duttadce9cf2013-09-15 11:51:00 -07001880 if (len < 3 + 2 + 1 + 2) {
1881 wpa_tdls_disable_link(sm, src_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 return -1;
Sunil Duttadce9cf2013-09-15 11:51:00 -07001883 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001884
1885 /* capability information */
1886 peer->capability = WPA_GET_LE16(pos);
1887 pos += 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888
1889 ielen = len - (pos - buf); /* start of IE in buf */
1890 if (wpa_supplicant_parse_ies(pos, ielen, &kde) < 0) {
1891 wpa_printf(MSG_INFO, "TDLS: Failed to parse IEs in TPK M2");
1892 goto error;
1893 }
1894
1895#ifdef CONFIG_TDLS_TESTING
1896 if (tdls_testing & TDLS_TESTING_DECLINE_RESP) {
1897 wpa_printf(MSG_DEBUG, "TDLS: Testing - decline response");
1898 status = WLAN_STATUS_REQUEST_DECLINED;
1899 goto error;
1900 }
1901#endif /* CONFIG_TDLS_TESTING */
1902
1903 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
1904 wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
1905 "TPK M2");
1906 goto error;
1907 }
1908 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M2",
1909 kde.lnkid, kde.lnkid_len);
1910 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
1911
1912 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1913 wpa_printf(MSG_INFO, "TDLS: TPK M2 from different BSS");
1914 status = WLAN_STATUS_NOT_IN_SAME_BSS;
1915 goto error;
1916 }
1917
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001918 if (copy_supp_rates(&kde, peer) < 0)
1919 goto error;
1920
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001921 if (copy_peer_ht_capab(&kde, peer) < 0)
1922 goto error;
1923
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001924 if (copy_peer_vht_capab(&kde, peer) < 0)
1925 goto error;
1926
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001927 if (copy_peer_ext_capab(&kde, peer) < 0)
1928 goto error;
1929
1930 peer->qos_info = kde.qosinfo;
1931
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001932 peer->aid = kde.aid;
1933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 if (!wpa_tdls_get_privacy(sm)) {
1935 peer->rsnie_p_len = 0;
1936 peer->cipher = WPA_CIPHER_NONE;
1937 goto skip_rsn;
1938 }
1939
1940 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
1941 kde.rsn_ie == NULL) {
1942 wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M2");
1943 status = WLAN_STATUS_INVALID_PARAMETERS;
1944 goto error;
1945 }
1946 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
1947 kde.rsn_ie, kde.rsn_ie_len);
1948
1949 /*
1950 * FIX: bitwise comparison of RSN IE is not the correct way of
1951 * validation this. It can be different, but certain fields must
1952 * match. Since we list only a single pairwise cipher in TPK M1, the
1953 * memcmp is likely to work in most cases, though.
1954 */
1955 if (kde.rsn_ie_len != peer->rsnie_i_len ||
1956 os_memcmp(peer->rsnie_i, kde.rsn_ie, peer->rsnie_i_len) != 0) {
1957 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M2 does "
1958 "not match with RSN IE used in TPK M1");
1959 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Sent in TPK M1",
1960 peer->rsnie_i, peer->rsnie_i_len);
1961 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
1962 kde.rsn_ie, kde.rsn_ie_len);
1963 status = WLAN_STATUS_INVALID_RSNIE;
1964 goto error;
1965 }
1966
1967 if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
1968 wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M2");
1969 status = WLAN_STATUS_INVALID_RSNIE;
1970 goto error;
1971 }
1972
1973 cipher = ie.pairwise_cipher;
1974 if (cipher == WPA_CIPHER_CCMP) {
1975 wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
1976 cipher = WPA_CIPHER_CCMP;
1977 } else {
1978 wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M2");
1979 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1980 goto error;
1981 }
1982
1983 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M2",
1984 kde.ftie, sizeof(*ftie));
1985 ftie = (struct wpa_tdls_ftie *) kde.ftie;
1986
1987 if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
1988 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M2 does "
1989 "not match with FTIE SNonce used in TPK M1");
1990 /* Silently discard the frame */
1991 return -1;
1992 }
1993
1994 /* Responder Nonce and RSN IE */
1995 os_memcpy(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN);
1996 os_memcpy(peer->rsnie_p, kde.rsn_ie, kde.rsn_ie_len);
1997 peer->rsnie_p_len = kde.rsn_ie_len;
1998 peer->cipher = cipher;
1999
2000 /* Lifetime */
2001 if (kde.key_lifetime == NULL) {
2002 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M2");
2003 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2004 goto error;
2005 }
2006 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2007 lifetime = WPA_GET_LE32(timeoutie->value);
2008 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M2",
2009 lifetime);
2010 if (lifetime != peer->lifetime) {
2011 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2012 "TPK M2 (expected %u)", lifetime, peer->lifetime);
2013 status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2014 goto error;
2015 }
2016
2017 wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
2018
2019 /* Process MIC check to see if TPK M2 is right */
2020 if (wpa_supplicant_verify_tdls_mic(2, peer, (u8 *) lnkid,
2021 (u8 *) timeoutie, ftie) < 0) {
2022 /* Discard the frame */
2023 wpa_tdls_del_key(sm, peer);
Sunil Dutt38ffd882013-09-30 17:23:23 +03002024 wpa_tdls_disable_peer_link(sm, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 return -1;
2026 }
2027
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002028 if (wpa_tdls_set_key(sm, peer) < 0) {
2029 /*
2030 * Some drivers may not be able to config the key prior to full
2031 * STA entry having been configured.
2032 */
2033 wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2034 "STA entry is complete");
2035 peer->reconfig_key = 1;
2036 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037
2038skip_rsn:
2039 peer->dtoken = dtoken;
2040
2041 wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Confirm / "
2042 "TPK Handshake Message 3");
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002043 if (wpa_tdls_send_tpk_m3(sm, src_addr, dtoken, lnkid, peer) < 0) {
2044 wpa_tdls_disable_link(sm, peer->addr);
2045 return -1;
2046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002048 ret = wpa_tdls_enable_link(sm, peer);
2049 if (ret < 0) {
2050 wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
2051 wpa_tdls_do_teardown(sm, peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002052 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002053 }
2054 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055
2056error:
2057 wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM, dtoken,
2058 status);
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002059 wpa_tdls_disable_link(sm, peer->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 return -1;
2061}
2062
2063
2064static int wpa_tdls_process_tpk_m3(struct wpa_sm *sm, const u8 *src_addr,
2065 const u8 *buf, size_t len)
2066{
2067 struct wpa_tdls_peer *peer;
2068 struct wpa_eapol_ie_parse kde;
2069 struct wpa_tdls_ftie *ftie;
2070 struct wpa_tdls_timeoutie *timeoutie;
2071 struct wpa_tdls_lnkid *lnkid;
2072 int ielen;
2073 u16 status;
2074 const u8 *pos;
2075 u32 lifetime;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002076 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077
2078 wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Confirm / TPK M3 "
2079 "(Peer " MACSTR ")", MAC2STR(src_addr));
2080 for (peer = sm->tdls; peer; peer = peer->next) {
2081 if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
2082 break;
2083 }
2084 if (peer == NULL) {
2085 wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
2086 "TPK M3: " MACSTR, MAC2STR(src_addr));
2087 return -1;
2088 }
2089 wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_RESPONSE);
2090
2091 if (len < 3 + 3)
Sunil Duttadce9cf2013-09-15 11:51:00 -07002092 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093 pos = buf;
2094 pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2095
2096 status = WPA_GET_LE16(pos);
2097
2098 if (status != 0) {
2099 wpa_printf(MSG_INFO, "TDLS: Status code in TPK M3: %u",
2100 status);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002101 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 }
2103 pos += 2 /* status code */ + 1 /* dialog token */;
2104
2105 ielen = len - (pos - buf); /* start of IE in buf */
2106 if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
2107 wpa_printf(MSG_INFO, "TDLS: Failed to parse KDEs in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002108 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 }
2110
2111 if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
2112 wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002113 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 }
2115 wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M3",
2116 (u8 *) kde.lnkid, kde.lnkid_len);
2117 lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
2118
2119 if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
2120 wpa_printf(MSG_INFO, "TDLS: TPK M3 from diff BSS");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002121 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 }
2123
2124 if (!wpa_tdls_get_privacy(sm))
2125 goto skip_rsn;
2126
2127 if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
2128 wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002129 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130 }
2131 wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M3",
2132 kde.ftie, sizeof(*ftie));
2133 ftie = (struct wpa_tdls_ftie *) kde.ftie;
2134
2135 if (kde.rsn_ie == NULL) {
2136 wpa_printf(MSG_INFO, "TDLS: No RSN IE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002137 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 }
2139 wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M3",
2140 kde.rsn_ie, kde.rsn_ie_len);
2141 if (kde.rsn_ie_len != peer->rsnie_p_len ||
2142 os_memcmp(kde.rsn_ie, peer->rsnie_p, peer->rsnie_p_len) != 0) {
2143 wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M3 does not match "
2144 "with the one sent in TPK M2");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002145 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 }
2147
2148 if (!os_memcmp(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN) == 0) {
2149 wpa_printf(MSG_INFO, "TDLS: FTIE ANonce in TPK M3 does "
2150 "not match with FTIE ANonce used in TPK M2");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002151 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 }
2153
2154 if (!os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) == 0) {
2155 wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M3 does not "
2156 "match with FTIE SNonce used in TPK M1");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002157 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 }
2159
2160 if (kde.key_lifetime == NULL) {
2161 wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M3");
Sunil Duttadce9cf2013-09-15 11:51:00 -07002162 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163 }
2164 timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2165 wpa_hexdump(MSG_DEBUG, "TDLS: Timeout IE Received from TPK M3",
2166 (u8 *) timeoutie, sizeof(*timeoutie));
2167 lifetime = WPA_GET_LE32(timeoutie->value);
2168 wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M3",
2169 lifetime);
2170 if (lifetime != peer->lifetime) {
2171 wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2172 "TPK M3 (expected %u)", lifetime, peer->lifetime);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002173 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 }
2175
2176 if (wpa_supplicant_verify_tdls_mic(3, peer, (u8 *) lnkid,
2177 (u8 *) timeoutie, ftie) < 0) {
2178 wpa_tdls_del_key(sm, peer);
Sunil Duttadce9cf2013-09-15 11:51:00 -07002179 goto error;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180 }
2181
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002182 if (wpa_tdls_set_key(sm, peer) < 0) {
2183 /*
2184 * Some drivers may not be able to config the key prior to full
2185 * STA entry having been configured.
2186 */
2187 wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2188 "STA entry is complete");
2189 peer->reconfig_key = 1;
2190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191
2192skip_rsn:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002193 ret = wpa_tdls_enable_link(sm, peer);
2194 if (ret < 0) {
2195 wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
2196 wpa_tdls_do_teardown(sm, peer,
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002197 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002198 }
2199 return ret;
Sunil Duttadce9cf2013-09-15 11:51:00 -07002200error:
2201 wpa_tdls_disable_link(sm, peer->addr);
2202 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203}
2204
2205
2206static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs)
2207{
2208 struct wpa_tdls_timeoutie *lifetime = (struct wpa_tdls_timeoutie *) ie;
2209
2210 os_memset(lifetime, 0, ie_len);
2211 lifetime->ie_type = WLAN_EID_TIMEOUT_INTERVAL;
2212 lifetime->ie_len = sizeof(struct wpa_tdls_timeoutie) - 2;
2213 lifetime->interval_type = WLAN_TIMEOUT_KEY_LIFETIME;
2214 WPA_PUT_LE32(lifetime->value, tsecs);
2215 os_memcpy(pos, ie, ie_len);
2216 return pos + ie_len;
2217}
2218
2219
2220/**
2221 * wpa_tdls_start - Initiate TDLS handshake (send TPK Handshake Message 1)
2222 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2223 * @peer: MAC address of the peer STA
2224 * Returns: 0 on success, or -1 on failure
2225 *
2226 * Send TPK Handshake Message 1 info to driver to start TDLS
2227 * handshake with the peer.
2228 */
2229int wpa_tdls_start(struct wpa_sm *sm, const u8 *addr)
2230{
2231 struct wpa_tdls_peer *peer;
2232 int tdls_prohibited = sm->tdls_prohibited;
2233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002234 if (sm->tdls_disabled || !sm->tdls_supported)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 return -1;
2236
2237#ifdef CONFIG_TDLS_TESTING
2238 if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
2239 tdls_prohibited) {
2240 wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
2241 "on TDLS");
2242 tdls_prohibited = 0;
2243 }
2244#endif /* CONFIG_TDLS_TESTING */
2245
2246 if (tdls_prohibited) {
2247 wpa_printf(MSG_DEBUG, "TDLS: TDLS is prohibited in this BSS - "
2248 "reject request to start setup");
2249 return -1;
2250 }
2251
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002252 peer = wpa_tdls_add_peer(sm, addr, NULL);
2253 if (peer == NULL)
2254 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255
2256 peer->initiator = 1;
2257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002258 /* add the peer to the driver as a "setup in progress" peer */
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002259 wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL, NULL, 0,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002260 NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002261
2262 if (wpa_tdls_send_tpk_m1(sm, peer) < 0) {
2263 wpa_tdls_disable_link(sm, peer->addr);
2264 return -1;
2265 }
2266
2267 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268}
2269
2270
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002271void wpa_tdls_remove(struct wpa_sm *sm, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272{
2273 struct wpa_tdls_peer *peer;
2274
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002275 if (sm->tdls_disabled || !sm->tdls_supported)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002276 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002277
2278 for (peer = sm->tdls; peer; peer = peer->next) {
2279 if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
2280 break;
2281 }
2282
2283 if (peer == NULL || !peer->tpk_success)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002284 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002286 if (sm->tdls_external_setup) {
2287 /*
2288 * Disable previous link to allow renegotiation to be completed
2289 * on AP path.
2290 */
2291 wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
2292 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293}
2294
2295
2296/**
2297 * wpa_supplicant_rx_tdls - Receive TDLS data frame
2298 *
2299 * This function is called to receive TDLS (ethertype = 0x890d) data frames.
2300 */
2301static void wpa_supplicant_rx_tdls(void *ctx, const u8 *src_addr,
2302 const u8 *buf, size_t len)
2303{
2304 struct wpa_sm *sm = ctx;
2305 struct wpa_tdls_frame *tf;
2306
2307 wpa_hexdump(MSG_DEBUG, "TDLS: Received Data frame encapsulation",
2308 buf, len);
2309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002310 if (sm->tdls_disabled || !sm->tdls_supported) {
2311 wpa_printf(MSG_DEBUG, "TDLS: Discard message - TDLS disabled "
2312 "or unsupported by driver");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 return;
2314 }
2315
2316 if (os_memcmp(src_addr, sm->own_addr, ETH_ALEN) == 0) {
2317 wpa_printf(MSG_DEBUG, "TDLS: Discard copy of own message");
2318 return;
2319 }
2320
2321 if (len < sizeof(*tf)) {
2322 wpa_printf(MSG_INFO, "TDLS: Drop too short frame");
2323 return;
2324 }
2325
2326 /* Check to make sure its a valid encapsulated TDLS frame */
2327 tf = (struct wpa_tdls_frame *) buf;
2328 if (tf->payloadtype != 2 /* TDLS_RFTYPE */ ||
2329 tf->category != WLAN_ACTION_TDLS) {
2330 wpa_printf(MSG_INFO, "TDLS: Invalid frame - payloadtype=%u "
2331 "category=%u action=%u",
2332 tf->payloadtype, tf->category, tf->action);
2333 return;
2334 }
2335
2336 switch (tf->action) {
2337 case WLAN_TDLS_SETUP_REQUEST:
2338 wpa_tdls_process_tpk_m1(sm, src_addr, buf, len);
2339 break;
2340 case WLAN_TDLS_SETUP_RESPONSE:
2341 wpa_tdls_process_tpk_m2(sm, src_addr, buf, len);
2342 break;
2343 case WLAN_TDLS_SETUP_CONFIRM:
2344 wpa_tdls_process_tpk_m3(sm, src_addr, buf, len);
2345 break;
2346 case WLAN_TDLS_TEARDOWN:
2347 wpa_tdls_recv_teardown(sm, src_addr, buf, len);
2348 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002349 case WLAN_TDLS_DISCOVERY_REQUEST:
2350 wpa_tdls_process_discovery_request(sm, src_addr, buf, len);
2351 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 default:
2353 /* Kernel code will process remaining frames */
2354 wpa_printf(MSG_DEBUG, "TDLS: Ignore TDLS frame action code %u",
2355 tf->action);
2356 break;
2357 }
2358}
2359
2360
2361/**
2362 * wpa_tdls_init - Initialize driver interface parameters for TDLS
2363 * @wpa_s: Pointer to wpa_supplicant data
2364 * Returns: 0 on success, -1 on failure
2365 *
2366 * This function is called to initialize driver interface parameters for TDLS.
2367 * wpa_drv_init() must have been called before this function to initialize the
2368 * driver interface.
2369 */
2370int wpa_tdls_init(struct wpa_sm *sm)
2371{
2372 if (sm == NULL)
2373 return -1;
2374
Dmitry Shmidt04949592012-07-19 12:16:46 -07002375 sm->l2_tdls = l2_packet_init(sm->bridge_ifname ? sm->bridge_ifname :
2376 sm->ifname,
2377 sm->own_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378 ETH_P_80211_ENCAP, wpa_supplicant_rx_tdls,
2379 sm, 0);
2380 if (sm->l2_tdls == NULL) {
2381 wpa_printf(MSG_ERROR, "TDLS: Failed to open l2_packet "
2382 "connection");
2383 return -1;
2384 }
2385
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386 /*
2387 * Drivers that support TDLS but don't implement the get_capa callback
2388 * are assumed to perform everything internally
2389 */
2390 if (wpa_sm_tdls_get_capa(sm, &sm->tdls_supported,
2391 &sm->tdls_external_setup) < 0) {
2392 sm->tdls_supported = 1;
2393 sm->tdls_external_setup = 0;
2394 }
2395
2396 wpa_printf(MSG_DEBUG, "TDLS: TDLS operation%s supported by "
2397 "driver", sm->tdls_supported ? "" : " not");
2398 wpa_printf(MSG_DEBUG, "TDLS: Driver uses %s link setup",
2399 sm->tdls_external_setup ? "external" : "internal");
2400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401 return 0;
2402}
2403
2404
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002405void wpa_tdls_teardown_peers(struct wpa_sm *sm)
2406{
2407 struct wpa_tdls_peer *peer;
2408
2409 peer = sm->tdls;
2410
2411 wpa_printf(MSG_DEBUG, "TDLS: Tear down peers");
2412
2413 while (peer) {
2414 wpa_printf(MSG_DEBUG, "TDLS: Tear down peer " MACSTR,
2415 MAC2STR(peer->addr));
2416 if (sm->tdls_external_setup)
2417 wpa_tdls_send_teardown(sm, peer->addr,
2418 WLAN_REASON_DEAUTH_LEAVING);
2419 else
2420 wpa_sm_tdls_oper(sm, TDLS_TEARDOWN, peer->addr);
2421
2422 peer = peer->next;
2423 }
2424}
2425
2426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427static void wpa_tdls_remove_peers(struct wpa_sm *sm)
2428{
2429 struct wpa_tdls_peer *peer, *tmp;
2430
2431 peer = sm->tdls;
2432 sm->tdls = NULL;
2433
2434 while (peer) {
2435 int res;
2436 tmp = peer->next;
2437 res = wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
2438 wpa_printf(MSG_DEBUG, "TDLS: Remove peer " MACSTR " (res=%d)",
2439 MAC2STR(peer->addr), res);
2440 wpa_tdls_peer_free(sm, peer);
2441 os_free(peer);
2442 peer = tmp;
2443 }
2444}
2445
2446
2447/**
2448 * wpa_tdls_deinit - Deinitialize driver interface parameters for TDLS
2449 *
2450 * This function is called to recover driver interface parameters for TDLS
2451 * and frees resources allocated for it.
2452 */
2453void wpa_tdls_deinit(struct wpa_sm *sm)
2454{
2455 if (sm == NULL)
2456 return;
2457
2458 if (sm->l2_tdls)
2459 l2_packet_deinit(sm->l2_tdls);
2460 sm->l2_tdls = NULL;
2461
2462 wpa_tdls_remove_peers(sm);
2463}
2464
2465
2466void wpa_tdls_assoc(struct wpa_sm *sm)
2467{
2468 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on association");
2469 wpa_tdls_remove_peers(sm);
2470}
2471
2472
2473void wpa_tdls_disassoc(struct wpa_sm *sm)
2474{
2475 wpa_printf(MSG_DEBUG, "TDLS: Remove peers on disassociation");
2476 wpa_tdls_remove_peers(sm);
2477}
2478
2479
2480static int wpa_tdls_prohibited(const u8 *ies, size_t len)
2481{
2482 struct wpa_eapol_ie_parse elems;
2483
2484 if (ies == NULL)
2485 return 0;
2486
2487 if (wpa_supplicant_parse_ies(ies, len, &elems) < 0)
2488 return 0;
2489
2490 if (elems.ext_capab == NULL || elems.ext_capab_len < 2 + 5)
2491 return 0;
2492
2493 /* bit 38 - TDLS Prohibited */
2494 return !!(elems.ext_capab[2 + 4] & 0x40);
2495}
2496
2497
2498void wpa_tdls_ap_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2499{
2500 sm->tdls_prohibited = wpa_tdls_prohibited(ies, len);
2501 wpa_printf(MSG_DEBUG, "TDLS: TDLS is %s in the target BSS",
2502 sm->tdls_prohibited ? "prohibited" : "allowed");
2503}
2504
2505
2506void wpa_tdls_assoc_resp_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2507{
2508 if (!sm->tdls_prohibited && wpa_tdls_prohibited(ies, len)) {
2509 wpa_printf(MSG_DEBUG, "TDLS: TDLS prohibited based on "
2510 "(Re)Association Response IEs");
2511 sm->tdls_prohibited = 1;
2512 }
2513}
2514
2515
2516void wpa_tdls_enable(struct wpa_sm *sm, int enabled)
2517{
2518 wpa_printf(MSG_DEBUG, "TDLS: %s", enabled ? "enabled" : "disabled");
2519 sm->tdls_disabled = !enabled;
2520}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002521
2522
2523int wpa_tdls_is_external_setup(struct wpa_sm *sm)
2524{
2525 return sm->tdls_external_setup;
2526}