• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * wpa_supplicant - TDLS
3  * Copyright (c) 2010-2011, Atheros Communications
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
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 "common/ieee802_11_common.h"
16 #include "crypto/sha256.h"
17 #include "crypto/crypto.h"
18 #include "crypto/aes_wrap.h"
19 #include "rsn_supp/wpa.h"
20 #include "rsn_supp/wpa_ie.h"
21 #include "rsn_supp/wpa_i.h"
22 #include "drivers/driver.h"
23 #include "l2_packet/l2_packet.h"
24 
25 #ifdef CONFIG_TDLS_TESTING
26 #define TDLS_TESTING_LONG_FRAME BIT(0)
27 #define TDLS_TESTING_ALT_RSN_IE BIT(1)
28 #define TDLS_TESTING_DIFF_BSSID BIT(2)
29 #define TDLS_TESTING_SHORT_LIFETIME BIT(3)
30 #define TDLS_TESTING_WRONG_LIFETIME_RESP BIT(4)
31 #define TDLS_TESTING_WRONG_LIFETIME_CONF BIT(5)
32 #define TDLS_TESTING_LONG_LIFETIME BIT(6)
33 #define TDLS_TESTING_CONCURRENT_INIT BIT(7)
34 #define TDLS_TESTING_NO_TPK_EXPIRATION BIT(8)
35 #define TDLS_TESTING_DECLINE_RESP BIT(9)
36 #define TDLS_TESTING_IGNORE_AP_PROHIBIT BIT(10)
37 #define TDLS_TESTING_WRONG_MIC BIT(11)
38 #define TDLS_TESTING_DOUBLE_TPK_M2 BIT(12)
39 unsigned int tdls_testing = 0;
40 #endif /* CONFIG_TDLS_TESTING */
41 
42 #define TPK_LIFETIME 43200 /* 12 hours */
43 #define TPK_M1_RETRY_COUNT 3
44 #define TPK_M1_TIMEOUT 5000 /* in milliseconds */
45 #define TPK_M2_RETRY_COUNT 10
46 #define TPK_M2_TIMEOUT 500 /* in milliseconds */
47 
48 #define TDLS_MIC_LEN		16
49 
50 #define TDLS_TIMEOUT_LEN	4
51 
52 struct wpa_tdls_ftie {
53 	u8 ie_type; /* FTIE */
54 	u8 ie_len;
55 	u8 mic_ctrl[2];
56 	u8 mic[TDLS_MIC_LEN];
57 	u8 Anonce[WPA_NONCE_LEN]; /* Responder Nonce in TDLS */
58 	u8 Snonce[WPA_NONCE_LEN]; /* Initiator Nonce in TDLS */
59 	/* followed by optional elements */
60 } STRUCT_PACKED;
61 
62 struct wpa_tdls_timeoutie {
63 	u8 ie_type; /* Timeout IE */
64 	u8 ie_len;
65 	u8 interval_type;
66 	u8 value[TDLS_TIMEOUT_LEN];
67 } STRUCT_PACKED;
68 
69 struct wpa_tdls_lnkid {
70 	u8 ie_type; /* Link Identifier IE */
71 	u8 ie_len;
72 	u8 bssid[ETH_ALEN];
73 	u8 init_sta[ETH_ALEN];
74 	u8 resp_sta[ETH_ALEN];
75 } STRUCT_PACKED;
76 
77 /* TDLS frame headers as per IEEE Std 802.11z-2010 */
78 struct wpa_tdls_frame {
79 	u8 payloadtype; /* IEEE80211_TDLS_RFTYPE */
80 	u8 category; /* Category */
81 	u8 action; /* Action (enum tdls_frame_type) */
82 } STRUCT_PACKED;
83 
84 static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs);
85 static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx);
86 static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer);
87 static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
88 				       struct wpa_tdls_peer *peer);
89 static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
90 				  u16 reason_code);
91 
92 
93 #define TDLS_MAX_IE_LEN 80
94 #define IEEE80211_MAX_SUPP_RATES 32
95 
96 struct wpa_tdls_peer {
97 	struct wpa_tdls_peer *next;
98 	unsigned int reconfig_key:1;
99 	int initiator; /* whether this end was initiator for TDLS setup */
100 	u8 addr[ETH_ALEN]; /* other end MAC address */
101 	u8 inonce[WPA_NONCE_LEN]; /* Initiator Nonce */
102 	u8 rnonce[WPA_NONCE_LEN]; /* Responder Nonce */
103 	u8 rsnie_i[TDLS_MAX_IE_LEN]; /* Initiator RSN IE */
104 	size_t rsnie_i_len;
105 	u8 rsnie_p[TDLS_MAX_IE_LEN]; /* Peer RSN IE */
106 	size_t rsnie_p_len;
107 	u32 lifetime;
108 	int cipher; /* Selected cipher (WPA_CIPHER_*) */
109 	u8 dtoken;
110 
111 	struct tpk {
112 		u8 kck[16]; /* TPK-KCK */
113 		u8 tk[16]; /* TPK-TK; assuming only CCMP will be used */
114 	} tpk;
115 	int tpk_set;
116 	int tk_set; /* TPK-TK configured to the driver */
117 	int tpk_success;
118 	int tpk_in_progress;
119 
120 	struct tpk_timer {
121 		u8 dest[ETH_ALEN];
122 		int count;      /* Retry Count */
123 		int timer;      /* Timeout in milliseconds */
124 		u8 action_code; /* TDLS frame type */
125 		u8 dialog_token;
126 		u16 status_code;
127 		u32 peer_capab;
128 		int buf_len;    /* length of TPK message for retransmission */
129 		u8 *buf;        /* buffer for TPK message */
130 	} sm_tmr;
131 
132 	u16 capability;
133 
134 	u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
135 	size_t supp_rates_len;
136 
137 	struct ieee80211_ht_capabilities *ht_capabilities;
138 	struct ieee80211_vht_capabilities *vht_capabilities;
139 
140 	u8 qos_info;
141 
142 	u16 aid;
143 
144 	u8 *ext_capab;
145 	size_t ext_capab_len;
146 
147 	u8 *supp_channels;
148 	size_t supp_channels_len;
149 
150 	u8 *supp_oper_classes;
151 	size_t supp_oper_classes_len;
152 
153 	u8 wmm_capable;
154 
155 	/* channel switch currently enabled */
156 	int chan_switch_enabled;
157 };
158 
159 
wpa_tdls_get_privacy(struct wpa_sm * sm)160 static int wpa_tdls_get_privacy(struct wpa_sm *sm)
161 {
162 	/*
163 	 * Get info needed from supplicant to check if the current BSS supports
164 	 * security. Other than OPEN mode, rest are considered secured
165 	 * WEP/WPA/WPA2 hence TDLS frames are processed for TPK handshake.
166 	 */
167 	return sm->pairwise_cipher != WPA_CIPHER_NONE;
168 }
169 
170 
wpa_add_ie(u8 * pos,const u8 * ie,size_t ie_len)171 static u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
172 {
173 	os_memcpy(pos, ie, ie_len);
174 	return pos + ie_len;
175 }
176 
177 
wpa_tdls_del_key(struct wpa_sm * sm,struct wpa_tdls_peer * peer)178 static int wpa_tdls_del_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
179 {
180 	if (wpa_sm_set_key(sm, WPA_ALG_NONE, peer->addr,
181 			   0, 0, NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE) < 0) {
182 		wpa_printf(MSG_WARNING, "TDLS: Failed to delete TPK-TK from "
183 			   "the driver");
184 		return -1;
185 	}
186 
187 	return 0;
188 }
189 
190 
wpa_tdls_set_key(struct wpa_sm * sm,struct wpa_tdls_peer * peer)191 static int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
192 {
193 	u8 key_len;
194 	u8 rsc[6];
195 	enum wpa_alg alg;
196 
197 	if (peer->tk_set) {
198 		/*
199 		 * This same TPK-TK has already been configured to the driver
200 		 * and this new configuration attempt (likely due to an
201 		 * unexpected retransmitted frame) would result in clearing
202 		 * the TX/RX sequence number which can break security, so must
203 		 * not allow that to happen.
204 		 */
205 		wpa_printf(MSG_INFO, "TDLS: TPK-TK for the peer " MACSTR
206 			   " has already been configured to the driver - do not reconfigure",
207 			   MAC2STR(peer->addr));
208 		return -1;
209 	}
210 
211 	os_memset(rsc, 0, 6);
212 
213 	switch (peer->cipher) {
214 	case WPA_CIPHER_CCMP:
215 		alg = WPA_ALG_CCMP;
216 		key_len = 16;
217 		break;
218 	case WPA_CIPHER_NONE:
219 		wpa_printf(MSG_DEBUG, "TDLS: Pairwise Cipher Suite: "
220 			   "NONE - do not use pairwise keys");
221 		return -1;
222 	default:
223 		wpa_printf(MSG_WARNING, "TDLS: Unsupported pairwise cipher %d",
224 			   sm->pairwise_cipher);
225 		return -1;
226 	}
227 
228 	wpa_printf(MSG_DEBUG, "TDLS: Configure pairwise key for peer " MACSTR,
229 		   MAC2STR(peer->addr));
230 	if (wpa_sm_set_key(sm, alg, peer->addr, 0, 1, rsc, sizeof(rsc),
231 			   peer->tpk.tk, key_len,
232 			   KEY_FLAG_PAIRWISE_RX_TX) < 0) {
233 		wpa_printf(MSG_WARNING, "TDLS: Failed to set TPK to the "
234 			   "driver");
235 		return -1;
236 	}
237 	peer->tk_set = 1;
238 	return 0;
239 }
240 
241 
wpa_tdls_send_tpk_msg(struct wpa_sm * sm,const u8 * dst,u8 action_code,u8 dialog_token,u16 status_code,u32 peer_capab,int initiator,const u8 * buf,size_t len)242 static int wpa_tdls_send_tpk_msg(struct wpa_sm *sm, const u8 *dst,
243 				 u8 action_code, u8 dialog_token,
244 				 u16 status_code, u32 peer_capab,
245 				 int initiator, const u8 *buf, size_t len)
246 {
247 	return wpa_sm_send_tdls_mgmt(sm, dst, action_code, dialog_token,
248 				     status_code, peer_capab, initiator, buf,
249 				     len);
250 }
251 
252 
wpa_tdls_tpk_send(struct wpa_sm * sm,const u8 * dest,u8 action_code,u8 dialog_token,u16 status_code,u32 peer_capab,int initiator,const u8 * msg,size_t msg_len)253 static int wpa_tdls_tpk_send(struct wpa_sm *sm, const u8 *dest, u8 action_code,
254 			     u8 dialog_token, u16 status_code, u32 peer_capab,
255 			     int initiator, const u8 *msg, size_t msg_len)
256 {
257 	struct wpa_tdls_peer *peer;
258 
259 	wpa_printf(MSG_DEBUG, "TDLS: TPK send dest=" MACSTR " action_code=%u "
260 		   "dialog_token=%u status_code=%u peer_capab=%u initiator=%d "
261 		   "msg_len=%u",
262 		   MAC2STR(dest), action_code, dialog_token, status_code,
263 		   peer_capab, initiator, (unsigned int) msg_len);
264 
265 	if (wpa_tdls_send_tpk_msg(sm, dest, action_code, dialog_token,
266 				  status_code, peer_capab, initiator, msg,
267 				  msg_len)) {
268 		wpa_printf(MSG_INFO, "TDLS: Failed to send message "
269 			   "(action_code=%u)", action_code);
270 		return -1;
271 	}
272 
273 	if (action_code == WLAN_TDLS_SETUP_CONFIRM ||
274 	    action_code == WLAN_TDLS_TEARDOWN ||
275 	    action_code == WLAN_TDLS_DISCOVERY_REQUEST ||
276 	    action_code == WLAN_TDLS_DISCOVERY_RESPONSE)
277 		return 0; /* No retries */
278 
279 	for (peer = sm->tdls; peer; peer = peer->next) {
280 		if (os_memcmp(peer->addr, dest, ETH_ALEN) == 0)
281 			break;
282 	}
283 
284 	if (peer == NULL) {
285 		wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
286 			   "retry " MACSTR, MAC2STR(dest));
287 		return 0;
288 	}
289 
290 	eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
291 
292 	if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
293 		peer->sm_tmr.count = TPK_M2_RETRY_COUNT;
294 		peer->sm_tmr.timer = TPK_M2_TIMEOUT;
295 	} else {
296 		peer->sm_tmr.count = TPK_M1_RETRY_COUNT;
297 		peer->sm_tmr.timer = TPK_M1_TIMEOUT;
298 	}
299 
300 	/* Copy message to resend on timeout */
301 	os_memcpy(peer->sm_tmr.dest, dest, ETH_ALEN);
302 	peer->sm_tmr.action_code = action_code;
303 	peer->sm_tmr.dialog_token = dialog_token;
304 	peer->sm_tmr.status_code = status_code;
305 	peer->sm_tmr.peer_capab = peer_capab;
306 	peer->sm_tmr.buf_len = msg_len;
307 	os_free(peer->sm_tmr.buf);
308 	peer->sm_tmr.buf = os_memdup(msg, msg_len);
309 	if (peer->sm_tmr.buf == NULL)
310 		return -1;
311 
312 	wpa_printf(MSG_DEBUG, "TDLS: Retry timeout registered "
313 		   "(action_code=%u)", action_code);
314 	eloop_register_timeout(peer->sm_tmr.timer / 1000,
315 			       (peer->sm_tmr.timer % 1000) * 1000,
316 			       wpa_tdls_tpk_retry_timeout, sm, peer);
317 	return 0;
318 }
319 
320 
wpa_tdls_do_teardown(struct wpa_sm * sm,struct wpa_tdls_peer * peer,u16 reason_code)321 static int wpa_tdls_do_teardown(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
322 				u16 reason_code)
323 {
324 	int ret;
325 
326 	ret = wpa_tdls_send_teardown(sm, peer->addr, reason_code);
327 	/* disable the link after teardown was sent */
328 	wpa_tdls_disable_peer_link(sm, peer);
329 
330 	return ret;
331 }
332 
333 
wpa_tdls_tpk_retry_timeout(void * eloop_ctx,void * timeout_ctx)334 static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx)
335 {
336 
337 	struct wpa_sm *sm = eloop_ctx;
338 	struct wpa_tdls_peer *peer = timeout_ctx;
339 
340 	if (peer->sm_tmr.count) {
341 		peer->sm_tmr.count--;
342 
343 		wpa_printf(MSG_INFO, "TDLS: Retrying sending of message "
344 			   "(action_code=%u)",
345 			   peer->sm_tmr.action_code);
346 
347 		if (peer->sm_tmr.buf == NULL) {
348 			wpa_printf(MSG_INFO, "TDLS: No retry buffer available "
349 				   "for action_code=%u",
350 				   peer->sm_tmr.action_code);
351 			eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm,
352 					     peer);
353 			return;
354 		}
355 
356 		/* resend TPK Handshake Message to Peer */
357 		if (wpa_tdls_send_tpk_msg(sm, peer->sm_tmr.dest,
358 					  peer->sm_tmr.action_code,
359 					  peer->sm_tmr.dialog_token,
360 					  peer->sm_tmr.status_code,
361 					  peer->sm_tmr.peer_capab,
362 					  peer->initiator,
363 					  peer->sm_tmr.buf,
364 					  peer->sm_tmr.buf_len)) {
365 			wpa_printf(MSG_INFO, "TDLS: Failed to retry "
366 				   "transmission");
367 		}
368 
369 		eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
370 		eloop_register_timeout(peer->sm_tmr.timer / 1000,
371 				       (peer->sm_tmr.timer % 1000) * 1000,
372 				       wpa_tdls_tpk_retry_timeout, sm, peer);
373 	} else {
374 		eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
375 
376 		wpa_printf(MSG_DEBUG, "TDLS: Sending Teardown Request");
377 		wpa_tdls_do_teardown(sm, peer,
378 				     WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
379 	}
380 }
381 
382 
wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm * sm,struct wpa_tdls_peer * peer,u8 action_code)383 static void wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm *sm,
384 					      struct wpa_tdls_peer *peer,
385 					      u8 action_code)
386 {
387 	if (action_code == peer->sm_tmr.action_code) {
388 		wpa_printf(MSG_DEBUG, "TDLS: Retry timeout cancelled for "
389 			   "action_code=%u", action_code);
390 
391 		/* Cancel Timeout registered */
392 		eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
393 
394 		/* free all resources meant for retry */
395 		os_free(peer->sm_tmr.buf);
396 		peer->sm_tmr.buf = NULL;
397 
398 		peer->sm_tmr.count = 0;
399 		peer->sm_tmr.timer = 0;
400 		peer->sm_tmr.buf_len = 0;
401 		peer->sm_tmr.action_code = 0xff;
402 	} else {
403 		wpa_printf(MSG_INFO, "TDLS: Error in cancelling retry timeout "
404 			   "(Unknown action_code=%u)", action_code);
405 	}
406 }
407 
408 
wpa_tdls_generate_tpk(struct wpa_tdls_peer * peer,const u8 * own_addr,const u8 * bssid)409 static void wpa_tdls_generate_tpk(struct wpa_tdls_peer *peer,
410 				  const u8 *own_addr, const u8 *bssid)
411 {
412 	u8 key_input[SHA256_MAC_LEN];
413 	const u8 *nonce[2];
414 	size_t len[2];
415 	u8 data[3 * ETH_ALEN];
416 
417 	/* IEEE Std 802.11-2016 12.7.9.2:
418 	 * TPK-Key-Input = Hash(min(SNonce, ANonce) || max(SNonce, ANonce))
419 	 * Hash = SHA-256 for TDLS
420 	 */
421 	len[0] = WPA_NONCE_LEN;
422 	len[1] = WPA_NONCE_LEN;
423 	if (os_memcmp(peer->inonce, peer->rnonce, WPA_NONCE_LEN) < 0) {
424 		nonce[0] = peer->inonce;
425 		nonce[1] = peer->rnonce;
426 	} else {
427 		nonce[0] = peer->rnonce;
428 		nonce[1] = peer->inonce;
429 	}
430 	wpa_hexdump(MSG_DEBUG, "TDLS: min(Nonce)", nonce[0], WPA_NONCE_LEN);
431 	wpa_hexdump(MSG_DEBUG, "TDLS: max(Nonce)", nonce[1], WPA_NONCE_LEN);
432 	sha256_vector(2, nonce, len, key_input);
433 	wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-Key-Input",
434 			key_input, SHA256_MAC_LEN);
435 
436 	/*
437 	 * TPK = KDF-Hash-Length(TPK-Key-Input, "TDLS PMK",
438 	 *	min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID)
439 	 */
440 
441 	if (os_memcmp(own_addr, peer->addr, ETH_ALEN) < 0) {
442 		os_memcpy(data, own_addr, ETH_ALEN);
443 		os_memcpy(data + ETH_ALEN, peer->addr, ETH_ALEN);
444 	} else {
445 		os_memcpy(data, peer->addr, ETH_ALEN);
446 		os_memcpy(data + ETH_ALEN, own_addr, ETH_ALEN);
447 	}
448 	os_memcpy(data + 2 * ETH_ALEN, bssid, ETH_ALEN);
449 	wpa_hexdump(MSG_DEBUG, "TDLS: KDF Context", data, sizeof(data));
450 
451 	sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data),
452 		   (u8 *) &peer->tpk, sizeof(peer->tpk));
453 	wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-KCK",
454 			peer->tpk.kck, sizeof(peer->tpk.kck));
455 	wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-TK",
456 			peer->tpk.tk, sizeof(peer->tpk.tk));
457 	peer->tpk_set = 1;
458 }
459 
460 
461 /**
462  * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
463  * @kck: TPK-KCK
464  * @lnkid: Pointer to the beginning of Link Identifier IE
465  * @rsnie: Pointer to the beginning of RSN IE used for handshake
466  * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
467  * @ftie: Pointer to the beginning of FT IE
468  * @mic: Pointer for writing MIC
469  *
470  * Calculate MIC for TDLS frame.
471  */
wpa_tdls_ftie_mic(const u8 * kck,u8 trans_seq,const u8 * lnkid,const u8 * rsnie,const u8 * timeoutie,const u8 * ftie,u8 * mic)472 static int wpa_tdls_ftie_mic(const u8 *kck, u8 trans_seq, const u8 *lnkid,
473 			     const u8 *rsnie, const u8 *timeoutie,
474 			     const u8 *ftie, u8 *mic)
475 {
476 	u8 *buf, *pos;
477 	struct wpa_tdls_ftie *_ftie;
478 	const struct wpa_tdls_lnkid *_lnkid;
479 	int ret;
480 	int len = 2 * ETH_ALEN + 1 + 2 + lnkid[1] + 2 + rsnie[1] +
481 		2 + timeoutie[1] + 2 + ftie[1];
482 	buf = os_zalloc(len);
483 	if (!buf) {
484 		wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
485 		return -1;
486 	}
487 
488 	pos = buf;
489 	_lnkid = (const struct wpa_tdls_lnkid *) lnkid;
490 	/* 1) TDLS initiator STA MAC address */
491 	os_memcpy(pos, _lnkid->init_sta, ETH_ALEN);
492 	pos += ETH_ALEN;
493 	/* 2) TDLS responder STA MAC address */
494 	os_memcpy(pos, _lnkid->resp_sta, ETH_ALEN);
495 	pos += ETH_ALEN;
496 	/* 3) Transaction Sequence number */
497 	*pos++ = trans_seq;
498 	/* 4) Link Identifier IE */
499 	os_memcpy(pos, lnkid, 2 + lnkid[1]);
500 	pos += 2 + lnkid[1];
501 	/* 5) RSN IE */
502 	os_memcpy(pos, rsnie, 2 + rsnie[1]);
503 	pos += 2 + rsnie[1];
504 	/* 6) Timeout Interval IE */
505 	os_memcpy(pos, timeoutie, 2 + timeoutie[1]);
506 	pos += 2 + timeoutie[1];
507 	/* 7) FTIE, with the MIC field of the FTIE set to 0 */
508 	os_memcpy(pos, ftie, 2 + ftie[1]);
509 	_ftie = (struct wpa_tdls_ftie *) pos;
510 	os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
511 	pos += 2 + ftie[1];
512 
513 	wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
514 	wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
515 	ret = omac1_aes_128(kck, buf, pos - buf, mic);
516 	os_free(buf);
517 	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
518 	return ret;
519 }
520 
521 
522 /**
523  * wpa_tdls_key_mic_teardown - Calculate TDLS FTIE MIC for Teardown frame
524  * @kck: TPK-KCK
525  * @trans_seq: Transaction Sequence Number (4 - Teardown)
526  * @rcode: Reason code for Teardown
527  * @dtoken: Dialog Token used for that particular link
528  * @lnkid: Pointer to the beginning of Link Identifier IE
529  * @ftie: Pointer to the beginning of FT IE
530  * @mic: Pointer for writing MIC
531  *
532  * Calculate MIC for TDLS frame.
533  */
wpa_tdls_key_mic_teardown(const u8 * kck,u8 trans_seq,u16 rcode,u8 dtoken,const u8 * lnkid,const u8 * ftie,u8 * mic)534 static int wpa_tdls_key_mic_teardown(const u8 *kck, u8 trans_seq, u16 rcode,
535 				     u8 dtoken, const u8 *lnkid,
536 				     const u8 *ftie, u8 *mic)
537 {
538 	u8 *buf, *pos;
539 	struct wpa_tdls_ftie *_ftie;
540 	int ret;
541 	int len;
542 
543 	if (lnkid == NULL)
544 		return -1;
545 
546 	len = 2 + lnkid[1] + sizeof(rcode) + sizeof(dtoken) +
547 		sizeof(trans_seq) + 2 + ftie[1];
548 
549 	buf = os_zalloc(len);
550 	if (!buf) {
551 		wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
552 		return -1;
553 	}
554 
555 	pos = buf;
556 	/* 1) Link Identifier IE */
557 	os_memcpy(pos, lnkid, 2 + lnkid[1]);
558 	pos += 2 + lnkid[1];
559 	/* 2) Reason Code */
560 	WPA_PUT_LE16(pos, rcode);
561 	pos += sizeof(rcode);
562 	/* 3) Dialog token */
563 	*pos++ = dtoken;
564 	/* 4) Transaction Sequence number */
565 	*pos++ = trans_seq;
566 	/* 7) FTIE, with the MIC field of the FTIE set to 0 */
567 	os_memcpy(pos, ftie, 2 + ftie[1]);
568 	_ftie = (struct wpa_tdls_ftie *) pos;
569 	os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
570 	pos += 2 + ftie[1];
571 
572 	wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
573 	wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
574 	ret = omac1_aes_128(kck, buf, pos - buf, mic);
575 	os_free(buf);
576 	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
577 	return ret;
578 }
579 
580 
wpa_supplicant_verify_tdls_mic(u8 trans_seq,struct wpa_tdls_peer * peer,const u8 * lnkid,const u8 * timeoutie,const struct wpa_tdls_ftie * ftie)581 static int wpa_supplicant_verify_tdls_mic(u8 trans_seq,
582 					  struct wpa_tdls_peer *peer,
583 					  const u8 *lnkid, const u8 *timeoutie,
584 					  const struct wpa_tdls_ftie *ftie)
585 {
586 	u8 mic[16];
587 
588 	if (peer->tpk_set) {
589 		wpa_tdls_ftie_mic(peer->tpk.kck, trans_seq, lnkid,
590 				  peer->rsnie_p, timeoutie, (u8 *) ftie,
591 				  mic);
592 		if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
593 			wpa_printf(MSG_INFO, "TDLS: Invalid MIC in FTIE - "
594 				   "dropping packet");
595 			wpa_hexdump(MSG_DEBUG, "TDLS: Received MIC",
596 				    ftie->mic, 16);
597 			wpa_hexdump(MSG_DEBUG, "TDLS: Calculated MIC",
598 				    mic, 16);
599 			return -1;
600 		}
601 	} else {
602 		wpa_printf(MSG_WARNING, "TDLS: Could not verify TDLS MIC, "
603 			   "TPK not set - dropping packet");
604 		return -1;
605 	}
606 	return 0;
607 }
608 
609 
wpa_supplicant_verify_tdls_mic_teardown(u8 trans_seq,u16 rcode,u8 dtoken,struct wpa_tdls_peer * peer,const u8 * lnkid,const struct wpa_tdls_ftie * ftie)610 static int wpa_supplicant_verify_tdls_mic_teardown(
611 	u8 trans_seq, u16 rcode, u8 dtoken, struct wpa_tdls_peer *peer,
612 	const u8 *lnkid, const struct wpa_tdls_ftie *ftie)
613 {
614 	u8 mic[16];
615 
616 	if (peer->tpk_set) {
617 		wpa_tdls_key_mic_teardown(peer->tpk.kck, trans_seq, rcode,
618 					  dtoken, lnkid, (u8 *) ftie, mic);
619 		if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
620 			wpa_printf(MSG_INFO, "TDLS: Invalid MIC in Teardown - "
621 				   "dropping packet");
622 			return -1;
623 		}
624 	} else {
625 		wpa_printf(MSG_INFO, "TDLS: Could not verify TDLS Teardown "
626 			   "MIC, TPK not set - dropping packet");
627 		return -1;
628 	}
629 	return 0;
630 }
631 
632 
wpa_tdls_tpk_timeout(void * eloop_ctx,void * timeout_ctx)633 static void wpa_tdls_tpk_timeout(void *eloop_ctx, void *timeout_ctx)
634 {
635 	struct wpa_sm *sm = eloop_ctx;
636 	struct wpa_tdls_peer *peer = timeout_ctx;
637 
638 	/*
639 	 * On TPK lifetime expiration, we have an option of either tearing down
640 	 * the direct link or trying to re-initiate it. The selection of what
641 	 * to do is not strictly speaking controlled by our role in the expired
642 	 * link, but for now, use that to select whether to renew or tear down
643 	 * the link.
644 	 */
645 
646 	if (peer->initiator) {
647 		u8 addr[ETH_ALEN];
648 
649 		wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
650 			   " - try to renew", MAC2STR(peer->addr));
651 		/* cache the peer address before do_teardown */
652 		os_memcpy(addr, peer->addr, ETH_ALEN);
653 		wpa_tdls_do_teardown(sm, peer,
654 				     WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
655 		wpa_tdls_start(sm, addr);
656 	} else {
657 		wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
658 			   " - tear down", MAC2STR(peer->addr));
659 		wpa_tdls_do_teardown(sm, peer,
660 				     WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
661 	}
662 }
663 
664 
wpa_tdls_peer_remove_from_list(struct wpa_sm * sm,struct wpa_tdls_peer * peer)665 static void wpa_tdls_peer_remove_from_list(struct wpa_sm *sm,
666 					   struct wpa_tdls_peer *peer)
667 {
668 	struct wpa_tdls_peer *cur, *prev;
669 
670 	cur = sm->tdls;
671 	prev = NULL;
672 	while (cur && cur != peer) {
673 		prev = cur;
674 		cur = cur->next;
675 	}
676 
677 	if (cur != peer) {
678 		wpa_printf(MSG_ERROR, "TDLS: Could not find peer " MACSTR
679 			   " to remove it from the list",
680 			   MAC2STR(peer->addr));
681 		return;
682 	}
683 
684 	if (prev)
685 		prev->next = peer->next;
686 	else
687 		sm->tdls = peer->next;
688 }
689 
690 
wpa_tdls_peer_clear(struct wpa_sm * sm,struct wpa_tdls_peer * peer)691 static void wpa_tdls_peer_clear(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
692 {
693 	wpa_printf(MSG_DEBUG, "TDLS: Clear state for peer " MACSTR,
694 		   MAC2STR(peer->addr));
695 	eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
696 	eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
697 	peer->reconfig_key = 0;
698 	peer->initiator = 0;
699 	peer->tpk_in_progress = 0;
700 	os_free(peer->sm_tmr.buf);
701 	peer->sm_tmr.buf = NULL;
702 	os_free(peer->ht_capabilities);
703 	peer->ht_capabilities = NULL;
704 	os_free(peer->vht_capabilities);
705 	peer->vht_capabilities = NULL;
706 	os_free(peer->ext_capab);
707 	peer->ext_capab = NULL;
708 	os_free(peer->supp_channels);
709 	peer->supp_channels = NULL;
710 	os_free(peer->supp_oper_classes);
711 	peer->supp_oper_classes = NULL;
712 	peer->rsnie_i_len = peer->rsnie_p_len = 0;
713 	peer->cipher = 0;
714 	peer->qos_info = 0;
715 	peer->wmm_capable = 0;
716 	peer->tk_set = peer->tpk_set = peer->tpk_success = 0;
717 	peer->chan_switch_enabled = 0;
718 	os_memset(&peer->tpk, 0, sizeof(peer->tpk));
719 	os_memset(peer->inonce, 0, WPA_NONCE_LEN);
720 	os_memset(peer->rnonce, 0, WPA_NONCE_LEN);
721 }
722 
723 
wpa_tdls_peer_free(struct wpa_sm * sm,struct wpa_tdls_peer * peer)724 static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
725 {
726 	wpa_tdls_peer_clear(sm, peer);
727 	wpa_tdls_peer_remove_from_list(sm, peer);
728 	os_free(peer);
729 }
730 
731 
wpa_tdls_linkid(struct wpa_sm * sm,struct wpa_tdls_peer * peer,struct wpa_tdls_lnkid * lnkid)732 static void wpa_tdls_linkid(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
733 			    struct wpa_tdls_lnkid *lnkid)
734 {
735 	lnkid->ie_type = WLAN_EID_LINK_ID;
736 	lnkid->ie_len = 3 * ETH_ALEN;
737 	os_memcpy(lnkid->bssid, sm->bssid, ETH_ALEN);
738 	if (peer->initiator) {
739 		os_memcpy(lnkid->init_sta, sm->own_addr, ETH_ALEN);
740 		os_memcpy(lnkid->resp_sta, peer->addr, ETH_ALEN);
741 	} else {
742 		os_memcpy(lnkid->init_sta, peer->addr, ETH_ALEN);
743 		os_memcpy(lnkid->resp_sta, sm->own_addr, ETH_ALEN);
744 	}
745 }
746 
747 
wpa_tdls_send_teardown(struct wpa_sm * sm,const u8 * addr,u16 reason_code)748 static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
749 				  u16 reason_code)
750 {
751 	struct wpa_tdls_peer *peer;
752 	struct wpa_tdls_ftie *ftie;
753 	struct wpa_tdls_lnkid lnkid;
754 	u8 dialog_token;
755 	u8 *rbuf, *pos;
756 	int ielen;
757 
758 	if (sm->tdls_disabled || !sm->tdls_supported)
759 		return -1;
760 
761 	/* Find the node and free from the list */
762 	for (peer = sm->tdls; peer; peer = peer->next) {
763 		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
764 			break;
765 	}
766 
767 	if (peer == NULL) {
768 		wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
769 			   "Teardown " MACSTR, MAC2STR(addr));
770 		return 0;
771 	}
772 
773 	/* Cancel active channel switch before teardown */
774 	if (peer->chan_switch_enabled) {
775 		wpa_printf(MSG_DEBUG, "TDLS: First returning link with " MACSTR
776 			   " to base channel", MAC2STR(addr));
777 		wpa_sm_tdls_disable_channel_switch(sm, peer->addr);
778 	}
779 
780 	dialog_token = peer->dtoken;
781 
782 	wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown for " MACSTR,
783 		   MAC2STR(addr));
784 
785 	ielen = 0;
786 	if (wpa_tdls_get_privacy(sm) && peer->tpk_set && peer->tpk_success) {
787 		/* To add FTIE for Teardown request and compute MIC */
788 		ielen += sizeof(*ftie);
789 #ifdef CONFIG_TDLS_TESTING
790 		if (tdls_testing & TDLS_TESTING_LONG_FRAME)
791 			ielen += 170;
792 #endif /* CONFIG_TDLS_TESTING */
793 	}
794 
795 	rbuf = os_zalloc(ielen + 1);
796 	if (rbuf == NULL)
797 		return -1;
798 	pos = rbuf;
799 
800 	if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
801 		goto skip_ies;
802 
803 	ftie = (struct wpa_tdls_ftie *) pos;
804 	ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
805 	/* Using the recent nonce which should be for CONFIRM frame */
806 	os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
807 	os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
808 	ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
809 	pos = (u8 *) (ftie + 1);
810 #ifdef CONFIG_TDLS_TESTING
811 	if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
812 		wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
813 			   "FTIE");
814 		ftie->ie_len += 170;
815 		*pos++ = 255; /* FTIE subelem */
816 		*pos++ = 168; /* FTIE subelem length */
817 		pos += 168;
818 	}
819 #endif /* CONFIG_TDLS_TESTING */
820 	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TDLS Teardown handshake",
821 		    (u8 *) ftie, pos - (u8 *) ftie);
822 
823 	/* compute MIC before sending */
824 	wpa_tdls_linkid(sm, peer, &lnkid);
825 	wpa_tdls_key_mic_teardown(peer->tpk.kck, 4, reason_code,
826 				  dialog_token, (u8 *) &lnkid, (u8 *) ftie,
827 				  ftie->mic);
828 
829 skip_ies:
830 	/* TODO: register for a Timeout handler, if Teardown is not received at
831 	 * the other end, then try again another time */
832 
833 	/* request driver to send Teardown using this FTIE */
834 	wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_TEARDOWN, 0,
835 			  reason_code, 0, peer->initiator, rbuf, pos - rbuf);
836 	os_free(rbuf);
837 
838 	return 0;
839 }
840 
841 
wpa_tdls_teardown_link(struct wpa_sm * sm,const u8 * addr,u16 reason_code)842 int wpa_tdls_teardown_link(struct wpa_sm *sm, const u8 *addr, u16 reason_code)
843 {
844 	struct wpa_tdls_peer *peer;
845 
846 	if (sm->tdls_disabled || !sm->tdls_supported)
847 		return -1;
848 
849 	for (peer = sm->tdls; peer; peer = peer->next) {
850 		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
851 			break;
852 	}
853 
854 	if (peer == NULL) {
855 		wpa_printf(MSG_DEBUG, "TDLS: Could not find peer " MACSTR
856 		   " for link Teardown", MAC2STR(addr));
857 		return -1;
858 	}
859 
860 	if (!peer->tpk_success) {
861 		wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
862 		   " not connected - cannot Teardown link", MAC2STR(addr));
863 		return -1;
864 	}
865 
866 	return wpa_tdls_do_teardown(sm, peer, reason_code);
867 }
868 
869 
wpa_tdls_disable_peer_link(struct wpa_sm * sm,struct wpa_tdls_peer * peer)870 static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
871 				       struct wpa_tdls_peer *peer)
872 {
873 	wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
874 	wpa_tdls_peer_free(sm, peer);
875 }
876 
877 
wpa_tdls_disable_unreachable_link(struct wpa_sm * sm,const u8 * addr)878 void wpa_tdls_disable_unreachable_link(struct wpa_sm *sm, const u8 *addr)
879 {
880 	struct wpa_tdls_peer *peer;
881 
882 	for (peer = sm->tdls; peer; peer = peer->next) {
883 		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
884 			break;
885 	}
886 
887 	if (!peer || !peer->tpk_success) {
888 		wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
889 			   " not connected - cannot teardown unreachable link",
890 			   MAC2STR(addr));
891 		return;
892 	}
893 
894 	if (wpa_tdls_is_external_setup(sm)) {
895 		/*
896 		 * Get us on the base channel, disable the link, send a
897 		 * teardown packet through the AP, and then reset link data.
898 		 */
899 		if (peer->chan_switch_enabled)
900 			wpa_sm_tdls_disable_channel_switch(sm, peer->addr);
901 		wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, addr);
902 		wpa_tdls_send_teardown(sm, addr,
903 				       WLAN_REASON_TDLS_TEARDOWN_UNREACHABLE);
904 		wpa_tdls_peer_free(sm, peer);
905 	} else {
906 		wpa_tdls_disable_peer_link(sm, peer);
907 	}
908 }
909 
910 
wpa_tdls_get_link_status(struct wpa_sm * sm,const u8 * addr)911 const char * wpa_tdls_get_link_status(struct wpa_sm *sm, const u8 *addr)
912 {
913 	struct wpa_tdls_peer *peer;
914 
915 	if (sm->tdls_disabled || !sm->tdls_supported)
916 		return "disabled";
917 
918 	for (peer = sm->tdls; peer; peer = peer->next) {
919 		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
920 			break;
921 	}
922 
923 	if (peer == NULL)
924 		return "peer does not exist";
925 
926 	if (!peer->tpk_success)
927 		return "peer not connected";
928 
929 	return "connected";
930 }
931 
932 
wpa_tdls_recv_teardown(struct wpa_sm * sm,const u8 * src_addr,const u8 * buf,size_t len)933 static int wpa_tdls_recv_teardown(struct wpa_sm *sm, const u8 *src_addr,
934 				  const u8 *buf, size_t len)
935 {
936 	struct wpa_tdls_peer *peer = NULL;
937 	struct wpa_tdls_ftie *ftie;
938 	struct wpa_tdls_lnkid *lnkid;
939 	struct wpa_eapol_ie_parse kde;
940 	u16 reason_code;
941 	const u8 *pos;
942 	int ielen;
943 
944 	/* Find the node and free from the list */
945 	for (peer = sm->tdls; peer; peer = peer->next) {
946 		if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
947 			break;
948 	}
949 
950 	if (peer == NULL) {
951 		wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
952 			   "Teardown " MACSTR, MAC2STR(src_addr));
953 		return 0;
954 	}
955 
956 	pos = buf;
957 	pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
958 
959 	reason_code = WPA_GET_LE16(pos);
960 	pos += 2;
961 
962 	wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown Request from " MACSTR
963 		   " (reason code %u)", MAC2STR(src_addr), reason_code);
964 
965 	ielen = len - (pos - buf); /* start of IE in buf */
966 
967 	/*
968 	 * Don't reject the message if failing to parse IEs. The IEs we need are
969 	 * explicitly checked below. Some APs may add arbitrary padding to the
970 	 * end of short TDLS frames and that would look like invalid IEs.
971 	 */
972 	if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0)
973 		wpa_printf(MSG_DEBUG,
974 			   "TDLS: Failed to parse IEs in Teardown - ignore as an interop workaround");
975 
976 	if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
977 		wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TDLS "
978 			   "Teardown");
979 		return -1;
980 	}
981 	lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
982 
983 	if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
984 		goto skip_ftie;
985 
986 	if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
987 		wpa_printf(MSG_INFO, "TDLS: No FTIE in TDLS Teardown");
988 		return -1;
989 	}
990 
991 	ftie = (struct wpa_tdls_ftie *) kde.ftie;
992 
993 	/* Process MIC check to see if TDLS Teardown is right */
994 	if (wpa_supplicant_verify_tdls_mic_teardown(4, reason_code,
995 						    peer->dtoken, peer,
996 						    (u8 *) lnkid, ftie) < 0) {
997 		wpa_printf(MSG_DEBUG, "TDLS: MIC failure for TDLS "
998 			   "Teardown Request from " MACSTR, MAC2STR(src_addr));
999 		return -1;
1000 	}
1001 
1002 skip_ftie:
1003 	/*
1004 	 * Request the driver to disable the direct link and clear associated
1005 	 * keys.
1006 	 */
1007 	wpa_tdls_disable_peer_link(sm, peer);
1008 	return 0;
1009 }
1010 
1011 
1012 /**
1013  * wpa_tdls_send_error - To send suitable TDLS status response with
1014  *	appropriate status code mentioning reason for error/failure.
1015  * @dst 	- MAC addr of Peer station
1016  * @tdls_action - TDLS frame type for which error code is sent
1017  * @initiator   - was this end the initiator of the connection
1018  * @status 	- status code mentioning reason
1019  */
1020 
wpa_tdls_send_error(struct wpa_sm * sm,const u8 * dst,u8 tdls_action,u8 dialog_token,int initiator,u16 status)1021 static int wpa_tdls_send_error(struct wpa_sm *sm, const u8 *dst,
1022 			       u8 tdls_action, u8 dialog_token, int initiator,
1023 			       u16 status)
1024 {
1025 	wpa_printf(MSG_DEBUG, "TDLS: Sending error to " MACSTR
1026 		   " (action=%u status=%u)",
1027 		   MAC2STR(dst), tdls_action, status);
1028 	return wpa_tdls_tpk_send(sm, dst, tdls_action, dialog_token, status,
1029 				 0, initiator, NULL, 0);
1030 }
1031 
1032 
1033 static struct wpa_tdls_peer *
wpa_tdls_add_peer(struct wpa_sm * sm,const u8 * addr,int * existing)1034 wpa_tdls_add_peer(struct wpa_sm *sm, const u8 *addr, int *existing)
1035 {
1036 	struct wpa_tdls_peer *peer;
1037 
1038 	if (existing)
1039 		*existing = 0;
1040 	for (peer = sm->tdls; peer; peer = peer->next) {
1041 		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0) {
1042 			if (existing)
1043 				*existing = 1;
1044 			return peer; /* re-use existing entry */
1045 		}
1046 	}
1047 
1048 	wpa_printf(MSG_INFO, "TDLS: Creating peer entry for " MACSTR,
1049 		   MAC2STR(addr));
1050 
1051 	peer = os_zalloc(sizeof(*peer));
1052 	if (peer == NULL)
1053 		return NULL;
1054 
1055 	os_memcpy(peer->addr, addr, ETH_ALEN);
1056 	peer->next = sm->tdls;
1057 	sm->tdls = peer;
1058 
1059 	return peer;
1060 }
1061 
1062 
wpa_tdls_send_tpk_m1(struct wpa_sm * sm,struct wpa_tdls_peer * peer)1063 static int wpa_tdls_send_tpk_m1(struct wpa_sm *sm,
1064 				struct wpa_tdls_peer *peer)
1065 {
1066 	size_t buf_len;
1067 	struct wpa_tdls_timeoutie timeoutie;
1068 	u16 rsn_capab;
1069 	struct wpa_tdls_ftie *ftie;
1070 	u8 *rbuf, *pos, *count_pos;
1071 	u16 count;
1072 	struct rsn_ie_hdr *hdr;
1073 	int status;
1074 
1075 	if (!wpa_tdls_get_privacy(sm)) {
1076 		wpa_printf(MSG_DEBUG, "TDLS: No security used on the link");
1077 		peer->rsnie_i_len = 0;
1078 		goto skip_rsnie;
1079 	}
1080 
1081 	/*
1082 	 * TPK Handshake Message 1:
1083 	 * FTIE: ANonce=0, SNonce=initiator nonce MIC=0, DataKDs=(RSNIE_I,
1084 	 * Timeout Interval IE))
1085 	 */
1086 
1087 	/* Filling RSN IE */
1088 	hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
1089 	hdr->elem_id = WLAN_EID_RSN;
1090 	WPA_PUT_LE16(hdr->version, RSN_VERSION);
1091 
1092 	pos = (u8 *) (hdr + 1);
1093 	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1094 	pos += RSN_SELECTOR_LEN;
1095 	count_pos = pos;
1096 	pos += 2;
1097 
1098 	count = 0;
1099 
1100 	/*
1101 	 * AES-CCMP is the default Encryption preferred for TDLS, so
1102 	 * RSN IE is filled only with CCMP CIPHER
1103 	 * Note: TKIP is not used to encrypt TDLS link.
1104 	 *
1105 	 * Regardless of the cipher used on the AP connection, select CCMP
1106 	 * here.
1107 	 */
1108 	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1109 	pos += RSN_SELECTOR_LEN;
1110 	count++;
1111 
1112 	WPA_PUT_LE16(count_pos, count);
1113 
1114 	WPA_PUT_LE16(pos, 1);
1115 	pos += 2;
1116 	RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1117 	pos += RSN_SELECTOR_LEN;
1118 
1119 	rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1120 	rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1121 #ifdef CONFIG_TDLS_TESTING
1122 	if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
1123 		wpa_printf(MSG_DEBUG, "TDLS: Use alternative RSN IE for "
1124 			   "testing");
1125 		rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1126 	}
1127 #endif /* CONFIG_TDLS_TESTING */
1128 	WPA_PUT_LE16(pos, rsn_capab);
1129 	pos += 2;
1130 #ifdef CONFIG_TDLS_TESTING
1131 	if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
1132 		/* Number of PMKIDs */
1133 		*pos++ = 0x00;
1134 		*pos++ = 0x00;
1135 	}
1136 #endif /* CONFIG_TDLS_TESTING */
1137 
1138 	hdr->len = (pos - peer->rsnie_i) - 2;
1139 	peer->rsnie_i_len = pos - peer->rsnie_i;
1140 	wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
1141 		    peer->rsnie_i, peer->rsnie_i_len);
1142 
1143 skip_rsnie:
1144 	buf_len = 0;
1145 	if (wpa_tdls_get_privacy(sm))
1146 		buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1147 			sizeof(struct wpa_tdls_timeoutie);
1148 #ifdef CONFIG_TDLS_TESTING
1149 	if (wpa_tdls_get_privacy(sm) &&
1150 	    (tdls_testing & TDLS_TESTING_LONG_FRAME))
1151 		buf_len += 170;
1152 	if (tdls_testing & TDLS_TESTING_DIFF_BSSID)
1153 		buf_len += sizeof(struct wpa_tdls_lnkid);
1154 #endif /* CONFIG_TDLS_TESTING */
1155 	rbuf = os_zalloc(buf_len + 1);
1156 	if (rbuf == NULL) {
1157 		wpa_tdls_peer_free(sm, peer);
1158 		return -1;
1159 	}
1160 	pos = rbuf;
1161 
1162 	if (!wpa_tdls_get_privacy(sm))
1163 		goto skip_ies;
1164 
1165 	/* Initiator RSN IE */
1166 	pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
1167 
1168 	ftie = (struct wpa_tdls_ftie *) pos;
1169 	ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1170 	ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1171 
1172 	if (os_get_random(peer->inonce, WPA_NONCE_LEN)) {
1173 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1174 			"TDLS: Failed to get random data for initiator Nonce");
1175 		os_free(rbuf);
1176 		wpa_tdls_peer_free(sm, peer);
1177 		return -1;
1178 	}
1179 	peer->tk_set = 0; /* A new nonce results in a new TK */
1180 	wpa_hexdump(MSG_DEBUG, "TDLS: Initiator Nonce for TPK handshake",
1181 		    peer->inonce, WPA_NONCE_LEN);
1182 	os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1183 
1184 	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK Handshake M1",
1185 		    (u8 *) ftie, sizeof(struct wpa_tdls_ftie));
1186 
1187 	pos = (u8 *) (ftie + 1);
1188 
1189 #ifdef CONFIG_TDLS_TESTING
1190 	if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1191 		wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1192 			   "FTIE");
1193 		ftie->ie_len += 170;
1194 		*pos++ = 255; /* FTIE subelem */
1195 		*pos++ = 168; /* FTIE subelem length */
1196 		pos += 168;
1197 	}
1198 #endif /* CONFIG_TDLS_TESTING */
1199 
1200 	/* Lifetime */
1201 	peer->lifetime = TPK_LIFETIME;
1202 #ifdef CONFIG_TDLS_TESTING
1203 	if (tdls_testing & TDLS_TESTING_SHORT_LIFETIME) {
1204 		wpa_printf(MSG_DEBUG, "TDLS: Testing - use short TPK "
1205 			   "lifetime");
1206 		peer->lifetime = 301;
1207 	}
1208 	if (tdls_testing & TDLS_TESTING_LONG_LIFETIME) {
1209 		wpa_printf(MSG_DEBUG, "TDLS: Testing - use long TPK "
1210 			   "lifetime");
1211 		peer->lifetime = 0xffffffff;
1212 	}
1213 #endif /* CONFIG_TDLS_TESTING */
1214 	pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1215 				     sizeof(timeoutie), peer->lifetime);
1216 	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
1217 
1218 skip_ies:
1219 
1220 #ifdef CONFIG_TDLS_TESTING
1221 	if (tdls_testing & TDLS_TESTING_DIFF_BSSID) {
1222 		struct wpa_tdls_lnkid *l = (struct wpa_tdls_lnkid *) pos;
1223 
1224 		wpa_printf(MSG_DEBUG, "TDLS: Testing - use incorrect BSSID in "
1225 			   "Link Identifier");
1226 		wpa_tdls_linkid(sm, peer, l);
1227 		l->bssid[5] ^= 0x01;
1228 		pos += sizeof(*l);
1229 	}
1230 #endif /* CONFIG_TDLS_TESTING */
1231 
1232 	wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Request / TPK "
1233 		   "Handshake Message 1 (peer " MACSTR ")",
1234 		   MAC2STR(peer->addr));
1235 
1236 	status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_SETUP_REQUEST,
1237 				   1, 0, 0, peer->initiator, rbuf, pos - rbuf);
1238 	os_free(rbuf);
1239 
1240 	return status;
1241 }
1242 
1243 
wpa_tdls_send_tpk_m2(struct wpa_sm * sm,const unsigned char * src_addr,u8 dtoken,struct wpa_tdls_lnkid * lnkid,const struct wpa_tdls_peer * peer)1244 static int wpa_tdls_send_tpk_m2(struct wpa_sm *sm,
1245 				const unsigned char *src_addr, u8 dtoken,
1246 				struct wpa_tdls_lnkid *lnkid,
1247 				const struct wpa_tdls_peer *peer)
1248 {
1249 	u8 *rbuf, *pos;
1250 	size_t buf_len;
1251 	u32 lifetime;
1252 	struct wpa_tdls_timeoutie timeoutie;
1253 	struct wpa_tdls_ftie *ftie;
1254 	int status;
1255 
1256 	buf_len = 0;
1257 	if (wpa_tdls_get_privacy(sm)) {
1258 		/* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1259 		 * Lifetime */
1260 		buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1261 			sizeof(struct wpa_tdls_timeoutie);
1262 #ifdef CONFIG_TDLS_TESTING
1263 		if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1264 			buf_len += 170;
1265 #endif /* CONFIG_TDLS_TESTING */
1266 	}
1267 
1268 	rbuf = os_zalloc(buf_len + 1);
1269 	if (rbuf == NULL)
1270 		return -1;
1271 	pos = rbuf;
1272 
1273 	if (!wpa_tdls_get_privacy(sm))
1274 		goto skip_ies;
1275 
1276 	/* Peer RSN IE */
1277 	pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1278 
1279 	ftie = (struct wpa_tdls_ftie *) pos;
1280 	ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1281 	/* TODO: ftie->mic_control to set 2-RESPONSE */
1282 	os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1283 	os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1284 	ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1285 	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK M2",
1286 		    (u8 *) ftie, sizeof(*ftie));
1287 
1288 	pos = (u8 *) (ftie + 1);
1289 
1290 #ifdef CONFIG_TDLS_TESTING
1291 	if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1292 		wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1293 			   "FTIE");
1294 		ftie->ie_len += 170;
1295 		*pos++ = 255; /* FTIE subelem */
1296 		*pos++ = 168; /* FTIE subelem length */
1297 		pos += 168;
1298 	}
1299 #endif /* CONFIG_TDLS_TESTING */
1300 
1301 	/* Lifetime */
1302 	lifetime = peer->lifetime;
1303 #ifdef CONFIG_TDLS_TESTING
1304 	if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_RESP) {
1305 		wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1306 			   "lifetime in response");
1307 		lifetime++;
1308 	}
1309 #endif /* CONFIG_TDLS_TESTING */
1310 	pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1311 				     sizeof(timeoutie), lifetime);
1312 	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds from initiator",
1313 		   lifetime);
1314 
1315 	/* compute MIC before sending */
1316 	wpa_tdls_ftie_mic(peer->tpk.kck, 2, (u8 *) lnkid, peer->rsnie_p,
1317 			  (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
1318 #ifdef CONFIG_TDLS_TESTING
1319 	if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
1320 		wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
1321 		ftie->mic[0] ^= 0x01;
1322 	}
1323 #endif /* CONFIG_TDLS_TESTING */
1324 
1325 skip_ies:
1326 	status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE,
1327 				   dtoken, 0, 0, peer->initiator, rbuf,
1328 				   pos - rbuf);
1329 	os_free(rbuf);
1330 
1331 	return status;
1332 }
1333 
1334 
wpa_tdls_send_tpk_m3(struct wpa_sm * sm,const unsigned char * src_addr,u8 dtoken,struct wpa_tdls_lnkid * lnkid,const struct wpa_tdls_peer * peer)1335 static int wpa_tdls_send_tpk_m3(struct wpa_sm *sm,
1336 				const unsigned char *src_addr, u8 dtoken,
1337 				struct wpa_tdls_lnkid *lnkid,
1338 				const struct wpa_tdls_peer *peer)
1339 {
1340 	u8 *rbuf, *pos;
1341 	size_t buf_len;
1342 	struct wpa_tdls_ftie *ftie;
1343 	struct wpa_tdls_timeoutie timeoutie;
1344 	u32 lifetime;
1345 	int status;
1346 	u32 peer_capab = 0;
1347 
1348 	buf_len = 0;
1349 	if (wpa_tdls_get_privacy(sm)) {
1350 		/* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
1351 		 * Lifetime */
1352 		buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
1353 			sizeof(struct wpa_tdls_timeoutie);
1354 #ifdef CONFIG_TDLS_TESTING
1355 		if (tdls_testing & TDLS_TESTING_LONG_FRAME)
1356 			buf_len += 170;
1357 #endif /* CONFIG_TDLS_TESTING */
1358 	}
1359 
1360 	rbuf = os_zalloc(buf_len + 1);
1361 	if (rbuf == NULL)
1362 		return -1;
1363 	pos = rbuf;
1364 
1365 	if (!wpa_tdls_get_privacy(sm))
1366 		goto skip_ies;
1367 
1368 	/* Peer RSN IE */
1369 	pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
1370 
1371 	ftie = (struct wpa_tdls_ftie *) pos;
1372 	ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
1373 	/*TODO: ftie->mic_control to set 3-CONFIRM */
1374 	os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
1375 	os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
1376 	ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
1377 
1378 	pos = (u8 *) (ftie + 1);
1379 
1380 #ifdef CONFIG_TDLS_TESTING
1381 	if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
1382 		wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
1383 			   "FTIE");
1384 		ftie->ie_len += 170;
1385 		*pos++ = 255; /* FTIE subelem */
1386 		*pos++ = 168; /* FTIE subelem length */
1387 		pos += 168;
1388 	}
1389 #endif /* CONFIG_TDLS_TESTING */
1390 
1391 	/* Lifetime */
1392 	lifetime = peer->lifetime;
1393 #ifdef CONFIG_TDLS_TESTING
1394 	if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_CONF) {
1395 		wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
1396 			   "lifetime in confirm");
1397 		lifetime++;
1398 	}
1399 #endif /* CONFIG_TDLS_TESTING */
1400 	pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1401 				     sizeof(timeoutie), lifetime);
1402 	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds",
1403 		   lifetime);
1404 
1405 	/* compute MIC before sending */
1406 	wpa_tdls_ftie_mic(peer->tpk.kck, 3, (u8 *) lnkid, peer->rsnie_p,
1407 			  (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
1408 #ifdef CONFIG_TDLS_TESTING
1409 	if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
1410 		wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
1411 		ftie->mic[0] ^= 0x01;
1412 	}
1413 #endif /* CONFIG_TDLS_TESTING */
1414 
1415 skip_ies:
1416 
1417 	if (peer->vht_capabilities)
1418 		peer_capab |= TDLS_PEER_VHT;
1419 	if (peer->ht_capabilities)
1420 		peer_capab |= TDLS_PEER_HT;
1421 	if (peer->wmm_capable)
1422 		peer_capab |= TDLS_PEER_WMM;
1423 
1424 	status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
1425 				   dtoken, 0, peer_capab, peer->initiator,
1426 				   rbuf, pos - rbuf);
1427 	os_free(rbuf);
1428 
1429 	return status;
1430 }
1431 
1432 
wpa_tdls_send_discovery_response(struct wpa_sm * sm,struct wpa_tdls_peer * peer,u8 dialog_token)1433 static int wpa_tdls_send_discovery_response(struct wpa_sm *sm,
1434 					    struct wpa_tdls_peer *peer,
1435 					    u8 dialog_token)
1436 {
1437 	size_t buf_len = 0;
1438 	struct wpa_tdls_timeoutie timeoutie;
1439 	u16 rsn_capab;
1440 	u8 *rbuf, *pos, *count_pos;
1441 	u16 count;
1442 	struct rsn_ie_hdr *hdr;
1443 	int status;
1444 
1445 	wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Discovery Response "
1446 		   "(peer " MACSTR ")", MAC2STR(peer->addr));
1447 	if (!wpa_tdls_get_privacy(sm))
1448 		goto skip_rsn_ies;
1449 
1450 	/* Filling RSN IE */
1451 	hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
1452 	hdr->elem_id = WLAN_EID_RSN;
1453 	WPA_PUT_LE16(hdr->version, RSN_VERSION);
1454 	pos = (u8 *) (hdr + 1);
1455 	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
1456 	pos += RSN_SELECTOR_LEN;
1457 	count_pos = pos;
1458 	pos += 2;
1459 	count = 0;
1460 
1461 	/*
1462 	* AES-CCMP is the default encryption preferred for TDLS, so
1463 	* RSN IE is filled only with CCMP cipher suite.
1464 	* Note: TKIP is not used to encrypt TDLS link.
1465 	*
1466 	* Regardless of the cipher used on the AP connection, select CCMP
1467 	* here.
1468 	*/
1469 	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1470 	pos += RSN_SELECTOR_LEN;
1471 	count++;
1472 	WPA_PUT_LE16(count_pos, count);
1473 	WPA_PUT_LE16(pos, 1);
1474 	pos += 2;
1475 	RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
1476 	pos += RSN_SELECTOR_LEN;
1477 
1478 	rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
1479 	rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
1480 	WPA_PUT_LE16(pos, rsn_capab);
1481 	pos += 2;
1482 	hdr->len = (pos - (u8 *) hdr) - 2;
1483 	peer->rsnie_i_len = pos - peer->rsnie_i;
1484 
1485 	wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for Discovery Response",
1486 		    (u8 *) hdr, hdr->len + 2);
1487 skip_rsn_ies:
1488 	buf_len = 0;
1489 	if (wpa_tdls_get_privacy(sm)) {
1490 		/* Peer RSN IE, Lifetime */
1491 		buf_len += peer->rsnie_i_len +
1492 			sizeof(struct wpa_tdls_timeoutie);
1493 	}
1494 	rbuf = os_zalloc(buf_len + 1);
1495 	if (rbuf == NULL) {
1496 		wpa_tdls_peer_free(sm, peer);
1497 		return -1;
1498 	}
1499 	pos = rbuf;
1500 
1501 	if (!wpa_tdls_get_privacy(sm))
1502 		goto skip_ies;
1503 	/* Initiator RSN IE */
1504 	pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
1505 	/* Lifetime */
1506 	peer->lifetime = TPK_LIFETIME;
1507 	pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
1508 				     sizeof(timeoutie), peer->lifetime);
1509 	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
1510 skip_ies:
1511 	status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_DISCOVERY_RESPONSE,
1512 				   dialog_token, 0, 0, 0, rbuf, pos - rbuf);
1513 	os_free(rbuf);
1514 
1515 	return status;
1516 }
1517 
1518 
1519 static int
wpa_tdls_process_discovery_request(struct wpa_sm * sm,const u8 * addr,const u8 * buf,size_t len)1520 wpa_tdls_process_discovery_request(struct wpa_sm *sm, const u8 *addr,
1521 				   const u8 *buf, size_t len)
1522 {
1523 	struct wpa_eapol_ie_parse kde;
1524 	const struct wpa_tdls_lnkid *lnkid;
1525 	struct wpa_tdls_peer *peer;
1526 	size_t min_req_len = sizeof(struct wpa_tdls_frame) +
1527 		1 /* dialog token */ + sizeof(struct wpa_tdls_lnkid);
1528 	u8 dialog_token;
1529 
1530 	wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from " MACSTR,
1531 		   MAC2STR(addr));
1532 
1533 	if (len < min_req_len) {
1534 		wpa_printf(MSG_DEBUG, "TDLS Discovery Request is too short: "
1535 			   "%d", (int) len);
1536 		return -1;
1537 	}
1538 
1539 	dialog_token = buf[sizeof(struct wpa_tdls_frame)];
1540 
1541 	/*
1542 	 * Some APs will tack on a weird IE to the end of a TDLS
1543 	 * discovery request packet. This needn't fail the response,
1544 	 * since the required IE are verified separately.
1545 	 */
1546 	if (wpa_supplicant_parse_ies(buf + sizeof(struct wpa_tdls_frame) + 1,
1547 				     len - (sizeof(struct wpa_tdls_frame) + 1),
1548 				     &kde) < 0) {
1549 		wpa_printf(MSG_DEBUG,
1550 			   "TDLS: Failed to parse IEs in Discovery Request - ignore as an interop workaround");
1551 	}
1552 
1553 	if (!kde.lnkid) {
1554 		wpa_printf(MSG_DEBUG, "TDLS: Link ID not found in Discovery "
1555 			   "Request");
1556 		return -1;
1557 	}
1558 
1559 	lnkid = (const struct wpa_tdls_lnkid *) kde.lnkid;
1560 
1561 	if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1562 		wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from different "
1563 			   " BSS " MACSTR, MAC2STR(lnkid->bssid));
1564 		return -1;
1565 	}
1566 
1567 	peer = wpa_tdls_add_peer(sm, addr, NULL);
1568 	if (peer == NULL)
1569 		return -1;
1570 
1571 	return wpa_tdls_send_discovery_response(sm, peer, dialog_token);
1572 }
1573 
1574 
wpa_tdls_send_discovery_request(struct wpa_sm * sm,const u8 * addr)1575 int wpa_tdls_send_discovery_request(struct wpa_sm *sm, const u8 *addr)
1576 {
1577 	if (sm->tdls_disabled || !sm->tdls_supported)
1578 		return -1;
1579 
1580 	wpa_printf(MSG_DEBUG, "TDLS: Sending Discovery Request to peer "
1581 		   MACSTR, MAC2STR(addr));
1582 	return wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_DISCOVERY_REQUEST,
1583 				 1, 0, 0, 1, NULL, 0);
1584 }
1585 
1586 
copy_supp_rates(const struct wpa_eapol_ie_parse * kde,struct wpa_tdls_peer * peer)1587 static int copy_supp_rates(const struct wpa_eapol_ie_parse *kde,
1588 			   struct wpa_tdls_peer *peer)
1589 {
1590 	if (!kde->supp_rates) {
1591 		wpa_printf(MSG_DEBUG, "TDLS: No supported rates received");
1592 		return -1;
1593 	}
1594 	peer->supp_rates_len = merge_byte_arrays(
1595 		peer->supp_rates, sizeof(peer->supp_rates),
1596 		kde->supp_rates + 2, kde->supp_rates_len - 2,
1597 		kde->ext_supp_rates ? kde->ext_supp_rates + 2 : NULL,
1598 		kde->ext_supp_rates ? kde->ext_supp_rates_len - 2 : 0);
1599 	return 0;
1600 }
1601 
1602 
copy_peer_ht_capab(const struct wpa_eapol_ie_parse * kde,struct wpa_tdls_peer * peer)1603 static int copy_peer_ht_capab(const struct wpa_eapol_ie_parse *kde,
1604 			      struct wpa_tdls_peer *peer)
1605 {
1606 	if (!kde->ht_capabilities) {
1607 		wpa_printf(MSG_DEBUG, "TDLS: No supported ht capabilities "
1608 			   "received");
1609 		return 0;
1610 	}
1611 
1612 	if (!peer->ht_capabilities) {
1613 		peer->ht_capabilities =
1614                         os_zalloc(sizeof(struct ieee80211_ht_capabilities));
1615 		if (peer->ht_capabilities == NULL)
1616                         return -1;
1617 	}
1618 
1619 	os_memcpy(peer->ht_capabilities, kde->ht_capabilities,
1620                   sizeof(struct ieee80211_ht_capabilities));
1621 	wpa_hexdump(MSG_DEBUG, "TDLS: Peer HT capabilities",
1622 		    (u8 *) peer->ht_capabilities,
1623 		    sizeof(struct ieee80211_ht_capabilities));
1624 
1625 	return 0;
1626 }
1627 
1628 
copy_peer_vht_capab(const struct wpa_eapol_ie_parse * kde,struct wpa_tdls_peer * peer)1629 static int copy_peer_vht_capab(const struct wpa_eapol_ie_parse *kde,
1630 			      struct wpa_tdls_peer *peer)
1631 {
1632 	if (!kde->vht_capabilities) {
1633 		wpa_printf(MSG_DEBUG, "TDLS: No supported vht capabilities "
1634 			   "received");
1635 		return 0;
1636 	}
1637 
1638 	if (!peer->vht_capabilities) {
1639 		peer->vht_capabilities =
1640                         os_zalloc(sizeof(struct ieee80211_vht_capabilities));
1641 		if (peer->vht_capabilities == NULL)
1642                         return -1;
1643 	}
1644 
1645 	os_memcpy(peer->vht_capabilities, kde->vht_capabilities,
1646                   sizeof(struct ieee80211_vht_capabilities));
1647 	wpa_hexdump(MSG_DEBUG, "TDLS: Peer VHT capabilities",
1648 		    (u8 *) peer->vht_capabilities,
1649 		    sizeof(struct ieee80211_vht_capabilities));
1650 
1651 	return 0;
1652 }
1653 
1654 
copy_peer_ext_capab(const struct wpa_eapol_ie_parse * kde,struct wpa_tdls_peer * peer)1655 static int copy_peer_ext_capab(const struct wpa_eapol_ie_parse *kde,
1656 			       struct wpa_tdls_peer *peer)
1657 {
1658 	if (!kde->ext_capab) {
1659 		wpa_printf(MSG_DEBUG, "TDLS: No extended capabilities "
1660 			   "received");
1661 		return 0;
1662 	}
1663 
1664 	if (!peer->ext_capab || peer->ext_capab_len < kde->ext_capab_len - 2) {
1665 		/* Need to allocate buffer to fit the new information */
1666 		os_free(peer->ext_capab);
1667 		peer->ext_capab = os_zalloc(kde->ext_capab_len - 2);
1668 		if (peer->ext_capab == NULL)
1669 			return -1;
1670 	}
1671 
1672 	peer->ext_capab_len = kde->ext_capab_len - 2;
1673 	os_memcpy(peer->ext_capab, kde->ext_capab + 2, peer->ext_capab_len);
1674 
1675 	return 0;
1676 }
1677 
1678 
copy_peer_wmm_capab(const struct wpa_eapol_ie_parse * kde,struct wpa_tdls_peer * peer)1679 static int copy_peer_wmm_capab(const struct wpa_eapol_ie_parse *kde,
1680 			       struct wpa_tdls_peer *peer)
1681 {
1682 	struct wmm_information_element *wmm;
1683 
1684 	if (!kde->wmm) {
1685 		wpa_printf(MSG_DEBUG, "TDLS: No supported WMM capabilities received");
1686 		return 0;
1687 	}
1688 
1689 	if (kde->wmm_len < sizeof(struct wmm_information_element)) {
1690 		wpa_printf(MSG_DEBUG, "TDLS: Invalid supported WMM capabilities received");
1691 		return -1;
1692 	}
1693 
1694 	wmm = (struct wmm_information_element *) kde->wmm;
1695 	peer->qos_info = wmm->qos_info;
1696 
1697 	peer->wmm_capable = 1;
1698 
1699 	wpa_printf(MSG_DEBUG, "TDLS: Peer WMM QOS Info 0x%x", peer->qos_info);
1700 	return 0;
1701 }
1702 
1703 
copy_peer_supp_channels(const struct wpa_eapol_ie_parse * kde,struct wpa_tdls_peer * peer)1704 static int copy_peer_supp_channels(const struct wpa_eapol_ie_parse *kde,
1705 				   struct wpa_tdls_peer *peer)
1706 {
1707 	if (!kde->supp_channels) {
1708 		wpa_printf(MSG_DEBUG, "TDLS: No supported channels received");
1709 		return 0;
1710 	}
1711 
1712 	if (!peer->supp_channels ||
1713 	    peer->supp_channels_len < kde->supp_channels_len) {
1714 		os_free(peer->supp_channels);
1715 		peer->supp_channels = os_zalloc(kde->supp_channels_len);
1716 		if (peer->supp_channels == NULL)
1717 			return -1;
1718 	}
1719 
1720 	peer->supp_channels_len = kde->supp_channels_len;
1721 
1722 	os_memcpy(peer->supp_channels, kde->supp_channels,
1723 		  peer->supp_channels_len);
1724 	wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Channels",
1725 		    (u8 *) peer->supp_channels, peer->supp_channels_len);
1726 	return 0;
1727 }
1728 
1729 
copy_peer_supp_oper_classes(const struct wpa_eapol_ie_parse * kde,struct wpa_tdls_peer * peer)1730 static int copy_peer_supp_oper_classes(const struct wpa_eapol_ie_parse *kde,
1731 				       struct wpa_tdls_peer *peer)
1732 {
1733 	if (!kde->supp_oper_classes) {
1734 		wpa_printf(MSG_DEBUG, "TDLS: No supported operating classes received");
1735 		return 0;
1736 	}
1737 
1738 	if (!peer->supp_oper_classes ||
1739 	    peer->supp_oper_classes_len < kde->supp_oper_classes_len) {
1740 		os_free(peer->supp_oper_classes);
1741 		peer->supp_oper_classes = os_zalloc(kde->supp_oper_classes_len);
1742 		if (peer->supp_oper_classes == NULL)
1743 			return -1;
1744 	}
1745 
1746 	peer->supp_oper_classes_len = kde->supp_oper_classes_len;
1747 	os_memcpy(peer->supp_oper_classes, kde->supp_oper_classes,
1748 		  peer->supp_oper_classes_len);
1749 	wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Operating Classes",
1750 		    (u8 *) peer->supp_oper_classes,
1751 		    peer->supp_oper_classes_len);
1752 	return 0;
1753 }
1754 
1755 
wpa_tdls_addset_peer(struct wpa_sm * sm,struct wpa_tdls_peer * peer,int add)1756 static int wpa_tdls_addset_peer(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
1757 				int add)
1758 {
1759 	return wpa_sm_tdls_peer_addset(sm, peer->addr, add, peer->aid,
1760 				       peer->capability,
1761 				       peer->supp_rates, peer->supp_rates_len,
1762 				       peer->ht_capabilities,
1763 				       peer->vht_capabilities,
1764 				       peer->qos_info, peer->wmm_capable,
1765 				       peer->ext_capab, peer->ext_capab_len,
1766 				       peer->supp_channels,
1767 				       peer->supp_channels_len,
1768 				       peer->supp_oper_classes,
1769 				       peer->supp_oper_classes_len);
1770 }
1771 
1772 
tdls_nonce_set(const u8 * nonce)1773 static int tdls_nonce_set(const u8 *nonce)
1774 {
1775 	int i;
1776 
1777 	for (i = 0; i < WPA_NONCE_LEN; i++) {
1778 		if (nonce[i])
1779 			return 1;
1780 	}
1781 
1782 	return 0;
1783 }
1784 
1785 
wpa_tdls_process_tpk_m1(struct wpa_sm * sm,const u8 * src_addr,const u8 * buf,size_t len)1786 static int wpa_tdls_process_tpk_m1(struct wpa_sm *sm, const u8 *src_addr,
1787 				   const u8 *buf, size_t len)
1788 {
1789 	struct wpa_tdls_peer *peer;
1790 	struct wpa_eapol_ie_parse kde;
1791 	struct wpa_ie_data ie;
1792 	int cipher;
1793 	const u8 *cpos;
1794 	struct wpa_tdls_ftie *ftie = NULL;
1795 	struct wpa_tdls_timeoutie *timeoutie;
1796 	struct wpa_tdls_lnkid *lnkid;
1797 	u32 lifetime = 0;
1798 #if 0
1799 	struct rsn_ie_hdr *hdr;
1800 	u8 *pos;
1801 	u16 rsn_capab;
1802 	u16 rsn_ver;
1803 #endif
1804 	u8 dtoken;
1805 	u16 ielen;
1806 	u16 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1807 	int tdls_prohibited = sm->tdls_prohibited;
1808 	int existing_peer = 0;
1809 
1810 	if (len < 3 + 3)
1811 		return -1;
1812 
1813 	cpos = buf;
1814 	cpos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
1815 
1816 	/* driver had already verified the frame format */
1817 	dtoken = *cpos++; /* dialog token */
1818 
1819 	wpa_printf(MSG_INFO, "TDLS: Dialog Token in TPK M1 %d", dtoken);
1820 
1821 	peer = wpa_tdls_add_peer(sm, src_addr, &existing_peer);
1822 	if (peer == NULL)
1823 		goto error;
1824 
1825 	/* If found, use existing entry instead of adding a new one;
1826 	 * how to handle the case where both ends initiate at the
1827 	 * same time? */
1828 	if (existing_peer) {
1829 		if (peer->tpk_success) {
1830 			wpa_printf(MSG_DEBUG, "TDLS: TDLS Setup Request while "
1831 				   "direct link is enabled - tear down the "
1832 				   "old link first");
1833 			wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
1834 			wpa_tdls_peer_clear(sm, peer);
1835 		} else if (peer->initiator) {
1836 			/*
1837 			 * An entry is already present, so check if we already
1838 			 * sent a TDLS Setup Request. If so, compare MAC
1839 			 * addresses and let the STA with the lower MAC address
1840 			 * continue as the initiator. The other negotiation is
1841 			 * terminated.
1842 			 */
1843 			if (os_memcmp(sm->own_addr, src_addr, ETH_ALEN) < 0) {
1844 				wpa_printf(MSG_DEBUG, "TDLS: Discard request "
1845 					   "from peer with higher address "
1846 					   MACSTR, MAC2STR(src_addr));
1847 				return -1;
1848 			} else {
1849 				wpa_printf(MSG_DEBUG, "TDLS: Accept request "
1850 					   "from peer with lower address "
1851 					   MACSTR " (terminate previously "
1852 					   "initiated negotiation",
1853 					   MAC2STR(src_addr));
1854 				wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK,
1855 						 peer->addr);
1856 				wpa_tdls_peer_clear(sm, peer);
1857 			}
1858 		}
1859 	}
1860 
1861 	/* capability information */
1862 	peer->capability = WPA_GET_LE16(cpos);
1863 	cpos += 2;
1864 
1865 	ielen = len - (cpos - buf); /* start of IE in buf */
1866 
1867 	/*
1868 	 * Don't reject the message if failing to parse IEs. The IEs we need are
1869 	 * explicitly checked below. Some APs may add arbitrary padding to the
1870 	 * end of short TDLS frames and that would look like invalid IEs.
1871 	 */
1872 	if (wpa_supplicant_parse_ies(cpos, ielen, &kde) < 0)
1873 		wpa_printf(MSG_DEBUG,
1874 			   "TDLS: Failed to parse IEs in TPK M1 - ignore as an interop workaround");
1875 
1876 	if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
1877 		wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
1878 			   "TPK M1");
1879 		goto error;
1880 	}
1881 	wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M1",
1882 		    kde.lnkid, kde.lnkid_len);
1883 	lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
1884 	if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
1885 		wpa_printf(MSG_INFO, "TDLS: TPK M1 from diff BSS");
1886 		status = WLAN_STATUS_REQUEST_DECLINED;
1887 		goto error;
1888 	}
1889 
1890 	wpa_printf(MSG_DEBUG, "TDLS: TPK M1 - TPK initiator " MACSTR,
1891 		   MAC2STR(src_addr));
1892 
1893 	if (copy_supp_rates(&kde, peer) < 0)
1894 		goto error;
1895 
1896 	if (copy_peer_ht_capab(&kde, peer) < 0)
1897 		goto error;
1898 
1899 	if (copy_peer_vht_capab(&kde, peer) < 0)
1900 		goto error;
1901 
1902 	if (copy_peer_ext_capab(&kde, peer) < 0)
1903 		goto error;
1904 
1905 	if (copy_peer_supp_channels(&kde, peer) < 0)
1906 		goto error;
1907 
1908 	if (copy_peer_supp_oper_classes(&kde, peer) < 0)
1909 		goto error;
1910 
1911 	peer->qos_info = kde.qosinfo;
1912 
1913 	/* Overwrite with the qos_info obtained in WMM IE */
1914 	if (copy_peer_wmm_capab(&kde, peer) < 0)
1915 		goto error;
1916 
1917 	peer->aid = kde.aid;
1918 
1919 #ifdef CONFIG_TDLS_TESTING
1920 	if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
1921 		peer = wpa_tdls_add_peer(sm, src_addr, NULL);
1922 		if (peer == NULL)
1923 			goto error;
1924 		wpa_printf(MSG_DEBUG, "TDLS: Testing concurrent initiation of "
1925 			   "TDLS setup - send own request");
1926 		peer->initiator = 1;
1927 		wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL,
1928 					NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0);
1929 		wpa_tdls_send_tpk_m1(sm, peer);
1930 	}
1931 
1932 	if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
1933 	    tdls_prohibited) {
1934 		wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
1935 			   "on TDLS");
1936 		tdls_prohibited = 0;
1937 	}
1938 #endif /* CONFIG_TDLS_TESTING */
1939 
1940 	if (tdls_prohibited) {
1941 		wpa_printf(MSG_INFO, "TDLS: TDLS prohibited in this BSS");
1942 		status = WLAN_STATUS_REQUEST_DECLINED;
1943 		goto error;
1944 	}
1945 
1946 	if (!wpa_tdls_get_privacy(sm)) {
1947 		if (kde.rsn_ie) {
1948 			wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M1 while "
1949 				   "security is disabled");
1950 			status = WLAN_STATUS_SECURITY_DISABLED;
1951 			goto error;
1952 		}
1953 		goto skip_rsn;
1954 	}
1955 
1956 	if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
1957 	    kde.rsn_ie == NULL) {
1958 		wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M1");
1959 		status = WLAN_STATUS_INVALID_PARAMETERS;
1960 		goto error;
1961 	}
1962 
1963 	if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
1964 		wpa_printf(MSG_INFO, "TDLS: Too long Initiator RSN IE in "
1965 			   "TPK M1");
1966 		status = WLAN_STATUS_INVALID_RSNIE;
1967 		goto error;
1968 	}
1969 
1970 	if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
1971 		wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M1");
1972 		status = WLAN_STATUS_INVALID_RSNIE;
1973 		goto error;
1974 	}
1975 
1976 	cipher = ie.pairwise_cipher;
1977 	if (cipher & WPA_CIPHER_CCMP) {
1978 		wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
1979 		cipher = WPA_CIPHER_CCMP;
1980 	} else {
1981 		wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M1");
1982 		status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1983 		goto error;
1984 	}
1985 
1986 	if ((ie.capabilities &
1987 	     (WPA_CAPABILITY_NO_PAIRWISE | WPA_CAPABILITY_PEERKEY_ENABLED)) !=
1988 	    WPA_CAPABILITY_PEERKEY_ENABLED) {
1989 		wpa_printf(MSG_INFO, "TDLS: Invalid RSN Capabilities in "
1990 			   "TPK M1");
1991 		status = WLAN_STATUS_INVALID_RSN_IE_CAPAB;
1992 		goto error;
1993 	}
1994 
1995 	/* Lifetime */
1996 	if (kde.key_lifetime == NULL) {
1997 		wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M1");
1998 		status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
1999 		goto error;
2000 	}
2001 	timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2002 	lifetime = WPA_GET_LE32(timeoutie->value);
2003 	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", lifetime);
2004 	if (lifetime < 300) {
2005 		wpa_printf(MSG_INFO, "TDLS: Too short TPK lifetime");
2006 		status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2007 		goto error;
2008 	}
2009 
2010 skip_rsn:
2011 #ifdef CONFIG_TDLS_TESTING
2012 	if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
2013 		if (os_memcmp(sm->own_addr, peer->addr, ETH_ALEN) < 0) {
2014 			/*
2015 			 * The request frame from us is going to win, so do not
2016 			 * replace information based on this request frame from
2017 			 * the peer.
2018 			 */
2019 			goto skip_rsn_check;
2020 		}
2021 	}
2022 #endif /* CONFIG_TDLS_TESTING */
2023 
2024 	peer->initiator = 0; /* Need to check */
2025 	peer->dtoken = dtoken;
2026 
2027 	if (!wpa_tdls_get_privacy(sm)) {
2028 		peer->rsnie_i_len = 0;
2029 		peer->rsnie_p_len = 0;
2030 		peer->cipher = WPA_CIPHER_NONE;
2031 		goto skip_rsn_check;
2032 	}
2033 
2034 	ftie = (struct wpa_tdls_ftie *) kde.ftie;
2035 	os_memcpy(peer->rsnie_i, kde.rsn_ie, kde.rsn_ie_len);
2036 	peer->rsnie_i_len = kde.rsn_ie_len;
2037 	peer->cipher = cipher;
2038 
2039 	if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0 ||
2040 	    !tdls_nonce_set(peer->inonce)) {
2041 		/*
2042 		 * There is no point in updating the RNonce for every obtained
2043 		 * TPK M1 frame (e.g., retransmission due to timeout) with the
2044 		 * same INonce (SNonce in FTIE). However, if the TPK M1 is
2045 		 * retransmitted with a different INonce, update the RNonce
2046 		 * since this is for a new TDLS session.
2047 		 */
2048 		wpa_printf(MSG_DEBUG,
2049 			   "TDLS: New TPK M1 INonce - generate new RNonce");
2050 		os_memcpy(peer->inonce, ftie->Snonce, WPA_NONCE_LEN);
2051 		if (os_get_random(peer->rnonce, WPA_NONCE_LEN)) {
2052 			wpa_msg(sm->ctx->ctx, MSG_WARNING,
2053 				"TDLS: Failed to get random data for responder nonce");
2054 			goto error;
2055 		}
2056 		peer->tk_set = 0; /* A new nonce results in a new TK */
2057 	}
2058 
2059 #if 0
2060 	/* get version info from RSNIE received from Peer */
2061 	hdr = (struct rsn_ie_hdr *) kde.rsn_ie;
2062 	rsn_ver = WPA_GET_LE16(hdr->version);
2063 
2064 	/* use min(peer's version, out version) */
2065 	if (rsn_ver > RSN_VERSION)
2066 		rsn_ver = RSN_VERSION;
2067 
2068 	hdr = (struct rsn_ie_hdr *) peer->rsnie_p;
2069 
2070 	hdr->elem_id = WLAN_EID_RSN;
2071 	WPA_PUT_LE16(hdr->version, rsn_ver);
2072 	pos = (u8 *) (hdr + 1);
2073 
2074 	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
2075 	pos += RSN_SELECTOR_LEN;
2076 	/* Include only the selected cipher in pairwise cipher suite */
2077 	WPA_PUT_LE16(pos, 1);
2078 	pos += 2;
2079 	if (cipher == WPA_CIPHER_CCMP)
2080 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
2081 	pos += RSN_SELECTOR_LEN;
2082 
2083 	WPA_PUT_LE16(pos, 1);
2084 	pos += 2;
2085 	RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
2086 	pos += RSN_SELECTOR_LEN;
2087 
2088 	rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
2089 	rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
2090 	WPA_PUT_LE16(pos, rsn_capab);
2091 	pos += 2;
2092 
2093 	hdr->len = (pos - peer->rsnie_p) - 2;
2094 	peer->rsnie_p_len = pos - peer->rsnie_p;
2095 #endif
2096 
2097 	/* temp fix: validation of RSNIE later */
2098 	os_memcpy(peer->rsnie_p, peer->rsnie_i, peer->rsnie_i_len);
2099 	peer->rsnie_p_len = peer->rsnie_i_len;
2100 
2101 	wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
2102 		    peer->rsnie_p, peer->rsnie_p_len);
2103 
2104 	peer->lifetime = lifetime;
2105 
2106 	wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
2107 
2108 skip_rsn_check:
2109 #ifdef CONFIG_TDLS_TESTING
2110 	if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT)
2111 		goto skip_add_peer;
2112 #endif /* CONFIG_TDLS_TESTING */
2113 
2114 	/* add supported rates, capabilities, and qos_info to the TDLS peer */
2115 	if (wpa_tdls_addset_peer(sm, peer, 1) < 0)
2116 		goto error;
2117 
2118 #ifdef CONFIG_TDLS_TESTING
2119 skip_add_peer:
2120 #endif /* CONFIG_TDLS_TESTING */
2121 	peer->tpk_in_progress = 1;
2122 
2123 	wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Response / TPK M2");
2124 	if (wpa_tdls_send_tpk_m2(sm, src_addr, dtoken, lnkid, peer) < 0) {
2125 		wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
2126 		goto error;
2127 	}
2128 
2129 #ifdef CONFIG_TDLS_TESTING
2130 	if (tdls_testing & TDLS_TESTING_DOUBLE_TPK_M2) {
2131 		wpa_printf(MSG_INFO, "TDLS: Testing - Send another TPK M2");
2132 		wpa_tdls_send_tpk_m2(sm, src_addr, dtoken, lnkid, peer);
2133 	}
2134 #endif /* CONFIG_TDLS_TESTING */
2135 
2136 	return 0;
2137 
2138 error:
2139 	wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE, dtoken, 0,
2140 			    status);
2141 	if (peer)
2142 		wpa_tdls_peer_free(sm, peer);
2143 	return -1;
2144 }
2145 
2146 
wpa_tdls_enable_link(struct wpa_sm * sm,struct wpa_tdls_peer * peer)2147 static int wpa_tdls_enable_link(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
2148 {
2149 	peer->tpk_success = 1;
2150 	peer->tpk_in_progress = 0;
2151 	eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
2152 	if (wpa_tdls_get_privacy(sm)) {
2153 		u32 lifetime = peer->lifetime;
2154 		/*
2155 		 * Start the initiator process a bit earlier to avoid race
2156 		 * condition with the responder sending teardown request.
2157 		 */
2158 		if (lifetime > 3 && peer->initiator)
2159 			lifetime -= 3;
2160 		eloop_register_timeout(lifetime, 0, wpa_tdls_tpk_timeout,
2161 				       sm, peer);
2162 #ifdef CONFIG_TDLS_TESTING
2163 		if (tdls_testing & TDLS_TESTING_NO_TPK_EXPIRATION) {
2164 			wpa_printf(MSG_DEBUG,
2165 				   "TDLS: Testing - disable TPK expiration");
2166 			eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
2167 		}
2168 #endif /* CONFIG_TDLS_TESTING */
2169 	}
2170 
2171 	if (peer->reconfig_key && wpa_tdls_set_key(sm, peer) < 0) {
2172 		wpa_printf(MSG_INFO, "TDLS: Could not configure key to the "
2173 			   "driver");
2174 		return -1;
2175 	}
2176 	peer->reconfig_key = 0;
2177 
2178 	return wpa_sm_tdls_oper(sm, TDLS_ENABLE_LINK, peer->addr);
2179 }
2180 
2181 
wpa_tdls_process_tpk_m2(struct wpa_sm * sm,const u8 * src_addr,const u8 * buf,size_t len)2182 static int wpa_tdls_process_tpk_m2(struct wpa_sm *sm, const u8 *src_addr,
2183 				   const u8 *buf, size_t len)
2184 {
2185 	struct wpa_tdls_peer *peer;
2186 	struct wpa_eapol_ie_parse kde;
2187 	struct wpa_ie_data ie;
2188 	int cipher;
2189 	struct wpa_tdls_ftie *ftie;
2190 	struct wpa_tdls_timeoutie *timeoutie;
2191 	struct wpa_tdls_lnkid *lnkid;
2192 	u32 lifetime;
2193 	u8 dtoken;
2194 	int ielen;
2195 	u16 status;
2196 	const u8 *pos;
2197 	int ret = 0;
2198 
2199 	wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Response / TPK M2 "
2200 		   "(Peer " MACSTR ")", MAC2STR(src_addr));
2201 	for (peer = sm->tdls; peer; peer = peer->next) {
2202 		if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
2203 			break;
2204 	}
2205 	if (peer == NULL) {
2206 		wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
2207 			   "TPK M2: " MACSTR, MAC2STR(src_addr));
2208 		return -1;
2209 	}
2210 	if (!peer->initiator) {
2211 		/*
2212 		 * This may happen if both devices try to initiate TDLS at the
2213 		 * same time and we accept the TPK M1 from the peer in
2214 		 * wpa_tdls_process_tpk_m1() and clear our previous state.
2215 		 */
2216 		wpa_printf(MSG_INFO, "TDLS: We were not the initiator, so "
2217 			   "ignore TPK M2 from " MACSTR, MAC2STR(src_addr));
2218 		return -1;
2219 	}
2220 
2221 	if (peer->tpk_success) {
2222 		wpa_printf(MSG_INFO, "TDLS: Ignore incoming TPK M2 retry, from "
2223 			   MACSTR " as TPK M3 was already sent",
2224 			   MAC2STR(src_addr));
2225 		return 0;
2226 	}
2227 
2228 	wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_REQUEST);
2229 
2230 	if (len < 3 + 2 + 1) {
2231 		wpa_tdls_disable_peer_link(sm, peer);
2232 		return -1;
2233 	}
2234 
2235 	pos = buf;
2236 	pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2237 	status = WPA_GET_LE16(pos);
2238 	pos += 2 /* status code */;
2239 
2240 	if (status != WLAN_STATUS_SUCCESS) {
2241 		wpa_printf(MSG_INFO, "TDLS: Status code in TPK M2: %u",
2242 			   status);
2243 		wpa_tdls_disable_peer_link(sm, peer);
2244 		return -1;
2245 	}
2246 
2247 	status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2248 
2249 	/* TODO: need to verify dialog token matches here or in kernel */
2250 	dtoken = *pos++; /* dialog token */
2251 
2252 	wpa_printf(MSG_DEBUG, "TDLS: Dialog Token in TPK M2 %d", dtoken);
2253 
2254 	if (len < 3 + 2 + 1 + 2) {
2255 		wpa_tdls_disable_peer_link(sm, peer);
2256 		return -1;
2257 	}
2258 
2259 	/* capability information */
2260 	peer->capability = WPA_GET_LE16(pos);
2261 	pos += 2;
2262 
2263 	ielen = len - (pos - buf); /* start of IE in buf */
2264 
2265 	/*
2266 	 * Don't reject the message if failing to parse IEs. The IEs we need are
2267 	 * explicitly checked below. Some APs may add arbitrary padding to the
2268 	 * end of short TDLS frames and that would look like invalid IEs.
2269 	 */
2270 	if (wpa_supplicant_parse_ies(pos, ielen, &kde) < 0)
2271 		wpa_printf(MSG_DEBUG,
2272 			   "TDLS: Failed to parse IEs in TPK M2 - ignore as an interop workaround");
2273 
2274 #ifdef CONFIG_TDLS_TESTING
2275 	if (tdls_testing & TDLS_TESTING_DECLINE_RESP) {
2276 		wpa_printf(MSG_DEBUG, "TDLS: Testing - decline response");
2277 		status = WLAN_STATUS_REQUEST_DECLINED;
2278 		goto error;
2279 	}
2280 #endif /* CONFIG_TDLS_TESTING */
2281 
2282 	if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
2283 		wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
2284 			   "TPK M2");
2285 		goto error;
2286 	}
2287 	wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M2",
2288 		    kde.lnkid, kde.lnkid_len);
2289 	lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
2290 
2291 	if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
2292 		wpa_printf(MSG_INFO, "TDLS: TPK M2 from different BSS");
2293 		status = WLAN_STATUS_NOT_IN_SAME_BSS;
2294 		goto error;
2295 	}
2296 
2297 	if (copy_supp_rates(&kde, peer) < 0)
2298 		goto error;
2299 
2300 	if (copy_peer_ht_capab(&kde, peer) < 0)
2301 		goto error;
2302 
2303 	if (copy_peer_vht_capab(&kde, peer) < 0)
2304 		goto error;
2305 
2306 	if (copy_peer_ext_capab(&kde, peer) < 0)
2307 		goto error;
2308 
2309 	if (copy_peer_supp_channels(&kde, peer) < 0)
2310 		goto error;
2311 
2312 	if (copy_peer_supp_oper_classes(&kde, peer) < 0)
2313 		goto error;
2314 
2315 	peer->qos_info = kde.qosinfo;
2316 
2317 	/* Overwrite with the qos_info obtained in WMM IE */
2318 	if (copy_peer_wmm_capab(&kde, peer) < 0)
2319 		goto error;
2320 
2321 	peer->aid = kde.aid;
2322 
2323 	if (!wpa_tdls_get_privacy(sm)) {
2324 		peer->rsnie_p_len = 0;
2325 		peer->cipher = WPA_CIPHER_NONE;
2326 		goto skip_rsn;
2327 	}
2328 
2329 	if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
2330 	    kde.rsn_ie == NULL) {
2331 		wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M2");
2332 		status = WLAN_STATUS_INVALID_PARAMETERS;
2333 		goto error;
2334 	}
2335 	wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
2336 		    kde.rsn_ie, kde.rsn_ie_len);
2337 
2338 	if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
2339 		wpa_printf(MSG_INFO,
2340 			   "TDLS: Too long Responder RSN IE in TPK M2");
2341 		status = WLAN_STATUS_INVALID_RSNIE;
2342 		goto error;
2343 	}
2344 
2345 	/*
2346 	 * FIX: bitwise comparison of RSN IE is not the correct way of
2347 	 * validation this. It can be different, but certain fields must
2348 	 * match. Since we list only a single pairwise cipher in TPK M1, the
2349 	 * memcmp is likely to work in most cases, though.
2350 	 */
2351 	if (kde.rsn_ie_len != peer->rsnie_i_len ||
2352 	    os_memcmp(peer->rsnie_i, kde.rsn_ie, peer->rsnie_i_len) != 0) {
2353 		wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M2 does "
2354 			   "not match with RSN IE used in TPK M1");
2355 		wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Sent in TPK M1",
2356 			    peer->rsnie_i, peer->rsnie_i_len);
2357 		wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
2358 			    kde.rsn_ie, kde.rsn_ie_len);
2359 		status = WLAN_STATUS_INVALID_RSNIE;
2360 		goto error;
2361 	}
2362 
2363 	if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
2364 		wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M2");
2365 		status = WLAN_STATUS_INVALID_RSNIE;
2366 		goto error;
2367 	}
2368 
2369 	cipher = ie.pairwise_cipher;
2370 	if (cipher == WPA_CIPHER_CCMP) {
2371 		wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
2372 		cipher = WPA_CIPHER_CCMP;
2373 	} else {
2374 		wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M2");
2375 		status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
2376 		goto error;
2377 	}
2378 
2379 	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M2",
2380 		    kde.ftie, sizeof(*ftie));
2381 	ftie = (struct wpa_tdls_ftie *) kde.ftie;
2382 
2383 	if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
2384 		wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M2 does "
2385 			   "not match with FTIE SNonce used in TPK M1");
2386 		/* Silently discard the frame */
2387 		return -1;
2388 	}
2389 
2390 	/* Responder Nonce and RSN IE */
2391 	os_memcpy(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN);
2392 	os_memcpy(peer->rsnie_p, kde.rsn_ie, kde.rsn_ie_len);
2393 	peer->rsnie_p_len = kde.rsn_ie_len;
2394 	peer->cipher = cipher;
2395 
2396 	/* Lifetime */
2397 	if (kde.key_lifetime == NULL) {
2398 		wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M2");
2399 		status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2400 		goto error;
2401 	}
2402 	timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2403 	lifetime = WPA_GET_LE32(timeoutie->value);
2404 	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M2",
2405 		   lifetime);
2406 	if (lifetime != peer->lifetime) {
2407 		wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2408 			   "TPK M2 (expected %u)", lifetime, peer->lifetime);
2409 		status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
2410 		goto error;
2411 	}
2412 
2413 	wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
2414 
2415 	/* Process MIC check to see if TPK M2 is right */
2416 	if (wpa_supplicant_verify_tdls_mic(2, peer, (u8 *) lnkid,
2417 					   (u8 *) timeoutie, ftie) < 0) {
2418 		/* Discard the frame */
2419 		wpa_tdls_del_key(sm, peer);
2420 		wpa_tdls_disable_peer_link(sm, peer);
2421 		return -1;
2422 	}
2423 
2424 	if (wpa_tdls_set_key(sm, peer) < 0) {
2425 		/*
2426 		 * Some drivers may not be able to config the key prior to full
2427 		 * STA entry having been configured.
2428 		 */
2429 		wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2430 			   "STA entry is complete");
2431 		peer->reconfig_key = 1;
2432 	}
2433 
2434 skip_rsn:
2435 	peer->dtoken = dtoken;
2436 
2437 	/* add supported rates, capabilities, and qos_info to the TDLS peer */
2438 	if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
2439 		goto error;
2440 
2441 	wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Confirm / "
2442 		   "TPK Handshake Message 3");
2443 	if (wpa_tdls_send_tpk_m3(sm, src_addr, dtoken, lnkid, peer) < 0)
2444 		goto error_no_msg;
2445 
2446 	if (!peer->tpk_success) {
2447 		/*
2448 		 * Enable Link only when tpk_success is 0, signifying that this
2449 		 * processing of TPK M2 frame is not because of a retransmission
2450 		 * during TDLS setup handshake.
2451 		 */
2452 		ret = wpa_tdls_enable_link(sm, peer);
2453 		if (ret < 0) {
2454 			wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
2455 			wpa_tdls_do_teardown(
2456 				sm, peer,
2457 				WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
2458 		}
2459 	}
2460 	return ret;
2461 
2462 error:
2463 	wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM, dtoken, 1,
2464 			    status);
2465 error_no_msg:
2466 	wpa_tdls_disable_peer_link(sm, peer);
2467 	return -1;
2468 }
2469 
2470 
wpa_tdls_process_tpk_m3(struct wpa_sm * sm,const u8 * src_addr,const u8 * buf,size_t len)2471 static int wpa_tdls_process_tpk_m3(struct wpa_sm *sm, const u8 *src_addr,
2472 				   const u8 *buf, size_t len)
2473 {
2474 	struct wpa_tdls_peer *peer;
2475 	struct wpa_eapol_ie_parse kde;
2476 	struct wpa_tdls_ftie *ftie;
2477 	struct wpa_tdls_timeoutie *timeoutie;
2478 	struct wpa_tdls_lnkid *lnkid;
2479 	int ielen;
2480 	u16 status;
2481 	const u8 *pos;
2482 	u32 lifetime;
2483 	int ret = 0;
2484 
2485 	wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Confirm / TPK M3 "
2486 		   "(Peer " MACSTR ")", MAC2STR(src_addr));
2487 	for (peer = sm->tdls; peer; peer = peer->next) {
2488 		if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
2489 			break;
2490 	}
2491 	if (peer == NULL) {
2492 		wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
2493 			   "TPK M3: " MACSTR, MAC2STR(src_addr));
2494 		return -1;
2495 	}
2496 	wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_RESPONSE);
2497 
2498 	if (len < 3 + 3)
2499 		goto error;
2500 	pos = buf;
2501 	pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
2502 
2503 	status = WPA_GET_LE16(pos);
2504 
2505 	if (status != 0) {
2506 		wpa_printf(MSG_INFO, "TDLS: Status code in TPK M3: %u",
2507 			   status);
2508 		goto error;
2509 	}
2510 	pos += 2 /* status code */ + 1 /* dialog token */;
2511 
2512 	ielen = len - (pos - buf); /* start of IE in buf */
2513 
2514 	/*
2515 	 * Don't reject the message if failing to parse IEs. The IEs we need are
2516 	 * explicitly checked below. Some APs piggy-back broken IEs to the end
2517 	 * of a TDLS Confirm packet, which will fail the link if we don't ignore
2518 	 * this error.
2519 	 */
2520 	if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
2521 		wpa_printf(MSG_DEBUG,
2522 			   "TDLS: Failed to parse KDEs in TPK M3 - ignore as an interop workaround");
2523 	}
2524 
2525 	if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
2526 		wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TPK M3");
2527 		goto error;
2528 	}
2529 	wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M3",
2530 		    (u8 *) kde.lnkid, kde.lnkid_len);
2531 	lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
2532 
2533 	if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
2534 		wpa_printf(MSG_INFO, "TDLS: TPK M3 from diff BSS");
2535 		goto error;
2536 	}
2537 
2538 	if (!wpa_tdls_get_privacy(sm))
2539 		goto skip_rsn;
2540 
2541 	if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
2542 		wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M3");
2543 		goto error;
2544 	}
2545 	wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M3",
2546 		    kde.ftie, sizeof(*ftie));
2547 	ftie = (struct wpa_tdls_ftie *) kde.ftie;
2548 
2549 	if (kde.rsn_ie == NULL) {
2550 		wpa_printf(MSG_INFO, "TDLS: No RSN IE in TPK M3");
2551 		goto error;
2552 	}
2553 	wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M3",
2554 		    kde.rsn_ie, kde.rsn_ie_len);
2555 	if (kde.rsn_ie_len != peer->rsnie_p_len ||
2556 	    os_memcmp(kde.rsn_ie, peer->rsnie_p, peer->rsnie_p_len) != 0) {
2557 		wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M3 does not match "
2558 			   "with the one sent in TPK M2");
2559 		goto error;
2560 	}
2561 
2562 	if (os_memcmp(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN) != 0) {
2563 		wpa_printf(MSG_INFO, "TDLS: FTIE ANonce in TPK M3 does "
2564 			   "not match with FTIE ANonce used in TPK M2");
2565 		goto error;
2566 	}
2567 
2568 	if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
2569 		wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M3 does not "
2570 			   "match with FTIE SNonce used in TPK M1");
2571 		goto error;
2572 	}
2573 
2574 	if (kde.key_lifetime == NULL) {
2575 		wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M3");
2576 		goto error;
2577 	}
2578 	timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
2579 	wpa_hexdump(MSG_DEBUG, "TDLS: Timeout IE Received from TPK M3",
2580 		    (u8 *) timeoutie, sizeof(*timeoutie));
2581 	lifetime = WPA_GET_LE32(timeoutie->value);
2582 	wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M3",
2583 		   lifetime);
2584 	if (lifetime != peer->lifetime) {
2585 		wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
2586 			   "TPK M3 (expected %u)", lifetime, peer->lifetime);
2587 		goto error;
2588 	}
2589 
2590 	if (wpa_supplicant_verify_tdls_mic(3, peer, (u8 *) lnkid,
2591 					   (u8 *) timeoutie, ftie) < 0) {
2592 		wpa_tdls_del_key(sm, peer);
2593 		goto error;
2594 	}
2595 
2596 	if (wpa_tdls_set_key(sm, peer) < 0) {
2597 		/*
2598 		 * Some drivers may not be able to config the key prior to full
2599 		 * STA entry having been configured.
2600 		 */
2601 		wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
2602 			   "STA entry is complete");
2603 		peer->reconfig_key = 1;
2604 	}
2605 
2606 skip_rsn:
2607 	/* add supported rates, capabilities, and qos_info to the TDLS peer */
2608 	if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
2609 		goto error;
2610 
2611 	if (!peer->tpk_success) {
2612 		/*
2613 		 * Enable Link only when tpk_success is 0, signifying that this
2614 		 * processing of TPK M3 frame is not because of a retransmission
2615 		 * during TDLS setup handshake.
2616 		 */
2617 		ret = wpa_tdls_enable_link(sm, peer);
2618 		if (ret < 0) {
2619 			wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
2620 			goto error;
2621 		}
2622 	}
2623 	return ret;
2624 error:
2625 	wpa_tdls_do_teardown(sm, peer, WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
2626 	return -1;
2627 }
2628 
2629 
wpa_add_tdls_timeoutie(u8 * pos,u8 * ie,size_t ie_len,u32 tsecs)2630 static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs)
2631 {
2632 	struct wpa_tdls_timeoutie *lifetime = (struct wpa_tdls_timeoutie *) ie;
2633 
2634 	os_memset(lifetime, 0, ie_len);
2635 	lifetime->ie_type = WLAN_EID_TIMEOUT_INTERVAL;
2636 	lifetime->ie_len = sizeof(struct wpa_tdls_timeoutie) - 2;
2637 	lifetime->interval_type = WLAN_TIMEOUT_KEY_LIFETIME;
2638 	WPA_PUT_LE32(lifetime->value, tsecs);
2639 	os_memcpy(pos, ie, ie_len);
2640 	return pos + ie_len;
2641 }
2642 
2643 
2644 /**
2645  * wpa_tdls_start - Initiate TDLS handshake (send TPK Handshake Message 1)
2646  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2647  * @peer: MAC address of the peer STA
2648  * Returns: 0 on success, or -1 on failure
2649  *
2650  * Send TPK Handshake Message 1 info to driver to start TDLS
2651  * handshake with the peer.
2652  */
wpa_tdls_start(struct wpa_sm * sm,const u8 * addr)2653 int wpa_tdls_start(struct wpa_sm *sm, const u8 *addr)
2654 {
2655 	struct wpa_tdls_peer *peer;
2656 	int tdls_prohibited = sm->tdls_prohibited;
2657 
2658 	if (sm->tdls_disabled || !sm->tdls_supported)
2659 		return -1;
2660 
2661 #ifdef CONFIG_TDLS_TESTING
2662 	if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
2663 	    tdls_prohibited) {
2664 		wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
2665 			   "on TDLS");
2666 		tdls_prohibited = 0;
2667 	}
2668 #endif /* CONFIG_TDLS_TESTING */
2669 
2670 	if (tdls_prohibited) {
2671 		wpa_printf(MSG_DEBUG, "TDLS: TDLS is prohibited in this BSS - "
2672 			   "reject request to start setup");
2673 		return -1;
2674 	}
2675 
2676 	peer = wpa_tdls_add_peer(sm, addr, NULL);
2677 	if (peer == NULL)
2678 		return -1;
2679 
2680 	if (peer->tpk_in_progress) {
2681 		wpa_printf(MSG_DEBUG, "TDLS: Setup is already in progress with the peer");
2682 		return 0;
2683 	}
2684 
2685 	peer->initiator = 1;
2686 
2687 	/* add the peer to the driver as a "setup in progress" peer */
2688 	if (wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL,
2689 				    NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0)) {
2690 		wpa_tdls_disable_peer_link(sm, peer);
2691 		return -1;
2692 	}
2693 
2694 	peer->tpk_in_progress = 1;
2695 
2696 	if (wpa_tdls_send_tpk_m1(sm, peer) < 0) {
2697 		wpa_tdls_disable_peer_link(sm, peer);
2698 		return -1;
2699 	}
2700 
2701 	return 0;
2702 }
2703 
2704 
wpa_tdls_remove(struct wpa_sm * sm,const u8 * addr)2705 void wpa_tdls_remove(struct wpa_sm *sm, const u8 *addr)
2706 {
2707 	struct wpa_tdls_peer *peer;
2708 
2709 	if (sm->tdls_disabled || !sm->tdls_supported)
2710 		return;
2711 
2712 	for (peer = sm->tdls; peer; peer = peer->next) {
2713 		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
2714 			break;
2715 	}
2716 
2717 	if (peer == NULL || !peer->tpk_success)
2718 		return;
2719 
2720 	if (sm->tdls_external_setup) {
2721 		/*
2722 		 * Disable previous link to allow renegotiation to be completed
2723 		 * on AP path.
2724 		 */
2725 		wpa_tdls_do_teardown(sm, peer,
2726 				     WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
2727 	}
2728 }
2729 
2730 
2731 /**
2732  * wpa_supplicant_rx_tdls - Receive TDLS data frame
2733  *
2734  * This function is called to receive TDLS (ethertype = 0x890d) data frames.
2735  */
wpa_supplicant_rx_tdls(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)2736 static void wpa_supplicant_rx_tdls(void *ctx, const u8 *src_addr,
2737 				   const u8 *buf, size_t len)
2738 {
2739 	struct wpa_sm *sm = ctx;
2740 	struct wpa_tdls_frame *tf;
2741 
2742 	wpa_hexdump(MSG_DEBUG, "TDLS: Received Data frame encapsulation",
2743 		    buf, len);
2744 
2745 	if (sm->tdls_disabled || !sm->tdls_supported) {
2746 		wpa_printf(MSG_DEBUG, "TDLS: Discard message - TDLS disabled "
2747 			   "or unsupported by driver");
2748 		return;
2749 	}
2750 
2751 	if (os_memcmp(src_addr, sm->own_addr, ETH_ALEN) == 0) {
2752 		wpa_printf(MSG_DEBUG, "TDLS: Discard copy of own message");
2753 		return;
2754 	}
2755 
2756 	if (len < sizeof(*tf)) {
2757 		wpa_printf(MSG_INFO, "TDLS: Drop too short frame");
2758 		return;
2759 	}
2760 
2761 	/* Check to make sure its a valid encapsulated TDLS frame */
2762 	tf = (struct wpa_tdls_frame *) buf;
2763 	if (tf->payloadtype != 2 /* TDLS_RFTYPE */ ||
2764 	    tf->category != WLAN_ACTION_TDLS) {
2765 		wpa_printf(MSG_INFO, "TDLS: Invalid frame - payloadtype=%u "
2766 			   "category=%u action=%u",
2767 			   tf->payloadtype, tf->category, tf->action);
2768 		return;
2769 	}
2770 
2771 	switch (tf->action) {
2772 	case WLAN_TDLS_SETUP_REQUEST:
2773 		wpa_tdls_process_tpk_m1(sm, src_addr, buf, len);
2774 		break;
2775 	case WLAN_TDLS_SETUP_RESPONSE:
2776 		wpa_tdls_process_tpk_m2(sm, src_addr, buf, len);
2777 		break;
2778 	case WLAN_TDLS_SETUP_CONFIRM:
2779 		wpa_tdls_process_tpk_m3(sm, src_addr, buf, len);
2780 		break;
2781 	case WLAN_TDLS_TEARDOWN:
2782 		wpa_tdls_recv_teardown(sm, src_addr, buf, len);
2783 		break;
2784 	case WLAN_TDLS_DISCOVERY_REQUEST:
2785 		wpa_tdls_process_discovery_request(sm, src_addr, buf, len);
2786 		break;
2787 	default:
2788 		/* Kernel code will process remaining frames */
2789 		wpa_printf(MSG_DEBUG, "TDLS: Ignore TDLS frame action code %u",
2790 			   tf->action);
2791 		break;
2792 	}
2793 }
2794 
2795 
2796 /**
2797  * wpa_tdls_init - Initialize driver interface parameters for TDLS
2798  * @wpa_s: Pointer to wpa_supplicant data
2799  * Returns: 0 on success, -1 on failure
2800  *
2801  * This function is called to initialize driver interface parameters for TDLS.
2802  * wpa_drv_init() must have been called before this function to initialize the
2803  * driver interface.
2804  */
wpa_tdls_init(struct wpa_sm * sm)2805 int wpa_tdls_init(struct wpa_sm *sm)
2806 {
2807 	if (sm == NULL)
2808 		return -1;
2809 
2810 	sm->l2_tdls = l2_packet_init(sm->bridge_ifname ? sm->bridge_ifname :
2811 				     sm->ifname,
2812 				     sm->own_addr,
2813 				     ETH_P_80211_ENCAP, wpa_supplicant_rx_tdls,
2814 				     sm, 0);
2815 	if (sm->l2_tdls == NULL) {
2816 		wpa_printf(MSG_ERROR, "TDLS: Failed to open l2_packet "
2817 			   "connection");
2818 		return -1;
2819 	}
2820 
2821 	/*
2822 	 * Drivers that support TDLS but don't implement the get_capa callback
2823 	 * are assumed to perform everything internally
2824 	 */
2825 	if (wpa_sm_tdls_get_capa(sm, &sm->tdls_supported,
2826 				 &sm->tdls_external_setup,
2827 				 &sm->tdls_chan_switch) < 0) {
2828 		sm->tdls_supported = 1;
2829 		sm->tdls_external_setup = 0;
2830 	}
2831 
2832 	wpa_printf(MSG_DEBUG, "TDLS: TDLS operation%s supported by "
2833 		   "driver", sm->tdls_supported ? "" : " not");
2834 	wpa_printf(MSG_DEBUG, "TDLS: Driver uses %s link setup",
2835 		   sm->tdls_external_setup ? "external" : "internal");
2836 	wpa_printf(MSG_DEBUG, "TDLS: Driver %s TDLS channel switching",
2837 		   sm->tdls_chan_switch ? "supports" : "does not support");
2838 
2839 	return 0;
2840 }
2841 
2842 
wpa_tdls_teardown_peers(struct wpa_sm * sm)2843 void wpa_tdls_teardown_peers(struct wpa_sm *sm)
2844 {
2845 	struct wpa_tdls_peer *peer, *tmp;
2846 
2847 	if (!sm)
2848 		return;
2849 	peer = sm->tdls;
2850 
2851 	wpa_printf(MSG_DEBUG, "TDLS: Tear down peers");
2852 
2853 	while (peer) {
2854 		tmp = peer->next;
2855 		wpa_printf(MSG_DEBUG, "TDLS: Tear down peer " MACSTR,
2856 			   MAC2STR(peer->addr));
2857 		if (sm->tdls_external_setup)
2858 			wpa_tdls_do_teardown(sm, peer,
2859 					     WLAN_REASON_DEAUTH_LEAVING);
2860 		else
2861 			wpa_sm_tdls_oper(sm, TDLS_TEARDOWN, peer->addr);
2862 
2863 		peer = tmp;
2864 	}
2865 }
2866 
2867 
wpa_tdls_remove_peers(struct wpa_sm * sm)2868 static void wpa_tdls_remove_peers(struct wpa_sm *sm)
2869 {
2870 	struct wpa_tdls_peer *peer, *tmp;
2871 
2872 	peer = sm->tdls;
2873 
2874 	while (peer) {
2875 		int res;
2876 		tmp = peer->next;
2877 		res = wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
2878 		wpa_printf(MSG_DEBUG, "TDLS: Remove peer " MACSTR " (res=%d)",
2879 			   MAC2STR(peer->addr), res);
2880 		wpa_tdls_peer_free(sm, peer);
2881 		peer = tmp;
2882 	}
2883 }
2884 
2885 
2886 /**
2887  * wpa_tdls_deinit - Deinitialize driver interface parameters for TDLS
2888  *
2889  * This function is called to recover driver interface parameters for TDLS
2890  * and frees resources allocated for it.
2891  */
wpa_tdls_deinit(struct wpa_sm * sm)2892 void wpa_tdls_deinit(struct wpa_sm *sm)
2893 {
2894 	if (sm == NULL)
2895 		return;
2896 
2897 	if (sm->l2_tdls)
2898 		l2_packet_deinit(sm->l2_tdls);
2899 	sm->l2_tdls = NULL;
2900 
2901 	wpa_tdls_remove_peers(sm);
2902 }
2903 
2904 
wpa_tdls_assoc(struct wpa_sm * sm)2905 void wpa_tdls_assoc(struct wpa_sm *sm)
2906 {
2907 	wpa_printf(MSG_DEBUG, "TDLS: Remove peers on association");
2908 	wpa_tdls_remove_peers(sm);
2909 }
2910 
2911 
wpa_tdls_disassoc(struct wpa_sm * sm)2912 void wpa_tdls_disassoc(struct wpa_sm *sm)
2913 {
2914 	wpa_printf(MSG_DEBUG, "TDLS: Remove peers on disassociation");
2915 	wpa_tdls_remove_peers(sm);
2916 }
2917 
2918 
wpa_tdls_prohibited(struct ieee802_11_elems * elems)2919 static int wpa_tdls_prohibited(struct ieee802_11_elems *elems)
2920 {
2921 	/* bit 38 - TDLS Prohibited */
2922 	return !!(elems->ext_capab[4] & 0x40);
2923 }
2924 
2925 
wpa_tdls_chan_switch_prohibited(struct ieee802_11_elems * elems)2926 static int wpa_tdls_chan_switch_prohibited(struct ieee802_11_elems *elems)
2927 {
2928 	/* bit 39 - TDLS Channel Switch Prohibited */
2929 	return !!(elems->ext_capab[4] & 0x80);
2930 }
2931 
2932 
wpa_tdls_ap_ies(struct wpa_sm * sm,const u8 * ies,size_t len)2933 void wpa_tdls_ap_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2934 {
2935 	struct ieee802_11_elems elems;
2936 
2937 	sm->tdls_prohibited = 0;
2938 	sm->tdls_chan_switch_prohibited = 0;
2939 
2940 	if (ies == NULL ||
2941 	    ieee802_11_parse_elems(ies, len, &elems, 0) == ParseFailed ||
2942 	    elems.ext_capab == NULL || elems.ext_capab_len < 5)
2943 		return;
2944 
2945 	sm->tdls_prohibited = wpa_tdls_prohibited(&elems);
2946 	wpa_printf(MSG_DEBUG, "TDLS: TDLS is %s in the target BSS",
2947 		   sm->tdls_prohibited ? "prohibited" : "allowed");
2948 	sm->tdls_chan_switch_prohibited =
2949 		wpa_tdls_chan_switch_prohibited(&elems);
2950 	wpa_printf(MSG_DEBUG, "TDLS: TDLS channel switch %s in the target BSS",
2951 		   sm->tdls_chan_switch_prohibited ? "prohibited" : "allowed");
2952 }
2953 
2954 
wpa_tdls_assoc_resp_ies(struct wpa_sm * sm,const u8 * ies,size_t len)2955 void wpa_tdls_assoc_resp_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
2956 {
2957 	struct ieee802_11_elems elems;
2958 
2959 	if (ies == NULL ||
2960 	    ieee802_11_parse_elems(ies, len, &elems, 0) == ParseFailed ||
2961 	    elems.ext_capab == NULL || elems.ext_capab_len < 5)
2962 		return;
2963 
2964 	if (!sm->tdls_prohibited && wpa_tdls_prohibited(&elems)) {
2965 		wpa_printf(MSG_DEBUG, "TDLS: TDLS prohibited based on "
2966 			   "(Re)Association Response IEs");
2967 		sm->tdls_prohibited = 1;
2968 	}
2969 
2970 	if (!sm->tdls_chan_switch_prohibited &&
2971 	    wpa_tdls_chan_switch_prohibited(&elems)) {
2972 		wpa_printf(MSG_DEBUG,
2973 			   "TDLS: TDLS channel switch prohibited based on (Re)Association Response IEs");
2974 		sm->tdls_chan_switch_prohibited = 1;
2975 	}
2976 }
2977 
2978 
wpa_tdls_enable(struct wpa_sm * sm,int enabled)2979 void wpa_tdls_enable(struct wpa_sm *sm, int enabled)
2980 {
2981 	wpa_printf(MSG_DEBUG, "TDLS: %s", enabled ? "enabled" : "disabled");
2982 	sm->tdls_disabled = !enabled;
2983 }
2984 
2985 
wpa_tdls_is_external_setup(struct wpa_sm * sm)2986 int wpa_tdls_is_external_setup(struct wpa_sm *sm)
2987 {
2988 	return sm->tdls_external_setup;
2989 }
2990 
2991 
wpa_tdls_enable_chan_switch(struct wpa_sm * sm,const u8 * addr,u8 oper_class,struct hostapd_freq_params * freq_params)2992 int wpa_tdls_enable_chan_switch(struct wpa_sm *sm, const u8 *addr,
2993 				u8 oper_class,
2994 				struct hostapd_freq_params *freq_params)
2995 {
2996 	struct wpa_tdls_peer *peer;
2997 	int ret;
2998 
2999 	if (sm->tdls_disabled || !sm->tdls_supported)
3000 		return -1;
3001 
3002 	if (!sm->tdls_chan_switch) {
3003 		wpa_printf(MSG_DEBUG,
3004 			   "TDLS: Channel switching not supported by the driver");
3005 		return -1;
3006 	}
3007 
3008 	if (sm->tdls_chan_switch_prohibited) {
3009 		wpa_printf(MSG_DEBUG,
3010 			   "TDLS: Channel switching is prohibited in this BSS - reject request to switch channel");
3011 		return -1;
3012 	}
3013 
3014 	for (peer = sm->tdls; peer; peer = peer->next) {
3015 		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
3016 			break;
3017 	}
3018 
3019 	if (peer == NULL || !peer->tpk_success) {
3020 		wpa_printf(MSG_ERROR, "TDLS: Peer " MACSTR
3021 			   " not found for channel switching", MAC2STR(addr));
3022 		return -1;
3023 	}
3024 
3025 	if (peer->chan_switch_enabled) {
3026 		wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
3027 			   " already has channel switching enabled",
3028 			   MAC2STR(addr));
3029 		return 0;
3030 	}
3031 
3032 	ret = wpa_sm_tdls_enable_channel_switch(sm, peer->addr,
3033 						oper_class, freq_params);
3034 	if (!ret)
3035 		peer->chan_switch_enabled = 1;
3036 
3037 	return ret;
3038 }
3039 
3040 
wpa_tdls_disable_chan_switch(struct wpa_sm * sm,const u8 * addr)3041 int wpa_tdls_disable_chan_switch(struct wpa_sm *sm, const u8 *addr)
3042 {
3043 	struct wpa_tdls_peer *peer;
3044 
3045 	if (sm->tdls_disabled || !sm->tdls_supported)
3046 		return -1;
3047 
3048 	for (peer = sm->tdls; peer; peer = peer->next) {
3049 		if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
3050 			break;
3051 	}
3052 
3053 	if (!peer || !peer->chan_switch_enabled) {
3054 		wpa_printf(MSG_ERROR, "TDLS: Channel switching not enabled for "
3055 			   MACSTR, MAC2STR(addr));
3056 		return -1;
3057 	}
3058 
3059 	/* ignore the return value */
3060 	wpa_sm_tdls_disable_channel_switch(sm, peer->addr);
3061 
3062 	peer->chan_switch_enabled = 0;
3063 	return 0;
3064 }
3065