eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

eapol.c (83018B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2013-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <string.h>
     29 #include <alloca.h>
     30 #include <linux/if_ether.h>
     31 #include <errno.h>
     32 #include <ell/ell.h>
     33 
     34 #include "ell/useful.h"
     35 #include "src/missing.h"
     36 #include "src/module.h"
     37 #include "src/crypto.h"
     38 #include "src/eapol.h"
     39 #include "src/ie.h"
     40 #include "src/util.h"
     41 #include "src/mpdu.h"
     42 #include "src/eap.h"
     43 #include "src/handshake.h"
     44 #include "src/watchlist.h"
     45 #include "src/erp.h"
     46 #include "src/iwd.h"
     47 #include "src/band.h"
     48 
     49 static struct l_queue *state_machines;
     50 static struct l_queue *preauths;
     51 static struct watchlist frame_watches;
     52 static uint32_t eapol_4way_handshake_time = 2;
     53 
     54 static eapol_rekey_offload_func_t rekey_offload = NULL;
     55 
     56 static eapol_tx_packet_func_t tx_packet = NULL;
     57 static eapol_install_pmk_func_t install_pmk = NULL;
     58 static void *tx_user_data;
     59 
     60 #define VERIFY_IS_ZERO(field)						\
     61 	do {								\
     62 		if (!l_memeqzero((field), sizeof((field))))	\
     63 			return false;					\
     64 	} while (false)							\
     65 
     66 #define MIC_MAXLEN	32
     67 
     68 static bool eapol_aes_siv_encrypt(const uint8_t *kek, size_t kek_len,
     69 				struct eapol_key *frame,
     70 				const uint8_t *data, size_t len)
     71 {
     72 	uint8_t encr[16 + len];
     73 	struct iovec ad[1];
     74 
     75 	ad[0].iov_base = frame;
     76 	ad[0].iov_len = EAPOL_KEY_DATA(frame, 0) - (uint8_t *)frame;
     77 
     78 	if (!aes_siv_encrypt(kek, kek_len, EAPOL_KEY_DATA(frame, 0),
     79 				len, ad, 1, encr))
     80 		return false;
     81 
     82 	memcpy(EAPOL_KEY_DATA(frame, 0), encr, sizeof(encr));
     83 
     84 	return true;
     85 }
     86 
     87 /*
     88  * MIC calculation depends on the selected hash function.  The has function
     89  * is given in the EAPoL Key Descriptor Version field.
     90  *
     91  * The input struct eapol_key *frame should have a zero-d MIC field
     92  */
     93 bool eapol_calculate_mic(enum ie_rsn_akm_suite akm, const uint8_t *kck,
     94 				const struct eapol_key *frame, uint8_t *mic,
     95 				size_t mic_len)
     96 {
     97 	size_t frame_len = EAPOL_FRAME_LEN(mic_len) +
     98 					EAPOL_KEY_DATA_LEN(frame, mic_len);
     99 
    100 	switch (frame->key_descriptor_version) {
    101 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_MD5_ARC4:
    102 		return hmac_md5(kck, 16, frame, frame_len, mic, mic_len);
    103 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_SHA1_AES:
    104 		return hmac_sha1(kck, 16, frame, frame_len, mic, mic_len);
    105 	case EAPOL_KEY_DESCRIPTOR_VERSION_AES_128_CMAC_AES:
    106 		return cmac_aes(kck, 16, frame, frame_len, mic, mic_len);
    107 	case EAPOL_KEY_DESCRIPTOR_VERSION_AKM_DEFINED:
    108 		switch (akm) {
    109 		case IE_RSN_AKM_SUITE_SAE_SHA256:
    110 		case IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256:
    111 		case IE_RSN_AKM_SUITE_OSEN:
    112 			return cmac_aes(kck, 16, frame, frame_len,
    113 						mic, mic_len);
    114 		case IE_RSN_AKM_SUITE_FT_OVER_8021X_SHA384:
    115 			return hmac_sha384(kck, 24, frame, frame_len,
    116 						mic, mic_len);
    117 		case IE_RSN_AKM_SUITE_OWE:
    118 			switch (mic_len) {
    119 			case 16:
    120 				return hmac_sha256(kck, mic_len, frame,
    121 						frame_len, mic,
    122 						mic_len);
    123 			case 24:
    124 				return hmac_sha384(kck, 24, frame, frame_len,
    125 						mic, mic_len);
    126 			}
    127 
    128 			/* fall through */
    129 		default:
    130 			return false;
    131 		}
    132 	default:
    133 		return false;
    134 	}
    135 }
    136 
    137 bool eapol_verify_mic(enum ie_rsn_akm_suite akm, const uint8_t *kck,
    138 			const struct eapol_key *frame, size_t mic_len)
    139 {
    140 	uint8_t mic[MIC_MAXLEN];
    141 	struct iovec iov[3];
    142 	struct l_checksum *checksum = NULL;
    143 
    144 	iov[0].iov_base = (void *) frame;
    145 	iov[0].iov_len = offsetof(struct eapol_key, key_data);
    146 
    147 	memset(mic, 0, sizeof(mic));
    148 	iov[1].iov_base = mic;
    149 	iov[1].iov_len = mic_len;
    150 
    151 	iov[2].iov_base = (void *) EAPOL_KEY_DATA(frame, mic_len) - 2;
    152 	iov[2].iov_len = EAPOL_KEY_DATA_LEN(frame, mic_len) + 2;
    153 
    154 	switch (frame->key_descriptor_version) {
    155 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_MD5_ARC4:
    156 		checksum = l_checksum_new_hmac(L_CHECKSUM_MD5, kck, 16);
    157 		break;
    158 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_SHA1_AES:
    159 		checksum = l_checksum_new_hmac(L_CHECKSUM_SHA1, kck, 16);
    160 		break;
    161 	case EAPOL_KEY_DESCRIPTOR_VERSION_AES_128_CMAC_AES:
    162 		checksum = l_checksum_new_cmac_aes(kck, 16);
    163 		break;
    164 	case EAPOL_KEY_DESCRIPTOR_VERSION_AKM_DEFINED:
    165 		switch (akm) {
    166 		case IE_RSN_AKM_SUITE_SAE_SHA256:
    167 		case IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256:
    168 		case IE_RSN_AKM_SUITE_OSEN:
    169 			checksum = l_checksum_new_cmac_aes(kck, 16);
    170 			break;
    171 		case IE_RSN_AKM_SUITE_FT_OVER_8021X_SHA384:
    172 			checksum = l_checksum_new_hmac(L_CHECKSUM_SHA384,
    173 							kck, 24);
    174 			break;
    175 		case IE_RSN_AKM_SUITE_OWE:
    176 			switch (mic_len) {
    177 			case 16:
    178 				checksum = l_checksum_new_hmac(
    179 							L_CHECKSUM_SHA256,
    180 							kck, 16);
    181 				break;
    182 			case 24:
    183 				checksum = l_checksum_new_hmac(
    184 							L_CHECKSUM_SHA384,
    185 							kck, 24);
    186 				break;
    187 			case 32:
    188 				checksum = l_checksum_new_hmac(
    189 							L_CHECKSUM_SHA512,
    190 							kck, 32);
    191 				break;
    192 			default:
    193 				l_error("Invalid MIC length of %zu for OWE",
    194 						mic_len);
    195 				return false;
    196 			}
    197 
    198 			break;
    199 		default:
    200 			return false;
    201 		}
    202 
    203 		break;
    204 	default:
    205 		return false;
    206 	}
    207 
    208 	if (checksum == NULL)
    209 		return false;
    210 
    211 	l_checksum_updatev(checksum, iov, 3);
    212 	l_checksum_get_digest(checksum, mic, mic_len);
    213 	l_checksum_free(checksum);
    214 
    215 	if (!memcmp(frame->key_data, mic, mic_len))
    216 		return true;
    217 
    218 	return false;
    219 }
    220 
    221 /*
    222  * IEEE 802.11 Table 12-8 -- Integrity and key-wrap algorithms
    223  */
    224 static size_t eapol_get_mic_length(enum ie_rsn_akm_suite akm, size_t pmk_len)
    225 {
    226 	switch (akm) {
    227 	case IE_RSN_AKM_SUITE_8021X_SUITE_B_SHA384:
    228 	case IE_RSN_AKM_SUITE_FT_OVER_8021X_SHA384:
    229 		return 24;
    230 	case IE_RSN_AKM_SUITE_OWE:
    231 		switch (pmk_len) {
    232 		case 32:
    233 			return 16;
    234 		case 48:
    235 			return 24;
    236 		case 64:
    237 			return 32;
    238 		default:
    239 			l_error("Invalid PMK length of %zu for OWE", pmk_len);
    240 			return 0;
    241 		}
    242 	case IE_RSN_AKM_SUITE_FILS_SHA256:
    243 	case IE_RSN_AKM_SUITE_FILS_SHA384:
    244 	case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256:
    245 	case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384:
    246 		return 0;
    247 	default:
    248 		return 16;
    249 	}
    250 }
    251 
    252 uint8_t *eapol_decrypt_key_data(enum ie_rsn_akm_suite akm, const uint8_t *kek,
    253 				const struct eapol_key *frame,
    254 				size_t *decrypted_size, size_t mic_len)
    255 {
    256 	size_t key_data_len = EAPOL_KEY_DATA_LEN(frame, mic_len);
    257 	const uint8_t *key_data = EAPOL_KEY_DATA(frame, mic_len);
    258 	size_t expected_len;
    259 	uint8_t *buf;
    260 	size_t kek_len;
    261 
    262 	switch (frame->key_descriptor_version) {
    263 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_MD5_ARC4:
    264 		expected_len = key_data_len;
    265 		break;
    266 	case EAPOL_KEY_DESCRIPTOR_VERSION_AKM_DEFINED:
    267 		switch (akm) {
    268 		case IE_RSN_AKM_SUITE_FILS_SHA256:
    269 		case IE_RSN_AKM_SUITE_FILS_SHA384:
    270 		case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256:
    271 		case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384:
    272 			if (key_data_len < 16)
    273 				return NULL;
    274 
    275 			expected_len = key_data_len - 16;
    276 			break;
    277 		case IE_RSN_AKM_SUITE_SAE_SHA256:
    278 		case IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256:
    279 		case IE_RSN_AKM_SUITE_OWE:
    280 		case IE_RSN_AKM_SUITE_OSEN:
    281 		case IE_RSN_AKM_SUITE_FT_OVER_8021X_SHA384:
    282 			if (key_data_len < 24 || key_data_len % 8)
    283 				return NULL;
    284 
    285 			expected_len = key_data_len - 8;
    286 			break;
    287 		default:
    288 			return NULL;
    289 		}
    290 
    291 		break;
    292 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_SHA1_AES:
    293 	case EAPOL_KEY_DESCRIPTOR_VERSION_AES_128_CMAC_AES:
    294 		if (key_data_len < 24 || key_data_len % 8)
    295 			return NULL;
    296 
    297 		expected_len = key_data_len - 8;
    298 		break;
    299 	default:
    300 		return NULL;
    301 	}
    302 
    303 	buf = l_new(uint8_t, expected_len);
    304 
    305 	switch (frame->key_descriptor_version) {
    306 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_MD5_ARC4:
    307 	{
    308 		uint8_t key[32];
    309 		bool ret;
    310 
    311 		memcpy(key, frame->eapol_key_iv, 16);
    312 		memcpy(key + 16, kek, 16);
    313 
    314 		ret = arc4_skip(key, 32, 256, key_data, key_data_len, buf);
    315 		explicit_bzero(key, sizeof(key));
    316 
    317 		if (!ret)
    318 			goto error;
    319 
    320 		break;
    321 	}
    322 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_SHA1_AES:
    323 	case EAPOL_KEY_DESCRIPTOR_VERSION_AES_128_CMAC_AES:
    324 	case EAPOL_KEY_DESCRIPTOR_VERSION_AKM_DEFINED:
    325 		switch (akm) {
    326 		case IE_RSN_AKM_SUITE_OWE:
    327 		case IE_RSN_AKM_SUITE_FT_OVER_8021X_SHA384:
    328 			switch (mic_len) {
    329 			case 16:
    330 				kek_len = 16;
    331 				break;
    332 			case 24:
    333 			case 32:
    334 				kek_len = 32;
    335 				break;
    336 			default:
    337 				l_error("Invalid MIC length of %zu for OWE",
    338 						mic_len);
    339 				goto error;
    340 			}
    341 
    342 			if (!aes_unwrap(kek, kek_len, key_data,
    343 						key_data_len, buf))
    344 				goto error;
    345 
    346 			break;
    347 		case IE_RSN_AKM_SUITE_FILS_SHA256:
    348 		case IE_RSN_AKM_SUITE_FILS_SHA384:
    349 		case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256:
    350 		case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384:
    351 		{
    352 			struct iovec ad[1];
    353 
    354 			ad[0].iov_base = (void *)frame;
    355 			ad[0].iov_len = key_data - (const uint8_t *)frame;
    356 
    357 			if (akm == IE_RSN_AKM_SUITE_FILS_SHA256 || akm ==
    358 					IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256)
    359 				kek_len = 32;
    360 			else
    361 				kek_len = 64;
    362 
    363 			if (!aes_siv_decrypt(kek, kek_len, key_data,
    364 						key_data_len, ad, 1, buf))
    365 				goto error;
    366 
    367 			break;
    368 		}
    369 		default:
    370 			kek_len = 16;
    371 
    372 			if (!aes_unwrap(kek, kek_len, key_data,
    373 						key_data_len, buf))
    374 				goto error;
    375 			break;
    376 		}
    377 
    378 		break;
    379 	}
    380 
    381 	if (decrypted_size)
    382 		*decrypted_size = expected_len;
    383 
    384 	return buf;
    385 
    386 error:
    387 	l_free(buf);
    388 	return NULL;
    389 }
    390 
    391 static int padded_aes_wrap(const uint8_t *kek, uint8_t *key_data,
    392 				size_t *key_data_len,
    393 				struct eapol_key *out_frame, size_t mic_len)
    394 {
    395 	if (*key_data_len < 16 || *key_data_len % 8)
    396 		key_data[(*key_data_len)++] = 0xdd;
    397 	while (*key_data_len < 16 || *key_data_len % 8)
    398 		key_data[(*key_data_len)++] = 0x00;
    399 
    400 	if (!aes_wrap(kek, key_data, *key_data_len,
    401 				EAPOL_KEY_DATA(out_frame, mic_len)))
    402 		return -ENOPROTOOPT;
    403 
    404 	*key_data_len += 8;
    405 	return 0;
    406 }
    407 
    408 /*
    409  * Pad and encrypt the plaintext Key Data contents in @key_data using
    410  * the encryption scheme required by @out_frame->key_descriptor_version,
    411  * write results to @out_frame->key_data and @out_frame->key_data_len.
    412  *
    413  * Note that for efficiency @key_data is being modified, including in
    414  * case of failure, so it must be sufficiently larger than @key_data_len.
    415  */
    416 static int eapol_encrypt_key_data(enum ie_rsn_akm_suite akm, const uint8_t *kek,
    417 				uint8_t *key_data, size_t key_data_len,
    418 				struct eapol_key *out_frame, size_t mic_len)
    419 {
    420 	uint8_t key[32];
    421 	int ret;
    422 
    423 	switch (out_frame->key_descriptor_version) {
    424 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_MD5_ARC4:
    425 		/*
    426 		 * Not following the spec to generate the IV. The spec outlines
    427 		 * a procedure where a 32 byte buffer is held and incremented
    428 		 * each time nonces are created, and the IV comes from this
    429 		 * buffer. In the end randomizing the IV every time should be
    430 		 * just as good. This is how we handle the GTK in AP mode.
    431 		 */
    432 		l_getrandom(out_frame->eapol_key_iv, 16);
    433 
    434 		memcpy(key, out_frame->eapol_key_iv, 16);
    435 		memcpy(key + 16, kek, 16);
    436 
    437 		ret = arc4_skip(key, 32, 256, key_data, key_data_len,
    438 				EAPOL_KEY_DATA(out_frame, mic_len));
    439 		explicit_bzero(key, sizeof(key));
    440 
    441 		if (!ret)
    442 			return -ENOTSUP;
    443 
    444 		break;
    445 	case EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_SHA1_AES:
    446 	case EAPOL_KEY_DESCRIPTOR_VERSION_AES_128_CMAC_AES:
    447 		ret = padded_aes_wrap(kek, key_data, &key_data_len,
    448 					out_frame, mic_len);
    449 		if (ret < 0)
    450 			return ret;
    451 
    452 		break;
    453 	case EAPOL_KEY_DESCRIPTOR_VERSION_AKM_DEFINED:
    454 		switch (akm) {
    455 		case IE_RSN_AKM_SUITE_SAE_SHA256:
    456 			ret = padded_aes_wrap(kek, key_data, &key_data_len,
    457 						out_frame, mic_len);
    458 			if (ret < 0)
    459 				return ret;
    460 			break;
    461 		default:
    462 			return -ENOTSUP;
    463 		}
    464 	}
    465 
    466 	l_put_be16(key_data_len, EAPOL_KEY_DATA(out_frame, mic_len) - 2);
    467 
    468 	return key_data_len;
    469 }
    470 
    471 static void eapol_key_data_append(struct eapol_key *ek,
    472 				size_t mic_len,
    473 				enum handshake_kde selector,
    474 				const uint8_t *data, size_t data_len)
    475 {
    476 	uint16_t key_data_len = EAPOL_KEY_DATA_LEN(ek, mic_len);
    477 	uint8_t *ptr = EAPOL_KEY_DATA(ek, mic_len);
    478 
    479 	ptr[key_data_len++] = IE_TYPE_VENDOR_SPECIFIC;
    480 	ptr[key_data_len++] = 4 + data_len;
    481 	l_put_be32(selector, ptr + key_data_len);
    482 	key_data_len += 4;
    483 	memcpy(ptr + key_data_len, data, data_len);
    484 	key_data_len += data_len;
    485 	l_put_be16(key_data_len, ek->key_data + mic_len);
    486 }
    487 
    488 #define VERIFY_PTK_COMMON(ek)	\
    489 	if (!ek->key_type)	\
    490 		return false;	\
    491 	if (ek->smk_message)	\
    492 		return false;	\
    493 	if (ek->request)	\
    494 		return false;	\
    495 	if (ek->error)		\
    496 		return false	\
    497 
    498 bool eapol_verify_ptk_1_of_4(const struct eapol_key *ek, size_t mic_len,
    499 				bool ptk_complete)
    500 {
    501 	/* Verify according to 802.11, Section 11.6.6.2 */
    502 	VERIFY_PTK_COMMON(ek);
    503 
    504 	if (ek->install)
    505 		return false;
    506 
    507 	if (!ek->key_ack)
    508 		return false;
    509 
    510 	if (ek->key_mic)
    511 		return false;
    512 
    513 	L_WARN_ON(ek->secure != ptk_complete);
    514 
    515 	if (ek->encrypted_key_data)
    516 		return false;
    517 
    518 	if (ek->wpa_key_id)
    519 		return false;
    520 
    521 	VERIFY_IS_ZERO(ek->key_rsc);
    522 	VERIFY_IS_ZERO(ek->reserved);
    523 
    524 	if (!l_memeqzero(EAPOL_KEY_MIC(ek), mic_len))
    525 		return false;
    526 
    527 	return true;
    528 }
    529 
    530 bool eapol_verify_ptk_2_of_4(const struct eapol_key *ek, bool ptk_complete)
    531 {
    532 	uint16_t key_len;
    533 
    534 	/* Verify according to 802.11, Section 11.6.6.3 */
    535 	VERIFY_PTK_COMMON(ek);
    536 
    537 	if (ek->install)
    538 		return false;
    539 
    540 	if (ek->key_ack)
    541 		return false;
    542 
    543 	if (!ek->key_mic)
    544 		return false;
    545 
    546 	L_WARN_ON(ek->secure != ptk_complete);
    547 
    548 	if (ek->encrypted_key_data)
    549 		return false;
    550 
    551 	if (ek->wpa_key_id)
    552 		return false;
    553 
    554 	if (ek->request)
    555 		return false;
    556 
    557 	key_len = L_BE16_TO_CPU(ek->key_length);
    558 	L_WARN_ON(key_len != 0);
    559 
    560 	VERIFY_IS_ZERO(ek->eapol_key_iv);
    561 	VERIFY_IS_ZERO(ek->key_rsc);
    562 	VERIFY_IS_ZERO(ek->reserved);
    563 
    564 	return true;
    565 }
    566 
    567 bool eapol_verify_ptk_3_of_4(const struct eapol_key *ek, bool is_wpa,
    568 				size_t mic_len)
    569 {
    570 	uint16_t key_len;
    571 
    572 	/* Verify according to 802.11, Section 11.6.6.4 */
    573 	VERIFY_PTK_COMMON(ek);
    574 
    575 	/*
    576 	 * TODO: Handle cases where install might be 0:
    577 	 * For PTK generation, 0 only if the AP does not support key mapping
    578 	 * keys, or if the STA has the No Pairwise bit (in the RSN Capabilities
    579 	 * field) equal to 1 and only the group key is used.
    580 	 */
    581 	if (!ek->install)
    582 		return false;
    583 
    584 	if (!ek->key_ack)
    585 		return false;
    586 
    587 	if (mic_len && !ek->key_mic)
    588 		return false;
    589 
    590 	if (ek->secure != !is_wpa)
    591 		return false;
    592 
    593 	/* Must be encrypted when GTK is present but reserved in WPA */
    594 	if (!ek->encrypted_key_data && !is_wpa)
    595 		return false;
    596 
    597 	if (ek->wpa_key_id)
    598 		return false;
    599 
    600 	key_len = L_BE16_TO_CPU(ek->key_length);
    601 	if (key_len != 16 && key_len != 32)
    602 		return false;
    603 
    604 	VERIFY_IS_ZERO(ek->reserved);
    605 
    606 	return true;
    607 }
    608 
    609 bool eapol_verify_ptk_4_of_4(const struct eapol_key *ek, bool is_wpa)
    610 {
    611 	uint16_t key_len;
    612 
    613 	/* Verify according to 802.11, Section 11.6.6.5 */
    614 	VERIFY_PTK_COMMON(ek);
    615 
    616 	if (ek->install)
    617 		return false;
    618 
    619 	if (ek->key_ack)
    620 		return false;
    621 
    622 	if (!ek->key_mic)
    623 		return false;
    624 
    625 	if (ek->secure != !is_wpa)
    626 		return false;
    627 
    628 	if (ek->encrypted_key_data)
    629 		return false;
    630 
    631 	if (ek->wpa_key_id)
    632 		return false;
    633 
    634 	if (ek->request)
    635 		return false;
    636 
    637 	key_len = L_BE16_TO_CPU(ek->key_length);
    638 	L_WARN_ON(key_len != 0);
    639 
    640 	VERIFY_IS_ZERO(ek->key_nonce);
    641 	VERIFY_IS_ZERO(ek->eapol_key_iv);
    642 	VERIFY_IS_ZERO(ek->key_rsc);
    643 	VERIFY_IS_ZERO(ek->reserved);
    644 
    645 	return true;
    646 }
    647 
    648 #define VERIFY_GTK_COMMON(ek)	\
    649 	if (ek->key_type)	\
    650 		return false;	\
    651 	if (ek->smk_message)	\
    652 		return false;	\
    653 	if (ek->request)	\
    654 		return false;	\
    655 	if (ek->error)		\
    656 		return false;	\
    657 	if (ek->install)	\
    658 		return false	\
    659 
    660 bool eapol_verify_gtk_1_of_2(const struct eapol_key *ek, bool is_wpa,
    661 				size_t mic_len)
    662 {
    663 	uint16_t key_len;
    664 
    665 	VERIFY_GTK_COMMON(ek);
    666 
    667 	if (!ek->key_ack)
    668 		return false;
    669 
    670 	if (mic_len && !ek->key_mic)
    671 		return false;
    672 
    673 	if (!ek->secure)
    674 		return false;
    675 
    676 	/* Must be encrypted when GTK is present but reserved in WPA */
    677 	if (!ek->encrypted_key_data && !is_wpa)
    678 		return false;
    679 
    680 	/*
    681 	 * In P802.11i/D3.0 the Key Length should be 16 for WPA but hostapd
    682 	 * uses 16 for CCMP and 32 for TKIP.  Since 802.11i-2004 there's
    683 	 * inconsistency in the required value, for example 0 is clearly
    684 	 * specified in 802.11-2012 11.6.7.2 but 11.6.2 doesn't list 0 and
    685 	 * makes the value depend on the pairwise key type.
    686 	 */
    687 	key_len = L_BE16_TO_CPU(ek->key_length);
    688 	if (key_len != 0 && key_len != 16 && key_len != 32)
    689 		return false;
    690 
    691 	VERIFY_IS_ZERO(ek->reserved);
    692 
    693 	/*
    694 	 * WPA_80211_v3_1, Section 2.2.4:
    695 	 * "Key Index (bits 4 and 5): specifies the key id of the temporal
    696 	 * key of the key derived from the message. The value of this shall be
    697 	 * zero (0) if the value of Key Type (bit 4) is Pairwise (1). The Key
    698 	 * Type and Key Index shall not both be 0 in the same message.
    699 	 *
    700 	 * Group keys shall not use key id 0. This means that key ids 1 to 3
    701 	 * are available to be used to identify Group keys. This document
    702 	 * recommends that implementations reserve key ids 1 and 2 for Group
    703 	 * Keys, and that key id 3 is not used.
    704 	 */
    705 	if (is_wpa && !ek->wpa_key_id)
    706 		return false;
    707 
    708 	return true;
    709 }
    710 
    711 bool eapol_verify_gtk_2_of_2(const struct eapol_key *ek, bool is_wpa)
    712 {
    713 	uint16_t key_len;
    714 
    715 	/* Verify according to 802.11, Section 11.6.7.3 */
    716 	VERIFY_GTK_COMMON(ek);
    717 
    718 	if (ek->key_ack)
    719 		return false;
    720 
    721 	if (!ek->key_mic)
    722 		return false;
    723 
    724 	if (!ek->secure)
    725 		return false;
    726 
    727 	if (ek->encrypted_key_data)
    728 		return false;
    729 
    730 	key_len = L_BE16_TO_CPU(ek->key_length);
    731 	if (key_len != 0)
    732 		return false;
    733 
    734 	VERIFY_IS_ZERO(ek->key_nonce);
    735 	VERIFY_IS_ZERO(ek->eapol_key_iv);
    736 	VERIFY_IS_ZERO(ek->key_rsc);
    737 	VERIFY_IS_ZERO(ek->reserved);
    738 
    739 	return true;
    740 }
    741 
    742 static struct eapol_key *eapol_create_common(
    743 				enum eapol_protocol_version protocol,
    744 				enum eapol_key_descriptor_version version,
    745 				bool secure,
    746 				uint64_t key_replay_counter,
    747 				const uint8_t snonce[],
    748 				size_t extra_len,
    749 				const uint8_t *extra_data,
    750 				int key_type,
    751 				bool is_wpa,
    752 				size_t mic_len)
    753 {
    754 	size_t extra_key_len = (mic_len == 0) ? 16 : 0;
    755 	size_t to_alloc = EAPOL_FRAME_LEN(mic_len);
    756 
    757 	struct eapol_key *out_frame = l_malloc(to_alloc + extra_len +
    758 						extra_key_len);
    759 
    760 	memset(out_frame, 0, to_alloc + extra_len + extra_key_len);
    761 
    762 	out_frame->header.protocol_version = protocol;
    763 	out_frame->header.packet_type = 0x3;
    764 	out_frame->header.packet_len = L_CPU_TO_BE16(to_alloc + extra_len +
    765 							extra_key_len - 4);
    766 	out_frame->descriptor_type = is_wpa ? EAPOL_DESCRIPTOR_TYPE_WPA :
    767 		EAPOL_DESCRIPTOR_TYPE_80211;
    768 	out_frame->key_descriptor_version = version;
    769 	out_frame->key_type = key_type;
    770 	out_frame->install = false;
    771 	out_frame->key_ack = false;
    772 	out_frame->key_mic = (mic_len) ? true : false;
    773 	out_frame->secure = secure;
    774 	out_frame->error = false;
    775 	out_frame->request = false;
    776 	out_frame->encrypted_key_data = (mic_len) ? false : true;
    777 	out_frame->smk_message = false;
    778 	out_frame->key_length = 0;
    779 	out_frame->key_replay_counter = L_CPU_TO_BE64(key_replay_counter);
    780 	memcpy(out_frame->key_nonce, snonce, sizeof(out_frame->key_nonce));
    781 
    782 	l_put_be16(extra_len + extra_key_len, out_frame->key_data + mic_len);
    783 
    784 	if (extra_len)
    785 		memcpy(EAPOL_KEY_DATA(out_frame, mic_len), extra_data,
    786 					extra_len);
    787 
    788 	return out_frame;
    789 }
    790 
    791 struct eapol_key *eapol_create_ptk_2_of_4(
    792 				enum eapol_protocol_version protocol,
    793 				enum eapol_key_descriptor_version version,
    794 				uint64_t key_replay_counter,
    795 				const uint8_t snonce[],
    796 				size_t extra_len,
    797 				const uint8_t *extra_data,
    798 				bool is_wpa,
    799 				size_t mic_len,
    800 				bool secure)
    801 {
    802 	return eapol_create_common(protocol, version, secure,
    803 					key_replay_counter, snonce, extra_len,
    804 					extra_data, 1, is_wpa, mic_len);
    805 }
    806 
    807 struct eapol_key *eapol_create_ptk_4_of_4(
    808 				enum eapol_protocol_version protocol,
    809 				enum eapol_key_descriptor_version version,
    810 				uint64_t key_replay_counter,
    811 				bool is_wpa,
    812 				size_t mic_len)
    813 {
    814 	uint8_t snonce[32];
    815 
    816 	memset(snonce, 0, sizeof(snonce));
    817 	return eapol_create_common(protocol, version,
    818 					is_wpa ? false : true,
    819 					key_replay_counter, snonce, 0, NULL,
    820 					1, is_wpa, mic_len);
    821 }
    822 
    823 struct eapol_key *eapol_create_gtk_2_of_2(
    824 				enum eapol_protocol_version protocol,
    825 				enum eapol_key_descriptor_version version,
    826 				uint64_t key_replay_counter,
    827 				size_t extra_len,
    828 				const uint8_t *extra_data,
    829 				bool is_wpa, uint8_t wpa_key_id, size_t mic_len)
    830 {
    831 	uint8_t snonce[32];
    832 	struct eapol_key *step2;
    833 
    834 	memset(snonce, 0, sizeof(snonce));
    835 	step2 = eapol_create_common(protocol, version, true,
    836 					key_replay_counter, snonce,
    837 					extra_len, extra_data, 0, is_wpa,
    838 					mic_len);
    839 
    840 	if (!step2)
    841 		return step2;
    842 
    843 	/*
    844 	 * WPA_80211_v3_1, Section 2.2.4:
    845 	 * "The Key Type and Key Index shall not both be 0 in the same message"
    846 	 *
    847 	 * The above means that even though sending the key index back to the
    848 	 * AP has no practical value, we must still do so.
    849 	 */
    850 	if (is_wpa)
    851 		step2->wpa_key_id = wpa_key_id;
    852 
    853 	return step2;
    854 }
    855 
    856 struct eapol_frame_watch {
    857 	uint32_t ifindex;
    858 	struct watchlist_item super;
    859 };
    860 
    861 static void eapol_frame_watch_free(struct watchlist_item *item)
    862 {
    863 	struct eapol_frame_watch *efw =
    864 		l_container_of(item, struct eapol_frame_watch, super);
    865 
    866 	l_free(efw);
    867 }
    868 
    869 static const struct watchlist_ops eapol_frame_watch_ops = {
    870 	.item_free = eapol_frame_watch_free,
    871 };
    872 
    873 static int32_t eapol_frame_watch_add(uint32_t ifindex,
    874 					eapol_frame_watch_func_t handler,
    875 					void *user_data)
    876 {
    877 	struct eapol_frame_watch *efw;
    878 
    879 	efw = l_new(struct eapol_frame_watch, 1);
    880 	efw->ifindex = ifindex;
    881 
    882 	return watchlist_link(&frame_watches, &efw->super,
    883 				handler, user_data, NULL);
    884 }
    885 
    886 static bool eapol_frame_watch_remove(uint32_t id)
    887 {
    888 	return watchlist_remove(&frame_watches, id);
    889 }
    890 
    891 struct eapol_sm {
    892 	struct handshake_state *handshake;
    893 	enum eapol_protocol_version protocol_version;
    894 	uint64_t replay_counter;
    895 	void *user_data;
    896 	struct l_timeout *timeout;
    897 	struct l_timeout *eapol_start_timeout;
    898 	unsigned int frame_retry;
    899 	uint16_t listen_interval;
    900 	bool have_replay:1;
    901 	bool started:1;
    902 	bool use_eapol_start:1;
    903 	bool require_handshake:1;
    904 	bool eap_exchanged:1;
    905 	bool last_eap_unencrypted:1;
    906 	struct eap_state *eap;
    907 	struct eapol_frame *early_frame;
    908 	bool early_frame_unencrypted : 1;
    909 	uint32_t watch_id;
    910 	uint8_t installed_gtk_len;
    911 	uint8_t installed_gtk[CRYPTO_MAX_GTK_LEN];
    912 	uint8_t installed_igtk_len;
    913 	uint8_t installed_igtk[CRYPTO_MAX_IGTK_LEN];
    914 	unsigned int mic_len;
    915 	bool rekey : 1;
    916 };
    917 
    918 static void eapol_sm_destroy(void *value)
    919 {
    920 	struct eapol_sm *sm = value;
    921 
    922 	l_timeout_remove(sm->timeout);
    923 	l_timeout_remove(sm->eapol_start_timeout);
    924 
    925 	if (sm->eap)
    926 		eap_free(sm->eap);
    927 
    928 	l_free(sm->early_frame);
    929 
    930 	eapol_frame_watch_remove(sm->watch_id);
    931 
    932 	sm->installed_gtk_len = 0;
    933 	explicit_bzero(sm->installed_gtk, sizeof(sm->installed_gtk));
    934 	sm->installed_igtk_len = 0;
    935 	explicit_bzero(sm->installed_igtk, sizeof(sm->installed_igtk));
    936 
    937 	l_free(sm);
    938 }
    939 
    940 struct eapol_sm *eapol_sm_new(struct handshake_state *hs)
    941 {
    942 	struct eapol_sm *sm;
    943 
    944 	sm = l_new(struct eapol_sm, 1);
    945 
    946 	sm->handshake = hs;
    947 
    948 	if (hs->settings_8021x && !hs->authenticator)
    949 		sm->use_eapol_start = true;
    950 
    951 	sm->require_handshake = true;
    952 
    953 	return sm;
    954 }
    955 
    956 void eapol_sm_free(struct eapol_sm *sm)
    957 {
    958 	l_queue_remove(state_machines, sm);
    959 
    960 	eapol_sm_destroy(sm);
    961 }
    962 
    963 void eapol_sm_set_listen_interval(struct eapol_sm *sm, uint16_t interval)
    964 {
    965 	sm->listen_interval = interval;
    966 }
    967 
    968 void eapol_sm_set_user_data(struct eapol_sm *sm, void *user_data)
    969 {
    970 	sm->user_data = user_data;
    971 }
    972 
    973 static void eapol_sm_write(struct eapol_sm *sm, const struct eapol_frame *ef,
    974 				bool noencrypt)
    975 {
    976 	const uint8_t *dst = sm->handshake->authenticator ?
    977 		sm->handshake->spa : sm->handshake->aa;
    978 
    979 	__eapol_tx_packet(sm->handshake->ifindex, dst, ETH_P_PAE, ef,
    980 				noencrypt);
    981 }
    982 
    983 static inline void handshake_failed(struct eapol_sm *sm, uint16_t reason_code)
    984 {
    985 	handshake_event(sm->handshake, HANDSHAKE_EVENT_FAILED, reason_code);
    986 
    987 	eapol_sm_free(sm);
    988 }
    989 
    990 static void eapol_timeout(struct l_timeout *timeout, void *user_data)
    991 {
    992 	struct eapol_sm *sm = user_data;
    993 
    994 	l_timeout_remove(sm->timeout);
    995 	sm->timeout = NULL;
    996 
    997 	handshake_failed(sm, MMPDU_REASON_CODE_4WAY_HANDSHAKE_TIMEOUT);
    998 }
    999 
   1000 static void eapol_install_gtk(struct eapol_sm *sm, uint8_t gtk_key_index,
   1001 					const uint8_t *gtk, size_t gtk_len,
   1002 					const uint8_t *rsc)
   1003 {
   1004 	/*
   1005 	 * Don't install the same GTK.  On older kernels this resets the
   1006 	 * replay counters, etc and can lead to various attacks
   1007 	 */
   1008 	if (sm->installed_gtk_len == gtk_len &&
   1009 			!memcmp(sm->installed_gtk, gtk, gtk_len))
   1010 		return;
   1011 
   1012 	handshake_state_install_gtk(sm->handshake, gtk_key_index,
   1013 					gtk, gtk_len, rsc, 6);
   1014 	memcpy(sm->installed_gtk, gtk, gtk_len);
   1015 	sm->installed_gtk_len = gtk_len;
   1016 }
   1017 
   1018 static void eapol_install_igtk(struct eapol_sm *sm, uint16_t igtk_key_index,
   1019 					const uint8_t *igtk, size_t igtk_len)
   1020 {
   1021 	/*
   1022 	 * Don't install the same IGTK.  On older kernels this resets the
   1023 	 * replay counters, etc and can lead to various attacks
   1024 	 */
   1025 	if (sm->installed_igtk_len == igtk_len - 6 &&
   1026 			!memcmp(sm->installed_igtk, igtk + 6, igtk_len - 6))
   1027 		return;
   1028 
   1029 	handshake_state_install_igtk(sm->handshake, igtk_key_index,
   1030 						igtk + 6, igtk_len - 6, igtk);
   1031 	memcpy(sm->installed_igtk, igtk + 6, igtk_len - 6);
   1032 	sm->installed_igtk_len = igtk_len - 6;
   1033 }
   1034 
   1035 static void __send_eapol_start(struct eapol_sm *sm, bool noencrypt)
   1036 {
   1037 	uint8_t buf[sizeof(struct eapol_frame)] = {};
   1038 	struct eapol_frame *frame = (struct eapol_frame *) buf;
   1039 
   1040 	frame->header.protocol_version = EAPOL_PROTOCOL_VERSION_2001;
   1041 	frame->header.packet_type = 1;
   1042 	l_put_be16(0, &frame->header.packet_len);
   1043 
   1044 	eapol_sm_write(sm, frame, noencrypt);
   1045 }
   1046 
   1047 static void send_eapol_start(struct l_timeout *timeout, void *user_data)
   1048 {
   1049 	struct eapol_sm *sm = user_data;
   1050 
   1051 	l_timeout_remove(sm->eapol_start_timeout);
   1052 	sm->eapol_start_timeout = NULL;
   1053 
   1054 	/*
   1055 	 * AP is probably waiting for us to start, so always send unencrypted
   1056 	 * since the key hasn't been established yet
   1057 	 */
   1058 	__send_eapol_start(sm, true);
   1059 }
   1060 
   1061 static void eapol_set_key_timeout(struct eapol_sm *sm,
   1062 					l_timeout_notify_cb_t cb)
   1063 {
   1064 	/*
   1065 	 * 802.11-2016 12.7.6.6: "The retransmit timeout value shall be
   1066 	 * 100 ms for the first timeout, half the listen interval for the
   1067 	 * second timeout, and the listen interval for subsequent timeouts.
   1068 	 * If there is no listen interval or the listen interval is zero,
   1069 	 * then 100 ms shall be used for all timeout values."
   1070 	 */
   1071 	unsigned int timeout_ms = 100;
   1072 	unsigned int beacon_us = 100 * 1024;
   1073 
   1074 	sm->frame_retry++;
   1075 
   1076 	if (sm->frame_retry == 2 &&
   1077 			sm->listen_interval != 0)
   1078 		timeout_ms = sm->listen_interval * beacon_us / 2000;
   1079 	else if (sm->frame_retry > 2 &&
   1080 			sm->listen_interval != 0)
   1081 		timeout_ms = sm->listen_interval * beacon_us / 1000;
   1082 
   1083 	if (sm->frame_retry > 1)
   1084 		l_timeout_modify_ms(sm->timeout, timeout_ms);
   1085 	else {
   1086 		if (sm->timeout)
   1087 			l_timeout_remove(sm->timeout);
   1088 
   1089 		sm->timeout = l_timeout_create_ms(timeout_ms, cb, sm,
   1090 								NULL);
   1091 	}
   1092 }
   1093 
   1094 /*
   1095  * GCC version 8.3 seems to have trouble correctly calculating
   1096  * ek->header.packet_len when optimization is enabled.  This results in iwd
   1097  * sending invalid 1_of_4 packets (with the KDE payload missing).  Work
   1098  * around this by dropping to O0 for this function when old GCC versions
   1099  * are used
   1100  */
   1101 #if __GNUC__ < 9
   1102 #pragma GCC optimize ("O0")
   1103 #endif
   1104 
   1105 /* 802.11-2016 Section 12.7.6.2 */
   1106 static void eapol_send_ptk_1_of_4(struct eapol_sm *sm)
   1107 {
   1108 	const uint8_t *aa = sm->handshake->aa;
   1109 	uint8_t frame_buf[512];
   1110 	struct eapol_key *ek = (struct eapol_key *) frame_buf;
   1111 	enum crypto_cipher cipher = ie_rsn_cipher_suite_to_cipher(
   1112 				sm->handshake->pairwise_cipher);
   1113 	uint8_t pmkid[16];
   1114 	uint8_t key_descriptor_version;
   1115 
   1116 	handshake_state_new_anonce(sm->handshake);
   1117 
   1118 	sm->replay_counter++;
   1119 
   1120 	memset(ek, 0, EAPOL_FRAME_LEN(sm->mic_len));
   1121 	ek->header.protocol_version = sm->protocol_version;
   1122 	ek->header.packet_type = 0x3;
   1123 	ek->descriptor_type = EAPOL_DESCRIPTOR_TYPE_80211;
   1124 	L_WARN_ON(eapol_key_descriptor_version_from_akm(
   1125 				sm->handshake->akm_suite,
   1126 				sm->handshake->pairwise_cipher,
   1127 				&key_descriptor_version) < 0);
   1128 	ek->key_descriptor_version = key_descriptor_version;
   1129 	ek->key_type = true;
   1130 	ek->key_ack = true;
   1131 	ek->key_length = L_CPU_TO_BE16(crypto_cipher_key_len(cipher));
   1132 	ek->key_replay_counter = L_CPU_TO_BE64(sm->replay_counter);
   1133 	memcpy(ek->key_nonce, sm->handshake->anonce, sizeof(ek->key_nonce));
   1134 
   1135 	/* Write the PMKID KDE into Key Data field unencrypted */
   1136 	crypto_derive_pmkid(sm->handshake->pmk, 32, sm->handshake->spa, aa,
   1137 			pmkid, L_CHECKSUM_SHA1);
   1138 
   1139 	eapol_key_data_append(ek, sm->mic_len, HANDSHAKE_KDE_PMKID, pmkid, 16);
   1140 
   1141 	if (sm->handshake->ptk_complete) {
   1142 		sm->rekey = true;
   1143 		sm->handshake->ptk_complete = false;
   1144 	}
   1145 
   1146 	ek->secure = sm->rekey;
   1147 
   1148 	ek->header.packet_len = L_CPU_TO_BE16(EAPOL_FRAME_LEN(sm->mic_len) +
   1149 				EAPOL_KEY_DATA_LEN(ek, sm->mic_len) - 4);
   1150 
   1151 	l_debug("STA: "MAC" retries=%u", MAC_STR(sm->handshake->spa),
   1152 			sm->frame_retry);
   1153 
   1154 	eapol_sm_write(sm, (struct eapol_frame *) ek, false);
   1155 }
   1156 
   1157 #if __GNUC__ < 9
   1158 #pragma GCC reset_options
   1159 #endif
   1160 
   1161 static void eapol_ptk_1_of_4_retry(struct l_timeout *timeout, void *user_data)
   1162 {
   1163 	struct eapol_sm *sm = user_data;
   1164 
   1165 	if (sm->frame_retry >= 3) {
   1166 		handshake_failed(sm, MMPDU_REASON_CODE_4WAY_HANDSHAKE_TIMEOUT);
   1167 		return;
   1168 	}
   1169 
   1170 	eapol_send_ptk_1_of_4(sm);
   1171 
   1172 	eapol_set_key_timeout(sm, eapol_ptk_1_of_4_retry);
   1173 }
   1174 
   1175 static inline size_t append_ie(uint8_t *ies, const uint8_t *ie)
   1176 {
   1177 	if (!ie)
   1178 		return 0;
   1179 
   1180 	memcpy(ies, ie, ie[1] + 2);
   1181 	return ie[1] + 2;
   1182 }
   1183 
   1184 static size_t append_oci(uint8_t *ies, const struct band_chandef *chandef)
   1185 {
   1186 	unsigned int len = 0;
   1187 
   1188 	ies[len++] = IE_TYPE_VENDOR_SPECIFIC;
   1189 	ies[len++] = 4 + 3;
   1190 	l_put_be32(HANDSHAKE_KDE_OCI, ies + len);
   1191 	len += 4;
   1192 	oci_from_chandef(chandef, ies + len);
   1193 	len += 3;
   1194 
   1195 	return len;
   1196 }
   1197 
   1198 static void eapol_handle_ptk_1_of_4(struct eapol_sm *sm,
   1199 					const struct eapol_key *ek,
   1200 					bool unencrypted)
   1201 {
   1202 	const uint8_t *kck;
   1203 	struct eapol_key *step2;
   1204 	uint8_t mic[MIC_MAXLEN];
   1205 	uint8_t ies[1024];
   1206 	size_t ies_len;
   1207 	const uint8_t *own_ie = sm->handshake->supplicant_ie;
   1208 	const uint8_t *pmkid;
   1209 	struct ie_rsn_info rsn_info;
   1210 
   1211 	l_debug("ifindex=%u", sm->handshake->ifindex);
   1212 
   1213 	if (!eapol_verify_ptk_1_of_4(ek, sm->mic_len,
   1214 					sm->handshake->ptk_complete))
   1215 		return;
   1216 
   1217 	if (sm->handshake->ptk_complete && unencrypted) {
   1218 		l_debug("Dropping unexpectedly unencrypted PTK 1/4 frame");
   1219 		return;
   1220 	}
   1221 
   1222 	pmkid = handshake_util_find_pmkid_kde(EAPOL_KEY_DATA(ek, sm->mic_len),
   1223 					EAPOL_KEY_DATA_LEN(ek, sm->mic_len));
   1224 
   1225 	if (!sm->handshake->wpa_ie) {
   1226 		if (ie_parse_rsne_from_data(own_ie, own_ie[1] + 2,
   1227 						&rsn_info) < 0)
   1228 			goto error_unspecified;
   1229 	}
   1230 
   1231 	/*
   1232 	 * Require the PMKID KDE whenever we've sent a list of PMKIDs in
   1233 	 * our RSNE and we've haven't seen any EAPOL-EAP frame since
   1234 	 * (sm->eap_exchanged is false), otherwise treat it as optional and
   1235 	 * only validate it against our PMK.  Some 802.11-2012 sections
   1236 	 * show message 1/4 without a PMKID KDE and there are APs that
   1237 	 * send no PMKID KDE.
   1238 	 */
   1239 	if (!sm->eap_exchanged && !sm->handshake->wpa_ie &&
   1240 			rsn_info.num_pmkids &&
   1241 			sm->require_handshake) {
   1242 		bool found = false;
   1243 		int i;
   1244 
   1245 		if (!pmkid)
   1246 			goto error_unspecified;
   1247 
   1248 		for (i = 0; i < rsn_info.num_pmkids; i++)
   1249 			if (!l_secure_memcmp(rsn_info.pmkids + i * 16,
   1250 						pmkid, 16)) {
   1251 				found = true;
   1252 				break;
   1253 			}
   1254 
   1255 		if (!found)
   1256 			goto error_unspecified;
   1257 	} else if (pmkid) {
   1258 		if (!handshake_state_pmkid_matches(sm->handshake, pmkid)) {
   1259 			l_debug("Authenticator sent a PMKID that didn't match");
   1260 
   1261 			/*
   1262 			 * If the AP has a different PMKSA from ours and we
   1263 			 * have means to create a new PMKSA through EAP then
   1264 			 * try that, otherwise give up.
   1265 			 */
   1266 			if (sm->eap) {
   1267 				__send_eapol_start(sm, unencrypted);
   1268 				return;
   1269 			}
   1270 
   1271 			/*
   1272 			 * Some APs are known to send a PMKID KDE with all
   1273 			 * zeros for the PMKID.  Others just send seemingly
   1274 			 * random data.  Likely we can still
   1275 			 * successfully negotiate a handshake, so ignore this
   1276 			 * for now and treat it as if the PMKID KDE was not
   1277 			 * included
   1278 			 */
   1279 		}
   1280 	}
   1281 
   1282 	/*
   1283 	 * If we're in a state where we have successfully processed Message 3,
   1284 	 * then assume that the new message 1 is a PTK rekey and start a new
   1285 	 * handshake
   1286 	 */
   1287 	if (!sm->handshake->have_snonce ||
   1288 			memcmp(sm->handshake->anonce,
   1289 					ek->key_nonce, sizeof(ek->key_nonce)) ||
   1290 			sm->handshake->ptk_complete) {
   1291 		if (sm->handshake->ptk_complete && sm->handshake->no_rekey) {
   1292 			/*
   1293 			 * In case of rekey not being allowed, signal to upper
   1294 			 * layers that we need to do a full reauth
   1295 			 */
   1296 			handshake_event(sm->handshake,
   1297 					HANDSHAKE_EVENT_REKEY_FAILED);
   1298 			return;
   1299 		}
   1300 
   1301 		if (sm->handshake->ptk_complete)
   1302 			sm->rekey = true;
   1303 
   1304 		handshake_state_new_snonce(sm->handshake);
   1305 		handshake_state_set_anonce(sm->handshake, ek->key_nonce);
   1306 
   1307 		if (!handshake_state_derive_ptk(sm->handshake))
   1308 			goto error_unspecified;
   1309 	}
   1310 
   1311 	if (IE_AKM_IS_FT(sm->handshake->akm_suite)) {
   1312 		/*
   1313 		 * Rebuild the RSNE to include the PMKR1Name and append
   1314 		 * MDE + FTE.
   1315 		 */
   1316 		rsn_info.num_pmkids = 1;
   1317 		rsn_info.pmkids = sm->handshake->pmk_r1_name;
   1318 
   1319 		ie_build_rsne(&rsn_info, ies);
   1320 		ies_len = ies[1] + 2;
   1321 
   1322 		ies_len += append_ie(ies + ies_len, sm->handshake->mde);
   1323 		ies_len += append_ie(ies + ies_len,
   1324 					sm->handshake->authenticator_fte);
   1325 	} else {
   1326 		ies_len = append_ie(ies, own_ie);
   1327 	}
   1328 
   1329 	if (sm->handshake->support_ip_allocation) {
   1330 		/* Wi-Fi P2P Technical Specification v1.7 Table 58 */
   1331 		ies[ies_len++] = IE_TYPE_VENDOR_SPECIFIC;
   1332 		ies[ies_len++] = 4 + 1;
   1333 		l_put_be32(HANDSHAKE_KDE_IP_ADDRESS_REQ, ies + ies_len);
   1334 		ies_len += 4;
   1335 		ies[ies_len++] = 0x01;
   1336 	}
   1337 
   1338 	/*
   1339 	 * IEEE 802.11-2020 Section 12.7.6.3
   1340 	 * "Additionally, contains an OCI KDE when
   1341 	 *  dot11RSNAOperatingChannelValidationActivated is true on the
   1342 	 *  Supplicant."
   1343 	 */
   1344 	if (sm->handshake->supplicant_ocvc && sm->handshake->chandef)
   1345 		ies_len += append_oci(ies + ies_len, sm->handshake->chandef);
   1346 
   1347 	/*
   1348 	 * 802.11-2020, Section 12.7.6.3:
   1349 	 * "The RSNXE that the Supplicant sent in its (Re)Association Request
   1350 	 * frame, if this element is present in the (Re)Association Request
   1351 	 * frame that the Supplicant sent."
   1352 	 */
   1353 	ies_len += append_ie(ies + ies_len, sm->handshake->supplicant_rsnxe);
   1354 
   1355 	step2 = eapol_create_ptk_2_of_4(sm->protocol_version,
   1356 					ek->key_descriptor_version,
   1357 					L_BE64_TO_CPU(ek->key_replay_counter),
   1358 					sm->handshake->snonce, ies_len, ies,
   1359 					sm->handshake->wpa_ie, sm->mic_len,
   1360 					sm->rekey);
   1361 
   1362 	kck = handshake_state_get_kck(sm->handshake);
   1363 
   1364 	if (sm->mic_len) {
   1365 		if (!eapol_calculate_mic(sm->handshake->akm_suite, kck,
   1366 				step2, mic, sm->mic_len)) {
   1367 			l_info("MIC calculation failed. "
   1368 				"Ensure Kernel Crypto is available.");
   1369 			l_free(step2);
   1370 			handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   1371 
   1372 			return;
   1373 		}
   1374 
   1375 		memcpy(EAPOL_KEY_MIC(step2), mic, sm->mic_len);
   1376 	} else {
   1377 		if (!eapol_aes_siv_encrypt(
   1378 				handshake_state_get_kek(sm->handshake),
   1379 				handshake_state_get_kek_len(sm->handshake),
   1380 				step2, ies, ies_len)) {
   1381 			l_debug("AES-SIV encryption failed");
   1382 			l_free(step2);
   1383 			handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   1384 			return;
   1385 		}
   1386 	}
   1387 
   1388 	eapol_sm_write(sm, (struct eapol_frame *) step2, unencrypted);
   1389 	l_free(step2);
   1390 
   1391 	l_timeout_remove(sm->eapol_start_timeout);
   1392 	sm->eapol_start_timeout = NULL;
   1393 
   1394 	return;
   1395 
   1396 error_unspecified:
   1397 	handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   1398 }
   1399 
   1400 #define EAPOL_PAIRWISE_UPDATE_COUNT 3
   1401 
   1402 /* 802.11-2016 Section 12.7.6.4 */
   1403 static void eapol_send_ptk_3_of_4(struct eapol_sm *sm)
   1404 {
   1405 	uint8_t frame_buf[512];
   1406 	unsigned int rsne_len = sm->handshake->authenticator_ie[1] + 2;
   1407 	uint8_t key_data_buf[128 + rsne_len];
   1408 	int key_data_len = rsne_len;
   1409 	struct eapol_key *ek = (struct eapol_key *) frame_buf;
   1410 	enum crypto_cipher cipher = ie_rsn_cipher_suite_to_cipher(
   1411 				sm->handshake->pairwise_cipher);
   1412 	enum crypto_cipher group_cipher = ie_rsn_cipher_suite_to_cipher(
   1413 				sm->handshake->group_cipher);
   1414 	const uint8_t *kck;
   1415 	const uint8_t *kek;
   1416 	uint8_t key_descriptor_version;
   1417 
   1418 	sm->replay_counter++;
   1419 
   1420 	memset(ek, 0, EAPOL_FRAME_LEN(sm->mic_len));
   1421 	ek->header.protocol_version = sm->protocol_version;
   1422 	ek->header.packet_type = 0x3;
   1423 	ek->descriptor_type = EAPOL_DESCRIPTOR_TYPE_80211;
   1424 	L_WARN_ON(eapol_key_descriptor_version_from_akm(
   1425 				sm->handshake->akm_suite,
   1426 				sm->handshake->pairwise_cipher,
   1427 				&key_descriptor_version) < 0);
   1428 	ek->key_descriptor_version = key_descriptor_version;
   1429 	ek->key_type = true;
   1430 	ek->install = true;
   1431 	ek->key_ack = true;
   1432 	ek->key_mic = true;
   1433 	ek->secure = true;
   1434 	ek->encrypted_key_data = true;
   1435 	ek->key_length = L_CPU_TO_BE16(crypto_cipher_key_len(cipher));
   1436 	ek->key_replay_counter = L_CPU_TO_BE64(sm->replay_counter);
   1437 	memcpy(ek->key_nonce, sm->handshake->anonce, sizeof(ek->key_nonce));
   1438 	memcpy(ek->key_rsc, sm->handshake->gtk_rsc, 6);
   1439 	ek->key_rsc[6] = 0;
   1440 	ek->key_rsc[7] = 0;
   1441 
   1442 	/*
   1443 	 * Just one RSNE in Key Data as we either accept the single pairwise
   1444 	 * cipher in the supplicant IE or fail.
   1445 	 */
   1446 	memcpy(key_data_buf, sm->handshake->authenticator_ie, rsne_len);
   1447 
   1448 	if (group_cipher) {
   1449 		uint8_t *gtk_kde = key_data_buf + key_data_len;
   1450 
   1451 		handshake_util_build_gtk_kde(group_cipher,
   1452 						sm->handshake->gtk,
   1453 						sm->handshake->gtk_index,
   1454 						gtk_kde);
   1455 		key_data_len += gtk_kde[1] + 2;
   1456 	}
   1457 
   1458 	if (sm->handshake->mfp) {
   1459 		enum crypto_cipher group_management_cipher =
   1460 			ie_rsn_cipher_suite_to_cipher(
   1461 				sm->handshake->group_management_cipher);
   1462 		uint8_t *igtk_kde = key_data_buf + key_data_len;
   1463 
   1464 		handshake_util_build_igtk_kde(group_management_cipher,
   1465 						sm->handshake->igtk,
   1466 						sm->handshake->igtk_index,
   1467 						igtk_kde);
   1468 		key_data_len += igtk_kde[1] + 2;
   1469 	}
   1470 
   1471 	if (sm->handshake->support_ip_allocation &&
   1472 			!sm->handshake->client_ip_addr) {
   1473 		handshake_event(sm->handshake, HANDSHAKE_EVENT_P2P_IP_REQUEST);
   1474 
   1475 		/*
   1476 		 * If .support_ip_allocation was set, the
   1477 		 * HANDSHAKE_EVENT_P2P_IP_REQUEST handler is expected to set
   1478 		 * .client_ip_addr if not already set.  Check if the handler
   1479 		 * was successful in allocating an address, if it wasn't we'll
   1480 		 * just skip the IP Address Allocation KDE.  In either case if
   1481 		 * we need to resend message 3/4 the event callback won't be
   1482 		 * triggered again because the condition above will be false.
   1483 		 */
   1484 		if (!sm->handshake->client_ip_addr)
   1485 			sm->handshake->support_ip_allocation = false;
   1486 	}
   1487 
   1488 	if (sm->handshake->support_ip_allocation) {
   1489 		/* Wi-Fi P2P Technical Specification v1.7 Table 59 */
   1490 		key_data_buf[key_data_len++] = IE_TYPE_VENDOR_SPECIFIC;
   1491 		key_data_buf[key_data_len++] = 4 + 12;
   1492 		l_put_be32(HANDSHAKE_KDE_IP_ADDRESS_ALLOC,
   1493 				key_data_buf + key_data_len + 0);
   1494 		l_put_u32(sm->handshake->client_ip_addr,
   1495 				key_data_buf + key_data_len + 4);
   1496 		l_put_u32(sm->handshake->subnet_mask,
   1497 				key_data_buf + key_data_len + 8);
   1498 		l_put_u32(sm->handshake->go_ip_addr,
   1499 				key_data_buf + key_data_len + 12);
   1500 		key_data_len += 4 + 12;
   1501 	}
   1502 
   1503 	kek = handshake_state_get_kek(sm->handshake);
   1504 	key_data_len = eapol_encrypt_key_data(sm->handshake->akm_suite, kek,
   1505 						key_data_buf, key_data_len, ek,
   1506 						sm->mic_len);
   1507 	explicit_bzero(key_data_buf, sizeof(key_data_buf));
   1508 
   1509 	if (key_data_len < 0)
   1510 		return;
   1511 
   1512 	ek->header.packet_len = L_CPU_TO_BE16(EAPOL_FRAME_LEN(sm->mic_len) +
   1513 				key_data_len - 4);
   1514 
   1515 	kck = handshake_state_get_kck(sm->handshake);
   1516 
   1517 	if (!eapol_calculate_mic(sm->handshake->akm_suite, kck, ek,
   1518 			EAPOL_KEY_MIC(ek), sm->mic_len))
   1519 		return;
   1520 
   1521 	l_debug("STA: "MAC" retries=%u", MAC_STR(sm->handshake->spa),
   1522 			sm->frame_retry);
   1523 
   1524 	eapol_sm_write(sm, (struct eapol_frame *) ek, false);
   1525 }
   1526 
   1527 static void eapol_ptk_3_of_4_retry(struct l_timeout *timeout,
   1528 						void *user_data)
   1529 {
   1530 	struct eapol_sm *sm = user_data;
   1531 
   1532 	if (sm->frame_retry >= EAPOL_PAIRWISE_UPDATE_COUNT) {
   1533 		handshake_failed(sm, MMPDU_REASON_CODE_4WAY_HANDSHAKE_TIMEOUT);
   1534 		return;
   1535 	}
   1536 
   1537 	eapol_send_ptk_3_of_4(sm);
   1538 
   1539 	eapol_set_key_timeout(sm, eapol_ptk_3_of_4_retry);
   1540 
   1541 	l_debug("attempt %i", sm->frame_retry);
   1542 }
   1543 
   1544 static const uint8_t *eapol_find_rsne(const uint8_t *data, size_t data_len,
   1545 				const uint8_t **optional)
   1546 {
   1547 	struct ie_tlv_iter iter;
   1548 	const uint8_t *first = NULL;
   1549 
   1550 	ie_tlv_iter_init(&iter, data, data_len);
   1551 
   1552 	while (ie_tlv_iter_next(&iter)) {
   1553 		if (ie_tlv_iter_get_tag(&iter) != IE_TYPE_RSN)
   1554 			continue;
   1555 
   1556 		if (!first) {
   1557 			first = ie_tlv_iter_get_data(&iter) - 2;
   1558 			continue;
   1559 		}
   1560 
   1561 		if (optional)
   1562 			*optional = ie_tlv_iter_get_data(&iter) - 2;
   1563 
   1564 		return first;
   1565 	}
   1566 
   1567 	return first;
   1568 }
   1569 
   1570 static const uint8_t *eapol_find_wfa_kde(const uint8_t *data, size_t data_len,
   1571 					uint8_t oi_type)
   1572 {
   1573 	struct ie_tlv_iter iter;
   1574 
   1575 	ie_tlv_iter_init(&iter, data, data_len);
   1576 
   1577 	while (ie_tlv_iter_next(&iter)) {
   1578 		if (ie_tlv_iter_get_tag(&iter) == IE_TYPE_VENDOR_SPECIFIC) {
   1579 			if (!is_ie_wfa_ie(iter.data, iter.len, oi_type))
   1580 				continue;
   1581 		} else
   1582 			continue;
   1583 
   1584 		return ie_tlv_iter_get_data(&iter) - 2;
   1585 	}
   1586 
   1587 	return NULL;
   1588 }
   1589 
   1590 /* 802.11-2016 Section 12.7.6.3 */
   1591 static void eapol_handle_ptk_2_of_4(struct eapol_sm *sm,
   1592 					const struct eapol_key *ek)
   1593 {
   1594 	const uint8_t *rsne;
   1595 	size_t ptk_size;
   1596 	const uint8_t *kck;
   1597 	const uint8_t *aa = sm->handshake->aa;
   1598 	enum l_checksum_type type;
   1599 
   1600 	l_debug("ifindex=%u", sm->handshake->ifindex);
   1601 
   1602 	if (!eapol_verify_ptk_2_of_4(ek, sm->rekey))
   1603 		return;
   1604 
   1605 	if (L_BE64_TO_CPU(ek->key_replay_counter) != sm->replay_counter)
   1606 		return;
   1607 
   1608 	ptk_size = handshake_state_get_ptk_size(sm->handshake);
   1609 
   1610 	type = L_CHECKSUM_SHA1;
   1611 	if (sm->handshake->akm_suite == IE_RSN_AKM_SUITE_SAE_SHA256)
   1612 		type = L_CHECKSUM_SHA256;
   1613 
   1614 	if (!crypto_derive_pairwise_ptk(sm->handshake->pmk,
   1615 					sm->handshake->pmk_len,
   1616 					sm->handshake->spa, aa,
   1617 					sm->handshake->anonce, ek->key_nonce,
   1618 					sm->handshake->ptk, ptk_size,
   1619 					type))
   1620 		return;
   1621 
   1622 	kck = handshake_state_get_kck(sm->handshake);
   1623 
   1624 	if (!eapol_verify_mic(sm->handshake->akm_suite, kck, ek,
   1625 					sm->mic_len))
   1626 		return;
   1627 
   1628 	/*
   1629 	 * 12.7.6.3 b) 2) "the Authenticator checks that the RSNE bitwise
   1630 	 * matches that from the (Re)Association Request frame.
   1631 	 */
   1632 	rsne = eapol_find_rsne(EAPOL_KEY_DATA(ek, sm->mic_len),
   1633 				EAPOL_KEY_DATA_LEN(ek, sm->mic_len), NULL);
   1634 	if (!rsne || rsne[1] != sm->handshake->supplicant_ie[1] ||
   1635 			memcmp(rsne + 2, sm->handshake->supplicant_ie + 2,
   1636 				rsne[1])) {
   1637 		handshake_failed(sm, MMPDU_REASON_CODE_IE_DIFFERENT);
   1638 		return;
   1639 	}
   1640 
   1641 	if (sm->handshake->support_ip_allocation) {
   1642 		size_t len;
   1643 		const uint8_t *ip_req_kde =
   1644 			handshake_util_find_kde(HANDSHAKE_KDE_IP_ADDRESS_REQ,
   1645 					EAPOL_KEY_DATA(ek, sm->mic_len),
   1646 					EAPOL_KEY_DATA_LEN(ek, sm->mic_len),
   1647 					&len);
   1648 
   1649 		if (ip_req_kde && (len < 1 || ip_req_kde[0] != 0x01)) {
   1650 			l_debug("Invalid IP Address Request KDE in frame 2/4");
   1651 			handshake_failed(sm, MMPDU_REASON_CODE_INVALID_IE);
   1652 			return;
   1653 		}
   1654 
   1655 		sm->handshake->support_ip_allocation = ip_req_kde != NULL;
   1656 	}
   1657 
   1658 	/*
   1659 	 * If the snonce is already set don't reset the retry counter as this
   1660 	 * is a rekey. To be safe take the most recent snonce (in this frame)
   1661 	 * in case the station created a new one.
   1662 	 */
   1663 	if (!sm->handshake->have_snonce)
   1664 		sm->frame_retry = 0;
   1665 
   1666 	memcpy(sm->handshake->snonce, ek->key_nonce,
   1667 			sizeof(sm->handshake->snonce));
   1668 	sm->handshake->have_snonce = true;
   1669 
   1670 	eapol_ptk_3_of_4_retry(NULL, sm);
   1671 }
   1672 
   1673 static const uint8_t *eapol_find_wpa_ie(const uint8_t *data, size_t data_len)
   1674 {
   1675 	struct ie_tlv_iter iter;
   1676 
   1677 	ie_tlv_iter_init(&iter, data, data_len);
   1678 
   1679 	while (ie_tlv_iter_next(&iter)) {
   1680 		if (ie_tlv_iter_get_tag(&iter) != IE_TYPE_VENDOR_SPECIFIC)
   1681 			continue;
   1682 
   1683 		if (is_ie_wpa_ie(ie_tlv_iter_get_data(&iter),
   1684 				ie_tlv_iter_get_length(&iter)))
   1685 			return ie_tlv_iter_get_data(&iter) - 2;
   1686 	}
   1687 
   1688 	return NULL;
   1689 }
   1690 
   1691 static bool eapol_check_ip_mask(const uint8_t *mask,
   1692 				const uint8_t *ip1, const uint8_t *ip2)
   1693 {
   1694 	uint32_t mask_uint = l_get_be32(mask);
   1695 	uint32_t ip1_uint = l_get_be32(ip1);
   1696 	uint32_t ip2_uint = l_get_be32(ip2);
   1697 
   1698 	return
   1699 		/* Check IPs are in the same subnet */
   1700 		((ip1_uint ^ ip2_uint) & mask_uint) == 0 &&
   1701 		/* Check IPs are different */
   1702 		ip1_uint != ip2_uint &&
   1703 		/* Check IPs are not subnet addresses */
   1704 		(ip1_uint & ~mask_uint) != 0 &&
   1705 		(ip2_uint & ~mask_uint) != 0 &&
   1706 		/* Check IPs are not broadcast addresses */
   1707 		(ip1_uint | mask_uint) != 0xffffffff &&
   1708 		(ip2_uint | mask_uint) != 0xffffffff &&
   1709 		/* Check the 1s are at the start of the mask */
   1710 		(uint32_t) (mask_uint << __builtin_popcountl(mask_uint)) == 0;
   1711 }
   1712 
   1713 static int eapol_ie_matches(const void *ies, size_t ies_len,
   1714 					enum ie_type type, uint8_t *target_ie)
   1715 {
   1716 	struct ie_tlv_iter iter;
   1717 
   1718 	ie_tlv_iter_init(&iter, ies, ies_len);
   1719 
   1720 	while (ie_tlv_iter_next(&iter)) {
   1721 		if (ie_tlv_iter_get_tag(&iter) != type)
   1722 			continue;
   1723 
   1724 		if (!target_ie)
   1725 			return -EINVAL;
   1726 
   1727 		if (memcmp(ie_tlv_iter_get_data(&iter) - 2,
   1728 						target_ie, target_ie[1] + 2))
   1729 			return -EBADMSG;
   1730 
   1731 		return 0;
   1732 	}
   1733 
   1734 	if (!target_ie)
   1735 		return 0;
   1736 
   1737 	return -ENOENT;
   1738 }
   1739 
   1740 static void eapol_handle_ptk_3_of_4(struct eapol_sm *sm,
   1741 					const struct eapol_key *ek,
   1742 					const uint8_t *decrypted_key_data,
   1743 					size_t decrypted_key_data_size,
   1744 					bool unencrypted)
   1745 {
   1746 	struct handshake_state *hs = sm->handshake;
   1747 	const uint8_t *kck;
   1748 	const uint8_t *kek;
   1749 	_auto_(l_free) struct eapol_key *step4 = NULL;
   1750 	uint8_t mic[MIC_MAXLEN];
   1751 	const uint8_t *gtk = NULL;
   1752 	size_t gtk_len;
   1753 	const uint8_t *igtk = NULL;
   1754 	size_t igtk_len;
   1755 	const uint8_t *key_id = NULL;
   1756 	size_t key_id_len;
   1757 	const uint8_t *rsne;
   1758 	struct ie_rsn_info rsn_info;
   1759 	const uint8_t *optional_rsne = NULL;
   1760 	const uint8_t *transition_disable;
   1761 	size_t transition_disable_len;
   1762 	uint8_t gtk_key_index;
   1763 	uint16_t igtk_key_index;
   1764 	const uint8_t *oci;
   1765 	size_t oci_len;
   1766 	int r;
   1767 
   1768 	l_debug("ifindex=%u", hs->ifindex);
   1769 
   1770 	if (!eapol_verify_ptk_3_of_4(ek, hs->wpa_ie, sm->mic_len))
   1771 		return;
   1772 
   1773 	/*
   1774 	 * 802.11-2016, Section 12.7.6.4:
   1775 	 * "On reception of message 3, the Supplicant silently discards the
   1776 	 * message if the Key Replay Counter field value has already been used
   1777 	 * or if the ANonce value in message 3 differs from the ANonce value
   1778 	 * in message 1."
   1779 	 */
   1780 	if (memcmp(hs->anonce, ek->key_nonce, sizeof(ek->key_nonce)))
   1781 		return;
   1782 
   1783 	/*
   1784 	 * 11.6.6.4: "Verifies the RSNE. If it is part of a Fast BSS Transition
   1785 	 * Initial Mobility Domain Association, see 12.4.2. Otherwise, if it is
   1786 	 * not identical to that the STA received in the Beacon or Probe
   1787 	 * Response frame, the STA shall disassociate.
   1788 	 */
   1789 	if (hs->wpa_ie)
   1790 		rsne = eapol_find_wpa_ie(decrypted_key_data,
   1791 					decrypted_key_data_size);
   1792 	else if (hs->osen_ie)
   1793 		rsne = eapol_find_wfa_kde(decrypted_key_data,
   1794 					decrypted_key_data_size,
   1795 					IE_WFA_OI_OSEN);
   1796 	else
   1797 		rsne = eapol_find_rsne(decrypted_key_data,
   1798 					decrypted_key_data_size,
   1799 					&optional_rsne);
   1800 
   1801 	if (!rsne)
   1802 		goto error_ie_different;
   1803 
   1804 	if (!hs->wpa_ie)
   1805 		r = ie_parse_rsne_from_data(rsne, rsne[1] + 2, &rsn_info);
   1806 	else
   1807 		r = ie_parse_wpa_from_data(rsne, rsne[1] + 2, &rsn_info);
   1808 
   1809 	if (r < 0)
   1810 		goto error_ie_different;
   1811 
   1812 	if ((rsne[1] != hs->authenticator_ie[1] ||
   1813 			memcmp(rsne + 2, hs->authenticator_ie + 2, rsne[1])) &&
   1814 			!handshake_util_ap_ie_matches(hs, &rsn_info,
   1815 							hs->authenticator_ie,
   1816 							hs->wpa_ie))
   1817 		goto error_ie_different;
   1818 
   1819 	oci = handshake_util_find_kde(HANDSHAKE_KDE_OCI, decrypted_key_data,
   1820 					decrypted_key_data_size, &oci_len);
   1821 
   1822 	if (hs->akm_suite &
   1823 			(IE_RSN_AKM_SUITE_FT_OVER_8021X |
   1824 			 IE_RSN_AKM_SUITE_FT_USING_PSK |
   1825 			 IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256)) {
   1826 		if (rsn_info.num_pmkids != 1 || memcmp(rsn_info.pmkids,
   1827 						hs->pmk_r1_name, 16))
   1828 			goto error_ie_different;
   1829 
   1830 		if (eapol_ie_matches(decrypted_key_data,
   1831 					decrypted_key_data_size,
   1832 					IE_TYPE_MOBILITY_DOMAIN,
   1833 					hs->mde) < 0)
   1834 			goto error_ie_different;
   1835 
   1836 		if (eapol_ie_matches(decrypted_key_data,
   1837 					decrypted_key_data_size,
   1838 					IE_TYPE_FAST_BSS_TRANSITION,
   1839 					hs->authenticator_fte) < 0)
   1840 			goto error_ie_different;
   1841 	}
   1842 
   1843 	/*
   1844 	 * 802.11-2020, Section 12.7.6.4:
   1845 	 * "If the RSNXE is present, the Supplicant verifies that the RSNXE is
   1846 	 * identical to that the STA received in the Beacon or Probe Response
   1847 	 * frame."
   1848 	 *
   1849 	 * Verify only if RSN is used
   1850 	 */
   1851 	if (!hs->osen_ie && !hs->wpa_ie &&
   1852 			eapol_ie_matches(decrypted_key_data,
   1853 					decrypted_key_data_size,
   1854 					IE_TYPE_RSNX,
   1855 					hs->authenticator_rsnxe) < 0)
   1856 		goto error_ie_different;
   1857 
   1858 	/*
   1859 	 * 802.11-2020, Section 12.7.6.4
   1860 	 * If dot11RSNAOperatingChannelValidationActivated is true and
   1861 	 * Authenticator RSNE indicates OCVC capability, the Supplicant
   1862 	 * silently discards message 3 if any of the following are true:
   1863 	 *  - OCI KDE or FTE OCI subelement is missing in the message
   1864 	 *  - Channel information in the OCI does not match current operating
   1865 	 *    channel parameters (see 12.2.9)
   1866 	 */
   1867 	if (hs->authenticator_ocvc &&
   1868 			handshake_state_verify_oci(hs, oci, oci_len) < 0)
   1869 		return;
   1870 
   1871 	/*
   1872 	 * If ptk_complete is set, then we are receiving Message 3 again.
   1873 	 * It must be a retransmission, otherwise the anonce wouldn't match
   1874 	 * and we wouldn't get here.  Skip processing the rest of the message
   1875 	 * and send our reply.  Do not install the keys again.
   1876 	 */
   1877 	if (hs->ptk_complete)
   1878 		goto retransmit;
   1879 
   1880 	/*
   1881 	 * 11.6.6.4: "If a second RSNE is provided in the message, the
   1882 	 * Supplicant uses the pairwise cipher suite specified in the second
   1883 	 * RSNE or deauthenticates."
   1884 	 */
   1885 	if (optional_rsne) {
   1886 		struct ie_rsn_info info2;
   1887 		uint16_t override;
   1888 
   1889 		if (ie_parse_rsne_from_data(optional_rsne, optional_rsne[1] + 2,
   1890 						&info2) < 0)
   1891 			goto error_ie_different;
   1892 
   1893 		/*
   1894 		 * 11.6.2:
   1895 		 * It may happen, for example, that a Supplicant selects a
   1896 		 * pairwise cipher suite which is advertised by an AP, but
   1897 		 * which policy disallows for this particular STA. An
   1898 		 * Authenticator may, therefore, insert a second RSNE to
   1899 		 * overrule the STA's selection. An Authenticator's SME shall
   1900 		 * insert the second RSNE, after the first RSNE, only for this
   1901 		 * purpose. The pairwise cipher suite in the second RSNE
   1902 		 * included shall be one of the ciphers advertised by the
   1903 		 * Authenticator. All other fields in the second RSNE shall be
   1904 		 * identical to the first RSNE.
   1905 		 *
   1906 		 * - Check that akm_suites and group_cipher are the same
   1907 		 *   between rsne1 and rsne2
   1908 		 * - Check that pairwise_ciphers is not the same between rsne1
   1909 		 *   and rsne2
   1910 		 * - Check that rsne2 pairwise_ciphers is a subset of rsne
   1911 		 */
   1912 		if (rsn_info.akm_suites != info2.akm_suites ||
   1913 				rsn_info.group_cipher != info2.group_cipher)
   1914 			goto error_ie_different;
   1915 
   1916 		override = info2.pairwise_ciphers;
   1917 
   1918 		if (override == rsn_info.pairwise_ciphers ||
   1919 				!(rsn_info.pairwise_ciphers & override) ||
   1920 				__builtin_popcount(override) != 1) {
   1921 			handshake_failed(sm,
   1922 				MMPDU_REASON_CODE_INVALID_PAIRWISE_CIPHER);
   1923 			return;
   1924 		}
   1925 
   1926 		handshake_state_override_pairwise_cipher(hs, override);
   1927 	}
   1928 
   1929 	if (!hs->wpa_ie && hs->group_cipher !=
   1930 				IE_RSN_CIPHER_SUITE_NO_GROUP_TRAFFIC) {
   1931 		gtk = handshake_util_find_gtk_kde(decrypted_key_data,
   1932 							decrypted_key_data_size,
   1933 							&gtk_len);
   1934 		if (!gtk) {
   1935 			handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   1936 			return;
   1937 		}
   1938 
   1939 		/* TODO: Handle tx bit */
   1940 
   1941 		gtk_key_index = bit_field(gtk[0], 0, 2);
   1942 		gtk += 2;
   1943 		gtk_len -= 2;
   1944 	}
   1945 
   1946 	if (hs->mfp) {
   1947 		igtk = handshake_util_find_igtk_kde(decrypted_key_data,
   1948 							decrypted_key_data_size,
   1949 							&igtk_len);
   1950 		if (!igtk) {
   1951 			handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   1952 			return;
   1953 		}
   1954 
   1955 		igtk_key_index = l_get_le16(igtk);
   1956 		igtk += 2;
   1957 		igtk_len -= 2;
   1958 	}
   1959 
   1960 	key_id = handshake_util_find_kde(HANDSHAKE_KDE_KEY_ID,
   1961 					decrypted_key_data,
   1962 					decrypted_key_data_size, &key_id_len);
   1963 	if (hs->ext_key_id_capable) {
   1964 		uint8_t idx;
   1965 
   1966 		if (!key_id) {
   1967 			l_debug("No extended key KDE in frame 3/4");
   1968 			handshake_failed(sm, MMPDU_REASON_CODE_INVALID_IE);
   1969 			return;
   1970 		}
   1971 
   1972 		if (key_id_len != 2) {
   1973 			l_error("invalid Key ID KDE format");
   1974 			handshake_failed(sm, MMPDU_REASON_CODE_INVALID_IE);
   1975 			return;
   1976 		}
   1977 
   1978 		idx = bit_field(key_id[0], 0, 2);
   1979 
   1980 		/*
   1981 		 * IEEE 802.11-2020 - 12.7.6.4 4-way handshake message 3
   1982 		 * "... the Authenticator assigns a new Key ID for the PTKSA in
   1983 		 * the range of 0 to 1 that is different from the Key ID
   1984 		 * assigned in the previous handshake"
   1985 		 */
   1986 		if ((idx != 0 && idx != 1) || (sm->rekey &&
   1987 						idx == hs->active_tk_index)) {
   1988 			l_error("invalid Key ID KDE value (%u)", idx);
   1989 			handshake_failed(sm, MMPDU_REASON_CODE_INVALID_IE);
   1990 			return;
   1991 		}
   1992 
   1993 		hs->active_tk_index = idx;
   1994 
   1995 		l_debug("using Extended key ID %u", hs->active_tk_index);
   1996 	}
   1997 
   1998 	if (hs->support_ip_allocation) {
   1999 		size_t len;
   2000 		const uint8_t *ip_alloc_kde =
   2001 			handshake_util_find_kde(HANDSHAKE_KDE_IP_ADDRESS_ALLOC,
   2002 						decrypted_key_data,
   2003 						decrypted_key_data_size,
   2004 						&len);
   2005 
   2006 		if (ip_alloc_kde && (len < 12 ||
   2007 				!eapol_check_ip_mask(ip_alloc_kde + 4,
   2008 							ip_alloc_kde,
   2009 							ip_alloc_kde + 8))) {
   2010 			l_debug("Invalid IP Allocation KDE in frame 3/4");
   2011 			handshake_failed(sm, MMPDU_REASON_CODE_INVALID_IE);
   2012 			return;
   2013 		}
   2014 
   2015 		hs->support_ip_allocation = ip_alloc_kde != NULL;
   2016 
   2017 		if (ip_alloc_kde) {
   2018 			hs->client_ip_addr = l_get_u32(ip_alloc_kde);
   2019 			hs->subnet_mask = l_get_u32(ip_alloc_kde + 4);
   2020 			hs->go_ip_addr = l_get_u32(ip_alloc_kde + 8);
   2021 		} else
   2022 			l_debug("Authenticator ignored our IP Address Request");
   2023 	}
   2024 
   2025 	transition_disable =
   2026 		handshake_util_find_kde(HANDSHAKE_KDE_TRANSITION_DISABLE,
   2027 					decrypted_key_data,
   2028 					decrypted_key_data_size,
   2029 					&transition_disable_len);
   2030 	if (transition_disable)
   2031 		handshake_event(hs, HANDSHAKE_EVENT_TRANSITION_DISABLE,
   2032 				transition_disable, transition_disable_len);
   2033 
   2034 retransmit:
   2035 	/*
   2036 	 * 802.11-2016, Section 12.7.6.4:
   2037 	 * "b) Verifies the message 3 MIC. If the calculated MIC does not match
   2038 	 * the MIC that the Authenticator included in the EAPOL-Key frame, the
   2039 	 * Supplicant silently discards message 3."
   2040 	 * "c) Updates the last-seen value of the Key Replay Counter field."
   2041 	 *
   2042 	 * Note that part b was done in eapol_key_handle
   2043 	 */
   2044 	sm->replay_counter = L_BE64_TO_CPU(ek->key_replay_counter);
   2045 	sm->have_replay = true;
   2046 
   2047 	step4 = eapol_create_ptk_4_of_4(sm->protocol_version,
   2048 					ek->key_descriptor_version,
   2049 					sm->replay_counter,
   2050 					hs->wpa_ie, sm->mic_len);
   2051 
   2052 	kck = handshake_state_get_kck(hs);
   2053 	kek = handshake_state_get_kek(hs);
   2054 
   2055 	if (sm->mic_len) {
   2056 		if (!eapol_calculate_mic(hs->akm_suite, kck,
   2057 						step4, mic, sm->mic_len)) {
   2058 			l_debug("MIC Calculation failed");
   2059 			handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   2060 			return;
   2061 		}
   2062 
   2063 		memcpy(EAPOL_KEY_MIC(step4), mic, sm->mic_len);
   2064 	} else {
   2065 		if (!eapol_aes_siv_encrypt(handshake_state_get_kek(hs),
   2066 						handshake_state_get_kek_len(hs),
   2067 						step4, NULL, 0)) {
   2068 			l_debug("AES-SIV encryption failed");
   2069 			handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   2070 			return;
   2071 		}
   2072 	}
   2073 
   2074 	/*
   2075 	 * For WPA1 the group handshake should be happening after we set the
   2076 	 * ptk, this flag tells netdev to wait for the gtk/igtk before
   2077 	 * completing the connection.
   2078 	 */
   2079 	if (!gtk && hs->group_cipher != IE_RSN_CIPHER_SUITE_NO_GROUP_TRAFFIC)
   2080 		hs->wait_for_gtk = true;
   2081 
   2082 	if (gtk)
   2083 		eapol_install_gtk(sm, gtk_key_index, gtk, gtk_len, ek->key_rsc);
   2084 
   2085 	if (igtk)
   2086 		eapol_install_igtk(sm, igtk_key_index, igtk, igtk_len);
   2087 
   2088 	/*
   2089 	 * Only install if this is the first 3/4 message (not retransmitting)
   2090 	 * and a rekey. Initial associations don't need the special RX -> TX
   2091 	 * procedure and can install the TK normally
   2092 	 */
   2093 	if (key_id && hs->ext_key_id_capable && sm->rekey) {
   2094 		handshake_state_install_ext_ptk(hs, hs->active_tk_index,
   2095 						(struct eapol_frame *) step4,
   2096 						ETH_P_PAE, unencrypted);
   2097 
   2098 		return;
   2099 	}
   2100 
   2101 	eapol_sm_write(sm, (struct eapol_frame *) step4, unencrypted);
   2102 
   2103 	if (hs->ptk_complete)
   2104 		return;
   2105 
   2106 	handshake_state_install_ptk(hs);
   2107 
   2108 	if (rekey_offload)
   2109 		rekey_offload(hs->ifindex, kek, kck,
   2110 				sm->replay_counter, sm->user_data);
   2111 
   2112 	l_timeout_remove(sm->timeout);
   2113 	sm->timeout = NULL;
   2114 
   2115 	return;
   2116 
   2117 error_ie_different:
   2118 	handshake_failed(sm, MMPDU_REASON_CODE_IE_DIFFERENT);
   2119 }
   2120 
   2121 /* 802.11-2016 Section 12.7.6.5 */
   2122 static void eapol_handle_ptk_4_of_4(struct eapol_sm *sm,
   2123 					const struct eapol_key *ek)
   2124 {
   2125 	const uint8_t *kck;
   2126 
   2127 	l_debug("ifindex=%u", sm->handshake->ifindex);
   2128 
   2129 	if (!eapol_verify_ptk_4_of_4(ek, false))
   2130 		return;
   2131 
   2132 	if (L_BE64_TO_CPU(ek->key_replay_counter) != sm->replay_counter)
   2133 		return;
   2134 
   2135 	/* Ensure we received Message 2 and thus have a PTK to verify MIC */
   2136 	if (!sm->handshake->have_snonce)
   2137 		return;
   2138 
   2139 	kck = handshake_state_get_kck(sm->handshake);
   2140 
   2141 	if (!eapol_verify_mic(sm->handshake->akm_suite, kck, ek,
   2142 				sm->mic_len))
   2143 		return;
   2144 
   2145 	l_timeout_remove(sm->timeout);
   2146 	sm->timeout = NULL;
   2147 
   2148 	/*
   2149 	 * If ptk_complete is set, then we are receiving Message 4 again.
   2150 	 * This might be a retransmission, so accept but don't install
   2151 	 * the keys again.
   2152 	 */
   2153 	if (!sm->handshake->ptk_complete)
   2154 		handshake_state_install_ptk(sm->handshake);
   2155 
   2156 	sm->handshake->ptk_complete = true;
   2157 }
   2158 
   2159 static void eapol_handle_gtk_1_of_2(struct eapol_sm *sm,
   2160 					const struct eapol_key *ek,
   2161 					const uint8_t *decrypted_key_data,
   2162 					size_t decrypted_key_data_size,
   2163 					bool unencrypted)
   2164 {
   2165 	struct handshake_state *hs = sm->handshake;
   2166 	const uint8_t *kck;
   2167 	struct eapol_key *step2;
   2168 	uint8_t mic[MIC_MAXLEN];
   2169 	const uint8_t *gtk;
   2170 	size_t gtk_len;
   2171 	uint8_t gtk_key_index;
   2172 	const uint8_t *igtk;
   2173 	size_t igtk_len;
   2174 	uint16_t igtk_key_index;
   2175 	const uint8_t *oci;
   2176 	size_t oci_len;
   2177 	uint8_t ies[1024];
   2178 	size_t ies_len = 0;
   2179 
   2180 	l_debug("ifindex=%u", hs->ifindex);
   2181 
   2182 	if (!eapol_verify_gtk_1_of_2(ek, hs->wpa_ie, sm->mic_len))
   2183 		return;
   2184 
   2185 	oci = handshake_util_find_kde(HANDSHAKE_KDE_OCI, decrypted_key_data,
   2186 					decrypted_key_data_size, &oci_len);
   2187 
   2188 	/*
   2189 	 * 802.11-2020, Section 12.7.2.2
   2190 	 * If dot11RSNAOperatingChannelValidationActivated is true and
   2191 	 * Authenticator RSNE indicates OCVC capability, the Supplicant
   2192 	 * silently discards message 1 if any of the following are true:
   2193 	 *   - OCI KDE is missing in the message
   2194 	 *   - Channel information in the OCI KDE does not match current
   2195 	 *   operating channel parameters (see 12.2.9)
   2196 	 */
   2197 	if (hs->authenticator_ocvc &&
   2198 			handshake_state_verify_oci(hs, oci, oci_len) < 0)
   2199 		return;
   2200 
   2201 	if (!hs->wpa_ie) {
   2202 		gtk = handshake_util_find_gtk_kde(decrypted_key_data,
   2203 							decrypted_key_data_size,
   2204 							&gtk_len);
   2205 		if (!gtk)
   2206 			return;
   2207 
   2208 		gtk_key_index = bit_field(gtk[0], 0, 2);
   2209 		gtk += 2;
   2210 		gtk_len -= 2;
   2211 	} else {
   2212 		gtk = decrypted_key_data;
   2213 		gtk_len = decrypted_key_data_size;
   2214 
   2215 		if (!gtk || gtk_len < CRYPTO_MIN_GTK_LEN ||
   2216 						gtk_len > CRYPTO_MAX_GTK_LEN)
   2217 			return;
   2218 
   2219 		gtk_key_index = ek->wpa_key_id;
   2220 	}
   2221 
   2222 	if (hs->mfp) {
   2223 		igtk = handshake_util_find_igtk_kde(decrypted_key_data,
   2224 							decrypted_key_data_size,
   2225 							&igtk_len);
   2226 		if (!igtk)
   2227 			return;
   2228 
   2229 		igtk_key_index = l_get_le16(igtk);
   2230 		igtk += 2;
   2231 		igtk_len -= 2;
   2232 	} else
   2233 		igtk = NULL;
   2234 
   2235 	/*
   2236 	 * IEEE 802.11-2020 Section 12.7.7.3
   2237 	 * "Key Data = OCI KDE when dot11RSNAOperatingChannelValidationActivated
   2238 	 *  on the [Supplicant]"
   2239 	 *
   2240 	 * Note: The spec reads "Authenticator" but this is incorrect and
   2241 	 * appears to be a copy-paste from a previous section. Above it has been
   2242 	 * changed to Supplicant.
   2243 	 */
   2244 	if (sm->handshake->supplicant_ocvc && sm->handshake->chandef)
   2245 		ies_len += append_oci(ies + ies_len, sm->handshake->chandef);
   2246 
   2247 	/*
   2248 	 * 802.11-2016, Section 12.7.7.2:
   2249 	 * "
   2250 	 * a) Verifies that the Key Replay Counter field value has not yet been
   2251 	 * seen before, i.e., its value is strictly larger than that in any
   2252 	 * other EAPOL-Key frame received thus far during this session.
   2253 	 * b) Verifies that the MIC is valid, i.e., it uses the KCK that is
   2254 	 * part of the PTK to verify that there is no data integrity error.
   2255 	 * c) Uses the MLME-SETKEYS.request primitive to configure the temporal
   2256 	 * GTK and, when present, IGTK into its IEEE 802.11 MAC.
   2257 	 * d) Responds by creating and sending message 2 of the group key
   2258 	 * handshake to the Authenticator and incrementing the replay counter.
   2259 	 * "
   2260 	 * Note: steps a & b are performed in eapol_key_handle
   2261 	 */
   2262 	sm->replay_counter = L_BE64_TO_CPU(ek->key_replay_counter);
   2263 	sm->have_replay = true;
   2264 
   2265 	step2 = eapol_create_gtk_2_of_2(sm->protocol_version,
   2266 					ek->key_descriptor_version,
   2267 					sm->replay_counter,
   2268 					ies_len, ies,
   2269 					hs->wpa_ie, ek->wpa_key_id,
   2270 					sm->mic_len);
   2271 
   2272 	kck = handshake_state_get_kck(hs);
   2273 
   2274 	if (sm->mic_len) {
   2275 		if (!eapol_calculate_mic(hs->akm_suite, kck,
   2276 						step2, mic, sm->mic_len)) {
   2277 			l_debug("MIC calculation failed");
   2278 			l_free(step2);
   2279 			handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   2280 			return;
   2281 		}
   2282 
   2283 		memcpy(EAPOL_KEY_MIC(step2), mic, sm->mic_len);
   2284 	} else {
   2285 		if (!eapol_aes_siv_encrypt(handshake_state_get_kek(hs),
   2286 						handshake_state_get_kek_len(hs),
   2287 						step2, NULL, 0)) {
   2288 			l_debug("AES-SIV encryption failed");
   2289 			l_free(step2);
   2290 			handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   2291 			return;
   2292 		}
   2293 	}
   2294 
   2295 	eapol_sm_write(sm, (struct eapol_frame *) step2, unencrypted);
   2296 	l_free(step2);
   2297 
   2298 	eapol_install_gtk(sm, gtk_key_index, gtk, gtk_len, ek->key_rsc);
   2299 
   2300 	if (igtk)
   2301 		eapol_install_igtk(sm, igtk_key_index, igtk, igtk_len);
   2302 }
   2303 
   2304 static struct eapol_sm *eapol_find_sm(uint32_t ifindex, const uint8_t *aa)
   2305 {
   2306 	const struct l_queue_entry *entry;
   2307 	struct eapol_sm *sm;
   2308 
   2309 	for (entry = l_queue_get_entries(state_machines); entry;
   2310 					entry = entry->next) {
   2311 		sm = entry->data;
   2312 
   2313 		if (sm->handshake->ifindex != ifindex)
   2314 			continue;
   2315 
   2316 		if (memcmp(sm->handshake->aa, aa, ETH_ALEN))
   2317 			continue;
   2318 
   2319 		return sm;
   2320 	}
   2321 
   2322 	return NULL;
   2323 }
   2324 
   2325 static void eapol_key_handle(struct eapol_sm *sm,
   2326 				const struct eapol_frame *frame,
   2327 				bool unencrypted)
   2328 {
   2329 	struct handshake_state *hs = sm->handshake;
   2330 	const struct eapol_key *ek;
   2331 	const uint8_t *kck;
   2332 	const uint8_t *kek;
   2333 	uint8_t *decrypted_key_data = NULL;
   2334 	size_t key_data_len = 0;
   2335 	uint64_t replay_counter;
   2336 	uint8_t expected_key_descriptor_version;
   2337 
   2338 	ek = eapol_key_validate((const uint8_t *) frame,
   2339 				sizeof(struct eapol_header) +
   2340 				L_BE16_TO_CPU(frame->header.packet_len),
   2341 				sm->mic_len);
   2342 	if (!ek)
   2343 		return;
   2344 
   2345 	/* Wrong direction */
   2346 	if (!ek->key_ack)
   2347 		return;
   2348 
   2349 	if (L_WARN_ON(eapol_key_descriptor_version_from_akm(hs->akm_suite,
   2350 				hs->pairwise_cipher,
   2351 				&expected_key_descriptor_version) < 0))
   2352 		return;
   2353 
   2354 	if (L_WARN_ON(expected_key_descriptor_version !=
   2355 				ek->key_descriptor_version))
   2356 		return;
   2357 
   2358 	/* Further Descriptor Type check */
   2359 	if (!hs->wpa_ie && ek->descriptor_type != EAPOL_DESCRIPTOR_TYPE_80211)
   2360 		return;
   2361 	else if (hs->wpa_ie &&
   2362 			ek->descriptor_type != EAPOL_DESCRIPTOR_TYPE_WPA)
   2363 		return;
   2364 
   2365 	replay_counter = L_BE64_TO_CPU(ek->key_replay_counter);
   2366 
   2367 	/*
   2368 	 * 802.11-2016, Section 12.7.2:
   2369 	 * "The Supplicant and Authenticator shall track the key replay counter
   2370 	 * per security association. The key replay counter shall be
   2371 	 * initialized to 0 on (re)association. The Authenticator shall
   2372 	 * increment the key replay counter on each successive EAPOL-Key frame."
   2373 	 *
   2374 	 * and
   2375 	 *
   2376 	 * "The Supplicant should also use the key replay counter and ignore
   2377 	 * EAPOL-Key frames with a Key Replay Counter field value smaller than
   2378 	 * or equal to any received in a valid message. The local Key Replay
   2379 	 * Counter field should not be updated until after the EAPOL-Key MIC is
   2380 	 * checked and is found to be valid. In other words, the Supplicant
   2381 	 * never updates the Key Replay Counter field for message 1 in the
   2382 	 * 4-way handshake, as it includes no MIC. This implies the Supplicant
   2383 	 * needs to allow for retransmission of message 1 when checking for
   2384 	 * the key replay counter of message 3."
   2385 	 *
   2386 	 * Note: The latter condition implies that Message 1 and Message 3
   2387 	 * can have the same replay counter, though other parts of the spec
   2388 	 * mandate that the Authenticator has to increment the replay counter
   2389 	 * for each frame sent.  Contradictory.
   2390 	 */
   2391 	if (sm->have_replay && sm->replay_counter >= replay_counter)
   2392 		return;
   2393 
   2394 	kck = handshake_state_get_kck(hs);
   2395 
   2396 	if (ek->key_mic) {
   2397 		/* Haven't received step 1 yet, so no ptk */
   2398 		if (!hs->have_snonce)
   2399 			return;
   2400 
   2401 		if (!eapol_verify_mic(hs->akm_suite, kck, ek, sm->mic_len))
   2402 			return;
   2403 	}
   2404 
   2405 	if ((ek->encrypted_key_data && !hs->wpa_ie) ||
   2406 			(ek->key_type == 0 && hs->wpa_ie)) {
   2407 		/*
   2408 		 * If using a MIC (non-FILS) but haven't received step 1 yet
   2409 		 * we disregard since there will be no ptk
   2410 		 */
   2411 		if (sm->mic_len && !hs->have_snonce)
   2412 			return;
   2413 
   2414 		kek = handshake_state_get_kek(hs);
   2415 
   2416 		decrypted_key_data = eapol_decrypt_key_data(
   2417 					hs->akm_suite, kek,
   2418 					ek, &key_data_len, sm->mic_len);
   2419 		if (!decrypted_key_data)
   2420 			return;
   2421 	} else
   2422 		key_data_len = EAPOL_KEY_DATA_LEN(ek, sm->mic_len);
   2423 
   2424 	if (ek->key_type == 0) {
   2425 		/* GTK handshake allowed only after PTK handshake complete */
   2426 		if (!hs->ptk_complete)
   2427 			goto done;
   2428 
   2429 		if (hs->group_cipher == IE_RSN_CIPHER_SUITE_NO_GROUP_TRAFFIC)
   2430 			goto done;
   2431 
   2432 		if (!decrypted_key_data)
   2433 			goto done;
   2434 
   2435 		eapol_handle_gtk_1_of_2(sm, ek, decrypted_key_data,
   2436 					key_data_len, unencrypted);
   2437 		goto done;
   2438 	}
   2439 
   2440 	/* If no MIC, then assume packet 1, otherwise packet 3 */
   2441 	if (!ek->key_mic && !ek->encrypted_key_data)
   2442 		eapol_handle_ptk_1_of_4(sm, ek, unencrypted);
   2443 	else {
   2444 		if (!key_data_len)
   2445 			goto done;
   2446 
   2447 		eapol_handle_ptk_3_of_4(sm, ek,
   2448 					decrypted_key_data ?:
   2449 					EAPOL_KEY_DATA(ek, sm->mic_len),
   2450 					key_data_len, unencrypted);
   2451 	}
   2452 
   2453 done:
   2454 	if (decrypted_key_data)
   2455 		explicit_bzero(decrypted_key_data, key_data_len);
   2456 
   2457 	l_free(decrypted_key_data);
   2458 }
   2459 
   2460 /* This respresentes the eapMsg message in 802.1X Figure 8-1 */
   2461 static void eapol_eap_msg_cb(const uint8_t *eap_data, size_t len,
   2462 					void *user_data)
   2463 {
   2464 	struct eapol_sm *sm = user_data;
   2465 	uint8_t buf[sizeof(struct eapol_frame) + len];
   2466 	struct eapol_frame *frame = (struct eapol_frame *) buf;
   2467 
   2468 	frame->header.protocol_version = sm->protocol_version;
   2469 	frame->header.packet_type = 0;
   2470 	l_put_be16(len, &frame->header.packet_len);
   2471 
   2472 	memcpy(frame->data, eap_data, len);
   2473 
   2474 	eapol_sm_write(sm, frame, sm->last_eap_unencrypted);
   2475 }
   2476 
   2477 /* This respresentes the eapTimout, eapFail and eapSuccess messages */
   2478 static void eapol_eap_complete_cb(enum eap_result result, void *user_data)
   2479 {
   2480 	struct eapol_sm *sm = user_data;
   2481 
   2482 	l_info("EAP completed with %s", result == EAP_RESULT_SUCCESS ?
   2483 			"eapSuccess" : (result == EAP_RESULT_FAIL ?
   2484 				"eapFail" : "eapTimeout"));
   2485 
   2486 	if (result != EAP_RESULT_SUCCESS) {
   2487 		eap_free(sm->eap);
   2488 		sm->eap = NULL;
   2489 		handshake_failed(sm, MMPDU_REASON_CODE_IEEE8021X_FAILED);
   2490 		return;
   2491 	}
   2492 
   2493 	if (install_pmk)
   2494 		install_pmk(sm->handshake, sm->handshake->pmk,
   2495 				sm->handshake->pmk_len);
   2496 
   2497 	eap_reset(sm->eap);
   2498 
   2499 	if (sm->handshake->authenticator) {
   2500 		if (L_WARN_ON(!sm->handshake->have_pmk)) {
   2501 			handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED);
   2502 			return;
   2503 		}
   2504 
   2505 		/* sm->mic_len will have been set in eapol_eap_results_cb */
   2506 
   2507 		sm->frame_retry = 0;
   2508 
   2509 		/* Kick off 4-Way Handshake */
   2510 		eapol_ptk_1_of_4_retry(NULL, sm);
   2511 	}
   2512 }
   2513 
   2514 /* This respresentes the eapResults message */
   2515 static void eapol_eap_results_cb(const uint8_t *msk_data, size_t msk_len,
   2516 				const uint8_t *emsk_data, size_t emsk_len,
   2517 				const uint8_t *iv, size_t iv_len,
   2518 				const uint8_t *session_id, size_t session_len,
   2519 				void *user_data)
   2520 {
   2521 	struct eapol_sm *sm = user_data;
   2522 
   2523 	l_debug("EAP key material received");
   2524 
   2525 	/*
   2526 	 * 802.11i 8.5.1.2:
   2527 	 *    "When not using a PSK, the PMK is derived from the AAA key.
   2528 	 *    The PMK shall be computed as the first 256 bits (bits 0-255)
   2529 	 *    of the AAA key: PMK = L(PTK, 0, 256)."
   2530 	 * 802.11-2016 12.7.1.3:
   2531 	 *    "When not using a PSK, the PMK is derived from the MSK.
   2532 	 *    The PMK shall be computed as the first PMK_bits bits
   2533 	 *    (bits 0 to PMK_bits-1) of the MSK: PMK = L(MSK, 0, PMK_bits)."
   2534 	 * RFC5247 explains AAA-Key refers to the MSK and confirms the
   2535 	 * first 32 bytes of the MSK are used.  MSK is at least 64 octets
   2536 	 * long per RFC3748.  Note WEP derives the PTK from MSK differently.
   2537 	 *
   2538 	 * In a Fast Transition initial mobility domain association the PMK
   2539 	 * maps to the XXKey, except with EAP:
   2540 	 * 802.11-2016 12.7.1.7.3:
   2541 	 *    "If the AKM negotiated is 00-0F-AC:3, then [...] XXKey shall be
   2542 	 *    the second 256 bits of the MSK (which is derived from the IEEE
   2543 	 *    802.1X authentication), i.e., XXKey = L(MSK, 256, 256)."
   2544 	 * So we need to save the first 64 bytes at minimum.
   2545 	 */
   2546 
   2547 	if (sm->handshake->akm_suite == IE_RSN_AKM_SUITE_FT_OVER_8021X) {
   2548 		if (msk_len < 64)
   2549 			goto msk_short;
   2550 	} else {
   2551 		if (msk_len < 32)
   2552 			goto msk_short;
   2553 	}
   2554 
   2555 	if (msk_len > sizeof(sm->handshake->pmk))
   2556 		msk_len = sizeof(sm->handshake->pmk);
   2557 
   2558 	sm->mic_len = eapol_get_mic_length(sm->handshake->akm_suite,
   2559 						sm->handshake->pmk_len);
   2560 
   2561 	switch (sm->handshake->akm_suite) {
   2562 	case IE_RSN_AKM_SUITE_FT_OVER_8021X:
   2563 		msk_len = 64;
   2564 		break;
   2565 	case IE_RSN_AKM_SUITE_8021X_SUITE_B_SHA384:
   2566 	case IE_RSN_AKM_SUITE_FT_OVER_8021X_SHA384:
   2567 		msk_len = 48;
   2568 		break;
   2569 	default:
   2570 		msk_len = 32;
   2571 		break;
   2572 	}
   2573 
   2574 	handshake_state_set_pmk(sm->handshake, msk_data, msk_len);
   2575 
   2576 	if (sm->handshake->support_fils && emsk_data && session_id)
   2577 		erp_cache_add(eap_get_identity(sm->eap), session_id,
   2578 				session_len, emsk_data, emsk_len,
   2579 				sm->handshake->ssid, sm->handshake->ssid_len);
   2580 
   2581 	return;
   2582 
   2583 msk_short:
   2584 	l_error("EAP method's MSK too short for AKM suite %u",
   2585 			sm->handshake->akm_suite);
   2586 
   2587 	handshake_failed(sm, MMPDU_REASON_CODE_IEEE8021X_FAILED);
   2588 }
   2589 
   2590 static void eapol_eap_event_cb(unsigned int event,
   2591 				const void *event_data, void *user_data)
   2592 {
   2593 	struct eapol_sm *sm = user_data;
   2594 
   2595 	handshake_event(sm->handshake, HANDSHAKE_EVENT_EAP_NOTIFY, event,
   2596 			event_data);
   2597 }
   2598 
   2599 void eapol_sm_set_use_eapol_start(struct eapol_sm *sm, bool enabled)
   2600 {
   2601 	sm->use_eapol_start = enabled;
   2602 }
   2603 
   2604 void eapol_sm_set_require_handshake(struct eapol_sm *sm, bool enabled)
   2605 {
   2606 	sm->require_handshake = enabled;
   2607 
   2608 	if (!sm->require_handshake)
   2609 		sm->use_eapol_start = false;
   2610 }
   2611 
   2612 static void eapol_auth_key_handle(struct eapol_sm *sm,
   2613 				const struct eapol_frame *frame)
   2614 {
   2615 	size_t frame_len = 4 + L_BE16_TO_CPU(frame->header.packet_len);
   2616 	const struct eapol_key *ek = eapol_key_validate((const void *) frame,
   2617 							frame_len, sm->mic_len);
   2618 	uint16_t key_data_len;
   2619 
   2620 	if (!ek)
   2621 		return;
   2622 
   2623 	/* Wrong direction */
   2624 	if (ek->key_ack)
   2625 		return;
   2626 
   2627 	if (ek->request)
   2628 		return; /* Not supported */
   2629 
   2630 	if (!sm->handshake->have_anonce)
   2631 		return; /* Not expecting an EAPoL-Key yet */
   2632 
   2633 	key_data_len = EAPOL_KEY_DATA_LEN(ek, sm->mic_len);
   2634 	if (key_data_len != 0)
   2635 		eapol_handle_ptk_2_of_4(sm, ek);
   2636 	else
   2637 		eapol_handle_ptk_4_of_4(sm, ek);
   2638 }
   2639 
   2640 static void eapol_rx_auth_packet(uint16_t proto, const uint8_t *from,
   2641 				const struct eapol_frame *frame,
   2642 				bool noencrypt,
   2643 				void *user_data)
   2644 {
   2645 	struct eapol_sm *sm = user_data;
   2646 
   2647 	if (proto != ETH_P_PAE || memcmp(from, sm->handshake->spa, 6))
   2648 		return;
   2649 
   2650 	if (sm->handshake->ptk_complete && noencrypt) {
   2651 		l_debug("Dropping unexpected unencrypted EAPoL frame");
   2652 		return;
   2653 	}
   2654 
   2655 	switch (frame->header.packet_type) {
   2656 	case 0:	/* EAPOL-EAP */
   2657 		if (!sm->eap) {
   2658 			l_error("Authenticator received an unexpected "
   2659 				"EAPOL-EAP frame from %s",
   2660 				util_address_to_string(from));
   2661 			return;
   2662 		}
   2663 
   2664 		eap_rx_packet(sm->eap, frame->data,
   2665 				L_BE16_TO_CPU(frame->header.packet_len));
   2666 		break;
   2667 
   2668 	case 1:	/* EAPOL-Start */
   2669 		/*
   2670 		 * The supplicant may have sent an EAPoL-Start even before
   2671 		 * we queued our EAP Identity Request or it may have missed our
   2672 		 * early Identity Request and may need a retransmission.  Tell
   2673 		 * sm->eap so it can decide whether to send a new Identity
   2674 		 * Request or ignore this.
   2675 		 *
   2676 		 * TODO: if we're already past the full handshake, send a
   2677 		 * new msg 1/4.
   2678 		 */
   2679 		if (sm->eap)
   2680 			eap_start(sm->eap);
   2681 
   2682 		break;
   2683 
   2684 	case 3: /* EAPOL-Key */
   2685 		eapol_auth_key_handle(sm, frame);
   2686 		break;
   2687 
   2688 	default:
   2689 		l_error("Authenticator received unknown packet type %i from %s",
   2690 			frame->header.packet_type,
   2691 			util_address_to_string(from));
   2692 		return;
   2693 	}
   2694 }
   2695 
   2696 static void eapol_rx_packet(uint16_t proto, const uint8_t *from,
   2697 				const struct eapol_frame *frame,
   2698 				bool unencrypted,
   2699 				void *user_data)
   2700 {
   2701 	struct eapol_sm *sm = user_data;
   2702 
   2703 	if (proto != ETH_P_PAE || memcmp(from, sm->handshake->aa, 6))
   2704 		return;
   2705 
   2706 	if (!sm->started) {
   2707 		size_t len = sizeof(struct eapol_header) +
   2708 			L_BE16_TO_CPU(frame->header.packet_len);
   2709 
   2710 		/*
   2711 		 * If the state machine hasn't started yet save the frame
   2712 		 * for processing later.
   2713 		 */
   2714 		if (sm->early_frame) /* Is the 1-element queue full */
   2715 			return;
   2716 
   2717 		sm->early_frame = l_memdup(frame, len);
   2718 		sm->early_frame_unencrypted = unencrypted;
   2719 
   2720 		return;
   2721 	}
   2722 
   2723 	if (!sm->protocol_version)
   2724 		sm->protocol_version = frame->header.protocol_version;
   2725 
   2726 	switch (frame->header.packet_type) {
   2727 	case 0: /* EAPOL-EAP */
   2728 		if (sm->handshake->ptk_complete && unencrypted) {
   2729 			l_debug("Dropping unexpected unencrypted EAP frame");
   2730 			return;
   2731 		}
   2732 
   2733 		l_timeout_remove(sm->eapol_start_timeout);
   2734 		sm->eapol_start_timeout = 0;
   2735 
   2736 		if (!sm->eap) {
   2737 			/* If we're not configured for EAP, send a NAK */
   2738 			sm->eap = eap_new(eapol_eap_msg_cb,
   2739 						eapol_eap_complete_cb, sm);
   2740 
   2741 			if (!sm->eap)
   2742 				return;
   2743 
   2744 			eap_set_key_material_func(sm->eap,
   2745 							eapol_eap_results_cb);
   2746 		}
   2747 
   2748 		sm->eap_exchanged = true;
   2749 		sm->last_eap_unencrypted = unencrypted;
   2750 
   2751 		eap_rx_packet(sm->eap, frame->data,
   2752 				L_BE16_TO_CPU(frame->header.packet_len));
   2753 
   2754 		break;
   2755 
   2756 	case 3: /* EAPOL-Key */
   2757 		if (!sm->handshake->have_pmk) {
   2758 			if (!sm->eap)
   2759 				return;
   2760 
   2761 			/*
   2762 			 * Either this is an error (EAP negotiation in
   2763 			 * progress) or the server is giving us a chance to
   2764 			 * use a cached PMK.  We don't yet cache PMKs so
   2765 			 * send an EAPOL-Start if we haven't sent one yet.
   2766 			 */
   2767 			if (sm->eapol_start_timeout) {
   2768 				l_timeout_remove(sm->eapol_start_timeout);
   2769 				sm->eapol_start_timeout = NULL;
   2770 				__send_eapol_start(sm, unencrypted);
   2771 			}
   2772 
   2773 			return;
   2774 		}
   2775 
   2776 		eapol_key_handle(sm, frame, unencrypted);
   2777 		break;
   2778 
   2779 	default:
   2780 		return;
   2781 	}
   2782 }
   2783 
   2784 void __eapol_update_replay_counter(uint32_t ifindex, const uint8_t *spa,
   2785 				const uint8_t *aa, uint64_t replay_counter)
   2786 {
   2787 	struct eapol_sm *sm;
   2788 
   2789 	sm = eapol_find_sm(ifindex, aa);
   2790 
   2791 	if (!sm)
   2792 		return;
   2793 
   2794 	if (sm->replay_counter >= replay_counter)
   2795 		return;
   2796 
   2797 	sm->replay_counter = replay_counter;
   2798 }
   2799 
   2800 void __eapol_set_tx_packet_func(eapol_tx_packet_func_t func)
   2801 {
   2802 	tx_packet = func;
   2803 }
   2804 
   2805 void __eapol_set_tx_user_data(void *user_data)
   2806 {
   2807 	tx_user_data = user_data;
   2808 }
   2809 
   2810 void __eapol_set_rekey_offload_func(eapol_rekey_offload_func_t func)
   2811 {
   2812 	rekey_offload = func;
   2813 }
   2814 
   2815 void __eapol_set_install_pmk_func(eapol_install_pmk_func_t func)
   2816 {
   2817 	install_pmk = func;
   2818 }
   2819 
   2820 void eapol_register(struct eapol_sm *sm)
   2821 {
   2822 	eapol_frame_watch_func_t rx_handler = sm->handshake->authenticator ?
   2823 		eapol_rx_auth_packet : eapol_rx_packet;
   2824 
   2825 	l_queue_push_head(state_machines, sm);
   2826 
   2827 	sm->watch_id = eapol_frame_watch_add(sm->handshake->ifindex,
   2828 						rx_handler, sm);
   2829 	sm->protocol_version = sm->handshake->proto_version;
   2830 }
   2831 
   2832 bool eapol_start(struct eapol_sm *sm)
   2833 {
   2834 	l_debug("");
   2835 
   2836 	if (sm->handshake->settings_8021x) {
   2837 		_auto_(l_free) char *network_id = NULL;
   2838 
   2839 		sm->eap = eap_new(eapol_eap_msg_cb, eapol_eap_complete_cb, sm);
   2840 
   2841 		if (!sm->eap)
   2842 			goto eap_error;
   2843 
   2844 		if (!eap_load_settings(sm->eap, sm->handshake->settings_8021x,
   2845 					"EAP-")) {
   2846 			eap_free(sm->eap);
   2847 			sm->eap = NULL;
   2848 
   2849 			goto eap_error;
   2850 		}
   2851 
   2852 		eap_set_key_material_func(sm->eap, eapol_eap_results_cb);
   2853 		eap_set_event_func(sm->eap, eapol_eap_event_cb);
   2854 
   2855 		network_id = l_util_hexstring(sm->handshake->ssid,
   2856 						sm->handshake->ssid_len);
   2857 		eap_set_peer_id(sm->eap, network_id);
   2858 	}
   2859 
   2860 	handshake_event(sm->handshake, HANDSHAKE_EVENT_STARTED);
   2861 
   2862 	sm->started = true;
   2863 
   2864 	if (sm->require_handshake)
   2865 		sm->timeout = l_timeout_create(eapol_4way_handshake_time,
   2866 				eapol_timeout, sm, NULL);
   2867 
   2868 	if (!sm->handshake->authenticator && sm->use_eapol_start) {
   2869 		/*
   2870 		 * We start a short timeout, if EAP packets are not received
   2871 		 * from AP, then we send the EAPoL-Start
   2872 		 */
   2873 		sm->eapol_start_timeout =
   2874 				l_timeout_create(1, send_eapol_start, sm, NULL);
   2875 	}
   2876 
   2877 	sm->mic_len = eapol_get_mic_length(sm->handshake->akm_suite,
   2878 						sm->handshake->pmk_len);
   2879 
   2880 	/* Process any frames received early due to scheduling */
   2881 	if (sm->early_frame) {
   2882 		eapol_rx_packet(ETH_P_PAE, sm->handshake->aa,
   2883 				sm->early_frame, sm->early_frame_unencrypted,
   2884 				sm);
   2885 		l_free(sm->early_frame);
   2886 		sm->early_frame = NULL;
   2887 	}
   2888 
   2889 	if (sm->handshake->authenticator) {
   2890 		if (!sm->protocol_version)
   2891 			sm->protocol_version = EAPOL_PROTOCOL_VERSION_2004;
   2892 
   2893 		if (sm->handshake->settings_8021x) {
   2894 			/*
   2895 			 * If we're allowed to, send EAP Identity request
   2896 			 * immediately, otherwise wait for an EAPoL-Start.
   2897 			 */
   2898 			if (!sm->use_eapol_start)
   2899 				eap_start(sm->eap);
   2900 		} else {
   2901 			if (L_WARN_ON(!sm->handshake->have_pmk))
   2902 				return false;
   2903 
   2904 			sm->frame_retry = 0;
   2905 
   2906 			/* Kick off handshake */
   2907 			eapol_ptk_1_of_4_retry(NULL, sm);
   2908 		}
   2909 	}
   2910 
   2911 	return true;
   2912 
   2913 eap_error:
   2914 	l_error("Error initializing EAP for ifindex %i",
   2915 			(int) sm->handshake->ifindex);
   2916 
   2917 	return false;
   2918 }
   2919 
   2920 struct preauth_sm {
   2921 	uint32_t ifindex;
   2922 	uint8_t aa[6];
   2923 	uint8_t spa[6];
   2924 	struct eap_state *eap;
   2925 	uint8_t pmk[32];
   2926 	eapol_preauth_cb_t cb;
   2927 	eapol_preauth_destroy_func_t destroy;
   2928 	void *user_data;
   2929 	struct l_timeout *timeout;
   2930 	uint32_t watch_id;
   2931 	bool initial_rx:1;
   2932 };
   2933 
   2934 #define EAPOL_TIMEOUT_SEC 1
   2935 
   2936 static void preauth_sm_destroy(void *value)
   2937 {
   2938 	struct preauth_sm *sm = value;
   2939 
   2940 	if (sm->destroy)
   2941 		sm->destroy(sm->user_data);
   2942 
   2943 	eap_free(sm->eap);
   2944 	l_timeout_remove(sm->timeout);
   2945 	eapol_frame_watch_remove(sm->watch_id);
   2946 	l_free(sm);
   2947 }
   2948 
   2949 static void preauth_frame(struct preauth_sm *sm, uint8_t packet_type,
   2950 				const uint8_t *data, size_t data_len)
   2951 {
   2952 	uint8_t buf[sizeof(struct eapol_frame) + data_len];
   2953 	struct eapol_frame *frame = (struct eapol_frame *) buf;
   2954 
   2955 	frame->header.protocol_version = EAPOL_PROTOCOL_VERSION_2001;
   2956 	frame->header.packet_type = packet_type;
   2957 	l_put_be16(data_len, &frame->header.packet_len);
   2958 
   2959 	if (data_len)
   2960 		memcpy(frame->data, data, data_len);
   2961 
   2962 	__eapol_tx_packet(sm->ifindex, sm->aa, 0x88c7, frame, false);
   2963 }
   2964 
   2965 static void preauth_rx_packet(uint16_t proto, const uint8_t *from,
   2966 				const struct eapol_frame *frame,
   2967 				bool unencrypted,
   2968 				void *user_data)
   2969 {
   2970 	struct preauth_sm *sm = user_data;
   2971 
   2972 	if (proto != 0x88c7 || memcmp(from, sm->aa, 6))
   2973 		return;
   2974 
   2975 	/*
   2976 	 * We do not expect any pre-auth packets to be unencrypted
   2977 	 * since we're authenticating via the currently connected AP
   2978 	 * and pre-authentication implies we are already connected
   2979 	 * and the keys are set
   2980 	 */
   2981 	if (L_WARN_ON(unencrypted))
   2982 		return;
   2983 
   2984 	if (frame->header.packet_type != 0) /* EAPOL-EAP */
   2985 		return;
   2986 
   2987 	if (!sm->initial_rx) {
   2988 		sm->initial_rx = true;
   2989 
   2990 		/*
   2991 		 * Initial frame from authenticator received, it's alive
   2992 		 * so set a longer timeout.  The timeout is for the whole
   2993 		 * EAP exchange as we have no way to monitor the
   2994 		 * negotiation progress and keep rearming the timer each
   2995 		 * time progress is made.
   2996 		 */
   2997 		l_timeout_modify(sm->timeout, EAPOL_TIMEOUT_SEC * 3);
   2998 	}
   2999 
   3000 	eap_rx_packet(sm->eap, frame->data,
   3001 			L_BE16_TO_CPU(frame->header.packet_len));
   3002 }
   3003 
   3004 static void preauth_eap_msg_cb(const uint8_t *eap_data, size_t len,
   3005 				void *user_data)
   3006 {
   3007 	struct preauth_sm *sm = user_data;
   3008 
   3009 	preauth_frame(sm, 0, eap_data, len);
   3010 }
   3011 
   3012 static void preauth_eap_complete_cb(enum eap_result result, void *user_data)
   3013 {
   3014 	struct preauth_sm *sm = user_data;
   3015 
   3016 	l_info("Preauthentication completed with %s",
   3017 		result == EAP_RESULT_SUCCESS ? "eapSuccess" :
   3018 		(result == EAP_RESULT_FAIL ? "eapFail" : "eapTimeout"));
   3019 
   3020 	l_queue_remove(preauths, sm);
   3021 
   3022 	if (result == EAP_RESULT_SUCCESS)
   3023 		sm->cb(sm->pmk, sm->user_data);
   3024 	else
   3025 		sm->cb(NULL, sm->user_data);
   3026 
   3027 	preauth_sm_destroy(sm);
   3028 }
   3029 
   3030 /* See eapol_eap_results_cb for documentation */
   3031 static void preauth_eap_results_cb(const uint8_t *msk_data, size_t msk_len,
   3032 				const uint8_t *emsk_data, size_t emsk_len,
   3033 				const uint8_t *iv, size_t iv_len,
   3034 				const uint8_t *session_id, size_t session_len,
   3035 				void *user_data)
   3036 {
   3037 	struct preauth_sm *sm = user_data;
   3038 
   3039 	l_debug("Preauthentication EAP key material received");
   3040 
   3041 	if (msk_len < 32)
   3042 		goto msk_short;
   3043 
   3044 	memcpy(sm->pmk, msk_data, 32);
   3045 
   3046 	return;
   3047 
   3048 msk_short:
   3049 	l_error("Preauthentication MSK too short");
   3050 
   3051 	l_queue_remove(preauths, sm);
   3052 
   3053 	sm->cb(NULL, sm->user_data);
   3054 
   3055 	preauth_sm_destroy(sm);
   3056 }
   3057 
   3058 static void preauth_timeout(struct l_timeout *timeout, void *user_data)
   3059 {
   3060 	struct preauth_sm *sm = user_data;
   3061 
   3062 	l_error("Preauthentication timeout");
   3063 
   3064 	l_queue_remove(preauths, sm);
   3065 
   3066 	sm->cb(NULL, sm->user_data);
   3067 
   3068 	preauth_sm_destroy(sm);
   3069 }
   3070 
   3071 struct preauth_sm *eapol_preauth_start(const uint8_t *aa,
   3072 					const struct handshake_state *hs,
   3073 					eapol_preauth_cb_t cb, void *user_data,
   3074 					eapol_preauth_destroy_func_t destroy)
   3075 {
   3076 	struct preauth_sm *sm;
   3077 
   3078 	sm = l_new(struct preauth_sm, 1);
   3079 
   3080 	sm->ifindex = hs->ifindex;
   3081 	memcpy(sm->aa, aa, 6);
   3082 	memcpy(sm->spa, hs->spa, 6);
   3083 	sm->cb = cb;
   3084 	sm->destroy = destroy;
   3085 	sm->user_data = user_data;
   3086 
   3087 	sm->eap = eap_new(preauth_eap_msg_cb, preauth_eap_complete_cb, sm);
   3088 	if (!sm->eap)
   3089 		goto err_free_sm;
   3090 
   3091 	if (!eap_load_settings(sm->eap, hs->settings_8021x, "EAP-"))
   3092 		goto err_free_eap;
   3093 
   3094 	eap_set_key_material_func(sm->eap, preauth_eap_results_cb);
   3095 
   3096 	sm->timeout = l_timeout_create(EAPOL_TIMEOUT_SEC, preauth_timeout,
   3097 					sm, NULL);
   3098 
   3099 	sm->watch_id = eapol_frame_watch_add(sm->ifindex,
   3100 						preauth_rx_packet, sm);
   3101 
   3102 	l_queue_push_head(preauths, sm);
   3103 
   3104 	/* Send EAPOL-Start */
   3105 	preauth_frame(sm, 1, NULL, 0);
   3106 
   3107 	return sm;
   3108 
   3109 err_free_eap:
   3110 	eap_free(sm->eap);
   3111 err_free_sm:
   3112 	l_free(sm);
   3113 
   3114 	return NULL;
   3115 }
   3116 
   3117 static bool preauth_remove_by_ifindex(void *data, void *user_data)
   3118 {
   3119 	struct preauth_sm *sm = data;
   3120 
   3121 	if (sm->ifindex != L_PTR_TO_UINT(user_data))
   3122 		return false;
   3123 
   3124 	preauth_sm_destroy(sm);
   3125 
   3126 	return true;
   3127 }
   3128 
   3129 void eapol_preauth_cancel(uint32_t ifindex)
   3130 {
   3131 	l_queue_foreach_remove(preauths, preauth_remove_by_ifindex,
   3132 				L_UINT_TO_PTR(ifindex));
   3133 }
   3134 
   3135 static bool eapol_frame_watch_match_ifindex(const void *a, const void *b)
   3136 {
   3137 	struct eapol_frame_watch *efw =
   3138 		l_container_of(a, struct eapol_frame_watch, super);
   3139 
   3140 	return efw->ifindex == L_PTR_TO_UINT(b);
   3141 }
   3142 
   3143 void __eapol_rx_packet(uint32_t ifindex, const uint8_t *src, uint16_t proto,
   3144 					const uint8_t *frame, size_t len,
   3145 					bool noencrypt)
   3146 {
   3147 	const struct eapol_header *eh;
   3148 
   3149 	/* Validate Header */
   3150 	if (len < sizeof(struct eapol_header))
   3151 		return;
   3152 
   3153 	eh = (const struct eapol_header *) frame;
   3154 
   3155 	switch (eh->protocol_version) {
   3156 	case EAPOL_PROTOCOL_VERSION_2001:
   3157 	case EAPOL_PROTOCOL_VERSION_2004:
   3158 	case EAPOL_PROTOCOL_VERSION_2010:
   3159 		break;
   3160 	default:
   3161 		return;
   3162 	}
   3163 
   3164 	if (len < sizeof(struct eapol_header) + L_BE16_TO_CPU(eh->packet_len))
   3165 		return;
   3166 
   3167 	WATCHLIST_NOTIFY_MATCHES(&frame_watches,
   3168 					eapol_frame_watch_match_ifindex,
   3169 					L_UINT_TO_PTR(ifindex),
   3170 					eapol_frame_watch_func_t, proto, src,
   3171 					(const struct eapol_frame *) eh,
   3172 					noencrypt);
   3173 }
   3174 
   3175 void __eapol_tx_packet(uint32_t ifindex, const uint8_t *dst, uint16_t proto,
   3176 			const struct eapol_frame *frame, bool noencrypt)
   3177 {
   3178 	if (!tx_packet)
   3179 		return;
   3180 
   3181 	tx_packet(ifindex, dst, proto, frame, noencrypt, tx_user_data);
   3182 }
   3183 
   3184 void __eapol_set_config(struct l_settings *config)
   3185 {
   3186 	if (!l_settings_get_uint(config, "EAPoL",
   3187 			"MaxHandshakeTime", &eapol_4way_handshake_time))
   3188 		eapol_4way_handshake_time = 5;
   3189 }
   3190 
   3191 int eapol_init(void)
   3192 {
   3193 	state_machines = l_queue_new();
   3194 	preauths = l_queue_new();
   3195 	watchlist_init(&frame_watches, &eapol_frame_watch_ops);
   3196 
   3197 	return 0;
   3198 }
   3199 
   3200 void eapol_exit(void)
   3201 {
   3202 	if (!l_queue_isempty(state_machines))
   3203 		l_warn("stale eapol state machines found");
   3204 
   3205 	l_queue_destroy(state_machines, eapol_sm_destroy);
   3206 
   3207 	if (!l_queue_isempty(preauths))
   3208 		l_warn("stale preauth state machines found");
   3209 
   3210 	l_queue_destroy(preauths, preauth_sm_destroy);
   3211 
   3212 	watchlist_destroy(&frame_watches);
   3213 }
   3214 
   3215 IWD_MODULE(eapol, eapol_init, eapol_exit);