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