eiwd

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

ft.c (29387B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2017-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 #include <alloca.h>
     28 #include <ell/ell.h>
     29 
     30 #include "src/ie.h"
     31 #include "src/handshake.h"
     32 #include "src/crypto.h"
     33 #include "src/ft.h"
     34 #include "src/mpdu.h"
     35 #include "src/auth-proto.h"
     36 #include "src/band.h"
     37 #include "src/scan.h"
     38 #include "src/util.h"
     39 #include "src/netdev.h"
     40 #include "src/module.h"
     41 #include "src/offchannel.h"
     42 #include "src/wiphy.h"
     43 
     44 static const unsigned int FT_ONCHANNEL_TIME = 300u; /* ms */
     45 
     46 static ft_tx_frame_func_t tx_frame = NULL;
     47 static struct l_queue *info_list = NULL;
     48 
     49 struct ft_info {
     50 	uint32_t ifindex;
     51 	uint8_t spa[6];
     52 	uint8_t aa[6];
     53 	uint8_t snonce[32];
     54 	uint8_t mde[3];
     55 	uint8_t *fte;
     56 	uint8_t *authenticator_ie;
     57 	uint8_t prev_bssid[6];
     58 	uint32_t frequency;
     59 	uint32_t ds_frequency;
     60 	uint32_t offchannel_id;
     61 	/* Status of Authenticate/Action frame response, or error (< 0) */
     62 	int status;
     63 
     64 	struct l_timeout *timeout;
     65 	struct wiphy_radio_work_item work;
     66 
     67 	struct ie_ft_info ft_info;
     68 
     69 	bool onchannel : 1;
     70 };
     71 
     72 /*
     73  * Calculate the MIC field of the FTE and write it directly to that FTE,
     74  * assuming it was all zeros before.  See 12.8.4 and 12.8.5.
     75  */
     76 static bool ft_calculate_fte_mic(struct handshake_state *hs, uint8_t seq_num,
     77 				const uint8_t *rsne, const uint8_t *fte,
     78 				const uint8_t *ric, uint8_t *out_mic)
     79 {
     80 	struct iovec iov[10];
     81 	int iov_elems = 0;
     82 	struct l_checksum *checksum;
     83 	const uint8_t *kck = handshake_state_get_kck(hs);
     84 	size_t kck_len = handshake_state_get_kck_len(hs);
     85 	uint8_t zero_mic[24] = {};
     86 
     87 	iov[iov_elems].iov_base = hs->spa;
     88 	iov[iov_elems++].iov_len = 6;
     89 
     90 	iov[iov_elems].iov_base = hs->aa;
     91 	iov[iov_elems++].iov_len = 6;
     92 
     93 	iov[iov_elems].iov_base = &seq_num;
     94 	iov[iov_elems++].iov_len = 1;
     95 
     96 	if (rsne) {
     97 		iov[iov_elems].iov_base = (void *) rsne;
     98 		iov[iov_elems++].iov_len = rsne[1] + 2;
     99 	}
    100 
    101 	iov[iov_elems].iov_base = hs->mde;
    102 	iov[iov_elems++].iov_len = hs->mde[1] + 2;
    103 
    104 	if (fte) {
    105 		iov[iov_elems].iov_base = (void *) fte;
    106 		iov[iov_elems++].iov_len = 4;
    107 
    108 		iov[iov_elems].iov_base = zero_mic;
    109 		iov[iov_elems++].iov_len = kck_len;
    110 
    111 		iov[iov_elems].iov_base = (void *) (fte + 4 + kck_len);
    112 		iov[iov_elems++].iov_len = fte[1] + 2 - 4 - kck_len;
    113 	}
    114 
    115 	if (ric) {
    116 		iov[iov_elems].iov_base = (void *) ric;
    117 		iov[iov_elems++].iov_len = ric[1] + 2;
    118 	}
    119 
    120 	if (kck_len == 16)
    121 		checksum = l_checksum_new_cmac_aes(kck, kck_len);
    122 	else
    123 		checksum = l_checksum_new_hmac(L_CHECKSUM_SHA384, kck, kck_len);
    124 
    125 	if (!checksum)
    126 		return false;
    127 
    128 	l_checksum_updatev(checksum, iov, iov_elems);
    129 	l_checksum_get_digest(checksum, out_mic, kck_len);
    130 	l_checksum_free(checksum);
    131 
    132 	return true;
    133 }
    134 
    135 /*
    136  * Validate the FC, the addresses, Auth Type and authentication sequence
    137  * number of an FT Authentication Response frame, return status code, and
    138  * the start of the IE array (RSN, MD, FT, TI and RIC).
    139  * See 8.3.3.1 for the header and 8.3.3.11 for the body format.
    140  */
    141 static bool ft_parse_authentication_resp_frame(const uint8_t *data, size_t len,
    142 				const uint8_t *addr1, const uint8_t *addr2,
    143 				const uint8_t *addr3, uint16_t auth_seq,
    144 				uint16_t *out_status, const uint8_t **out_ies,
    145 				size_t *out_ies_len)
    146 {
    147 	uint16_t status = 0;
    148 
    149 	if (len < 30)
    150 		return false;
    151 
    152 	if (memcmp(data + 4, addr1, 6))
    153 		return false;
    154 	if (memcmp(data + 10, addr2, 6))
    155 		return false;
    156 	if (memcmp(data + 16, addr3, 6))
    157 		return false;
    158 
    159 	/* Check Authentication algorithm number is FT (2) */
    160 	if (l_get_le16(data + 24) != 2)
    161 		return false;
    162 
    163 	if (l_get_le16(data + 26) != auth_seq)
    164 		return false;
    165 
    166 	if (auth_seq == 2 || auth_seq == 4)
    167 		status = l_get_le16(data + 28);
    168 
    169 	if (out_status)
    170 		*out_status = status;
    171 
    172 	if (status == 0 && out_ies) {
    173 		*out_ies = data + 28;
    174 		*out_ies_len = len - 28;
    175 	}
    176 
    177 	return true;
    178 }
    179 
    180 static bool ft_parse_associate_resp_frame(const uint8_t *frame, size_t frame_len,
    181 				uint16_t *out_status, const uint8_t **rsne,
    182 				const uint8_t **mde, const uint8_t **fte)
    183 {
    184 	const struct mmpdu_header *mpdu;
    185 	const struct mmpdu_association_response *body;
    186 	struct ie_tlv_iter iter;
    187 
    188 	mpdu = mpdu_validate(frame, frame_len);
    189 	if (!mpdu)
    190 		return false;
    191 
    192 	body = mmpdu_body(mpdu);
    193 
    194 	ie_tlv_iter_init(&iter, body->ies, (const uint8_t *) mpdu + frame_len -
    195 				body->ies);
    196 
    197 	while (ie_tlv_iter_next(&iter)) {
    198 		switch (ie_tlv_iter_get_tag(&iter)) {
    199 		case IE_TYPE_RSN:
    200 			if (*rsne)
    201 				return false;
    202 
    203 			*rsne = ie_tlv_iter_get_data(&iter) - 2;
    204 			break;
    205 
    206 		case IE_TYPE_MOBILITY_DOMAIN:
    207 			if (*mde)
    208 				return false;
    209 
    210 			*mde = ie_tlv_iter_get_data(&iter) - 2;
    211 			break;
    212 
    213 		case IE_TYPE_FAST_BSS_TRANSITION:
    214 			if (*fte)
    215 				return false;
    216 
    217 			*fte = ie_tlv_iter_get_data(&iter) - 2;
    218 			break;
    219 		}
    220 	}
    221 
    222 	*out_status = L_LE16_TO_CPU(body->status_code);
    223 
    224 	return true;
    225 }
    226 
    227 static bool ft_verify_rsne(struct handshake_state *hs,
    228 				const uint8_t *rsne, const uint8_t *pmk_r0_name,
    229 				const uint8_t *authenticator_ie)
    230 {
    231 	/*
    232 	 * In an RSN, check for an RSNE containing the PMK-R0-Name and
    233 	 * the remaining fields same as in the advertised RSNE.
    234 	 *
    235 	 * 12.8.3: "The RSNE shall be present only if dot11RSNAActivated
    236 	 * is true. If present, the RSNE shall be set as follows:
    237 	 * - Version field shall be set to 1.
    238 	 * - PMKID Count field shall be set to 1.
    239 	 * - PMKID List field shall be set to the value contained in the
    240 	 *   first message of this sequence.
    241 	 * - All other fields shall be identical to the contents of the
    242 	 *   RSNE advertised by the AP in Beacon and Probe Response frames."
    243 	 */
    244 
    245 	struct ie_rsn_info msg2_rsne;
    246 
    247 	if (!rsne)
    248 		return false;
    249 
    250 	if (ie_parse_rsne_from_data(rsne, rsne[1] + 2,
    251 						&msg2_rsne) < 0)
    252 		return false;
    253 
    254 	if (msg2_rsne.num_pmkids != 1 ||
    255 				memcmp(msg2_rsne.pmkids, pmk_r0_name, 16))
    256 		return false;
    257 
    258 	if (!handshake_util_ap_ie_matches(hs, &msg2_rsne, authenticator_ie, false))
    259 		return false;
    260 
    261 	return true;
    262 }
    263 
    264 static int parse_ies(struct handshake_state *hs,
    265 			const uint8_t *authenticator_ie,
    266 			const uint8_t *ies, size_t ies_len,
    267 			const uint8_t **mde_out,
    268 			const uint8_t **fte_out)
    269 {
    270 	struct ie_tlv_iter iter;
    271 	const uint8_t *rsne = NULL;
    272 	const uint8_t *mde = NULL;
    273 	const uint8_t *fte = NULL;
    274 	bool is_rsn;
    275 
    276 	ie_tlv_iter_init(&iter, ies, ies_len);
    277 
    278 	while (ie_tlv_iter_next(&iter)) {
    279 		switch (ie_tlv_iter_get_tag(&iter)) {
    280 		case IE_TYPE_RSN:
    281 			if (rsne)
    282 				goto ft_error;
    283 
    284 			rsne = ie_tlv_iter_get_data(&iter) - 2;
    285 			break;
    286 
    287 		case IE_TYPE_MOBILITY_DOMAIN:
    288 			if (mde)
    289 				goto ft_error;
    290 
    291 			mde = ie_tlv_iter_get_data(&iter) - 2;
    292 			break;
    293 
    294 		case IE_TYPE_FAST_BSS_TRANSITION:
    295 			if (fte)
    296 				goto ft_error;
    297 
    298 			fte = ie_tlv_iter_get_data(&iter) - 2;
    299 			break;
    300 		}
    301 	}
    302 
    303 	is_rsn = hs->supplicant_ie != NULL;
    304 
    305 	if (is_rsn) {
    306 		if (!ft_verify_rsne(hs, rsne, hs->pmk_r0_name,
    307 					authenticator_ie))
    308 			goto ft_error;
    309 	} else if (rsne)
    310 		goto ft_error;
    311 
    312 	if (mde_out)
    313 		*mde_out = mde;
    314 
    315 	if (fte_out)
    316 		*fte_out = fte;
    317 
    318 	return 0;
    319 
    320 ft_error:
    321 	return -EINVAL;
    322 }
    323 
    324 static bool ft_parse_fte(struct handshake_state *hs,
    325 				const uint8_t *snonce,
    326 				const uint8_t *fte,
    327 				struct ie_ft_info *ft_info)
    328 {
    329 	/*
    330 	 * In an RSN, check for an FT IE with the same R0KH-ID and the same
    331 	 * SNonce that we sent, and check that the R1KH-ID and the ANonce
    332 	 * are present.  Use them to generate new PMK-R1, PMK-R1-Name and PTK
    333 	 * in handshake.c.
    334 	 *
    335 	 * 12.8.3: "The FTE shall be present only if dot11RSNAActivated is
    336 	 * true. If present, the FTE shall be set as follows:
    337 	 * - R0KH-ID shall be identical to the R0KH-ID provided by the FTO
    338 	 *   in the first message.
    339 	 * - R1KH-ID shall be set to the R1KH-ID of the target AP, from
    340 	 *   dot11FTR1KeyHolderID.
    341 	 * - ANonce shall be set to a value chosen randomly by the target AP,
    342 	 *   following the recommendations of 11.6.5.
    343 	 * - SNonce shall be set to the value contained in the first message
    344 	 *   of this sequence.
    345 	 * - All other fields shall be set to 0."
    346 	 */
    347 	uint8_t zeros[24] = {};
    348 	uint32_t kck_len = handshake_state_get_kck_len(hs);
    349 
    350 	if (!fte)
    351 		return false;
    352 
    353 	if (ie_parse_fast_bss_transition_from_data(fte, fte[1] + 2,
    354 					kck_len, ft_info) < 0)
    355 		return false;
    356 
    357 	if (ft_info->mic_element_count != 0 ||
    358 			memcmp(ft_info->mic, zeros, kck_len))
    359 		return false;
    360 
    361 	if (hs->r0khid_len != ft_info->r0khid_len ||
    362 			memcmp(hs->r0khid, ft_info->r0khid,
    363 				hs->r0khid_len) ||
    364 			!ft_info->r1khid_present)
    365 		return false;
    366 
    367 	if (memcmp(ft_info->snonce, snonce, 32))
    368 		return false;
    369 
    370 	return true;
    371 }
    372 
    373 static bool mde_equal(const uint8_t *mde1, const uint8_t *mde2)
    374 {
    375 	if (!mde1 || !mde2)
    376 		return false;
    377 
    378 	/*
    379 	 * Check for an MD IE identical to the one we sent in message 1
    380 	 *
    381 	 * 12.8.3: "The MDE shall contain the MDID and FT Capability and
    382 	 * Policy fields. This element shall be the same as the MDE
    383 	 * advertised by the target AP in Beacon and Probe Response frames."
    384 	 */
    385 	return memcmp(mde1, mde1, mde1[1] + 2) == 0;
    386 }
    387 
    388 static int ft_over_ds_parse_action_response(const uint8_t *frame,
    389 					size_t frame_len,
    390 					const uint8_t **spa_out,
    391 					const uint8_t **aa_out,
    392 					const uint8_t **ies_out,
    393 					size_t *ies_len)
    394 {
    395 	struct mmpdu_header *hdr = (struct mmpdu_header *)frame;
    396 	size_t hdr_len = mmpdu_header_len(hdr);
    397 	uint16_t status;
    398 	const uint8_t *aa;
    399 	const uint8_t *spa;
    400 
    401 	if (frame_len < hdr_len + 16)
    402 		return -EINVAL;
    403 
    404 	frame += hdr_len;
    405 	frame_len -= hdr_len;
    406 
    407 	/* Category FT */
    408 	if (frame[0] != 6)
    409 		return -EINVAL;
    410 
    411 	/* FT Action */
    412 	if (frame[1] != 2)
    413 		return -EINVAL;
    414 
    415 	spa = frame + 2;
    416 	aa = frame + 8;
    417 
    418 	if (memcmp(spa, hdr->address_1, 6))
    419 		return -EINVAL;
    420 
    421 	if (spa_out)
    422 		*spa_out = spa;
    423 
    424 	if (aa_out)
    425 		*aa_out = aa;
    426 
    427 	status = l_get_le16(frame + 14);
    428 	if (status != 0)
    429 		goto done;
    430 
    431 	if (ies_out && ies_len) {
    432 		*ies_out = frame + 16;
    433 		*ies_len = frame_len - 16;
    434 	}
    435 
    436 done:
    437 	return (int)status;
    438 }
    439 
    440 int __ft_rx_associate(uint32_t ifindex, const uint8_t *frame, size_t frame_len)
    441 {
    442 	struct netdev *netdev = netdev_find(ifindex);
    443 	struct handshake_state *hs = netdev_get_handshake(netdev);
    444 	uint32_t kck_len = handshake_state_get_kck_len(hs);
    445 	const uint8_t *rsne = NULL;
    446 	const uint8_t *mde = NULL;
    447 	const uint8_t *fte = NULL;
    448 	const uint8_t *sent_mde = hs->mde;
    449 	bool is_rsn = hs->supplicant_ie != NULL;
    450 	uint16_t out_status;
    451 
    452 	if (!ft_parse_associate_resp_frame(frame, frame_len, &out_status, &rsne,
    453 					&mde, &fte))
    454 		return -EBADMSG;
    455 
    456 	if (out_status != 0)
    457 		return (int)out_status;
    458 
    459 	/*
    460 	 * During a transition in an RSN, check for an RSNE containing the
    461 	 * PMK-R1-Name and the remaining fields same as in the advertised
    462 	 * RSNE.
    463 	 *
    464 	 * 12.8.5: "The RSNE shall be present only if dot11RSNAActivated is
    465 	 * true. If present, the RSNE shall be set as follows:
    466 	 * - Version field shall be set to 1.
    467 	 * - PMKID Count field shall be set to 1.
    468 	 * - PMKID field shall contain the PMKR1Name
    469 	 * - All other fields shall be identical to the contents of the RSNE
    470 	 *   advertised by the target AP in Beacon and Probe Response frames."
    471 	 */
    472 	if (is_rsn) {
    473 		struct ie_rsn_info msg4_rsne;
    474 
    475 		if (!rsne)
    476 			return -EBADMSG;
    477 
    478 		if (ie_parse_rsne_from_data(rsne, rsne[1] + 2,
    479 						&msg4_rsne) < 0)
    480 			return -EBADMSG;
    481 
    482 		if (msg4_rsne.num_pmkids != 1 ||
    483 				memcmp(msg4_rsne.pmkids, hs->pmk_r1_name, 16))
    484 			return -EBADMSG;
    485 
    486 		if (!handshake_util_ap_ie_matches(hs, &msg4_rsne,
    487 							hs->authenticator_ie,
    488 							false))
    489 			return -EBADMSG;
    490 	} else {
    491 		if (rsne)
    492 			return -EBADMSG;
    493 	}
    494 
    495 	/* An MD IE identical to the one we sent must be present */
    496 	if (sent_mde && (!mde || memcmp(sent_mde, mde, sent_mde[1] + 2)))
    497 		return -EBADMSG;
    498 
    499 	/*
    500 	 * An FT IE is required in an initial mobility domain
    501 	 * association and re-associations in an RSN but not present
    502 	 * in a non-RSN (12.4.2 vs. 12.4.3).
    503 	 */
    504 	if (sent_mde && is_rsn && !fte)
    505 		return -EBADMSG;
    506 	if (!(sent_mde && is_rsn) && fte)
    507 		return -EBADMSG;
    508 
    509 	if (fte) {
    510 		struct ie_ft_info ft_info;
    511 		uint8_t mic[24];
    512 
    513 		if (ie_parse_fast_bss_transition_from_data(fte, fte[1] + 2,
    514 						kck_len, &ft_info) < 0)
    515 			return -EBADMSG;
    516 
    517 		/*
    518 		 * In an RSN, check for an FT IE with the same
    519 		 * R0KH-ID, R1KH-ID, ANonce and SNonce that we
    520 		 * received in message 2, MIC Element Count
    521 		 * of 6 and the correct MIC.
    522 		 */
    523 
    524 		if (!ft_calculate_fte_mic(hs, 6, rsne, fte, NULL, mic))
    525 			return -EBADMSG;
    526 
    527 		if (ft_info.mic_element_count != 3 ||
    528 				memcmp(ft_info.mic, mic, kck_len))
    529 			return -EBADMSG;
    530 
    531 		if (hs->r0khid_len != ft_info.r0khid_len ||
    532 				memcmp(hs->r0khid, ft_info.r0khid,
    533 					hs->r0khid_len) ||
    534 				!ft_info.r1khid_present ||
    535 				memcmp(hs->r1khid, ft_info.r1khid, 6))
    536 			return -EBADMSG;
    537 
    538 		if (memcmp(ft_info.anonce, hs->anonce, 32))
    539 			return -EBADMSG;
    540 
    541 		if (memcmp(ft_info.snonce, hs->snonce, 32))
    542 			return -EBADMSG;
    543 
    544 		if (ft_info.gtk_len) {
    545 			uint8_t gtk[32];
    546 
    547 			if (!handshake_decode_fte_key(hs, ft_info.gtk,
    548 							ft_info.gtk_len,
    549 							gtk))
    550 				return -EBADMSG;
    551 
    552 			if (ft_info.gtk_rsc[6] != 0x00 ||
    553 					ft_info.gtk_rsc[7] != 0x00)
    554 				return -EBADMSG;
    555 
    556 			handshake_state_install_gtk(hs, ft_info.gtk_key_id,
    557 							gtk, ft_info.gtk_len,
    558 							ft_info.gtk_rsc, 6);
    559 		}
    560 
    561 		if (ft_info.igtk_len) {
    562 			uint8_t igtk[16];
    563 
    564 			if (!handshake_decode_fte_key(hs, ft_info.igtk,
    565 						ft_info.igtk_len, igtk))
    566 				return -EBADMSG;
    567 
    568 			handshake_state_install_igtk(hs, ft_info.igtk_key_id,
    569 						igtk, ft_info.igtk_len,
    570 						ft_info.igtk_ipn);
    571 		}
    572 
    573 		handshake_state_install_ptk(hs);
    574 	}
    575 
    576 	return 0;
    577 }
    578 
    579 static bool ft_build_authenticate_ies(struct handshake_state *hs, bool ocvc,
    580 				const uint8_t *new_snonce, uint8_t *buf,
    581 				size_t *len)
    582 {
    583 	uint32_t kck_len = handshake_state_get_kck_len(hs);
    584 	bool is_rsn = hs->supplicant_ie != NULL;
    585 	uint8_t *ptr = buf;
    586 
    587 	if (is_rsn) {
    588 		struct ie_rsn_info rsn_info;
    589 
    590 		/*
    591 		 * Rebuild the RSNE to include the PMKR0Name and append
    592 		 * MDE + FTE.
    593 		 *
    594 		 * 12.8.2: "If present, the RSNE shall be set as follows:
    595 		 * - Version field shall be set to 1.
    596 		 * - PMKID Count field shall be set to 1.
    597 		 * - PMKID List field shall contain the PMKR0Name.
    598 		 * - All other fields shall be as specified in 8.4.2.27
    599 		 *   and 11.5.3."
    600 		 */
    601 		if (ie_parse_rsne_from_data(hs->supplicant_ie,
    602 						hs->supplicant_ie[1] + 2,
    603 						&rsn_info) < 0)
    604 			return false;
    605 
    606 		rsn_info.num_pmkids = 1;
    607 		rsn_info.pmkids = hs->pmk_r0_name;
    608 		rsn_info.ocvc = ocvc;
    609 
    610 		ie_build_rsne(&rsn_info, ptr);
    611 		ptr += ptr[1] + 2;
    612 	}
    613 
    614 	/* The MDE advertised by the BSS must be passed verbatim */
    615 	ptr[0] = IE_TYPE_MOBILITY_DOMAIN;
    616 	ptr[1] = 3;
    617 	memcpy(ptr + 2, hs->mde + 2, 3);
    618 	ptr += 5;
    619 
    620 	if (is_rsn) {
    621 		struct ie_ft_info ft_info;
    622 
    623 		/*
    624 		 * 12.8.2: "If present, the FTE shall be set as follows:
    625 		 * - R0KH-ID shall be the value of R0KH-ID obtained by the
    626 		 *   FTO during its FT initial mobility domain association
    627 		 *   exchange.
    628 		 * - SNonce shall be set to a value chosen randomly by the
    629 		 *   FTO, following the recommendations of 11.6.5.
    630 		 * - All other fields shall be set to 0."
    631 		 */
    632 
    633 		memset(&ft_info, 0, sizeof(ft_info));
    634 
    635 		memcpy(ft_info.r0khid, hs->r0khid, hs->r0khid_len);
    636 		ft_info.r0khid_len = hs->r0khid_len;
    637 
    638 		memcpy(ft_info.snonce, new_snonce, 32);
    639 
    640 		ie_build_fast_bss_transition(&ft_info, kck_len, ptr);
    641 
    642 		ptr += ptr[1] + 2;
    643 	}
    644 
    645 	if (len)
    646 		*len = ptr - buf;
    647 
    648 	return true;
    649 }
    650 
    651 void __ft_set_tx_frame_func(ft_tx_frame_func_t func)
    652 {
    653 	tx_frame = func;
    654 }
    655 
    656 static bool ft_parse_ies(struct ft_info *info, struct handshake_state *hs,
    657 			const uint8_t *ies, size_t ies_len)
    658 {
    659 	const uint8_t *mde = NULL;
    660 	const uint8_t *fte = NULL;
    661 	bool is_rsn = hs->supplicant_ie != NULL;
    662 
    663 	if (parse_ies(hs, info->authenticator_ie, ies, ies_len,
    664 				&mde, &fte) < 0)
    665 		return false;
    666 
    667 	if (!mde_equal(info->mde, mde))
    668 		goto ft_error;
    669 
    670 	if (is_rsn) {
    671 		if (!ft_parse_fte(hs, info->snonce, fte, &info->ft_info))
    672 			goto ft_error;
    673 
    674 		info->fte = l_memdup(fte, fte[1] + 2);
    675 	} else if (fte)
    676 		goto ft_error;
    677 
    678 	return true;
    679 
    680 ft_error:
    681 	return false;
    682 }
    683 
    684 static struct ft_info *ft_info_find(uint32_t ifindex, const uint8_t *aa)
    685 {
    686 	const struct l_queue_entry *e;
    687 
    688 	for (e = l_queue_get_entries(info_list); e; e = e->next) {
    689 		struct ft_info *info = e->data;
    690 
    691 		if (info->ifindex != ifindex)
    692 			continue;
    693 
    694 		if (aa && memcmp(info->aa, aa, 6))
    695 			continue;
    696 
    697 		return info;
    698 	}
    699 
    700 	return NULL;
    701 }
    702 
    703 void __ft_rx_action(uint32_t ifindex, const uint8_t *frame, size_t frame_len)
    704 {
    705 	struct netdev *netdev = netdev_find(ifindex);
    706 	struct handshake_state *hs = netdev_get_handshake(netdev);
    707 	struct ft_info *info;
    708 	int ret;
    709 	const uint8_t *aa = NULL;
    710 	const uint8_t *spa = NULL;
    711 	const uint8_t *ies = NULL;
    712 	size_t ies_len = 0;
    713 
    714 	ret = ft_over_ds_parse_action_response(frame, frame_len, &spa, &aa,
    715 						&ies, &ies_len);
    716 	if (ret < 0) {
    717 		l_debug("Could not parse action response");
    718 		return;
    719 	}
    720 
    721 	info = ft_info_find(ifindex, aa);
    722 	if (!info) {
    723 		l_debug("No FT info found for BSS "MAC, MAC_STR(aa));
    724 		return;
    725 	}
    726 
    727 
    728 	if (ret != 0) {
    729 		l_debug("BSS "MAC" rejected FT action with status=%u",
    730 				MAC_STR(info->aa), ret);
    731 		info->status = ret;
    732 		goto done;
    733 	}
    734 
    735 	if (!ft_parse_ies(info, hs, ies, ies_len)) {
    736 		l_debug("Could not parse action response IEs");
    737 		goto ft_error;
    738 	}
    739 
    740 	info->status = ret;
    741 
    742 done:
    743 	l_timeout_remove(info->timeout);
    744 	info->timeout = NULL;
    745 
    746 	wiphy_radio_work_done(netdev_get_wiphy(netdev), info->work.id);
    747 
    748 	return;
    749 
    750 ft_error:
    751 	l_debug("FT-over-DS authenticate to "MAC" failed", MAC_STR(info->aa));
    752 }
    753 
    754 static struct ft_info *ft_info_new(struct handshake_state *hs,
    755 					const struct scan_bss *target_bss)
    756 {
    757 	struct ft_info *info = l_new(struct ft_info, 1);
    758 
    759 	info->ifindex = hs->ifindex;
    760 	memcpy(info->spa, hs->spa, 6);
    761 	memcpy(info->aa, target_bss->addr, 6);
    762 	memcpy(info->mde, target_bss->mde, sizeof(info->mde));
    763 	memcpy(info->prev_bssid, hs->aa, 6);
    764 
    765 	info->frequency = target_bss->frequency;
    766 
    767 	if (target_bss->rsne)
    768 		info->authenticator_ie = l_memdup(target_bss->rsne,
    769 						target_bss->rsne[1] + 2);
    770 
    771 	l_getrandom(info->snonce, 32);
    772 	info->status = -ENOENT;
    773 
    774 	return info;
    775 }
    776 
    777 static void ft_info_destroy(void *data)
    778 {
    779 	struct ft_info *info = data;
    780 
    781 	if (info->fte)
    782 		l_free(info->fte);
    783 
    784 	if (info->authenticator_ie)
    785 		l_free(info->authenticator_ie);
    786 
    787 	if (info->timeout)
    788 		l_timeout_remove(info->timeout);
    789 
    790 	l_free(info);
    791 }
    792 
    793 static bool ft_prepare_handshake(struct ft_info *info,
    794 					struct handshake_state *hs)
    795 {
    796 	uint32_t kck_len = handshake_state_get_kck_len(hs);
    797 	struct ie_rsn_info rsn_info;
    798 	struct ie_ft_info ft_info;
    799 	uint8_t *fte;
    800 	uint8_t *rsne;
    801 
    802 	handshake_state_set_authenticator_address(hs, info->aa);
    803 
    804 	memcpy(hs->mde + 2, info->mde, 3);
    805 
    806 	handshake_state_set_chandef(hs, NULL);
    807 
    808 	if (!hs->supplicant_ie)
    809 		return true;
    810 
    811 	if (info->authenticator_ie)
    812 		handshake_state_set_authenticator_ie(hs,
    813 							info->authenticator_ie);
    814 
    815 	memcpy(hs->snonce, info->snonce, sizeof(hs->snonce));
    816 
    817 	handshake_state_set_authenticator_fte(hs, info->fte);
    818 
    819 	handshake_state_set_anonce(hs, info->ft_info.anonce);
    820 
    821 	handshake_state_set_kh_ids(hs, info->ft_info.r0khid,
    822 						info->ft_info.r0khid_len,
    823 						info->ft_info.r1khid);
    824 
    825 	handshake_state_derive_ptk(hs);
    826 
    827 	/*
    828 	 * Rebuild the RSNE to include the PMKR1Name and append
    829 	 * MDE + FTE.
    830 	 *
    831 	 * 12.8.4: "If present, the RSNE shall be set as follows:
    832 	 * - Version field shall be set to 1.
    833 	 * - PMKID Count field shall be set to 1.
    834 	 * - PMKID field shall contain the PMKR1Name.
    835 	 * - All other fields shall be as specified in 8.4.2.27
    836 	 *   and 11.5.3."
    837 	 */
    838 	if (ie_parse_rsne_from_data(hs->supplicant_ie,
    839 					hs->supplicant_ie[1] + 2,
    840 					&rsn_info) < 0)
    841 		return false;
    842 
    843 	rsn_info.num_pmkids = 1;
    844 	rsn_info.pmkids = hs->pmk_r1_name;
    845 	/* Always set OCVC false for FT for now */
    846 	rsn_info.ocvc = false;
    847 	rsne = alloca(256);
    848 
    849 	ie_build_rsne(&rsn_info, rsne);
    850 	handshake_state_set_supplicant_ie(hs, rsne);
    851 
    852 	/*
    853 	 * 12.8.4: "If present, the FTE shall be set as follows:
    854 	 * - ANonce, SNonce, R0KH-ID, and R1KH-ID shall be set to
    855 	 *   the values contained in the second message of this
    856 	 *   sequence.
    857 	 * - The Element Count field of the MIC Control field shall
    858 	 *   be set to the number of elements protected in this
    859 	 *   frame (variable).
    860 	 * [...]
    861 	 * - All other fields shall be set to 0."
    862 	 */
    863 	memset(&ft_info, 0, sizeof(ft_info));
    864 	ft_info.mic_element_count = 3;
    865 	memcpy(ft_info.r0khid, hs->r0khid, hs->r0khid_len);
    866 	ft_info.r0khid_len = hs->r0khid_len;
    867 	memcpy(ft_info.r1khid, hs->r1khid, 6);
    868 	ft_info.r1khid_present = true;
    869 	memcpy(ft_info.anonce, hs->anonce, 32);
    870 	memcpy(ft_info.snonce, hs->snonce, 32);
    871 
    872 	/*
    873 	 * IEEE 802.11-2020 Section 13.7.1 FT reassociation in an RSN
    874 	 *
    875 	 * "If dot11RSNAOperatingChannelValidationActivated is true and
    876 	 *  the FTO indicates OCVC capability, the target AP shall
    877 	 *  ensure that OCI subelement of the FTE matches by ensuring
    878 	 *  that all of the following are true:
    879 	 *      - OCI subelement is present
    880 	 *      - Channel information in the OCI matches current
    881 	 *        operating channel parameters (see 12.2.9)"
    882 	 */
    883 	if (hs->supplicant_ocvc && hs->chandef) {
    884 		oci_from_chandef(hs->chandef, ft_info.oci);
    885 		ft_info.oci_present = true;
    886 	}
    887 
    888 	fte = alloca(256);
    889 	ie_build_fast_bss_transition(&ft_info, kck_len, fte);
    890 
    891 	if (!ft_calculate_fte_mic(hs, 5, rsne, fte, NULL, ft_info.mic))
    892 		return false;
    893 
    894 	/* Rebuild the FT IE now with the MIC included */
    895 	ie_build_fast_bss_transition(&ft_info, kck_len, fte);
    896 
    897 	handshake_state_set_supplicant_fte(hs, fte);
    898 
    899 	return true;
    900 }
    901 
    902 static bool ft_send_action(struct wiphy_radio_work_item *work)
    903 {
    904 	struct ft_info *info = l_container_of(work, struct ft_info, work);
    905 	struct netdev *netdev = netdev_find(info->ifindex);
    906 	struct handshake_state *hs = netdev_get_handshake(netdev);
    907 	uint8_t ft_req[14];
    908 	struct iovec iov[5];
    909 	uint8_t ies[512];
    910 	size_t len;
    911 	int ret = -EINVAL;
    912 
    913 	ft_req[0] = 6; /* FT category */
    914 	ft_req[1] = 1; /* FT Request action */
    915 	memcpy(ft_req + 2, info->spa, 6);
    916 	memcpy(ft_req + 8, info->aa, 6);
    917 
    918 	l_debug("");
    919 
    920 	if (!ft_build_authenticate_ies(hs, hs->supplicant_ocvc, info->snonce,
    921 					ies, &len))
    922 		goto failed;
    923 
    924 	iov[0].iov_base = ft_req;
    925 	iov[0].iov_len = sizeof(ft_req);
    926 
    927 	iov[1].iov_base = ies;
    928 	iov[1].iov_len = len;
    929 
    930 	ret = tx_frame(hs->ifindex, 0x00d0, info->ds_frequency, hs->aa, iov, 2);
    931 	if (ret < 0)
    932 		goto failed;
    933 
    934 	l_queue_push_tail(info_list, info);
    935 
    936 	return false;
    937 
    938 failed:
    939 	l_debug("FT-over-DS action failed to "MAC, MAC_STR(hs->aa));
    940 
    941 	ft_info_destroy(info);
    942 	return true;
    943 }
    944 
    945 struct wiphy_radio_work_item_ops ft_ops = {
    946 	.do_work = ft_send_action,
    947 };
    948 
    949 static void ft_ds_timeout(struct l_timeout *timeout, void *user_data)
    950 {
    951 	struct ft_info *info = user_data;
    952 	struct netdev *netdev = netdev_find(info->ifindex);
    953 
    954 	wiphy_radio_work_done(netdev_get_wiphy(netdev), info->work.id);
    955 }
    956 
    957 int ft_action(uint32_t ifindex, uint32_t freq, const struct scan_bss *target)
    958 {
    959 	struct netdev *netdev = netdev_find(ifindex);
    960 	struct handshake_state *hs = netdev_get_handshake(netdev);
    961 	struct ft_info *info;
    962 
    963 	info = ft_info_new(hs, target);
    964 	info->ds_frequency = freq;
    965 	info->timeout = l_timeout_create_ms(200, ft_ds_timeout, info, NULL);
    966 
    967 	wiphy_radio_work_insert(netdev_get_wiphy(netdev), &info->work,
    968 				WIPHY_WORK_PRIORITY_FT, &ft_ops);
    969 
    970 	return 0;
    971 }
    972 
    973 void __ft_rx_authenticate(uint32_t ifindex, const uint8_t *frame,
    974 				size_t frame_len)
    975 {
    976 	struct netdev *netdev = netdev_find(ifindex);
    977 	struct handshake_state *hs = netdev_get_handshake(netdev);
    978 	struct ft_info *info;
    979 	uint16_t status;
    980 	const uint8_t *ies;
    981 	size_t ies_len;
    982 
    983 	info = ft_info_find(ifindex, NULL);
    984 	if (!info)
    985 		return;
    986 
    987 	if (!ft_parse_authentication_resp_frame(frame, frame_len,
    988 					info->spa, info->aa, info->aa, 2,
    989 					&status, &ies, &ies_len)) {
    990 		l_debug("Could not parse auth response");
    991 		return;
    992 	}
    993 
    994 	if (status != 0) {
    995 		l_debug("BSS "MAC" rejected FT auth with status=%u",
    996 				MAC_STR(info->aa), status);
    997 		info->status = status;
    998 		goto cancel;
    999 	}
   1000 
   1001 	if (!ft_parse_ies(info, hs, ies, ies_len)) {
   1002 		l_debug("Could not parse auth response IEs");
   1003 		goto cancel;
   1004 	}
   1005 
   1006 	info->status = status;
   1007 
   1008 cancel:
   1009 	/*
   1010 	 * Verified to be expected target, offchannel or onchannel work can
   1011 	 * now be canceled
   1012 	 */
   1013 	if (info->onchannel) {
   1014 		l_timeout_remove(info->timeout);
   1015 		info->timeout = NULL;
   1016 		wiphy_radio_work_done(netdev_get_wiphy(netdev), info->work.id);
   1017 	} else
   1018 		offchannel_cancel(netdev_get_wdev_id(netdev),
   1019 							info->offchannel_id);
   1020 }
   1021 
   1022 static void ft_send_authenticate(void *user_data)
   1023 {
   1024 	struct ft_info *info = user_data;
   1025 	struct netdev *netdev = netdev_find(info->ifindex);
   1026 	struct handshake_state *hs = netdev_get_handshake(netdev);
   1027 	uint8_t ies[256];
   1028 	size_t len;
   1029 	struct iovec iov[2];
   1030 	struct mmpdu_authentication auth;
   1031 
   1032 	l_debug("");
   1033 
   1034 	/* Authentication body */
   1035 	auth.algorithm = L_CPU_TO_LE16(MMPDU_AUTH_ALGO_FT);
   1036 	auth.transaction_sequence = L_CPU_TO_LE16(1);
   1037 	auth.status = L_CPU_TO_LE16(0);
   1038 
   1039 	iov[0].iov_base = &auth;
   1040 	iov[0].iov_len = sizeof(struct mmpdu_authentication);
   1041 
   1042 	if (!ft_build_authenticate_ies(hs, hs->supplicant_ocvc, info->snonce,
   1043 					ies, &len))
   1044 		return;
   1045 
   1046 	iov[1].iov_base = ies;
   1047 	iov[1].iov_len = len;
   1048 
   1049 	tx_frame(info->ifindex, 0x00b0, info->frequency, info->aa, iov, 2);
   1050 }
   1051 
   1052 static void ft_authenticate_destroy(int error, void *user_data)
   1053 {
   1054 	struct ft_info *info = user_data;
   1055 
   1056 	info->offchannel_id = 0;
   1057 }
   1058 
   1059 /*
   1060  * There is no callback here because its assumed that another work item will
   1061  * be inserted following this call which will check if authentication succeeded
   1062  * via ft_handshake_setup.
   1063  *
   1064  * If the netdev goes away while authentication is in-flight station will clear
   1065  * the authentications during cleanup, and in turn cancel the offchannel
   1066  * request.
   1067  */
   1068 int ft_authenticate(uint32_t ifindex, const struct scan_bss *target)
   1069 {
   1070 	struct netdev *netdev = netdev_find(ifindex);
   1071 	struct handshake_state *hs = netdev_get_handshake(netdev);
   1072 	struct ft_info *info = ft_info_new(hs, target);
   1073 
   1074 	info->offchannel_id = offchannel_start(netdev_get_wdev_id(netdev),
   1075 						WIPHY_WORK_PRIORITY_FT,
   1076 						target->frequency,
   1077 						200, ft_send_authenticate, info,
   1078 						ft_authenticate_destroy);
   1079 
   1080 	l_queue_push_tail(info_list, info);
   1081 
   1082 	return 0;
   1083 }
   1084 
   1085 static void ft_onchannel_timeout(struct l_timeout *timeout, void *user_data)
   1086 {
   1087 	struct ft_info *info = user_data;
   1088 	struct netdev *netdev = netdev_find(info->ifindex);
   1089 
   1090 	wiphy_radio_work_done(netdev_get_wiphy(netdev), info->work.id);
   1091 }
   1092 
   1093 static bool ft_send_authenticate_onchannel(struct wiphy_radio_work_item *work)
   1094 {
   1095 	struct ft_info *info = l_container_of(work, struct ft_info, work);
   1096 
   1097 	ft_send_authenticate(info);
   1098 
   1099 	info->timeout = l_timeout_create_ms(FT_ONCHANNEL_TIME,
   1100 						ft_onchannel_timeout,
   1101 						info, NULL);
   1102 	return false;
   1103 }
   1104 
   1105 struct wiphy_radio_work_item_ops ft_onchannel_ops = {
   1106 	.do_work = ft_send_authenticate_onchannel,
   1107 };
   1108 
   1109 int ft_authenticate_onchannel(uint32_t ifindex, const struct scan_bss *target)
   1110 {
   1111 	struct netdev *netdev = netdev_find(ifindex);
   1112 	struct handshake_state *hs = netdev_get_handshake(netdev);
   1113 	struct ft_info *info = ft_info_new(hs, target);
   1114 
   1115 	info->onchannel = true;
   1116 
   1117 	wiphy_radio_work_insert(netdev_get_wiphy(netdev), &info->work,
   1118 				WIPHY_WORK_PRIORITY_FT, &ft_onchannel_ops);
   1119 	l_queue_push_tail(info_list, info);
   1120 
   1121 	return 0;
   1122 }
   1123 
   1124 int ft_handshake_setup(uint32_t ifindex, const uint8_t *target)
   1125 {
   1126 	struct netdev *netdev = netdev_find(ifindex);
   1127 	struct handshake_state *hs = netdev_get_handshake(netdev);
   1128 	struct ft_info *info;
   1129 	int ret = 0;
   1130 
   1131 	info = ft_info_find(ifindex, target);
   1132 	if (!info)
   1133 		return -ENOENT;
   1134 
   1135 	/*
   1136 	 * Either failed or no response. This may have been an FT-over-DS
   1137 	 * attempt so clear out the entry so FT-over-Air can try again.
   1138 	 */
   1139 	if (info->status != 0) {
   1140 		int status = info->status;
   1141 
   1142 		l_queue_remove(info_list, info);
   1143 		ft_info_destroy(info);
   1144 
   1145 		return status;
   1146 	}
   1147 
   1148 	/*
   1149 	 * This shouldn't ever fail:
   1150 	 *  - supplicant_ie has already been validated long ago
   1151 	 *  - l_checksum_* shouldn't fail since we presumable have kernel
   1152 	 *    support, how else could we have made it this far.
   1153 	 * But just in case...
   1154 	 */
   1155 	if (L_WARN_ON(!ft_prepare_handshake(info, hs)))
   1156 		ret = -EINVAL;
   1157 
   1158 	/* After this no previous auths will be valid */
   1159 	ft_clear_authentications(ifindex);
   1160 
   1161 	return ret;
   1162 }
   1163 
   1164 static bool remove_ifindex(void *data, void *user_data)
   1165 {
   1166 	struct ft_info *info = data;
   1167 	uint32_t ifindex = L_PTR_TO_UINT(user_data);
   1168 
   1169 	if (info->ifindex != ifindex)
   1170 		return false;
   1171 
   1172 	if (info->offchannel_id)
   1173 		offchannel_cancel(netdev_get_wdev_id(netdev_find(ifindex)),
   1174 					info->offchannel_id);
   1175 
   1176 	ft_info_destroy(info);
   1177 	return true;
   1178 }
   1179 
   1180 void ft_clear_authentications(uint32_t ifindex)
   1181 {
   1182 	l_queue_foreach_remove(info_list, remove_ifindex,
   1183 				L_UINT_TO_PTR(ifindex));
   1184 }
   1185 
   1186 static int ft_init(void)
   1187 {
   1188 	info_list = l_queue_new();
   1189 
   1190 	return 0;
   1191 }
   1192 
   1193 static void ft_exit(void)
   1194 {
   1195 	if (!l_queue_isempty(info_list))
   1196 		l_warn("stale FT info objects found!");
   1197 
   1198 	l_queue_destroy(info_list, ft_info_destroy);
   1199 }
   1200 
   1201 IWD_MODULE(ft, ft_init, ft_exit);