xref: /freebsd/contrib/wpa/src/ap/ieee802_11.c (revision 71e72c9e91c4b8007a4292e09669e8b549c29e97)
1 /*
2  * hostapd / IEEE 802.11 Management
3  * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
4  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #include "utils/includes.h"
11 
12 #ifndef CONFIG_NATIVE_WINDOWS
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "crypto/crypto.h"
17 #include "crypto/sha256.h"
18 #include "crypto/sha384.h"
19 #include "crypto/sha512.h"
20 #include "crypto/random.h"
21 #include "crypto/aes.h"
22 #include "crypto/aes_siv.h"
23 #include "common/ieee802_11_defs.h"
24 #include "common/ieee802_11_common.h"
25 #include "common/wpa_ctrl.h"
26 #include "common/sae.h"
27 #include "common/dpp.h"
28 #include "common/ocv.h"
29 #include "common/wpa_common.h"
30 #include "common/wpa_ctrl.h"
31 #include "common/ptksa_cache.h"
32 #include "common/nan_de.h"
33 #include "radius/radius.h"
34 #include "radius/radius_client.h"
35 #include "p2p/p2p.h"
36 #include "wps/wps.h"
37 #include "fst/fst.h"
38 #include "hostapd.h"
39 #include "beacon.h"
40 #include "ieee802_11_auth.h"
41 #include "sta_info.h"
42 #include "ieee802_1x.h"
43 #include "wpa_auth.h"
44 #include "pmksa_cache_auth.h"
45 #include "wmm.h"
46 #include "ap_list.h"
47 #include "accounting.h"
48 #include "ap_config.h"
49 #include "ap_mlme.h"
50 #include "p2p_hostapd.h"
51 #include "ap_drv_ops.h"
52 #include "wnm_ap.h"
53 #include "hw_features.h"
54 #include "ieee802_11.h"
55 #include "dfs.h"
56 #include "mbo_ap.h"
57 #include "rrm.h"
58 #include "taxonomy.h"
59 #include "fils_hlp.h"
60 #include "dpp_hostapd.h"
61 #include "gas_query_ap.h"
62 #include "comeback_token.h"
63 #include "nan_usd_ap.h"
64 #include "pasn/pasn_common.h"
65 
66 
67 #ifdef CONFIG_FILS
68 static struct wpabuf *
69 prepare_auth_resp_fils(struct hostapd_data *hapd,
70 		       struct sta_info *sta, u16 *resp,
71 		       struct rsn_pmksa_cache_entry *pmksa,
72 		       struct wpabuf *erp_resp,
73 		       const u8 *msk, size_t msk_len,
74 		       int *is_pub);
75 #endif /* CONFIG_FILS */
76 
77 #ifdef CONFIG_PASN
78 #ifdef CONFIG_FILS
79 
80 static void pasn_fils_auth_resp(struct hostapd_data *hapd,
81 				struct sta_info *sta, u16 status,
82 				struct wpabuf *erp_resp,
83 				const u8 *msk, size_t msk_len);
84 
85 #endif /* CONFIG_FILS */
86 #endif /* CONFIG_PASN */
87 
88 static void handle_auth(struct hostapd_data *hapd,
89 			const struct ieee80211_mgmt *mgmt, size_t len,
90 			int rssi, int from_queue);
91 static int add_associated_sta(struct hostapd_data *hapd,
92 			      struct sta_info *sta, int reassoc);
93 #ifdef CONFIG_IEEE8021X_AUTH
94 static struct rsn_pmksa_cache_entry *
95 pmksa_cache_search(void *ctx, const u8 *spa, const u8 *pmkid, bool is_ml);
96 #endif /* CONFIG_IEEE8021X_AUTH */
97 
98 
hostapd_eid_multi_ap(struct hostapd_data * hapd,u8 * eid,size_t len)99 static u8 * hostapd_eid_multi_ap(struct hostapd_data *hapd, u8 *eid, size_t len)
100 {
101 	struct multi_ap_params multi_ap = { 0 };
102 
103 	if (!hapd->conf->multi_ap)
104 		return eid;
105 
106 	if (hapd->conf->multi_ap & BACKHAUL_BSS)
107 		multi_ap.capability |= MULTI_AP_BACKHAUL_BSS;
108 	if (hapd->conf->multi_ap & FRONTHAUL_BSS)
109 		multi_ap.capability |= MULTI_AP_FRONTHAUL_BSS;
110 
111 	if (hapd->conf->multi_ap_client_disallow &
112 	    PROFILE1_CLIENT_ASSOC_DISALLOW)
113 		multi_ap.capability |=
114 			MULTI_AP_PROFILE1_BACKHAUL_STA_DISALLOWED;
115 	if (hapd->conf->multi_ap_client_disallow &
116 	    PROFILE2_CLIENT_ASSOC_DISALLOW)
117 		multi_ap.capability |=
118 			MULTI_AP_PROFILE2_BACKHAUL_STA_DISALLOWED;
119 
120 	multi_ap.profile = hapd->conf->multi_ap_profile;
121 	multi_ap.vlanid = hapd->conf->multi_ap_vlanid;
122 
123 	return eid + add_multi_ap_ie(eid, len, &multi_ap);
124 }
125 
126 
hostapd_supp_rates(struct hostapd_data * hapd,u8 * buf)127 static size_t hostapd_supp_rates(struct hostapd_data *hapd, u8 *buf)
128 {
129 	u8 *pos = buf;
130 	int i;
131 
132 	if (!hapd->current_rates)
133 		return 0;
134 
135 	for (i = 0; i < hapd->num_rates; i++) {
136 		*pos = hapd->current_rates[i].rate / 5;
137 		if (hapd->current_rates[i].flags & HOSTAPD_RATE_BASIC)
138 			*pos |= 0x80;
139 		pos++;
140 	}
141 
142 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
143 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
144 
145 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
146 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
147 
148 #ifdef CONFIG_IEEE80211AX
149 	if (hapd->iconf->ieee80211ax && hapd->iconf->require_he)
150 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY;
151 #endif /* CONFIG_IEEE80211AX */
152 
153 #ifdef CONFIG_IEEE80211BE
154 	if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be &&
155 	    (hapd->iconf->require_eht || hapd->conf->bss_require_eht))
156 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_EHT_PHY;
157 #endif /* CONFIG_IEEE80211BE */
158 
159 #ifdef CONFIG_SAE
160 	if ((hapd->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
161 	     hostapd_sae_pw_id_in_use(hapd->conf) == 2) &&
162 	    hapd->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK &&
163 	    wpa_key_mgmt_only_sae(hapd->conf->wpa_key_mgmt))
164 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY;
165 #endif /* CONFIG_SAE */
166 
167 	return pos - buf;
168 }
169 
170 
hostapd_eid_supp_rates(struct hostapd_data * hapd,u8 * eid)171 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
172 {
173 	u8 *pos = eid;
174 	u8 buf[100];
175 	size_t len;
176 
177 	len = hostapd_supp_rates(hapd, buf);
178 	if (len == 0)
179 		return eid;
180 	/* Only up to first eight values in this element */
181 	if (len > 8)
182 		len = 8;
183 
184 	*pos++ = WLAN_EID_SUPP_RATES;
185 	*pos++ = len;
186 	os_memcpy(pos, buf, len);
187 	pos += len;
188 
189 	return pos;
190 }
191 
192 
hostapd_eid_ext_supp_rates(struct hostapd_data * hapd,u8 * eid)193 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
194 {
195 	u8 *pos = eid;
196 	u8 buf[100];
197 	size_t len;
198 
199 	len = hostapd_supp_rates(hapd, buf);
200 	/* Starting from the 9th value for this element */
201 	if (len <= 8)
202 		return eid;
203 
204 	*pos++ = WLAN_EID_EXT_SUPP_RATES;
205 	*pos++ = len - 8;
206 	os_memcpy(pos, &buf[8], len - 8);
207 	pos += len - 8;
208 
209 	return pos;
210 }
211 
212 
hostapd_eid_rm_enabled_capab(struct hostapd_data * hapd,u8 * eid,size_t len)213 u8 * hostapd_eid_rm_enabled_capab(struct hostapd_data *hapd, u8 *eid,
214 				  size_t len)
215 {
216 	size_t i;
217 
218 	for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
219 		if (hapd->conf->radio_measurements[i])
220 			break;
221 	}
222 
223 	if (i == RRM_CAPABILITIES_IE_LEN || len < 2 + RRM_CAPABILITIES_IE_LEN)
224 		return eid;
225 
226 	*eid++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
227 	*eid++ = RRM_CAPABILITIES_IE_LEN;
228 	os_memcpy(eid, hapd->conf->radio_measurements, RRM_CAPABILITIES_IE_LEN);
229 
230 	return eid + RRM_CAPABILITIES_IE_LEN;
231 }
232 
233 
hostapd_own_capab_info(struct hostapd_data * hapd)234 u16 hostapd_own_capab_info(struct hostapd_data *hapd)
235 {
236 	int capab = WLAN_CAPABILITY_ESS;
237 	int privacy = 0;
238 	int dfs;
239 	int i;
240 
241 	/* Check if any of configured channels require DFS */
242 	dfs = hostapd_is_dfs_required(hapd->iface);
243 	if (dfs < 0) {
244 		wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
245 			   dfs);
246 		dfs = 0;
247 	}
248 
249 	if (hapd->iface->num_sta_no_short_preamble == 0 &&
250 	    hapd->iconf->preamble == SHORT_PREAMBLE)
251 		capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
252 
253 #ifdef CONFIG_WEP
254 	privacy = hapd->conf->ssid.wep.keys_set;
255 
256 	if (hapd->conf->ieee802_1x &&
257 	    (hapd->conf->default_wep_key_len ||
258 	     hapd->conf->individual_wep_key_len))
259 		privacy = 1;
260 #endif /* CONFIG_WEP */
261 
262 	if (hapd->conf->wpa)
263 		privacy = 1;
264 
265 	if (privacy)
266 		capab |= WLAN_CAPABILITY_PRIVACY;
267 
268 	if (hapd->iface->current_mode &&
269 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
270 	    hapd->iface->num_sta_no_short_slot_time == 0)
271 		capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
272 
273 	/*
274 	 * Currently, Spectrum Management capability bit is set when directly
275 	 * requested in configuration by spectrum_mgmt_required or when AP is
276 	 * running on DFS channel.
277 	 * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
278 	 */
279 	if (hapd->iface->current_mode &&
280 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
281 	    (hapd->iconf->spectrum_mgmt_required || dfs))
282 		capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
283 
284 	for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
285 		if (hapd->conf->radio_measurements[i]) {
286 			capab |= IEEE80211_CAP_RRM;
287 			break;
288 		}
289 	}
290 
291 	return capab;
292 }
293 
294 
295 #ifdef CONFIG_WEP
296 #ifndef CONFIG_NO_RC4
auth_shared_key(struct hostapd_data * hapd,struct sta_info * sta,u16 auth_transaction,const u8 * challenge,int iswep)297 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
298 			   u16 auth_transaction, const u8 *challenge,
299 			   int iswep)
300 {
301 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
302 		       HOSTAPD_LEVEL_DEBUG,
303 		       "authentication (shared key, transaction %d)",
304 		       auth_transaction);
305 
306 	if (auth_transaction == 1) {
307 		if (!sta->challenge) {
308 			/* Generate a pseudo-random challenge */
309 			u8 key[8];
310 
311 			sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
312 			if (sta->challenge == NULL)
313 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
314 
315 			if (os_get_random(key, sizeof(key)) < 0) {
316 				os_free(sta->challenge);
317 				sta->challenge = NULL;
318 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
319 			}
320 
321 			rc4_skip(key, sizeof(key), 0,
322 				 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
323 		}
324 		return 0;
325 	}
326 
327 	if (auth_transaction != 3)
328 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
329 
330 	/* Transaction 3 */
331 	if (!iswep || !sta->challenge || !challenge ||
332 	    os_memcmp_const(sta->challenge, challenge,
333 			    WLAN_AUTH_CHALLENGE_LEN)) {
334 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
335 			       HOSTAPD_LEVEL_INFO,
336 			       "shared key authentication - invalid "
337 			       "challenge-response");
338 		return WLAN_STATUS_CHALLENGE_FAIL;
339 	}
340 
341 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
342 		       HOSTAPD_LEVEL_DEBUG,
343 		       "authentication OK (shared key)");
344 	sta->flags |= WLAN_STA_AUTH;
345 	wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
346 	os_free(sta->challenge);
347 	sta->challenge = NULL;
348 
349 	return 0;
350 }
351 #endif /* CONFIG_NO_RC4 */
352 #endif /* CONFIG_WEP */
353 
354 
send_auth_reply(struct hostapd_data * hapd,struct sta_info * sta,const u8 * dst,u16 auth_alg,u16 auth_transaction,u16 resp,const u8 * ies,size_t ies_len,const char * dbg)355 static int send_auth_reply(struct hostapd_data *hapd, struct sta_info *sta,
356 			   const u8 *dst,
357 			   u16 auth_alg, u16 auth_transaction, u16 resp,
358 			   const u8 *ies, size_t ies_len, const char *dbg)
359 {
360 	struct ieee80211_mgmt *reply;
361 	u8 *buf;
362 	size_t rlen;
363 	int reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
364 	const u8 *sa = hapd->own_addr;
365 	struct wpabuf *ml_resp = NULL;
366 	size_t ml_resp_len = 0;
367 #ifdef CONFIG_IEEE8021X_AUTH
368 	size_t mic_len = 0;
369 #endif /* CONFIG_IEEE8021X_AUTH */
370 
371 #ifdef CONFIG_IEEE80211BE
372 	if (ap_sta_is_mld(hapd, sta)) {
373 		ml_resp = hostapd_ml_auth_resp(hapd);
374 		if (!ml_resp)
375 			return -1;
376 		ml_resp_len = wpabuf_len(ml_resp);
377 	}
378 #endif /* CONFIG_IEEE80211BE */
379 
380 	rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len + ml_resp_len;
381 #ifdef CONFIG_IEEE8021X_AUTH
382 	/* Add MIC element for an Authentication frame carrying an EAP-Success
383 	 * message and for an Authentication frame with transaction sequence
384 	 * frame 2, if PMKSA caching was used.
385 	 */
386 	if (auth_alg == WLAN_AUTH_802_1X &&
387 	    (resp == WLAN_STATUS_802_1_X_AUTH_SUCCESS ||
388 	     sta->eap_auth_data.add_mic)) {
389 		mic_len = wpa_mic_len(sta->eap_auth_data.akm,
390 				      sta->eap_auth_data.pmk_len,
391 				      RSN_HASH_NOT_SPECIFIED);
392 		rlen += 2 + mic_len;
393 	}
394 #endif /* CONFIG_IEEE8021X_AUTH */
395 
396 	buf = os_zalloc(rlen);
397 	if (!buf) {
398 		wpabuf_free(ml_resp);
399 		return -1;
400 	}
401 
402 	reply = (struct ieee80211_mgmt *) buf;
403 	reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
404 					    WLAN_FC_STYPE_AUTH);
405 	os_memcpy(reply->da, dst, ETH_ALEN);
406 	os_memcpy(reply->sa, sa, ETH_ALEN);
407 	os_memcpy(reply->bssid, sa, ETH_ALEN);
408 
409 	reply->u.auth.auth_alg = host_to_le16(auth_alg);
410 	reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
411 	reply->u.auth.status_code = host_to_le16(resp);
412 
413 	if (ies && ies_len)
414 		os_memcpy(reply->u.auth.variable, ies, ies_len);
415 
416 #ifdef CONFIG_IEEE80211BE
417 	if (ml_resp)
418 		os_memcpy(reply->u.auth.variable + ies_len,
419 			  wpabuf_head(ml_resp), wpabuf_len(ml_resp));
420 
421 	wpabuf_free(ml_resp);
422 #endif /* CONFIG_IEEE80211BE */
423 
424 	wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
425 		   " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu) (dbg=%s)",
426 		   MAC2STR(dst), auth_alg, auth_transaction,
427 		   resp, (unsigned long) ies_len, dbg);
428 #ifdef CONFIG_TESTING_OPTIONS
429 #ifdef CONFIG_SAE
430 	if (hapd->conf->sae_confirm_immediate == 2 &&
431 	    auth_alg == WLAN_AUTH_SAE) {
432 		if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT && sta &&
433 		    (resp == WLAN_STATUS_SUCCESS ||
434 		     resp == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
435 		     resp == WLAN_STATUS_SAE_PK)) {
436 			wpa_printf(MSG_DEBUG,
437 				   "TESTING: Postpone SAE Commit transmission until Confirm is ready");
438 			os_free(sta->sae_postponed_commit);
439 			sta->sae_postponed_commit = buf;
440 			sta->sae_postponed_commit_len = rlen;
441 			return WLAN_STATUS_SUCCESS;
442 		}
443 
444 		if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_CONFIRM &&
445 		    sta && sta->sae_postponed_commit) {
446 			wpa_printf(MSG_DEBUG,
447 				   "TESTING: Send postponed SAE Commit first, immediately followed by SAE Confirm");
448 			if (hostapd_drv_send_mlme(hapd,
449 						  sta->sae_postponed_commit,
450 						  sta->sae_postponed_commit_len,
451 						  0, NULL, 0, 0) < 0)
452 				wpa_printf(MSG_INFO, "send_auth_reply: send failed");
453 			os_free(sta->sae_postponed_commit);
454 			sta->sae_postponed_commit = NULL;
455 			sta->sae_postponed_commit_len = 0;
456 		}
457 	}
458 #endif /* CONFIG_SAE */
459 #endif /* CONFIG_TESTING_OPTIONS */
460 
461 #ifdef CONFIG_IEEE8021X_AUTH
462 	if (auth_alg == WLAN_AUTH_802_1X &&
463 	    (resp == WLAN_STATUS_802_1_X_AUTH_SUCCESS ||
464 	     sta->eap_auth_data.add_mic)) {
465 		const u8 *frame, *data, *rsne, *rsnxe;
466 		u8 data_buf[500], mic[WPA_1X_MAX_MIC_LEN];
467 		size_t frame_len, data_len;
468 		const u8 *aa = sa;
469 		u8 *ptr = reply->u.auth.variable + ies_len + ml_resp_len;
470 
471 #ifdef CONFIG_IEEE80211BE
472 		if (ap_sta_is_mld(hapd, sta))
473 			aa = hapd->mld->mld_addr;
474 #endif /* CONFIG_IEEE80211BE */
475 
476 		rsne = hostapd_wpa_ie(hapd, WLAN_EID_RSN);
477 		if (!rsne) {
478 			wpa_printf(MSG_INFO, "No AP RSNE");
479 			return -1;
480 		}
481 		os_memcpy(data_buf, rsne, 2 + rsne[1]);
482 		data_len = 2 + rsne[1];
483 
484 		rsnxe = hostapd_wpa_ie(hapd, WLAN_EID_RSNX);
485 		if (rsnxe) {
486 			wpa_printf(MSG_DEBUG, "Found AP RSNXE");
487 			os_memcpy(&data_buf[data_len], rsnxe, 2 + rsnxe[1]);
488 			data_len += 2 + rsnxe[1];
489 		} else {
490 			wpa_printf(MSG_DEBUG, "No AP RSNXE");
491 		}
492 		data = data_buf;
493 
494 		/* MIC element */
495 		ptr[0] = WLAN_EID_MIC;
496 		ptr[1] = mic_len;
497 		os_memset(ptr + 2, 0, mic_len);
498 
499 		frame = (const u8 *) &reply->u.auth.auth_alg;
500 		frame_len =  rlen - IEEE80211_HDRLEN;
501 		if (wpa_auth_8021x_mic(sta->eap_auth_data.akm,
502 				       sta->eap_auth_data.ptk.kck,
503 				       sta->eap_auth_data.ptk.kck_len,
504 				       aa, sta->addr, data, data_len,
505 				       frame, frame_len, mic)) {
506 			wpa_printf(MSG_INFO, "Failed to derive MIC");
507 			return -1;
508 		}
509 		os_memcpy(ptr + 2, mic, mic_len);
510 	}
511 #endif /* CONFIF_IEEE8021X_AUTH */
512 
513 	if (hostapd_drv_send_mlme(hapd, reply, rlen, 0, NULL, 0, 0) < 0)
514 		wpa_printf(MSG_INFO, "send_auth_reply: send failed");
515 	else
516 		reply_res = WLAN_STATUS_SUCCESS;
517 
518 	os_free(buf);
519 
520 	return reply_res;
521 }
522 
523 
524 #ifdef CONFIG_IEEE8021X_AUTH
send_8021x_auth_reply(struct hostapd_data * hapd,struct sta_info * sta,u16 auth_transaction,u16 resp,struct wpabuf * ies)525 static void send_8021x_auth_reply(struct hostapd_data *hapd,
526 				  struct sta_info *sta,
527 				  u16 auth_transaction, u16 resp,
528 				  struct wpabuf *ies)
529 {
530 	send_auth_reply(hapd, sta, sta->addr, WLAN_AUTH_802_1X,
531 			auth_transaction, resp, wpabuf_head(ies),
532 			wpabuf_len(ies), "send-8021x-auth-reply");
533 
534 	if (sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS &&
535 				   resp != WLAN_STATUS_802_1_X_AUTH_SUCCESS)) {
536 		hostapd_drv_sta_remove(hapd, sta->addr);
537 		sta->added_unassoc = 0;
538 	}
539 	wpabuf_free(ies);
540 }
541 #endif /* IEEE8021X_AUTH */
542 
543 
544 #ifdef CONFIG_IEEE80211R_AP
handle_auth_ft_finish(void * ctx,const u8 * dst,u16 auth_transaction,u16 status,const u8 * ies,size_t ies_len)545 static void handle_auth_ft_finish(void *ctx, const u8 *dst,
546 				  u16 auth_transaction, u16 status,
547 				  const u8 *ies, size_t ies_len)
548 {
549 	struct hostapd_data *hapd = ctx;
550 	struct sta_info *sta;
551 	int reply_res;
552 
553 	reply_res = send_auth_reply(hapd, NULL, dst, WLAN_AUTH_FT,
554 				    auth_transaction, status, ies, ies_len,
555 				    "auth-ft-finish");
556 
557 	sta = ap_get_sta(hapd, dst);
558 	if (sta == NULL)
559 		return;
560 
561 	if (sta->added_unassoc && (reply_res != WLAN_STATUS_SUCCESS ||
562 				   status != WLAN_STATUS_SUCCESS)) {
563 		hostapd_drv_sta_remove(hapd, sta->addr);
564 		sta->added_unassoc = 0;
565 		return;
566 	}
567 
568 	if (status != WLAN_STATUS_SUCCESS)
569 		return;
570 
571 	hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
572 		       HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
573 	sta->flags |= WLAN_STA_AUTH;
574 	mlme_authenticate_indication(hapd, sta);
575 }
576 #endif /* CONFIG_IEEE80211R_AP */
577 
578 
579 #ifdef CONFIG_SAE
580 
sae_set_state(struct sta_info * sta,enum sae_state state,const char * reason)581 static void sae_set_state(struct sta_info *sta, enum sae_state state,
582 			  const char *reason)
583 {
584 	wpa_printf(MSG_DEBUG, "SAE: State %s -> %s for peer " MACSTR " (%s)",
585 		   sae_state_txt(sta->sae->state), sae_state_txt(state),
586 		   MAC2STR(sta->addr), reason);
587 	sta->sae->state = state;
588 }
589 
590 
in_mac_addr_list(const u8 * list,unsigned int num,const u8 * addr)591 static bool in_mac_addr_list(const u8 *list, unsigned int num, const u8 *addr)
592 {
593 	unsigned int i;
594 
595 	for (i = 0; list && i < num; i++) {
596 		if (ether_addr_equal(&list[i * ETH_ALEN], addr))
597 			return true;
598 	}
599 
600 	return false;
601 }
602 
603 
604 static struct sae_password_entry *
sae_password_find_pw(struct hostapd_data * hapd,struct sta_info * sta)605 sae_password_find_pw(struct hostapd_data *hapd, struct sta_info *sta)
606 {
607 	struct sae_password_entry *pw = NULL;
608 
609 	if (!sta->sae || !sta->sae->tmp || !sta->sae->tmp->used_pw)
610 		return NULL;
611 
612 
613 	for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
614 		if (pw == sta->sae->tmp->used_pw)
615 			return pw;
616 	}
617 
618 	return NULL;
619 }
620 
621 
is_other_sae_password(struct hostapd_data * hapd,struct sta_info * sta,struct sae_password_entry * used_pw)622 static bool is_other_sae_password(struct hostapd_data *hapd,
623 				  struct sta_info *sta,
624 				  struct sae_password_entry *used_pw)
625 {
626 	struct sae_password_entry *pw;
627 
628 	for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
629 		if (pw == used_pw ||
630 		    pw->identifier ||
631 		    !is_broadcast_ether_addr(pw->peer_addr))
632 			continue;
633 
634 		if (in_mac_addr_list(pw->success_mac,
635 				     pw->num_success_mac,
636 				     sta->addr))
637 			return true;
638 
639 		if (!in_mac_addr_list(pw->fail_mac, pw->num_fail_mac,
640 				      sta->addr))
641 			return true;
642 	}
643 
644 	return false;
645 }
646 
647 
has_sae_success_seen(struct hostapd_data * hapd,struct sta_info * sta)648 static bool has_sae_success_seen(struct hostapd_data *hapd,
649 				 struct sta_info *sta)
650 {
651 	struct sae_password_entry *pw;
652 
653 	for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
654 		if (pw->identifier ||
655 		    !is_broadcast_ether_addr(pw->peer_addr))
656 			continue;
657 
658 		if (in_mac_addr_list(pw->success_mac,
659 				     pw->num_success_mac,
660 				     sta->addr))
661 			return true;
662 	}
663 
664 	return false;
665 }
666 
667 
sae_password_mark_success(struct hostapd_data * hapd,struct sae_password_entry * pw,const u8 * addr)668 static int sae_password_mark_success(struct hostapd_data *hapd,
669 				      struct sae_password_entry *pw,
670 				      const u8 *addr)
671 {
672 	if (in_mac_addr_list(pw->success_mac, pw->num_success_mac, addr))
673 		return 0;
674 
675 	if (!pw->success_mac) {
676 		pw->success_mac = os_zalloc(hapd->conf->sae_track_password *
677 					    ETH_ALEN);
678 		if (!pw->success_mac)
679 			return -1;
680 		pw->num_success_mac = hapd->conf->sae_track_password;
681 	}
682 
683 	os_memcpy(&pw->success_mac[pw->next_success_mac * ETH_ALEN], addr,
684 		  ETH_ALEN);
685 	pw->next_success_mac = (pw->next_success_mac + 1) % pw->num_success_mac;
686 	return 0;
687 }
688 
689 
sae_password_track_success(struct hostapd_data * hapd,struct sta_info * sta)690 static void sae_password_track_success(struct hostapd_data *hapd,
691 				       struct sta_info *sta)
692 {
693 	struct sae_password_entry *pw;
694 
695 	if (!hapd->conf->sae_track_password)
696 		return;
697 
698 	pw = sae_password_find_pw(hapd, sta);
699 	if (!pw)
700 		return;
701 
702 	sae_password_mark_success(hapd, pw, sta->addr);
703 }
704 
705 
sae_password_track_fail(struct hostapd_data * hapd,struct sta_info * sta)706 static bool sae_password_track_fail(struct hostapd_data *hapd,
707 				    struct sta_info *sta)
708 {
709 	struct sae_password_entry *pw;
710 
711 	if (!hapd->conf->sae_track_password)
712 		return false;
713 
714 	pw = sae_password_find_pw(hapd, sta);
715 	if (!pw)
716 		return false;
717 
718 	if (in_mac_addr_list(pw->fail_mac,
719 			     pw->num_fail_mac,
720 			     sta->addr))
721 		return is_other_sae_password(hapd, sta, pw);
722 
723 	if (!pw->fail_mac) {
724 		pw->fail_mac = os_zalloc(hapd->conf->sae_track_password *
725 					 ETH_ALEN);
726 		if (!pw->fail_mac)
727 			return false;
728 		pw->num_fail_mac = hapd->conf->sae_track_password;
729 	}
730 
731 	os_memcpy(&pw->fail_mac[pw->next_fail_mac * ETH_ALEN], sta->addr,
732 		  ETH_ALEN);
733 	pw->next_fail_mac = (pw->next_fail_mac + 1) % pw->num_fail_mac;
734 
735 	return is_other_sae_password(hapd, sta, pw);
736 }
737 
738 
sae_password_bind(struct hostapd_data * hapd,const u8 * addr,const char * password)739 int sae_password_bind(struct hostapd_data *hapd, const u8 *addr,
740 		      const char *password)
741 {
742 	struct sae_password_entry *pw;
743 
744 	if (!hapd->conf->sae_track_password)
745 		return -1;
746 
747 	for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
748 		if (pw->identifier ||
749 		    !is_broadcast_ether_addr(pw->peer_addr) ||
750 		    os_strcmp(password, pw->password) != 0)
751 			continue;
752 
753 		return sae_password_mark_success(hapd, pw, addr);
754 	}
755 
756 	return -1;
757 }
758 
759 
sae_get_password(struct hostapd_data * hapd,struct sta_info * sta,const u8 * rx_id,size_t rx_id_len,struct sae_password_entry ** pw_entry,struct sae_pt ** s_pt,const struct sae_pk ** s_pk)760 const char * sae_get_password(struct hostapd_data *hapd,
761 			      struct sta_info *sta,
762 			      const u8 *rx_id, size_t rx_id_len,
763 			      struct sae_password_entry **pw_entry,
764 			      struct sae_pt **s_pt,
765 			      const struct sae_pk **s_pk)
766 {
767 	const char *password = NULL;
768 	struct sae_password_entry *pw;
769 	struct sae_pt *pt = NULL;
770 	const struct sae_pk *pk = NULL;
771 	struct hostapd_sta_wpa_psk_short *psk = NULL;
772 
773 	/* With sae_track_password functionality enabled, try to first find the
774 	 * next viable wildcard-address password if a password identifier was
775 	 * not used. Select an wildcard-addr entry if the STA is known to have
776 	 * used it successfully before. If no such entry exists, pick a
777 	 * wildcard-addr entry that does not have a failed entry tracked for the
778 	 * STA. */
779 	if (!rx_id && sta && hapd->conf->sae_track_password) {
780 		struct sae_password_entry *success = NULL, *no_fail = NULL;
781 
782 		for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
783 			if (pw->identifier ||
784 			    !is_broadcast_ether_addr(pw->peer_addr))
785 				continue;
786 			if (in_mac_addr_list(pw->success_mac,
787 					     pw->num_success_mac,
788 					     sta->addr)) {
789 				success = pw;
790 				break;
791 			}
792 
793 			if (!no_fail &&
794 			    !in_mac_addr_list(pw->fail_mac, pw->num_fail_mac,
795 					      sta->addr))
796 				no_fail = pw;
797 		}
798 
799 		pw = success ? success : no_fail;
800 		if (pw) {
801 			password = pw->password;
802 			pt = pw->pt;
803 			if (!(hapd->conf->mesh & MESH_ENABLED))
804 				pk = pw->pk;
805 			goto found;
806 		}
807 	}
808 
809 	/* If sae_track_password functionality is not enabled or no suitable
810 	 * password entry was found with it, pick the first entry that matches
811 	 * the STA MAC address and password identifier (if used). */
812 	for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
813 		if (!is_broadcast_ether_addr(pw->peer_addr) &&
814 		    (!sta ||
815 		     !ether_addr_equal(pw->peer_addr, sta->addr)))
816 			continue;
817 		if ((rx_id && !pw->identifier) || (!rx_id && pw->identifier))
818 			continue;
819 		if (rx_id && pw->identifier &&
820 		    (rx_id_len != os_strlen(pw->identifier) ||
821 		     os_memcmp(rx_id, pw->identifier, rx_id_len) != 0))
822 			continue;
823 		password = pw->password;
824 		pt = pw->pt;
825 		if (!(hapd->conf->mesh & MESH_ENABLED))
826 			pk = pw->pk;
827 		break;
828 	}
829 	if (!password && !rx_id && !hapd->conf->sae_password_psk) {
830 		password = hapd->conf->ssid.wpa_passphrase;
831 		pt = hapd->conf->ssid.pt;
832 	}
833 
834 	if (!password && sta && !rx_id) {
835 		for (psk = sta->psk; psk; psk = psk->next) {
836 			if (psk->is_passphrase) {
837 				password = psk->passphrase;
838 				break;
839 			}
840 		}
841 	}
842 
843 	/* Try to decrypt the received password identifier if no plaintext
844 	 * identifier match was found. */
845 	if (!password && rx_id && rx_id_len > 4 + 4 + AES_BLOCK_SIZE &&
846 	    hapd->conf->sae_pw_id_key) {
847 		u8 *plain, *pos, *counter;
848 		size_t plain_len;
849 		const u8 *id;
850 		size_t id_len;
851 
852 		plain = os_malloc(rx_id_len);
853 		if (!plain)
854 			goto fail;
855 		if (aes_siv_decrypt(
856 			    wpabuf_head(hapd->conf->sae_pw_id_key),
857 			    wpabuf_len(hapd->conf->sae_pw_id_key),
858 			    rx_id, rx_id_len, 0, NULL, NULL, plain) < 0)
859 			goto fail;
860 		plain_len = rx_id_len - AES_BLOCK_SIZE;
861 		wpa_hexdump_ascii(MSG_DEBUG,
862 				  "SAE: Decrypted password identifier info",
863 				  plain, plain_len);
864 		/* 4 octet date | Password ID | <padding> | 4 octet counter */
865 		counter = plain + plain_len - 4;
866 		wpa_printf(MSG_DEBUG, "SAE: Generation time %u counter %u",
867 			   WPA_GET_BE32(plain), WPA_GET_BE32(counter));
868 		id = pos = plain + 4;
869 		while (pos < counter) {
870 			if (*pos == 0x00)
871 				break;
872 			pos++;
873 		}
874 		id_len = pos - id;
875 		wpa_hexdump_ascii(MSG_DEBUG,
876 				  "SAE: Decrypted password identifier",
877 				  id, id_len);
878 		for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
879 			if (!is_broadcast_ether_addr(pw->peer_addr) &&
880 			    (!sta ||
881 			     !ether_addr_equal(pw->peer_addr, sta->addr)))
882 				continue;
883 			if (!pw->identifier ||
884 			    os_strlen(pw->identifier) != id_len ||
885 			    os_memcmp(id, pw->identifier, id_len) != 0)
886 				continue;
887 			password = pw->password;
888 			if (!(hapd->conf->mesh & MESH_ENABLED))
889 				pk = pw->pk;
890 			if (sta && sta->sae && sta->sae->tmp) {
891 				os_free(sta->sae->tmp->dec_pw_id);
892 				sta->sae->tmp->dec_pw_id =
893 					os_zalloc(id_len + 1);
894 				if (sta->sae->tmp->dec_pw_id) {
895 					os_memcpy(sta->sae->tmp->dec_pw_id,
896 						  id, id_len);
897 					sta->sae->tmp->dec_pw_id_len = id_len;
898 					sta->sae->tmp->pw_id_counter =
899 						WPA_GET_BE32(counter);
900 					wpa_printf(MSG_DEBUG,
901 						   "SAE: Bound decrypted password identifier to STA");
902 				}
903 			}
904 			break;
905 		}
906 	fail:
907 		os_free(plain);
908 	}
909 
910 found:
911 	if (pw_entry)
912 		*pw_entry = pw;
913 	if (s_pt)
914 		*s_pt = pt;
915 	if (s_pk)
916 		*s_pk = pk;
917 
918 	return password;
919 }
920 
921 
auth_build_sae_commit(struct hostapd_data * hapd,struct sta_info * sta,int update,int status_code)922 static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
923 					     struct sta_info *sta, int update,
924 					     int status_code)
925 {
926 	struct wpabuf *buf;
927 	const char *password = NULL;
928 	struct sae_password_entry *pw;
929 	const u8 *rx_id = NULL;
930 	size_t rx_id_len = 0;
931 	int use_pt = 0;
932 	struct sae_pt *pt = NULL;
933 	const struct sae_pk *pk = NULL;
934 	const u8 *own_addr = hapd->own_addr;
935 
936 #ifdef CONFIG_IEEE80211BE
937 	if (ap_sta_is_mld(hapd, sta))
938 		own_addr = hapd->mld->mld_addr;
939 #endif /* CONFIG_IEEE80211BE */
940 
941 	if (sta->sae->tmp) {
942 		rx_id = sta->sae->tmp->parsed_pw_id ?
943 			sta->sae->tmp->parsed_pw_id : sta->sae->tmp->pw_id;
944 		rx_id_len = sta->sae->tmp->parsed_pw_id ?
945 			sta->sae->tmp->parsed_pw_id_len :
946 			sta->sae->tmp->pw_id_len;
947 		use_pt = sta->sae->h2e;
948 #ifdef CONFIG_SAE_PK
949 		os_memcpy(sta->sae->tmp->own_addr, own_addr, ETH_ALEN);
950 		os_memcpy(sta->sae->tmp->peer_addr, sta->addr, ETH_ALEN);
951 #endif /* CONFIG_SAE_PK */
952 	}
953 
954 	if (rx_id && hapd->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
955 		use_pt = 1;
956 	else if (status_code == WLAN_STATUS_SUCCESS)
957 		use_pt = 0;
958 	else if (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
959 		 status_code == WLAN_STATUS_SAE_PK)
960 		use_pt = 1;
961 
962 	password = sae_get_password(hapd, sta, rx_id, rx_id_len, &pw, &pt, &pk);
963 	if (!password) {
964 		wpa_printf(MSG_DEBUG, "SAE: No password available");
965 		return NULL;
966 	}
967 
968 	if (use_pt) {
969 		struct sae_pt *tmp_pt = NULL;
970 		bool failed = false;
971 
972 		if (!pt && pw) {
973 			int groups[2] = { sta->sae->group, 0 };
974 
975 			wpa_printf(MSG_DEBUG,
976 				   "SAE: Derive PT for encrypted PW ID");
977 			tmp_pt = sae_derive_pt(groups, hapd->conf->ssid.ssid,
978 					       hapd->conf->ssid.ssid_len,
979 					       (const u8 *) pw->password,
980 					       os_strlen(pw->password),
981 					       rx_id, rx_id_len);
982 			if (!tmp_pt) {
983 				wpa_printf(MSG_DEBUG,
984 					   "SAE: Could not derive PT");
985 				return NULL;
986 			}
987 			pt = tmp_pt;
988 			update = 1;
989 		}
990 
991 		if (!pt) {
992 			wpa_printf(MSG_DEBUG, "SAE: No PT available");
993 			return NULL;
994 		}
995 
996 		if (update &&
997 		    sae_prepare_commit_pt(sta->sae, pt, own_addr, sta->addr,
998 					  NULL, pk) < 0)
999 			failed = true;
1000 
1001 		sae_deinit_pt(tmp_pt);
1002 		if (failed)
1003 			return NULL;
1004 	}
1005 
1006 	if (update && !use_pt &&
1007 	    sae_prepare_commit(own_addr, sta->addr,
1008 			       (u8 *) password, os_strlen(password),
1009 			       sta->sae) < 0) {
1010 		wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
1011 		return NULL;
1012 	}
1013 
1014 	if (pw && sta->sae->tmp)
1015 		sta->sae->tmp->used_pw = pw;
1016 
1017 	if (pw && pw->vlan_id) {
1018 		if (!sta->sae->tmp) {
1019 			wpa_printf(MSG_INFO,
1020 				   "SAE: No temporary data allocated - cannot store VLAN ID");
1021 			return NULL;
1022 		}
1023 		sta->sae->tmp->vlan_id = pw->vlan_id;
1024 	}
1025 
1026 	buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN +
1027 			   (rx_id ? 3 + rx_id_len : 0));
1028 	if (buf &&
1029 	    sae_write_commit(sta->sae, buf, sta->sae->tmp ?
1030 			     sta->sae->tmp->anti_clogging_token : NULL,
1031 			     rx_id, rx_id_len) < 0) {
1032 		wpabuf_free(buf);
1033 		buf = NULL;
1034 	}
1035 
1036 	return buf;
1037 }
1038 
1039 
auth_build_sae_confirm(struct hostapd_data * hapd,struct sta_info * sta)1040 static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
1041 					      struct sta_info *sta)
1042 {
1043 	struct wpabuf *buf;
1044 
1045 	buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
1046 	if (buf == NULL)
1047 		return NULL;
1048 
1049 #ifdef CONFIG_SAE_PK
1050 #ifdef CONFIG_TESTING_OPTIONS
1051 	if (sta->sae->tmp)
1052 		sta->sae->tmp->omit_pk_elem = hapd->conf->sae_pk_omit;
1053 #endif /* CONFIG_TESTING_OPTIONS */
1054 #endif /* CONFIG_SAE_PK */
1055 
1056 	if (sae_write_confirm(sta->sae, buf) < 0) {
1057 		wpabuf_free(buf);
1058 		return NULL;
1059 	}
1060 
1061 	return buf;
1062 }
1063 
1064 
auth_sae_send_commit(struct hostapd_data * hapd,struct sta_info * sta,int update,int status_code)1065 static int auth_sae_send_commit(struct hostapd_data *hapd,
1066 				struct sta_info *sta,
1067 				int update, int status_code)
1068 {
1069 	struct wpabuf *data;
1070 	int reply_res;
1071 	u16 status;
1072 
1073 	data = auth_build_sae_commit(hapd, sta, update, status_code);
1074 	if (!data && sta->sae->tmp &&
1075 	    (sta->sae->tmp->pw_id || sta->sae->tmp->parsed_pw_id))
1076 		return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1077 	if (data == NULL)
1078 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1079 
1080 	if (sta->sae->tmp && sta->sae->pk)
1081 		status = WLAN_STATUS_SAE_PK;
1082 	else if (sta->sae->tmp && sta->sae->h2e)
1083 		status = WLAN_STATUS_SAE_HASH_TO_ELEMENT;
1084 	else
1085 		status = WLAN_STATUS_SUCCESS;
1086 #ifdef CONFIG_TESTING_OPTIONS
1087 	if (hapd->conf->sae_commit_status >= 0 &&
1088 	    hapd->conf->sae_commit_status != status) {
1089 		wpa_printf(MSG_INFO,
1090 			   "TESTING: Override SAE commit status code %u --> %d",
1091 			   status, hapd->conf->sae_commit_status);
1092 		status = hapd->conf->sae_commit_status;
1093 	}
1094 #endif /* CONFIG_TESTING_OPTIONS */
1095 	reply_res = send_auth_reply(hapd, sta, sta->addr,
1096 				    WLAN_AUTH_SAE, 1,
1097 				    status, wpabuf_head(data),
1098 				    wpabuf_len(data), "sae-send-commit");
1099 
1100 	wpabuf_free(data);
1101 
1102 	return reply_res;
1103 }
1104 
1105 
auth_sae_send_confirm(struct hostapd_data * hapd,struct sta_info * sta)1106 static int auth_sae_send_confirm(struct hostapd_data *hapd,
1107 				 struct sta_info *sta)
1108 {
1109 	struct wpabuf *data;
1110 	int reply_res;
1111 
1112 	data = auth_build_sae_confirm(hapd, sta);
1113 	if (data == NULL)
1114 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1115 
1116 	reply_res = send_auth_reply(hapd, sta, sta->addr,
1117 				    WLAN_AUTH_SAE, 2,
1118 				    WLAN_STATUS_SUCCESS, wpabuf_head(data),
1119 				    wpabuf_len(data), "sae-send-confirm");
1120 
1121 	wpabuf_free(data);
1122 
1123 	return reply_res;
1124 }
1125 
1126 #endif /* CONFIG_SAE */
1127 
1128 
1129 #if defined(CONFIG_SAE) || defined(CONFIG_PASN)
1130 
use_anti_clogging(struct hostapd_data * hapd)1131 static int use_anti_clogging(struct hostapd_data *hapd)
1132 {
1133 	struct sta_info *sta;
1134 	unsigned int open = 0;
1135 
1136 	if (hapd->conf->anti_clogging_threshold == 0)
1137 		return 1;
1138 
1139 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1140 #ifdef CONFIG_SAE
1141 		if (sta->sae &&
1142 		    (sta->sae->state == SAE_COMMITTED ||
1143 		     sta->sae->state == SAE_CONFIRMED))
1144 			open++;
1145 #endif /* CONFIG_SAE */
1146 #ifdef CONFIG_PASN
1147 		if (sta->pasn && sta->pasn->ecdh)
1148 			open++;
1149 #endif /* CONFIG_PASN */
1150 		if (open >= hapd->conf->anti_clogging_threshold)
1151 			return 1;
1152 	}
1153 
1154 #ifdef CONFIG_SAE
1155 	/* In addition to already existing open SAE sessions, check whether
1156 	 * there are enough pending commit messages in the processing queue to
1157 	 * potentially result in too many open sessions. */
1158 	if (open + dl_list_len(&hapd->sae_commit_queue) >=
1159 	    hapd->conf->anti_clogging_threshold)
1160 		return 1;
1161 #endif /* CONFIG_SAE */
1162 
1163 	return 0;
1164 }
1165 
1166 #endif /* defined(CONFIG_SAE) || defined(CONFIG_PASN) */
1167 
1168 
1169 #ifdef CONFIG_SAE
1170 
sae_check_big_sync(struct hostapd_data * hapd,struct sta_info * sta)1171 static int sae_check_big_sync(struct hostapd_data *hapd, struct sta_info *sta)
1172 {
1173 	if (sta->sae->sync > hapd->conf->sae_sync) {
1174 		sae_set_state(sta, SAE_NOTHING, "Sync > dot11RSNASAESync");
1175 		sta->sae->sync = 0;
1176 		if (sta->sae->tmp) {
1177 			/* Disable this SAE instance for 10 seconds to avoid
1178 			 * unnecessary flood of multiple SAE commits in
1179 			 * unexpected mesh cases. */
1180 			if (os_get_reltime(&sta->sae->tmp->disabled_until) == 0)
1181 				sta->sae->tmp->disabled_until.sec += 10;
1182 		}
1183 		return -1;
1184 	}
1185 	return 0;
1186 }
1187 
1188 
sae_proto_instance_disabled(struct sta_info * sta)1189 static bool sae_proto_instance_disabled(struct sta_info *sta)
1190 {
1191 	struct sae_temporary_data *tmp;
1192 
1193 	if (!sta->sae)
1194 		return false;
1195 	tmp = sta->sae->tmp;
1196 	if (!tmp)
1197 		return false;
1198 
1199 	if (os_reltime_initialized(&tmp->disabled_until)) {
1200 		struct os_reltime now;
1201 
1202 		os_get_reltime(&now);
1203 		if (os_reltime_before(&now, &tmp->disabled_until))
1204 			return true;
1205 	}
1206 
1207 	return false;
1208 }
1209 
1210 
auth_sae_retransmit_timer(void * eloop_ctx,void * eloop_data)1211 static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
1212 {
1213 	struct hostapd_data *hapd = eloop_ctx;
1214 	struct sta_info *sta = eloop_data;
1215 	int ret;
1216 
1217 	if (sae_check_big_sync(hapd, sta))
1218 		return;
1219 	sta->sae->sync++;
1220 	wpa_printf(MSG_DEBUG, "SAE: Auth SAE retransmit timer for " MACSTR
1221 		   " (sync=%d state=%s)",
1222 		   MAC2STR(sta->addr), sta->sae->sync,
1223 		   sae_state_txt(sta->sae->state));
1224 
1225 	switch (sta->sae->state) {
1226 	case SAE_COMMITTED:
1227 		ret = auth_sae_send_commit(hapd, sta, 0, -1);
1228 		eloop_register_timeout(0,
1229 				       hapd->dot11RSNASAERetransPeriod * 1000,
1230 				       auth_sae_retransmit_timer, hapd, sta);
1231 		break;
1232 	case SAE_CONFIRMED:
1233 		ret = auth_sae_send_confirm(hapd, sta);
1234 		eloop_register_timeout(0,
1235 				       hapd->dot11RSNASAERetransPeriod * 1000,
1236 				       auth_sae_retransmit_timer, hapd, sta);
1237 		break;
1238 	default:
1239 		ret = -1;
1240 		break;
1241 	}
1242 
1243 	if (ret != WLAN_STATUS_SUCCESS)
1244 		wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
1245 }
1246 
1247 
sae_clear_retransmit_timer(struct hostapd_data * hapd,struct sta_info * sta)1248 void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
1249 {
1250 	eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
1251 }
1252 
1253 
sae_set_retransmit_timer(struct hostapd_data * hapd,struct sta_info * sta)1254 static void sae_set_retransmit_timer(struct hostapd_data *hapd,
1255 				     struct sta_info *sta)
1256 {
1257 	if (!(hapd->conf->mesh & MESH_ENABLED))
1258 		return;
1259 
1260 	eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
1261 	eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
1262 			       auth_sae_retransmit_timer, hapd, sta);
1263 }
1264 
1265 
sae_sme_send_external_auth_status(struct hostapd_data * hapd,struct sta_info * sta,u16 status)1266 static void sae_sme_send_external_auth_status(struct hostapd_data *hapd,
1267 					      struct sta_info *sta, u16 status)
1268 {
1269 	struct external_auth params;
1270 
1271 	os_memset(&params, 0, sizeof(params));
1272 	params.status = status;
1273 
1274 #ifdef CONFIG_IEEE80211BE
1275 	if (ap_sta_is_mld(hapd, sta))
1276 		params.bssid =
1277 			sta->mld_info.links[sta->mld_assoc_link_id].peer_addr;
1278 #endif /* CONFIG_IEEE80211BE */
1279 	if (!params.bssid)
1280 		params.bssid = sta->addr;
1281 
1282 	if (status == WLAN_STATUS_SUCCESS && sta->sae &&
1283 	    !hapd->conf->disable_pmksa_caching)
1284 		params.pmkid = sta->sae->pmkid;
1285 
1286 	hostapd_drv_send_external_auth_status(hapd, &params);
1287 }
1288 
1289 
sae_assign_vlan(struct hostapd_data * hapd,struct sta_info * sta,int vlan_id)1290 static int sae_assign_vlan(struct hostapd_data *hapd, struct sta_info *sta,
1291 			   int vlan_id)
1292 {
1293 #ifndef CONFIG_NO_VLAN
1294 	struct vlan_description vlan_desc;
1295 
1296 	if (vlan_id > 0) {
1297 		wpa_printf(MSG_DEBUG, "SAE: Assign STA " MACSTR
1298 			   " to VLAN ID %d",
1299 			   MAC2STR(sta->addr), vlan_id);
1300 
1301 		if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
1302 			os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1303 			vlan_desc.notempty = 1;
1304 			vlan_desc.untagged = vlan_id;
1305 			if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
1306 				wpa_printf(MSG_INFO,
1307 					   "Invalid VLAN ID %d in sae_password",
1308 					   vlan_id);
1309 				return -1;
1310 			}
1311 
1312 			if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0 ||
1313 			    ap_sta_bind_vlan(hapd, sta) < 0) {
1314 				wpa_printf(MSG_INFO,
1315 					   "Failed to assign VLAN ID %d from sae_password to "
1316 					   MACSTR, vlan_id,
1317 					   MAC2STR(sta->addr));
1318 				return -1;
1319 			}
1320 		} else {
1321 			sta->vlan_id = vlan_id;
1322 		}
1323 	}
1324 #endif /* CONFIG_NO_VLAN */
1325 
1326 	return 0;
1327 }
1328 
1329 
sae_accept_sta(struct hostapd_data * hapd,struct sta_info * sta)1330 void sae_accept_sta(struct hostapd_data *hapd, struct sta_info *sta)
1331 {
1332 	if (sta->sae->tmp &&
1333 	    sae_assign_vlan(hapd, sta, sta->sae->tmp->vlan_id) < 0)
1334 		return;
1335 
1336 	sta->flags |= WLAN_STA_AUTH;
1337 	sta->auth_alg = WLAN_AUTH_SAE;
1338 	mlme_authenticate_indication(hapd, sta);
1339 	wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1340 	sae_set_state(sta, SAE_ACCEPTED, "Accept Confirm");
1341 	crypto_bignum_deinit(sta->sae->peer_commit_scalar_accepted, 0);
1342 	sta->sae->peer_commit_scalar_accepted = sta->sae->peer_commit_scalar;
1343 	sta->sae->peer_commit_scalar = NULL;
1344 	wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
1345 			       sta->sae->pmk, sta->sae->pmk_len,
1346 			       sta->sae->pmkid, sta->sae->akmp,
1347 			       ap_sta_is_mld(hapd, sta), sta->vlan_id);
1348 	sae_sme_send_external_auth_status(hapd, sta, WLAN_STATUS_SUCCESS);
1349 	if (sta->sae->tmp) {
1350 		struct sae_temporary_data *tmp = sta->sae->tmp;
1351 
1352 		wpabuf_free(sta->sae_pw_id);
1353 		sta->sae_pw_id = NULL;
1354 		if (tmp->dec_pw_id) {
1355 			sta->sae_pw_id = wpabuf_alloc_copy(
1356 				tmp->dec_pw_id, tmp->dec_pw_id_len);
1357 			sta->sae_pw_id_counter = tmp->pw_id_counter;
1358 		} else if (tmp->pw_id) {
1359 			sta->sae_pw_id = wpabuf_alloc_copy(
1360 				tmp->pw_id, tmp->pw_id_len);
1361 		}
1362 	}
1363 }
1364 
1365 
sae_sm_step(struct hostapd_data * hapd,struct sta_info * sta,u16 auth_transaction,u16 status_code,int allow_reuse,int * sta_removed)1366 static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
1367 		       u16 auth_transaction, u16 status_code,
1368 		       int allow_reuse, int *sta_removed)
1369 {
1370 	int ret;
1371 
1372 	*sta_removed = 0;
1373 
1374 	if (auth_transaction != WLAN_AUTH_TR_SEQ_SAE_COMMIT &&
1375 	    auth_transaction != WLAN_AUTH_TR_SEQ_SAE_CONFIRM)
1376 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1377 
1378 	wpa_printf(MSG_DEBUG, "SAE: Peer " MACSTR " state=%s auth_trans=%u",
1379 		   MAC2STR(sta->addr), sae_state_txt(sta->sae->state),
1380 		   auth_transaction);
1381 
1382 	if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT &&
1383 	    sae_proto_instance_disabled(sta)) {
1384 		wpa_printf(MSG_DEBUG,
1385 			   "SAE: Protocol instance temporarily disabled - discard received SAE commit");
1386 		return WLAN_STATUS_SUCCESS;
1387 	}
1388 
1389 	switch (sta->sae->state) {
1390 	case SAE_NOTHING:
1391 		if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1392 			struct sae_temporary_data *tmp = sta->sae->tmp;
1393 			bool immediate_confirm;
1394 
1395 			if (tmp) {
1396 				sta->sae->h2e =
1397 					(status_code ==
1398 					 WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1399 					 status_code == WLAN_STATUS_SAE_PK);
1400 				sta->sae->pk =
1401 					status_code == WLAN_STATUS_SAE_PK;
1402 			}
1403 			ret = auth_sae_send_commit(hapd, sta,
1404 						   !allow_reuse, status_code);
1405 			if (ret == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER)
1406 				wpa_msg(hapd->msg_ctx, MSG_INFO,
1407 					WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER
1408 					MACSTR, MAC2STR(sta->addr));
1409 			if (ret)
1410 				return ret;
1411 
1412 			if (tmp && tmp->parsed_pw_id && !tmp->pw_id) {
1413 				tmp->pw_id = tmp->parsed_pw_id;
1414 				tmp->pw_id_len = tmp->parsed_pw_id_len;
1415 				tmp->parsed_pw_id = NULL;
1416 				tmp->parsed_pw_id_len = 0;
1417 				wpa_hexdump_ascii(MSG_DEBUG,
1418 						  "SAE: Known Password Identifier bound to this STA",
1419 						  tmp->pw_id, tmp->pw_id_len);
1420 			}
1421 
1422 			sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
1423 
1424 			if (sae_process_commit(sta->sae) < 0)
1425 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
1426 
1427 			/*
1428 			 * In mesh case, both Commit and Confirm are sent
1429 			 * immediately. In infrastructure BSS, by default, only
1430 			 * a single Authentication frame (Commit) is expected
1431 			 * from the AP here and the second one (Confirm) will
1432 			 * be sent once the STA has sent its second
1433 			 * Authentication frame (Confirm). This behavior can be
1434 			 * overridden with explicit configuration so that the
1435 			 * infrastructure BSS case sends both frames together.
1436 			 */
1437 			immediate_confirm = (hapd->conf->mesh & MESH_ENABLED) ||
1438 				hapd->conf->sae_confirm_immediate;
1439 
1440 			/* If sae_track_password is enabled and the STA has not
1441 			 * yet been tracked to having successfully completed
1442 			 * SAE authentication with the password that the AP
1443 			 * tries to use, do not send Confirm immediately to
1444 			 * avoid an explicit indication on the STA side on
1445 			 * password mismatch. */
1446 			if (immediate_confirm &&
1447 			    hapd->conf->sae_track_password &&
1448 			    (!sta->sae->tmp || !sta->sae->tmp->parsed_pw_id) &&
1449 			    !has_sae_success_seen(hapd, sta))
1450 				immediate_confirm = false;
1451 
1452 			if (immediate_confirm) {
1453 				/*
1454 				 * Send both Commit and Confirm immediately
1455 				 * based on SAE finite state machine
1456 				 * Nothing -> Confirm transition.
1457 				 */
1458 				ret = auth_sae_send_confirm(hapd, sta);
1459 				if (ret)
1460 					return ret;
1461 				sae_set_state(sta, SAE_CONFIRMED,
1462 					      "Sent Confirm (mesh)");
1463 			} else {
1464 				/*
1465 				 * For infrastructure BSS, send only the Commit
1466 				 * message now to get alternating sequence of
1467 				 * Authentication frames between the AP and STA.
1468 				 * Confirm will be sent in
1469 				 * Committed -> Confirmed/Accepted transition
1470 				 * when receiving Confirm from STA.
1471 				 */
1472 			}
1473 			sta->sae->sync = 0;
1474 			sae_set_retransmit_timer(hapd, sta);
1475 		} else {
1476 			hostapd_logger(hapd, sta->addr,
1477 				       HOSTAPD_MODULE_IEEE80211,
1478 				       HOSTAPD_LEVEL_DEBUG,
1479 				       "SAE confirm before commit");
1480 		}
1481 		break;
1482 	case SAE_COMMITTED:
1483 		sae_clear_retransmit_timer(hapd, sta);
1484 		if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1485 			if (sae_process_commit(sta->sae) < 0)
1486 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
1487 
1488 			ret = auth_sae_send_confirm(hapd, sta);
1489 			if (ret)
1490 				return ret;
1491 			sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
1492 			sta->sae->sync = 0;
1493 			sae_set_retransmit_timer(hapd, sta);
1494 		} else if (hapd->conf->mesh & MESH_ENABLED) {
1495 			/*
1496 			 * In mesh case, follow SAE finite state machine and
1497 			 * send Commit now, if sync count allows.
1498 			 */
1499 			if (sae_check_big_sync(hapd, sta))
1500 				return WLAN_STATUS_SUCCESS;
1501 			sta->sae->sync++;
1502 
1503 			ret = auth_sae_send_commit(hapd, sta, 0, status_code);
1504 			if (ret)
1505 				return ret;
1506 
1507 			sae_set_retransmit_timer(hapd, sta);
1508 		} else {
1509 			/*
1510 			 * For instructure BSS, send the postponed Confirm from
1511 			 * Nothing -> Confirmed transition that was reduced to
1512 			 * Nothing -> Committed above.
1513 			 */
1514 			ret = auth_sae_send_confirm(hapd, sta);
1515 			if (ret)
1516 				return ret;
1517 
1518 			sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
1519 
1520 			/*
1521 			 * Since this was triggered on Confirm RX, run another
1522 			 * step to get to Accepted without waiting for
1523 			 * additional events.
1524 			 */
1525 			return sae_sm_step(hapd, sta, auth_transaction,
1526 					   WLAN_STATUS_SUCCESS, 0, sta_removed);
1527 		}
1528 		break;
1529 	case SAE_CONFIRMED:
1530 		sae_clear_retransmit_timer(hapd, sta);
1531 		if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1532 			if (sae_check_big_sync(hapd, sta))
1533 				return WLAN_STATUS_SUCCESS;
1534 			sta->sae->sync++;
1535 
1536 			ret = auth_sae_send_commit(hapd, sta, 1, status_code);
1537 			if (ret)
1538 				return ret;
1539 
1540 			if (sae_process_commit(sta->sae) < 0)
1541 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
1542 
1543 			ret = auth_sae_send_confirm(hapd, sta);
1544 			if (ret)
1545 				return ret;
1546 
1547 			sae_set_retransmit_timer(hapd, sta);
1548 		} else {
1549 			sta->sae->send_confirm = 0xffff;
1550 			sae_accept_sta(hapd, sta);
1551 		}
1552 		break;
1553 	case SAE_ACCEPTED:
1554 		if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT &&
1555 		    (hapd->conf->mesh & MESH_ENABLED)) {
1556 			wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
1557 				   ") doing reauthentication",
1558 				   MAC2STR(sta->addr));
1559 			wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1560 			ap_free_sta(hapd, sta);
1561 			*sta_removed = 1;
1562 		} else if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1563 			wpa_printf(MSG_DEBUG, "SAE: Start reauthentication");
1564 			ret = auth_sae_send_commit(hapd, sta, 1, status_code);
1565 			if (ret)
1566 				return ret;
1567 			sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
1568 
1569 			if (sae_process_commit(sta->sae) < 0)
1570 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
1571 			sta->sae->sync = 0;
1572 			sae_set_retransmit_timer(hapd, sta);
1573 		} else {
1574 			if (sae_check_big_sync(hapd, sta))
1575 				return WLAN_STATUS_SUCCESS;
1576 			sta->sae->sync++;
1577 
1578 			ret = auth_sae_send_confirm(hapd, sta);
1579 			sae_clear_temp_data(sta->sae);
1580 			if (ret)
1581 				return ret;
1582 		}
1583 		break;
1584 	default:
1585 		wpa_printf(MSG_ERROR, "SAE: invalid state %d",
1586 			   sta->sae->state);
1587 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1588 	}
1589 	return WLAN_STATUS_SUCCESS;
1590 }
1591 
1592 
sae_pick_next_group(struct hostapd_data * hapd,struct sta_info * sta)1593 static void sae_pick_next_group(struct hostapd_data *hapd, struct sta_info *sta)
1594 {
1595 	struct sae_data *sae = sta->sae;
1596 	struct hostapd_bss_config *conf = hapd->conf;
1597 	int i, *groups = conf->sae_groups;
1598 	int default_groups[] = { 19, 0, 0 };
1599 
1600 	if (sae->state != SAE_COMMITTED)
1601 		return;
1602 
1603 	wpa_printf(MSG_DEBUG, "SAE: Previously selected group: %d", sae->group);
1604 
1605 	if (!groups) {
1606 		groups = default_groups;
1607 		if (wpa_key_mgmt_sae_ext_key(conf->wpa_key_mgmt |
1608 					     conf->rsn_override_key_mgmt |
1609 					     conf->rsn_override_key_mgmt_2))
1610 			default_groups[1] = 20;
1611 	}
1612 
1613 	for (i = 0; groups[i] > 0; i++) {
1614 		if (sae->group == groups[i])
1615 			break;
1616 	}
1617 
1618 	if (groups[i] <= 0) {
1619 		wpa_printf(MSG_DEBUG,
1620 			   "SAE: Previously selected group not found from the current configuration");
1621 		return;
1622 	}
1623 
1624 	for (;;) {
1625 		i++;
1626 		if (groups[i] <= 0) {
1627 			wpa_printf(MSG_DEBUG,
1628 				   "SAE: No alternative group enabled");
1629 			return;
1630 		}
1631 
1632 		if (sae_set_group(sae, groups[i]) < 0)
1633 			continue;
1634 
1635 		break;
1636 	}
1637 	wpa_printf(MSG_DEBUG, "SAE: Selected new group: %d", groups[i]);
1638 }
1639 
1640 
sae_status_success(struct hostapd_data * hapd,u16 status_code)1641 static int sae_status_success(struct hostapd_data *hapd, u16 status_code)
1642 {
1643 	enum sae_pwe sae_pwe = hapd->conf->sae_pwe;
1644 	int id_in_use;
1645 	bool sae_pk = false;
1646 
1647 	id_in_use = hostapd_sae_pw_id_in_use(hapd->conf);
1648 	if (id_in_use == 2 && sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
1649 		sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
1650 	else if (id_in_use == 1 && sae_pwe == SAE_PWE_HUNT_AND_PECK)
1651 		sae_pwe = SAE_PWE_BOTH;
1652 #ifdef CONFIG_SAE_PK
1653 	sae_pk = hostapd_sae_pk_in_use(hapd->conf);
1654 	if (sae_pwe == SAE_PWE_HUNT_AND_PECK && sae_pk)
1655 		sae_pwe = SAE_PWE_BOTH;
1656 #endif /* CONFIG_SAE_PK */
1657 	if (sae_pwe == SAE_PWE_HUNT_AND_PECK &&
1658 	    (hapd->conf->wpa_key_mgmt &
1659 	     (WPA_KEY_MGMT_SAE_EXT_KEY | WPA_KEY_MGMT_FT_SAE_EXT_KEY)))
1660 		sae_pwe = SAE_PWE_BOTH;
1661 
1662 	return ((sae_pwe == SAE_PWE_HUNT_AND_PECK ||
1663 		 sae_pwe == SAE_PWE_FORCE_HUNT_AND_PECK) &&
1664 		status_code == WLAN_STATUS_SUCCESS) ||
1665 		(sae_pwe == SAE_PWE_HASH_TO_ELEMENT &&
1666 		 (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1667 		  (sae_pk && status_code == WLAN_STATUS_SAE_PK))) ||
1668 		(sae_pwe == SAE_PWE_BOTH &&
1669 		 (status_code == WLAN_STATUS_SUCCESS ||
1670 		  status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1671 		  (sae_pk && status_code == WLAN_STATUS_SAE_PK)));
1672 }
1673 
1674 
sae_is_group_enabled(struct hostapd_data * hapd,int group)1675 static int sae_is_group_enabled(struct hostapd_data *hapd, int group)
1676 {
1677 	struct hostapd_bss_config *conf = hapd->conf;
1678 	int *groups = conf->sae_groups;
1679 	int default_groups[] = { 19, 0, 0 };
1680 	int i;
1681 
1682 	if (!groups) {
1683 		groups = default_groups;
1684 		if (wpa_key_mgmt_sae_ext_key(conf->wpa_key_mgmt |
1685 					     conf->rsn_override_key_mgmt |
1686 					     conf->rsn_override_key_mgmt_2))
1687 			default_groups[1] = 20;
1688 	}
1689 
1690 	for (i = 0; groups[i] > 0; i++) {
1691 		if (groups[i] == group)
1692 			return 1;
1693 	}
1694 
1695 	return 0;
1696 }
1697 
1698 
check_sae_rejected_groups(struct hostapd_data * hapd,struct sae_data * sae)1699 static int check_sae_rejected_groups(struct hostapd_data *hapd,
1700 				     struct sae_data *sae)
1701 {
1702 	const struct wpabuf *groups;
1703 	size_t i, count, len;
1704 	const u8 *pos;
1705 
1706 	if (!sae->tmp)
1707 		return 0;
1708 	groups = sae->tmp->peer_rejected_groups;
1709 	if (!groups)
1710 		return 0;
1711 
1712 	pos = wpabuf_head(groups);
1713 	len = wpabuf_len(groups);
1714 	if (len & 1) {
1715 		wpa_printf(MSG_DEBUG,
1716 			   "SAE: Invalid length of the Rejected Groups element payload: %zu",
1717 			   len);
1718 		return 1;
1719 	}
1720 
1721 	count = len / 2;
1722 	for (i = 0; i < count; i++) {
1723 		int enabled;
1724 		u16 group;
1725 
1726 		group = WPA_GET_LE16(pos);
1727 		pos += 2;
1728 		enabled = sae_is_group_enabled(hapd, group);
1729 		wpa_printf(MSG_DEBUG, "SAE: Rejected group %u is %s",
1730 			   group, enabled ? "enabled" : "disabled");
1731 		if (enabled)
1732 			return 1;
1733 	}
1734 
1735 	return 0;
1736 }
1737 
1738 
handle_auth_sae(struct hostapd_data * hapd,struct sta_info * sta,const struct ieee80211_mgmt * mgmt,size_t len,u16 auth_transaction,u16 status_code)1739 static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
1740 			    const struct ieee80211_mgmt *mgmt, size_t len,
1741 			    u16 auth_transaction, u16 status_code)
1742 {
1743 	int resp = WLAN_STATUS_SUCCESS;
1744 	struct wpabuf *data = NULL;
1745 	struct hostapd_bss_config *conf = hapd->conf;
1746 	int *groups = conf->sae_groups;
1747 	int default_groups[] = { 19, 0, 0 };
1748 	const u8 *pos, *end;
1749 	int sta_removed = 0;
1750 	bool success_status;
1751 
1752 	if (!groups) {
1753 		groups = default_groups;
1754 		if (wpa_key_mgmt_sae_ext_key(conf->wpa_key_mgmt |
1755 					     conf->rsn_override_key_mgmt |
1756 					     conf->rsn_override_key_mgmt_2))
1757 			default_groups[1] = 20;
1758 	}
1759 
1760 #ifdef CONFIG_TESTING_OPTIONS
1761 	if (hapd->conf->sae_reflection_attack &&
1762 	    auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1763 		wpa_printf(MSG_DEBUG, "SAE: TESTING - reflection attack");
1764 		pos = mgmt->u.auth.variable;
1765 		end = ((const u8 *) mgmt) + len;
1766 		resp = status_code;
1767 		send_auth_reply(hapd, sta, sta->addr,
1768 				WLAN_AUTH_SAE,
1769 				auth_transaction, resp, pos, end - pos,
1770 				"auth-sae-reflection-attack");
1771 		goto remove_sta;
1772 	}
1773 
1774 	if (hapd->conf->sae_commit_override &&
1775 	    auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1776 		wpa_printf(MSG_DEBUG, "SAE: TESTING - commit override");
1777 		send_auth_reply(hapd, sta, sta->addr,
1778 				WLAN_AUTH_SAE,
1779 				auth_transaction, resp,
1780 				wpabuf_head(hapd->conf->sae_commit_override),
1781 				wpabuf_len(hapd->conf->sae_commit_override),
1782 				"sae-commit-override");
1783 		goto remove_sta;
1784 	}
1785 #endif /* CONFIG_TESTING_OPTIONS */
1786 	if (!sta->sae) {
1787 		if (auth_transaction != WLAN_AUTH_TR_SEQ_SAE_COMMIT ||
1788 		    !sae_status_success(hapd, status_code)) {
1789 			wpa_printf(MSG_DEBUG, "SAE: Unexpected Status Code %u",
1790 				   status_code);
1791 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1792 			goto reply;
1793 		}
1794 		sta->sae = os_zalloc(sizeof(*sta->sae));
1795 		if (!sta->sae) {
1796 			resp = -1;
1797 			goto remove_sta;
1798 		}
1799 		if (!hostapd_sae_pw_id_in_use(hapd->conf))
1800 			sta->sae->no_pw_id = 1;
1801 		sae_set_state(sta, SAE_NOTHING, "Init");
1802 		sta->sae->sync = 0;
1803 	}
1804 
1805 	if (sta->mesh_sae_pmksa_caching) {
1806 		wpa_printf(MSG_DEBUG,
1807 			   "SAE: Cancel use of mesh PMKSA caching because peer starts SAE authentication");
1808 		wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1809 		sta->mesh_sae_pmksa_caching = 0;
1810 	}
1811 
1812 	if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1813 		const u8 *token = NULL;
1814 		size_t token_len = 0;
1815 		int allow_reuse = 0;
1816 
1817 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1818 			       HOSTAPD_LEVEL_DEBUG,
1819 			       "start SAE authentication (RX commit, status=%u (%s))",
1820 			       status_code, status2str(status_code));
1821 
1822 		if ((hapd->conf->mesh & MESH_ENABLED) &&
1823 		    status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
1824 		    sta->sae->tmp) {
1825 			pos = mgmt->u.auth.variable;
1826 			end = ((const u8 *) mgmt) + len;
1827 			if (pos + sizeof(le16) > end) {
1828 				wpa_printf(MSG_ERROR,
1829 					   "SAE: Too short anti-clogging token request");
1830 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1831 				goto reply;
1832 			}
1833 			resp = sae_group_allowed(sta->sae, groups,
1834 						 WPA_GET_LE16(pos));
1835 			if (resp != WLAN_STATUS_SUCCESS) {
1836 				wpa_printf(MSG_ERROR,
1837 					   "SAE: Invalid group in anti-clogging token request");
1838 				goto reply;
1839 			}
1840 			pos += sizeof(le16);
1841 
1842 			wpabuf_free(sta->sae->tmp->anti_clogging_token);
1843 			sta->sae->tmp->anti_clogging_token =
1844 				wpabuf_alloc_copy(pos, end - pos);
1845 			if (sta->sae->tmp->anti_clogging_token == NULL) {
1846 				wpa_printf(MSG_ERROR,
1847 					   "SAE: Failed to alloc for anti-clogging token");
1848 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1849 				goto remove_sta;
1850 			}
1851 
1852 			/*
1853 			 * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
1854 			 * is 76, a new Commit Message shall be constructed
1855 			 * with the Anti-Clogging Token from the received
1856 			 * Authentication frame, and the commit-scalar and
1857 			 * COMMIT-ELEMENT previously sent.
1858 			 */
1859 			resp = auth_sae_send_commit(hapd, sta, 0, status_code);
1860 			if (resp != WLAN_STATUS_SUCCESS) {
1861 				wpa_printf(MSG_ERROR,
1862 					   "SAE: Failed to send commit message");
1863 				goto remove_sta;
1864 			}
1865 			sae_set_state(sta, SAE_COMMITTED,
1866 				      "Sent Commit (anti-clogging token case in mesh)");
1867 			sta->sae->sync = 0;
1868 			sae_set_retransmit_timer(hapd, sta);
1869 			return;
1870 		}
1871 
1872 		if ((hapd->conf->mesh & MESH_ENABLED) &&
1873 		    status_code ==
1874 		    WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1875 		    sta->sae->tmp) {
1876 			wpa_printf(MSG_DEBUG,
1877 				   "SAE: Peer did not accept our SAE group");
1878 			sae_pick_next_group(hapd, sta);
1879 			goto remove_sta;
1880 		}
1881 
1882 		if (!sae_status_success(hapd, status_code))
1883 			goto remove_sta;
1884 
1885 		if (sae_proto_instance_disabled(sta)) {
1886 			wpa_printf(MSG_DEBUG,
1887 				   "SAE: Protocol instance temporarily disabled - discard received SAE commit");
1888 			return;
1889 		}
1890 
1891 		if (!(hapd->conf->mesh & MESH_ENABLED) &&
1892 		    sta->sae->state == SAE_COMMITTED) {
1893 			/* This is needed in the infrastructure BSS case to
1894 			 * address a sequence where a STA entry may remain in
1895 			 * hostapd across two attempts to do SAE authentication
1896 			 * by the same STA. The second attempt may end up trying
1897 			 * to use a different group and that would not be
1898 			 * allowed if we remain in Committed state with the
1899 			 * previously set parameters. */
1900 			pos = mgmt->u.auth.variable;
1901 			end = ((const u8 *) mgmt) + len;
1902 			if ((!sta->sae->tmp ||
1903 			     !sta->sae->tmp->try_other_password) &&
1904 			    end - pos >= (int) sizeof(le16) &&
1905 			    sae_group_allowed(sta->sae, groups,
1906 					      WPA_GET_LE16(pos)) ==
1907 			    WLAN_STATUS_SUCCESS) {
1908 				/* Do not waste resources deriving the same PWE
1909 				 * again since the same group is reused. */
1910 				sae_set_state(sta, SAE_NOTHING,
1911 					      "Allow previous PWE to be reused");
1912 				allow_reuse = 1;
1913 			} else {
1914 				sae_set_state(sta, SAE_NOTHING,
1915 					      "Clear existing state to allow restart");
1916 				sae_clear_data(sta->sae);
1917 			}
1918 		}
1919 
1920 		resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
1921 					((const u8 *) mgmt) + len -
1922 					mgmt->u.auth.variable, &token,
1923 					&token_len, groups, status_code ==
1924 					WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1925 					status_code == WLAN_STATUS_SAE_PK,
1926 					NULL);
1927 		if (resp == SAE_SILENTLY_DISCARD) {
1928 			wpa_printf(MSG_DEBUG,
1929 				   "SAE: Drop commit message from " MACSTR " due to reflection attack",
1930 				   MAC2STR(sta->addr));
1931 			goto remove_sta;
1932 		}
1933 
1934 		if (resp == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER) {
1935 			wpa_msg(hapd->msg_ctx, MSG_INFO,
1936 				WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER
1937 				MACSTR, MAC2STR(sta->addr));
1938 			sae_clear_retransmit_timer(hapd, sta);
1939 			sae_set_state(sta, SAE_NOTHING,
1940 				      "Unknown Password Identifier");
1941 			if (sta->sae->state == SAE_NOTHING)
1942 				goto reply;
1943 			goto remove_sta;
1944 		}
1945 
1946 		if (token &&
1947 		    check_comeback_token(hapd->comeback_key,
1948 					 hapd->comeback_pending_idx, sta->addr,
1949 					 token, token_len)
1950 		    < 0) {
1951 			wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
1952 				   "incorrect token from " MACSTR,
1953 				   MAC2STR(sta->addr));
1954 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1955 			goto remove_sta;
1956 		}
1957 
1958 		if (resp != WLAN_STATUS_SUCCESS)
1959 			goto reply;
1960 
1961 		if (check_sae_rejected_groups(hapd, sta->sae)) {
1962 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1963 			goto reply;
1964 		}
1965 
1966 		if (!token && use_anti_clogging(hapd) && !allow_reuse) {
1967 			int h2e = 0;
1968 
1969 			wpa_printf(MSG_DEBUG,
1970 				   "SAE: Request anti-clogging token from "
1971 				   MACSTR, MAC2STR(sta->addr));
1972 			if (sta->sae->tmp)
1973 				h2e = sta->sae->h2e;
1974 			if (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1975 			    status_code == WLAN_STATUS_SAE_PK)
1976 				h2e = 1;
1977 			data = auth_build_token_req(
1978 				&hapd->last_comeback_key_update,
1979 				hapd->comeback_key,
1980 				hapd->comeback_idx,
1981 				hapd->comeback_pending_idx,
1982 				sizeof(hapd->comeback_pending_idx),
1983 				sta->sae->group,
1984 				sta->addr, h2e);
1985 			resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
1986 			if (hapd->conf->mesh & MESH_ENABLED)
1987 				sae_set_state(sta, SAE_NOTHING,
1988 					      "Request anti-clogging token case in mesh");
1989 			goto reply;
1990 		}
1991 
1992 		resp = sae_sm_step(hapd, sta, auth_transaction,
1993 				   status_code, allow_reuse, &sta_removed);
1994 	} else if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_CONFIRM) {
1995 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1996 			       HOSTAPD_LEVEL_DEBUG,
1997 			       "SAE authentication (RX confirm, status=%u (%s))",
1998 			       status_code, status2str(status_code));
1999 		if (status_code != WLAN_STATUS_SUCCESS)
2000 			goto remove_sta;
2001 		if (sta->sae->state >= SAE_CONFIRMED ||
2002 		    !(hapd->conf->mesh & MESH_ENABLED)) {
2003 			const u8 *var;
2004 			size_t var_len;
2005 			u16 peer_send_confirm;
2006 
2007 			var = mgmt->u.auth.variable;
2008 			var_len = ((u8 *) mgmt) + len - mgmt->u.auth.variable;
2009 			if (var_len < 2) {
2010 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2011 				goto reply;
2012 			}
2013 
2014 			peer_send_confirm = WPA_GET_LE16(var);
2015 
2016 			if (sta->sae->state == SAE_ACCEPTED &&
2017 			    (peer_send_confirm <= sta->sae->rc ||
2018 			     peer_send_confirm == 0xffff)) {
2019 				wpa_printf(MSG_DEBUG,
2020 					   "SAE: Silently ignore unexpected Confirm from peer "
2021 					   MACSTR
2022 					   " (peer-send-confirm=%u Rc=%u)",
2023 					   MAC2STR(sta->addr),
2024 					   peer_send_confirm, sta->sae->rc);
2025 				return;
2026 			}
2027 
2028 			if (sae_check_confirm(sta->sae, var, var_len,
2029 					      NULL) < 0) {
2030 				if (sae_password_track_fail(hapd, sta)) {
2031 					wpa_printf(MSG_DEBUG,
2032 						   "SAE: Reject mismatching Confirm so that another password can be attempted by "
2033 						   MACSTR,
2034 						   MAC2STR(sta->addr));
2035 					if (sta->sae->tmp)
2036 						sta->sae->tmp->
2037 							try_other_password = 1;
2038 					resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2039 					goto reply;
2040 				}
2041 				resp = WLAN_STATUS_CHALLENGE_FAIL;
2042 				goto reply;
2043 			}
2044 			sae_password_track_success(hapd, sta);
2045 			sta->sae->rc = peer_send_confirm;
2046 		}
2047 		resp = sae_sm_step(hapd, sta, auth_transaction,
2048 				   status_code, 0, &sta_removed);
2049 	} else {
2050 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2051 			       HOSTAPD_LEVEL_DEBUG,
2052 			       "unexpected SAE authentication transaction %u (status=%u (%s))",
2053 			       auth_transaction, status_code,
2054 			       status2str(status_code));
2055 		if (status_code != WLAN_STATUS_SUCCESS)
2056 			goto remove_sta;
2057 		resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
2058 	}
2059 
2060 reply:
2061 	if (!sta_removed && resp != WLAN_STATUS_SUCCESS) {
2062 		pos = mgmt->u.auth.variable;
2063 		end = ((const u8 *) mgmt) + len;
2064 
2065 		/* Copy the Finite Cyclic Group field from the request if we
2066 		 * rejected it as unsupported group. */
2067 		if (resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
2068 		    !data && end - pos >= 2)
2069 			data = wpabuf_alloc_copy(pos, 2);
2070 
2071 		send_auth_reply(hapd, sta, sta->addr,
2072 				WLAN_AUTH_SAE,
2073 				auth_transaction, resp,
2074 				data ? wpabuf_head(data) : (u8 *) "",
2075 				data ? wpabuf_len(data) : 0, "auth-sae");
2076 		sae_sme_send_external_auth_status(hapd, sta, resp);
2077 	}
2078 
2079 remove_sta:
2080 	if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT)
2081 		success_status = sae_status_success(hapd, status_code);
2082 	else
2083 		success_status = status_code == WLAN_STATUS_SUCCESS;
2084 	if (!sta_removed && sta->added_unassoc &&
2085 	    (resp != WLAN_STATUS_SUCCESS || !success_status)) {
2086 		hostapd_drv_sta_remove(hapd, sta->addr);
2087 		sta->added_unassoc = 0;
2088 	}
2089 	wpabuf_free(data);
2090 }
2091 
2092 
2093 /**
2094  * auth_sae_init_committed - Send COMMIT and start SAE in committed state
2095  * @hapd: BSS data for the device initiating the authentication
2096  * @sta: the peer to which commit authentication frame is sent
2097  *
2098  * This function implements Init event handling (IEEE Std 802.11-2012,
2099  * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
2100  * sta->sae structure should be initialized appropriately via a call to
2101  * sae_prepare_commit().
2102  */
auth_sae_init_committed(struct hostapd_data * hapd,struct sta_info * sta)2103 int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
2104 {
2105 	int ret;
2106 
2107 	if (!sta->sae || !sta->sae->tmp)
2108 		return -1;
2109 
2110 	if (sta->sae->state != SAE_NOTHING)
2111 		return -1;
2112 
2113 	ret = auth_sae_send_commit(hapd, sta, 0, -1);
2114 	if (ret)
2115 		return -1;
2116 
2117 	sae_set_state(sta, SAE_COMMITTED, "Init and sent commit");
2118 	sta->sae->sync = 0;
2119 	sae_set_retransmit_timer(hapd, sta);
2120 
2121 	return 0;
2122 }
2123 
2124 
auth_sae_process_commit(void * eloop_ctx,void * user_ctx)2125 void auth_sae_process_commit(void *eloop_ctx, void *user_ctx)
2126 {
2127 	struct hostapd_data *hapd = eloop_ctx;
2128 	struct hostapd_sae_commit_queue *q;
2129 	unsigned int queue_len;
2130 
2131 	q = dl_list_first(&hapd->sae_commit_queue,
2132 			  struct hostapd_sae_commit_queue, list);
2133 	if (!q)
2134 		return;
2135 	wpa_printf(MSG_DEBUG,
2136 		   "SAE: Process next available message from queue");
2137 	dl_list_del(&q->list);
2138 	handle_auth(hapd, (const struct ieee80211_mgmt *) q->msg, q->len,
2139 		    q->rssi, 1);
2140 	os_free(q);
2141 
2142 	if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
2143 		return;
2144 	queue_len = dl_list_len(&hapd->sae_commit_queue);
2145 	eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
2146 			       hapd, NULL);
2147 }
2148 
2149 
auth_sae_queue(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int rssi)2150 static void auth_sae_queue(struct hostapd_data *hapd,
2151 			   const struct ieee80211_mgmt *mgmt, size_t len,
2152 			   int rssi)
2153 {
2154 	struct hostapd_sae_commit_queue *q, *q2;
2155 	unsigned int queue_len;
2156 	const struct ieee80211_mgmt *mgmt2;
2157 
2158 	queue_len = dl_list_len(&hapd->sae_commit_queue);
2159 	if (queue_len >= 15) {
2160 		wpa_printf(MSG_DEBUG,
2161 			   "SAE: No more room in message queue - drop the new frame from "
2162 			   MACSTR, MAC2STR(mgmt->sa));
2163 		return;
2164 	}
2165 
2166 	wpa_printf(MSG_DEBUG, "SAE: Queue Authentication message from "
2167 		   MACSTR " for processing (queue_len %u)", MAC2STR(mgmt->sa),
2168 		   queue_len);
2169 	q = os_zalloc(sizeof(*q) + len);
2170 	if (!q)
2171 		return;
2172 	q->rssi = rssi;
2173 	q->len = len;
2174 	os_memcpy(q->msg, mgmt, len);
2175 
2176 	/* Check whether there is already a queued Authentication frame from the
2177 	 * same station with the same transaction number and if so, replace that
2178 	 * queue entry with the new one. This avoids issues with a peer that
2179 	 * sends multiple times (e.g., due to frequent SAE retries). There is no
2180 	 * point in us trying to process the old attempts after a new one has
2181 	 * obsoleted them. */
2182 	dl_list_for_each(q2, &hapd->sae_commit_queue,
2183 			 struct hostapd_sae_commit_queue, list) {
2184 		mgmt2 = (const struct ieee80211_mgmt *) q2->msg;
2185 		if (ether_addr_equal(mgmt->sa, mgmt2->sa) &&
2186 		    mgmt->u.auth.auth_transaction ==
2187 		    mgmt2->u.auth.auth_transaction) {
2188 			wpa_printf(MSG_DEBUG,
2189 				   "SAE: Replace queued message from same STA with same transaction number");
2190 			dl_list_add(&q2->list, &q->list);
2191 			dl_list_del(&q2->list);
2192 			os_free(q2);
2193 			goto queued;
2194 		}
2195 	}
2196 
2197 	/* No pending identical entry, so add to the end of the queue */
2198 	dl_list_add_tail(&hapd->sae_commit_queue, &q->list);
2199 
2200 queued:
2201 	if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
2202 		return;
2203 	eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
2204 			       hapd, NULL);
2205 }
2206 
2207 
auth_sae_queued_addr(struct hostapd_data * hapd,const u8 * addr)2208 static int auth_sae_queued_addr(struct hostapd_data *hapd, const u8 *addr)
2209 {
2210 	struct hostapd_sae_commit_queue *q;
2211 	const struct ieee80211_mgmt *mgmt;
2212 
2213 	dl_list_for_each(q, &hapd->sae_commit_queue,
2214 			 struct hostapd_sae_commit_queue, list) {
2215 		mgmt = (const struct ieee80211_mgmt *) q->msg;
2216 		if (ether_addr_equal(addr, mgmt->sa))
2217 			return 1;
2218 	}
2219 
2220 	return 0;
2221 }
2222 
2223 #endif /* CONFIG_SAE */
2224 
2225 
wpa_res_to_status_code(enum wpa_validate_result res)2226 static u16 wpa_res_to_status_code(enum wpa_validate_result res)
2227 {
2228 	switch (res) {
2229 	case WPA_IE_OK:
2230 		return WLAN_STATUS_SUCCESS;
2231 	case WPA_INVALID_IE:
2232 		return WLAN_STATUS_INVALID_ELEMENT;
2233 	case WPA_INVALID_GROUP:
2234 		return WLAN_STATUS_INVALID_GROUP_CIPHER;
2235 	case WPA_INVALID_PAIRWISE:
2236 		return WLAN_STATUS_INVALID_PAIRWISE_CIPHER;
2237 	case WPA_INVALID_AKMP:
2238 		return WLAN_STATUS_INVALID_AKMP;
2239 	case WPA_NOT_ENABLED:
2240 		return WLAN_STATUS_INVALID_ELEMENT;
2241 	case WPA_ALLOC_FAIL:
2242 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2243 	case WPA_MGMT_FRAME_PROTECTION_VIOLATION:
2244 		return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
2245 	case WPA_INVALID_MGMT_GROUP_CIPHER:
2246 		return WLAN_STATUS_CIPHER_OUT_OF_POLICY;
2247 	case WPA_INVALID_MDIE:
2248 		return WLAN_STATUS_INVALID_MDE;
2249 	case WPA_INVALID_PROTO:
2250 		return WLAN_STATUS_INVALID_ELEMENT;
2251 	case WPA_INVALID_PMKID:
2252 		return WLAN_STATUS_INVALID_PMKID;
2253 	case WPA_DENIED_OTHER_REASON:
2254 		return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
2255 	}
2256 	return WLAN_STATUS_INVALID_ELEMENT;
2257 }
2258 
2259 
2260 #ifdef CONFIG_IEEE8021X_AUTH
2261 
2262 static struct wpabuf *
prepare_802_1x_auth_resp(struct hostapd_data * hapd,struct sta_info * sta,u16 auth_transaction,u16 status,struct rsn_pmksa_cache_entry * cached_pmk,const u8 * eap_req,size_t eap_req_len)2263 prepare_802_1x_auth_resp(struct hostapd_data *hapd, struct sta_info *sta,
2264 			 u16 auth_transaction, u16 status,
2265 			 struct rsn_pmksa_cache_entry *cached_pmk,
2266 			 const u8 *eap_req, size_t eap_req_len)
2267 {
2268 	struct wpabuf *pub = NULL, *data;
2269 	bool enc_assoc = ap_sta_support_enc_assoc(hapd,
2270 						  sta->eap_auth_data.rsnxe,
2271 						  sta->eap_auth_data.rsnxe_len);
2272 
2273 	data = wpabuf_alloc(1000 + eap_req_len);
2274 	if (!data) {
2275 		wpa_printf(MSG_INFO,
2276 			   "Authentication frame buffer allocation failed");
2277 		return NULL;
2278 	}
2279 
2280 	/* Encapsulation Length field */
2281 	wpabuf_put_le16(data, eap_req_len);
2282 	/* Encapsulation field */
2283 	wpabuf_put_data(data, eap_req, eap_req_len);
2284 
2285 	if (status != WLAN_STATUS_SUCCESS &&
2286 	    status != WLAN_STATUS_802_1_X_AUTH_SUCCESS)
2287 		goto reply;
2288 
2289 	/* Authentication frames with transaction sequence greater than or
2290 	 * equal to 3 contain Authentication fields only.
2291 	 */
2292 	if (auth_transaction == 2) {
2293 		/* Per IEEE 802.11bi/D4.0, 12.16.8.3 (IEEE 802.1X), a responder
2294 		 * that sets
2295 		 * dot11EPPReAssociationFrameEncryptionSupportActivated
2296 		 * to false or does not receive the RSNXE in the first
2297 		 * Authentication frame with the (Re)Association Frame
2298 		 * Encryption Support field set to 1 shall not include
2299 		 * a Diffie-Hellman Parameter element nor a Nonce element
2300 		 * nor an RSNE in the second Authentication frame for
2301 		 * IEEE 802.1X authentication.
2302 		 */
2303 		if (enc_assoc) {
2304 			u8 a_nonce[WPA_NONCE_LEN];
2305 			struct hostapd_bss_config *conf = hapd->conf;
2306 			int res;
2307 
2308 			/* Derive own public key */
2309 			if (sta->eap_auth_data.ecdh) {
2310 				pub = crypto_ecdh_get_pubkey(
2311 					sta->eap_auth_data.ecdh, 1);
2312 				if (!pub) {
2313 					status =
2314 						WLAN_STATUS_UNSPECIFIED_FAILURE;
2315 					goto reply;
2316 				}
2317 			}
2318 
2319 			/* ANonce generation */
2320 			if (random_get_bytes(a_nonce, WPA_NONCE_LEN) < 0) {
2321 				status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2322 				goto reply;
2323 			}
2324 			os_memcpy(sta->eap_auth_data.anonce, a_nonce,
2325 				  WPA_NONCE_LEN);
2326 
2327 
2328 			if (pub && wpabuf_resize(&data, wpabuf_len(pub)) < 0) {
2329 				status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2330 				goto reply;
2331 			}
2332 
2333 			/* Per IEEE 802.11bi/D4.0, 12.16.8.3 (IEEE 802.1X),
2334 			 * responder shall include an RSNE with the AKM and
2335 			 * pairwise cipher suite as indicated in the first
2336 			 * Authentication frame.
2337 			 */
2338 			res = wpa_write_802_1x_rsne(
2339 				hapd->wpa_auth,
2340 				wpabuf_mhead_u8(data) + wpabuf_len(data),
2341 				(wpabuf_size(data) - wpabuf_len(data)),
2342 				cached_pmk ? cached_pmk->pmkid : NULL,
2343 				sta->eap_auth_data.akm,
2344 				sta->eap_auth_data.cipher,
2345 				conf->wpa_group,
2346 				conf->group_mgmt_cipher,
2347 				conf->ieee80211w);
2348 			if (res < 0) {
2349 				status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2350 				goto reply;
2351 			}
2352 			wpabuf_put(data, res);
2353 
2354 			/* DH Parameter element */
2355 			wpabuf_put_u8(data, WLAN_EID_EXTENSION);
2356 			wpabuf_put_u8(data, 1 + 2 + wpabuf_len(pub));
2357 			wpabuf_put_u8(data, WLAN_EID_EXT_OWE_DH_PARAM);
2358 			wpabuf_put_le16(data, sta->eap_auth_data.group);
2359 			wpabuf_put_buf(data, pub);
2360 
2361 			/* ANonce in Nonce element */
2362 			wpabuf_put_u8(data, WLAN_EID_EXTENSION);
2363 			wpabuf_put_u8(data, 1 + WPA_NONCE_LEN);
2364 			wpabuf_put_u8(data, WLAN_EID_EXT_NONCE);
2365 			wpabuf_put_data(data, a_nonce, WPA_NONCE_LEN);
2366 		} else {
2367 			/* Per IEEE 802.11bi/D4.0, 12.16.5 (IEEE 802.1X
2368 			 * authentication utilizing Authentication frames), the
2369 			 * Responser shall construct the second Authentication
2370 			 * frame with an AKM Suite Selector element indicating
2371 			 * the same IEEE 802.1X AKM indicated in the first
2372 			 * Authentication frame.
2373 			 */
2374 			wpabuf_put_u8(data, WLAN_EID_EXTENSION);
2375 			wpabuf_put_u8(data, 1 + RSN_SELECTOR_LEN);
2376 			wpabuf_put_u8(data, WLAN_EID_EXT_AKM_SUITE_SELECTOR);
2377 			RSN_SELECTOR_PUT(wpabuf_put(data, RSN_SELECTOR_LEN),
2378 					 wpa_akm_to_suite(
2379 						 sta->eap_auth_data.akm));
2380 		}
2381 	} /* if (auth_transaction == 2) */
2382 reply:
2383 	wpabuf_free(pub);
2384 	return data;
2385 }
2386 
2387 
wpa_auth_validate_802_1x_frame(struct hostapd_data * hapd,struct sta_info * sta,struct ieee802_11_elems * elems)2388 u16 wpa_auth_validate_802_1x_frame(struct hostapd_data *hapd,
2389 				   struct sta_info *sta,
2390 				   struct ieee802_11_elems *elems)
2391 {
2392 	struct wpa_ie_data rsn;
2393 	const int default_groups[] = { 19, 0 };
2394 	bool enc_assoc = ap_sta_support_enc_assoc(hapd,
2395 						  elems->rsnxe,
2396 						  elems->rsnxe_len);
2397 
2398 	/* Per IEEE P802.11bi/D4.0, 12.16.8.3 (IEEE 802.1X), an originator that
2399 	 * sets dot11EPPReAssociationFrameEncryptionSupportActivated to false or
2400 	 * does not receive the RSNXE from the responder with the
2401 	 * (Re)Association Frame Encryption Support field set to 1 shall not
2402 	 * include a Diffie-Hellman Parameter element nor an RSNE nor an RSNXE
2403 	 * nor a Nonce element in the first Authentication frame for IEEE 802.1X
2404 	 * authentication.
2405 	 */
2406 	if (!enc_assoc &&
2407 	    (elems->rsn_ie || elems->nonce || elems->owe_dh)) {
2408 		wpa_printf(MSG_INFO,
2409 			   "Invalid inclusion of RSNE/Nonce/DHE when (Re)Association frame encryption is not supported");
2410 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2411 	}
2412 	if (enc_assoc &&
2413 	    (!elems->rsn_ie || !elems->nonce ||
2414 	     elems->nonce_len != WPA_NONCE_LEN || !elems->owe_dh)) {
2415 		wpa_printf(MSG_ERROR, "Missing RSNE/DHIE/Nonce");
2416 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2417 	}
2418 
2419 	/* Both RSNE and AKM Suite Selector element shall not be present at the
2420 	 * same time. */
2421 	if (elems->rsn_ie && elems->akm_suite_selector) {
2422 		wpa_printf(MSG_INFO,
2423 			   "Incorrect inclusion of both RSNE and AKM Suite Selector element");
2424 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2425 	}
2426 
2427 	if (enc_assoc &&
2428 	    (!elems->rsn_ie ||
2429 	     wpa_parse_wpa_ie_rsn(elems->rsn_ie - 2, elems->rsn_ie_len + 2,
2430 				  &rsn) < 0)) {
2431 		wpa_printf(MSG_INFO, "No valid RSNE");
2432 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2433 	}
2434 
2435 	if (enc_assoc && elems->rsn_ie) {
2436 		if (!(rsn.pairwise_cipher & hapd->conf->rsn_pairwise)) {
2437 			wpa_printf(MSG_INFO,
2438 				   "Invalid pairwise cipher (0x%x) in RSNE",
2439 				   rsn.pairwise_cipher);
2440 			return WPA_INVALID_PAIRWISE;
2441 		}
2442 		sta->eap_auth_data.cipher = rsn.pairwise_cipher;
2443 		wpa_printf(MSG_DEBUG, "Received pairwise cipher (0x%x) in RSNE",
2444 			   rsn.pairwise_cipher);
2445 
2446 		if (!(rsn.key_mgmt & hapd->conf->wpa_key_mgmt)) {
2447 			wpa_printf(MSG_INFO, "Invalid key mgmt (0x%x) in RSNE",
2448 				   rsn.key_mgmt);
2449 			return WPA_INVALID_AKMP;
2450 		}
2451 		sta->eap_auth_data.akm = rsn.key_mgmt;
2452 		wpa_printf(MSG_DEBUG, "Received keymgmt (0x%x) in RSNE",
2453 			   rsn.key_mgmt);
2454 	}
2455 
2456 	/* Validate AKM Suite Selector element */
2457 	if (elems->akm_suite_selector) {
2458 		sta->eap_auth_data.akm = rsn_key_mgmt_to_wpa_akm(
2459 			RSN_SELECTOR_GET(elems->akm_suite_selector));
2460 		if (!(sta->eap_auth_data.akm & hapd->conf->wpa_key_mgmt)) {
2461 			wpa_printf(MSG_INFO,
2462 				   "Invalid key mgmt (0x%x) in AKM Suite Selector element",
2463 				   sta->eap_auth_data.akm);
2464 			return WPA_INVALID_AKMP;
2465 		}
2466 		wpa_printf(MSG_DEBUG,
2467 			   "Received keymgmt (0x%x) in AKM Suite Selector element",
2468 			   sta->eap_auth_data.akm);
2469 	}
2470 
2471 	if (elems->rsnxe) {
2472 		os_free(sta->eap_auth_data.rsnxe);
2473 		sta->eap_auth_data.rsnxe =
2474 			os_memdup(elems->rsnxe, elems->rsnxe_len);
2475 		sta->eap_auth_data.rsnxe_len = elems->rsnxe_len;
2476 	}
2477 
2478 	/* Store SNonce */
2479 	if (elems->nonce && elems->nonce_len == WPA_NONCE_LEN) {
2480 		os_memcpy(sta->eap_auth_data.snonce, elems->nonce,
2481 			  WPA_NONCE_LEN);
2482 		wpa_hexdump(MSG_DEBUG, "SNonce", elems->nonce, WPA_NONCE_LEN);
2483 	}
2484 
2485 	/* Validate DH Parameter element */
2486 	if (elems->owe_dh) {
2487 		u16 group;
2488 		u8 pubkey_len;
2489 		const u8 *pubkey;
2490 		struct wpabuf *secret;
2491 
2492 		group = WPA_GET_LE16(elems->owe_dh);
2493 		if (!int_array_includes(default_groups, group)) {
2494 			wpa_printf(MSG_INFO,
2495 				   "Received unsupported group value %u",
2496 				   group);
2497 			return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2498 		}
2499 		sta->eap_auth_data.group = group;
2500 		pubkey = elems->owe_dh + 2;
2501 		pubkey_len = elems->owe_dh_len - 2;
2502 
2503 		/* TODO: Any more validation of peer public key needed? */
2504 		if (!pubkey_len) {
2505 			wpa_printf(MSG_INFO, "Missing DH public key");
2506 			return WLAN_STATUS_INVALID_PUBLIC_KEY;
2507 		}
2508 
2509 		/* Setup ECDH context */
2510 		crypto_ecdh_deinit(sta->eap_auth_data.ecdh);
2511 		sta->eap_auth_data.ecdh = crypto_ecdh_init(group);
2512 		if (!sta->eap_auth_data.ecdh) {
2513 			wpa_printf(MSG_INFO, "Failed to setup ECDH context");
2514 			return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2515 		}
2516 
2517 		/* Generate shared secret */
2518 		wpabuf_clear_free(sta->eap_auth_data.dhss);
2519 		sta->eap_auth_data.dhss = NULL;
2520 		secret = crypto_ecdh_set_peerkey(sta->eap_auth_data.ecdh, 0,
2521 						 pubkey, pubkey_len);
2522 		if (!secret) {
2523 			wpa_printf(MSG_INFO, "Invalid peer public key");
2524 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
2525 		}
2526 		wpa_hexdump_buf_key(MSG_DEBUG, "DH shared secret", secret);
2527 		sta->eap_auth_data.dhss = secret;
2528 	}
2529 
2530 	return WLAN_STATUS_SUCCESS;
2531 }
2532 
2533 
2534 /**
2535  * ieee80211_send_eap_req - Callback function to send EAP-Request message in an
2536  *	Authentication frame
2537  *
2538  * This function is called from ieee802_1x_eapol_send() using the
2539  * hapd->send_eap_req callback. Its main purpose is to prepend the EAP-Request
2540  * data with an IEEE 802.1X header and call prepare_802_1x_auth_resp() to send
2541  * out the next IEEE 802.1X Authentication frame to the station. If this is an
2542  * EAP-Success frame, it also fetches the MSK derived from the successful EAP
2543  * authentication to derive PMK and PTK and configure the TK to the driver.
2544  */
ieee80211_send_eap_req(struct hostapd_data * hapd,struct sta_info * sta,u8 type,u16 auth_transaction,u16 status,struct rsn_pmksa_cache_entry * cached_pmk,const u8 * eap_req,size_t eap_req_len)2545 void ieee80211_send_eap_req(struct hostapd_data *hapd, struct sta_info *sta,
2546 			    u8 type, u16 auth_transaction, u16 status,
2547 			    struct rsn_pmksa_cache_entry *cached_pmk,
2548 			    const u8 *eap_req, size_t eap_req_len)
2549 {
2550 	bool enc_assoc = ap_sta_support_enc_assoc(hapd,
2551 						  sta->eap_auth_data.rsnxe,
2552 						  sta->eap_auth_data.rsnxe_len);
2553 	struct ieee802_1x_hdr *xhdr;
2554 	u8 *data;
2555 	size_t len = eap_req_len + sizeof(struct ieee802_1x_hdr);
2556 	struct wpabuf *reply;
2557 
2558 	wpa_printf(MSG_DEBUG,
2559 		   "Process EAP-Request data for TX using an Authentication frame");
2560 
2561 	data = os_malloc(len);
2562 	if (!data) {
2563 		wpa_printf(MSG_ERROR, "malloc() failed for %s", __func__);
2564 		return;
2565 	}
2566 
2567 	xhdr = (struct ieee802_1x_hdr *) data;
2568 	xhdr->version = hapd->conf->eapol_version;
2569 	xhdr->type = type;
2570 	xhdr->length = host_to_be16(eap_req_len);
2571 
2572 	if (eap_req && eap_req_len > 0)
2573 		os_memcpy(xhdr + 1, eap_req, eap_req_len);
2574 
2575 	wpa_hexdump(MSG_MSGDUMP, "EAP-Request", eap_req, eap_req_len);
2576 
2577 	/* EAP-Success */
2578 	if (enc_assoc && eap_req_len > 0 && eap_req[0] == 3) {
2579 		u8 msk[2 * PMK_LEN] = { 0 };
2580 		size_t _len = 2 * PMK_LEN;
2581 		size_t pmk_len, kdk_len;
2582 		bool is_ml = ap_sta_is_mld(hapd, sta);
2583 		enum wpa_alg alg =
2584 			wpa_cipher_to_alg(sta->eap_auth_data.cipher);
2585 		size_t key_len =
2586 			wpa_cipher_key_len(sta->eap_auth_data.cipher);
2587 		const u8 *aa = hapd->own_addr;
2588 		struct rsn_pmksa_cache *pmksa =
2589 			wpa_auth_get_pmksa_cache(hapd->wpa_auth, is_ml);
2590 		struct rsn_pmksa_cache_entry *entry;
2591 #ifdef CONFIG_TESTING_OPTIONS
2592 		bool force_kdk = hapd->conf->force_kdk_derivation;
2593 #else /* CONFIG_TESTING_OPTIONS */
2594 		bool force_kdk = false;
2595 #endif /* CONFIG_TESTING_OPTIONS */
2596 
2597 #ifdef CONFIG_IEEE80211BE
2598 		if (is_ml)
2599 			aa = hapd->mld->mld_addr;
2600 #endif /* CONFIG_IEEE80211BE */
2601 
2602 		/* Per IEEE 802.11bi/D4.0, 12.16.5 (IEEE 802.1X authentication
2603 		 * utilizing Authentication frames), if the IEEE 802.1X
2604 		 * authentication is successful, the Status Code field
2605 		 * is set to 802_1_X_AUTH_SUCCESS. */
2606 		status = WLAN_STATUS_802_1_X_AUTH_SUCCESS;
2607 		/* TODO: If the IEEE 802.1X authentication fails,
2608 		 * the status code is set to 802_1_X_AUTH_FAILED. */
2609 		os_memset(&sta->eap_auth_data.ptk, 0, sizeof(struct wpa_ptk));
2610 		if (wpa_auth_802_1x_get_msk(hapd->wpa_auth, sta->addr,
2611 					    msk, &_len)) {
2612 			wpa_printf(MSG_INFO, "Failed to get MSK");
2613 			os_free(data);
2614 			return;
2615 		}
2616 
2617 		if (wpa_key_mgmt_sha384(sta->eap_auth_data.akm))
2618 			pmk_len = PMK_LEN_SUITE_B_192;
2619 		else
2620 			pmk_len = PMK_LEN;
2621 
2622 		sta->eap_auth_data.pmk_len = pmk_len;
2623 		os_memcpy(sta->eap_auth_data.pmk, msk, pmk_len);
2624 
2625 		if (force_kdk ||
2626 		    (wpa_auth_ap_support_secure_ltf(hapd->wpa_auth) &&
2627 		     ieee802_11_rsnx_capab(sta->eap_auth_data.rsnxe,
2628 					   WLAN_RSNX_CAPAB_SECURE_LTF)))
2629 			kdk_len = WPA_KDK_MAX_LEN;
2630 		else
2631 			kdk_len = 0;
2632 		if (wpa_auth_802_1x_pmk_to_ptk(
2633 			    msk, sta->eap_auth_data.pmk_len,
2634 			    sta->addr, aa,
2635 			    sta->eap_auth_data.snonce,
2636 			    sta->eap_auth_data.anonce,
2637 			    sta->eap_auth_data.akm,
2638 			    sta->eap_auth_data.cipher,
2639 			    wpabuf_head_u8(sta->eap_auth_data.dhss),
2640 			    wpabuf_len(sta->eap_auth_data.dhss),
2641 			    &sta->eap_auth_data.ptk, kdk_len)) {
2642 			wpa_printf(MSG_INFO, "Failed to derive the PTK");
2643 			os_free(data);
2644 			return;
2645 		}
2646 		wpa_printf(MSG_DEBUG, "PTK derived successfully");
2647 
2648 		if (wpa_auth_802_1x_set_key(hapd->wpa_auth,
2649 					    alg, sta->addr,
2650 					    sta->eap_auth_data.ptk.tk,
2651 					    key_len)) {
2652 			wpa_printf(MSG_INFO,
2653 				   "Failed to set the TK to the driver");
2654 			os_free(data);
2655 			return;
2656 		}
2657 
2658 		/* Delete DHss after successful PTK derivation */
2659 		wpabuf_clear_free(sta->eap_auth_data.dhss);
2660 		sta->eap_auth_data.dhss = NULL;
2661 
2662 		/* TODO: Fill session_timeout? */
2663 		wpa_hexdump_key(MSG_DEBUG, "IEEE802.1X: Cache PMK",
2664 				msk, pmk_len);
2665 
2666 		entry = pmksa_cache_auth_add(pmksa, msk, pmk_len, NULL,
2667 					     sta->eap_auth_data.ptk.kck,
2668 					     sta->eap_auth_data.ptk.kck_len,
2669 					     aa, sta->addr, 0, sta->eapol_sm,
2670 					     sta->eap_auth_data.akm);
2671 		if (!entry) {
2672 			wpa_printf(MSG_INFO, "Failed to add PMKSA entry");
2673 			return;
2674 		}
2675 		os_memcpy(sta->eap_auth_data.epp_pmkid_cur, entry->pmkid,
2676 			  PMKID_LEN);
2677 	}
2678 
2679 	reply = prepare_802_1x_auth_resp(hapd, sta, auth_transaction, status,
2680 					 cached_pmk, data, len);
2681 	if (reply)
2682 		send_8021x_auth_reply(hapd, sta, auth_transaction, status,
2683 				      reply);
2684 	os_free(data);
2685 }
2686 
2687 
handle_auth_802_1x(struct hostapd_data * hapd,struct sta_info * sta,const u8 * pos,size_t len,u16 auth_alg,u16 auth_transaction)2688 static void handle_auth_802_1x(struct hostapd_data *hapd, struct sta_info *sta,
2689 			       const u8 *pos, size_t len, u16 auth_alg,
2690 			       u16 auth_transaction)
2691 {
2692 	struct ieee802_1x_hdr *eapol_pdu;
2693 	u16 encap_len, resp = WLAN_STATUS_SUCCESS;
2694 	const u8 *end;
2695 	struct wpabuf *reply;
2696 
2697 	if (len < 2) {
2698 		wpa_printf(MSG_INFO, "Missing Encapsulation Length field");
2699 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2700 		goto fail;
2701 	}
2702 	end = pos + len;
2703 	encap_len = WPA_GET_LE16(pos);
2704 	pos += 2;
2705 	if (encap_len > end - pos) {
2706 		wpa_printf(MSG_INFO, "Truncated Encapsulation field");
2707 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2708 		goto fail;
2709 	}
2710 
2711 	/* Start of Encapsulation field */
2712 	eapol_pdu = (struct ieee802_1x_hdr *) pos;
2713 
2714 	if (auth_transaction == 1 &&
2715 	    eapol_pdu->type != IEEE802_1X_TYPE_EAPOL_START) {
2716 		wpa_printf(MSG_INFO,
2717 			   "Received unexpected EAPOL PDU type %u in the first Authentication frame",
2718 			   eapol_pdu->type);
2719 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2720 		goto fail;
2721 	}
2722 	pos += encap_len;
2723 	sta->eap_auth_data.auth_transaction = auth_transaction;
2724 
2725 	/* Process Authentication frame elements
2726 	 * Authentication frames with transaction sequence greater
2727 	 * than or equal to 3 contain Authentication fields only.
2728 	 */
2729 	if (auth_transaction == 1) {
2730 		struct wpa_ie_data data;
2731 		struct ieee802_11_elems elems;
2732 		struct rsn_pmksa_cache_entry *cached_pmk = NULL;
2733 		bool is_ml = ap_sta_is_mld(hapd, sta);
2734 		bool enc_assoc, pmkid_privacy;
2735 		size_t i;
2736 
2737 		if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) ==
2738 		    ParseFailed) {
2739 			wpa_printf(MSG_INFO, "Could not parse elements");
2740 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2741 			goto fail;
2742 		}
2743 
2744 		resp = wpa_auth_validate_802_1x_frame(hapd, sta, &elems);
2745 		if (resp)
2746 			goto fail;
2747 
2748 		enc_assoc = ap_sta_support_enc_assoc(hapd, elems.rsnxe,
2749 						     elems.rsnxe_len);
2750 
2751 		pmkid_privacy = hapd->conf->pmksa_caching_privacy &&
2752 			ieee802_11_rsnx_capab_len(
2753 				elems.rsnxe, elems.rsnxe_len,
2754 				WLAN_RSNX_CAPAB_PMKSA_CACHING_PRIVACY);
2755 
2756 		os_memset(&data, 0, sizeof(data));
2757 		if (enc_assoc &&
2758 		    (!elems.rsn_ie ||
2759 		     wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2,
2760 					  elems.rsn_ie_len + 2, &data) < 0)) {
2761 			wpa_printf(MSG_INFO, "No valid RSNE");
2762 			goto fail;
2763 		}
2764 
2765 		for (i = 0; i < data.num_pmkid; i++) {
2766 			const u8 *aa;
2767 			enum wpa_alg alg;
2768 			size_t key_len, kdk_len;
2769 #ifdef CONFIG_TESTING_OPTIONS
2770 			bool force_kdk = hapd->conf->force_kdk_derivation;
2771 #else /* CONFIG_TESTING_OPTIONS */
2772 			bool force_kdk = false;
2773 #endif /* CONFIG_TESTING_OPTIONS */
2774 
2775 			wpa_hexdump(MSG_DEBUG, "RSNE: STA PMKID",
2776 				    &data.pmkid[i * PMKID_LEN], PMKID_LEN);
2777 
2778 			cached_pmk = pmksa_cache_search(
2779 				hapd, pmkid_privacy ? NULL : sta->addr,
2780 				&data.pmkid[i * PMKID_LEN], is_ml);
2781 			if (!cached_pmk)
2782 				continue;
2783 
2784 			aa = hapd->own_addr;
2785 			alg = wpa_cipher_to_alg(sta->eap_auth_data.cipher);
2786 			key_len = wpa_cipher_key_len(sta->eap_auth_data.cipher);
2787 
2788 #ifdef CONFIG_IEEE80211BE
2789 			if (ap_sta_is_mld(hapd, sta))
2790 				aa = hapd->mld->mld_addr;
2791 #endif /* CONFIG_IEEE80211BE */
2792 			wpa_printf(MSG_DEBUG,
2793 				   "Found a matching PMKSA cache entry");
2794 			os_memcpy(sta->eap_auth_data.epp_pmkid_cur,
2795 				  cached_pmk->pmkid, PMKID_LEN);
2796 			reply = prepare_802_1x_auth_resp(
2797 				hapd, sta, auth_transaction + 1,
2798 				WLAN_STATUS_SUCCESS, cached_pmk, NULL, 0);
2799 			if (!reply) {
2800 				wpa_printf(MSG_INFO,
2801 					   "Failed to prepare IEEE 802.1X Authentication frame");
2802 				return;
2803 			}
2804 
2805 			if (force_kdk ||
2806 			    (wpa_auth_ap_support_secure_ltf(hapd->wpa_auth) &&
2807 			     ieee802_11_rsnx_capab(sta->eap_auth_data.rsnxe,
2808 						   WLAN_RSNX_CAPAB_SECURE_LTF)))
2809 				kdk_len = WPA_KDK_MAX_LEN;
2810 			else
2811 				kdk_len = 0;
2812 
2813 			if (wpa_auth_802_1x_pmk_to_ptk(
2814 				    cached_pmk->pmk, cached_pmk->pmk_len,
2815 				    sta->addr, aa,
2816 				    sta->eap_auth_data.snonce,
2817 				    sta->eap_auth_data.anonce,
2818 				    sta->eap_auth_data.akm,
2819 				    sta->eap_auth_data.cipher,
2820 				    wpabuf_head_u8(sta->eap_auth_data.dhss),
2821 				    wpabuf_len(sta->eap_auth_data.dhss),
2822 				    &sta->eap_auth_data.ptk, kdk_len)) {
2823 				wpa_printf(MSG_INFO, "Failed to derive PTK");
2824 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2825 				goto fail;
2826 			}
2827 			wpa_printf(MSG_DEBUG, "PTK derived successfully");
2828 
2829 			if (wpa_auth_802_1x_set_key(hapd->wpa_auth, alg,
2830 						    sta->addr,
2831 						    sta->eap_auth_data.ptk.tk,
2832 						    key_len)) {
2833 				wpa_printf(MSG_INFO,
2834 					   "Failed to set TK to driver");
2835 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2836 				goto fail;
2837 			}
2838 
2839 			sta->flags |= WLAN_STA_AUTH;
2840 			sta->auth_alg = WLAN_AUTH_802_1X;
2841 			sta->eap_auth_data.add_mic = true;
2842 			send_8021x_auth_reply(hapd, sta, auth_transaction + 1,
2843 					      WLAN_STATUS_SUCCESS, reply);
2844 			/* Delete DHss after successful PTK derivation */
2845 			wpabuf_clear_free(sta->eap_auth_data.dhss);
2846 			sta->eap_auth_data.dhss = NULL;
2847 			return;
2848 		}
2849 
2850 		/* Start EAPOL SM to process EAPOL PDU */
2851 		if (!sta->eapol_sm) {
2852 			sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
2853 			if (!sta->eapol_sm)
2854 				return;
2855 		}
2856 
2857 		ieee802_1x_eapol_sm_set_port_enabled(sta->eapol_sm, true);
2858 	}
2859 
2860 	/* Forward the extracted EAP PDU to AS */
2861 	ieee802_1x_receive(hapd, sta->addr, (const u8 *) eapol_pdu,
2862 			   encap_len, FRAME_NOT_ENCRYPTED);
2863 	return;
2864 
2865 fail:
2866 	reply = prepare_802_1x_auth_resp(hapd, sta, auth_transaction + 1,
2867 					 resp, NULL, NULL, 0);
2868 	if (reply)
2869 		send_8021x_auth_reply(hapd, sta, auth_transaction + 1, resp,
2870 				      reply);
2871 }
2872 
2873 #endif /* CONFIG_IEEE8021X_AUTH */
2874 
2875 
2876 #ifdef CONFIG_FILS
2877 
2878 static void handle_auth_fils_finish(struct hostapd_data *hapd,
2879 				    struct sta_info *sta, u16 resp,
2880 				    struct wpabuf *data, int pub);
2881 
handle_auth_fils(struct hostapd_data * hapd,struct sta_info * sta,const u8 * pos,size_t len,u16 auth_alg,u16 auth_transaction,u16 status_code,void (* cb)(struct hostapd_data * hapd,struct sta_info * sta,u16 resp,struct wpabuf * data,int pub))2882 void handle_auth_fils(struct hostapd_data *hapd, struct sta_info *sta,
2883 		      const u8 *pos, size_t len, u16 auth_alg,
2884 		      u16 auth_transaction, u16 status_code,
2885 		      void (*cb)(struct hostapd_data *hapd,
2886 				 struct sta_info *sta, u16 resp,
2887 				 struct wpabuf *data, int pub))
2888 {
2889 	u16 resp = WLAN_STATUS_SUCCESS;
2890 	const u8 *end;
2891 	struct ieee802_11_elems elems;
2892 	enum wpa_validate_result res;
2893 	struct wpa_ie_data rsn;
2894 	struct rsn_pmksa_cache_entry *pmksa = NULL;
2895 
2896 	if (auth_transaction != WLAN_AUTH_TR_SEQ_SAE_COMMIT ||
2897 	    status_code != WLAN_STATUS_SUCCESS)
2898 		return;
2899 
2900 	end = pos + len;
2901 
2902 	wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
2903 		    pos, end - pos);
2904 
2905 	/* TODO: FILS PK */
2906 #ifdef CONFIG_FILS_SK_PFS
2907 	if (auth_alg == WLAN_AUTH_FILS_SK_PFS) {
2908 		u16 group;
2909 		struct wpabuf *pub;
2910 		size_t elem_len;
2911 
2912 		/* Using FILS PFS */
2913 
2914 		/* Finite Cyclic Group */
2915 		if (end - pos < 2) {
2916 			wpa_printf(MSG_DEBUG,
2917 				   "FILS: No room for Finite Cyclic Group");
2918 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2919 			goto fail;
2920 		}
2921 		group = WPA_GET_LE16(pos);
2922 		pos += 2;
2923 		if (group != hapd->conf->fils_dh_group) {
2924 			wpa_printf(MSG_DEBUG,
2925 				   "FILS: Unsupported Finite Cyclic Group: %u (expected %u)",
2926 				   group, hapd->conf->fils_dh_group);
2927 			resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2928 			goto fail;
2929 		}
2930 
2931 		crypto_ecdh_deinit(sta->fils_ecdh);
2932 		sta->fils_ecdh = crypto_ecdh_init(group);
2933 		if (!sta->fils_ecdh) {
2934 			wpa_printf(MSG_INFO,
2935 				   "FILS: Could not initialize ECDH with group %d",
2936 				   group);
2937 			resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2938 			goto fail;
2939 		}
2940 
2941 		pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
2942 		if (!pub) {
2943 			wpa_printf(MSG_DEBUG,
2944 				   "FILS: Failed to derive ECDH public key");
2945 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2946 			goto fail;
2947 		}
2948 		elem_len = wpabuf_len(pub);
2949 		wpabuf_free(pub);
2950 
2951 		/* Element */
2952 		if ((size_t) (end - pos) < elem_len) {
2953 			wpa_printf(MSG_DEBUG, "FILS: No room for Element");
2954 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2955 			goto fail;
2956 		}
2957 
2958 		wpabuf_free(sta->fils_g_sta);
2959 		sta->fils_g_sta = wpabuf_alloc_copy(pos, elem_len);
2960 		wpabuf_clear_free(sta->fils_dh_ss);
2961 		sta->fils_dh_ss = crypto_ecdh_set_peerkey(sta->fils_ecdh, 1,
2962 							  pos, elem_len);
2963 		if (!sta->fils_dh_ss) {
2964 			wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
2965 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2966 			goto fail;
2967 		}
2968 		wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", sta->fils_dh_ss);
2969 		pos += elem_len;
2970 	} else {
2971 		crypto_ecdh_deinit(sta->fils_ecdh);
2972 		sta->fils_ecdh = NULL;
2973 		wpabuf_clear_free(sta->fils_dh_ss);
2974 		sta->fils_dh_ss = NULL;
2975 	}
2976 #endif /* CONFIG_FILS_SK_PFS */
2977 
2978 	wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
2979 	if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
2980 		wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
2981 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2982 		goto fail;
2983 	}
2984 
2985 	/* RSNE */
2986 	wpa_hexdump(MSG_DEBUG, "FILS: RSN element",
2987 		    elems.rsn_ie, elems.rsn_ie_len);
2988 	if (!elems.rsn_ie ||
2989 	    wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
2990 				 &rsn) < 0) {
2991 		wpa_printf(MSG_DEBUG, "FILS: No valid RSN element");
2992 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2993 		goto fail;
2994 	}
2995 
2996 	if (!sta->wpa_sm)
2997 		sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr,
2998 						NULL);
2999 	if (!sta->wpa_sm) {
3000 		wpa_printf(MSG_DEBUG,
3001 			   "FILS: Failed to initialize RSN state machine");
3002 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3003 		goto fail;
3004 	}
3005 
3006 	wpa_auth_set_rsn_selection(sta->wpa_sm, elems.rsn_selection,
3007 				   elems.rsn_selection_len);
3008 	res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
3009 				  hapd->iface->freq,
3010 				  elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3011 				  elems.rsnxe ? elems.rsnxe - 2 : NULL,
3012 				  elems.rsnxe ? elems.rsnxe_len + 2 : 0,
3013 				  elems.mdie, elems.mdie_len, NULL, 0, NULL,
3014 				  ap_sta_is_mld(hapd, sta));
3015 	resp = wpa_res_to_status_code(res);
3016 	if (resp != WLAN_STATUS_SUCCESS)
3017 		goto fail;
3018 
3019 	if (!elems.nonce) {
3020 		wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
3021 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3022 		goto fail;
3023 	}
3024 	wpa_hexdump(MSG_DEBUG, "FILS: SNonce", elems.nonce, NONCE_LEN);
3025 	os_memcpy(sta->fils_snonce, elems.nonce, NONCE_LEN);
3026 
3027 	/* PMKID List */
3028 	if (rsn.pmkid && rsn.num_pmkid > 0) {
3029 		u8 num;
3030 		const u8 *pmkid;
3031 
3032 		wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3033 			    rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3034 
3035 		pmkid = rsn.pmkid;
3036 		num = rsn.num_pmkid;
3037 		while (num) {
3038 			wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN);
3039 			pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr,
3040 						   pmkid);
3041 			if (pmksa)
3042 				break;
3043 			pmksa = wpa_auth_pmksa_get_fils_cache_id(hapd->wpa_auth,
3044 								 sta->addr,
3045 								 pmkid);
3046 			if (pmksa)
3047 				break;
3048 			pmkid += PMKID_LEN;
3049 			num--;
3050 		}
3051 	}
3052 	if (pmksa && wpa_auth_sta_key_mgmt(sta->wpa_sm) != pmksa->akmp) {
3053 		wpa_printf(MSG_DEBUG,
3054 			   "FILS: Matching PMKSA cache entry has different AKMP (0x%x != 0x%x) - ignore",
3055 			   wpa_auth_sta_key_mgmt(sta->wpa_sm), pmksa->akmp);
3056 		pmksa = NULL;
3057 	}
3058 	if (pmksa)
3059 		wpa_printf(MSG_DEBUG, "FILS: Found matching PMKSA cache entry");
3060 
3061 	/* FILS Session */
3062 	if (!elems.fils_session) {
3063 		wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
3064 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3065 		goto fail;
3066 	}
3067 	wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3068 		    FILS_SESSION_LEN);
3069 	os_memcpy(sta->fils_session, elems.fils_session, FILS_SESSION_LEN);
3070 
3071 	/* Wrapped Data */
3072 	if (elems.wrapped_data) {
3073 		wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3074 			    elems.wrapped_data,
3075 			    elems.wrapped_data_len);
3076 		if (!pmksa) {
3077 #ifndef CONFIG_NO_RADIUS
3078 			if (!sta->eapol_sm) {
3079 				sta->eapol_sm =
3080 					ieee802_1x_alloc_eapol_sm(hapd, sta);
3081 			}
3082 			wpa_printf(MSG_DEBUG,
3083 				   "FILS: Forward EAP-Initiate/Re-auth to authentication server");
3084 			ieee802_1x_encapsulate_radius(
3085 				hapd, sta, elems.wrapped_data,
3086 				elems.wrapped_data_len);
3087 			sta->fils_pending_cb = cb;
3088 			wpa_printf(MSG_DEBUG,
3089 				   "FILS: Will send Authentication frame once the response from authentication server is available");
3090 			sta->flags |= WLAN_STA_PENDING_FILS_ERP;
3091 			/* Calculate pending PMKID here so that we do not need
3092 			 * to maintain a copy of the EAP-Initiate/Reauth
3093 			 * message. */
3094 			if (fils_pmkid_erp(wpa_auth_sta_key_mgmt(sta->wpa_sm),
3095 					   elems.wrapped_data,
3096 					   elems.wrapped_data_len,
3097 					   sta->fils_erp_pmkid) == 0)
3098 				sta->fils_erp_pmkid_set = 1;
3099 			return;
3100 #else /* CONFIG_NO_RADIUS */
3101 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3102 			goto fail;
3103 #endif /* CONFIG_NO_RADIUS */
3104 		}
3105 	}
3106 
3107 fail:
3108 	if (cb) {
3109 		struct wpabuf *data;
3110 		int pub = 0;
3111 
3112 		data = prepare_auth_resp_fils(hapd, sta, &resp, pmksa, NULL,
3113 					      NULL, 0, &pub);
3114 		if (!data) {
3115 			wpa_printf(MSG_DEBUG,
3116 				   "%s: prepare_auth_resp_fils() returned failure",
3117 				   __func__);
3118 		}
3119 
3120 		cb(hapd, sta, resp, data, pub);
3121 	}
3122 }
3123 
3124 
3125 static struct wpabuf *
prepare_auth_resp_fils(struct hostapd_data * hapd,struct sta_info * sta,u16 * resp,struct rsn_pmksa_cache_entry * pmksa,struct wpabuf * erp_resp,const u8 * msk,size_t msk_len,int * is_pub)3126 prepare_auth_resp_fils(struct hostapd_data *hapd,
3127 		       struct sta_info *sta, u16 *resp,
3128 		       struct rsn_pmksa_cache_entry *pmksa,
3129 		       struct wpabuf *erp_resp,
3130 		       const u8 *msk, size_t msk_len,
3131 		       int *is_pub)
3132 {
3133 	u8 fils_nonce[NONCE_LEN];
3134 	size_t ielen;
3135 	struct wpabuf *data = NULL;
3136 	const u8 *ie;
3137 	u8 *ie_buf = NULL;
3138 	const u8 *pmk = NULL;
3139 	size_t pmk_len = 0;
3140 	u8 pmk_buf[PMK_LEN_MAX];
3141 	struct wpabuf *pub = NULL;
3142 
3143 	if (*resp != WLAN_STATUS_SUCCESS)
3144 		goto fail;
3145 
3146 	ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
3147 	if (!ie) {
3148 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3149 		goto fail;
3150 	}
3151 
3152 	if (pmksa) {
3153 		/* Add PMKID of the selected PMKSA into RSNE */
3154 		ie_buf = os_malloc(ielen + 2 + 2 + PMKID_LEN);
3155 		if (!ie_buf) {
3156 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3157 			goto fail;
3158 		}
3159 
3160 		os_memcpy(ie_buf, ie, ielen);
3161 		if (wpa_insert_pmkid(ie_buf, &ielen, pmksa->pmkid, true) < 0) {
3162 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3163 			goto fail;
3164 		}
3165 		ie = ie_buf;
3166 	}
3167 
3168 	if (random_get_bytes(fils_nonce, NONCE_LEN) < 0) {
3169 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3170 		goto fail;
3171 	}
3172 	wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS Nonce",
3173 		    fils_nonce, NONCE_LEN);
3174 
3175 #ifdef CONFIG_FILS_SK_PFS
3176 	if (sta->fils_dh_ss && sta->fils_ecdh) {
3177 		pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
3178 		if (!pub) {
3179 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3180 			goto fail;
3181 		}
3182 	}
3183 #endif /* CONFIG_FILS_SK_PFS */
3184 
3185 	data = wpabuf_alloc(1000 + ielen + (pub ? wpabuf_len(pub) : 0));
3186 	if (!data) {
3187 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3188 		goto fail;
3189 	}
3190 
3191 	/* TODO: FILS PK */
3192 #ifdef CONFIG_FILS_SK_PFS
3193 	if (pub) {
3194 		/* Finite Cyclic Group */
3195 		wpabuf_put_le16(data, hapd->conf->fils_dh_group);
3196 
3197 		/* Element */
3198 		wpabuf_put_buf(data, pub);
3199 	}
3200 #endif /* CONFIG_FILS_SK_PFS */
3201 
3202 	/* RSNE */
3203 	wpabuf_put_data(data, ie, ielen);
3204 
3205 	/* MDE when using FILS+FT (already included in ie,ielen with RSNE) */
3206 
3207 #ifdef CONFIG_IEEE80211R_AP
3208 	if (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm))) {
3209 		/* FTE[R1KH-ID,R0KH-ID] when using FILS+FT */
3210 		int res;
3211 
3212 		res = wpa_auth_write_fte(hapd->wpa_auth, sta->wpa_sm,
3213 					 wpabuf_put(data, 0),
3214 					 wpabuf_tailroom(data));
3215 		if (res < 0) {
3216 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3217 			goto fail;
3218 		}
3219 		wpabuf_put(data, res);
3220 	}
3221 #endif /* CONFIG_IEEE80211R_AP */
3222 
3223 	/* FILS Nonce */
3224 	wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
3225 	wpabuf_put_u8(data, 1 + NONCE_LEN); /* Length */
3226 	/* Element ID Extension */
3227 	wpabuf_put_u8(data, WLAN_EID_EXT_NONCE);
3228 	wpabuf_put_data(data, fils_nonce, NONCE_LEN);
3229 
3230 	/* FILS Session */
3231 	wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
3232 	wpabuf_put_u8(data, 1 + FILS_SESSION_LEN); /* Length */
3233 	/* Element ID Extension */
3234 	wpabuf_put_u8(data, WLAN_EID_EXT_FILS_SESSION);
3235 	wpabuf_put_data(data, sta->fils_session, FILS_SESSION_LEN);
3236 
3237 	/* Wrapped Data */
3238 	if (!pmksa && erp_resp) {
3239 		wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
3240 		wpabuf_put_u8(data, 1 + wpabuf_len(erp_resp)); /* Length */
3241 		/* Element ID Extension */
3242 		wpabuf_put_u8(data, WLAN_EID_EXT_WRAPPED_DATA);
3243 		wpabuf_put_buf(data, erp_resp);
3244 
3245 		if (fils_rmsk_to_pmk(wpa_auth_sta_key_mgmt(sta->wpa_sm),
3246 				     msk, msk_len, sta->fils_snonce, fils_nonce,
3247 				     sta->fils_dh_ss ?
3248 				     wpabuf_head(sta->fils_dh_ss) : NULL,
3249 				     sta->fils_dh_ss ?
3250 				     wpabuf_len(sta->fils_dh_ss) : 0,
3251 				     pmk_buf, &pmk_len)) {
3252 			wpa_printf(MSG_DEBUG, "FILS: Failed to derive PMK");
3253 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3254 			wpabuf_free(data);
3255 			data = NULL;
3256 			goto fail;
3257 		}
3258 		pmk = pmk_buf;
3259 
3260 		/* Don't use DHss in PTK derivation if PMKSA caching is not
3261 		 * used. */
3262 		wpabuf_clear_free(sta->fils_dh_ss);
3263 		sta->fils_dh_ss = NULL;
3264 
3265 		if (sta->fils_erp_pmkid_set) {
3266 			/* TODO: get PMKLifetime from WPA parameters */
3267 			unsigned int dot11RSNAConfigPMKLifetime = 43200;
3268 			int session_timeout;
3269 
3270 			session_timeout = dot11RSNAConfigPMKLifetime;
3271 			if (sta->session_timeout_set) {
3272 				struct os_reltime now, diff;
3273 
3274 				os_get_reltime(&now);
3275 				os_reltime_sub(&sta->session_timeout, &now,
3276 					       &diff);
3277 				session_timeout = diff.sec;
3278 			}
3279 
3280 			sta->fils_erp_pmkid_set = 0;
3281 			wpa_auth_add_fils_pmk_pmkid(sta->wpa_sm, pmk, pmk_len,
3282 						    sta->fils_erp_pmkid);
3283 			if (!hapd->conf->disable_pmksa_caching &&
3284 			    wpa_auth_pmksa_add2(
3285 				    hapd->wpa_auth, sta->addr,
3286 				    pmk, pmk_len,
3287 				    sta->fils_erp_pmkid,
3288 				    session_timeout,
3289 				    wpa_auth_sta_key_mgmt(sta->wpa_sm),
3290 				    NULL, ap_sta_is_mld(hapd, sta)) < 0) {
3291 				wpa_printf(MSG_ERROR,
3292 					   "FILS: Failed to add PMKSA cache entry based on ERP");
3293 			}
3294 		}
3295 	} else if (pmksa) {
3296 		pmk = pmksa->pmk;
3297 		pmk_len = pmksa->pmk_len;
3298 	}
3299 
3300 	if (!pmk) {
3301 		wpa_printf(MSG_DEBUG, "FILS: No PMK available");
3302 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3303 		wpabuf_free(data);
3304 		data = NULL;
3305 		goto fail;
3306 	}
3307 
3308 	if (fils_auth_pmk_to_ptk(sta->wpa_sm, pmk, pmk_len,
3309 				 sta->fils_snonce, fils_nonce,
3310 				 sta->fils_dh_ss ?
3311 				 wpabuf_head(sta->fils_dh_ss) : NULL,
3312 				 sta->fils_dh_ss ?
3313 				 wpabuf_len(sta->fils_dh_ss) : 0,
3314 				 sta->fils_g_sta, pub) < 0) {
3315 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3316 		wpabuf_free(data);
3317 		data = NULL;
3318 		goto fail;
3319 	}
3320 
3321 fail:
3322 	if (is_pub)
3323 		*is_pub = pub != NULL;
3324 	os_free(ie_buf);
3325 	wpabuf_free(pub);
3326 	wpabuf_clear_free(sta->fils_dh_ss);
3327 	sta->fils_dh_ss = NULL;
3328 #ifdef CONFIG_FILS_SK_PFS
3329 	crypto_ecdh_deinit(sta->fils_ecdh);
3330 	sta->fils_ecdh = NULL;
3331 #endif /* CONFIG_FILS_SK_PFS */
3332 	return data;
3333 }
3334 
3335 
handle_auth_fils_finish(struct hostapd_data * hapd,struct sta_info * sta,u16 resp,struct wpabuf * data,int pub)3336 static void handle_auth_fils_finish(struct hostapd_data *hapd,
3337 				    struct sta_info *sta, u16 resp,
3338 				    struct wpabuf *data, int pub)
3339 {
3340 	u16 auth_alg;
3341 
3342 	auth_alg = (pub ||
3343 		    resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) ?
3344 		WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
3345 	send_auth_reply(hapd, sta, sta->addr, auth_alg, 2, resp,
3346 			data ? wpabuf_head(data) : (u8 *) "",
3347 			data ? wpabuf_len(data) : 0, "auth-fils-finish");
3348 	wpabuf_free(data);
3349 
3350 	if (resp == WLAN_STATUS_SUCCESS) {
3351 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3352 			       HOSTAPD_LEVEL_DEBUG,
3353 			       "authentication OK (FILS)");
3354 		sta->flags |= WLAN_STA_AUTH;
3355 		wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
3356 		sta->auth_alg = pub ? WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
3357 		mlme_authenticate_indication(hapd, sta);
3358 	}
3359 }
3360 
3361 
ieee802_11_finish_fils_auth(struct hostapd_data * hapd,struct sta_info * sta,int success,struct wpabuf * erp_resp,const u8 * msk,size_t msk_len)3362 void ieee802_11_finish_fils_auth(struct hostapd_data *hapd,
3363 				 struct sta_info *sta, int success,
3364 				 struct wpabuf *erp_resp,
3365 				 const u8 *msk, size_t msk_len)
3366 {
3367 	u16 resp;
3368 	u32 flags = sta->flags;
3369 
3370 	sta->flags &= ~(WLAN_STA_PENDING_FILS_ERP |
3371 			WLAN_STA_PENDING_PASN_FILS_ERP);
3372 
3373 	resp = success ? WLAN_STATUS_SUCCESS : WLAN_STATUS_UNSPECIFIED_FAILURE;
3374 
3375 	if (flags & WLAN_STA_PENDING_FILS_ERP) {
3376 		struct wpabuf *data;
3377 		int pub = 0;
3378 
3379 		if (!sta->fils_pending_cb)
3380 			return;
3381 
3382 		data = prepare_auth_resp_fils(hapd, sta, &resp, NULL, erp_resp,
3383 					      msk, msk_len, &pub);
3384 		if (!data) {
3385 			wpa_printf(MSG_DEBUG,
3386 				   "%s: prepare_auth_resp_fils() failure",
3387 				   __func__);
3388 		}
3389 		sta->fils_pending_cb(hapd, sta, resp, data, pub);
3390 #ifdef CONFIG_PASN
3391 	} else if (flags & WLAN_STA_PENDING_PASN_FILS_ERP) {
3392 		pasn_fils_auth_resp(hapd, sta, resp, erp_resp,
3393 				    msk, msk_len);
3394 #endif /* CONFIG_PASN */
3395 	}
3396 }
3397 
3398 #endif /* CONFIG_FILS */
3399 
3400 
ieee802_11_allowed_address(struct hostapd_data * hapd,const u8 * addr,const u8 * msg,size_t len,struct radius_sta * info)3401 static int ieee802_11_allowed_address(struct hostapd_data *hapd, const u8 *addr,
3402 				      const u8 *msg, size_t len,
3403 				      struct radius_sta *info)
3404 {
3405 	int res;
3406 
3407 	res = hostapd_allowed_address(hapd, addr, msg, len, info, 0);
3408 
3409 	if (res == HOSTAPD_ACL_REJECT) {
3410 		wpa_printf(MSG_DEBUG, "Station " MACSTR
3411 			   " not allowed to authenticate",
3412 			   MAC2STR(addr));
3413 		return HOSTAPD_ACL_REJECT;
3414 	}
3415 
3416 	if (res == HOSTAPD_ACL_PENDING) {
3417 		wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
3418 			   " waiting for an external authentication",
3419 			   MAC2STR(addr));
3420 		/* Authentication code will re-send the authentication frame
3421 		 * after it has received (and cached) information from the
3422 		 * external source. */
3423 		return HOSTAPD_ACL_PENDING;
3424 	}
3425 
3426 	return res;
3427 }
3428 
3429 
ieee802_11_set_radius_info(struct hostapd_data * hapd,struct sta_info * sta,int res,struct radius_sta * info)3430 int ieee802_11_set_radius_info(struct hostapd_data *hapd, struct sta_info *sta,
3431 			       int res, struct radius_sta *info)
3432 {
3433 	u32 session_timeout = info->session_timeout;
3434 	u32 acct_interim_interval = info->acct_interim_interval;
3435 	struct vlan_description *vlan_id = &info->vlan_id;
3436 	struct hostapd_sta_wpa_psk_short *psk = info->psk;
3437 	char *identity = info->identity;
3438 	char *radius_cui = info->radius_cui;
3439 
3440 	if (vlan_id->notempty &&
3441 	    !hostapd_vlan_valid(hapd->conf->vlan, vlan_id)) {
3442 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
3443 			       HOSTAPD_LEVEL_INFO,
3444 			       "Invalid VLAN %d%s received from RADIUS server",
3445 			       vlan_id->untagged,
3446 			       vlan_id->tagged[0] ? "+" : "");
3447 		return -1;
3448 	}
3449 	if (ap_sta_set_vlan(hapd, sta, vlan_id) < 0)
3450 		return -1;
3451 	if (sta->vlan_id)
3452 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
3453 			       HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
3454 
3455 	hostapd_free_psk_list(sta->psk);
3456 	if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED)
3457 		hostapd_copy_psk_list(&sta->psk, psk);
3458 	else
3459 		sta->psk = NULL;
3460 
3461 	os_free(sta->identity);
3462 	if (identity)
3463 		sta->identity = os_strdup(identity);
3464 	else
3465 		sta->identity = NULL;
3466 
3467 	os_free(sta->radius_cui);
3468 	if (radius_cui)
3469 		sta->radius_cui = os_strdup(radius_cui);
3470 	else
3471 		sta->radius_cui = NULL;
3472 
3473 	if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
3474 		sta->acct_interim_interval = acct_interim_interval;
3475 	if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT) {
3476 		sta->session_timeout_set = 1;
3477 		os_get_reltime(&sta->session_timeout);
3478 		sta->session_timeout.sec += session_timeout;
3479 		ap_sta_session_timeout(hapd, sta, session_timeout);
3480 	} else {
3481 		sta->session_timeout_set = 0;
3482 		ap_sta_no_session_timeout(hapd, sta);
3483 	}
3484 
3485 	return 0;
3486 }
3487 
3488 
3489 #ifdef CONFIG_PASN
3490 #ifdef CONFIG_FILS
3491 
pasn_fils_auth_resp(struct hostapd_data * hapd,struct sta_info * sta,u16 status,struct wpabuf * erp_resp,const u8 * msk,size_t msk_len)3492 static void pasn_fils_auth_resp(struct hostapd_data *hapd,
3493 				struct sta_info *sta, u16 status,
3494 				struct wpabuf *erp_resp,
3495 				const u8 *msk, size_t msk_len)
3496 {
3497 	struct pasn_data *pasn = sta->pasn;
3498 	struct pasn_fils *fils = &pasn->fils;
3499 	u8 pmk[PMK_LEN_MAX];
3500 	size_t pmk_len;
3501 	int ret;
3502 
3503 	wpa_printf(MSG_DEBUG, "PASN: FILS: Handle AS response - status=%u",
3504 		   status);
3505 
3506 	if (status != WLAN_STATUS_SUCCESS)
3507 		goto fail;
3508 
3509 	if (!pasn->secret) {
3510 		wpa_printf(MSG_DEBUG, "PASN: FILS: Missing secret");
3511 		goto fail;
3512 	}
3513 
3514 	if (random_get_bytes(fils->anonce, NONCE_LEN) < 0) {
3515 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed to get ANonce");
3516 		goto fail;
3517 	}
3518 
3519 	wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS ANonce",
3520 		    fils->anonce, NONCE_LEN);
3521 
3522 	ret = fils_rmsk_to_pmk(pasn_get_akmp(pasn), msk, msk_len, fils->nonce,
3523 			       fils->anonce, NULL, 0, pmk, &pmk_len);
3524 	if (ret) {
3525 		wpa_printf(MSG_DEBUG, "FILS: Failed to derive PMK");
3526 		goto fail;
3527 	}
3528 
3529 	ret = pasn_pmk_to_ptk(pmk, pmk_len, sta->addr, hapd->own_addr,
3530 			      wpabuf_head(pasn->secret),
3531 			      wpabuf_len(pasn->secret),
3532 			      pasn_get_ptk(sta->pasn), pasn_get_akmp(sta->pasn),
3533 			      pasn_get_cipher(sta->pasn), sta->pasn->kdk_len,
3534 			      sta->pasn->kek_len, &sta->pasn->hash_alg,
3535 			      pasn->auth_alg == WLAN_AUTH_EPPKE);
3536 	if (ret) {
3537 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed to derive PTK");
3538 		goto fail;
3539 	}
3540 
3541 	if (pasn->secure_ltf) {
3542 		ret = wpa_ltf_keyseed(pasn_get_ptk(pasn), pasn_get_akmp(pasn),
3543 				      pasn_get_cipher(pasn));
3544 		if (ret) {
3545 			wpa_printf(MSG_DEBUG,
3546 				   "PASN: FILS: Failed to derive LTF keyseed");
3547 			goto fail;
3548 		}
3549 	}
3550 
3551 	wpa_printf(MSG_DEBUG, "PASN: PTK successfully derived");
3552 
3553 	wpabuf_free(pasn->secret);
3554 	pasn->secret = NULL;
3555 
3556 	fils->erp_resp = erp_resp;
3557 	ret = handle_auth_pasn_resp(sta->pasn, hapd->own_addr, sta->addr, NULL,
3558 				    WLAN_STATUS_SUCCESS);
3559 	wpabuf_free(pasn->frame);
3560 	pasn->frame = NULL;
3561 	fils->erp_resp = NULL;
3562 
3563 	if (ret) {
3564 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed to send response");
3565 		goto fail;
3566 	}
3567 
3568 	fils->state = PASN_FILS_STATE_COMPLETE;
3569 	return;
3570 fail:
3571 	ap_free_sta(hapd, sta);
3572 }
3573 
3574 
pasn_wd_handle_fils(struct hostapd_data * hapd,struct sta_info * sta,struct wpabuf * wd)3575 static int pasn_wd_handle_fils(struct hostapd_data *hapd, struct sta_info *sta,
3576 			       struct wpabuf *wd)
3577 {
3578 #ifdef CONFIG_NO_RADIUS
3579 	wpa_printf(MSG_DEBUG, "PASN: FILS: RADIUS is not configured. Fail");
3580 	return -1;
3581 #else /* CONFIG_NO_RADIUS */
3582 	struct pasn_data *pasn = sta->pasn;
3583 	struct pasn_fils *fils = &pasn->fils;
3584 	struct ieee802_11_elems elems;
3585 	struct wpa_ie_data rsne_data;
3586 	struct wpabuf *fils_wd;
3587 	const u8 *data;
3588 	size_t buf_len;
3589 	u16 alg, seq, status;
3590 	int ret;
3591 
3592 	if (fils->state != PASN_FILS_STATE_NONE) {
3593 		wpa_printf(MSG_DEBUG, "PASN: FILS: Not expecting wrapped data");
3594 		return -1;
3595 	}
3596 
3597 	if (!wd) {
3598 		wpa_printf(MSG_DEBUG, "PASN: FILS: No wrapped data");
3599 		return -1;
3600 	}
3601 
3602 	data = wpabuf_head_u8(wd);
3603 	buf_len = wpabuf_len(wd);
3604 
3605 	if (buf_len < 6) {
3606 		wpa_printf(MSG_DEBUG, "PASN: FILS: Buffer too short. len=%zu",
3607 			   buf_len);
3608 		return -1;
3609 	}
3610 
3611 	alg = WPA_GET_LE16(data);
3612 	seq = WPA_GET_LE16(data + 2);
3613 	status = WPA_GET_LE16(data + 4);
3614 
3615 	wpa_printf(MSG_DEBUG, "PASN: FILS: alg=%u, seq=%u, status=%u",
3616 		   alg, seq, status);
3617 
3618 	if (alg != WLAN_AUTH_FILS_SK || seq != 1 ||
3619 	    status != WLAN_STATUS_SUCCESS) {
3620 		wpa_printf(MSG_DEBUG,
3621 			   "PASN: FILS: Dropping peer authentication");
3622 		return -1;
3623 	}
3624 
3625 	data += 6;
3626 	buf_len -= 6;
3627 
3628 	if (ieee802_11_parse_elems(data, buf_len, &elems, 1) == ParseFailed) {
3629 		wpa_printf(MSG_DEBUG, "PASN: FILS: Could not parse elements");
3630 		return -1;
3631 	}
3632 
3633 	if (!elems.rsn_ie || !elems.nonce || !elems.nonce ||
3634 	    !elems.wrapped_data || !elems.fils_session) {
3635 		wpa_printf(MSG_DEBUG, "PASN: FILS: Missing IEs");
3636 		return -1;
3637 	}
3638 
3639 	ret = wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3640 				   &rsne_data);
3641 	if (ret) {
3642 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed parsing RSNE");
3643 		return -1;
3644 	}
3645 
3646 	ret = wpa_pasn_validate_rsne(&rsne_data, false);
3647 	if (ret) {
3648 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed validating RSNE");
3649 		return -1;
3650 	}
3651 
3652 	if (rsne_data.num_pmkid) {
3653 		wpa_printf(MSG_DEBUG,
3654 			   "PASN: FILS: Not expecting PMKID in RSNE");
3655 		return -1;
3656 	}
3657 
3658 	wpa_hexdump(MSG_DEBUG, "PASN: FILS: Nonce", elems.nonce, NONCE_LEN);
3659 	os_memcpy(fils->nonce, elems.nonce, NONCE_LEN);
3660 
3661 	wpa_hexdump(MSG_DEBUG, "PASN: FILS: Session", elems.fils_session,
3662 		    FILS_SESSION_LEN);
3663 	os_memcpy(fils->session, elems.fils_session, FILS_SESSION_LEN);
3664 
3665 	fils_wd = ieee802_11_defrag(elems.wrapped_data, elems.wrapped_data_len,
3666 				    true);
3667 
3668 	if (!fils_wd) {
3669 		wpa_printf(MSG_DEBUG, "PASN: FILS: Missing wrapped data");
3670 		return -1;
3671 	}
3672 
3673 	if (!sta->eapol_sm)
3674 		sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
3675 
3676 	wpa_printf(MSG_DEBUG,
3677 		   "PASN: FILS: Forward EAP-Initiate/Re-auth to AS");
3678 
3679 	ieee802_1x_encapsulate_radius(hapd, sta, wpabuf_head(fils_wd),
3680 				      wpabuf_len(fils_wd));
3681 
3682 	sta->flags |= WLAN_STA_PENDING_PASN_FILS_ERP;
3683 
3684 	fils->state = PASN_FILS_STATE_PENDING_AS;
3685 
3686 	/*
3687 	 * Calculate pending PMKID here so that we do not need to maintain a
3688 	 * copy of the EAP-Initiate/Reautt message.
3689 	 */
3690 	fils_pmkid_erp(pasn_get_akmp(pasn),
3691 		       wpabuf_head(fils_wd), wpabuf_len(fils_wd),
3692 		       fils->erp_pmkid);
3693 
3694 	wpabuf_free(fils_wd);
3695 	return 0;
3696 #endif /* CONFIG_NO_RADIUS */
3697 }
3698 
3699 #endif /* CONFIG_FILS */
3700 
3701 
hapd_pasn_send_mlme(void * ctx,const u8 * data,size_t data_len,int noack,unsigned int freq,unsigned int wait)3702 static int hapd_pasn_send_mlme(void *ctx, const u8 *data, size_t data_len,
3703 			       int noack, unsigned int freq, unsigned int wait)
3704 {
3705 	struct hostapd_data *hapd = ctx;
3706 
3707 	return hostapd_drv_send_mlme(hapd, data, data_len, 0, NULL, 0, 0);
3708 }
3709 
3710 
3711 static struct rsn_pmksa_cache_entry *
pmksa_cache_search(void * ctx,const u8 * spa,const u8 * pmkid,bool is_ml)3712 pmksa_cache_search(void *ctx, const u8 *spa, const u8 *pmkid, bool is_ml)
3713 {
3714 	struct hostapd_data *hapd = ctx;
3715 	struct rsn_pmksa_cache_entry *entry;
3716 	struct rsn_pmksa_cache *pmksa = wpa_auth_get_pmksa_cache(hapd->wpa_auth,
3717 								 is_ml);
3718 
3719 	entry = pmksa_cache_auth_get(pmksa, spa, pmkid);
3720 	if (entry)
3721 		return entry;
3722 
3723 #ifdef CONFIG_IEEE80211BE
3724 	if (is_ml) {
3725 		struct hostapd_data *tmp_hapd;
3726 
3727 		/* Search in link caches of each affiliated AP MLD link */
3728 		for_each_mld_link(tmp_hapd, hapd) {
3729 			pmksa = wpa_auth_get_pmksa_cache(tmp_hapd->wpa_auth,
3730 							 false);
3731 			entry = pmksa_cache_auth_get(pmksa, spa, pmkid);
3732 			if (entry)
3733 				return entry;
3734 		}
3735 	} else if (hapd->conf->mld_ap) {
3736 		/* Search in the MLD cache */
3737 		pmksa = wpa_auth_get_pmksa_cache(hapd->wpa_auth, true);
3738 		entry = pmksa_cache_auth_get(pmksa, spa, pmkid);
3739 		if (entry)
3740 			return entry;
3741 	}
3742 #endif /* CONFIG_IEEE80211BE */
3743 
3744 	return NULL;
3745 }
3746 
3747 
3748 #ifdef CONFIG_ENC_ASSOC
eppke_set_key(void * ctx,enum wpa_alg alg,const u8 * addr,int vlan_id,const u8 * key,size_t key_len)3749 static int eppke_set_key(void *ctx, enum wpa_alg alg, const u8 *addr,
3750 			 int vlan_id, const u8 *key, size_t key_len)
3751 {
3752 	struct hostapd_data *hapd = ctx;
3753 
3754 	return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr,
3755 				   0, vlan_id, 1, NULL, 0, key, key_len,
3756 				   KEY_FLAG_PAIRWISE_RX_TX);
3757 }
3758 #else /* CONFIG_ENC_ASSOC */
3759 #define eppke_set_key NULL
3760 #endif /* CONFIG_ENC_ASSOC */
3761 
3762 
3763 #ifdef CONFIG_SAE
3764 /**
3765  * hapd_pasn_get_pt_for_pw_id - Look up SAE PT for a password identifier
3766  *
3767  * Called by the PASN responder when an SAE commit frame contains a password
3768  * identifier that was not known at PASN-setup time (e.g., for EPPKE where
3769  * the PT cannot be pre-selected before the commit is received).
3770  *
3771  * For plaintext identifiers sae_get_password() returns the pre-computed PT
3772  * (pw_entry->pt). We must NOT return that pointer directly because the
3773  * caller will free it; clone it so the caller always owns the returned PT.
3774  * counter and dec_pw_id are set to 0/NULL for plaintext identifiers.
3775  *
3776  * For encrypted password identifiers the PT is not pre-computed (the
3777  * pre-computed PT is keyed to the decrypted identifier, not the encrypted
3778  * one). Derive the PT on-the-fly using the raw (encrypted) identifier as
3779  * the salt, mirroring what auth_build_sae_commit() does for regular SAE.
3780  * The decrypted blob is parsed to extract the real password identifier
3781  * (dec_pw_id) and the counter, both returned to the caller. dec_pw_id is
3782  * heap-allocated and the caller takes ownership (must free with os_free()).
3783  */
3784 static struct sae_pt *
hapd_pasn_get_pt_for_pw_id(void * ctx,const u8 * pw_id,size_t pw_id_len,int group,const char ** password,unsigned int * counter,u8 ** dec_pw_id,size_t * dec_pw_id_len)3785 hapd_pasn_get_pt_for_pw_id(void *ctx, const u8 *pw_id, size_t pw_id_len,
3786 			    int group, const char **password,
3787 			    unsigned int *counter,
3788 			    u8 **dec_pw_id, size_t *dec_pw_id_len)
3789 {
3790 	struct hostapd_data *hapd = ctx;
3791 	struct sae_password_entry *pw_entry = NULL;
3792 	struct sae_pt *pt = NULL;
3793 	int groups[2] = { group, 0 };
3794 
3795 	*counter = 0;
3796 	*dec_pw_id = NULL;
3797 	*dec_pw_id_len = 0;
3798 
3799 	*password = sae_get_password(hapd, NULL, pw_id, pw_id_len, &pw_entry,
3800 				     &pt, NULL);
3801 	if (!*password)
3802 		return NULL;
3803 
3804 	if (pt) {
3805 		/* Plaintext identifier: sae_get_password() found a
3806 		 * pre-computed PT.  Clone it so the caller can free it
3807 		 * without affecting the password entry's own PT.
3808 		 * counter and dec_pw_id stay 0/NULL for plaintext. */
3809 		return sae_derive_pt(groups, hapd->conf->ssid.ssid,
3810 				     hapd->conf->ssid.ssid_len,
3811 				     (const u8 *) pw_entry->password,
3812 				     os_strlen(pw_entry->password),
3813 				     pw_id, pw_id_len);
3814 	}
3815 
3816 	if (pw_entry) {
3817 		/* Encrypted identifier: no pre-computed PT exists for the
3818 		 * raw (encrypted) identifier. Decrypt the blob once to
3819 		 * extract the real password identifier (dec_pw_id) and the
3820 		 * counter, then derive the PT using the encrypted bytes as
3821 		 * the salt (matching auth_build_sae_commit()).
3822 		 *
3823 		 * Decrypted format:
3824 		 *   4-byte date | Password ID | NUL padding | 4-byte counter
3825 		 */
3826 		if (hapd->conf->sae_pw_id_key &&
3827 		    pw_id_len > 4 + 4 + AES_BLOCK_SIZE) {
3828 			u8 *plain;
3829 			size_t plain_len;
3830 
3831 			plain = os_malloc(pw_id_len);
3832 			if (plain &&
3833 			    aes_siv_decrypt(
3834 				    wpabuf_head(hapd->conf->sae_pw_id_key),
3835 				    wpabuf_len(hapd->conf->sae_pw_id_key),
3836 				    pw_id, pw_id_len,
3837 				    0, NULL, NULL, plain) == 0) {
3838 				const u8 *id, *pos;
3839 
3840 				plain_len = pw_id_len - AES_BLOCK_SIZE;
3841 				/* Counter is the last 4 bytes */
3842 				*counter = WPA_GET_BE32(plain + plain_len - 4);
3843 				wpa_printf(MSG_DEBUG,
3844 					   "SAE: Generation time %u counter %u",
3845 					   WPA_GET_BE32(plain), *counter);
3846 				/* Real password ID starts at byte 4,
3847 				 * NUL-terminated before the counter */
3848 				id = plain + 4;
3849 				pos = id;
3850 				while (pos < plain + plain_len - 4) {
3851 					if (*pos == 0x00)
3852 						break;
3853 					pos++;
3854 				}
3855 				*dec_pw_id_len = pos - id;
3856 				wpa_hexdump_ascii(
3857 					MSG_DEBUG,
3858 					"SAE: Decrypted password identifier",
3859 					id, *dec_pw_id_len);
3860 				*dec_pw_id = os_memdup(id, *dec_pw_id_len);
3861 				if (!*dec_pw_id)
3862 					*dec_pw_id_len = 0;
3863 			}
3864 			os_free(plain);
3865 		}
3866 
3867 		pt = sae_derive_pt(groups, hapd->conf->ssid.ssid,
3868 				   hapd->conf->ssid.ssid_len,
3869 				   (const u8 *) pw_entry->password,
3870 				   os_strlen(pw_entry->password),
3871 				   pw_id, pw_id_len);
3872 		if (!pt)
3873 			wpa_printf(MSG_DEBUG,
3874 				   "PASN: Failed to derive PT for encrypted password identifier");
3875 		return pt;
3876 	}
3877 
3878 	return NULL;
3879 }
3880 #endif /* CONFIG_SAE */
3881 
3882 
hapd_initialize_pasn(struct hostapd_data * hapd,struct sta_info * sta)3883 static void hapd_initialize_pasn(struct hostapd_data *hapd,
3884 				 struct sta_info *sta)
3885 {
3886 	struct pasn_data *pasn = sta->pasn;
3887 
3888 	pasn_register_callbacks(pasn, hapd, hapd_pasn_send_mlme,
3889 				NULL, eppke_set_key, pmksa_cache_search);
3890 	pasn_set_bssid(pasn, hapd->own_addr);
3891 	pasn_set_own_addr(pasn, hapd->own_addr);
3892 #ifdef CONFIG_PMKSA_PRIVACY
3893 	pasn->pmksa_caching_privacy = hapd->conf->pmksa_caching_privacy;
3894 #endif /* CONFIG_PMKSA_PRIVACY */
3895 #if defined(CONFIG_IEEE80211BE) && defined(CONFIG_ENC_ASSOC)
3896 	/* Per IEEE802.11bi/D4.0, 12.16.9 (Enhanced privacy
3897 	 * protection key exchange), if (Re)Association frame
3898 	 * Encryption is activated, KEK in PASN shall be true.
3899 	 */
3900 	if (hapd->conf->assoc_frame_encryption)
3901 		pasn->derive_kek = true;
3902 	if (hapd->conf->mld_ap)
3903 		pasn_set_own_mld_addr(pasn, hapd->mld->mld_addr);
3904 #endif /* CONFIG_IEEE80211BE && CONFIG_ENC_ASSOC */
3905 	pasn_set_peer_addr(pasn, sta->addr);
3906 	pasn_set_wpa_key_mgmt(pasn, hapd->conf->wpa_key_mgmt |
3907 			      hapd->conf->rsn_override_key_mgmt |
3908 			      hapd->conf->rsn_override_key_mgmt_2);
3909 	pasn_set_rsn_pairwise(pasn, hapd->conf->rsn_pairwise |
3910 			      hapd->conf->rsn_override_pairwise);
3911 	pasn_set_mfp(pasn, hapd->conf->ieee80211w);
3912 	os_free(pasn->pasn_groups);
3913 	pasn->pasn_groups = int_array_dup(hapd->conf->pasn_groups);
3914 	pasn->noauth = hapd->conf->pasn_noauth;
3915 #ifdef CONFIG_ENC_ASSOC
3916 	pasn->eppke_unauth = hapd->conf->eppke_unauth;
3917 #endif /* CONFIG_ENC_ASSOC */
3918 	if (hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_AP)
3919 		pasn_enable_kdk_derivation(pasn);
3920 
3921 #ifdef CONFIG_TESTING_OPTIONS
3922 	pasn->corrupt_mic = hapd->conf->pasn_corrupt_mic;
3923 	pasn->pasn_test_groups = hapd->conf->pasn_test_groups;
3924 	if (hapd->conf->force_kdk_derivation)
3925 		pasn_enable_kdk_derivation(pasn);
3926 #endif /* CONFIG_TESTING_OPTIONS */
3927 	pasn->use_anti_clogging = use_anti_clogging(hapd);
3928 	sae_get_password(hapd, sta, NULL, 0, NULL, &pasn->pt, NULL);
3929 #ifdef CONFIG_SAE
3930 	/* Register a callback so the PASN responder can look up the correct
3931 	 * SAE PT when the STA's commit frame contains a password identifier
3932 	 * that was not known at setup time (EPPKE cases).
3933 	 */
3934 	pasn->get_pt_for_pw_id = hapd_pasn_get_pt_for_pw_id;
3935 #endif /* CONFIG_SAE */
3936 	pasn_set_rsne(pasn, wpa_auth_get_wpa_ie(hapd->wpa_auth,
3937 						&pasn->rsn_ie_len));
3938 	pasn_set_rsnxe_ie(pasn, hostapd_wpa_ie(hapd, WLAN_EID_RSNX));
3939 	pasn->disable_pmksa_caching = hapd->conf->disable_pmksa_caching;
3940 #ifdef CONFIG_ENC_ASSOC
3941 	pasn->tk_configured = false;
3942 #endif /* CONFIG_ENC_ASSOC */
3943 	pasn_set_responder_pmksa(
3944 		pasn,
3945 		wpa_auth_get_pmksa_cache(hapd->wpa_auth,
3946 					 ap_sta_is_epp(sta) ?
3947 					 ap_sta_is_mld(hapd, sta) : false));
3948 
3949 	pasn->comeback_after = hapd->conf->pasn_comeback_after;
3950 	pasn->comeback_idx = hapd->comeback_idx;
3951 	pasn->comeback_key =  hapd->comeback_key;
3952 	pasn->comeback_pending_idx = hapd->comeback_pending_idx;
3953 }
3954 
3955 
pasn_set_keys_from_cache(struct hostapd_data * hapd,const u8 * own_addr,const u8 * sta_addr,int cipher,int akmp)3956 static int pasn_set_keys_from_cache(struct hostapd_data *hapd,
3957 				    const u8 *own_addr, const u8 *sta_addr,
3958 				    int cipher, int akmp)
3959 {
3960 	struct ptksa_cache_entry *entry;
3961 
3962 	entry = ptksa_cache_get(hapd->ptksa, sta_addr, cipher);
3963 	if (!entry) {
3964 		wpa_printf(MSG_DEBUG, "PASN: peer " MACSTR
3965 			   " not present in PTKSA cache", MAC2STR(sta_addr));
3966 		return -1;
3967 	}
3968 
3969 	if (!ether_addr_equal(entry->own_addr, own_addr)) {
3970 		wpa_printf(MSG_DEBUG,
3971 			   "PASN: own addr " MACSTR " and PTKSA entry own addr "
3972 			   MACSTR " differ",
3973 			   MAC2STR(own_addr), MAC2STR(entry->own_addr));
3974 		return -1;
3975 	}
3976 
3977 	wpa_printf(MSG_DEBUG, "PASN: " MACSTR " present in PTKSA cache",
3978 		   MAC2STR(sta_addr));
3979 	hostapd_drv_set_secure_ranging_ctx(hapd, own_addr, sta_addr, cipher,
3980 					   entry->ptk.tk_len, entry->ptk.tk,
3981 					   entry->ptk.ltf_keyseed_len,
3982 					   entry->ptk.ltf_keyseed, 0);
3983 
3984 	return 0;
3985 }
3986 
3987 
hapd_pasn_update_params(struct hostapd_data * hapd,struct sta_info * sta,const struct ieee80211_mgmt * mgmt,size_t len)3988 static void hapd_pasn_update_params(struct hostapd_data *hapd,
3989 				    struct sta_info *sta,
3990 				    const struct ieee80211_mgmt *mgmt,
3991 				    size_t len)
3992 {
3993 	struct pasn_data *pasn = sta->pasn;
3994 	struct ieee802_11_elems elems;
3995 	struct wpa_ie_data rsn_data;
3996 #ifdef CONFIG_FILS
3997 	struct wpa_pasn_params_data pasn_params;
3998 	struct wpabuf *wrapped_data = NULL;
3999 #endif /* CONFIG_FILS */
4000 	int akmp;
4001 
4002 	if (ieee802_11_parse_elems(mgmt->u.auth.variable,
4003 				   len - offsetof(struct ieee80211_mgmt,
4004 						  u.auth.variable),
4005 				   &elems, 0) == ParseFailed) {
4006 		wpa_printf(MSG_DEBUG,
4007 			   "PASN: Failed parsing Authentication frame");
4008 		return;
4009 	}
4010 
4011 	if (!elems.rsn_ie ||
4012 	    wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4013 				 &rsn_data)) {
4014 		wpa_printf(MSG_DEBUG, "PASN: Failed parsing RSNE");
4015 		return;
4016 	}
4017 
4018 	if (!(rsn_data.key_mgmt & pasn->wpa_key_mgmt) ||
4019 	    !(rsn_data.pairwise_cipher & pasn->rsn_pairwise)) {
4020 		wpa_printf(MSG_DEBUG, "PASN: Mismatch in AKMP/cipher");
4021 		return;
4022 	}
4023 
4024 #ifdef CONFIG_ENC_ASSOC
4025 	pasn->auth_alg = mgmt->u.auth.auth_alg;
4026 	pasn->authorized = ap_sta_is_authorized(sta);
4027 #ifdef CONFIG_IEEE80211BE
4028 	pasn->is_ml_peer = sta->mld_info.mld_sta;
4029 #endif /* CONFIG_IEEE80211BE */
4030 #endif /* CONFIG_ENC_ASSOC */
4031 
4032 	pasn_set_akmp(pasn, rsn_data.key_mgmt);
4033 	pasn_set_cipher(pasn, rsn_data.pairwise_cipher);
4034 
4035 	if (pasn->derive_kdk &&
4036 	    !ieee802_11_rsnx_capab_len(elems.rsnxe, elems.rsnxe_len,
4037 				       WLAN_RSNX_CAPAB_SECURE_LTF))
4038 		pasn_disable_kdk_derivation(pasn);
4039 #ifdef CONFIG_TESTING_OPTIONS
4040 	if (hapd->conf->force_kdk_derivation)
4041 		pasn_enable_kdk_derivation(pasn);
4042 #endif /* CONFIG_TESTING_OPTIONS */
4043 	akmp = pasn_get_akmp(pasn);
4044 
4045 	if (wpa_key_mgmt_ft(akmp) && rsn_data.num_pmkid) {
4046 #ifdef CONFIG_IEEE80211R_AP
4047 		pasn->pmk_r1_len = 0;
4048 		wpa_ft_fetch_pmk_r1(hapd->wpa_auth, sta->addr,
4049 				    rsn_data.pmkid,
4050 				    pasn->pmk_r1, &pasn->pmk_r1_len, NULL,
4051 				    NULL, NULL, NULL,
4052 				    NULL, NULL, NULL);
4053 #endif /* CONFIG_IEEE80211R_AP */
4054 	}
4055 #ifdef CONFIG_FILS
4056 	if (akmp != WPA_KEY_MGMT_FILS_SHA256 &&
4057 	    akmp != WPA_KEY_MGMT_FILS_SHA384)
4058 		return;
4059 	if (!elems.pasn_params ||
4060 	    wpa_pasn_parse_parameter_ie(elems.pasn_params - 3,
4061 					elems.pasn_params_len + 3,
4062 					false, &pasn_params)) {
4063 		wpa_printf(MSG_DEBUG,
4064 			   "PASN: Failed validation of PASN Parameters element");
4065 		return;
4066 	}
4067 	if (pasn_params.wrapped_data_format != WPA_PASN_WRAPPED_DATA_NO) {
4068 		wrapped_data = ieee802_11_defrag(elems.wrapped_data,
4069 						 elems.wrapped_data_len, true);
4070 		if (!wrapped_data) {
4071 			wpa_printf(MSG_DEBUG, "PASN: Missing wrapped data");
4072 			return;
4073 		}
4074 		if (pasn_wd_handle_fils(hapd, sta, wrapped_data))
4075 			wpa_printf(MSG_DEBUG,
4076 				   "PASN: Failed processing FILS wrapped data");
4077 		else
4078 			pasn->fils_wd_valid = true;
4079 	}
4080 	wpabuf_free(wrapped_data);
4081 #endif /* CONFIG_FILS */
4082 }
4083 
4084 
handle_auth_pasn(struct hostapd_data * hapd,struct sta_info * sta,const struct ieee80211_mgmt * mgmt,size_t len,u16 trans_seq,u16 status)4085 static void handle_auth_pasn(struct hostapd_data *hapd, struct sta_info *sta,
4086 			     const struct ieee80211_mgmt *mgmt, size_t len,
4087 			     u16 trans_seq, u16 status)
4088 {
4089 	int ret;
4090 #ifdef CONFIG_P2P
4091 	struct ieee802_11_elems elems;
4092 
4093 	if (len < 24) {
4094 		wpa_printf(MSG_DEBUG, "PASN: Too short Management frame");
4095 		return;
4096 	}
4097 
4098 	if (ieee802_11_parse_elems(mgmt->u.auth.variable,
4099 				   len - offsetof(struct ieee80211_mgmt,
4100 						  u.auth.variable),
4101 				   &elems, 1) == ParseFailed) {
4102 		wpa_printf(MSG_DEBUG,
4103 			   "PASN: Failed parsing Authentication frame");
4104 		return;
4105 	}
4106 
4107 	if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) ==
4108 	    (P2P_ENABLED | P2P_GROUP_OWNER) &&
4109 	    hapd->p2p && elems.p2p2_ie && elems.p2p2_ie_len) {
4110 		p2p_pasn_auth_rx(hapd->p2p, mgmt, len, hapd->iface->freq);
4111 		return;
4112 	}
4113 #endif /* CONFIG_P2P */
4114 
4115 	if (hapd->conf->wpa != WPA_PROTO_RSN) {
4116 		wpa_printf(MSG_INFO, "PASN: RSN is not configured");
4117 		return;
4118 	}
4119 
4120 	wpa_printf(MSG_INFO, "PASN authentication: sta=" MACSTR,
4121 		   MAC2STR(sta->addr));
4122 
4123 	if (trans_seq == WLAN_AUTH_TR_SEQ_PASN_AUTH1) {
4124 		if (sta->pasn && sta->auth_alg != WLAN_AUTH_EPPKE) {
4125 			wpa_printf(MSG_DEBUG,
4126 				   "PASN: Not expecting transaction == 1");
4127 			return;
4128 		}
4129 
4130 		if (status != WLAN_STATUS_SUCCESS) {
4131 			wpa_printf(MSG_DEBUG,
4132 				   "PASN: Failure status in transaction == 1");
4133 			return;
4134 		}
4135 
4136 		if (!sta->pasn)
4137 			sta->pasn = pasn_data_init();
4138 		if (!sta->pasn) {
4139 			wpa_printf(MSG_DEBUG,
4140 				   "PASN: Failed to allocate PASN context");
4141 			return;
4142 		}
4143 
4144 		hapd_initialize_pasn(hapd, sta);
4145 
4146 		hapd_pasn_update_params(hapd, sta, mgmt, len);
4147 		ret = handle_auth_pasn_1(sta->pasn, hapd->own_addr, sta->addr,
4148 					 mgmt, len, false);
4149 		wpabuf_free(sta->pasn->frame);
4150 		sta->pasn->frame = NULL;
4151 		if (ret < 0) {
4152 			hostapd_drv_set_secure_ranging_ctx(hapd, hapd->own_addr,
4153 							   sta->addr, 0, 0,
4154 							   NULL, 0, NULL, 1);
4155 			ap_free_sta(hapd, sta);
4156 		}
4157 	} else if (trans_seq == WLAN_AUTH_TR_SEQ_PASN_AUTH3) {
4158 		if (!sta->pasn) {
4159 			wpa_printf(MSG_DEBUG,
4160 				   "PASN: Not expecting transaction == 3");
4161 			return;
4162 		}
4163 
4164 		if (status != WLAN_STATUS_SUCCESS) {
4165 			wpa_printf(MSG_DEBUG,
4166 				   "PASN: Failure status in transaction == 3");
4167 			ap_free_sta_pasn(hapd, sta);
4168 			return;
4169 		}
4170 
4171 		ret = handle_auth_pasn_3(sta->pasn, hapd->own_addr, sta->addr,
4172 					 mgmt, len);
4173 		if (ret == 0) {
4174 #ifdef CONFIG_ENC_ASSOC
4175 			if (ap_sta_is_epp(sta)) {
4176 				sta->auth_alg = WLAN_AUTH_EPPKE;
4177 				sta->flags |= WLAN_STA_AUTH;
4178 			}
4179 #endif /* CONFIG_ENC_ASSOC */
4180 			ptksa_cache_add(hapd->ptksa, hapd->own_addr, sta->addr,
4181 					pasn_get_cipher(sta->pasn), 43200,
4182 					pasn_get_ptk(sta->pasn), NULL, NULL,
4183 					pasn_get_akmp(sta->pasn),
4184 					sta->pasn->auth_alg);
4185 #ifdef CONFIG_ENC_ASSOC
4186 			/* TODO: Support VLAN ID assignment based on configured
4187 			 * SAE passwords. */
4188 			if (ap_sta_is_epp(sta) && !sta->pasn->tk_configured &&
4189 			    sta->pasn->eppke_set_key)
4190 				sta->pasn->eppke_set_key(
4191 					sta->pasn->cb_ctx,
4192 					wpa_cipher_to_alg(sta->pasn->cipher),
4193 					sta->addr, 0,
4194 					sta->pasn->ptk.tk,
4195 					sta->pasn->ptk.tk_len);
4196 #endif /* CONFIG_ENC_ASSOC */
4197 			if (!ap_sta_is_epp(sta))
4198 				pasn_set_keys_from_cache(
4199 					hapd, hapd->own_addr,
4200 					sta->addr,
4201 					pasn_get_cipher(sta->pasn),
4202 					pasn_get_akmp(sta->pasn));
4203 		}
4204 		if (!ap_sta_is_epp(sta) ||
4205 		    (ret < 0 &&
4206 		     ap_sta_is_epp(sta) && !ap_sta_is_authorized(sta)))
4207 			ap_free_sta(hapd, sta);
4208 
4209 	} else {
4210 		wpa_printf(MSG_DEBUG,
4211 			   "PASN: Invalid transaction %u - ignore", trans_seq);
4212 	}
4213 }
4214 
4215 #endif /* CONFIG_PASN */
4216 
4217 
handle_auth(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int rssi,int from_queue)4218 static void handle_auth(struct hostapd_data *hapd,
4219 			const struct ieee80211_mgmt *mgmt, size_t len,
4220 			int rssi, int from_queue)
4221 {
4222 	u16 auth_alg, auth_transaction, status_code;
4223 	u16 resp = WLAN_STATUS_SUCCESS;
4224 	struct sta_info *sta = NULL;
4225 	int res, reply_res;
4226 	u16 fc;
4227 	const u8 *challenge = NULL;
4228 	u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
4229 	size_t resp_ies_len = 0;
4230 	u16 seq_ctrl;
4231 	struct radius_sta rad_info;
4232 	const u8 *dst, *sa;
4233 	bool mld_sta = false;
4234 
4235 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
4236 		wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
4237 			   (unsigned long) len);
4238 		return;
4239 	}
4240 
4241 #ifdef CONFIG_TESTING_OPTIONS
4242 	if (hapd->iconf->ignore_auth_probability > 0.0 &&
4243 	    drand48() < hapd->iconf->ignore_auth_probability) {
4244 		wpa_printf(MSG_INFO,
4245 			   "TESTING: ignoring auth frame from " MACSTR,
4246 			   MAC2STR(mgmt->sa));
4247 		return;
4248 	}
4249 #endif /* CONFIG_TESTING_OPTIONS */
4250 
4251 	sa = mgmt->sa;
4252 #ifdef CONFIG_IEEE80211BE
4253 	/*
4254 	 * Handle MLO authentication before the station is added to hostapd and
4255 	 * the driver so that the station MLD MAC address would be used in both
4256 	 * hostapd and the driver.
4257 	 */
4258 	sa = hostapd_process_ml_auth(hapd, mgmt, len);
4259 	if (sa)
4260 		mld_sta = true;
4261 	else
4262 		sa = mgmt->sa;
4263 #endif /* CONFIG_IEEE80211BE */
4264 
4265 	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4266 	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
4267 	status_code = le_to_host16(mgmt->u.auth.status_code);
4268 	fc = le_to_host16(mgmt->frame_control);
4269 	seq_ctrl = le_to_host16(mgmt->seq_ctrl);
4270 
4271 	if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
4272 	    2 + WLAN_AUTH_CHALLENGE_LEN &&
4273 	    mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
4274 	    mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
4275 		challenge = &mgmt->u.auth.variable[2];
4276 
4277 	wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
4278 		   "auth_transaction=%d status_code=%d protected=%d%s "
4279 		   "seq_ctrl=0x%x%s%s",
4280 		   MAC2STR(sa), auth_alg, auth_transaction,
4281 		   status_code, !!(fc & WLAN_FC_PROTECTED),
4282 		   challenge ? " challenge" : "",
4283 		   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "",
4284 		   from_queue ? " (from queue)" : "");
4285 
4286 #ifdef CONFIG_NO_RC4
4287 	if (auth_alg == WLAN_AUTH_SHARED_KEY) {
4288 		wpa_printf(MSG_INFO,
4289 			   "Unsupported authentication algorithm (%d)",
4290 			   auth_alg);
4291 		resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
4292 		goto fail;
4293 	}
4294 #endif /* CONFIG_NO_RC4 */
4295 
4296 #ifdef CONFIG_IEEE8021X_AUTH
4297 	if (auth_alg == WLAN_AUTH_802_1X &&
4298 	    !hapd->conf->eap_using_authentication_frames) {
4299 		wpa_printf(MSG_INFO,
4300 			   "Unsupported authentication algorithm (%d)",
4301 			   auth_alg);
4302 		resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
4303 		goto fail;
4304 	}
4305 #endif /* CONFIG_IEEE8021X_AUTH */
4306 
4307 	if (hapd->tkip_countermeasures) {
4308 		wpa_printf(MSG_DEBUG,
4309 			   "Ongoing TKIP countermeasures (Michael MIC failure) - reject authentication");
4310 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4311 		goto fail;
4312 	}
4313 
4314 	if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
4315 	       auth_alg == WLAN_AUTH_OPEN) ||
4316 #ifdef CONFIG_IEEE80211R_AP
4317 	      (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
4318 	       auth_alg == WLAN_AUTH_FT) ||
4319 #endif /* CONFIG_IEEE80211R_AP */
4320 #ifdef CONFIG_SAE
4321 	      (hapd->conf->wpa &&
4322 	       wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt |
4323 				hapd->conf->rsn_override_key_mgmt |
4324 				hapd->conf->rsn_override_key_mgmt_2) &&
4325 	       auth_alg == WLAN_AUTH_SAE) ||
4326 #endif /* CONFIG_SAE */
4327 #ifdef CONFIG_FILS
4328 	      (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
4329 	       auth_alg == WLAN_AUTH_FILS_SK) ||
4330 	      (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
4331 	       hapd->conf->fils_dh_group &&
4332 	       auth_alg == WLAN_AUTH_FILS_SK_PFS) ||
4333 #endif /* CONFIG_FILS */
4334 #ifdef CONFIG_PASN
4335 	      (hapd->conf->wpa &&
4336 	       (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PASN) &&
4337 	       auth_alg == WLAN_AUTH_PASN) ||
4338 #endif /* CONFIG_PASN */
4339 #ifdef CONFIG_ENC_ASSOC
4340 	      (hapd->conf->wpa &&
4341 	       ((hapd->conf->wpa_key_mgmt |
4342 		 hapd->conf->rsn_override_key_mgmt |
4343 		 hapd->conf->rsn_override_key_mgmt_2) & WPA_KEY_MGMT_EPPKE) &&
4344 	       hapd->conf->assoc_frame_encryption &&
4345 	       auth_alg == WLAN_AUTH_EPPKE) ||
4346 #endif /* CONFIG_ENC_ASSOC */
4347 #ifdef CONFIG_IEEE8021X_AUTH
4348 	      (hapd->conf->wpa &&
4349 	       wpa_key_mgmt_wpa_ieee8021x(hapd->conf->wpa_key_mgmt) &&
4350 	       auth_alg == WLAN_AUTH_802_1X) ||
4351 #endif /* CONFIG_IEEE8021X_AUTH */
4352 	      ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
4353 	       auth_alg == WLAN_AUTH_SHARED_KEY))) {
4354 		wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
4355 			   auth_alg);
4356 		resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
4357 		goto fail;
4358 	}
4359 
4360 	if (!(auth_transaction == 1 ||
4361 #ifdef CONFIG_SAE
4362 	      (auth_alg == WLAN_AUTH_SAE &&
4363 	       auth_transaction == WLAN_AUTH_TR_SEQ_SAE_CONFIRM) ||
4364 #endif /* CONFIG_SAE */
4365 #ifdef CONFIG_PASN
4366 	      (auth_alg == WLAN_AUTH_PASN &&
4367 	       auth_transaction == WLAN_AUTH_TR_SEQ_PASN_AUTH3) ||
4368 #endif /* CONFIG_PASN */
4369 #ifdef CONFIG_ENC_ASSOC
4370 	      (auth_alg == WLAN_AUTH_EPPKE &&
4371 	       auth_transaction == WLAN_AUTH_TR_SEQ_PASN_AUTH3) ||
4372 #endif /* CONFIG_ENC_ASSOC */
4373 #ifdef CONFIG_IEEE8021X_AUTH
4374 	      /* EAP over Auth involves variable number of frames depending
4375 	       * on the EAP authentication method */
4376 	      auth_alg == WLAN_AUTH_802_1X ||
4377 #endif /* CONFIG_IEEE8021X_AUTH */
4378 	      (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
4379 		wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
4380 			   auth_transaction);
4381 		resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
4382 		goto fail;
4383 	}
4384 
4385 	if (!hostapd_acceptable_sta_addr(hapd, mgmt->sa, sa, mld_sta)) {
4386 		wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
4387 			   MAC2STR(sa));
4388 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4389 		goto fail;
4390 	}
4391 
4392 	if (hapd->conf->no_auth_if_seen_on) {
4393 		struct hostapd_data *other;
4394 
4395 		other = sta_track_seen_on(hapd->iface, sa,
4396 					  hapd->conf->no_auth_if_seen_on);
4397 		if (other) {
4398 			u8 *pos;
4399 			u32 info;
4400 			u8 op_class, channel, phytype;
4401 
4402 			wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
4403 				   MACSTR " since STA has been seen on %s",
4404 				   hapd->conf->iface, MAC2STR(sa),
4405 				   hapd->conf->no_auth_if_seen_on);
4406 
4407 			resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
4408 			pos = &resp_ies[0];
4409 			*pos++ = WLAN_EID_NEIGHBOR_REPORT;
4410 			*pos++ = 13;
4411 			os_memcpy(pos, other->own_addr, ETH_ALEN);
4412 			pos += ETH_ALEN;
4413 			info = 0; /* TODO: BSSID Information */
4414 			WPA_PUT_LE32(pos, info);
4415 			pos += 4;
4416 			if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
4417 				phytype = 8; /* dmg */
4418 			else if (other->iconf->ieee80211ac)
4419 				phytype = 9; /* vht */
4420 			else if (other->iconf->ieee80211n)
4421 				phytype = 7; /* ht */
4422 			else if (other->iconf->hw_mode ==
4423 				 HOSTAPD_MODE_IEEE80211A)
4424 				phytype = 4; /* ofdm */
4425 			else if (other->iconf->hw_mode ==
4426 				 HOSTAPD_MODE_IEEE80211G)
4427 				phytype = 6; /* erp */
4428 			else
4429 				phytype = 5; /* hrdsss */
4430 			if (ieee80211_freq_to_channel_ext(
4431 				    hostapd_hw_get_freq(other,
4432 							other->iconf->channel),
4433 				    other->iconf->secondary_channel,
4434 				    other->iconf->ieee80211ac,
4435 				    &op_class, &channel) == NUM_HOSTAPD_MODES) {
4436 				op_class = 0;
4437 				channel = other->iconf->channel;
4438 			}
4439 			*pos++ = op_class;
4440 			*pos++ = channel;
4441 			*pos++ = phytype;
4442 			resp_ies_len = pos - &resp_ies[0];
4443 			goto fail;
4444 		}
4445 	}
4446 
4447 	res = ieee802_11_allowed_address(hapd, sa, (const u8 *) mgmt, len,
4448 					 &rad_info);
4449 	if (res == HOSTAPD_ACL_REJECT) {
4450 		wpa_msg(hapd->msg_ctx, MSG_DEBUG,
4451 			"Ignore Authentication frame from " MACSTR
4452 			" due to ACL reject", MAC2STR(sa));
4453 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4454 		goto fail;
4455 	}
4456 	if (res == HOSTAPD_ACL_PENDING)
4457 		return;
4458 
4459 #ifdef CONFIG_IEEE80211BE
4460 	if (mld_sta) {
4461 		res = ieee802_11_allowed_address(hapd, mgmt->sa,
4462 						 (const u8 *) mgmt, len,
4463 						 &rad_info);
4464 		if (res == HOSTAPD_ACL_REJECT) {
4465 			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
4466 				"Ignore Authentication frame from " MACSTR
4467 				" due to ACL reject", MAC2STR(mgmt->sa));
4468 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4469 			goto fail;
4470 		}
4471 		if (res == HOSTAPD_ACL_PENDING)
4472 			return;
4473 	}
4474 #endif /* CONFIG_IEEE80211BE */
4475 
4476 #ifdef CONFIG_SAE
4477 	if (auth_alg == WLAN_AUTH_SAE && !from_queue &&
4478 	    (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT ||
4479 	     (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_CONFIRM &&
4480 	      auth_sae_queued_addr(hapd, sa)))) {
4481 		/* Handle SAE Authentication commit message through a queue to
4482 		 * provide more control for postponing the needed heavy
4483 		 * processing under a possible DoS attack scenario. In addition,
4484 		 * queue SAE Authentication confirm message if there happens to
4485 		 * be a queued commit message from the same peer. This is needed
4486 		 * to avoid reordering Authentication frames within the same
4487 		 * SAE exchange. */
4488 		auth_sae_queue(hapd, mgmt, len, rssi);
4489 		return;
4490 	}
4491 #endif /* CONFIG_SAE */
4492 
4493 	sta = ap_get_sta(hapd, sa);
4494 	if (sta) {
4495 		sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
4496 		sta->ft_over_ds = 0;
4497 		if ((fc & WLAN_FC_RETRY) &&
4498 		    sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4499 		    sta->last_seq_ctrl == seq_ctrl &&
4500 		    sta->last_subtype == WLAN_FC_STYPE_AUTH) {
4501 			hostapd_logger(hapd, sta->addr,
4502 				       HOSTAPD_MODULE_IEEE80211,
4503 				       HOSTAPD_LEVEL_DEBUG,
4504 				       "Drop repeated authentication frame seq_ctrl=0x%x",
4505 				       seq_ctrl);
4506 			return;
4507 		}
4508 #ifdef CONFIG_PASN
4509 		if (auth_alg == WLAN_AUTH_PASN &&
4510 		    (sta->flags & WLAN_STA_ASSOC)) {
4511 			wpa_printf(MSG_DEBUG,
4512 				   "PASN: auth: Existing station: " MACSTR,
4513 				   MAC2STR(sta->addr));
4514 			return;
4515 		}
4516 #endif /* CONFIG_PASN */
4517 	} else {
4518 #ifdef CONFIG_MESH
4519 		if (hapd->conf->mesh & MESH_ENABLED) {
4520 			/* if the mesh peer is not available, we don't do auth.
4521 			 */
4522 			wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
4523 				   " not yet known - drop Authentication frame",
4524 				   MAC2STR(sa));
4525 			/*
4526 			 * Save a copy of the frame so that it can be processed
4527 			 * if a new peer entry is added shortly after this.
4528 			 */
4529 			wpabuf_free(hapd->mesh_pending_auth);
4530 			hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
4531 			os_get_reltime(&hapd->mesh_pending_auth_time);
4532 			return;
4533 		}
4534 #endif /* CONFIG_MESH */
4535 
4536 		sta = ap_sta_add(hapd, sa);
4537 		if (!sta) {
4538 			wpa_printf(MSG_DEBUG, "ap_sta_add() failed");
4539 			resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4540 			goto fail;
4541 		}
4542 	}
4543 
4544 #if defined(CONFIG_ENC_ASSOC) || defined(CONFIG_IEEE8021X_AUTH)
4545 	if (auth_alg == WLAN_AUTH_EPPKE || auth_alg == WLAN_AUTH_802_1X) {
4546 		wpa_printf(MSG_DEBUG, "Mark the station as an EPP peer");
4547 		sta->epp_sta = true;
4548 	}
4549 #endif /* CONFIG_ENC_ASSOC || CONFIG_IEEE8021X_AUTH */
4550 
4551 #ifdef CONFIG_IEEE80211BE
4552 	/* Set the non-AP MLD information based on the initial Authentication
4553 	 * frame. Once the STA entry has been added to the driver, the driver
4554 	 * will translate addresses in the frame and we need to avoid overriding
4555 	 * peer_addr based on mgmt->sa which would have been translated to the
4556 	 * MLD MAC address. */
4557 	if (!sta->added_unassoc && auth_transaction == 1) {
4558 		ap_sta_free_sta_profile(&sta->mld_info);
4559 		os_memset(&sta->mld_info, 0, sizeof(sta->mld_info));
4560 
4561 		if (mld_sta) {
4562 			u8 link_id = hapd->mld_link_id;
4563 
4564 			ap_sta_set_mld(sta, true);
4565 			sta->mld_assoc_link_id = link_id;
4566 
4567 			/*
4568 			 * Set the MLD address as the station address and the
4569 			 * station addresses.
4570 			 */
4571 			os_memcpy(sta->mld_info.common_info.mld_addr, sa,
4572 				  ETH_ALEN);
4573 			os_memcpy(sta->mld_info.links[link_id].peer_addr,
4574 				  mgmt->sa, ETH_ALEN);
4575 			os_memcpy(sta->mld_info.links[link_id].local_addr,
4576 				  hapd->own_addr, ETH_ALEN);
4577 		}
4578 
4579 		if ((!(sta->flags & WLAN_STA_MFP) ||
4580 		     !ap_sta_is_authorized(sta)) && sta->wpa_sm) {
4581 			struct wpa_state_machine *sm = sta->wpa_sm;
4582 
4583 			clear_wpa_sm_for_each_partner_link(hapd, sta);
4584 			clear_wpa_sm_for_all_sta(hapd, sm);
4585 			wpa_auth_sta_deinit(sm);
4586 			sta->wpa_sm = NULL;
4587 		}
4588 	}
4589 #endif /* CONFIG_IEEE80211BE */
4590 
4591 	sta->last_seq_ctrl = seq_ctrl;
4592 	sta->last_subtype = WLAN_FC_STYPE_AUTH;
4593 #ifdef CONFIG_MBO
4594 	sta->auth_rssi = rssi;
4595 #endif /* CONFIG_MBO */
4596 
4597 	res = ieee802_11_set_radius_info(hapd, sta, res, &rad_info);
4598 	if (res) {
4599 		wpa_printf(MSG_DEBUG, "ieee802_11_set_radius_info() failed");
4600 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4601 		goto fail;
4602 	}
4603 
4604 	sta->flags &= ~WLAN_STA_PREAUTH;
4605 	ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
4606 
4607 	/*
4608 	 * If the driver supports full AP client state, add a station to the
4609 	 * driver before sending authentication reply to make sure the driver
4610 	 * has resources, and not to go through the entire authentication and
4611 	 * association handshake, and fail it at the end.
4612 	 *
4613 	 * If this is not the first transaction, in a multi-step authentication
4614 	 * algorithm, the station already exists in the driver
4615 	 * (sta->added_unassoc = 1) so skip it.
4616 	 *
4617 	 * In mesh mode, the station was already added to the driver when the
4618 	 * NEW_PEER_CANDIDATE event is received.
4619 	 *
4620 	 * If PMF was negotiated for the existing association, skip this to
4621 	 * avoid dropping the STA entry and the associated keys. This is needed
4622 	 * to allow the original connection work until the attempt can complete
4623 	 * (re)association, so that unprotected Authentication frame cannot be
4624 	 * used to bypass PMF protection.
4625 	 *
4626 	 * PASN authentication does not require adding/removing station to the
4627 	 * driver so skip this flow in case of PASN authentication.
4628 	 */
4629 	if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
4630 	    (!(sta->flags & WLAN_STA_MFP) || !ap_sta_is_authorized(sta)) &&
4631 	    !(hapd->conf->mesh & MESH_ENABLED) &&
4632 	    !(sta->added_unassoc) && auth_alg != WLAN_AUTH_PASN) {
4633 		if (ap_sta_re_add(hapd, sta) < 0) {
4634 			resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4635 			goto fail;
4636 		}
4637 	}
4638 
4639 	switch (auth_alg) {
4640 	case WLAN_AUTH_OPEN:
4641 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4642 			       HOSTAPD_LEVEL_DEBUG,
4643 			       "authentication OK (open system)");
4644 		sta->flags |= WLAN_STA_AUTH;
4645 		wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
4646 		sta->auth_alg = WLAN_AUTH_OPEN;
4647 		mlme_authenticate_indication(hapd, sta);
4648 		break;
4649 #ifdef CONFIG_WEP
4650 #ifndef CONFIG_NO_RC4
4651 	case WLAN_AUTH_SHARED_KEY:
4652 		resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
4653 				       fc & WLAN_FC_PROTECTED);
4654 		if (resp != 0)
4655 			wpa_printf(MSG_DEBUG,
4656 				   "auth_shared_key() failed: status=%d", resp);
4657 		sta->auth_alg = WLAN_AUTH_SHARED_KEY;
4658 		mlme_authenticate_indication(hapd, sta);
4659 		if (sta->challenge && auth_transaction == 1) {
4660 			resp_ies[0] = WLAN_EID_CHALLENGE;
4661 			resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
4662 			os_memcpy(resp_ies + 2, sta->challenge,
4663 				  WLAN_AUTH_CHALLENGE_LEN);
4664 			resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
4665 		}
4666 		break;
4667 #endif /* CONFIG_NO_RC4 */
4668 #endif /* CONFIG_WEP */
4669 #ifdef CONFIG_IEEE80211R_AP
4670 	case WLAN_AUTH_FT:
4671 		sta->auth_alg = WLAN_AUTH_FT;
4672 		if (sta->wpa_sm == NULL)
4673 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
4674 							sta->addr, NULL);
4675 		if (sta->wpa_sm == NULL) {
4676 			wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
4677 				   "state machine");
4678 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4679 			goto fail;
4680 		}
4681 		wpa_ft_process_auth(sta->wpa_sm,
4682 				    auth_transaction, mgmt->u.auth.variable,
4683 				    len - IEEE80211_HDRLEN -
4684 				    sizeof(mgmt->u.auth),
4685 				    handle_auth_ft_finish, hapd);
4686 		/* handle_auth_ft_finish() callback will complete auth. */
4687 		return;
4688 #endif /* CONFIG_IEEE80211R_AP */
4689 #ifdef CONFIG_SAE
4690 	case WLAN_AUTH_SAE:
4691 #ifdef CONFIG_MESH
4692 		if (status_code == WLAN_STATUS_SUCCESS &&
4693 		    hapd->conf->mesh & MESH_ENABLED) {
4694 			if (sta->wpa_sm == NULL)
4695 				sta->wpa_sm =
4696 					wpa_auth_sta_init(hapd->wpa_auth,
4697 							  sta->addr, NULL);
4698 			if (sta->wpa_sm == NULL) {
4699 				wpa_printf(MSG_DEBUG,
4700 					   "SAE: Failed to initialize WPA state machine");
4701 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4702 				goto fail;
4703 			}
4704 		}
4705 #endif /* CONFIG_MESH */
4706 		handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
4707 				status_code);
4708 		return;
4709 #endif /* CONFIG_SAE */
4710 #ifdef CONFIG_FILS
4711 	case WLAN_AUTH_FILS_SK:
4712 	case WLAN_AUTH_FILS_SK_PFS:
4713 		handle_auth_fils(hapd, sta, mgmt->u.auth.variable,
4714 				 len - IEEE80211_HDRLEN - sizeof(mgmt->u.auth),
4715 				 auth_alg, auth_transaction, status_code,
4716 				 handle_auth_fils_finish);
4717 		return;
4718 #endif /* CONFIG_FILS */
4719 #ifdef CONFIG_IEEE8021X_AUTH
4720 	case WLAN_AUTH_802_1X:
4721 		handle_auth_802_1x(hapd, sta, mgmt->u.auth.variable,
4722 				   len - IEEE80211_HDRLEN -
4723 				   sizeof(mgmt->u.auth),
4724 				   auth_alg, auth_transaction);
4725 		return;
4726 #endif /* CONFIG_IEEE8021X_AUTH */
4727 #ifdef CONFIG_ENC_ASSOC
4728 	case WLAN_AUTH_EPPKE:
4729 #endif /* CONFIG_ENC_ASSOC */
4730 #ifdef CONFIG_PASN
4731 	case WLAN_AUTH_PASN:
4732 		handle_auth_pasn(hapd, sta, mgmt, len, auth_transaction,
4733 				 status_code);
4734 		return;
4735 #endif /* CONFIG_PASN */
4736 	}
4737 
4738  fail:
4739 	dst = mgmt->sa;
4740 
4741 #ifdef CONFIG_IEEE80211BE
4742 	if (ap_sta_is_mld(hapd, sta))
4743 		dst = sta->addr;
4744 #endif /* CONFIG_IEEE80211BE */
4745 
4746 	reply_res = send_auth_reply(hapd, sta, dst, auth_alg,
4747 				    auth_alg == WLAN_AUTH_SAE ?
4748 				    auth_transaction : auth_transaction + 1,
4749 				    resp, resp_ies, resp_ies_len,
4750 				    "handle-auth");
4751 
4752 	if (sta && sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
4753 					  reply_res != WLAN_STATUS_SUCCESS)) {
4754 		hostapd_drv_sta_remove(hapd, sta->addr);
4755 		sta->added_unassoc = 0;
4756 	}
4757 }
4758 
4759 
hostapd_max_bssid_indicator(struct hostapd_data * hapd)4760 static u8 hostapd_max_bssid_indicator(struct hostapd_data *hapd)
4761 {
4762 	size_t num_bss_nontx;
4763 	u8 max_bssid_ind = 0;
4764 
4765 	if (!hapd->iconf->mbssid || hapd->iface->num_bss <= 1)
4766 		return 0;
4767 
4768 	if (hapd->iface->conf->mbssid_max > 0)
4769 		num_bss_nontx = hapd->iface->conf->mbssid_max - 1;
4770 	else
4771 		num_bss_nontx = hapd->iface->conf->num_bss - 1;
4772 	while (num_bss_nontx > 0) {
4773 		max_bssid_ind++;
4774 		num_bss_nontx >>= 1;
4775 	}
4776 	return max_bssid_ind;
4777 }
4778 
4779 
hostapd_get_aid_word(struct hostapd_data * hapd,struct sta_info * sta,int i)4780 static u32 hostapd_get_aid_word(struct hostapd_data *hapd,
4781 				struct sta_info *sta, int i)
4782 {
4783 #ifdef CONFIG_IEEE80211BE
4784 	u32 aid_word = 0;
4785 
4786 	/* Do not assign an AID that is in use on any of the affiliated links
4787 	 * when finding an AID for a non-AP MLD. */
4788 	if (hapd->conf->mld_ap && sta->mld_info.mld_sta) {
4789 		int j;
4790 
4791 		for (j = 0; j < MAX_NUM_MLD_LINKS; j++) {
4792 			struct hostapd_data *link_bss;
4793 
4794 			if (!sta->mld_info.links[j].valid)
4795 				continue;
4796 
4797 			link_bss = hostapd_mld_get_link_bss(hapd, j);
4798 			if (!link_bss) {
4799 				/* This shouldn't happen, just skip */
4800 				wpa_printf(MSG_ERROR,
4801 					   "MLD: Failed to get link BSS for AID");
4802 				continue;
4803 			}
4804 
4805 			aid_word |= link_bss->sta_aid[i];
4806 		}
4807 
4808 		return aid_word;
4809 	}
4810 #endif /* CONFIG_IEEE80211BE */
4811 
4812 	return hapd->sta_aid[i];
4813 }
4814 
4815 
hostapd_get_aid(struct hostapd_data * hapd,struct sta_info * sta)4816 int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
4817 {
4818 	int i, j = 32, aid;
4819 
4820 	/* Transmitted and non-transmitted BSSIDs share the same AID pool, so
4821 	 * use the shared storage in the transmitted BSS to find the next
4822 	 * available value. */
4823 	hapd = hostapd_mbssid_get_tx_bss(hapd);
4824 
4825 	/* get a unique AID */
4826 	if (sta->aid > 0) {
4827 		wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
4828 		return 0;
4829 	}
4830 
4831 	if (TEST_FAIL())
4832 		return -1;
4833 
4834 	for (i = 0; i < AID_WORDS; i++) {
4835 		u32 aid_word = hostapd_get_aid_word(hapd, sta, i);
4836 
4837 		if (aid_word == (u32) -1)
4838 			continue;
4839 		for (j = 0; j < 32; j++) {
4840 			if (!(aid_word & BIT(j)))
4841 				break;
4842 		}
4843 		if (j < 32)
4844 			break;
4845 	}
4846 	if (j == 32)
4847 		return -1;
4848 	aid = i * 32 + j + (1 << hostapd_max_bssid_indicator(hapd));
4849 	if (aid > 2007)
4850 		return -1;
4851 
4852 	sta->aid = aid;
4853 	hapd->sta_aid[i] |= BIT(j);
4854 	wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
4855 	return 0;
4856 }
4857 
4858 
check_ssid(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ssid_ie,size_t ssid_ie_len)4859 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
4860 		      const u8 *ssid_ie, size_t ssid_ie_len)
4861 {
4862 	if (ssid_ie == NULL)
4863 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
4864 
4865 	if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
4866 	    os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
4867 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4868 			       HOSTAPD_LEVEL_INFO,
4869 			       "Station tried to associate with unknown SSID "
4870 			       "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
4871 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
4872 	}
4873 
4874 	return WLAN_STATUS_SUCCESS;
4875 }
4876 
4877 
check_wmm(struct hostapd_data * hapd,struct sta_info * sta,const u8 * wmm_ie,size_t wmm_ie_len)4878 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
4879 		     const u8 *wmm_ie, size_t wmm_ie_len)
4880 {
4881 	sta->flags &= ~WLAN_STA_WMM;
4882 	sta->qosinfo = 0;
4883 	if (wmm_ie && hapd->conf->wmm_enabled) {
4884 		struct wmm_information_element *wmm;
4885 
4886 		if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
4887 			hostapd_logger(hapd, sta->addr,
4888 				       HOSTAPD_MODULE_WPA,
4889 				       HOSTAPD_LEVEL_DEBUG,
4890 				       "invalid WMM element in association "
4891 				       "request");
4892 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
4893 		}
4894 
4895 		sta->flags |= WLAN_STA_WMM;
4896 		wmm = (struct wmm_information_element *) wmm_ie;
4897 		sta->qosinfo = wmm->qos_info;
4898 	}
4899 	return WLAN_STATUS_SUCCESS;
4900 }
4901 
check_multi_ap(struct hostapd_data * hapd,struct sta_info * sta,const u8 * multi_ap_ie,size_t multi_ap_len)4902 static u16 check_multi_ap(struct hostapd_data *hapd, struct sta_info *sta,
4903 			  const u8 *multi_ap_ie, size_t multi_ap_len)
4904 {
4905 	struct multi_ap_params multi_ap;
4906 	u16 status;
4907 
4908 	sta->flags &= ~WLAN_STA_MULTI_AP;
4909 
4910 	if (!hapd->conf->multi_ap)
4911 		return WLAN_STATUS_SUCCESS;
4912 
4913 	if (!multi_ap_ie) {
4914 		if (!(hapd->conf->multi_ap & FRONTHAUL_BSS)) {
4915 			hostapd_logger(hapd, sta->addr,
4916 				       HOSTAPD_MODULE_IEEE80211,
4917 				       HOSTAPD_LEVEL_INFO,
4918 				       "Non-Multi-AP STA tries to associate with backhaul-only BSS");
4919 			return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
4920 		}
4921 
4922 		return WLAN_STATUS_SUCCESS;
4923 	}
4924 
4925 	status = check_multi_ap_ie(multi_ap_ie + 4, multi_ap_len - 4,
4926 				   &multi_ap);
4927 	if (status != WLAN_STATUS_SUCCESS)
4928 		return status;
4929 
4930 	if (multi_ap.capability && multi_ap.capability != MULTI_AP_BACKHAUL_STA)
4931 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4932 			       HOSTAPD_LEVEL_INFO,
4933 			       "Multi-AP IE with unexpected value 0x%02x",
4934 			       multi_ap.capability);
4935 
4936 	if (multi_ap.profile == MULTI_AP_PROFILE_1 &&
4937 	    (hapd->conf->multi_ap_client_disallow &
4938 	     PROFILE1_CLIENT_ASSOC_DISALLOW)) {
4939 		hostapd_logger(hapd, sta->addr,
4940 			       HOSTAPD_MODULE_IEEE80211,
4941 			       HOSTAPD_LEVEL_INFO,
4942 			       "Multi-AP Profile-1 clients not allowed");
4943 		return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
4944 	}
4945 
4946 	if (multi_ap.profile >= MULTI_AP_PROFILE_2 &&
4947 	    (hapd->conf->multi_ap_client_disallow &
4948 	     PROFILE2_CLIENT_ASSOC_DISALLOW)) {
4949 		hostapd_logger(hapd, sta->addr,
4950 			       HOSTAPD_MODULE_IEEE80211,
4951 			       HOSTAPD_LEVEL_INFO,
4952 			       "Multi-AP Profile-2 clients not allowed");
4953 		return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
4954 	}
4955 
4956 	if (!(multi_ap.capability & MULTI_AP_BACKHAUL_STA)) {
4957 		if (hapd->conf->multi_ap & FRONTHAUL_BSS)
4958 			return WLAN_STATUS_SUCCESS;
4959 
4960 		hostapd_logger(hapd, sta->addr,
4961 			       HOSTAPD_MODULE_IEEE80211,
4962 			       HOSTAPD_LEVEL_INFO,
4963 			       "Non-Multi-AP STA tries to associate with backhaul-only BSS");
4964 		return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
4965 	}
4966 
4967 	if (!(hapd->conf->multi_ap & BACKHAUL_BSS))
4968 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4969 			       HOSTAPD_LEVEL_DEBUG,
4970 			       "Backhaul STA tries to associate with fronthaul-only BSS");
4971 
4972 	sta->flags |= WLAN_STA_MULTI_AP;
4973 	return WLAN_STATUS_SUCCESS;
4974 }
4975 
4976 
copy_supp_rates(struct hostapd_data * hapd,struct sta_info * sta,struct ieee802_11_elems * elems)4977 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
4978 			   struct ieee802_11_elems *elems)
4979 {
4980 	/* Supported rates not used in IEEE 802.11ad/DMG */
4981 	if (hapd->iface->current_mode &&
4982 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD)
4983 		return WLAN_STATUS_SUCCESS;
4984 
4985 	if (!elems->supp_rates) {
4986 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4987 			       HOSTAPD_LEVEL_DEBUG,
4988 			       "No supported rates element in AssocReq");
4989 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
4990 	}
4991 
4992 	if (elems->supp_rates_len + elems->ext_supp_rates_len >
4993 	    sizeof(sta->supported_rates)) {
4994 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4995 			       HOSTAPD_LEVEL_DEBUG,
4996 			       "Invalid supported rates element length %d+%d",
4997 			       elems->supp_rates_len,
4998 			       elems->ext_supp_rates_len);
4999 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
5000 	}
5001 
5002 	sta->supported_rates_len = merge_byte_arrays(
5003 		sta->supported_rates, sizeof(sta->supported_rates),
5004 		elems->supp_rates, elems->supp_rates_len,
5005 		elems->ext_supp_rates, elems->ext_supp_rates_len);
5006 
5007 	return WLAN_STATUS_SUCCESS;
5008 }
5009 
5010 
5011 #ifdef CONFIG_OWE
5012 
owe_group_supported(struct hostapd_data * hapd,u16 group)5013 static int owe_group_supported(struct hostapd_data *hapd, u16 group)
5014 {
5015 	int i;
5016 	int *groups = hapd->conf->owe_groups;
5017 
5018 	if (group != 19 && group != 20 && group != 21)
5019 		return 0;
5020 
5021 	if (!groups)
5022 		return 1;
5023 
5024 	for (i = 0; groups[i] > 0; i++) {
5025 		if (groups[i] == group)
5026 			return 1;
5027 	}
5028 
5029 	return 0;
5030 }
5031 
5032 
owe_process_assoc_req(struct hostapd_data * hapd,struct sta_info * sta,const u8 * owe_dh,u8 owe_dh_len)5033 static u16 owe_process_assoc_req(struct hostapd_data *hapd,
5034 				 struct sta_info *sta, const u8 *owe_dh,
5035 				 u8 owe_dh_len)
5036 {
5037 	struct wpabuf *secret, *pub, *hkey;
5038 	int res;
5039 	u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
5040 	const char *info = "OWE Key Generation";
5041 	const u8 *addr[2];
5042 	size_t len[2];
5043 	u16 group;
5044 	size_t hash_len, prime_len;
5045 
5046 	if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
5047 		wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
5048 		return WLAN_STATUS_SUCCESS;
5049 	}
5050 
5051 	group = WPA_GET_LE16(owe_dh);
5052 	if (!owe_group_supported(hapd, group)) {
5053 		wpa_printf(MSG_DEBUG, "OWE: Unsupported DH group %u", group);
5054 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
5055 	}
5056 	if (group == 19)
5057 		prime_len = 32;
5058 	else if (group == 20)
5059 		prime_len = 48;
5060 	else if (group == 21)
5061 		prime_len = 66;
5062 	else
5063 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
5064 
5065 	if (sta->owe_group == group && sta->owe_ecdh) {
5066 		/* This is a workaround for mac80211 behavior of retransmitting
5067 		 * the Association Request frames multiple times if the link
5068 		 * layer retries (i.e., seq# remains same) fail. The mac80211
5069 		 * initiated retransmission will use a different seq# and as
5070 		 * such, will go through duplicate detection. If we were to
5071 		 * change our DH key for that attempt, there would be two
5072 		 * different DH shared secrets and the STA would likely select
5073 		 * the wrong one. */
5074 		wpa_printf(MSG_DEBUG,
5075 			   "OWE: Try to reuse own previous DH key since the STA tried to go through OWE association again");
5076 	} else {
5077 		crypto_ecdh_deinit(sta->owe_ecdh);
5078 		sta->owe_ecdh = crypto_ecdh_init(group);
5079 	}
5080 	if (!sta->owe_ecdh)
5081 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
5082 	sta->owe_group = group;
5083 
5084 	secret = crypto_ecdh_set_peerkey(sta->owe_ecdh, 0, owe_dh + 2,
5085 					 owe_dh_len - 2);
5086 	secret = wpabuf_zeropad(secret, prime_len);
5087 	if (!secret) {
5088 		wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
5089 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
5090 	}
5091 	wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
5092 
5093 	/* prk = HKDF-extract(C | A | group, z) */
5094 
5095 	pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
5096 	if (!pub) {
5097 		wpabuf_clear_free(secret);
5098 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
5099 	}
5100 
5101 	/* PMKID = Truncate-128(Hash(C | A)) */
5102 	addr[0] = owe_dh + 2;
5103 	len[0] = owe_dh_len - 2;
5104 	addr[1] = wpabuf_head(pub);
5105 	len[1] = wpabuf_len(pub);
5106 	if (group == 19) {
5107 		res = sha256_vector(2, addr, len, pmkid);
5108 		hash_len = SHA256_MAC_LEN;
5109 	} else if (group == 20) {
5110 		res = sha384_vector(2, addr, len, pmkid);
5111 		hash_len = SHA384_MAC_LEN;
5112 	} else if (group == 21) {
5113 		res = sha512_vector(2, addr, len, pmkid);
5114 		hash_len = SHA512_MAC_LEN;
5115 	} else {
5116 		wpabuf_free(pub);
5117 		wpabuf_clear_free(secret);
5118 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
5119 	}
5120 	pub = wpabuf_zeropad(pub, prime_len);
5121 	if (res < 0 || !pub) {
5122 		wpabuf_free(pub);
5123 		wpabuf_clear_free(secret);
5124 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
5125 	}
5126 
5127 	hkey = wpabuf_alloc(owe_dh_len - 2 + wpabuf_len(pub) + 2);
5128 	if (!hkey) {
5129 		wpabuf_free(pub);
5130 		wpabuf_clear_free(secret);
5131 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
5132 	}
5133 
5134 	wpabuf_put_data(hkey, owe_dh + 2, owe_dh_len - 2); /* C */
5135 	wpabuf_put_buf(hkey, pub); /* A */
5136 	wpabuf_free(pub);
5137 	wpabuf_put_le16(hkey, group); /* group */
5138 	if (group == 19)
5139 		res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
5140 				  wpabuf_head(secret), wpabuf_len(secret), prk);
5141 	else if (group == 20)
5142 		res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
5143 				  wpabuf_head(secret), wpabuf_len(secret), prk);
5144 	else if (group == 21)
5145 		res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
5146 				  wpabuf_head(secret), wpabuf_len(secret), prk);
5147 	wpabuf_clear_free(hkey);
5148 	wpabuf_clear_free(secret);
5149 	if (res < 0)
5150 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
5151 
5152 	wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
5153 
5154 	/* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
5155 
5156 	os_free(sta->owe_pmk);
5157 	sta->owe_pmk = os_malloc(hash_len);
5158 	if (!sta->owe_pmk) {
5159 		os_memset(prk, 0, SHA512_MAC_LEN);
5160 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
5161 	}
5162 
5163 	if (group == 19)
5164 		res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
5165 				      os_strlen(info), sta->owe_pmk, hash_len);
5166 	else if (group == 20)
5167 		res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
5168 				      os_strlen(info), sta->owe_pmk, hash_len);
5169 	else if (group == 21)
5170 		res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
5171 				      os_strlen(info), sta->owe_pmk, hash_len);
5172 	os_memset(prk, 0, SHA512_MAC_LEN);
5173 	if (res < 0) {
5174 		os_free(sta->owe_pmk);
5175 		sta->owe_pmk = NULL;
5176 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
5177 	}
5178 	sta->owe_pmk_len = hash_len;
5179 
5180 	wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sta->owe_pmk, sta->owe_pmk_len);
5181 	wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
5182 	wpa_auth_pmksa_add2(hapd->wpa_auth, sta->addr, sta->owe_pmk,
5183 			    sta->owe_pmk_len, pmkid, 0, WPA_KEY_MGMT_OWE,
5184 			    NULL, ap_sta_is_mld(hapd, sta));
5185 
5186 	return WLAN_STATUS_SUCCESS;
5187 }
5188 
5189 
owe_validate_request(struct hostapd_data * hapd,const u8 * peer,const u8 * rsn_ie,size_t rsn_ie_len,const u8 * owe_dh,size_t owe_dh_len)5190 u16 owe_validate_request(struct hostapd_data *hapd, const u8 *peer,
5191 			 const u8 *rsn_ie, size_t rsn_ie_len,
5192 			 const u8 *owe_dh, size_t owe_dh_len)
5193 {
5194 	struct wpa_ie_data data;
5195 	int res;
5196 
5197 	if (!rsn_ie || rsn_ie_len < 2) {
5198 		wpa_printf(MSG_DEBUG, "OWE: Invalid RSNE from " MACSTR,
5199 			   MAC2STR(peer));
5200 		return WLAN_STATUS_INVALID_ELEMENT;
5201 	}
5202 	rsn_ie -= 2;
5203 	rsn_ie_len += 2;
5204 
5205 	res = wpa_parse_wpa_ie_rsn(rsn_ie, rsn_ie_len, &data);
5206 	if (res) {
5207 		wpa_printf(MSG_DEBUG, "Failed to parse RSNE from " MACSTR
5208 			   " (res=%d)", MAC2STR(peer), res);
5209 		wpa_hexdump(MSG_DEBUG, "RSNE", rsn_ie, rsn_ie_len);
5210 		return wpa_res_to_status_code(res);
5211 	}
5212 	if (!(data.key_mgmt & WPA_KEY_MGMT_OWE)) {
5213 		wpa_printf(MSG_DEBUG,
5214 			   "OWE: Unexpected key mgmt 0x%x from " MACSTR,
5215 			   (unsigned int) data.key_mgmt, MAC2STR(peer));
5216 		return WLAN_STATUS_INVALID_AKMP;
5217 	}
5218 	if (!owe_dh) {
5219 		wpa_printf(MSG_DEBUG,
5220 			   "OWE: No Diffie-Hellman Parameter element from "
5221 			   MACSTR, MAC2STR(peer));
5222 		return WLAN_STATUS_INVALID_AKMP;
5223 	}
5224 
5225 	return WLAN_STATUS_SUCCESS;
5226 }
5227 
5228 
owe_process_rsn_ie(struct hostapd_data * hapd,struct sta_info * sta,const u8 * rsn_ie,size_t rsn_ie_len,const u8 * owe_dh,size_t owe_dh_len,const u8 * link_addr)5229 u16 owe_process_rsn_ie(struct hostapd_data *hapd,
5230 		       struct sta_info *sta,
5231 		       const u8 *rsn_ie, size_t rsn_ie_len,
5232 		       const u8 *owe_dh, size_t owe_dh_len,
5233 		       const u8 *link_addr)
5234 {
5235 	u16 status;
5236 	u8 *owe_buf, ie[256 * 2];
5237 	size_t ie_len = 0;
5238 	enum wpa_validate_result res;
5239 
5240 	if (!rsn_ie || rsn_ie_len < 2) {
5241 		wpa_printf(MSG_DEBUG, "OWE: No RSNE in (Re)AssocReq");
5242 		status = WLAN_STATUS_INVALID_ELEMENT;
5243 		goto end;
5244 	}
5245 
5246 	if (!sta->wpa_sm)
5247 		sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,	sta->addr,
5248 						NULL);
5249 	if (!sta->wpa_sm) {
5250 		wpa_printf(MSG_WARNING,
5251 			   "OWE: Failed to initialize WPA state machine");
5252 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
5253 		goto end;
5254 	}
5255 #ifdef CONFIG_IEEE80211BE
5256 	if (ap_sta_is_mld(hapd, sta))
5257 		wpa_auth_set_ml_info(sta->wpa_sm,
5258 				     sta->mld_assoc_link_id, &sta->mld_info);
5259 #endif /* CONFIG_IEEE80211BE */
5260 	rsn_ie -= 2;
5261 	rsn_ie_len += 2;
5262 	res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
5263 				  hapd->iface->freq, rsn_ie, rsn_ie_len,
5264 				  NULL, 0, NULL, 0, owe_dh, owe_dh_len, NULL,
5265 				  ap_sta_is_mld(hapd, sta));
5266 	status = wpa_res_to_status_code(res);
5267 	if (status != WLAN_STATUS_SUCCESS)
5268 		goto end;
5269 	status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
5270 	if (status != WLAN_STATUS_SUCCESS)
5271 		goto end;
5272 	owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, ie, sizeof(ie));
5273 	if (!owe_buf) {
5274 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
5275 		goto end;
5276 	}
5277 
5278 	if (sta->owe_ecdh) {
5279 		struct wpabuf *pub;
5280 
5281 		pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
5282 		if (!pub) {
5283 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
5284 			goto end;
5285 		}
5286 
5287 		/* OWE Diffie-Hellman Parameter element */
5288 		*owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
5289 		*owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
5290 		*owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
5291 							 */
5292 		WPA_PUT_LE16(owe_buf, sta->owe_group);
5293 		owe_buf += 2;
5294 		os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
5295 		owe_buf += wpabuf_len(pub);
5296 		wpabuf_free(pub);
5297 		sta->external_dh_updated = 1;
5298 	}
5299 	ie_len = owe_buf - ie;
5300 
5301 end:
5302 	wpa_printf(MSG_DEBUG, "OWE: Update status %d, ie len %d for peer "
5303 			      MACSTR, status, (unsigned int) ie_len,
5304 			      MAC2STR(link_addr ? link_addr : sta->addr));
5305 	hostapd_drv_update_dh_ie(hapd, link_addr ? link_addr : sta->addr,
5306 				 status,
5307 				 status == WLAN_STATUS_SUCCESS ? ie : NULL,
5308 				 ie_len);
5309 
5310 	return status;
5311 }
5312 
5313 #endif /* CONFIG_OWE */
5314 
5315 
hapd_is_known_sta(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ies,size_t ies_len)5316 static bool hapd_is_known_sta(struct hostapd_data *hapd, struct sta_info *sta,
5317 			      const u8 *ies, size_t ies_len)
5318 {
5319 	const u8 *ie, *pos, *end, *timestamp_pos, *mic;
5320 	u64 timestamp;
5321 	u8 mic_len;
5322 
5323 	if (!hapd->conf->known_sta_identification)
5324 		return false;
5325 
5326 	ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_KNOWN_STA_IDENTIFICATION);
5327 	if (!ie)
5328 		return false;
5329 
5330 	pos = ie + 3;
5331 	end = &ie[2 + ie[1]];
5332 	if (end - pos < 8 + 1)
5333 		return false; /* truncated element */
5334 	timestamp_pos = pos;
5335 	timestamp = WPA_GET_LE64(pos);
5336 	pos += 8;
5337 	mic_len = *pos++;
5338 	if (mic_len > end - pos)
5339 		return false; /* truncated element */
5340 	mic = pos;
5341 
5342 	wpa_printf(MSG_DEBUG, "RSN: STA " MACSTR
5343 		   " included Known STA Identification element: Timestamp=0x%llx mic_len=%u",
5344 		   MAC2STR(sta->addr), (unsigned long long) timestamp, mic_len);
5345 
5346 	if (timestamp <= sta->last_known_sta_id_timestamp) {
5347 		wpa_printf(MSG_DEBUG,
5348 			   "RSN: Ignore reused or old Known STA Identification");
5349 		return false;
5350 	}
5351 
5352 	if (!wpa_auth_sm_known_sta_identification(sta->wpa_sm, timestamp_pos,
5353 						  mic, mic_len)) {
5354 		wpa_printf(MSG_DEBUG,
5355 			   "RSN: Ignore Known STA Identification with invalid MIC or due to KCK not available");
5356 		return false;
5357 	}
5358 
5359 	wpa_printf(MSG_DEBUG, "RSN: Valid Known STA Identification");
5360 	sta->last_known_sta_id_timestamp = timestamp;
5361 
5362 	return true;
5363 }
5364 
5365 
check_sa_query(struct hostapd_data * hapd,struct sta_info * sta,int reassoc,const u8 * ies,size_t ies_len,bool enc_assoc)5366 static bool check_sa_query(struct hostapd_data *hapd, struct sta_info *sta,
5367 			   int reassoc, const u8 *ies, size_t ies_len,
5368 			   bool enc_assoc)
5369 {
5370 	if ((sta->flags &
5371 	     (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED)) !=
5372 	    (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED))
5373 		return false;
5374 
5375 #ifdef CONFIG_ENC_ASSOC
5376 	if (enc_assoc && sta->epp_sta) {
5377 		/* Skip SA Query since either the STA knows the PTK that is in
5378 		 * use in the existing association or a new EPPKE authentication
5379 		 * has already authenticated the STA and has replaced the TK and
5380 		 * there is not really any point in starting SA Query procedure.
5381 		 */
5382 		return false;
5383 	}
5384 #endif /* CONFIG_ENC_ASSOC */
5385 
5386 	if (!sta->sa_query_timed_out && sta->sa_query_count > 0)
5387 		ap_check_sa_query_timeout(hapd, sta);
5388 
5389 	if (!sta->sa_query_timed_out &&
5390 	    (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
5391 		if (hapd_is_known_sta(hapd, sta, ies, ies_len))
5392 			return false;
5393 
5394 		/*
5395 		 * STA has already been associated with MFP and SA Query timeout
5396 		 * has not been reached. Reject the association attempt
5397 		 * temporarily and start SA Query, if one is not pending.
5398 		 */
5399 		if (sta->sa_query_count == 0)
5400 			ap_sta_start_sa_query(hapd, sta);
5401 
5402 		return true;
5403 	}
5404 
5405 	return false;
5406 }
5407 
5408 
__check_assoc_ies(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ies,size_t ies_len,struct ieee802_11_elems * elems,enum link_parse_type type,struct wpa_state_machine * assoc_wpa_sm)5409 static int __check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
5410 			     const u8 *ies, size_t ies_len,
5411 			     struct ieee802_11_elems *elems,
5412 			     enum link_parse_type type,
5413 			     struct wpa_state_machine *assoc_wpa_sm)
5414 {
5415 	int resp;
5416 	const u8 *wpa_ie;
5417 	size_t wpa_ie_len;
5418 	const u8 *p2p_dev_addr = NULL;
5419 #ifdef CONFIG_PMKSA_PRIVACY
5420 	bool derive_next_pmkid = true;
5421 #endif /* CONFIG_PMKSA_PRIVACY */
5422 #ifdef CONFIG_IEEE8021X_AUTH
5423 	bool mic_check = true;
5424 #endif /* CONFIG_IEEE8021X_AUTH */
5425 #ifdef CONFIG_SAE
5426 	bool epp_sta = false;
5427 
5428 #ifdef CONFIG_ENC_ASSOC
5429 	epp_sta = sta->epp_sta;
5430 #endif /* CONFIG_ENC_ASSOC */
5431 #endif /* CONFIG_SAE */
5432 
5433 	if (type != LINK_PARSE_RECONF) {
5434 		resp = check_ssid(hapd, sta, elems->ssid, elems->ssid_len);
5435 		if (resp != WLAN_STATUS_SUCCESS)
5436 			goto out;
5437 	}
5438 
5439 	resp = check_wmm(hapd, sta, elems->wmm, elems->wmm_len);
5440 	if (resp != WLAN_STATUS_SUCCESS)
5441 		goto out;
5442 	resp = check_ext_capab(hapd, sta, elems->ext_capab,
5443 			       elems->ext_capab_len);
5444 	if (resp != WLAN_STATUS_SUCCESS)
5445 		goto out;
5446 	resp = copy_supp_rates(hapd, sta, elems);
5447 	if (resp != WLAN_STATUS_SUCCESS)
5448 		goto out;
5449 
5450 	resp = check_multi_ap(hapd, sta, elems->multi_ap, elems->multi_ap_len);
5451 	if (resp != WLAN_STATUS_SUCCESS)
5452 		goto out;
5453 
5454 	resp = copy_sta_ht_capab(hapd, sta, elems->ht_capabilities);
5455 	if (resp != WLAN_STATUS_SUCCESS)
5456 		goto out;
5457 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
5458 	    !(sta->flags & WLAN_STA_HT)) {
5459 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5460 			       HOSTAPD_LEVEL_INFO, "Station does not support "
5461 			       "mandatory HT PHY - reject association");
5462 		resp = WLAN_STATUS_ASSOC_DENIED_NO_HT;
5463 		goto out;
5464 	}
5465 
5466 #ifdef CONFIG_IEEE80211AC
5467 	if (hapd->iconf->ieee80211ac) {
5468 		resp = copy_sta_vht_capab(hapd, sta, elems->vht_capabilities);
5469 		if (resp != WLAN_STATUS_SUCCESS)
5470 			goto out;
5471 
5472 		resp = set_sta_vht_opmode(hapd, sta, elems->opmode_notif);
5473 		if (resp != WLAN_STATUS_SUCCESS)
5474 			goto out;
5475 	}
5476 
5477 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
5478 	    !(sta->flags & WLAN_STA_VHT)) {
5479 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5480 			       HOSTAPD_LEVEL_INFO, "Station does not support "
5481 			       "mandatory VHT PHY - reject association");
5482 		resp = WLAN_STATUS_ASSOC_DENIED_NO_VHT;
5483 		goto out;
5484 	}
5485 
5486 	if (hapd->conf->vendor_vht && !elems->vht_capabilities) {
5487 		resp = copy_sta_vendor_vht(hapd, sta, elems->vendor_vht,
5488 					   elems->vendor_vht_len);
5489 		if (resp != WLAN_STATUS_SUCCESS)
5490 			goto out;
5491 	}
5492 #endif /* CONFIG_IEEE80211AC */
5493 #ifdef CONFIG_IEEE80211AX
5494 	if (hostapd_is_he_enabled(hapd)) {
5495 		resp = copy_sta_he_capab(hapd, sta, IEEE80211_MODE_AP,
5496 					 elems->he_capabilities,
5497 					 elems->he_capabilities_len);
5498 		if (resp != WLAN_STATUS_SUCCESS)
5499 			goto out;
5500 
5501 		if (hapd->iconf->require_he && !(sta->flags & WLAN_STA_HE)) {
5502 			hostapd_logger(hapd, sta->addr,
5503 				       HOSTAPD_MODULE_IEEE80211,
5504 				       HOSTAPD_LEVEL_INFO,
5505 				       "Station does not support mandatory HE PHY - reject association");
5506 			resp = WLAN_STATUS_DENIED_HE_NOT_SUPPORTED;
5507 			goto out;
5508 		}
5509 
5510 		if (is_6ghz_op_class(hapd->iconf->op_class)) {
5511 			if (!(sta->flags & WLAN_STA_HE)) {
5512 				hostapd_logger(hapd, sta->addr,
5513 					       HOSTAPD_MODULE_IEEE80211,
5514 					       HOSTAPD_LEVEL_INFO,
5515 					       "Station does not support mandatory HE PHY - reject association");
5516 				resp = WLAN_STATUS_DENIED_HE_NOT_SUPPORTED;
5517 				goto out;
5518 			}
5519 			resp = copy_sta_he_6ghz_capab(hapd, sta,
5520 						      elems->he_6ghz_band_cap);
5521 			if (resp != WLAN_STATUS_SUCCESS)
5522 				goto out;
5523 		}
5524 	}
5525 #endif /* CONFIG_IEEE80211AX */
5526 #ifdef CONFIG_IEEE80211BE
5527 	if (hostapd_is_eht_enabled(hapd)) {
5528 		resp = copy_sta_eht_capab(hapd, sta, IEEE80211_MODE_AP,
5529 					  elems->he_capabilities,
5530 					  elems->he_capabilities_len,
5531 					  elems->eht_capabilities,
5532 					  elems->eht_capabilities_len);
5533 		if (resp != WLAN_STATUS_SUCCESS)
5534 			goto out;
5535 
5536 		if ((hapd->iconf->require_eht || hapd->conf->bss_require_eht) &&
5537 		    !(sta->flags & WLAN_STA_EHT)) {
5538 			hostapd_logger(hapd, sta->addr,
5539 				       HOSTAPD_MODULE_IEEE80211,
5540 				       HOSTAPD_LEVEL_INFO,
5541 				       "Station does not support mandatory EHT PHY - reject association");
5542 			return WLAN_STATUS_DENIED_EHT_NOT_SUPPORTED;
5543 		}
5544 
5545 		if (!assoc_wpa_sm) {
5546 			resp = hostapd_process_ml_assoc_req(hapd, elems, sta);
5547 			if (resp != WLAN_STATUS_SUCCESS)
5548 				goto out;
5549 		}
5550 	}
5551 #endif /* CONFIG_IEEE80211BE */
5552 
5553 #ifdef CONFIG_P2P
5554 	if (elems->p2p && ies && ies_len) {
5555 		wpabuf_free(sta->p2p_ie);
5556 		sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
5557 							  P2P_IE_VENDOR_TYPE);
5558 		if (sta->p2p_ie)
5559 			p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
5560 	} else {
5561 		wpabuf_free(sta->p2p_ie);
5562 		sta->p2p_ie = NULL;
5563 	}
5564 #endif /* CONFIG_P2P */
5565 
5566 #ifdef CONFIG_IEEE8021X_AUTH
5567 	/* Per IEEE 802.11bi/D4.0, 12.16.6 ((Re)Association Request/Response
5568 	 * frame encryption), if IEEE 802.1X is used and FT protocol is not
5569 	 * used, the EPP non-AP STA shall include a MIC element in the
5570 	 * (Re)Association Request frame.
5571 	 * Skip MIC validation on partner AP MLD links.
5572 	 */
5573 #ifdef CONFIG_IEEE80211BE
5574 	if (ap_sta_is_mld(hapd, sta) &&
5575 	    hapd->mld_link_id != sta->mld_assoc_link_id)
5576 		mic_check = false;
5577 #endif /* CONFIG_IEEE80211BE */
5578 	if (ap_sta_is_epp(sta) && sta->auth_alg == WLAN_AUTH_802_1X &&
5579 	    mic_check) {
5580 		const u8 *data;
5581 		u8 mic_len, data_buf[(255 + 2) * 2], mic[WPA_1X_MAX_MIC_LEN];
5582 		const u8 *aa = hapd->own_addr;
5583 		size_t data_len = 0;
5584 		int ret;
5585 
5586 		if (!elems->mic) {
5587 			wpa_printf(MSG_DEBUG, "802.1X: Missing MIC element");
5588 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5589 			goto out;
5590 		}
5591 
5592 		if (wpa_key_mgmt_sha384(sta->eap_auth_data.akm))
5593 			mic_len = SHA384_MAC_LEN / 2;
5594 		else
5595 			mic_len = SHA256_MAC_LEN / 2;
5596 
5597 		if (mic_len != elems->mic_len) {
5598 			wpa_printf(MSG_DEBUG, "802.1X: Invalid MIC len: %u",
5599 				   elems->mic_len);
5600 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5601 			goto out;
5602 		}
5603 
5604 #ifdef CONFIG_IEEE80211BE
5605 		if (ap_sta_is_mld(hapd, sta))
5606 			aa = hapd->mld->mld_addr;
5607 #endif /* CONFIG_IEEE80211BE */
5608 
5609 		os_memcpy(data_buf, elems->rsn_ie - 2, elems->rsn_ie_len + 2);
5610 		data_len += 2 + elems->rsn_ie_len;
5611 		os_memcpy(data_buf + data_len, elems->rsnxe - 2,
5612 			  elems->rsnxe_len + 2);
5613 		data_len += 2 + elems->rsnxe_len;
5614 
5615 		data = data_buf;
5616 		ret = wpa_auth_8021x_mic(sta->eap_auth_data.akm,
5617 					 sta->eap_auth_data.ptk.kck,
5618 					 sta->eap_auth_data.ptk.kck_len, aa,
5619 					 sta->addr, data, data_len,
5620 					 NULL, 0, mic);
5621 		wpa_hexdump_key(MSG_DEBUG, "802.1X: Frame MIC",
5622 				elems->mic, elems->mic_len);
5623 		if (ret || os_memcmp(mic, elems->mic, mic_len) != 0) {
5624 			wpa_printf(MSG_INFO, "802.1X: Failed MIC verification");
5625 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5626 			goto out;
5627 		}
5628 	}
5629 #endif /* CONFIG_IEEE8021X_AUTH */
5630 
5631 	/* Link Reconfiguration Request frame for add link operation will not
5632 	 * have RSN and other security IEs. So, skip the checks.
5633 	 */
5634 	if (type == LINK_PARSE_RECONF) {
5635 		wpa_printf(MSG_DEBUG,
5636 			   "MLD: Skip security IE checks for Link Reconfiguration request");
5637 		goto skip_wpa_ies;
5638 	}
5639 
5640 	if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems->rsn_ie) {
5641 		wpa_ie = elems->rsn_ie;
5642 		wpa_ie_len = elems->rsn_ie_len;
5643 	} else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
5644 		   elems->wpa_ie) {
5645 		wpa_ie = elems->wpa_ie;
5646 		wpa_ie_len = elems->wpa_ie_len;
5647 	} else {
5648 		wpa_ie = NULL;
5649 		wpa_ie_len = 0;
5650 	}
5651 
5652 #ifdef CONFIG_WPS
5653 	sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
5654 	if (hapd->conf->wps_state && elems->wps_ie && ies && ies_len) {
5655 		wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
5656 			   "Request - assume WPS is used");
5657 		sta->flags |= WLAN_STA_WPS;
5658 		wpabuf_free(sta->wps_ie);
5659 		sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
5660 							  WPS_IE_VENDOR_TYPE);
5661 		if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
5662 			wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
5663 			sta->flags |= WLAN_STA_WPS2;
5664 		}
5665 		wpa_ie = NULL;
5666 		wpa_ie_len = 0;
5667 		if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
5668 			wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
5669 				   "(Re)Association Request - reject");
5670 			resp = WLAN_STATUS_INVALID_ELEMENT;
5671 			goto out;
5672 		}
5673 	} else if (hapd->conf->wps_state && wpa_ie == NULL) {
5674 		wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
5675 			   "(Re)Association Request - possible WPS use");
5676 		sta->flags |= WLAN_STA_MAYBE_WPS;
5677 	} else
5678 #endif /* CONFIG_WPS */
5679 	if (hapd->conf->wpa && wpa_ie == NULL) {
5680 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5681 			       HOSTAPD_LEVEL_INFO,
5682 			       "No WPA/RSN IE in association request");
5683 		resp = WLAN_STATUS_INVALID_ELEMENT;
5684 		goto out;
5685 	}
5686 
5687 	if (hapd->conf->wpa && wpa_ie) {
5688 		enum wpa_validate_result res;
5689 #ifdef CONFIG_IEEE80211BE
5690 		struct mld_info *info = &sta->mld_info;
5691 		bool init = !sta->wpa_sm;
5692 #endif /* CONFIG_IEEE80211BE */
5693 
5694 		wpa_ie -= 2;
5695 		wpa_ie_len += 2;
5696 
5697 		if (!sta->wpa_sm) {
5698 			/* NOTE: For links other than the assoc-link the
5699 			 * separate wpa_sm is only allocated internally to this
5700 			 * function.
5701 			 */
5702 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
5703 							sta->addr,
5704 							p2p_dev_addr);
5705 
5706 			if (!sta->wpa_sm) {
5707 				wpa_printf(MSG_WARNING,
5708 					   "Failed to initialize RSN state machine");
5709 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5710 				goto out;
5711 			}
5712 #ifdef CONFIG_SAE
5713 			if (sta->sae && sta->sae->state == SAE_ACCEPTED &&
5714 			    wpa_key_mgmt_sae_ext_key(sta->sae->akmp))
5715 				wpa_auth_set_hash_alg_sae_ext_key(
5716 					sta->wpa_sm, sta->sae->pmk_len);
5717 #endif /* CONFIG_SAE */
5718 		}
5719 
5720 #ifdef CONFIG_IEEE80211BE
5721 		if (ap_sta_is_mld(hapd, sta)) {
5722 			wpa_printf(MSG_DEBUG,
5723 				   "MLD: %s ML info in RSN Authenticator",
5724 				   init ? "Set" : "Reset");
5725 			wpa_auth_set_ml_info(sta->wpa_sm,
5726 					     sta->mld_assoc_link_id,
5727 					     info);
5728 		}
5729 #endif /* CONFIG_IEEE80211BE */
5730 
5731 		wpa_auth_set_auth_alg(sta->wpa_sm, sta->auth_alg);
5732 		if (sta->auth_alg == WLAN_AUTH_SAE)
5733 			wpa_auth_set_sae_pw_id(sta->wpa_sm, sta->sae_pw_id,
5734 					       sta->sae_pw_id_counter);
5735 		wpa_auth_set_rsn_selection(sta->wpa_sm, elems->rsn_selection,
5736 					   elems->rsn_selection_len);
5737 		res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
5738 					  hapd->iface->freq,
5739 					  wpa_ie, wpa_ie_len,
5740 					  elems->rsnxe ? elems->rsnxe - 2 :
5741 					  NULL,
5742 					  elems->rsnxe ? elems->rsnxe_len + 2 :
5743 					  0,
5744 					  elems->mdie, elems->mdie_len,
5745 					  elems->owe_dh, elems->owe_dh_len,
5746 					  assoc_wpa_sm,
5747 					  ap_sta_is_mld(hapd, sta));
5748 		resp = wpa_res_to_status_code(res);
5749 		if (resp != WLAN_STATUS_SUCCESS)
5750 			goto out;
5751 
5752 		if (wpa_auth_uses_mfp(sta->wpa_sm))
5753 			sta->flags |= WLAN_STA_MFP;
5754 		else
5755 			sta->flags &= ~WLAN_STA_MFP;
5756 
5757 		if (wpa_auth_uses_spp_amsdu(sta->wpa_sm))
5758 			sta->flags |= WLAN_STA_SPP_AMSDU;
5759 		else
5760 			sta->flags &= ~WLAN_STA_SPP_AMSDU;
5761 
5762 #ifdef CONFIG_PMKSA_PRIVACY
5763 	/* Per IEEE 802.11bi/D4.0, 12.16.7 (PMKSA caching privacy), when both
5764 	 * the AP and non-AP STA support PMKSA caching privacy, the non-AP STA
5765 	 * shall include a Nonce element in the (Re)Association Request frame.
5766 	 * Skip Nonce element processing for partner AP MLD links. */
5767 #ifdef CONFIG_IEEE80211BE
5768 	if (ap_sta_is_mld(hapd, sta) &&
5769 	    hapd->mld_link_id != sta->mld_assoc_link_id)
5770 		derive_next_pmkid = false;
5771 #endif /* CONFIG_IEEE80211BE */
5772 
5773 	if (derive_next_pmkid && ap_sta_is_epp(sta) &&
5774 	    hapd->conf->pmksa_caching_privacy &&
5775 	    ieee802_11_rsnx_capab_len(elems->rsnxe, elems->rsnxe_len,
5776 				      WLAN_RSNX_CAPAB_PMKSA_CACHING_PRIVACY)) {
5777 		int akmp;
5778 		size_t pmk_len;
5779 		u8 *pmkid_next;
5780 
5781 		if (!elems->nonce) {
5782 			wpa_printf(MSG_DEBUG, "STA " MACSTR
5783 				   " did not include Nonce element to compute next PMKID",
5784 				   MAC2STR(sta->addr));
5785 			goto skip_pmkid_update;
5786 		}
5787 		os_memcpy(sta->snonce, elems->nonce, NONCE_LEN);
5788 		wpa_hexdump(MSG_DEBUG,
5789 			    "RSN: Received SNonce to compute next PMKID",
5790 			    sta->snonce, NONCE_LEN);
5791 
5792 		switch (sta->auth_alg) {
5793 		case WLAN_AUTH_EPPKE:
5794 			if (!sta->pasn) {
5795 				wpa_printf(MSG_INFO,
5796 					   "EPPKE: Missing PASN data - cannot derive a new PMKID");
5797 				goto skip_pmkid_update;
5798 			}
5799 			pmk_len = sta->pasn->pmk_len;
5800 			pmkid_next = sta->epp_pmkid_next;
5801 			break;
5802 #ifdef CONFIG_IEEE8021X_AUTH
5803 		case WLAN_AUTH_802_1X:
5804 			pmk_len = sta->eap_auth_data.pmk_len;
5805 			pmkid_next = sta->eap_auth_data.epp_pmkid_next;
5806 			break;
5807 #endif /* CONFIG_IEEE8021X_AUTH */
5808 		default:
5809 			wpa_printf(MSG_INFO,
5810 				   "EPP: Unsupported auth alg %u for PMKID privacy",
5811 				   sta->auth_alg);
5812 			goto skip_pmkid_update;
5813 		}
5814 
5815 		if (random_get_bytes(sta->anonce, NONCE_LEN) < 0)
5816 			goto skip_pmkid_update;
5817 		wpa_hexdump_key(MSG_DEBUG,
5818 				"EPP: Generated ANonce to compute next PMKID",
5819 				sta->anonce, NONCE_LEN);
5820 
5821 		akmp = wpa_auth_sta_key_mgmt(sta->wpa_sm);
5822 		if (akmp < 0 ||
5823 		    wpa_auth_epp_derive_new_pmkid(sta->anonce, sta->snonce,
5824 						  akmp, pmk_len,
5825 						  pmkid_next) < 0) {
5826 			wpa_printf(MSG_INFO,
5827 				   "EPP: Failed to generate new PMKID");
5828 			goto skip_pmkid_update;
5829 		}
5830 		wpa_hexdump_key(MSG_DEBUG, "EPP: New PMKID",
5831 				pmkid_next, PMKID_LEN);
5832 	}
5833 skip_pmkid_update:
5834 #endif /* CONFIG_PMKSA_PRIVACY */
5835 
5836 #ifdef CONFIG_IEEE80211R_AP
5837 		if (sta->auth_alg == WLAN_AUTH_FT) {
5838 			if (type != LINK_PARSE_REASSOC) {
5839 				wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
5840 					   "to use association (not "
5841 					   "re-association) with FT auth_alg",
5842 					   MAC2STR(sta->addr));
5843 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5844 				goto out;
5845 			}
5846 
5847 			resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
5848 						       ies_len);
5849 			if (resp != WLAN_STATUS_SUCCESS)
5850 				goto out;
5851 		}
5852 #endif /* CONFIG_IEEE80211R_AP */
5853 
5854 		if (assoc_wpa_sm)
5855 			goto skip_sae_owe;
5856 #ifdef CONFIG_SAE
5857 		if (wpa_auth_uses_sae(sta->wpa_sm) && sta->sae &&
5858 		    sta->sae->state == SAE_ACCEPTED) {
5859 			if (!sta->sae->h2e &&
5860 			    wpa_key_mgmt_sae_ext_key(wpa_auth_sta_key_mgmt(
5861 							     sta->wpa_sm))) {
5862 				wpa_printf(MSG_DEBUG, "SAE: STA " MACSTR
5863 					   " tried to use EXT-KEY AKM without H2E",
5864 					   MAC2STR(sta->addr));
5865 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5866 				goto out;
5867 			}
5868 			wpa_auth_add_sae_pmkid(sta->wpa_sm, sta->sae->pmkid);
5869 		}
5870 
5871 		if (wpa_auth_uses_sae(sta->wpa_sm) &&
5872 		    sta->auth_alg == WLAN_AUTH_OPEN) {
5873 			struct rsn_pmksa_cache_entry *sa;
5874 			sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
5875 			if (!sa || !wpa_key_mgmt_sae(sa->akmp)) {
5876 				wpa_printf(MSG_DEBUG,
5877 					   "SAE: No PMKSA cache entry found for "
5878 					   MACSTR, MAC2STR(sta->addr));
5879 				resp = WLAN_STATUS_INVALID_PMKID;
5880 				goto out;
5881 			}
5882 			wpa_printf(MSG_DEBUG, "SAE: " MACSTR
5883 				   " using PMKSA caching", MAC2STR(sta->addr));
5884 			sae_assign_vlan(hapd, sta, sa->sae_vlan_id);
5885 			if (wpa_key_mgmt_sae_ext_key(sa->akmp))
5886 				wpa_auth_set_hash_alg_sae_ext_key(
5887 					sta->wpa_sm, sa->pmk_len);
5888 		} else if (!epp_sta && wpa_auth_uses_sae(sta->wpa_sm) &&
5889 			   sta->auth_alg != WLAN_AUTH_SAE &&
5890 			   !(sta->auth_alg == WLAN_AUTH_FT &&
5891 			     wpa_auth_uses_ft_sae(sta->wpa_sm))) {
5892 			wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
5893 				   "SAE AKM after non-SAE auth_alg %u",
5894 				   MAC2STR(sta->addr), sta->auth_alg);
5895 			resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
5896 			goto out;
5897 		}
5898 
5899 		if (hapd->conf->sae_pwe == SAE_PWE_BOTH &&
5900 		    sta->auth_alg == WLAN_AUTH_SAE &&
5901 		    sta->sae && !sta->sae->h2e &&
5902 		    ieee802_11_rsnx_capab_len(elems->rsnxe, elems->rsnxe_len,
5903 					      WLAN_RSNX_CAPAB_SAE_H2E)) {
5904 			if (hapd->conf->sae_accept_h2e_without_use) {
5905 				wpa_printf(MSG_INFO, "SAE: " MACSTR
5906 					   " indicates support for SAE H2E, but did not use it - accepting due to sae_accept_h2e_without_use",
5907 					   MAC2STR(sta->addr));
5908 			} else {
5909 				wpa_printf(MSG_INFO, "SAE: " MACSTR
5910 					   " indicates support for SAE H2E, but did not use it",
5911 					   MAC2STR(sta->addr));
5912 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5913 				goto out;
5914 			}
5915 		}
5916 #endif /* CONFIG_SAE */
5917 
5918 #ifdef CONFIG_OWE
5919 		if (((hapd->conf->wpa_key_mgmt |
5920 		      hapd->conf->rsn_override_key_mgmt |
5921 		      hapd->conf->rsn_override_key_mgmt_2) &
5922 		     WPA_KEY_MGMT_OWE) &&
5923 		    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
5924 		    elems->owe_dh) {
5925 			resp = owe_process_assoc_req(hapd, sta, elems->owe_dh,
5926 						     elems->owe_dh_len);
5927 			if (resp != WLAN_STATUS_SUCCESS)
5928 				goto out;
5929 		}
5930 #endif /* CONFIG_OWE */
5931 	skip_sae_owe:
5932 
5933 #ifdef CONFIG_DPP2
5934 		dpp_pfs_free(sta->dpp_pfs);
5935 		sta->dpp_pfs = NULL;
5936 
5937 		if (DPP_VERSION > 1 &&
5938 		    (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
5939 		    hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
5940 		    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
5941 		    elems->owe_dh && !assoc_wpa_sm) {
5942 			sta->dpp_pfs = dpp_pfs_init(
5943 				wpabuf_head(hapd->conf->dpp_netaccesskey),
5944 				wpabuf_len(hapd->conf->dpp_netaccesskey));
5945 			if (!sta->dpp_pfs) {
5946 				wpa_printf(MSG_DEBUG,
5947 					   "DPP: Could not initialize PFS");
5948 				/* Try to continue without PFS */
5949 				goto pfs_fail;
5950 			}
5951 
5952 			if (dpp_pfs_process(sta->dpp_pfs, elems->owe_dh,
5953 					    elems->owe_dh_len) < 0) {
5954 				dpp_pfs_free(sta->dpp_pfs);
5955 				sta->dpp_pfs = NULL;
5956 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5957 				goto out;
5958 			}
5959 		}
5960 		if (!assoc_wpa_sm)
5961 			wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
5962 					   sta->dpp_pfs->secret : NULL);
5963 	pfs_fail:
5964 #endif /* CONFIG_DPP2 */
5965 
5966 		if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
5967 		    wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
5968 			hostapd_logger(hapd, sta->addr,
5969 				       HOSTAPD_MODULE_IEEE80211,
5970 				       HOSTAPD_LEVEL_INFO,
5971 				       "Station tried to use TKIP with HT "
5972 				       "association");
5973 			resp = WLAN_STATUS_CIPHER_OUT_OF_POLICY;
5974 			goto out;
5975 		}
5976 
5977 		wpa_auth_set_ssid_protection(
5978 			sta->wpa_sm,
5979 			hapd->conf->ssid_protection &&
5980 			ieee802_11_rsnx_capab_len(
5981 				elems->rsnxe, elems->rsnxe_len,
5982 				WLAN_RSNX_CAPAB_SSID_PROTECTION));
5983 	} else
5984 		wpa_auth_sta_no_wpa(sta->wpa_sm);
5985 
5986 skip_wpa_ies:
5987 
5988 #ifdef CONFIG_P2P
5989 	if (ies && ies_len)
5990 		p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
5991 #endif /* CONFIG_P2P */
5992 
5993 #ifdef CONFIG_HS20
5994 	wpabuf_free(sta->hs20_ie);
5995 	if (elems->hs20 && elems->hs20_len > 4) {
5996 		int release;
5997 
5998 		sta->hs20_ie = wpabuf_alloc_copy(elems->hs20 + 4,
5999 						 elems->hs20_len - 4);
6000 		release = ((elems->hs20[4] >> 4) & 0x0f) + 1;
6001 		if (release >= 2 && !wpa_auth_uses_mfp(sta->wpa_sm) &&
6002 		    hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
6003 			wpa_printf(MSG_DEBUG,
6004 				   "HS 2.0: PMF not negotiated by release %d station "
6005 				   MACSTR, release, MAC2STR(sta->addr));
6006 			resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
6007 			goto out;
6008 		}
6009 	} else {
6010 		sta->hs20_ie = NULL;
6011 	}
6012 
6013 	wpabuf_free(sta->roaming_consortium);
6014 	if (elems->roaming_cons_sel)
6015 		sta->roaming_consortium = wpabuf_alloc_copy(
6016 			elems->roaming_cons_sel + 4,
6017 			elems->roaming_cons_sel_len - 4);
6018 	else
6019 		sta->roaming_consortium = NULL;
6020 #endif /* CONFIG_HS20 */
6021 
6022 #ifdef CONFIG_FST
6023 	wpabuf_free(sta->mb_ies);
6024 	if (hapd->iface->fst)
6025 		sta->mb_ies = mb_ies_by_info(&elems->mb_ies);
6026 	else
6027 		sta->mb_ies = NULL;
6028 #endif /* CONFIG_FST */
6029 
6030 #ifdef CONFIG_MBO
6031 	mbo_ap_check_sta_assoc(hapd, sta, elems);
6032 
6033 	if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
6034 	    elems->mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
6035 	    hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
6036 		wpa_printf(MSG_INFO,
6037 			   "MBO: Reject WPA2 association without PMF");
6038 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6039 		goto out;
6040 	}
6041 #endif /* CONFIG_MBO */
6042 
6043 #if defined(CONFIG_FILS) && defined(CONFIG_OCV)
6044 	if (type != LINK_PARSE_RECONF &&
6045 	    wpa_auth_uses_ocv(sta->wpa_sm) &&
6046 	    (sta->auth_alg == WLAN_AUTH_FILS_SK ||
6047 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
6048 	     sta->auth_alg == WLAN_AUTH_FILS_PK)) {
6049 		struct wpa_channel_info ci;
6050 		int tx_chanwidth;
6051 		int tx_seg1_idx;
6052 		enum oci_verify_result res;
6053 
6054 		if (hostapd_drv_channel_info(hapd, &ci) != 0) {
6055 			wpa_printf(MSG_WARNING,
6056 				   "Failed to get channel info to validate received OCI in FILS (Re)Association Request frame");
6057 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6058 			goto out;
6059 		}
6060 
6061 		if (get_sta_tx_parameters(sta->wpa_sm,
6062 					  channel_width_to_int(ci.chanwidth),
6063 					  ci.seg1_idx, &tx_chanwidth,
6064 					  &tx_seg1_idx) < 0) {
6065 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6066 			goto out;
6067 		}
6068 
6069 		res = ocv_verify_tx_params(elems->oci, elems->oci_len, &ci,
6070 					   tx_chanwidth, tx_seg1_idx);
6071 		if (wpa_auth_uses_ocv(sta->wpa_sm) == 2 &&
6072 		    res == OCI_NOT_FOUND) {
6073 			/* Work around misbehaving STAs */
6074 			wpa_printf(MSG_INFO,
6075 				   "FILS: Disable OCV with a STA that does not send OCI");
6076 			wpa_auth_set_ocv(sta->wpa_sm, 0);
6077 		} else if (res != OCI_SUCCESS) {
6078 			wpa_printf(MSG_WARNING, "FILS: OCV failed: %s",
6079 				   ocv_errorstr);
6080 			wpa_msg(hapd->msg_ctx, MSG_INFO, OCV_FAILURE "addr="
6081 				MACSTR " frame=fils-reassoc-req error=%s",
6082 				MAC2STR(sta->addr), ocv_errorstr);
6083 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6084 			goto out;
6085 		}
6086 	}
6087 #endif /* CONFIG_FILS && CONFIG_OCV */
6088 
6089 	ap_copy_sta_supp_op_classes(sta, elems->supp_op_classes,
6090 				    elems->supp_op_classes_len);
6091 
6092 	if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
6093 	    elems->rrm_enabled &&
6094 	    elems->rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
6095 		os_memcpy(sta->rrm_enabled_capa, elems->rrm_enabled,
6096 			  sizeof(sta->rrm_enabled_capa));
6097 
6098 	if (elems->power_capab) {
6099 		sta->min_tx_power = elems->power_capab[0];
6100 		sta->max_tx_power = elems->power_capab[1];
6101 		sta->power_capab = 1;
6102 	} else {
6103 		sta->power_capab = 0;
6104 	}
6105 
6106 	if (elems->bss_max_idle_period &&
6107 	    hapd->conf->max_acceptable_idle_period) {
6108 		u16 req;
6109 
6110 		req = WPA_GET_LE16(elems->bss_max_idle_period);
6111 		if (req <= hapd->conf->max_acceptable_idle_period)
6112 			sta->max_idle_period = req;
6113 		else if (hapd->conf->max_acceptable_idle_period >
6114 			 hapd->conf->ap_max_inactivity)
6115 			sta->max_idle_period =
6116 				hapd->conf->max_acceptable_idle_period;
6117 	}
6118 
6119 	if (elems->wfa_capab)
6120 		hostapd_wfa_capab(hapd, sta, elems->wfa_capab,
6121 				  elems->wfa_capab + elems->wfa_capab_len);
6122 
6123 out:
6124 	if (resp != WLAN_STATUS_SUCCESS || assoc_wpa_sm) {
6125 		struct wpa_state_machine *sm = sta->wpa_sm;
6126 
6127 #ifdef CONFIG_IEEE80211BE
6128 		clear_wpa_sm_for_each_partner_link(hapd, sta);
6129 		clear_wpa_sm_for_all_sta(hapd, sm);
6130 #endif /* CONFIG_IEEE80211BE */
6131 
6132 		wpa_auth_sta_deinit(sm);
6133 
6134 		/* Only keep a reference to the main wpa_sm and drop the
6135 		 * per-link instance.
6136 		 * This reference is needed during group rekey handling.
6137 		 */
6138 		if (resp == WLAN_STATUS_SUCCESS) {
6139 			sta->wpa_sm = assoc_wpa_sm;
6140 #ifdef CONFIG_IEEE80211BE
6141 			set_wpa_sm_for_each_partner_link(hapd, sta,
6142 							 assoc_wpa_sm);
6143 #endif /* CONFIG_IEEE80211BE */
6144 		} else {
6145 			sta->wpa_sm = NULL;
6146 #ifdef CONFIG_IEEE80211BE
6147 			clear_wpa_sm_for_each_partner_link(hapd, sta);
6148 #endif /* CONFIG_IEEE80211BE */
6149 		}
6150 	}
6151 
6152 	return resp;
6153 }
6154 
6155 
check_assoc_ies(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ies,size_t ies_len,enum link_parse_type type)6156 static int check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
6157 			   const u8 *ies, size_t ies_len,
6158 			   enum link_parse_type type)
6159 {
6160 	struct ieee802_11_elems elems;
6161 
6162 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
6163 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
6164 			       HOSTAPD_LEVEL_INFO,
6165 			       "Station sent an invalid association request");
6166 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
6167 	}
6168 
6169 	return __check_assoc_ies(hapd, sta, ies, ies_len, &elems, type, NULL);
6170 }
6171 
6172 
6173 #ifdef CONFIG_IEEE80211BE
6174 
ieee80211_ml_build_assoc_resp(struct hostapd_data * hapd,struct mld_link_info * link)6175 void ieee80211_ml_build_assoc_resp(struct hostapd_data *hapd,
6176 				   struct mld_link_info *link)
6177 {
6178 	u8 buf[EHT_ML_MAX_STA_PROF_LEN];
6179 	u8 *p = buf;
6180 	size_t buflen = sizeof(buf);
6181 
6182 	/* Capability Info */
6183 	WPA_PUT_LE16(p, hostapd_own_capab_info(hapd));
6184 	p += 2;
6185 
6186 	/* Status Code */
6187 	WPA_PUT_LE16(p, link->status);
6188 	p += 2;
6189 
6190 	if (link->status != WLAN_STATUS_SUCCESS)
6191 		goto out;
6192 
6193 	/* AID is not included */
6194 	p = hostapd_eid_supp_rates(hapd, p);
6195 	p = hostapd_eid_ext_supp_rates(hapd, p);
6196 	p = hostapd_eid_rm_enabled_capab(hapd, p, buf + buflen - p);
6197 	p = hostapd_eid_ht_capabilities(hapd, p);
6198 	p = hostapd_eid_ht_operation(hapd, p);
6199 
6200 	if (hostapd_is_vht_enabled(hapd)) {
6201 		p = hostapd_eid_vht_capabilities(hapd, p, 0);
6202 		p = hostapd_eid_vht_operation(hapd, p);
6203 	}
6204 
6205 	if (hostapd_is_he_enabled(hapd)) {
6206 		p = hostapd_eid_he_capab(hapd, p, IEEE80211_MODE_AP);
6207 		p = hostapd_eid_he_operation(hapd, p);
6208 		p = hostapd_eid_spatial_reuse(hapd, p);
6209 		p = hostapd_eid_he_mu_edca_parameter_set(hapd, p);
6210 		p = hostapd_eid_he_6ghz_band_cap(hapd, p);
6211 		if (hostapd_is_eht_enabled(hapd)) {
6212 			p = hostapd_eid_eht_capab(hapd, p, IEEE80211_MODE_AP);
6213 			p = hostapd_eid_eht_operation(hapd, p);
6214 		}
6215 	}
6216 
6217 	p = hostapd_eid_ext_capab(hapd, p, false);
6218 	p = hostapd_eid_mbo(hapd, p, buf + buflen - p);
6219 	p = hostapd_eid_wmm(hapd, p);
6220 
6221 	if (hapd->conf->assocresp_elements &&
6222 	    (size_t) (buf + buflen - p) >=
6223 	    wpabuf_len(hapd->conf->assocresp_elements)) {
6224 		os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
6225 			  wpabuf_len(hapd->conf->assocresp_elements));
6226 		p += wpabuf_len(hapd->conf->assocresp_elements);
6227 	}
6228 
6229 out:
6230 	os_free(link->resp_sta_profile);
6231 	link->resp_sta_profile = os_memdup(buf, p - buf);
6232 	link->resp_sta_profile_len = link->resp_sta_profile ? p - buf : 0;
6233 }
6234 
6235 
ieee80211_ml_process_link(struct hostapd_data * hapd,struct sta_info * origin_sta,struct mld_link_info * link,const u8 * ies,size_t ies_len,enum link_parse_type type,bool offload,bool * set_beacon)6236 int ieee80211_ml_process_link(struct hostapd_data *hapd,
6237 			      struct sta_info *origin_sta,
6238 			      struct mld_link_info *link,
6239 			      const u8 *ies, size_t ies_len,
6240 			      enum link_parse_type type, bool offload,
6241 			      bool *set_beacon)
6242 {
6243 	struct ieee802_11_elems elems;
6244 	struct wpabuf *mlbuf = NULL;
6245 	struct sta_info *sta = NULL;
6246 	u16 status = WLAN_STATUS_SUCCESS;
6247 	int i;
6248 
6249 	wpa_printf(MSG_DEBUG, "MLD: link: link_id=%u, peer=" MACSTR,
6250 		   hapd->mld_link_id, MAC2STR(link->peer_addr));
6251 
6252 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
6253 		wpa_printf(MSG_DEBUG, "MLD: link: Element parsing failed");
6254 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
6255 		goto out;
6256 	}
6257 
6258 	sta = ap_get_sta(hapd, origin_sta->addr);
6259 	if (sta || TEST_FAIL()) {
6260 		wpa_printf(MSG_INFO, "MLD: link: Station already exists");
6261 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
6262 		sta = NULL;
6263 		goto out;
6264 	}
6265 
6266 	sta = ap_sta_add(hapd, origin_sta->addr);
6267 	if (!sta) {
6268 		wpa_printf(MSG_DEBUG, "MLD: link: ap_sta_add() failed");
6269 		status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
6270 		goto out;
6271 	}
6272 
6273 	if (type != LINK_PARSE_RECONF) {
6274 		mlbuf = ieee802_11_defrag(elems.basic_mle, elems.basic_mle_len,
6275 					  true);
6276 		if (!mlbuf)
6277 			goto out;
6278 
6279 		if (ieee802_11_parse_link_assoc_req(&elems, mlbuf,
6280 						    hapd->mld_link_id, true) ==
6281 		    ParseFailed) {
6282 			wpa_printf(MSG_DEBUG,
6283 				   "MLD: link: Failed to parse association request Multi-Link element");
6284 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
6285 			goto out;
6286 		}
6287 	}
6288 
6289 	sta->flags |= origin_sta->flags | WLAN_STA_ASSOC_REQ_OK;
6290 	sta->mld_assoc_link_id = origin_sta->mld_assoc_link_id;
6291 	ap_sta_set_mld(sta, true);
6292 	sta->auth_alg = origin_sta->auth_alg;
6293 #ifdef CONFIG_ENC_ASSOC
6294 	sta->epp_sta = origin_sta->epp_sta;
6295 #endif /* CONFIG_ENC_ASSOC */
6296 
6297 	status = __check_assoc_ies(hapd, sta, NULL, 0, &elems, type,
6298 				   origin_sta->wpa_sm);
6299 	if (status != WLAN_STATUS_SUCCESS) {
6300 		wpa_printf(MSG_DEBUG, "MLD: link: Element check failed");
6301 		goto out;
6302 	}
6303 
6304 	os_memcpy(&sta->mld_info, &origin_sta->mld_info, sizeof(sta->mld_info));
6305 	for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
6306 		struct mld_link_info *li = &sta->mld_info.links[i];
6307 
6308 		li->resp_sta_profile = NULL;
6309 		li->resp_sta_profile_len = 0;
6310 
6311 		if (type == LINK_PARSE_RECONF && i == hapd->mld_link_id) {
6312 			os_memcpy(li->local_addr, hapd->own_addr, ETH_ALEN);
6313 			os_memcpy(li->peer_addr, link->peer_addr, ETH_ALEN);
6314 		}
6315 	}
6316 
6317 	if (!offload) {
6318 		/*
6319 		 * Get the AID from the station on which the association was
6320 		 * performed, and mark it as used.
6321 		 */
6322 		sta->aid = origin_sta->aid;
6323 		if (sta->aid == 0) {
6324 			wpa_printf(MSG_DEBUG, "MLD: link: No AID assigned");
6325 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
6326 			goto out;
6327 		}
6328 		hapd->sta_aid[(sta->aid - 1) / 32] |= BIT((sta->aid - 1) % 32);
6329 		sta->listen_interval = origin_sta->listen_interval;
6330 		if (update_ht_state(hapd, sta) > 0 && set_beacon)
6331 			*set_beacon = true;
6332 	}
6333 
6334 	/*
6335 	 * Do not initialize the EAPOL state machine.
6336 	 * TODO: Maybe it is needed?
6337 	 */
6338 	sta->eapol_sm = NULL;
6339 
6340 	wpa_printf(MSG_DEBUG, "MLD: link=%u, association OK (aid=%u)",
6341 		   hapd->mld_link_id, sta->aid);
6342 
6343 	sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC_REQ_OK;
6344 	sta->vlan_id = origin_sta->vlan_id;
6345 
6346 	/* TODO: What other processing is required? */
6347 
6348 	if (!offload &&
6349 	    add_associated_sta(hapd, sta, type == LINK_PARSE_REASSOC))
6350 		status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
6351 out:
6352 	wpabuf_free(mlbuf);
6353 	link->status = status;
6354 
6355 	if (!offload && type != LINK_PARSE_RECONF)
6356 		ieee80211_ml_build_assoc_resp(hapd, link);
6357 
6358 	wpa_printf(MSG_DEBUG, "MLD: link: status=%u", status);
6359 	if (status != WLAN_STATUS_SUCCESS) {
6360 		wpa_release_link_auth_ref(origin_sta->wpa_sm,
6361 					  hapd->mld_link_id, true);
6362 		if (sta)
6363 			ap_free_sta(hapd, sta);
6364 		return -1;
6365 	}
6366 
6367 	return 0;
6368 }
6369 
6370 
hostapd_is_multiple_link_mld(struct hostapd_data * hapd)6371 bool hostapd_is_multiple_link_mld(struct hostapd_data *hapd)
6372 {
6373 	struct hostapd_data *bss;
6374 
6375 	if (!hapd->conf->mld_ap)
6376 		return false;
6377 
6378 	if (!hapd->iface || !hapd->iface->interfaces ||
6379 	    hapd->iface->interfaces->count <= 1)
6380 		return false;
6381 
6382 	/*
6383 	 * Checking for interfaces count above is not sufficient as there
6384 	 * could be non-MLD interfaces or MLD interface that are not affiliated
6385 	 * with the same MLD as the currently processing one. So need to check
6386 	 * if other partner links exist for this the same AP MLD.
6387 	 */
6388 	for_each_mld_link(bss, hapd) {
6389 		if (bss != hapd)
6390 			return true;
6391 	}
6392 
6393 	return false;
6394 }
6395 
6396 #endif /* CONFIG_IEEE80211BE */
6397 
6398 
hostapd_process_assoc_ml_info(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ies,size_t ies_len,bool reassoc,int tx_link_status,bool offload,bool * set_beacon)6399 int hostapd_process_assoc_ml_info(struct hostapd_data *hapd,
6400 				  struct sta_info *sta,
6401 				  const u8 *ies, size_t ies_len,
6402 				  bool reassoc, int tx_link_status,
6403 				  bool offload,
6404 				  bool *set_beacon)
6405 {
6406 	int ret = 0;
6407 #ifdef CONFIG_IEEE80211BE
6408 	unsigned int i;
6409 
6410 	if (!hostapd_is_multiple_link_mld(hapd))
6411 		return 0;
6412 
6413 	for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
6414 		struct hostapd_data *bss = NULL;
6415 		struct mld_link_info *link = &sta->mld_info.links[i];
6416 		bool link_bss_found = false;
6417 
6418 		if (!link->valid || i == sta->mld_assoc_link_id)
6419 			continue;
6420 
6421 		for_each_mld_link(bss, hapd) {
6422 			if (bss == hapd)
6423 				continue;
6424 
6425 			if (bss->mld_link_id != i)
6426 				continue;
6427 
6428 			link_bss_found = true;
6429 			break;
6430 		}
6431 
6432 		if (!link_bss_found || TEST_FAIL()) {
6433 			wpa_printf(MSG_DEBUG,
6434 				   "MLD: No link match for link_id=%u", i);
6435 
6436 			link->status = WLAN_STATUS_UNSPECIFIED_FAILURE;
6437 			if (!offload)
6438 				ieee80211_ml_build_assoc_resp(hapd, link);
6439 		} else if (tx_link_status != WLAN_STATUS_SUCCESS) {
6440 			/* TX link rejected the connection */
6441 			link->status = WLAN_STATUS_DENIED_TX_LINK_NOT_ACCEPTED;
6442 			if (!offload)
6443 				ieee80211_ml_build_assoc_resp(hapd, link);
6444 		} else {
6445 			if (ieee80211_ml_process_link(
6446 				    bss, sta, link, ies, ies_len,
6447 				    reassoc ? LINK_PARSE_REASSOC :
6448 				    LINK_PARSE_ASSOC, offload,
6449 				    set_beacon))
6450 				ret = -1;
6451 		}
6452 	}
6453 #endif /* CONFIG_IEEE80211BE */
6454 
6455 	return ret;
6456 }
6457 
6458 
send_deauth(struct hostapd_data * hapd,const u8 * addr,u16 reason_code)6459 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
6460 			u16 reason_code)
6461 {
6462 	int send_len;
6463 	struct ieee80211_mgmt reply;
6464 
6465 	os_memset(&reply, 0, sizeof(reply));
6466 	reply.frame_control =
6467 		IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
6468 	os_memcpy(reply.da, addr, ETH_ALEN);
6469 	os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
6470 	os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
6471 
6472 	send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
6473 	reply.u.deauth.reason_code = host_to_le16(reason_code);
6474 
6475 	if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0, NULL, 0, 0) < 0)
6476 		wpa_printf(MSG_INFO, "Failed to send deauth: %s",
6477 			   strerror(errno));
6478 }
6479 
6480 
add_associated_sta(struct hostapd_data * hapd,struct sta_info * sta,int reassoc)6481 static int add_associated_sta(struct hostapd_data *hapd,
6482 			      struct sta_info *sta, int reassoc)
6483 {
6484 	struct ieee80211_ht_capabilities ht_cap;
6485 	struct ieee80211_vht_capabilities vht_cap;
6486 	struct ieee80211_he_capabilities he_cap;
6487 	struct ieee80211_eht_capabilities eht_cap;
6488 	int set = 1;
6489 	const u8 *mld_link_addr = NULL;
6490 	bool mld_link_sta = false, epp_sta = false;
6491 	u16 eml_cap = 0;
6492 
6493 #ifdef CONFIG_ENC_ASSOC
6494 	epp_sta = sta->epp_sta;
6495 #endif /* CONFIG_ENC_ASSOC */
6496 
6497 #ifdef CONFIG_IEEE80211BE
6498 	if (ap_sta_is_mld(hapd, sta)) {
6499 		u8 mld_link_id = hapd->mld_link_id;
6500 
6501 		mld_link_sta = sta->mld_assoc_link_id != mld_link_id;
6502 		mld_link_addr = sta->mld_info.links[mld_link_id].peer_addr;
6503 
6504 		if (hapd->mld_link_id != sta->mld_assoc_link_id)
6505 			set = 0;
6506 		eml_cap = sta->mld_info.common_info.eml_capa;
6507 	}
6508 #endif /* CONFIG_IEEE80211BE */
6509 
6510 	/*
6511 	 * Remove the STA entry to ensure the STA PS state gets cleared and
6512 	 * configuration gets updated. This is relevant for cases, such as
6513 	 * FT-over-the-DS, where a station re-associates back to the same AP but
6514 	 * skips the authentication flow, or if working with a driver that
6515 	 * does not support full AP client state.
6516 	 *
6517 	 * Skip this if the STA has already completed FT reassociation and the
6518 	 * TK has been configured since the TX/RX PN must not be reset to 0 for
6519 	 * the same key.
6520 	 *
6521 	 * FT-over-the-DS has a special case where the STA entry (and as such,
6522 	 * the TK) has not yet been configured to the driver depending on which
6523 	 * driver interface is used. For that case, allow add-STA operation to
6524 	 * be used (instead of set-STA). This is needed to allow mac80211-based
6525 	 * drivers to accept the STA parameter configuration. Since this is
6526 	 * after a new FT-over-DS exchange, a new TK has been derived, so key
6527 	 * reinstallation is not a concern for this case.
6528 	 */
6529 	wpa_printf(MSG_DEBUG, "Add associated STA " MACSTR
6530 		   " (added_unassoc=%d auth_alg=%u ft_over_ds=%u reassoc=%d authorized=%d ft_tk=%d fils_tk=%d)",
6531 		   MAC2STR(sta->addr), sta->added_unassoc, sta->auth_alg,
6532 		   sta->ft_over_ds, reassoc,
6533 		   !!(sta->flags & WLAN_STA_AUTHORIZED),
6534 		   wpa_auth_sta_ft_tk_already_set(sta->wpa_sm),
6535 		   wpa_auth_sta_fils_tk_already_set(sta->wpa_sm));
6536 
6537 	if (!mld_link_sta && !sta->added_unassoc &&
6538 	    (!(sta->flags & WLAN_STA_AUTHORIZED) ||
6539 	     (reassoc && sta->ft_over_ds && sta->auth_alg == WLAN_AUTH_FT) ||
6540 	     (!wpa_auth_sta_ft_tk_already_set(sta->wpa_sm) &&
6541 	      !wpa_auth_sta_fils_tk_already_set(sta->wpa_sm)))) {
6542 		hostapd_drv_sta_remove(hapd, sta->addr);
6543 		wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
6544 		set = 0;
6545 
6546 		 /* Do not allow the FT-over-DS exception to be used more than
6547 		  * once per authentication exchange to guarantee a new TK is
6548 		  * used here */
6549 		sta->ft_over_ds = 0;
6550 	}
6551 
6552 	if (sta->flags & WLAN_STA_HT)
6553 		hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
6554 #ifdef CONFIG_IEEE80211AC
6555 	if (sta->flags & WLAN_STA_VHT)
6556 		hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
6557 #endif /* CONFIG_IEEE80211AC */
6558 #ifdef CONFIG_IEEE80211AX
6559 	if (sta->flags & WLAN_STA_HE) {
6560 		hostapd_get_he_capab(hapd, sta->he_capab, &he_cap,
6561 				     sta->he_capab_len);
6562 	}
6563 #endif /* CONFIG_IEEE80211AX */
6564 #ifdef CONFIG_IEEE80211BE
6565 	if (sta->flags & WLAN_STA_EHT)
6566 		hostapd_get_eht_capab(hapd, sta->eht_capab, &eht_cap,
6567 				      sta->eht_capab_len);
6568 #endif /* CONFIG_IEEE80211BE */
6569 
6570 	/*
6571 	 * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
6572 	 * will be set when the ACK frame for the (Re)Association Response frame
6573 	 * is processed (TX status driver event).
6574 	 */
6575 	if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
6576 			    sta->supported_rates, sta->supported_rates_len,
6577 			    sta->listen_interval,
6578 			    sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
6579 			    sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
6580 			    sta->flags & WLAN_STA_HE ? &he_cap : NULL,
6581 			    sta->flags & WLAN_STA_HE ? sta->he_capab_len : 0,
6582 			    sta->flags & WLAN_STA_EHT ? &eht_cap : NULL,
6583 			    sta->flags & WLAN_STA_EHT ? sta->eht_capab_len : 0,
6584 			    sta->he_6ghz_capab,
6585 			    sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
6586 			    sta->vht_opmode, sta->p2p_ie ? 1 : 0,
6587 			    set, mld_link_addr, mld_link_sta, eml_cap,
6588 			    epp_sta)) {
6589 		hostapd_logger(hapd, sta->addr,
6590 			       HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
6591 			       "Could not %s STA to kernel driver",
6592 			       set ? "set" : "add");
6593 
6594 		if (sta->added_unassoc) {
6595 			hostapd_drv_sta_remove(hapd, sta->addr);
6596 			sta->added_unassoc = 0;
6597 		}
6598 
6599 		return -1;
6600 	}
6601 
6602 	sta->added_unassoc = 0;
6603 
6604 	return 0;
6605 }
6606 
6607 
send_assoc_resp(struct hostapd_data * hapd,struct sta_info * sta,const u8 * addr,u16 status_code,int reassoc,const u8 * ies,size_t ies_len,int rssi,int omit_rsnxe)6608 static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
6609 			   const u8 *addr, u16 status_code, int reassoc,
6610 			   const u8 *ies, size_t ies_len, int rssi,
6611 			   int omit_rsnxe)
6612 {
6613 	int send_len;
6614 	u8 *buf;
6615 	size_t buflen;
6616 	struct ieee80211_mgmt *reply;
6617 	u8 *p;
6618 	u16 res = WLAN_STATUS_SUCCESS;
6619 
6620 	buflen = sizeof(struct ieee80211_mgmt) + 1024;
6621 #ifdef CONFIG_FILS
6622 	if (sta && sta->fils_hlp_resp)
6623 		buflen += wpabuf_len(sta->fils_hlp_resp);
6624 	if (sta)
6625 		buflen += 150;
6626 #endif /* CONFIG_FILS */
6627 #ifdef CONFIG_OWE
6628 	if (sta && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
6629 		buflen += 150;
6630 #endif /* CONFIG_OWE */
6631 #ifdef CONFIG_DPP2
6632 	if (sta && sta->dpp_pfs)
6633 		buflen += 5 + sta->dpp_pfs->curve->prime_len;
6634 #endif /* CONFIG_DPP2 */
6635 #ifdef CONFIG_IEEE80211BE
6636 	if (hostapd_is_eht_enabled(hapd)) {
6637 		buflen += hostapd_eid_eht_capab_len(hapd, IEEE80211_MODE_AP);
6638 		buflen += 3 + sizeof(struct ieee80211_eht_operation);
6639 		if (hapd->iconf->punct_bitmap)
6640 			buflen += EHT_OPER_DISABLED_SUBCHAN_BITMAP_SIZE;
6641 		if (ap_sta_is_mld(hapd, sta))
6642 			buflen += hostapd_eid_eht_ml_tid_to_link_map_len(hapd);
6643 	}
6644 #endif /* CONFIG_IEEE80211BE */
6645 
6646 	buf = os_zalloc(buflen);
6647 	if (!buf) {
6648 		res = WLAN_STATUS_UNSPECIFIED_FAILURE;
6649 		goto done;
6650 	}
6651 	reply = (struct ieee80211_mgmt *) buf;
6652 	reply->frame_control =
6653 		IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6654 			     (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
6655 			      WLAN_FC_STYPE_ASSOC_RESP));
6656 
6657 	os_memcpy(reply->da, addr, ETH_ALEN);
6658 	os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
6659 	os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
6660 
6661 	send_len = IEEE80211_HDRLEN;
6662 	send_len += sizeof(reply->u.assoc_resp);
6663 	reply->u.assoc_resp.capab_info =
6664 		host_to_le16(hostapd_own_capab_info(hapd));
6665 	reply->u.assoc_resp.status_code = host_to_le16(status_code);
6666 
6667 	reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0) |
6668 					       BIT(14) | BIT(15));
6669 	/* Supported rates */
6670 	p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
6671 	/* Extended supported rates */
6672 	p = hostapd_eid_ext_supp_rates(hapd, p);
6673 
6674 	/* Radio measurement capabilities */
6675 	p = hostapd_eid_rm_enabled_capab(hapd, p, buf + buflen - p);
6676 
6677 #ifdef CONFIG_MBO
6678 	if (status_code == WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
6679 	    rssi != 0) {
6680 		int delta = hapd->iconf->rssi_reject_assoc_rssi - rssi;
6681 
6682 		p = hostapd_eid_mbo_rssi_assoc_rej(hapd, p, buf + buflen - p,
6683 						   delta);
6684 	}
6685 #endif /* CONFIG_MBO */
6686 
6687 #ifdef CONFIG_IEEE80211R_AP
6688 	if (sta && status_code == WLAN_STATUS_SUCCESS) {
6689 		/* IEEE 802.11r: Mobility Domain Information, Fast BSS
6690 		 * Transition Information, RSN, [RIC Response] */
6691 		p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
6692 						buf + buflen - p,
6693 						sta->auth_alg, ies, ies_len,
6694 						omit_rsnxe, reassoc,
6695 						sta->vlan_id);
6696 		if (!p) {
6697 			wpa_printf(MSG_DEBUG,
6698 				   "FT: Failed to write AssocResp IEs");
6699 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
6700 			goto done;
6701 		}
6702 	}
6703 #endif /* CONFIG_IEEE80211R_AP */
6704 #ifdef CONFIG_FILS
6705 	if (sta && status_code == WLAN_STATUS_SUCCESS &&
6706 	    (sta->auth_alg == WLAN_AUTH_FILS_SK ||
6707 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
6708 	     sta->auth_alg == WLAN_AUTH_FILS_PK))
6709 		p = wpa_auth_write_assoc_resp_fils(sta->wpa_sm, p,
6710 						   buf + buflen - p);
6711 #endif /* CONFIG_FILS */
6712 
6713 #ifdef CONFIG_OWE
6714 	if (sta && status_code == WLAN_STATUS_SUCCESS &&
6715 	    ((hapd->conf->wpa_key_mgmt |
6716 	      hapd->conf->rsn_override_key_mgmt |
6717 	      hapd->conf->rsn_override_key_mgmt_2) &
6718 	     WPA_KEY_MGMT_OWE))
6719 		p = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, p,
6720 						  buf + buflen - p);
6721 #endif /* CONFIG_OWE */
6722 
6723 	if (sta && status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
6724 		p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
6725 
6726 	p = hostapd_eid_ht_capabilities(hapd, p);
6727 	p = hostapd_eid_ht_operation(hapd, p);
6728 
6729 #ifdef CONFIG_IEEE80211AC
6730 	if (hostapd_is_vht_enabled(hapd) &&
6731 	    !is_6ghz_op_class(hapd->iconf->op_class)) {
6732 		u32 nsts = 0, sta_nsts;
6733 
6734 		if (sta && hapd->conf->use_sta_nsts && sta->vht_capabilities) {
6735 			struct ieee80211_vht_capabilities *capa;
6736 
6737 			nsts = (hapd->iface->conf->vht_capab >>
6738 				VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
6739 			capa = sta->vht_capabilities;
6740 			sta_nsts = (le_to_host32(capa->vht_capabilities_info) >>
6741 				    VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
6742 
6743 			if (nsts < sta_nsts)
6744 				nsts = 0;
6745 			else
6746 				nsts = sta_nsts;
6747 		}
6748 		p = hostapd_eid_vht_capabilities(hapd, p, nsts);
6749 		p = hostapd_eid_vht_operation(hapd, p);
6750 	}
6751 #endif /* CONFIG_IEEE80211AC */
6752 
6753 #ifdef CONFIG_IEEE80211AX
6754 	if (hostapd_is_he_enabled(hapd)) {
6755 		p = hostapd_eid_he_capab(hapd, p, IEEE80211_MODE_AP);
6756 		p = hostapd_eid_he_operation(hapd, p);
6757 		p = hostapd_eid_cca(hapd, p);
6758 		p = hostapd_eid_spatial_reuse(hapd, p);
6759 		p = hostapd_eid_he_mu_edca_parameter_set(hapd, p);
6760 		p = hostapd_eid_he_6ghz_band_cap(hapd, p);
6761 	}
6762 #endif /* CONFIG_IEEE80211AX */
6763 
6764 	p = hostapd_eid_ext_capab(hapd, p, false);
6765 	p = hostapd_eid_bss_max_idle_period(hapd, p,
6766 					    sta ? sta->max_idle_period : 0);
6767 	if (sta && sta->qos_map_enabled)
6768 		p = hostapd_eid_qos_map_set(hapd, p);
6769 
6770 #ifdef CONFIG_FST
6771 	if (hapd->iface->fst_ies) {
6772 		os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
6773 			  wpabuf_len(hapd->iface->fst_ies));
6774 		p += wpabuf_len(hapd->iface->fst_ies);
6775 	}
6776 #endif /* CONFIG_FST */
6777 
6778 #ifdef CONFIG_TESTING_OPTIONS
6779 	if (hapd->conf->rsnxe_override_ft &&
6780 	    buf + buflen - p >=
6781 	    (long int) wpabuf_len(hapd->conf->rsnxe_override_ft) &&
6782 	    sta && sta->auth_alg == WLAN_AUTH_FT) {
6783 		wpa_printf(MSG_DEBUG, "TESTING: RSNXE FT override");
6784 		os_memcpy(p, wpabuf_head(hapd->conf->rsnxe_override_ft),
6785 			  wpabuf_len(hapd->conf->rsnxe_override_ft));
6786 		p += wpabuf_len(hapd->conf->rsnxe_override_ft);
6787 		goto rsnxe_done;
6788 	}
6789 #endif /* CONFIG_TESTING_OPTIONS */
6790 	if (!omit_rsnxe)
6791 		p = hostapd_eid_rsnxe(hapd, p, buf + buflen - p);
6792 #ifdef CONFIG_TESTING_OPTIONS
6793 rsnxe_done:
6794 #endif /* CONFIG_TESTING_OPTIONS */
6795 
6796 #ifdef CONFIG_IEEE80211BE
6797 	if (hostapd_is_eht_enabled(hapd)) {
6798 		if (hapd->conf->mld_ap)
6799 			p = hostapd_eid_eht_ml_assoc(hapd, sta, p);
6800 		p = hostapd_eid_eht_capab(hapd, p, IEEE80211_MODE_AP);
6801 		p = hostapd_eid_eht_operation(hapd, p);
6802 		if (ap_sta_is_mld(hapd, sta))
6803 			p = hostapd_eid_eht_ml_tid_to_link_map(hapd, p);
6804 	}
6805 #endif /* CONFIG_IEEE80211BE */
6806 
6807 #ifdef CONFIG_OWE
6808 	if (((hapd->conf->wpa_key_mgmt | hapd->conf->rsn_override_key_mgmt |
6809 	      hapd->conf->rsn_override_key_mgmt_2) & WPA_KEY_MGMT_OWE) &&
6810 	    sta && sta->owe_ecdh && status_code == WLAN_STATUS_SUCCESS &&
6811 	    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
6812 	    !wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
6813 		struct wpabuf *pub;
6814 
6815 		pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
6816 		if (!pub) {
6817 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
6818 			goto done;
6819 		}
6820 		/* OWE Diffie-Hellman Parameter element */
6821 		*p++ = WLAN_EID_EXTENSION; /* Element ID */
6822 		*p++ = 1 + 2 + wpabuf_len(pub); /* Length */
6823 		*p++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension */
6824 		WPA_PUT_LE16(p, sta->owe_group);
6825 		p += 2;
6826 		os_memcpy(p, wpabuf_head(pub), wpabuf_len(pub));
6827 		p += wpabuf_len(pub);
6828 		wpabuf_free(pub);
6829 	}
6830 #endif /* CONFIG_OWE */
6831 
6832 #ifdef CONFIG_ENC_ASSOC
6833 	if (sta &&
6834 	    (sta->auth_alg == WLAN_AUTH_EPPKE ||
6835 	     sta->auth_alg == WLAN_AUTH_802_1X) &&
6836 	    wpa_auth_ap_sta_support_assoc_enc(sta->wpa_sm) &&
6837 	    status_code == WLAN_STATUS_SUCCESS) {
6838 		/* Ensure GMK/Counter are initialized before the Key Delivery
6839 		 * element is built. For auth-frame STAs the normal 4-way
6840 		 * handshake path (SM_STATE AUTHENTICATION2) is skipped, so
6841 		 * wpa_group_ensure_init() must be called here instead.
6842 		 * Without this, the first non-auth-frame STA that connects
6843 		 * later triggers a GTK rotation that is unknown to this STA,
6844 		 * breaking broadcast frame reception. */
6845 		wpa_auth_ensure_group_init(sta->wpa_sm);
6846 		reply->frame_control |= WLAN_FC_PROTECTED;
6847 
6848 #ifdef CONFIG_PMKSA_PRIVACY
6849 		/* Include a Nonce element (ANonce) to compute next PMKID */
6850 		if (wpa_auth_ap_sta_support_pmkid_privacy(sta->wpa_sm)) {
6851 			switch (sta->auth_alg) {
6852 			case WLAN_AUTH_EPPKE:
6853 #ifdef CONFIG_IEEE8021X_AUTH
6854 			case WLAN_AUTH_802_1X:
6855 #endif /* CONFIG_IEEE8021X_AUTH */
6856 				break;
6857 			default:
6858 				wpa_printf(MSG_INFO,
6859 					   "EPP: Unsupported auth alg %u for PMKID privacy support",
6860 					   sta->auth_alg);
6861 				goto skip_nonce;
6862 			}
6863 
6864 			*p++ = WLAN_EID_EXTENSION; /* Element ID */
6865 			*p++ = 1 + NONCE_LEN; /* Length */
6866 			*p++ = WLAN_EID_EXT_NONCE; /* Element ID Extension */
6867 			os_memcpy(p, sta->anonce, NONCE_LEN);
6868 			p += NONCE_LEN;
6869 		}
6870 	skip_nonce:
6871 #ifdef CONFIG_SAE
6872 		/* For EPPKE with SAE, propagate the password identifier from
6873 		 * the PASN wrapped SAE commit to the WPA state machine so that
6874 		 * wpa_auth_eid_key_delivery() can include the SAE PW IDs KDE
6875 		 * in the encrypted (Re)Association Response frame.
6876 		 */
6877 		if (sta->auth_alg == WLAN_AUTH_EPPKE && sta->pasn &&
6878 		    wpa_key_mgmt_sae(wpa_auth_sta_key_mgmt(sta->wpa_sm))) {
6879 			const u8 *pw_id = NULL;
6880 			size_t pw_id_len = 0;
6881 
6882 			/* Use the decrypted (real) identifier when the STA
6883 			 * presented an encrypted alternative identifier.
6884 			 * Fall back to the raw parsed identifier for the
6885 			 * plaintext case (first connection). */
6886 			if (sta->pasn->dec_pw_id &&
6887 			    sta->pasn->dec_pw_id_len) {
6888 				pw_id = sta->pasn->dec_pw_id;
6889 				pw_id_len = sta->pasn->dec_pw_id_len;
6890 			} else if (sta->pasn->sae.tmp) {
6891 				if (sta->pasn->sae.tmp->parsed_pw_id) {
6892 					pw_id = sta->pasn->sae.tmp->parsed_pw_id;
6893 					pw_id_len = sta->pasn->sae.tmp->parsed_pw_id_len;
6894 				} else if (sta->pasn->sae.tmp->pw_id) {
6895 					pw_id = sta->pasn->sae.tmp->pw_id;
6896 					pw_id_len =
6897 					sta->pasn->sae.tmp->pw_id_len;
6898 				}
6899 			}
6900 			if (pw_id && pw_id_len) {
6901 				struct wpabuf *pw_id_buf;
6902 
6903 				pw_id_buf = wpabuf_alloc_copy(pw_id, pw_id_len);
6904 				if (pw_id_buf) {
6905 					wpa_auth_set_sae_pw_id(
6906 						sta->wpa_sm, pw_id_buf,
6907 						sta->pasn->sae_pw_id_counter);
6908 					wpabuf_free(pw_id_buf);
6909 				}
6910 			}
6911 		}
6912 #endif /* CONFIG_SAE */
6913 #endif /* CONFIG_PMKSA_PRIVACY */
6914 
6915 		p = wpa_auth_write_assoc_resp_eppke(sta->wpa_sm, p,
6916 						    buf + buflen - p,
6917 						    ap_sta_is_mld(hapd, sta));
6918 	}
6919 #endif /* CONFIG_ENC_ASSOC */
6920 
6921 #ifdef CONFIG_DPP2
6922 	if (DPP_VERSION > 1 && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
6923 	    sta && sta->dpp_pfs && status_code == WLAN_STATUS_SUCCESS &&
6924 	    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP) {
6925 		os_memcpy(p, wpabuf_head(sta->dpp_pfs->ie),
6926 			  wpabuf_len(sta->dpp_pfs->ie));
6927 		p += wpabuf_len(sta->dpp_pfs->ie);
6928 	}
6929 #endif /* CONFIG_DPP2 */
6930 
6931 #ifdef CONFIG_IEEE80211AC
6932 	if (sta && hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
6933 		p = hostapd_eid_vendor_vht(hapd, p);
6934 #endif /* CONFIG_IEEE80211AC */
6935 
6936 	if (sta && (sta->flags & WLAN_STA_WMM))
6937 		p = hostapd_eid_wmm(hapd, p);
6938 
6939 #ifdef CONFIG_WPS
6940 	if (sta &&
6941 	    ((sta->flags & WLAN_STA_WPS) ||
6942 	     ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa))) {
6943 		struct wpabuf *wps = wps_build_assoc_resp_ie();
6944 		if (wps) {
6945 			os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
6946 			p += wpabuf_len(wps);
6947 			wpabuf_free(wps);
6948 		}
6949 	}
6950 #endif /* CONFIG_WPS */
6951 
6952 	if (sta && (sta->flags & WLAN_STA_MULTI_AP))
6953 		p = hostapd_eid_multi_ap(hapd, p, buf + buflen - p);
6954 
6955 #ifdef CONFIG_P2P
6956 	if (sta && sta->p2p_ie && hapd->p2p_group) {
6957 		struct wpabuf *p2p_resp_ie;
6958 		enum p2p_status_code status;
6959 		switch (status_code) {
6960 		case WLAN_STATUS_SUCCESS:
6961 			status = P2P_SC_SUCCESS;
6962 			break;
6963 		case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
6964 			status = P2P_SC_FAIL_LIMIT_REACHED;
6965 			break;
6966 		default:
6967 			status = P2P_SC_FAIL_INVALID_PARAMS;
6968 			break;
6969 		}
6970 		p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
6971 		if (p2p_resp_ie) {
6972 			os_memcpy(p, wpabuf_head(p2p_resp_ie),
6973 				  wpabuf_len(p2p_resp_ie));
6974 			p += wpabuf_len(p2p_resp_ie);
6975 			wpabuf_free(p2p_resp_ie);
6976 		}
6977 	}
6978 #endif /* CONFIG_P2P */
6979 
6980 #ifdef CONFIG_P2P_MANAGER
6981 	if (hapd->conf->p2p & P2P_MANAGE)
6982 		p = hostapd_eid_p2p_manage(hapd, p);
6983 #endif /* CONFIG_P2P_MANAGER */
6984 
6985 	p = hostapd_eid_mbo(hapd, p, buf + buflen - p);
6986 
6987 	if (hapd->conf->assocresp_elements &&
6988 	    (size_t) (buf + buflen - p) >=
6989 	    wpabuf_len(hapd->conf->assocresp_elements)) {
6990 		os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
6991 			  wpabuf_len(hapd->conf->assocresp_elements));
6992 		p += wpabuf_len(hapd->conf->assocresp_elements);
6993 	}
6994 
6995 	send_len += p - reply->u.assoc_resp.variable;
6996 
6997 #ifdef CONFIG_FILS
6998 	if (sta &&
6999 	    (sta->auth_alg == WLAN_AUTH_FILS_SK ||
7000 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
7001 	     sta->auth_alg == WLAN_AUTH_FILS_PK) &&
7002 	    status_code == WLAN_STATUS_SUCCESS) {
7003 		struct ieee802_11_elems elems;
7004 
7005 		if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
7006 		    ParseFailed || !elems.fils_session) {
7007 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7008 			goto done;
7009 		}
7010 
7011 		/* FILS Session */
7012 		*p++ = WLAN_EID_EXTENSION; /* Element ID */
7013 		*p++ = 1 + FILS_SESSION_LEN; /* Length */
7014 		*p++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
7015 		os_memcpy(p, elems.fils_session, FILS_SESSION_LEN);
7016 		send_len += 2 + 1 + FILS_SESSION_LEN;
7017 
7018 		send_len = fils_encrypt_assoc(sta->wpa_sm, buf, send_len,
7019 					      buflen, sta->fils_hlp_resp);
7020 		if (send_len < 0) {
7021 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7022 			goto done;
7023 		}
7024 	}
7025 #endif /* CONFIG_FILS */
7026 
7027 	if (hostapd_drv_send_mlme(hapd, reply, send_len, 0, NULL, 0, 0) < 0) {
7028 		wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
7029 			   strerror(errno));
7030 		res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7031 	}
7032 
7033 done:
7034 	os_free(buf);
7035 	return res;
7036 }
7037 
7038 
7039 #ifdef CONFIG_OWE
owe_assoc_req_process(struct hostapd_data * hapd,struct sta_info * sta,const u8 * owe_dh,u8 owe_dh_len,u8 * owe_buf,size_t owe_buf_len,u16 * status)7040 u8 * owe_assoc_req_process(struct hostapd_data *hapd, struct sta_info *sta,
7041 			   const u8 *owe_dh, u8 owe_dh_len,
7042 			   u8 *owe_buf, size_t owe_buf_len, u16 *status)
7043 {
7044 #ifdef CONFIG_TESTING_OPTIONS
7045 	if (hapd->conf->own_ie_override) {
7046 		wpa_printf(MSG_DEBUG, "OWE: Using IE override");
7047 		*status = WLAN_STATUS_SUCCESS;
7048 		return wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
7049 						     owe_buf_len);
7050 	}
7051 #endif /* CONFIG_TESTING_OPTIONS */
7052 
7053 	if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
7054 		wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
7055 		owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
7056 							owe_buf_len);
7057 		*status = WLAN_STATUS_SUCCESS;
7058 		return owe_buf;
7059 	}
7060 
7061 	if (sta->owe_pmk && sta->external_dh_updated) {
7062 		wpa_printf(MSG_DEBUG, "OWE: Using previously derived PMK");
7063 		*status = WLAN_STATUS_SUCCESS;
7064 		return owe_buf;
7065 	}
7066 
7067 	*status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
7068 	if (*status != WLAN_STATUS_SUCCESS)
7069 		return NULL;
7070 
7071 	owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
7072 						owe_buf_len);
7073 
7074 	if (sta->owe_ecdh && owe_buf) {
7075 		struct wpabuf *pub;
7076 
7077 		pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
7078 		if (!pub) {
7079 			*status = WLAN_STATUS_UNSPECIFIED_FAILURE;
7080 			return owe_buf;
7081 		}
7082 
7083 		/* OWE Diffie-Hellman Parameter element */
7084 		*owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
7085 		*owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
7086 		*owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
7087 							 */
7088 		WPA_PUT_LE16(owe_buf, sta->owe_group);
7089 		owe_buf += 2;
7090 		os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
7091 		owe_buf += wpabuf_len(pub);
7092 		wpabuf_free(pub);
7093 	}
7094 
7095 	return owe_buf;
7096 }
7097 #endif /* CONFIG_OWE */
7098 
7099 
7100 #ifdef CONFIG_FILS
7101 
fils_hlp_finish_assoc(struct hostapd_data * hapd,struct sta_info * sta)7102 void fils_hlp_finish_assoc(struct hostapd_data *hapd, struct sta_info *sta)
7103 {
7104 	u16 reply_res;
7105 
7106 	wpa_printf(MSG_DEBUG, "FILS: Finish association with " MACSTR,
7107 		   MAC2STR(sta->addr));
7108 	eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
7109 	if (!sta->fils_pending_assoc_req)
7110 		return;
7111 	reply_res = send_assoc_resp(hapd, sta, sta->addr, WLAN_STATUS_SUCCESS,
7112 				    sta->fils_pending_assoc_is_reassoc,
7113 				    sta->fils_pending_assoc_req,
7114 				    sta->fils_pending_assoc_req_len, 0, 0);
7115 	os_free(sta->fils_pending_assoc_req);
7116 	sta->fils_pending_assoc_req = NULL;
7117 	sta->fils_pending_assoc_req_len = 0;
7118 	wpabuf_free(sta->fils_hlp_resp);
7119 	sta->fils_hlp_resp = NULL;
7120 	wpabuf_free(sta->hlp_dhcp_discover);
7121 	sta->hlp_dhcp_discover = NULL;
7122 
7123 	/*
7124 	 * Remove the station in case transmission of a success response fails.
7125 	 * At this point the station was already added associated to the driver.
7126 	 */
7127 	if (reply_res != WLAN_STATUS_SUCCESS)
7128 		hostapd_drv_sta_remove(hapd, sta->addr);
7129 }
7130 
7131 
fils_hlp_timeout(void * eloop_ctx,void * eloop_data)7132 void fils_hlp_timeout(void *eloop_ctx, void *eloop_data)
7133 {
7134 	struct hostapd_data *hapd = eloop_ctx;
7135 	struct sta_info *sta = eloop_data;
7136 
7137 	wpa_printf(MSG_DEBUG,
7138 		   "FILS: HLP response timeout - continue with association response for "
7139 		   MACSTR, MAC2STR(sta->addr));
7140 	if (sta->fils_drv_assoc_finish)
7141 		hostapd_notify_assoc_fils_finish(hapd, sta);
7142 	else
7143 		fils_hlp_finish_assoc(hapd, sta);
7144 }
7145 
7146 #endif /* CONFIG_FILS */
7147 
7148 
7149 #ifdef CONFIG_IEEE80211BE
handle_mlo_translate(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,bool reassoc,struct hostapd_data ** assoc_hapd)7150 static struct sta_info * handle_mlo_translate(struct hostapd_data *hapd,
7151 					      const struct ieee80211_mgmt *mgmt,
7152 					      size_t len, bool reassoc,
7153 					      struct hostapd_data **assoc_hapd)
7154 {
7155 	struct sta_info *sta;
7156 	struct ieee802_11_elems elems;
7157 	u8 mld_addr[ETH_ALEN];
7158 	const u8 *pos;
7159 
7160 	if (!hostapd_is_eht_enabled(hapd))
7161 		return NULL;
7162 
7163 	if (reassoc) {
7164 		len -= IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req);
7165 		pos = mgmt->u.reassoc_req.variable;
7166 	} else {
7167 		len -= IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req);
7168 		pos = mgmt->u.assoc_req.variable;
7169 	}
7170 
7171 	if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed)
7172 		return NULL;
7173 
7174 	if (hostapd_process_ml_assoc_req_addr(hapd, elems.basic_mle,
7175 					      elems.basic_mle_len,
7176 					      mld_addr))
7177 		return NULL;
7178 
7179 	sta = ap_get_sta(hapd, mld_addr);
7180 	if (!sta)
7181 		return NULL;
7182 
7183 	wpa_printf(MSG_DEBUG, "MLD: assoc: mld=" MACSTR ", link=" MACSTR,
7184 		   MAC2STR(mld_addr), MAC2STR(mgmt->sa));
7185 
7186 	return hostapd_ml_get_assoc_sta(hapd, sta, assoc_hapd);
7187 }
7188 #endif /* CONFIG_IEEE80211BE */
7189 
7190 
handle_assoc(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int reassoc,int rssi)7191 static void handle_assoc(struct hostapd_data *hapd,
7192 			 const struct ieee80211_mgmt *mgmt, size_t len,
7193 			 int reassoc, int rssi)
7194 {
7195 	u16 capab_info, listen_interval, seq_ctrl, fc;
7196 	int resp = WLAN_STATUS_SUCCESS;
7197 	u16 reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7198 	const u8 *pos;
7199 	int left, i;
7200 	struct sta_info *sta;
7201 	u8 *tmp = NULL;
7202 #ifdef CONFIG_FILS
7203 	int delay_assoc = 0;
7204 #endif /* CONFIG_FILS */
7205 	int omit_rsnxe = 0;
7206 	bool set_beacon = false;
7207 	bool mld_addrs_not_translated = false;
7208 
7209 	if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
7210 				      sizeof(mgmt->u.assoc_req))) {
7211 		wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
7212 			   reassoc, (unsigned long) len);
7213 		return;
7214 	}
7215 
7216 #ifdef CONFIG_TESTING_OPTIONS
7217 	if (reassoc) {
7218 		if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
7219 		    drand48() < hapd->iconf->ignore_reassoc_probability) {
7220 			wpa_printf(MSG_INFO,
7221 				   "TESTING: ignoring reassoc request from "
7222 				   MACSTR, MAC2STR(mgmt->sa));
7223 			return;
7224 		}
7225 	} else {
7226 		if (hapd->iconf->ignore_assoc_probability > 0.0 &&
7227 		    drand48() < hapd->iconf->ignore_assoc_probability) {
7228 			wpa_printf(MSG_INFO,
7229 				   "TESTING: ignoring assoc request from "
7230 				   MACSTR, MAC2STR(mgmt->sa));
7231 			return;
7232 		}
7233 	}
7234 #endif /* CONFIG_TESTING_OPTIONS */
7235 
7236 	fc = le_to_host16(mgmt->frame_control);
7237 	seq_ctrl = le_to_host16(mgmt->seq_ctrl);
7238 
7239 	if (reassoc) {
7240 		capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
7241 		listen_interval = le_to_host16(
7242 			mgmt->u.reassoc_req.listen_interval);
7243 		wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
7244 			   " capab_info=0x%02x listen_interval=%d current_ap="
7245 			   MACSTR " seq_ctrl=0x%x%s",
7246 			   MAC2STR(mgmt->sa), capab_info, listen_interval,
7247 			   MAC2STR(mgmt->u.reassoc_req.current_ap),
7248 			   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
7249 		left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
7250 		pos = mgmt->u.reassoc_req.variable;
7251 	} else {
7252 		capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
7253 		listen_interval = le_to_host16(
7254 			mgmt->u.assoc_req.listen_interval);
7255 		wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
7256 			   " capab_info=0x%02x listen_interval=%d "
7257 			   "seq_ctrl=0x%x%s",
7258 			   MAC2STR(mgmt->sa), capab_info, listen_interval,
7259 			   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
7260 		left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
7261 		pos = mgmt->u.assoc_req.variable;
7262 	}
7263 
7264 	sta = ap_get_sta(hapd, mgmt->sa);
7265 
7266 #ifdef CONFIG_IEEE80211BE
7267 	/*
7268 	 * It is possible that the association frame is from an associated
7269 	 * non-AP MLD station, that tries to re-associate using different link
7270 	 * addresses. In such a case, try to find the station based on the AP
7271 	 * MLD MAC address.
7272 	 */
7273 	if (!sta) {
7274 		struct hostapd_data *assoc_hapd;
7275 
7276 		sta = handle_mlo_translate(hapd, mgmt, len, reassoc,
7277 					   &assoc_hapd);
7278 		if (sta) {
7279 			if (hapd != assoc_hapd) {
7280 				wpa_printf(MSG_DEBUG,
7281 					   "MLD: Switching to assoc hapd/station");
7282 				hapd = assoc_hapd;
7283 				mld_addrs_not_translated = true;
7284 			}
7285 
7286 			/* Allow link address to be changed if an SA query
7287 			 * procedure has expired. */
7288 			if (sta->sa_query_timed_out) {
7289 				u8 _link = hapd->mld_link_id;
7290 
7291 				os_memcpy(sta->mld_info.links[_link].peer_addr,
7292 					  mgmt->sa, ETH_ALEN);
7293 			}
7294 
7295 		}
7296 	}
7297 #endif /* CONFIG_IEEE80211BE */
7298 
7299 #ifdef CONFIG_IEEE80211R_AP
7300 	if (sta && sta->auth_alg == WLAN_AUTH_FT &&
7301 	    (sta->flags & WLAN_STA_AUTH) == 0) {
7302 		wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
7303 			   "prior to authentication since it is using "
7304 			   "over-the-DS FT", MAC2STR(mgmt->sa));
7305 
7306 		/*
7307 		 * Mark station as authenticated, to avoid adding station
7308 		 * entry in the driver as associated and not authenticated
7309 		 */
7310 		sta->flags |= WLAN_STA_AUTH;
7311 	} else
7312 #endif /* CONFIG_IEEE80211R_AP */
7313 	if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
7314 		if (hapd->iface->current_mode &&
7315 		    hapd->iface->current_mode->mode ==
7316 			HOSTAPD_MODE_IEEE80211AD) {
7317 			int acl_res;
7318 			struct radius_sta info;
7319 
7320 			acl_res = ieee802_11_allowed_address(hapd, mgmt->sa,
7321 							     (const u8 *) mgmt,
7322 							     len, &info);
7323 			if (acl_res == HOSTAPD_ACL_REJECT) {
7324 				wpa_msg(hapd->msg_ctx, MSG_DEBUG,
7325 					"Ignore Association Request frame from "
7326 					MACSTR " due to ACL reject",
7327 					MAC2STR(mgmt->sa));
7328 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
7329 				goto fail;
7330 			}
7331 			if (acl_res == HOSTAPD_ACL_PENDING)
7332 				return;
7333 
7334 			/* DMG/IEEE 802.11ad does not use authentication.
7335 			 * Allocate sta entry upon association. */
7336 			sta = ap_sta_add(hapd, mgmt->sa);
7337 			if (!sta) {
7338 				hostapd_logger(hapd, mgmt->sa,
7339 					       HOSTAPD_MODULE_IEEE80211,
7340 					       HOSTAPD_LEVEL_INFO,
7341 					       "Failed to add STA");
7342 				resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
7343 				goto fail;
7344 			}
7345 
7346 			acl_res = ieee802_11_set_radius_info(
7347 				hapd, sta, acl_res, &info);
7348 			if (acl_res) {
7349 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
7350 				goto fail;
7351 			}
7352 
7353 			hostapd_logger(hapd, sta->addr,
7354 				       HOSTAPD_MODULE_IEEE80211,
7355 				       HOSTAPD_LEVEL_DEBUG,
7356 				       "Skip authentication for DMG/IEEE 802.11ad");
7357 			sta->flags |= WLAN_STA_AUTH;
7358 			wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
7359 			sta->auth_alg = WLAN_AUTH_OPEN;
7360 		} else {
7361 			hostapd_logger(hapd, mgmt->sa,
7362 				       HOSTAPD_MODULE_IEEE80211,
7363 				       HOSTAPD_LEVEL_INFO,
7364 				       "Station tried to associate before authentication (aid=%d flags=0x%x)",
7365 				       sta ? sta->aid : -1,
7366 				       sta ? sta->flags : 0);
7367 			send_deauth(hapd, mgmt->sa,
7368 				    WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
7369 			return;
7370 		}
7371 	}
7372 
7373 	if ((fc & WLAN_FC_RETRY) &&
7374 	    sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
7375 	    sta->last_seq_ctrl == seq_ctrl &&
7376 	    sta->last_subtype == (reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
7377 				  WLAN_FC_STYPE_ASSOC_REQ)) {
7378 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
7379 			       HOSTAPD_LEVEL_DEBUG,
7380 			       "Drop repeated association frame seq_ctrl=0x%x",
7381 			       seq_ctrl);
7382 		return;
7383 	}
7384 	sta->last_seq_ctrl = seq_ctrl;
7385 	sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
7386 		WLAN_FC_STYPE_ASSOC_REQ;
7387 
7388 	if (hapd->tkip_countermeasures) {
7389 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
7390 		goto fail;
7391 	}
7392 
7393 	if (listen_interval > hapd->conf->max_listen_interval) {
7394 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
7395 			       HOSTAPD_LEVEL_DEBUG,
7396 			       "Too large Listen Interval (%d)",
7397 			       listen_interval);
7398 		resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
7399 		goto fail;
7400 	}
7401 
7402 #ifdef CONFIG_MBO
7403 	if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
7404 		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
7405 		goto fail;
7406 	}
7407 
7408 	if (hapd->iconf->rssi_reject_assoc_rssi && rssi &&
7409 	    rssi < hapd->iconf->rssi_reject_assoc_rssi &&
7410 	    (sta->auth_rssi == 0 ||
7411 	     sta->auth_rssi < hapd->iconf->rssi_reject_assoc_rssi)) {
7412 		resp = WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS;
7413 		goto fail;
7414 	}
7415 #endif /* CONFIG_MBO */
7416 
7417 	if (hapd->conf->wpa &&
7418 	    check_sa_query(hapd, sta, reassoc, pos, left,
7419 			   fc & WLAN_FC_PROTECTED)) {
7420 		resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
7421 		goto fail;
7422 	}
7423 
7424 	/*
7425 	 * sta->capability is used in check_assoc_ies() for RRM enabled
7426 	 * capability element.
7427 	 */
7428 	sta->capability = capab_info;
7429 
7430 #ifdef CONFIG_FILS
7431 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
7432 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
7433 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
7434 		int res;
7435 
7436 		/* The end of the payload is encrypted. Need to decrypt it
7437 		 * before parsing. */
7438 
7439 		tmp = os_memdup(pos, left);
7440 		if (!tmp) {
7441 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
7442 			goto fail;
7443 		}
7444 
7445 		res = fils_decrypt_assoc(sta->wpa_sm, sta->fils_session, mgmt,
7446 					 len, tmp, left);
7447 		if (res < 0) {
7448 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
7449 			goto fail;
7450 		}
7451 		pos = tmp;
7452 		left = res;
7453 	}
7454 #endif /* CONFIG_FILS */
7455 
7456 	/* followed by SSID and Supported rates; and HT capabilities if 802.11n
7457 	 * is used */
7458 	resp = check_assoc_ies(hapd, sta, pos, left,
7459 			       reassoc ? LINK_PARSE_REASSOC : LINK_PARSE_ASSOC);
7460 	if (resp != WLAN_STATUS_SUCCESS)
7461 		goto fail;
7462 #ifdef CONFIG_IEEE80211R_AP
7463 	if (reassoc && sta->auth_alg == WLAN_AUTH_FT)
7464 		omit_rsnxe = !get_ie(pos, left, WLAN_EID_RSNX);
7465 #endif /* CONFIG_IEEE80211R_AP */
7466 	if (hapd->conf->rsn_override_omit_rsnxe)
7467 		omit_rsnxe = 1;
7468 
7469 	if (hostapd_get_aid(hapd, sta) < 0) {
7470 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
7471 			       HOSTAPD_LEVEL_INFO, "No room for more AIDs");
7472 		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
7473 		goto fail;
7474 	}
7475 
7476 	sta->listen_interval = listen_interval;
7477 
7478 	if (hapd->iface->current_mode &&
7479 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
7480 		sta->flags |= WLAN_STA_NONERP;
7481 	for (i = 0; i < sta->supported_rates_len; i++) {
7482 		if ((sta->supported_rates[i] & 0x7f) > 22) {
7483 			sta->flags &= ~WLAN_STA_NONERP;
7484 			break;
7485 		}
7486 	}
7487 	if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
7488 		sta->nonerp_set = 1;
7489 		hapd->iface->num_sta_non_erp++;
7490 		if (hapd->iface->num_sta_non_erp == 1)
7491 			set_beacon = true;
7492 	}
7493 
7494 	if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
7495 	    !sta->no_short_slot_time_set) {
7496 		sta->no_short_slot_time_set = 1;
7497 		hapd->iface->num_sta_no_short_slot_time++;
7498 		if (hapd->iface->current_mode &&
7499 		    hapd->iface->current_mode->mode ==
7500 		    HOSTAPD_MODE_IEEE80211G &&
7501 		    hapd->iface->num_sta_no_short_slot_time == 1)
7502 			set_beacon = true;
7503 	}
7504 
7505 	if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7506 		sta->flags |= WLAN_STA_SHORT_PREAMBLE;
7507 	else
7508 		sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
7509 
7510 	if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
7511 	    !sta->no_short_preamble_set) {
7512 		sta->no_short_preamble_set = 1;
7513 		hapd->iface->num_sta_no_short_preamble++;
7514 		if (hapd->iface->current_mode &&
7515 		    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
7516 		    && hapd->iface->num_sta_no_short_preamble == 1)
7517 			set_beacon = true;
7518 	}
7519 
7520 	if (update_ht_state(hapd, sta) > 0)
7521 		set_beacon = true;
7522 
7523 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
7524 		       HOSTAPD_LEVEL_DEBUG,
7525 		       "association OK (aid %d)", sta->aid);
7526 	/* Station will be marked associated, after it acknowledges AssocResp
7527 	 */
7528 	sta->flags |= WLAN_STA_ASSOC_REQ_OK;
7529 
7530 	if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
7531 		wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
7532 			   "SA Query procedure", reassoc ? "re" : "");
7533 		/* TODO: Send a protected Disassociate frame to the STA using
7534 		 * the old key and Reason Code "Previous Authentication no
7535 		 * longer valid". Make sure this is only sent protected since
7536 		 * unprotected frame would be received by the STA that is now
7537 		 * trying to associate.
7538 		 */
7539 	}
7540 
7541 	/* Make sure that the previously registered inactivity timer will not
7542 	 * remove the STA immediately. */
7543 	sta->timeout_next = STA_NULLFUNC;
7544 
7545 #ifdef CONFIG_TAXONOMY
7546 	taxonomy_sta_info_assoc_req(hapd, sta, pos, left);
7547 #endif /* CONFIG_TAXONOMY */
7548 
7549 	sta->pending_wds_enable = 0;
7550 
7551 #ifdef CONFIG_FILS
7552 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
7553 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
7554 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
7555 		if (fils_process_hlp(hapd, sta, pos, left) > 0)
7556 			delay_assoc = 1;
7557 	}
7558 #endif /* CONFIG_FILS */
7559 
7560  fail:
7561 
7562 	/*
7563 	 * In case of a successful response, add the station to the driver.
7564 	 * Otherwise, the kernel may ignore Data frames before we process the
7565 	 * ACK frame (TX status). In case of a failure, this station will be
7566 	 * removed.
7567 	 *
7568 	 * Note that this is not compliant with the IEEE 802.11 standard that
7569 	 * states that a non-AP station should transition into the
7570 	 * authenticated/associated state only after the station acknowledges
7571 	 * the (Re)Association Response frame. However, still do this as:
7572 	 *
7573 	 * 1. In case the station does not acknowledge the (Re)Association
7574 	 *    Response frame, it will be removed.
7575 	 * 2. Data frames will be dropped in the kernel until the station is
7576 	 *    set into authorized state, and there are no significant known
7577 	 *    issues with processing other non-Data Class 3 frames during this
7578 	 *    window.
7579 	 */
7580 	if (sta)
7581 		hostapd_process_assoc_ml_info(hapd, sta, pos, left, reassoc,
7582 					      resp, false, &set_beacon);
7583 
7584 	if (resp == WLAN_STATUS_SUCCESS && sta &&
7585 	    add_associated_sta(hapd, sta, reassoc))
7586 		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
7587 
7588 #ifdef CONFIG_FILS
7589 	if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS &&
7590 	    eloop_is_timeout_registered(fils_hlp_timeout, hapd, sta) &&
7591 	    sta->fils_pending_assoc_req) {
7592 		if (set_beacon)
7593 			ieee802_11_update_beacons(hapd->iface);
7594 
7595 		/* Do not reschedule fils_hlp_timeout in case the station
7596 		 * retransmits (Re)Association Request frame while waiting for
7597 		 * the previously started FILS HLP wait, so that the timeout can
7598 		 * be determined from the first pending attempt. */
7599 		wpa_printf(MSG_DEBUG,
7600 			   "FILS: Continue waiting for HLP processing before sending (Re)Association Response frame to "
7601 			   MACSTR, MAC2STR(sta->addr));
7602 		os_free(tmp);
7603 		return;
7604 	}
7605 	if (sta) {
7606 		eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
7607 		os_free(sta->fils_pending_assoc_req);
7608 		sta->fils_pending_assoc_req = NULL;
7609 		sta->fils_pending_assoc_req_len = 0;
7610 		wpabuf_free(sta->fils_hlp_resp);
7611 		sta->fils_hlp_resp = NULL;
7612 	}
7613 	if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS) {
7614 		if (set_beacon)
7615 			ieee802_11_update_beacons(hapd->iface);
7616 
7617 		sta->fils_pending_assoc_req = tmp;
7618 		sta->fils_pending_assoc_req_len = left;
7619 		sta->fils_pending_assoc_is_reassoc = reassoc;
7620 		sta->fils_drv_assoc_finish = 0;
7621 		wpa_printf(MSG_DEBUG,
7622 			   "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
7623 			   MACSTR, MAC2STR(sta->addr));
7624 		eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
7625 		eloop_register_timeout(0, hapd->conf->fils_hlp_wait_time * 1024,
7626 				       fils_hlp_timeout, hapd, sta);
7627 		return;
7628 	}
7629 #endif /* CONFIG_FILS */
7630 
7631 #ifdef CONFIG_TESTING_OPTIONS
7632 	if (hapd->conf->association_response_status_code >= 0) {
7633 		wpa_printf(MSG_DEBUG,
7634 			   "TESTING: Forcing association response status code to %d",
7635 			   hapd->conf->association_response_status_code);
7636 		resp = hapd->conf->association_response_status_code;
7637 	}
7638 #endif /* CONFIG_TESTING_OPTIONS */
7639 
7640 	if (resp >= 0)
7641 		reply_res = send_assoc_resp(hapd,
7642 					    mld_addrs_not_translated ?
7643 					    NULL : sta,
7644 					    mgmt->sa, resp, reassoc,
7645 					    pos, left, rssi, omit_rsnxe);
7646 
7647 	if (set_beacon)
7648 		ieee802_11_update_beacons(hapd->iface);
7649 
7650 	os_free(tmp);
7651 
7652 	/*
7653 	 * Remove the station in case transmission of a success response fails
7654 	 * (the STA was added associated to the driver) or if the station was
7655 	 * previously added unassociated.
7656 	 */
7657 	if (sta && ((reply_res != WLAN_STATUS_SUCCESS &&
7658 		     resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc)) {
7659 		hostapd_drv_sta_remove(hapd, sta->addr);
7660 		sta->added_unassoc = 0;
7661 	}
7662 }
7663 
7664 
hostapd_deauth_sta(struct hostapd_data * hapd,struct sta_info * sta,const struct ieee80211_mgmt * mgmt)7665 static void hostapd_deauth_sta(struct hostapd_data *hapd,
7666 			       struct sta_info *sta,
7667 			       const struct ieee80211_mgmt *mgmt)
7668 {
7669 	wpa_msg(hapd->msg_ctx, MSG_DEBUG,
7670 		"deauthentication: STA=" MACSTR " reason_code=%d",
7671 		MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
7672 
7673 	ap_sta_set_authorized(hapd, sta, 0);
7674 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
7675 	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
7676 			WLAN_STA_ASSOC_REQ_OK);
7677 	hostapd_set_sta_flags(hapd, sta);
7678 	wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
7679 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
7680 		       HOSTAPD_LEVEL_DEBUG, "deauthenticated");
7681 	mlme_deauthenticate_indication(
7682 		hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
7683 	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
7684 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
7685 	ap_free_sta(hapd, sta);
7686 }
7687 
7688 
hostapd_disassoc_sta(struct hostapd_data * hapd,struct sta_info * sta,const struct ieee80211_mgmt * mgmt)7689 static void hostapd_disassoc_sta(struct hostapd_data *hapd,
7690 				 struct sta_info *sta,
7691 				 const struct ieee80211_mgmt *mgmt)
7692 {
7693 	wpa_msg(hapd->msg_ctx, MSG_DEBUG,
7694 		"disassocation: STA=" MACSTR " reason_code=%d",
7695 		MAC2STR(mgmt->sa), le_to_host16(mgmt->u.disassoc.reason_code));
7696 
7697 	ap_sta_set_authorized(hapd, sta, 0);
7698 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
7699 	sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
7700 	hostapd_set_sta_flags(hapd, sta);
7701 	wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
7702 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
7703 		       HOSTAPD_LEVEL_INFO, "disassociated");
7704 	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
7705 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
7706 	/* Stop Accounting and IEEE 802.1X sessions, but leave the STA
7707 	 * authenticated. */
7708 	accounting_sta_stop(hapd, sta);
7709 	ieee802_1x_free_station(hapd, sta);
7710 	if (sta->ipaddr)
7711 		hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
7712 	ap_sta_ip6addr_del(hapd, sta);
7713 	hostapd_drv_sta_remove(hapd, sta->addr);
7714 	sta->added_unassoc = 0;
7715 
7716 	if (sta->timeout_next == STA_NULLFUNC ||
7717 	    sta->timeout_next == STA_DISASSOC) {
7718 		sta->timeout_next = STA_DEAUTH;
7719 		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
7720 		eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
7721 				       hapd, sta);
7722 	}
7723 
7724 	mlme_disassociate_indication(
7725 		hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
7726 
7727 	/* DMG/IEEE 802.11ad does not use deauthication. Deallocate sta upon
7728 	 * disassociation. */
7729 	if (hapd->iface->current_mode &&
7730 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
7731 		sta->flags &= ~WLAN_STA_AUTH;
7732 		wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
7733 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
7734 			       HOSTAPD_LEVEL_DEBUG, "deauthenticated");
7735 		ap_free_sta(hapd, sta);
7736 	}
7737 }
7738 
7739 
hostapd_ml_handle_disconnect(struct hostapd_data * hapd,struct sta_info * sta,const struct ieee80211_mgmt * mgmt,bool disassoc)7740 static bool hostapd_ml_handle_disconnect(struct hostapd_data *hapd,
7741 					 struct sta_info *sta,
7742 					 const struct ieee80211_mgmt *mgmt,
7743 					 bool disassoc)
7744 {
7745 #ifdef CONFIG_IEEE80211BE
7746 	struct hostapd_data *assoc_hapd, *tmp_hapd;
7747 	struct sta_info *assoc_sta;
7748 	struct sta_info *tmp_sta;
7749 
7750 	if (!hostapd_is_multiple_link_mld(hapd))
7751 		return false;
7752 
7753 	/*
7754 	 * Get the station on which the association was performed, as it holds
7755 	 * the information about all the other links.
7756 	 */
7757 	assoc_sta = hostapd_ml_get_assoc_sta(hapd, sta, &assoc_hapd);
7758 	if (!assoc_sta)
7759 		return false;
7760 
7761 	for_each_mld_link(tmp_hapd, assoc_hapd) {
7762 		if (tmp_hapd == assoc_hapd)
7763 			continue;
7764 
7765 		if (!assoc_sta->mld_info.links[tmp_hapd->mld_link_id].valid)
7766 			continue;
7767 
7768 		for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
7769 		     tmp_sta = tmp_sta->next) {
7770 			if (tmp_sta->mld_assoc_link_id !=
7771 			    assoc_sta->mld_assoc_link_id ||
7772 			    tmp_sta->aid != assoc_sta->aid)
7773 				continue;
7774 
7775 			if (!disassoc)
7776 				hostapd_deauth_sta(tmp_hapd, tmp_sta, mgmt);
7777 			else
7778 				hostapd_disassoc_sta(tmp_hapd, tmp_sta, mgmt);
7779 			break;
7780 		}
7781 	}
7782 
7783 	/* Remove the station on which the association was performed. */
7784 	if (!disassoc)
7785 		hostapd_deauth_sta(assoc_hapd, assoc_sta, mgmt);
7786 	else
7787 		hostapd_disassoc_sta(assoc_hapd, assoc_sta, mgmt);
7788 
7789 	return true;
7790 #else /* CONFIG_IEEE80211BE */
7791 	return false;
7792 #endif /* CONFIG_IEEE80211BE */
7793 }
7794 
7795 
handle_disassoc(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len)7796 static void handle_disassoc(struct hostapd_data *hapd,
7797 			    const struct ieee80211_mgmt *mgmt, size_t len)
7798 {
7799 	struct sta_info *sta;
7800 
7801 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
7802 		wpa_msg(hapd->msg_ctx, MSG_DEBUG,
7803 			   "handle_disassoc - too short payload (len=%lu)",
7804 			   (unsigned long) len);
7805 		return;
7806 	}
7807 
7808 	sta = ap_get_sta(hapd, mgmt->sa);
7809 	if (!sta) {
7810 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
7811 			" trying to disassociate, but it is not associated",
7812 			MAC2STR(mgmt->sa));
7813 		return;
7814 	}
7815 
7816 	if (hostapd_ml_handle_disconnect(hapd, sta, mgmt, true))
7817 		return;
7818 
7819 	hostapd_disassoc_sta(hapd, sta, mgmt);
7820 }
7821 
7822 
handle_deauth(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len)7823 static void handle_deauth(struct hostapd_data *hapd,
7824 			  const struct ieee80211_mgmt *mgmt, size_t len)
7825 {
7826 	struct sta_info *sta;
7827 
7828 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
7829 		wpa_msg(hapd->msg_ctx, MSG_DEBUG,
7830 			"handle_deauth - too short payload (len=%lu)",
7831 			(unsigned long) len);
7832 		return;
7833 	}
7834 
7835 	/* Clear the PTKSA cache entries for PASN */
7836 	ptksa_cache_flush(hapd->ptksa, mgmt->sa, WPA_CIPHER_NONE);
7837 
7838 	sta = ap_get_sta(hapd, mgmt->sa);
7839 	if (!sta) {
7840 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
7841 			" trying to deauthenticate, but it is not authenticated",
7842 			MAC2STR(mgmt->sa));
7843 		return;
7844 	}
7845 
7846 	if (hostapd_ml_handle_disconnect(hapd, sta, mgmt, false))
7847 		return;
7848 
7849 	hostapd_deauth_sta(hapd, sta, mgmt);
7850 }
7851 
7852 
handle_beacon(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,struct hostapd_frame_info * fi)7853 static void handle_beacon(struct hostapd_data *hapd,
7854 			  const struct ieee80211_mgmt *mgmt, size_t len,
7855 			  struct hostapd_frame_info *fi)
7856 {
7857 	struct ieee802_11_elems elems;
7858 
7859 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
7860 		wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
7861 			   (unsigned long) len);
7862 		return;
7863 	}
7864 
7865 	(void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
7866 				      len - (IEEE80211_HDRLEN +
7867 					     sizeof(mgmt->u.beacon)), &elems,
7868 				      0);
7869 
7870 	ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
7871 }
7872 
7873 
hostapd_action_vs(struct hostapd_data * hapd,struct sta_info * sta,const struct ieee80211_mgmt * mgmt,size_t len,unsigned int freq,bool protected)7874 static int hostapd_action_vs(struct hostapd_data *hapd,
7875 			     struct sta_info *sta,
7876 			     const struct ieee80211_mgmt *mgmt, size_t len,
7877 			     unsigned int freq, bool protected)
7878 {
7879 	const u8 *pos, *end;
7880 	u32 oui_type;
7881 
7882 	pos = (const u8 *) &mgmt->u.action;
7883 	end = ((const u8 *) mgmt) + len;
7884 
7885 	if (end - pos < 1 + 4)
7886 		return -1;
7887 	pos++;
7888 
7889 	oui_type = WPA_GET_BE32(pos);
7890 	pos += 4;
7891 
7892 	switch (oui_type) {
7893 	case WFA_CAPAB_VENDOR_TYPE:
7894 		hostapd_wfa_capab(hapd, sta, pos, end);
7895 		return 0;
7896 	default:
7897 		wpa_printf(MSG_DEBUG,
7898 			   "Ignore unknown Vendor Specific Action frame OUI/type %08x%s",
7899 			   oui_type, protected ? " (protected)" : "");
7900 		break;
7901 	}
7902 
7903 	return -1;
7904 }
7905 
7906 
robust_action_frame(u8 category)7907 static int robust_action_frame(u8 category)
7908 {
7909 	return category != WLAN_ACTION_PUBLIC &&
7910 		category != WLAN_ACTION_HT &&
7911 		category != WLAN_ACTION_UNPROTECTED_WNM &&
7912 		category != WLAN_ACTION_SELF_PROTECTED &&
7913 		category != WLAN_ACTION_UNPROTECTED_DMG &&
7914 		category != WLAN_ACTION_VHT &&
7915 		category != WLAN_ACTION_UNPROTECTED_S1G &&
7916 		category != WLAN_ACTION_HE &&
7917 		category != WLAN_ACTION_EHT &&
7918 		category != WLAN_ACTION_VENDOR_SPECIFIC;
7919 }
7920 
7921 
handle_action(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,unsigned int freq)7922 static int handle_action(struct hostapd_data *hapd,
7923 			 const struct ieee80211_mgmt *mgmt, size_t len,
7924 			 unsigned int freq)
7925 {
7926 	struct sta_info *sta;
7927 	u8 *action __maybe_unused;
7928 
7929 	if (len < IEEE80211_HDRLEN + 2 + 1) {
7930 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
7931 			       HOSTAPD_LEVEL_DEBUG,
7932 			       "handle_action - too short payload (len=%lu)",
7933 			       (unsigned long) len);
7934 		return 0;
7935 	}
7936 
7937 	action = (u8 *) &mgmt->u.action.u;
7938 	wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
7939 		   " da " MACSTR " len %d freq %u",
7940 		   mgmt->u.action.category, *action,
7941 		   MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) len, freq);
7942 
7943 	sta = ap_get_sta(hapd, mgmt->sa);
7944 
7945 	if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
7946 	    (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
7947 		wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
7948 			   "frame (category=%u) from unassociated STA " MACSTR,
7949 			   mgmt->u.action.category, MAC2STR(mgmt->sa));
7950 		return 0;
7951 	}
7952 
7953 	if (sta && (sta->flags & WLAN_STA_MFP) &&
7954 	    !(mgmt->frame_control & host_to_le16(WLAN_FC_PROTECTED)) &&
7955 	    robust_action_frame(mgmt->u.action.category)) {
7956 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
7957 			       HOSTAPD_LEVEL_DEBUG,
7958 			       "Dropped unprotected Robust Action frame from "
7959 			       "an MFP STA");
7960 		return 0;
7961 	}
7962 
7963 	if (sta) {
7964 		u16 fc = le_to_host16(mgmt->frame_control);
7965 		u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
7966 
7967 		if ((fc & WLAN_FC_RETRY) &&
7968 		    sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
7969 		    sta->last_seq_ctrl == seq_ctrl &&
7970 		    sta->last_subtype == WLAN_FC_STYPE_ACTION) {
7971 			hostapd_logger(hapd, sta->addr,
7972 				       HOSTAPD_MODULE_IEEE80211,
7973 				       HOSTAPD_LEVEL_DEBUG,
7974 				       "Drop repeated action frame seq_ctrl=0x%x",
7975 				       seq_ctrl);
7976 			return 1;
7977 		}
7978 
7979 		sta->last_seq_ctrl = seq_ctrl;
7980 		sta->last_subtype = WLAN_FC_STYPE_ACTION;
7981 	}
7982 
7983 	switch (mgmt->u.action.category) {
7984 #ifdef CONFIG_IEEE80211R_AP
7985 	case WLAN_ACTION_FT:
7986 		if (!sta ||
7987 		    wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
7988 				     len - IEEE80211_HDRLEN))
7989 			break;
7990 		return 1;
7991 #endif /* CONFIG_IEEE80211R_AP */
7992 	case WLAN_ACTION_WMM:
7993 		hostapd_wmm_action(hapd, mgmt, len);
7994 		return 1;
7995 	case WLAN_ACTION_SA_QUERY:
7996 		ieee802_11_sa_query_action(hapd, mgmt, len);
7997 		return 1;
7998 #ifdef CONFIG_WNM_AP
7999 	case WLAN_ACTION_WNM:
8000 		ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
8001 		return 1;
8002 #endif /* CONFIG_WNM_AP */
8003 #ifdef CONFIG_FST
8004 	case WLAN_ACTION_FST:
8005 		if (hapd->iface->fst)
8006 			fst_rx_action(hapd->iface->fst, mgmt, len);
8007 		else
8008 			wpa_printf(MSG_DEBUG,
8009 				   "FST: Ignore FST Action frame - no FST attached");
8010 		return 1;
8011 #endif /* CONFIG_FST */
8012 	case WLAN_ACTION_PUBLIC:
8013 	case WLAN_ACTION_PROTECTED_DUAL:
8014 		if (len >= IEEE80211_HDRLEN + 2 &&
8015 		    mgmt->u.action.u.public_action.action ==
8016 		    WLAN_PA_20_40_BSS_COEX) {
8017 			hostapd_2040_coex_action(hapd, mgmt, len);
8018 			return 1;
8019 		}
8020 #ifdef CONFIG_DPP
8021 		if (len >= IEEE80211_HDRLEN + 6 &&
8022 		    mgmt->u.action.u.vs_public_action.action ==
8023 		    WLAN_PA_VENDOR_SPECIFIC &&
8024 		    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
8025 		    OUI_WFA &&
8026 		    mgmt->u.action.u.vs_public_action.variable[0] ==
8027 		    DPP_OUI_TYPE) {
8028 			const u8 *pos, *end;
8029 
8030 			pos = mgmt->u.action.u.vs_public_action.oui;
8031 			end = ((const u8 *) mgmt) + len;
8032 			hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
8033 					      freq);
8034 			return 1;
8035 		}
8036 		if (len >= IEEE80211_HDRLEN + 2 &&
8037 		    (mgmt->u.action.u.public_action.action ==
8038 		     WLAN_PA_GAS_INITIAL_RESP ||
8039 		     mgmt->u.action.u.public_action.action ==
8040 		     WLAN_PA_GAS_COMEBACK_RESP)) {
8041 			const u8 *pos, *end;
8042 
8043 			pos = &mgmt->u.action.u.public_action.action;
8044 			end = ((const u8 *) mgmt) + len;
8045 			if (gas_query_ap_rx(hapd->gas, mgmt->sa,
8046 					    mgmt->u.action.category,
8047 					    pos, end - pos, freq) == 0)
8048 				return 1;
8049 		}
8050 #endif /* CONFIG_DPP */
8051 #ifdef CONFIG_NAN_USD
8052 		if (mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
8053 		    len >= IEEE80211_HDRLEN + 5 &&
8054 		    mgmt->u.action.u.vs_public_action.action ==
8055 		    WLAN_PA_VENDOR_SPECIFIC &&
8056 		    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
8057 		    OUI_WFA &&
8058 		    mgmt->u.action.u.vs_public_action.variable[0] ==
8059 		    NAN_SDF_OUI_TYPE) {
8060 			const u8 *pos, *end;
8061 
8062 			pos = mgmt->u.action.u.vs_public_action.variable;
8063 			end = ((const u8 *) mgmt) + len;
8064 			pos++;
8065 			hostapd_nan_usd_rx_sdf(hapd, mgmt->sa, mgmt->bssid,
8066 					       freq, pos, end - pos);
8067 			return 1;
8068 		}
8069 #endif /* CONFIG_NAN_USD */
8070 		if (hapd->public_action_cb) {
8071 			hapd->public_action_cb(hapd->public_action_cb_ctx,
8072 					       (u8 *) mgmt, len, freq);
8073 		}
8074 		if (hapd->public_action_cb2) {
8075 			hapd->public_action_cb2(hapd->public_action_cb2_ctx,
8076 						(u8 *) mgmt, len, freq);
8077 		}
8078 		if (hapd->public_action_cb || hapd->public_action_cb2)
8079 			return 1;
8080 		break;
8081 	case WLAN_ACTION_VENDOR_SPECIFIC:
8082 		if (hapd->vendor_action_cb) {
8083 			if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
8084 						   (u8 *) mgmt, len, freq) == 0)
8085 				return 1;
8086 		}
8087 		if (sta &&
8088 		    hostapd_action_vs(hapd, sta, mgmt, len, freq, false) == 0)
8089 			return 1;
8090 		break;
8091 	case WLAN_ACTION_VENDOR_SPECIFIC_PROTECTED:
8092 		if (sta &&
8093 		    hostapd_action_vs(hapd, sta, mgmt, len, freq, true) == 0)
8094 			return 1;
8095 		break;
8096 #ifndef CONFIG_NO_RRM
8097 	case WLAN_ACTION_RADIO_MEASUREMENT:
8098 		hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
8099 		return 1;
8100 #endif /* CONFIG_NO_RRM */
8101 #ifdef CONFIG_IEEE80211BE
8102 	case WLAN_ACTION_PROTECTED_EHT:
8103 		ieee802_11_rx_protected_eht_action(hapd, mgmt, len);
8104 		return 1;
8105 #endif /* CONFIG_IEEE80211BE */
8106 	}
8107 
8108 	hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8109 		       HOSTAPD_LEVEL_DEBUG,
8110 		       "handle_action - unknown action category %d or invalid "
8111 		       "frame",
8112 		       mgmt->u.action.category);
8113 
8114 	return 1;
8115 }
8116 
8117 
8118 /**
8119  * notify_mgmt_frame - Notify of Management frames on the control interface
8120  * @hapd: hostapd BSS data structure (the BSS to which the Management frame was
8121  * sent to)
8122  * @buf: Management frame data (starting from the IEEE 802.11 header)
8123  * @len: Length of frame data in octets
8124  *
8125  * Notify the control interface of any received Management frame.
8126  */
notify_mgmt_frame(struct hostapd_data * hapd,const u8 * buf,size_t len)8127 static void notify_mgmt_frame(struct hostapd_data *hapd, const u8 *buf,
8128 			      size_t len)
8129 {
8130 
8131 	int hex_len = len * 2 + 1;
8132 	char *hex = os_malloc(hex_len);
8133 
8134 	if (hex) {
8135 		wpa_snprintf_hex(hex, hex_len, buf, len);
8136 		wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO,
8137 			     AP_MGMT_FRAME_RECEIVED "buf=%s", hex);
8138 		os_free(hex);
8139 	}
8140 }
8141 
8142 
8143 /**
8144  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
8145  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
8146  * sent to)
8147  * @buf: management frame data (starting from IEEE 802.11 header)
8148  * @len: length of frame data in octets
8149  * @fi: meta data about received frame (signal level, etc.)
8150  *
8151  * Process all incoming IEEE 802.11 management frames. This will be called for
8152  * each frame received from the kernel driver through wlan#ap interface. In
8153  * addition, it can be called to re-inserted pending frames (e.g., when using
8154  * external RADIUS server as an MAC ACL).
8155  */
ieee802_11_mgmt(struct hostapd_data * hapd,const u8 * buf,size_t len,struct hostapd_frame_info * fi)8156 int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
8157 		    struct hostapd_frame_info *fi)
8158 {
8159 	struct ieee80211_mgmt *mgmt;
8160 	u16 fc, stype;
8161 	int ret = 0;
8162 	unsigned int freq;
8163 	int ssi_signal = fi ? fi->ssi_signal : 0;
8164 
8165 	if (len < 24)
8166 		return 0;
8167 
8168 	if (fi && fi->freq)
8169 		freq = fi->freq;
8170 	else
8171 		freq = hapd->iface->freq;
8172 
8173 	mgmt = (struct ieee80211_mgmt *) buf;
8174 	fc = le_to_host16(mgmt->frame_control);
8175 	stype = WLAN_FC_GET_STYPE(fc);
8176 
8177 	if (is_multicast_ether_addr(mgmt->sa) ||
8178 	    is_zero_ether_addr(mgmt->sa) ||
8179 	    ether_addr_equal(mgmt->sa, hapd->own_addr)) {
8180 		/* Do not process any frames with unexpected/invalid SA so that
8181 		 * we do not add any state for unexpected STA addresses or end
8182 		 * up sending out frames to unexpected destination. */
8183 		wpa_printf(MSG_DEBUG, "MGMT: Invalid SA=" MACSTR
8184 			   " in received frame - ignore this frame silently",
8185 			   MAC2STR(mgmt->sa));
8186 		return 0;
8187 	}
8188 
8189 	if (stype == WLAN_FC_STYPE_BEACON) {
8190 		handle_beacon(hapd, mgmt, len, fi);
8191 		return 1;
8192 	}
8193 
8194 	if (!is_broadcast_ether_addr(mgmt->bssid) &&
8195 #ifdef CONFIG_NAN_USD
8196 	    !nan_de_is_nan_network_id(mgmt->bssid) &&
8197 	    !nan_de_is_p2p_network_id(mgmt->bssid) &&
8198 #endif /* CONFIG_NAN_USD */
8199 #ifdef CONFIG_P2P
8200 	    /* Invitation responses can be sent with the peer MAC as BSSID */
8201 	    !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
8202 	      stype == WLAN_FC_STYPE_ACTION) &&
8203 #endif /* CONFIG_P2P */
8204 #ifdef CONFIG_MESH
8205 	    !(hapd->conf->mesh & MESH_ENABLED) &&
8206 #endif /* CONFIG_MESH */
8207 #ifdef CONFIG_IEEE80211BE
8208 	    !(hapd->conf->mld_ap &&
8209 	      ether_addr_equal(hapd->mld->mld_addr, mgmt->bssid)) &&
8210 #endif /* CONFIG_IEEE80211BE */
8211 	    !ether_addr_equal(mgmt->bssid, hapd->own_addr)) {
8212 		wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
8213 			   MAC2STR(mgmt->bssid));
8214 		return 0;
8215 	}
8216 
8217 	if (hapd->iface->state != HAPD_IFACE_ENABLED) {
8218 		wpa_printf(MSG_DEBUG, "MGMT: Ignore management frame while interface is not enabled (SA=" MACSTR " DA=" MACSTR " subtype=%u)",
8219 			   MAC2STR(mgmt->sa), MAC2STR(mgmt->da), stype);
8220 		return 1;
8221 	}
8222 
8223 	if (stype == WLAN_FC_STYPE_PROBE_REQ) {
8224 		handle_probe_req(hapd, mgmt, len, ssi_signal);
8225 		return 1;
8226 	}
8227 
8228 	if ((!is_broadcast_ether_addr(mgmt->da) ||
8229 	     stype != WLAN_FC_STYPE_ACTION) &&
8230 #ifdef CONFIG_IEEE80211BE
8231 	    !(hapd->conf->mld_ap &&
8232 	      ether_addr_equal(hapd->mld->mld_addr, mgmt->bssid)) &&
8233 #endif /* CONFIG_IEEE80211BE */
8234 #ifdef CONFIG_NAN_USD
8235 	    !ether_addr_equal(mgmt->da, nan_network_id) &&
8236 	    !ether_addr_equal(mgmt->da, p2p_network_id) &&
8237 #endif /* CONFIG_NAN_USD */
8238 	    !ether_addr_equal(mgmt->da, hapd->own_addr)) {
8239 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8240 			       HOSTAPD_LEVEL_DEBUG,
8241 			       "MGMT: DA=" MACSTR " not our address",
8242 			       MAC2STR(mgmt->da));
8243 		return 0;
8244 	}
8245 
8246 	if (hapd->iconf->track_sta_max_num)
8247 		sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
8248 
8249 	if (hapd->conf->notify_mgmt_frames)
8250 		notify_mgmt_frame(hapd, buf, len);
8251 
8252 	switch (stype) {
8253 	case WLAN_FC_STYPE_AUTH:
8254 		wpa_printf(MSG_DEBUG, "mgmt::auth");
8255 		handle_auth(hapd, mgmt, len, ssi_signal, 0);
8256 		ret = 1;
8257 		break;
8258 	case WLAN_FC_STYPE_ASSOC_REQ:
8259 		wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
8260 		handle_assoc(hapd, mgmt, len, 0, ssi_signal);
8261 		ret = 1;
8262 		break;
8263 	case WLAN_FC_STYPE_REASSOC_REQ:
8264 		wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
8265 		handle_assoc(hapd, mgmt, len, 1, ssi_signal);
8266 		ret = 1;
8267 		break;
8268 	case WLAN_FC_STYPE_DISASSOC:
8269 		wpa_printf(MSG_DEBUG, "mgmt::disassoc");
8270 		handle_disassoc(hapd, mgmt, len);
8271 		ret = 1;
8272 		break;
8273 	case WLAN_FC_STYPE_DEAUTH:
8274 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
8275 		handle_deauth(hapd, mgmt, len);
8276 		ret = 1;
8277 		break;
8278 	case WLAN_FC_STYPE_ACTION:
8279 		wpa_printf(MSG_DEBUG, "mgmt::action");
8280 		ret = handle_action(hapd, mgmt, len, freq);
8281 		break;
8282 	default:
8283 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8284 			       HOSTAPD_LEVEL_DEBUG,
8285 			       "unknown mgmt frame subtype %d", stype);
8286 		break;
8287 	}
8288 
8289 	return ret;
8290 }
8291 
8292 
handle_auth_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)8293 static void handle_auth_cb(struct hostapd_data *hapd,
8294 			   const struct ieee80211_mgmt *mgmt,
8295 			   size_t len, int ok)
8296 {
8297 	u16 auth_alg, auth_transaction, status_code;
8298 	struct sta_info *sta;
8299 	bool success_status;
8300 
8301 	sta = ap_get_sta(hapd, mgmt->da);
8302 	if (!sta) {
8303 		wpa_printf(MSG_DEBUG, "handle_auth_cb: STA " MACSTR
8304 			   " not found",
8305 			   MAC2STR(mgmt->da));
8306 		return;
8307 	}
8308 
8309 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
8310 		wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
8311 			   (unsigned long) len);
8312 		auth_alg = 0;
8313 		auth_transaction = 0;
8314 		status_code = WLAN_STATUS_UNSPECIFIED_FAILURE;
8315 		goto fail;
8316 	}
8317 
8318 	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
8319 	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
8320 	status_code = le_to_host16(mgmt->u.auth.status_code);
8321 
8322 	if (!ok) {
8323 		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
8324 			       HOSTAPD_LEVEL_NOTICE,
8325 			       "did not acknowledge authentication response");
8326 		goto fail;
8327 	}
8328 
8329 	if (status_code == WLAN_STATUS_SUCCESS &&
8330 	    ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
8331 	     (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
8332 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
8333 			       HOSTAPD_LEVEL_INFO, "authenticated");
8334 		sta->flags |= WLAN_STA_AUTH;
8335 		if (sta->added_unassoc)
8336 			hostapd_set_sta_flags(hapd, sta);
8337 		return;
8338 	}
8339 
8340 fail:
8341 	success_status = status_code == WLAN_STATUS_SUCCESS;
8342 #ifdef CONFIG_SAE
8343 	if (auth_alg == WLAN_AUTH_SAE &&
8344 	    auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT)
8345 		success_status = sae_status_success(hapd, status_code);
8346 #endif /* CONFIG_SAE */
8347 #ifdef CONFIG_IEEE8021X_AUTH
8348 	if (auth_alg == WLAN_AUTH_802_1X &&
8349 	    status_code == WLAN_STATUS_802_1_X_AUTH_SUCCESS) {
8350 		sta->flags |= WLAN_STA_AUTH;
8351 		sta->auth_alg = WLAN_AUTH_802_1X;
8352 		success_status = true;
8353 	}
8354 #endif  /* CONFIG_IEEE8021X_AUTH */
8355 	if (!success_status && sta->added_unassoc) {
8356 		hostapd_drv_sta_remove(hapd, sta->addr);
8357 		sta->added_unassoc = 0;
8358 	}
8359 }
8360 
8361 
hostapd_set_wds_encryption(struct hostapd_data * hapd,struct sta_info * sta,char * ifname_wds)8362 static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
8363 				       struct sta_info *sta,
8364 				       char *ifname_wds)
8365 {
8366 #ifdef CONFIG_WEP
8367 	int i;
8368 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
8369 
8370 	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
8371 		return;
8372 
8373 	for (i = 0; i < 4; i++) {
8374 		if (ssid->wep.key[i] &&
8375 		    hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
8376 					0, i == ssid->wep.idx, NULL, 0,
8377 					ssid->wep.key[i], ssid->wep.len[i],
8378 					i == ssid->wep.idx ?
8379 					KEY_FLAG_GROUP_RX_TX_DEFAULT :
8380 					KEY_FLAG_GROUP_RX_TX)) {
8381 			wpa_printf(MSG_WARNING,
8382 				   "Could not set WEP keys for WDS interface; %s",
8383 				   ifname_wds);
8384 			break;
8385 		}
8386 	}
8387 #endif /* CONFIG_WEP */
8388 }
8389 
8390 
8391 #ifdef CONFIG_IEEE80211BE
ieee80211_ml_link_sta_assoc_cb(struct hostapd_data * hapd,struct sta_info * sta,struct mld_link_info * link,bool ok)8392 static void ieee80211_ml_link_sta_assoc_cb(struct hostapd_data *hapd,
8393 					   struct sta_info *sta,
8394 					   struct mld_link_info *link,
8395 					   bool ok)
8396 {
8397 	bool updated = false;
8398 
8399 	if (!ok) {
8400 		hostapd_logger(hapd, link->peer_addr, HOSTAPD_MODULE_IEEE80211,
8401 			       HOSTAPD_LEVEL_DEBUG,
8402 			       "did not acknowledge association response");
8403 		sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
8404 
8405 		/* The STA is added only in case of SUCCESS */
8406 		if (link->status == WLAN_STATUS_SUCCESS)
8407 			hostapd_drv_sta_remove(hapd, sta->addr);
8408 
8409 		return;
8410 	}
8411 
8412 	if (link->status != WLAN_STATUS_SUCCESS)
8413 		return;
8414 
8415 	sta->flags |= WLAN_STA_ASSOC;
8416 	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
8417 
8418 	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
8419 		updated = ap_sta_set_authorized_flag(hapd, sta, 1);
8420 
8421 	hostapd_set_sta_flags(hapd, sta);
8422 	if (updated)
8423 		ap_sta_set_authorized_event(hapd, sta, 1);
8424 
8425 	/*
8426 	 * TODOs:
8427 	 * - IEEE 802.1X port enablement is not needed as done on the station
8428 	 *     doing the connection.
8429 	 * - Not handling accounting
8430 	 * - Need to handle VLAN configuration
8431 	 */
8432 }
8433 #endif /* CONFIG_IEEE80211BE */
8434 
8435 
hostapd_ml_handle_assoc_cb(struct hostapd_data * hapd,struct sta_info * sta,bool ok)8436 static void hostapd_ml_handle_assoc_cb(struct hostapd_data *hapd,
8437 				       struct sta_info *sta, bool ok)
8438 {
8439 #ifdef CONFIG_IEEE80211BE
8440 	struct hostapd_data *tmp_hapd;
8441 
8442 	if (!hostapd_is_multiple_link_mld(hapd))
8443 		return;
8444 
8445 	for_each_mld_link(tmp_hapd, hapd) {
8446 		struct mld_link_info *link;
8447 		struct sta_info *tmp_sta;
8448 
8449 		if (tmp_hapd == hapd)
8450 			continue;
8451 
8452 		link = &sta->mld_info.links[tmp_hapd->mld_link_id];
8453 		if (!link->valid)
8454 			continue;
8455 
8456 		for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
8457 		     tmp_sta = tmp_sta->next) {
8458 			if (tmp_sta == sta ||
8459 			    tmp_sta->mld_assoc_link_id !=
8460 			    sta->mld_assoc_link_id ||
8461 			    tmp_sta->aid != sta->aid)
8462 				continue;
8463 
8464 			ieee80211_ml_link_sta_assoc_cb(tmp_hapd, tmp_sta, link,
8465 						       ok);
8466 			break;
8467 		}
8468 	}
8469 #endif /* CONFIG_IEEE80211BE */
8470 }
8471 
8472 
handle_assoc_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int reassoc,int ok)8473 static void handle_assoc_cb(struct hostapd_data *hapd,
8474 			    const struct ieee80211_mgmt *mgmt,
8475 			    size_t len, int reassoc, int ok)
8476 {
8477 	u16 status;
8478 	struct sta_info *sta;
8479 	int new_assoc = 1;
8480 
8481 	sta = ap_get_sta(hapd, mgmt->da);
8482 	if (!sta) {
8483 		wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
8484 			   MAC2STR(mgmt->da));
8485 		return;
8486 	}
8487 
8488 #ifdef CONFIG_IEEE80211BE
8489 	if (ap_sta_is_mld(hapd, sta) &&
8490 	    hapd->mld_link_id != sta->mld_assoc_link_id) {
8491 		/* See ieee80211_ml_link_sta_assoc_cb() for the MLD case */
8492 		wpa_printf(MSG_DEBUG,
8493 			   "%s: MLD: ignore on link station (%d != %d)",
8494 			   __func__, hapd->mld_link_id, sta->mld_assoc_link_id);
8495 		return;
8496 	}
8497 #endif /* CONFIG_IEEE80211BE */
8498 
8499 	if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
8500 				      sizeof(mgmt->u.assoc_resp))) {
8501 		wpa_printf(MSG_INFO,
8502 			   "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
8503 			   reassoc, (unsigned long) len);
8504 		hostapd_drv_sta_remove(hapd, sta->addr);
8505 		return;
8506 	}
8507 
8508 	if (reassoc)
8509 		status = le_to_host16(mgmt->u.reassoc_resp.status_code);
8510 	else
8511 		status = le_to_host16(mgmt->u.assoc_resp.status_code);
8512 
8513 	if (!ok) {
8514 		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
8515 			       HOSTAPD_LEVEL_DEBUG,
8516 			       "did not acknowledge association response");
8517 		sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
8518 		/* The STA is added only in case of SUCCESS */
8519 		if (status == WLAN_STATUS_SUCCESS)
8520 			hostapd_drv_sta_remove(hapd, sta->addr);
8521 
8522 		goto handle_ml;
8523 	}
8524 
8525 	if (status != WLAN_STATUS_SUCCESS)
8526 		goto handle_ml;
8527 
8528 	/* Stop previous accounting session, if one is started, and allocate
8529 	 * new session id for the new session. */
8530 	accounting_sta_stop(hapd, sta);
8531 
8532 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
8533 		       HOSTAPD_LEVEL_INFO,
8534 		       "associated (aid %d)",
8535 		       sta->aid);
8536 
8537 	if (sta->flags & WLAN_STA_ASSOC)
8538 		new_assoc = 0;
8539 	sta->flags |= WLAN_STA_ASSOC;
8540 	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
8541 	if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
8542 	    sta->auth_alg == WLAN_AUTH_FILS_SK ||
8543 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
8544 	    sta->auth_alg == WLAN_AUTH_FILS_PK ||
8545 	    sta->auth_alg == WLAN_AUTH_FT ||
8546 	    sta->auth_alg == WLAN_AUTH_EPPKE ||
8547 	    sta->auth_alg == WLAN_AUTH_802_1X) {
8548 		/*
8549 		 * Open, static WEP, FT protocol, or FILS; no separate
8550 		 * authorization step.
8551 		 */
8552 		ap_sta_set_authorized(hapd, sta, 1);
8553 	}
8554 
8555 	if (reassoc)
8556 		mlme_reassociate_indication(hapd, sta);
8557 	else
8558 		mlme_associate_indication(hapd, sta);
8559 
8560 	ap_sta_set_sa_query_timeout(hapd, sta, 0);
8561 
8562 #ifdef CONFIG_PMKSA_PRIVACY
8563 	if (ok && status == WLAN_STATUS_SUCCESS && sta->epp_sta &&
8564 	    wpa_auth_ap_sta_support_pmkid_privacy(sta->wpa_sm)) {
8565 		bool is_ml = ap_sta_is_mld(hapd, sta);
8566 		struct rsn_pmksa_cache_entry *entry, *next;
8567 		struct rsn_pmksa_cache *pmksa, *t_pmksa;
8568 		const u8 *pmkid_cur, *pmkid_next;
8569 
8570 		switch (sta->auth_alg) {
8571 		case WLAN_AUTH_EPPKE:
8572 			if (!sta->pasn) {
8573 				wpa_printf(MSG_INFO, "EPP: Missing PASN data");
8574 				goto skip_update;
8575 			}
8576 			pmkid_cur = sta->pasn->epp_pmkid_cur;
8577 			pmkid_next = sta->epp_pmkid_next;
8578 			break;
8579 #ifdef CONFIG_IEEE8021X_AUTH
8580 		case WLAN_AUTH_802_1X:
8581 			pmkid_cur = sta->eap_auth_data.epp_pmkid_cur;
8582 			pmkid_next = sta->eap_auth_data.epp_pmkid_next;
8583 			break;
8584 #endif /* CONFIG_IEEE8021X_AUTH */
8585 		default:
8586 			wpa_printf(MSG_INFO,
8587 				   "EPP: Unsupported auth alg %u for PMKID privacy support",
8588 				   sta->auth_alg);
8589 			goto skip_update;
8590 		}
8591 
8592 		pmksa = t_pmksa = wpa_auth_get_pmksa_cache(hapd->wpa_auth,
8593 							   is_ml);
8594 
8595 		entry = pmksa_cache_auth_get(t_pmksa, NULL, pmkid_cur);
8596 		if (entry)
8597 			goto update_pmksa_entry;
8598 
8599 #ifdef CONFIG_IEEE80211BE
8600 		if (!entry && is_ml) {
8601 			struct hostapd_data *tmp_hapd;
8602 
8603 			/* Search in link caches of each AP MLD link */
8604 			for_each_mld_link(tmp_hapd, hapd) {
8605 				t_pmksa = wpa_auth_get_pmksa_cache(
8606 					tmp_hapd->wpa_auth, false);
8607 				entry = pmksa_cache_auth_get(t_pmksa, NULL,
8608 							     pmkid_cur);
8609 				if (entry)
8610 					break;
8611 			}
8612 		} else if (!entry && !is_ml && hapd->conf->mld_ap) {
8613 			/* Search in the MLD cache */
8614 			t_pmksa = wpa_auth_get_pmksa_cache(hapd->wpa_auth,
8615 							   true);
8616 			entry = pmksa_cache_auth_get(t_pmksa, NULL, pmkid_cur);
8617 		}
8618 #endif /* CONFIG_IEEE80211BE */
8619 
8620 update_pmksa_entry:
8621 		if (entry) {
8622 			wpa_printf(MSG_DEBUG,
8623 				   "EPP: PMKSA caching privacy on - update PMKSA cache entry");
8624 			next = os_memdup(entry, sizeof(*entry));
8625 			if (!next)
8626 				goto skip_update;
8627 			os_memcpy(next->pmkid, pmkid_next, PMKID_LEN);
8628 			os_memcpy(next->spa, sta->addr, ETH_ALEN);
8629 			next->vlan_desc = NULL;
8630 			next->identity = NULL;
8631 			next->dpp_pkhash = NULL;
8632 			next->cui = NULL;
8633 			pmksa_cache_from_eapol_data(next, sta->eapol_sm);
8634 			pmksa_cache_free_entry(t_pmksa, entry);
8635 			pmksa_cache_auth_add_entry(pmksa, next);
8636 		}
8637 	}
8638 skip_update:
8639 #endif /* CONFIG_PMKSA_PRIVACY */
8640 
8641 	if (sta->eapol_sm == NULL) {
8642 		/*
8643 		 * This STA does not use RADIUS server for EAP authentication,
8644 		 * so bind it to the selected VLAN interface now, since the
8645 		 * interface selection is not going to change anymore.
8646 		 */
8647 		if (ap_sta_bind_vlan(hapd, sta) < 0)
8648 			goto handle_ml;
8649 	} else if (sta->vlan_id) {
8650 		/* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
8651 		if (ap_sta_bind_vlan(hapd, sta) < 0)
8652 			goto handle_ml;
8653 	}
8654 
8655 	hostapd_set_sta_flags(hapd, sta);
8656 
8657 	if (!(sta->flags & WLAN_STA_WDS) && sta->pending_wds_enable) {
8658 		wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for STA "
8659 			   MACSTR " based on pending request",
8660 			   MAC2STR(sta->addr));
8661 		sta->pending_wds_enable = 0;
8662 		sta->flags |= WLAN_STA_WDS;
8663 	}
8664 
8665 	/* WPS not supported on backhaul BSS. Disable 4addr mode on fronthaul */
8666 	if ((sta->flags & WLAN_STA_WDS) ||
8667 	    (sta->flags & WLAN_STA_MULTI_AP &&
8668 	     (hapd->conf->multi_ap & BACKHAUL_BSS) &&
8669 	     hapd->conf->wds_sta &&
8670 	     !(sta->flags & WLAN_STA_WPS))) {
8671 		int ret;
8672 		char ifname_wds[IFNAMSIZ + 1];
8673 
8674 		wpa_printf(MSG_DEBUG, "Reenable 4-address WDS mode for STA "
8675 			   MACSTR " (aid %u)",
8676 			   MAC2STR(sta->addr), sta->aid);
8677 		ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
8678 					  sta->aid, 1);
8679 		if (!ret)
8680 			hostapd_set_wds_encryption(hapd, sta, ifname_wds);
8681 	}
8682 
8683 	if (sta->auth_alg == WLAN_AUTH_FT)
8684 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
8685 	else
8686 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
8687 	hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
8688 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
8689 
8690 #ifdef CONFIG_FILS
8691 	if ((sta->auth_alg == WLAN_AUTH_FILS_SK ||
8692 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
8693 	     sta->auth_alg == WLAN_AUTH_FILS_PK) &&
8694 	    fils_set_tk(sta->wpa_sm) < 0) {
8695 		wpa_printf(MSG_DEBUG, "FILS: TK configuration failed");
8696 		ap_sta_disconnect(hapd, sta, sta->addr,
8697 				  WLAN_REASON_UNSPECIFIED);
8698 		return;
8699 	}
8700 #endif /* CONFIG_FILS */
8701 
8702 	if (sta->pending_eapol_rx) {
8703 		struct os_reltime now, age;
8704 
8705 		os_get_reltime(&now);
8706 		os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
8707 		if (age.sec == 0 && age.usec < 200000) {
8708 			wpa_printf(MSG_DEBUG,
8709 				   "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
8710 				   MAC2STR(sta->addr));
8711 			ieee802_1x_receive(
8712 				hapd, mgmt->da,
8713 				wpabuf_head(sta->pending_eapol_rx->buf),
8714 				wpabuf_len(sta->pending_eapol_rx->buf),
8715 				sta->pending_eapol_rx->encrypted);
8716 		}
8717 		wpabuf_free(sta->pending_eapol_rx->buf);
8718 		os_free(sta->pending_eapol_rx);
8719 		sta->pending_eapol_rx = NULL;
8720 	}
8721 
8722 handle_ml:
8723 	hostapd_ml_handle_assoc_cb(hapd, sta, ok);
8724 }
8725 
8726 
handle_deauth_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)8727 static void handle_deauth_cb(struct hostapd_data *hapd,
8728 			     const struct ieee80211_mgmt *mgmt,
8729 			     size_t len, int ok)
8730 {
8731 	struct sta_info *sta;
8732 	if (is_multicast_ether_addr(mgmt->da))
8733 		return;
8734 	sta = ap_get_sta(hapd, mgmt->da);
8735 	if (!sta) {
8736 		wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
8737 			   " not found", MAC2STR(mgmt->da));
8738 		return;
8739 	}
8740 	if (ok)
8741 		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
8742 			   MAC2STR(sta->addr));
8743 	else
8744 		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
8745 			   "deauth", MAC2STR(sta->addr));
8746 
8747 	ap_sta_deauth_cb(hapd, sta);
8748 }
8749 
8750 
handle_disassoc_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)8751 static void handle_disassoc_cb(struct hostapd_data *hapd,
8752 			       const struct ieee80211_mgmt *mgmt,
8753 			       size_t len, int ok)
8754 {
8755 	struct sta_info *sta;
8756 	if (is_multicast_ether_addr(mgmt->da))
8757 		return;
8758 	sta = ap_get_sta(hapd, mgmt->da);
8759 	if (!sta) {
8760 		wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
8761 			   " not found", MAC2STR(mgmt->da));
8762 		return;
8763 	}
8764 	if (ok)
8765 		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
8766 			   MAC2STR(sta->addr));
8767 	else
8768 		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
8769 			   "disassoc", MAC2STR(sta->addr));
8770 
8771 	ap_sta_disassoc_cb(hapd, sta);
8772 }
8773 
8774 
handle_action_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)8775 static void handle_action_cb(struct hostapd_data *hapd,
8776 			     const struct ieee80211_mgmt *mgmt,
8777 			     size_t len, int ok)
8778 {
8779 	struct sta_info *sta;
8780 #ifndef CONFIG_NO_RRM
8781 	const struct rrm_measurement_report_element *report;
8782 #endif /* CONFIG_NO_RRM */
8783 
8784 #ifdef CONFIG_DPP
8785 	if (len >= IEEE80211_HDRLEN + 6 &&
8786 	    mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
8787 	    mgmt->u.action.u.vs_public_action.action ==
8788 	    WLAN_PA_VENDOR_SPECIFIC &&
8789 	    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
8790 	    OUI_WFA &&
8791 	    mgmt->u.action.u.vs_public_action.variable[0] ==
8792 	    DPP_OUI_TYPE) {
8793 		const u8 *pos, *end;
8794 
8795 		pos = &mgmt->u.action.u.vs_public_action.variable[1];
8796 		end = ((const u8 *) mgmt) + len;
8797 		hostapd_dpp_tx_status(hapd, mgmt->da, pos, end - pos, ok);
8798 		return;
8799 	}
8800 	if (len >= IEEE80211_HDRLEN + 2 &&
8801 	    mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
8802 	    (mgmt->u.action.u.public_action.action ==
8803 	     WLAN_PA_GAS_INITIAL_REQ ||
8804 	     mgmt->u.action.u.public_action.action ==
8805 	     WLAN_PA_GAS_COMEBACK_REQ)) {
8806 		const u8 *pos, *end;
8807 
8808 		pos = mgmt->u.action.u.public_action.variable;
8809 		end = ((const u8 *) mgmt) + len;
8810 		gas_query_ap_tx_status(hapd->gas, mgmt->da, pos, end - pos, ok);
8811 		return;
8812 	}
8813 #endif /* CONFIG_DPP */
8814 	if (is_multicast_ether_addr(mgmt->da))
8815 		return;
8816 	sta = ap_get_sta(hapd, mgmt->da);
8817 	if (!sta) {
8818 		wpa_printf(MSG_DEBUG, "handle_action_cb: STA " MACSTR
8819 			   " not found", MAC2STR(mgmt->da));
8820 		return;
8821 	}
8822 
8823 #ifdef CONFIG_HS20
8824 	if (ok && len >= IEEE80211_HDRLEN + 2 &&
8825 	    mgmt->u.action.category == WLAN_ACTION_WNM &&
8826 	    mgmt->u.action.u.vs_public_action.action == WNM_NOTIFICATION_REQ &&
8827 	    sta->hs20_deauth_on_ack) {
8828 		wpa_printf(MSG_DEBUG, "HS 2.0: Deauthenticate STA " MACSTR
8829 			   " on acknowledging the WNM-Notification",
8830 			   MAC2STR(sta->addr));
8831 		ap_sta_session_timeout(hapd, sta, 0);
8832 		return;
8833 	}
8834 #endif /* CONFIG_HS20 */
8835 
8836 #ifdef CONFIG_IEEE80211BE
8837 	/* Frame header (24B) + Category (1B) + Action code (1B) +
8838 	 * Dialog token (1B) + Count (1B) + Status list (count * 3B)
8839 	 */
8840 	if (len >= IEEE80211_HDRLEN + 3 + 1 + 3 &&
8841 	    mgmt->u.action.category == WLAN_ACTION_PROTECTED_EHT &&
8842 	    mgmt->u.action.u.link_reconf_resp.action ==
8843 	    WLAN_PROT_EHT_LINK_RECONFIG_RESPONSE) {
8844 		hostapd_link_reconf_resp_tx_status(hapd, sta, mgmt, len, ok);
8845 		return;
8846 	}
8847 #endif /* CONFIG_IEEE80211BE */
8848 
8849 #ifndef CONFIG_NO_RRM
8850 	if (len < 24 + 5 + sizeof(*report))
8851 		return;
8852 	report = (const struct rrm_measurement_report_element *)
8853 		&mgmt->u.action.u.rrm.variable[2];
8854 	if (mgmt->u.action.category == WLAN_ACTION_RADIO_MEASUREMENT &&
8855 	    mgmt->u.action.u.rrm.action == WLAN_RRM_RADIO_MEASUREMENT_REQUEST &&
8856 	    report->eid == WLAN_EID_MEASURE_REQUEST &&
8857 	    report->len >= 3 &&
8858 	    report->type == MEASURE_TYPE_BEACON)
8859 		hostapd_rrm_beacon_req_tx_status(hapd, mgmt, len, ok);
8860 #endif /* CONFIG_NO_RRM */
8861 }
8862 
8863 
8864 /**
8865  * ieee802_11_mgmt_cb - Process management frame TX status callback
8866  * @hapd: hostapd BSS data structure (the BSS from which the management frame
8867  * was sent from)
8868  * @buf: management frame data (starting from IEEE 802.11 header)
8869  * @len: length of frame data in octets
8870  * @stype: management frame subtype from frame control field
8871  * @ok: Whether the frame was ACK'ed
8872  */
ieee802_11_mgmt_cb(struct hostapd_data * hapd,const u8 * buf,size_t len,u16 stype,int ok)8873 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
8874 			u16 stype, int ok)
8875 {
8876 	const struct ieee80211_mgmt *mgmt;
8877 	mgmt = (const struct ieee80211_mgmt *) buf;
8878 
8879 #ifdef CONFIG_TESTING_OPTIONS
8880 	if (hapd->ext_mgmt_frame_handling) {
8881 		size_t hex_len = 2 * len + 1;
8882 		char *hex = os_malloc(hex_len);
8883 
8884 		if (hex) {
8885 			wpa_snprintf_hex(hex, hex_len, buf, len);
8886 			wpa_msg(hapd->msg_ctx, MSG_INFO,
8887 				"MGMT-TX-STATUS stype=%u ok=%d buf=%s",
8888 				stype, ok, hex);
8889 			os_free(hex);
8890 		}
8891 		return;
8892 	}
8893 #endif /* CONFIG_TESTING_OPTIONS */
8894 
8895 	switch (stype) {
8896 	case WLAN_FC_STYPE_AUTH:
8897 		wpa_printf(MSG_DEBUG, "mgmt::auth cb");
8898 		handle_auth_cb(hapd, mgmt, len, ok);
8899 		break;
8900 	case WLAN_FC_STYPE_ASSOC_RESP:
8901 		wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
8902 		handle_assoc_cb(hapd, mgmt, len, 0, ok);
8903 		break;
8904 	case WLAN_FC_STYPE_REASSOC_RESP:
8905 		wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
8906 		handle_assoc_cb(hapd, mgmt, len, 1, ok);
8907 		break;
8908 	case WLAN_FC_STYPE_PROBE_RESP:
8909 		wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
8910 		break;
8911 	case WLAN_FC_STYPE_DEAUTH:
8912 		wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
8913 		handle_deauth_cb(hapd, mgmt, len, ok);
8914 		break;
8915 	case WLAN_FC_STYPE_DISASSOC:
8916 		wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
8917 		handle_disassoc_cb(hapd, mgmt, len, ok);
8918 		break;
8919 	case WLAN_FC_STYPE_ACTION:
8920 		wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
8921 		handle_action_cb(hapd, mgmt, len, ok);
8922 		break;
8923 	default:
8924 		wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
8925 		break;
8926 	}
8927 }
8928 
8929 
ieee802_11_get_mib(struct hostapd_data * hapd,char * buf,size_t buflen)8930 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
8931 {
8932 	/* TODO */
8933 	return 0;
8934 }
8935 
8936 
ieee802_11_get_mib_sta(struct hostapd_data * hapd,struct sta_info * sta,char * buf,size_t buflen)8937 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
8938 			   char *buf, size_t buflen)
8939 {
8940 	int len = 0, ret;
8941 
8942 	ret = os_snprintf(buf + len, buflen - len,
8943 			  "auth_alg=%d\n",
8944 			  sta->auth_alg);
8945 	if (os_snprintf_error(buflen - len, ret))
8946 		return len;
8947 	len += ret;
8948 
8949 	return len;
8950 }
8951 
8952 
hostapd_tx_status(struct hostapd_data * hapd,const u8 * addr,const u8 * buf,size_t len,int ack)8953 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
8954 		       const u8 *buf, size_t len, int ack)
8955 {
8956 	struct sta_info *sta;
8957 	struct hostapd_iface *iface = hapd->iface;
8958 
8959 	sta = ap_get_sta(hapd, addr);
8960 	if (sta == NULL && iface->num_bss > 1) {
8961 		size_t j;
8962 		for (j = 0; j < iface->num_bss; j++) {
8963 			hapd = iface->bss[j];
8964 			sta = ap_get_sta(hapd, addr);
8965 			if (sta)
8966 				break;
8967 		}
8968 	}
8969 	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
8970 		return;
8971 	if (sta->flags & WLAN_STA_PENDING_POLL) {
8972 		wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
8973 			   "activity poll", MAC2STR(sta->addr),
8974 			   ack ? "ACKed" : "did not ACK");
8975 		if (ack)
8976 			sta->flags &= ~WLAN_STA_PENDING_POLL;
8977 	}
8978 
8979 	ieee802_1x_tx_status(hapd, sta, buf, len, ack);
8980 }
8981 
8982 
hostapd_client_poll_ok(struct hostapd_data * hapd,const u8 * addr)8983 void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
8984 {
8985 	struct sta_info *sta;
8986 	struct hostapd_iface *iface = hapd->iface;
8987 
8988 	sta = ap_get_sta(hapd, addr);
8989 	if (sta == NULL && iface->num_bss > 1) {
8990 		size_t j;
8991 		for (j = 0; j < iface->num_bss; j++) {
8992 			hapd = iface->bss[j];
8993 			sta = ap_get_sta(hapd, addr);
8994 			if (sta)
8995 				break;
8996 		}
8997 	}
8998 	if (sta == NULL)
8999 		return;
9000 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
9001 		MAC2STR(sta->addr));
9002 	if (!(sta->flags & WLAN_STA_PENDING_POLL))
9003 		return;
9004 
9005 	wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
9006 		   "activity poll", MAC2STR(sta->addr));
9007 	sta->flags &= ~WLAN_STA_PENDING_POLL;
9008 }
9009 
9010 
ieee802_11_rx_from_unknown(struct hostapd_data * hapd,const u8 * src,int wds)9011 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
9012 				int wds)
9013 {
9014 	struct sta_info *sta;
9015 
9016 	sta = ap_get_sta(hapd, src);
9017 	if (sta &&
9018 	    ((sta->flags & WLAN_STA_ASSOC) ||
9019 	     ((sta->flags & WLAN_STA_ASSOC_REQ_OK) && wds))) {
9020 		if (!hapd->conf->wds_sta)
9021 			return;
9022 
9023 		if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK)) ==
9024 		    WLAN_STA_ASSOC_REQ_OK) {
9025 			wpa_printf(MSG_DEBUG,
9026 				   "Postpone 4-address WDS mode enabling for STA "
9027 				   MACSTR " since TX status for AssocResp is not yet known",
9028 				   MAC2STR(sta->addr));
9029 			sta->pending_wds_enable = 1;
9030 			return;
9031 		}
9032 
9033 		if (wds && !(sta->flags & WLAN_STA_WDS)) {
9034 			int ret;
9035 			char ifname_wds[IFNAMSIZ + 1];
9036 
9037 			wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
9038 				   "STA " MACSTR " (aid %u)",
9039 				   MAC2STR(sta->addr), sta->aid);
9040 			sta->flags |= WLAN_STA_WDS;
9041 			ret = hostapd_set_wds_sta(hapd, ifname_wds,
9042 						  sta->addr, sta->aid, 1);
9043 			if (!ret)
9044 				hostapd_set_wds_encryption(hapd, sta,
9045 							   ifname_wds);
9046 		}
9047 		return;
9048 	}
9049 
9050 	wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
9051 		   MACSTR, MAC2STR(src));
9052 	if (is_multicast_ether_addr(src) || is_zero_ether_addr(src) ||
9053 	    ether_addr_equal(src, hapd->own_addr)) {
9054 		/* Broadcast bit set in SA or unexpected SA?! Ignore the frame
9055 		 * silently. */
9056 		return;
9057 	}
9058 
9059 	if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
9060 		wpa_printf(MSG_DEBUG, "Association Response to the STA has "
9061 			   "already been sent, but no TX status yet known - "
9062 			   "ignore Class 3 frame issue with " MACSTR,
9063 			   MAC2STR(src));
9064 		return;
9065 	}
9066 
9067 	if (sta && (sta->flags & WLAN_STA_AUTH))
9068 		hostapd_drv_sta_disassoc(
9069 			hapd, src,
9070 			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
9071 	else
9072 		hostapd_drv_sta_deauth(
9073 			hapd, src,
9074 			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
9075 }
9076 
9077 
hostapd_add_tpe_info(u8 * eid,u8 tx_pwr_count,enum max_tx_pwr_interpretation tx_pwr_intrpn,u8 tx_pwr_cat,u8 tx_pwr)9078 static u8 * hostapd_add_tpe_info(u8 *eid, u8 tx_pwr_count,
9079 				 enum max_tx_pwr_interpretation tx_pwr_intrpn,
9080 				 u8 tx_pwr_cat, u8 tx_pwr)
9081 {
9082 	int i;
9083 
9084 	*eid++ = WLAN_EID_TRANSMIT_POWER_ENVELOPE; /* Element ID */
9085 	*eid++ = 2 + tx_pwr_count; /* Length */
9086 
9087 	/*
9088 	 * Transmit Power Information field
9089 	 *	bits 0-2 : Maximum Transmit Power Count
9090 	 *	bits 3-5 : Maximum Transmit Power Interpretation
9091 	 *	bits 6-7 : Maximum Transmit Power Category
9092 	 */
9093 	*eid++ = tx_pwr_count | (tx_pwr_intrpn << 3) | (tx_pwr_cat << 6);
9094 
9095 	/* Maximum Transmit Power field */
9096 	for (i = 0; i <= tx_pwr_count; i++)
9097 		*eid++ = tx_pwr;
9098 
9099 	return eid;
9100 }
9101 
9102 
9103 /*
9104  * TODO: Extract power limits from channel data after 6G regulatory
9105  *	support.
9106  */
9107 #define REG_PSD_MAX_TXPOWER_FOR_DEFAULT_CLIENT      (-1) /* dBm/MHz */
9108 #define REG_PSD_MAX_TXPOWER_FOR_SUBORDINATE_CLIENT  5    /* dBm/MHz */
9109 
hostapd_eid_txpower_envelope(struct hostapd_data * hapd,u8 * eid)9110 u8 * hostapd_eid_txpower_envelope(struct hostapd_data *hapd, u8 *eid)
9111 {
9112 	struct hostapd_iface *iface = hapd->iface;
9113 	struct hostapd_config *iconf = iface->conf;
9114 	struct hostapd_hw_modes *mode = iface->current_mode;
9115 	struct hostapd_channel_data *chan;
9116 	int dfs, i;
9117 	u8 channel, tx_pwr_count, local_pwr_constraint;
9118 	int max_tx_power;
9119 	u8 tx_pwr;
9120 
9121 	if (!mode)
9122 		return eid;
9123 
9124 	if (ieee80211_freq_to_chan(iface->freq, &channel) == NUM_HOSTAPD_MODES)
9125 		return eid;
9126 
9127 	for (i = 0; i < mode->num_channels; i++) {
9128 		if (mode->channels[i].freq == iface->freq)
9129 			break;
9130 	}
9131 	if (i == mode->num_channels)
9132 		return eid;
9133 
9134 #ifdef CONFIG_IEEE80211AX
9135 	/* IEEE Std 802.11ax-2021, Annex E.2.7 (6 GHz band in the United
9136 	 * States): An AP that is an Indoor Access Point per regulatory rules
9137 	 * shall send at least two Transmit Power Envelope elements in Beacon
9138 	 * and Probe Response frames as follows:
9139 	 *  - Maximum Transmit Power Category subfield = Default;
9140 	 *	Unit interpretation = Regulatory client EIRP PSD
9141 	 *  - Maximum Transmit Power Category subfield = Subordinate Device;
9142 	 *	Unit interpretation = Regulatory client EIRP PSD
9143 	 */
9144 	if (is_6ghz_op_class(iconf->op_class)) {
9145 		enum max_tx_pwr_interpretation tx_pwr_intrpn;
9146 		int err;
9147 		int max_eirp_psd = REG_PSD_MAX_TXPOWER_FOR_DEFAULT_CLIENT * 2;
9148 		int max_eirp_power = iconf->reg_def_cli_eirp;
9149 
9150 		/* Same Maximum Transmit Power for all 20 MHz bands */
9151 		tx_pwr_count = 0;
9152 		tx_pwr_intrpn = REGULATORY_CLIENT_EIRP_PSD;
9153 
9154 		/* Default Transmit Power Envelope for Global Operating Class */
9155 		err = hostap_afc_get_chan_max_eirp_power(iface, true,
9156 							 &max_eirp_psd);
9157 		if (err < 0) {
9158 			if (hapd->iconf->reg_def_cli_eirp_psd != -1)
9159 				max_eirp_psd =
9160 					hapd->iconf->reg_def_cli_eirp_psd;
9161 			else
9162 				max_eirp_psd =
9163 					REG_PSD_MAX_TXPOWER_FOR_DEFAULT_CLIENT * 2;
9164 		}
9165 
9166 		eid = hostapd_add_tpe_info(eid, tx_pwr_count, tx_pwr_intrpn,
9167 					   REG_DEFAULT_CLIENT, max_eirp_psd);
9168 
9169 		/* Indoor Access Point must include an additional TPE for
9170 		 * subordinate devices */
9171 		if (he_reg_is_indoor(iconf->he_6ghz_reg_pwr_type)) {
9172 			if (err < 0) {
9173 				/* non-AFC connection */
9174 				if (hapd->iconf->reg_sub_cli_eirp_psd != -1)
9175 					max_eirp_psd =
9176 						hapd->iconf->reg_sub_cli_eirp_psd;
9177 				else
9178 					max_eirp_psd =
9179 						REG_PSD_MAX_TXPOWER_FOR_SUBORDINATE_CLIENT * 2;
9180 			}
9181 			eid = hostapd_add_tpe_info(eid, tx_pwr_count,
9182 						   tx_pwr_intrpn,
9183 						   REG_SUBORDINATE_CLIENT,
9184 						   max_eirp_psd);
9185 		}
9186 
9187 		if (hostap_afc_get_chan_max_eirp_power(iface, false,
9188 						       &max_eirp_power)) {
9189 			max_eirp_power = iconf->reg_def_cli_eirp;
9190 			if (max_eirp_power == -1 ||
9191 			    !he_reg_is_sp(iconf->he_6ghz_reg_pwr_type))
9192 				return eid;
9193 		}
9194 
9195 		return hostapd_add_tpe_info(eid, tx_pwr_count,
9196 					    REGULATORY_CLIENT_EIRP,
9197 					    REG_DEFAULT_CLIENT,
9198 					    max_eirp_power);
9199 	}
9200 #endif /* CONFIG_IEEE80211AX */
9201 
9202 	switch (hostapd_get_oper_chwidth(iconf)) {
9203 	case CONF_OPER_CHWIDTH_USE_HT:
9204 		if (iconf->secondary_channel == 0) {
9205 			/* Max Transmit Power count = 0 (20 MHz) */
9206 			tx_pwr_count = 0;
9207 		} else {
9208 			/* Max Transmit Power count = 1 (20, 40 MHz) */
9209 			tx_pwr_count = 1;
9210 		}
9211 		break;
9212 	case CONF_OPER_CHWIDTH_80MHZ:
9213 		/* Max Transmit Power count = 2 (20, 40, and 80 MHz) */
9214 		tx_pwr_count = 2;
9215 		break;
9216 	case CONF_OPER_CHWIDTH_80P80MHZ:
9217 	case CONF_OPER_CHWIDTH_160MHZ:
9218 		/* Max Transmit Power count = 3 (20, 40, 80, 160/80+80 MHz) */
9219 		tx_pwr_count = 3;
9220 		break;
9221 	default:
9222 		return eid;
9223 	}
9224 
9225 	/*
9226 	 * Below local_pwr_constraint logic is referred from
9227 	 * hostapd_eid_pwr_constraint.
9228 	 *
9229 	 * Check if DFS is required by regulatory.
9230 	 */
9231 	dfs = hostapd_is_dfs_required(hapd->iface);
9232 	if (dfs < 0)
9233 		dfs = 0;
9234 
9235 	/*
9236 	 * In order to meet regulations when TPC is not implemented using
9237 	 * a transmit power that is below the legal maximum (including any
9238 	 * mitigation factor) should help. In this case, indicate 3 dB below
9239 	 * maximum allowed transmit power.
9240 	 */
9241 	if (hapd->iconf->local_pwr_constraint == -1)
9242 		local_pwr_constraint = (dfs == 0) ? 0 : 3;
9243 	else
9244 		local_pwr_constraint = hapd->iconf->local_pwr_constraint;
9245 
9246 	/*
9247 	 * A STA that is not an AP shall use a transmit power less than or
9248 	 * equal to the local maximum transmit power level for the channel.
9249 	 * The local maximum transmit power can be calculated from the formula:
9250 	 * local max TX pwr = max TX pwr - local pwr constraint
9251 	 * Where max TX pwr is maximum transmit power level specified for
9252 	 * channel in Country element and local pwr constraint is specified
9253 	 * for channel in this Power Constraint element.
9254 	 */
9255 	chan = &mode->channels[i];
9256 	max_tx_power = chan->max_tx_power - local_pwr_constraint;
9257 
9258 	/*
9259 	 * Local Maximum Transmit power is encoded as two's complement
9260 	 * with a 0.5 dB step.
9261 	 */
9262 	max_tx_power *= 2; /* in 0.5 dB steps */
9263 	if (max_tx_power > 127) {
9264 		/* 63.5 has special meaning of 63.5 dBm or higher */
9265 		max_tx_power = 127;
9266 	}
9267 	if (max_tx_power < -128)
9268 		max_tx_power = -128;
9269 	if (max_tx_power < 0)
9270 		tx_pwr = 0x80 + max_tx_power + 128;
9271 	else
9272 		tx_pwr = max_tx_power;
9273 
9274 	return hostapd_add_tpe_info(eid, tx_pwr_count, LOCAL_EIRP,
9275 				    0 /* Reserved for bands other than 6 GHz */,
9276 				    tx_pwr);
9277 }
9278 
9279 
9280 /* Wide Bandwidth Channel Switch subelement */
hostapd_eid_wb_channel_switch(struct hostapd_data * hapd,u8 * eid,u8 chan1,u8 chan2)9281 static u8 * hostapd_eid_wb_channel_switch(struct hostapd_data *hapd, u8 *eid,
9282 					  u8 chan1, u8 chan2)
9283 {
9284 	u8 bw;
9285 
9286 	/* bandwidth: 0: 40, 1: 80, 160, 80+80, 4 to 255 reserved as per
9287 	 * IEEE Std 802.11-2024, 9.4.2.156 and Table 9-316 (VHT Operation
9288 	 * Information subfields).
9289 	 */
9290 	switch (hapd->cs_freq_params.bandwidth) {
9291 	case 320:
9292 		/* As per IEEE Std 802.11be-2024, 35.15.3 (Channel switching
9293 		 * methods for an EHT BSS), for EHT BSS operating channel width
9294 		 * wider than 160 MHz, the announced BSS bandwidth in the Wide
9295 		 * Bandwidth Channel Switch element is less than the BSS
9296 		 * bandwidth in the Bandwidth Indication element
9297 		 */
9298 
9299 		/* Modifying the center frequency to 160 MHz */
9300 		if (hapd->cs_freq_params.channel < chan1)
9301 			chan1 -= 16;
9302 		else
9303 			chan1 += 16;
9304 
9305 		/* fallthrough */
9306 	case 160:
9307 		/* Update the CCFS0 and CCFS1 values in the element based on
9308 		 * IEEE Std 802.11-2024, Table 9-316 (VHT Operation
9309 		 * Information subfields).
9310 		 */
9311 
9312 		/* CCFS1 - The channel center frequency index of the 160 MHz
9313 		 * channel. */
9314 		chan2 = chan1;
9315 
9316 		/* CCFS0 - The channel center frequency index of the 80 MHz
9317 		 * channel segment that contains the primary channel. */
9318 		if (hapd->cs_freq_params.channel < chan1)
9319 			chan1 -= 8;
9320 		else
9321 			chan1 += 8;
9322 
9323 		bw = 1;
9324 		break;
9325 	case 80:
9326 		bw = 1;
9327 		break;
9328 	case 40:
9329 		bw = 0;
9330 		break;
9331 	default:
9332 		/* not valid VHT bandwidth or not in CSA */
9333 		return eid;
9334 	}
9335 
9336 	*eid++ = WLAN_EID_WIDE_BW_CHSWITCH;
9337 	*eid++ = 3; /* Length of Wide Bandwidth Channel Switch element */
9338 	*eid++ = bw; /* New Channel Width */
9339 	*eid++ = chan1; /* New Channel Center Frequency Segment 0 */
9340 	*eid++ = chan2; /* New Channel Center Frequency Segment 1 */
9341 
9342 	return eid;
9343 }
9344 
9345 
9346 #ifdef CONFIG_IEEE80211BE
9347 /* Bandwidth Indication element that is also used as the Bandwidth Indication
9348  * For Channel Switch subelement within a Channel Switch Wrapper element. */
hostapd_eid_bw_indication(struct hostapd_data * hapd,u8 * eid,u8 chan1,u8 chan2)9349 static u8 * hostapd_eid_bw_indication(struct hostapd_data *hapd, u8 *eid,
9350 				      u8 chan1, u8 chan2)
9351 {
9352 	u16 punct_bitmap = hapd->cs_freq_params.punct_bitmap;
9353 	struct ieee80211_bw_ind_element *bw_ind_elem;
9354 	size_t elen = 4;
9355 
9356 	if (hapd->cs_freq_params.bandwidth <= 160 && !punct_bitmap)
9357 		return eid;
9358 
9359 	if (punct_bitmap)
9360 		elen += EHT_OPER_DISABLED_SUBCHAN_BITMAP_SIZE;
9361 
9362 	*eid++ = WLAN_EID_EXTENSION;
9363 	*eid++ = 1 + elen;
9364 	*eid++ = WLAN_EID_EXT_BANDWIDTH_INDICATION;
9365 
9366 	bw_ind_elem = (struct ieee80211_bw_ind_element *) eid;
9367 	os_memset(bw_ind_elem, 0, sizeof(struct ieee80211_bw_ind_element));
9368 
9369 	switch (hapd->cs_freq_params.bandwidth) {
9370 	case 320:
9371 		bw_ind_elem->bw_ind_info.control |= BW_IND_CHANNEL_WIDTH_320MHZ;
9372 		chan2 = chan1;
9373 		if (hapd->cs_freq_params.channel < chan1)
9374 			chan1 -= 16;
9375 		else
9376 			chan1 += 16;
9377 		break;
9378 	case 160:
9379 		bw_ind_elem->bw_ind_info.control |= BW_IND_CHANNEL_WIDTH_160MHZ;
9380 		chan2 = chan1;
9381 		if (hapd->cs_freq_params.channel < chan1)
9382 			chan1 -= 8;
9383 		else
9384 			chan1 += 8;
9385 		break;
9386 	case 80:
9387 		bw_ind_elem->bw_ind_info.control |= BW_IND_CHANNEL_WIDTH_80MHZ;
9388 		break;
9389 	case 40:
9390 		if (hapd->cs_freq_params.sec_channel_offset == 1)
9391 			bw_ind_elem->bw_ind_info.control |=
9392 				BW_IND_CHANNEL_WIDTH_40MHZ;
9393 		else
9394 			bw_ind_elem->bw_ind_info.control |=
9395 				BW_IND_CHANNEL_WIDTH_20MHZ;
9396 		break;
9397 	default:
9398 		bw_ind_elem->bw_ind_info.control |= BW_IND_CHANNEL_WIDTH_20MHZ;
9399 		break;
9400 	}
9401 
9402 	bw_ind_elem->bw_ind_info.ccfs0 = chan1;
9403 	bw_ind_elem->bw_ind_info.ccfs1 = chan2;
9404 
9405 	if (punct_bitmap) {
9406 		bw_ind_elem->bw_ind_params |=
9407 			BW_IND_PARAMETER_DISABLED_SUBCHAN_BITMAP_PRESENT;
9408 		bw_ind_elem->bw_ind_info.disabled_chan_bitmap =
9409 			host_to_le16(punct_bitmap);
9410 	}
9411 
9412 	return eid + elen;
9413 }
9414 #endif /* CONFIG_IEEE80211BE */
9415 
9416 
hostapd_eid_chsw_wrapper(struct hostapd_data * hapd,u8 * eid)9417 u8 * hostapd_eid_chsw_wrapper(struct hostapd_data *hapd, u8 *eid)
9418 {
9419 	u8 chan1 = 0, chan2 = 0;
9420 	u8 *eid_len_offset;
9421 	int freq1;
9422 
9423 	if (!hostapd_is_vht_enabled(hapd) &&
9424 	    !hostapd_is_he_enabled(hapd) &&
9425 	    !hostapd_is_eht_enabled(hapd))
9426 		return eid;
9427 
9428 	if (!hapd->cs_freq_params.channel ||
9429 	    (!hapd->cs_freq_params.vht_enabled &&
9430 	     !hapd->cs_freq_params.he_enabled &&
9431 	     !hapd->cs_freq_params.eht_enabled))
9432 		return eid;
9433 
9434 	freq1 = hapd->cs_freq_params.center_freq1 ?
9435 		hapd->cs_freq_params.center_freq1 :
9436 		hapd->cs_freq_params.freq;
9437 	if (ieee80211_freq_to_chan(freq1, &chan1) !=
9438 	    HOSTAPD_MODE_IEEE80211A)
9439 		return eid;
9440 
9441 	if (hapd->cs_freq_params.center_freq2 &&
9442 	    ieee80211_freq_to_chan(hapd->cs_freq_params.center_freq2,
9443 				   &chan2) != HOSTAPD_MODE_IEEE80211A)
9444 		return eid;
9445 
9446 	*eid++ = WLAN_EID_CHANNEL_SWITCH_WRAPPER;
9447 	eid_len_offset = eid++; /* Length of Channel Switch Wrapper element */
9448 
9449 	eid = hostapd_eid_wb_channel_switch(hapd, eid, chan1, chan2);
9450 
9451 #ifdef CONFIG_IEEE80211BE
9452 	if (hostapd_is_eht_enabled(hapd)) {
9453 		/* Bandwidth Indication For Channel Switch subelement */
9454 		eid = hostapd_eid_bw_indication(hapd, eid, chan1, chan2);
9455 	}
9456 #endif /* CONFIG_IEEE80211BE */
9457 
9458 	*eid_len_offset = (eid - eid_len_offset) - 1;
9459 	return eid;
9460 }
9461 
9462 
hostapd_eid_nr_db_len(struct hostapd_data * hapd,size_t * current_len)9463 static size_t hostapd_eid_nr_db_len(struct hostapd_data *hapd,
9464 				    size_t *current_len)
9465 {
9466 	struct hostapd_neighbor_entry *nr;
9467 	size_t total_len = 0, len = *current_len;
9468 
9469 	dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
9470 			 list) {
9471 		if (!nr->nr || wpabuf_len(nr->nr) < 12)
9472 			continue;
9473 
9474 		if (nr->short_ssid == hapd->conf->ssid.short_ssid)
9475 			continue;
9476 
9477 		/* Start a new element */
9478 		if (!len ||
9479 		    len + RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN > 255) {
9480 			len = RNR_HEADER_LEN;
9481 			total_len += RNR_HEADER_LEN;
9482 		}
9483 
9484 		len += RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN;
9485 		total_len += RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN;
9486 	}
9487 
9488 	*current_len = len;
9489 	return total_len;
9490 }
9491 
9492 
9493 #ifdef CONFIG_IEEE80211BE
hostapd_mbssid_mld_match(struct hostapd_data * tx_hapd,struct hostapd_data * ml_hapd,u8 * match_idx)9494 static bool hostapd_mbssid_mld_match(struct hostapd_data *tx_hapd,
9495 				     struct hostapd_data *ml_hapd,
9496 				     u8 *match_idx)
9497 {
9498 	size_t bss_idx;
9499 
9500 	if (!ml_hapd->conf->mld_ap)
9501 		return false;
9502 
9503 	if (!tx_hapd->iconf->mbssid || tx_hapd->iface->num_bss <= 1) {
9504 		if (hostapd_is_ml_partner(tx_hapd, ml_hapd)) {
9505 			if (match_idx)
9506 				*match_idx = 0;
9507 			return true;
9508 		}
9509 
9510 		return false;
9511 	}
9512 
9513 	for (bss_idx = 0; bss_idx < tx_hapd->iface->num_bss; bss_idx++) {
9514 		struct hostapd_data *bss = tx_hapd->iface->bss[bss_idx];
9515 
9516 		if (!bss)
9517 			continue;
9518 
9519 		if (hostapd_is_ml_partner(bss, ml_hapd)) {
9520 			if (match_idx)
9521 				*match_idx = bss_idx;
9522 			return true;
9523 		}
9524 	}
9525 
9526 	return false;
9527 }
9528 #endif /* CONFIG_IEEE80211BE */
9529 
9530 
9531 struct mbssid_ie_profiles {
9532 	u8 start;
9533 	u8 end;
9534 };
9535 
hostapd_skip_rnr(size_t i,struct mbssid_ie_profiles * skip_profiles,bool ap_mld,u8 tbtt_info_len,bool mld_update,struct hostapd_data * reporting_hapd,struct hostapd_data * bss,u8 * match_idx)9536 static bool hostapd_skip_rnr(size_t i, struct mbssid_ie_profiles *skip_profiles,
9537 			     bool ap_mld, u8 tbtt_info_len, bool mld_update,
9538 			     struct hostapd_data *reporting_hapd,
9539 			     struct hostapd_data *bss, u8 *match_idx)
9540 {
9541 	bool reporting_ap_mld = false;
9542 
9543 #ifdef CONFIG_IEEE80211BE
9544 	reporting_ap_mld = !!reporting_hapd->conf->mld_ap;
9545 #endif /* CONFIG_IEEE80211BE */
9546 
9547 	if (!mld_update && skip_profiles &&
9548 	    i >= skip_profiles->start && i < skip_profiles->end)
9549 		return true;
9550 
9551 	/* No need to report if length is for normal TBTT and both the reporting
9552 	 * AP and neighbor AP are affiliated with an AP MLD. MLD TBTT will
9553 	 * include this. */
9554 	if (tbtt_info_len == RNR_TBTT_INFO_LEN && ap_mld && reporting_ap_mld)
9555 		return true;
9556 
9557 	/* No need to report if length is for MLD TBTT and the BSS is not
9558 	 * affiliated with an aP MLD. Normal TBTT will include this. */
9559 	if (tbtt_info_len == RNR_TBTT_INFO_MLD_LEN && !ap_mld)
9560 		return true;
9561 
9562 #ifdef CONFIG_IEEE80211BE
9563 	/* If building for co-location and they are ML partners, no need to
9564 	 * include since the ML RNR will carry this. */
9565 	if (!mld_update && hostapd_is_ml_partner(reporting_hapd, bss))
9566 		return true;
9567 
9568 	/* If building for ML RNR and they are not ML partners, don't include.
9569 	 */
9570 	if (mld_update &&
9571 	    !hostapd_mbssid_mld_match(reporting_hapd, bss, match_idx))
9572 		return true;
9573 
9574 	/* When MLD parameters are added to beacon RNR and in case of EMA
9575 	 * beacons we report only affiliated APs belonging to the reported
9576 	 * non Tx profiles and TX profile will be reported in every EMA beacon.
9577 	 */
9578 	if (mld_update && skip_profiles && match_idx &&
9579 	    (*match_idx < skip_profiles->start ||
9580 	     *match_idx >= skip_profiles->end))
9581 		return true;
9582 #endif /* CONFIG_IEEE80211BE */
9583 
9584 	return false;
9585 }
9586 
9587 
hostapd_rnr_get_bss_info(struct hostapd_data * hapd,struct hostapd_data * reporting_hapd,struct mbssid_ie_profiles * skip_profiles,size_t i,u8 tbtt_info_len,bool mld_update,u8 * op_class,u8 * channel,u8 * match_idx)9588 static bool hostapd_rnr_get_bss_info(struct hostapd_data *hapd,
9589 				     struct hostapd_data *reporting_hapd,
9590 				     struct mbssid_ie_profiles *skip_profiles,
9591 				     size_t i, u8 tbtt_info_len,
9592 				     bool mld_update,
9593 				     u8 *op_class, u8 *channel, u8 *match_idx)
9594 {
9595 	struct hostapd_data *bss;
9596 	bool ap_mld = false;
9597 	u8 tmp_match_idx = 255;
9598 	enum oper_chan_width bss_chwidth;
9599 	int secondary_channel;
9600 	u8 seg0, seg1;
9601 
9602 	if (!hapd->iface || i >= hapd->iface->num_bss || !op_class || !channel)
9603 		return false;
9604 
9605 	bss = hapd->iface->bss[i];
9606 	if (!bss || !bss->conf || !bss->started || !bss->beacon_set_done ||
9607 	    bss == reporting_hapd)
9608 		return false;
9609 
9610 #ifdef CONFIG_IEEE80211BE
9611 	ap_mld = !!bss->conf->mld_ap;
9612 #endif /* CONFIG_IEEE80211BE */
9613 
9614 	/* MLD RNR has to be included for the parameter change count */
9615 	if (bss->conf->ignore_broadcast_ssid && !(ap_mld && mld_update))
9616 		return false;
9617 
9618 	if (!match_idx)
9619 		match_idx = &tmp_match_idx;
9620 
9621 	if (hostapd_skip_rnr(i, skip_profiles, ap_mld, tbtt_info_len,
9622 			     mld_update, reporting_hapd, bss, match_idx))
9623 		return false;
9624 
9625 	hostapd_get_oper_chan_info_of_bss(bss, &bss_chwidth, &seg0, &seg1);
9626 	secondary_channel = bss->iconf->secondary_channel;
9627 
9628 	if (seg0 == bss->iconf->channel &&
9629 	    bss_chwidth == CONF_OPER_CHWIDTH_USE_HT)
9630 		secondary_channel = 0;
9631 
9632 	if (ieee80211_freq_to_channel_ext(
9633 		    bss->iface->freq,
9634 		    secondary_channel, bss_chwidth,
9635 		    op_class, channel) == NUM_HOSTAPD_MODES)
9636 		return false;
9637 
9638 	return true;
9639 }
9640 
9641 
9642 static size_t
hostapd_eid_rnr_iface_len(struct hostapd_data * hapd,struct hostapd_data * reporting_hapd,size_t * current_len,struct mbssid_ie_profiles * skip_profiles,bool mld_update)9643 hostapd_eid_rnr_iface_len(struct hostapd_data *hapd,
9644 			  struct hostapd_data *reporting_hapd,
9645 			  size_t *current_len,
9646 			  struct mbssid_ie_profiles *skip_profiles,
9647 			  bool mld_update)
9648 {
9649 	struct hostapd_iface *iface = hapd->iface;
9650 	size_t total_len = 0, len = *current_len;
9651 	int total_tbtt_count = 0;
9652 	size_t i;
9653 	u8 tbtt_info_len = mld_update ? RNR_TBTT_INFO_MLD_LEN :
9654 		RNR_TBTT_INFO_LEN;
9655 	bool reporting_ap_mld = false;
9656 	bool have_pending_group;
9657 	u8 pending_op_class = 0, pending_channel = 0;
9658 	bool *tbtt_added = NULL;
9659 
9660 #ifdef CONFIG_IEEE80211BE
9661 	reporting_ap_mld = !!reporting_hapd->conf->mld_ap;
9662 #endif /* CONFIG_IEEE80211BE */
9663 
9664 repeat_rnr_len:
9665 	os_free(tbtt_added);
9666 	tbtt_added = os_zalloc(iface->num_bss);
9667 	if (!tbtt_added)
9668 		return total_len;
9669 
9670 	have_pending_group = false;
9671 	for (;;) {
9672 		int tbtt_count = 0;
9673 		bool group_found = false, group_pending = false;
9674 		u8 rnr_op_class = 0, rnr_channel = 0;
9675 
9676 		if (have_pending_group) {
9677 			rnr_op_class = pending_op_class;
9678 			rnr_channel = pending_channel;
9679 			group_found = true;
9680 		}
9681 
9682 		if (!group_found) {
9683 			for (i = 0; i < iface->num_bss; i++) {
9684 				if (tbtt_added[i])
9685 					continue;
9686 				if (!hostapd_rnr_get_bss_info(
9687 					    hapd, reporting_hapd, skip_profiles,
9688 					    i, tbtt_info_len, mld_update,
9689 					    &rnr_op_class, &rnr_channel, NULL))
9690 					continue;
9691 				group_found = true;
9692 				break;
9693 			}
9694 		}
9695 
9696 		if (!group_found)
9697 			break;
9698 
9699 		if (!len ||
9700 		    len + RNR_TBTT_HEADER_LEN + tbtt_info_len > 255) {
9701 			len = RNR_HEADER_LEN;
9702 			total_len += RNR_HEADER_LEN;
9703 		}
9704 
9705 		len += RNR_TBTT_HEADER_LEN;
9706 		total_len += RNR_TBTT_HEADER_LEN;
9707 
9708 		for (i = 0; i < iface->num_bss; i++) {
9709 			u8 bss_op_class, bss_channel;
9710 
9711 			if (tbtt_added[i])
9712 				continue;
9713 
9714 			if (!hostapd_rnr_get_bss_info(
9715 				    hapd, reporting_hapd, skip_profiles,
9716 				    i, tbtt_info_len, mld_update,
9717 				    &bss_op_class, &bss_channel, NULL))
9718 				continue;
9719 
9720 			if (rnr_op_class != bss_op_class ||
9721 			    rnr_channel != bss_channel)
9722 				continue;
9723 
9724 			if (len + tbtt_info_len > 255 ||
9725 			    tbtt_count >= RNR_TBTT_INFO_COUNT_MAX) {
9726 				group_pending = true;
9727 				break;
9728 			}
9729 
9730 			len += tbtt_info_len;
9731 			total_len += tbtt_info_len;
9732 			tbtt_count++;
9733 			tbtt_added[i] = true;
9734 		}
9735 
9736 		if (!tbtt_count) {
9737 			len -= RNR_TBTT_HEADER_LEN;
9738 			total_len -= RNR_TBTT_HEADER_LEN;
9739 			break;
9740 		}
9741 
9742 		total_tbtt_count += tbtt_count;
9743 
9744 		if (group_pending) {
9745 			have_pending_group = true;
9746 			pending_op_class = rnr_op_class;
9747 			pending_channel = rnr_channel;
9748 		} else {
9749 			have_pending_group = false;
9750 		}
9751 	}
9752 
9753 	/* If building for co-location, re-build again but this time include
9754 	 * ML TBTTs if the reporting AP is affiliated with an AP MLD.
9755 	 */
9756 	if (!mld_update && tbtt_info_len == RNR_TBTT_INFO_LEN &&
9757 	    reporting_ap_mld) {
9758 		tbtt_info_len = RNR_TBTT_INFO_MLD_LEN;
9759 		goto repeat_rnr_len;
9760 	}
9761 
9762 	os_free(tbtt_added);
9763 
9764 	if (!total_tbtt_count)
9765 		total_len = 0;
9766 	else
9767 		*current_len = len;
9768 
9769 	return total_len;
9770 }
9771 
9772 
9773 enum colocation_mode {
9774 	NO_COLOCATED_6GHZ,
9775 	STANDALONE_6GHZ,
9776 	COLOCATED_6GHZ,
9777 	COLOCATED_LOWER_BAND,
9778 };
9779 
get_colocation_mode(struct hostapd_data * hapd)9780 static enum colocation_mode get_colocation_mode(struct hostapd_data *hapd)
9781 {
9782 	u8 i;
9783 	bool is_6ghz = is_6ghz_op_class(hapd->iconf->op_class);
9784 
9785 	if (!hapd->iface || !hapd->iface->interfaces)
9786 		return NO_COLOCATED_6GHZ;
9787 
9788 	if (is_6ghz && hapd->iface->interfaces->count == 1)
9789 		return STANDALONE_6GHZ;
9790 
9791 	for (i = 0; i < hapd->iface->interfaces->count; i++) {
9792 		struct hostapd_iface *iface;
9793 		bool is_colocated_6ghz;
9794 
9795 		iface = hapd->iface->interfaces->iface[i];
9796 		if (iface == hapd->iface || !iface || !iface->conf)
9797 			continue;
9798 
9799 		is_colocated_6ghz = is_6ghz_op_class(iface->conf->op_class);
9800 		if (!is_6ghz && is_colocated_6ghz)
9801 			return COLOCATED_LOWER_BAND;
9802 		if (is_6ghz && !is_colocated_6ghz)
9803 			return COLOCATED_6GHZ;
9804 	}
9805 
9806 	if (is_6ghz)
9807 		return STANDALONE_6GHZ;
9808 
9809 	return NO_COLOCATED_6GHZ;
9810 }
9811 
9812 
hostapd_eid_rnr_colocation_len(struct hostapd_data * hapd,size_t * current_len)9813 static size_t hostapd_eid_rnr_colocation_len(struct hostapd_data *hapd,
9814 					     size_t *current_len)
9815 {
9816 	struct hostapd_iface *iface;
9817 	size_t len = 0;
9818 	size_t i;
9819 
9820 	if (!hapd->iface || !hapd->iface->interfaces)
9821 		return 0;
9822 
9823 	for (i = 0; i < hapd->iface->interfaces->count; i++) {
9824 		iface = hapd->iface->interfaces->iface[i];
9825 
9826 		if (!iface || iface == hapd->iface ||
9827 		    iface->state != HAPD_IFACE_ENABLED ||
9828 		    !is_6ghz_op_class(iface->conf->op_class))
9829 			continue;
9830 
9831 		len += hostapd_eid_rnr_iface_len(iface->bss[0], hapd,
9832 						 current_len, NULL, false);
9833 	}
9834 
9835 	return len;
9836 }
9837 
9838 
hostapd_eid_rnr_mlo_len(struct hostapd_data * hapd,u32 type,struct mbssid_ie_profiles * skip_profiles,size_t * current_len)9839 static size_t hostapd_eid_rnr_mlo_len(struct hostapd_data *hapd, u32 type,
9840 				      struct mbssid_ie_profiles *skip_profiles,
9841 				      size_t *current_len)
9842 {
9843 	size_t len = 0;
9844 #ifdef CONFIG_IEEE80211BE
9845 	struct hostapd_iface *iface;
9846 	size_t i;
9847 
9848 	if (!hapd->iface || !hapd->iface->interfaces)
9849 		return 0;
9850 
9851 	/* TODO: Allow for FILS/Action as well */
9852 	if (type != WLAN_FC_STYPE_BEACON && type != WLAN_FC_STYPE_PROBE_RESP)
9853 		return 0;
9854 
9855 	for (i = 0; i < hapd->iface->interfaces->count; i++) {
9856 		iface = hapd->iface->interfaces->iface[i];
9857 
9858 		if (!iface || iface == hapd->iface ||
9859 		    hapd->iface->freq == iface->freq)
9860 			continue;
9861 
9862 		len += hostapd_eid_rnr_iface_len(iface->bss[0], hapd,
9863 						 current_len, skip_profiles,
9864 						 true);
9865 	}
9866 #endif /* CONFIG_IEEE80211BE */
9867 
9868 	return len;
9869 }
9870 
9871 
hostapd_eid_rnr_len(struct hostapd_data * hapd,u32 type,bool include_mld_params)9872 size_t hostapd_eid_rnr_len(struct hostapd_data *hapd, u32 type,
9873 			   bool include_mld_params)
9874 {
9875 	size_t total_len = 0, current_len = 0;
9876 	enum colocation_mode mode = get_colocation_mode(hapd);
9877 
9878 	switch (type) {
9879 	case WLAN_FC_STYPE_BEACON:
9880 		if (hapd->conf->rnr)
9881 			total_len += hostapd_eid_nr_db_len(hapd, &current_len);
9882 		/* fallthrough */
9883 	case WLAN_FC_STYPE_PROBE_RESP:
9884 		if (mode == COLOCATED_LOWER_BAND)
9885 			total_len +=
9886 				hostapd_eid_rnr_colocation_len(hapd,
9887 							       &current_len);
9888 
9889 		if (hapd->conf->rnr && hapd->iface->num_bss > 1 &&
9890 		    !hapd->iconf->mbssid)
9891 			total_len += hostapd_eid_rnr_iface_len(hapd, hapd,
9892 							       &current_len,
9893 							       NULL, false);
9894 		break;
9895 	case WLAN_FC_STYPE_ACTION:
9896 		if (hapd->iface->num_bss > 1 && mode == STANDALONE_6GHZ)
9897 			total_len += hostapd_eid_rnr_iface_len(hapd, hapd,
9898 							       &current_len,
9899 							       NULL, false);
9900 		break;
9901 	}
9902 
9903 	/* For EMA Beacons, MLD neighbor repoting is added as part of
9904 	 * MBSSID RNR. */
9905 	if (include_mld_params &&
9906 	    (type != WLAN_FC_STYPE_BEACON ||
9907 	     hapd->iconf->mbssid != ENHANCED_MBSSID_ENABLED))
9908 		total_len += hostapd_eid_rnr_mlo_len(hapd, type, NULL,
9909 						     &current_len);
9910 
9911 	return total_len;
9912 }
9913 
9914 
hostapd_eid_nr_db(struct hostapd_data * hapd,u8 * eid,size_t * current_len)9915 static u8 * hostapd_eid_nr_db(struct hostapd_data *hapd, u8 *eid,
9916 			      size_t *current_len)
9917 {
9918 	struct hostapd_neighbor_entry *nr;
9919 	size_t len = *current_len;
9920 	u8 *size_offset = (eid - len) + 1;
9921 
9922 	dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
9923 			 list) {
9924 		if (!nr->nr || wpabuf_len(nr->nr) < 12)
9925 			continue;
9926 
9927 		if (nr->short_ssid == hapd->conf->ssid.short_ssid)
9928 			continue;
9929 
9930 		/* Start a new element */
9931 		if (!len ||
9932 		    len + RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN > 255) {
9933 			*eid++ = WLAN_EID_REDUCED_NEIGHBOR_REPORT;
9934 			size_offset = eid++;
9935 			len = RNR_HEADER_LEN;
9936 		}
9937 
9938 		/* TBTT Information Header subfield (2 octets) */
9939 		*eid++ = 0;
9940 		/* TBTT Information Length */
9941 		*eid++ = RNR_TBTT_INFO_LEN;
9942 		/* Operating Class */
9943 		*eid++ = wpabuf_head_u8(nr->nr)[10];
9944 		/* Channel Number */
9945 		*eid++ = wpabuf_head_u8(nr->nr)[11];
9946 		len += RNR_TBTT_HEADER_LEN;
9947 		/* TBTT Information Set */
9948 		/* TBTT Information field */
9949 		/* Neighbor AP TBTT Offset */
9950 		*eid++ = RNR_NEIGHBOR_AP_OFFSET_UNKNOWN;
9951 		/* BSSID */
9952 		os_memcpy(eid, nr->bssid, ETH_ALEN);
9953 		eid += ETH_ALEN;
9954 		/* Short SSID */
9955 		os_memcpy(eid, &nr->short_ssid, 4);
9956 		eid += 4;
9957 		/* BSS parameters */
9958 		*eid++ = nr->bss_parameters;
9959 		/* 20 MHz PSD */
9960 		*eid++ = RNR_20_MHZ_PSD_MAX_TXPOWER;
9961 		len += RNR_TBTT_INFO_LEN;
9962 		*size_offset = (eid - size_offset) - 1;
9963 	}
9964 
9965 	*current_len = len;
9966 	return eid;
9967 }
9968 
9969 
hostapd_eid_rnr_bss(struct hostapd_data * hapd,struct hostapd_data * reporting_hapd,size_t i,u8 * tbtt_count,size_t * len,u8 ** pos,u8 ** tbtt_count_pos,u8 tbtt_info_len,u8 op_class,u8 channel,u8 match_idx)9970 static bool hostapd_eid_rnr_bss(struct hostapd_data *hapd,
9971 				struct hostapd_data *reporting_hapd,
9972 				size_t i, u8 *tbtt_count, size_t *len,
9973 				u8 **pos, u8 **tbtt_count_pos, u8 tbtt_info_len,
9974 				u8 op_class, u8 channel,
9975 				u8 match_idx)
9976 {
9977 	struct hostapd_iface *iface = hapd->iface;
9978 	struct hostapd_data *bss = iface->bss[i];
9979 	u8 bss_param = 0;
9980 #ifdef CONFIG_IEEE80211BE
9981 	bool ap_mld = false;
9982 #endif /* CONFIG_IEEE80211BE */
9983 	u8 *eid = *pos;
9984 
9985 	if (!bss || !bss->conf || bss == reporting_hapd)
9986 		return false;
9987 
9988 #ifdef CONFIG_IEEE80211BE
9989 	ap_mld = !!bss->conf->mld_ap;
9990 #endif /* CONFIG_IEEE80211BE */
9991 
9992 	if (*len + tbtt_info_len > 255 ||
9993 	    *tbtt_count >= RNR_TBTT_INFO_COUNT_MAX)
9994 		return true;
9995 
9996 	if (!(*tbtt_count)) {
9997 		/* Add neighbor report header info only if there is at least
9998 		 * one TBTT info available. */
9999 		*tbtt_count_pos = eid++;
10000 		*eid++ = tbtt_info_len;
10001 		*eid++ = op_class;
10002 		*eid++ = channel;
10003 		*len += RNR_TBTT_HEADER_LEN;
10004 	}
10005 
10006 	*eid++ = RNR_NEIGHBOR_AP_OFFSET_UNKNOWN;
10007 	os_memcpy(eid, bss->own_addr, ETH_ALEN);
10008 	eid += ETH_ALEN;
10009 	os_memcpy(eid, &bss->conf->ssid.short_ssid, 4);
10010 	eid += 4;
10011 	if (bss->conf->ssid.short_ssid == reporting_hapd->conf->ssid.short_ssid)
10012 		bss_param |= RNR_BSS_PARAM_SAME_SSID;
10013 
10014 	if (iface->conf->mbssid != MBSSID_DISABLED && iface->num_bss > 1) {
10015 		bss_param |= RNR_BSS_PARAM_MULTIPLE_BSSID;
10016 		if (bss == hostapd_mbssid_get_tx_bss(hapd))
10017 			bss_param |= RNR_BSS_PARAM_TRANSMITTED_BSSID;
10018 	}
10019 
10020 	if (is_6ghz_op_class(op_class) &&
10021 	    bss->conf->unsol_bcast_probe_resp_interval)
10022 		bss_param |= RNR_BSS_PARAM_UNSOLIC_PROBE_RESP_ACTIVE;
10023 
10024 	bss_param |= RNR_BSS_PARAM_CO_LOCATED;
10025 
10026 	*eid++ = bss_param;
10027 	*eid++ = RNR_20_MHZ_PSD_MAX_TXPOWER;
10028 
10029 #ifdef CONFIG_IEEE80211BE
10030 	/* Include the MLD parameters only when TBTT length is for ML RNR */
10031 	if (ap_mld && tbtt_info_len == RNR_TBTT_INFO_MLD_LEN) {
10032 		u8 param_ch = bss->eht_mld_bss_param_change;
10033 
10034 		/* If BSS is not a partner of the reporting_hapd or
10035 		 * it is one of the nontransmitted hapd,
10036 		 *  a) MLD ID advertised shall be 255.
10037 		 *  b) Link ID advertised shall be 15.
10038 		 *  c) BPCC advertised shall be 255 */
10039 		/* MLD ID */
10040 		*eid++ = match_idx;
10041 		/* Link ID (Bit 3 to Bit 0)
10042 		 * BPCC (Bit 4 to Bit 7) */
10043 		*eid++ = match_idx < 255 ?
10044 			bss->mld_link_id | ((param_ch & 0xF) << 4) :
10045 			(MAX_NUM_MLD_LINKS | 0xF0);
10046 		/* BPCC (Bit 3 to Bit 0) */
10047 		*eid = match_idx < 255 ? ((param_ch & 0xF0) >> 4) : 0x0F;
10048 #ifdef CONFIG_TESTING_OPTIONS
10049 		if (bss->conf->mld_indicate_disabled)
10050 			*eid |= RNR_TBTT_INFO_MLD_PARAM2_LINK_DISABLED;
10051 #endif /* CONFIG_TESTING_OPTIONS */
10052 		eid++;
10053 	}
10054 #endif /* CONFIG_IEEE80211BE */
10055 
10056 	*len += tbtt_info_len;
10057 	(*tbtt_count)++;
10058 	*pos = eid;
10059 
10060 	return false;
10061 }
10062 
10063 
hostapd_eid_rnr_iface(struct hostapd_data * hapd,struct hostapd_data * reporting_hapd,u8 * eid,size_t * current_len,struct mbssid_ie_profiles * skip_profiles,bool mld_update)10064 static u8 * hostapd_eid_rnr_iface(struct hostapd_data *hapd,
10065 				  struct hostapd_data *reporting_hapd,
10066 				  u8 *eid, size_t *current_len,
10067 				  struct mbssid_ie_profiles *skip_profiles,
10068 				  bool mld_update)
10069 {
10070 	struct hostapd_iface *iface = hapd->iface;
10071 	size_t i;
10072 	size_t len = *current_len;
10073 	u8 *eid_start = eid, *size_offset = (eid - len) + 1;
10074 	u8 *tbtt_count_pos = size_offset + 1;
10075 	u8 total_tbtt_count = 0;
10076 	u8 tbtt_info_len = mld_update ? RNR_TBTT_INFO_MLD_LEN :
10077 		RNR_TBTT_INFO_LEN;
10078 	bool reporting_ap_mld = false;
10079 	bool have_pending_group;
10080 	u8 pending_op_class = 0, pending_channel = 0;
10081 	bool *tbtt_added = NULL;
10082 
10083 	if (!(iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA) || !iface->freq)
10084 		return eid;
10085 
10086 #ifdef CONFIG_IEEE80211BE
10087 	reporting_ap_mld = !!reporting_hapd->conf->mld_ap;
10088 #endif /* CONFIG_IEEE80211BE */
10089 
10090 repeat_rnr:
10091 	os_free(tbtt_added);
10092 	tbtt_added = os_zalloc(iface->num_bss);
10093 	if (!tbtt_added)
10094 		return eid;
10095 
10096 	have_pending_group = false;
10097 	for (;;) {
10098 		u8 tbtt_count = 0;
10099 		bool group_found = false, group_pending = false;
10100 		u8 rnr_op_class = 0, rnr_channel = 0;
10101 
10102 		if (have_pending_group) {
10103 			rnr_op_class = pending_op_class;
10104 			rnr_channel = pending_channel;
10105 			group_found = true;
10106 		}
10107 
10108 		if (!group_found) {
10109 			for (i = 0; i < iface->num_bss; i++) {
10110 				if (tbtt_added[i])
10111 					continue;
10112 				if (!hostapd_rnr_get_bss_info(
10113 					    hapd, reporting_hapd, skip_profiles,
10114 					    i, tbtt_info_len, mld_update,
10115 					    &rnr_op_class, &rnr_channel, NULL))
10116 					continue;
10117 				group_found = true;
10118 				break;
10119 			}
10120 		}
10121 
10122 		if (!group_found)
10123 			break;
10124 
10125 		if (!len ||
10126 		    len + RNR_TBTT_HEADER_LEN + tbtt_info_len > 255) {
10127 			eid_start = eid;
10128 			*eid++ = WLAN_EID_REDUCED_NEIGHBOR_REPORT;
10129 			size_offset = eid++;
10130 			len = RNR_HEADER_LEN;
10131 		}
10132 
10133 		for (i = 0; i < iface->num_bss; i++) {
10134 			u8 op_class, channel, match_idx = 255;
10135 
10136 			if (tbtt_added[i])
10137 				continue;
10138 
10139 			if (!hostapd_rnr_get_bss_info(
10140 				    hapd, reporting_hapd, skip_profiles,
10141 				    i, tbtt_info_len, mld_update,
10142 				    &op_class, &channel, &match_idx))
10143 				continue;
10144 
10145 			if (rnr_op_class != op_class ||
10146 			    rnr_channel != channel)
10147 				continue;
10148 
10149 			if (hostapd_eid_rnr_bss(hapd, reporting_hapd, i,
10150 						&tbtt_count, &len, &eid,
10151 						&tbtt_count_pos, tbtt_info_len,
10152 						op_class, channel,
10153 						match_idx)) {
10154 				group_pending = true;
10155 				break;
10156 			}
10157 
10158 			tbtt_added[i] = true;
10159 		}
10160 
10161 		if (tbtt_count) {
10162 			*tbtt_count_pos = RNR_TBTT_INFO_COUNT(tbtt_count - 1);
10163 			*size_offset = (eid - size_offset) - 1;
10164 		} else {
10165 			break;
10166 		}
10167 
10168 		total_tbtt_count += tbtt_count;
10169 
10170 		if (group_pending) {
10171 			have_pending_group = true;
10172 			pending_op_class = rnr_op_class;
10173 			pending_channel = rnr_channel;
10174 		} else {
10175 			have_pending_group = false;
10176 		}
10177 	}
10178 
10179 	/* If building for co-location, re-build again but this time include
10180 	 * ML TBTTs if the reporting AP is affiliated with an AP MLD.
10181 	 */
10182 	if (!mld_update && tbtt_info_len == RNR_TBTT_INFO_LEN &&
10183 	    reporting_ap_mld) {
10184 		tbtt_info_len = RNR_TBTT_INFO_MLD_LEN;
10185 		goto repeat_rnr;
10186 	}
10187 
10188 	os_free(tbtt_added);
10189 
10190 	if (!total_tbtt_count)
10191 		return eid_start;
10192 
10193 	*current_len = len;
10194 	return eid;
10195 }
10196 
10197 
hostapd_eid_rnr_colocation(struct hostapd_data * hapd,u8 * eid,size_t * current_len)10198 static u8 * hostapd_eid_rnr_colocation(struct hostapd_data *hapd, u8 *eid,
10199 				       size_t *current_len)
10200 {
10201 	struct hostapd_iface *iface;
10202 	size_t i;
10203 
10204 	if (!hapd->iface || !hapd->iface->interfaces)
10205 		return eid;
10206 
10207 	for (i = 0; i < hapd->iface->interfaces->count; i++) {
10208 		iface = hapd->iface->interfaces->iface[i];
10209 
10210 		if (!iface || iface == hapd->iface ||
10211 		    iface->state != HAPD_IFACE_ENABLED ||
10212 		    !is_6ghz_op_class(iface->conf->op_class))
10213 			continue;
10214 
10215 		eid = hostapd_eid_rnr_iface(iface->bss[0], hapd, eid,
10216 					    current_len, NULL, false);
10217 	}
10218 
10219 	return eid;
10220 }
10221 
10222 
hostapd_eid_rnr_mlo(struct hostapd_data * hapd,u32 type,u8 * eid,struct mbssid_ie_profiles * skip_profiles,size_t * current_len)10223 static u8 * hostapd_eid_rnr_mlo(struct hostapd_data *hapd, u32 type,
10224 				u8 *eid,
10225 				struct mbssid_ie_profiles *skip_profiles,
10226 				size_t *current_len)
10227 {
10228 #ifdef CONFIG_IEEE80211BE
10229 	struct hostapd_iface *iface;
10230 	size_t i;
10231 
10232 	if (!hapd->iface || !hapd->iface->interfaces)
10233 		return eid;
10234 
10235 	/* TODO: Allow for FILS/Action as well */
10236 	if (type != WLAN_FC_STYPE_BEACON && type != WLAN_FC_STYPE_PROBE_RESP)
10237 		return eid;
10238 
10239 	for (i = 0; i < hapd->iface->interfaces->count; i++) {
10240 		iface = hapd->iface->interfaces->iface[i];
10241 
10242 		if (!iface || iface == hapd->iface ||
10243 		    hapd->iface->freq == iface->freq)
10244 			continue;
10245 
10246 		eid = hostapd_eid_rnr_iface(iface->bss[0], hapd, eid,
10247 					    current_len, skip_profiles, true);
10248 	}
10249 #endif /* CONFIG_IEEE80211BE */
10250 
10251 	return eid;
10252 }
10253 
10254 
hostapd_eid_rnr(struct hostapd_data * hapd,u8 * eid,u32 type,bool include_mld_params)10255 u8 * hostapd_eid_rnr(struct hostapd_data *hapd, u8 *eid, u32 type,
10256 		     bool include_mld_params)
10257 {
10258 	u8 *eid_start = eid;
10259 	size_t current_len = 0;
10260 	enum colocation_mode mode = get_colocation_mode(hapd);
10261 
10262 	switch (type) {
10263 	case WLAN_FC_STYPE_BEACON:
10264 		if (hapd->conf->rnr)
10265 			eid = hostapd_eid_nr_db(hapd, eid, &current_len);
10266 		/* fallthrough */
10267 	case WLAN_FC_STYPE_PROBE_RESP:
10268 		if (mode == COLOCATED_LOWER_BAND)
10269 			eid = hostapd_eid_rnr_colocation(hapd, eid,
10270 							 &current_len);
10271 
10272 		if (hapd->conf->rnr && hapd->iface->num_bss > 1 &&
10273 		    !hapd->iconf->mbssid)
10274 			eid = hostapd_eid_rnr_iface(hapd, hapd, eid,
10275 						    &current_len, NULL, false);
10276 		break;
10277 	case WLAN_FC_STYPE_ACTION:
10278 		if (hapd->iface->num_bss > 1 && mode == STANDALONE_6GHZ)
10279 			eid = hostapd_eid_rnr_iface(hapd, hapd, eid,
10280 						    &current_len, NULL, false);
10281 		break;
10282 	default:
10283 		return eid_start;
10284 	}
10285 
10286 	/* For EMA Beacons, MLD neighbor repoting is added as part of
10287 	 * MBSSID RNR. */
10288 	if (include_mld_params &&
10289 	    (type != WLAN_FC_STYPE_BEACON ||
10290 	     hapd->iconf->mbssid != ENHANCED_MBSSID_ENABLED))
10291 		eid = hostapd_eid_rnr_mlo(hapd, type, eid, NULL, &current_len);
10292 
10293 	if (eid == eid_start + 2)
10294 		return eid_start;
10295 
10296 	return eid;
10297 }
10298 
10299 
mbssid_known_bss(unsigned int i,const u8 * known_bss,size_t known_bss_len)10300 static bool mbssid_known_bss(unsigned int i, const u8 *known_bss,
10301 			     size_t known_bss_len)
10302 {
10303 	if (!known_bss || known_bss_len <= i / 8)
10304 		return false;
10305 	known_bss = &known_bss[i / 8];
10306 	return *known_bss & (u8) (BIT(i % 8));
10307 }
10308 
10309 
hostapd_mbssid_ext_capa(struct hostapd_data * bss,struct hostapd_data * tx_bss,u8 * buf)10310 static size_t hostapd_mbssid_ext_capa(struct hostapd_data *bss,
10311 				      struct hostapd_data *tx_bss, u8 *buf)
10312 {
10313 	u8 ext_capa_tx[20], *ext_capa_tx_end, ext_capa[20], *ext_capa_end;
10314 	size_t ext_capa_len, ext_capa_tx_len;
10315 
10316 	ext_capa_tx_end = hostapd_eid_ext_capab(tx_bss, ext_capa_tx,
10317 						true);
10318 	ext_capa_tx_len = ext_capa_tx_end - ext_capa_tx;
10319 	ext_capa_end = hostapd_eid_ext_capab(bss, ext_capa, true);
10320 	ext_capa_len = ext_capa_end - ext_capa;
10321 	if (ext_capa_tx_len != ext_capa_len ||
10322 	    os_memcmp(ext_capa_tx, ext_capa, ext_capa_len) != 0) {
10323 		os_memcpy(buf, ext_capa, ext_capa_len);
10324 		return ext_capa_len;
10325 	}
10326 
10327 	return 0;
10328 }
10329 
10330 
hostapd_eid_mbssid_elem_len(struct hostapd_data * hapd,u32 frame_type,size_t * bss_index,const u8 * known_bss,size_t known_bss_len)10331 static size_t hostapd_eid_mbssid_elem_len(struct hostapd_data *hapd,
10332 					  u32 frame_type, size_t *bss_index,
10333 					  const u8 *known_bss,
10334 					  size_t known_bss_len)
10335 {
10336 	struct hostapd_data *tx_bss = hostapd_mbssid_get_tx_bss(hapd);
10337 	size_t len, i, tx_xrate_len;
10338 	u8 ext_capa[20], buf[100];
10339 
10340 	/* Element ID: 1 octet
10341 	 * Length: 1 octet
10342 	 * MaxBSSID Indicator: 1 octet
10343 	 * Optional Subelements: vatiable
10344 	 *
10345 	 * Total fixed length: 3 octets
10346 	 *
10347 	 * 1 octet in len for the MaxBSSID Indicator field.
10348 	 */
10349 	len = 1;
10350 
10351 	tx_xrate_len = hostapd_eid_ext_supp_rates(tx_bss, buf) - buf;
10352 
10353 	for (i = *bss_index; i < hapd->iface->num_bss; i++) {
10354 		struct hostapd_data *bss = hapd->iface->bss[i];
10355 		const u8 *auth, *rsn = NULL, *rsnx = NULL;
10356 		size_t nontx_profile_len, auth_len, xrate_len;
10357 		u8 ie_count = 0;
10358 
10359 		if (!bss || !bss->conf || !bss->started ||
10360 		    mbssid_known_bss(i, known_bss, known_bss_len))
10361 			continue;
10362 
10363 		/*
10364 		 * Sublement ID: 1 octet
10365 		 * Length: 1 octet
10366 		 * Nontransmitted capabilities: 4 octets
10367 		 * SSID element: 2 + variable
10368 		 * Multiple BSSID Index Element: 3 octets (+2 octets in beacons)
10369 		 * Fixed length = 1 + 1 + 4 + 2 + 3 = 11
10370 		 */
10371 		nontx_profile_len = 11 + bss->conf->ssid.ssid_len;
10372 
10373 		if (frame_type == WLAN_FC_STYPE_BEACON)
10374 			nontx_profile_len += 2;
10375 
10376 		auth = wpa_auth_get_wpa_ie(bss->wpa_auth, &auth_len);
10377 		if (auth) {
10378 			rsn = get_ie(auth, auth_len, WLAN_EID_RSN);
10379 			if (rsn)
10380 				nontx_profile_len += 2 + rsn[1];
10381 
10382 			rsnx = get_ie(auth, auth_len, WLAN_EID_RSNX);
10383 			if (rsnx)
10384 				nontx_profile_len += 2 + rsnx[1];
10385 		}
10386 
10387 		nontx_profile_len += hostapd_mbssid_ext_capa(bss, tx_bss,
10388 							     ext_capa);
10389 
10390 		if (!rsn && hostapd_wpa_ie(tx_bss, WLAN_EID_RSN))
10391 			ie_count++;
10392 		if (!rsnx && hostapd_wpa_ie(tx_bss, WLAN_EID_RSNX))
10393 			ie_count++;
10394 
10395 		xrate_len = hostapd_eid_ext_supp_rates(bss, buf) - buf;
10396 
10397 		if (xrate_len)
10398 			nontx_profile_len += xrate_len;
10399 		else if (tx_xrate_len)
10400 			ie_count++;
10401 
10402 #ifdef CONFIG_IEEE80211BE
10403 		/* For ML Probe Response frame, the solicited hapd's MLE will
10404 		 * be in the frame body */
10405 		if (bss->conf->mld_ap &&
10406 		    (bss != hapd || frame_type != WLAN_FC_STYPE_PROBE_RESP))
10407 			nontx_profile_len += hostapd_eid_eht_basic_ml_len(
10408 				bss, NULL, true, false);
10409 #endif /* CONFIG_IEEE80211BE */
10410 
10411 		if (ie_count)
10412 			nontx_profile_len += 4 + ie_count + 1;
10413 
10414 		if (len + nontx_profile_len > 255)
10415 			break;
10416 
10417 		len += nontx_profile_len;
10418 	}
10419 
10420 	*bss_index = i;
10421 
10422 	/* Add 2 octets to get the full size of the element */
10423 	return len + 2;
10424 }
10425 
10426 
hostapd_eid_mbssid_len(struct hostapd_data * hapd_probed,u32 frame_type,u8 * elem_count,const u8 * known_bss,size_t known_bss_len,size_t * rnr_len)10427 size_t hostapd_eid_mbssid_len(struct hostapd_data *hapd_probed, u32 frame_type,
10428 			      u8 *elem_count, const u8 *known_bss,
10429 			      size_t known_bss_len, size_t *rnr_len)
10430 {
10431 	struct hostapd_data *hapd = hostapd_mbssid_get_tx_bss(hapd_probed);
10432 	size_t len = 0, bss_index = 1;
10433 
10434 	if (!hapd->iconf->mbssid ||
10435 	    (frame_type != WLAN_FC_STYPE_BEACON &&
10436 	     frame_type != WLAN_FC_STYPE_PROBE_RESP))
10437 		return 0;
10438 
10439 	/*
10440 	 * Include the Multiple BSSID element whenever MBSSID is enabled. The
10441 	 * element may include zero or more nontransmitted BSSID profiles.
10442 	 */
10443 	if (hapd->iface->num_bss == 1) {
10444 		if (frame_type == WLAN_FC_STYPE_BEACON) {
10445 			if (!elem_count) {
10446 				wpa_printf(MSG_INFO,
10447 					   "MBSSID: Insufficient data for Beacon frames");
10448 				return 0;
10449 			}
10450 			*elem_count = 1;
10451 		}
10452 		return 3;
10453 	}
10454 
10455 	if (frame_type == WLAN_FC_STYPE_BEACON) {
10456 		if (!elem_count) {
10457 			wpa_printf(MSG_INFO,
10458 				   "MBSSID: Insufficient data for Beacon frames");
10459 			return 0;
10460 		}
10461 		*elem_count = 0;
10462 	}
10463 
10464 	while (bss_index < hapd->iface->num_bss) {
10465 		size_t rnr_count = bss_index;
10466 
10467 		len += hostapd_eid_mbssid_elem_len(hapd_probed, frame_type,
10468 						   &bss_index, known_bss,
10469 						   known_bss_len);
10470 
10471 		if (frame_type == WLAN_FC_STYPE_BEACON)
10472 			*elem_count += 1;
10473 		if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED && rnr_len) {
10474 			size_t rnr_cur_len = 0;
10475 			struct mbssid_ie_profiles skip_profiles = {
10476 				rnr_count, bss_index
10477 			};
10478 
10479 			*rnr_len += hostapd_eid_rnr_iface_len(
10480 				hapd, hostapd_mbssid_get_tx_bss(hapd),
10481 				&rnr_cur_len, &skip_profiles, false);
10482 
10483 			*rnr_len += hostapd_eid_rnr_mlo_len(
10484 				hostapd_mbssid_get_tx_bss(hapd), frame_type,
10485 				&skip_profiles, &rnr_cur_len);
10486 		}
10487 	}
10488 
10489 	if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED && rnr_len)
10490 		*rnr_len += hostapd_eid_rnr_len(hapd, frame_type, false);
10491 
10492 	return len;
10493 }
10494 
10495 
hostapd_eid_mbssid_elem(struct hostapd_data * hapd,u8 * eid,u8 * end,u32 frame_type,u8 max_bssid_indicator,size_t * bss_index,u8 elem_count,const u8 * known_bss,size_t known_bss_len)10496 static u8 * hostapd_eid_mbssid_elem(struct hostapd_data *hapd, u8 *eid, u8 *end,
10497 				    u32 frame_type, u8 max_bssid_indicator,
10498 				    size_t *bss_index, u8 elem_count,
10499 				    const u8 *known_bss, size_t known_bss_len)
10500 {
10501 	struct hostapd_data *tx_bss = hostapd_mbssid_get_tx_bss(hapd);
10502 	size_t i, tx_xrate_len;
10503 	u8 *eid_len_offset, *max_bssid_indicator_offset;
10504 	u8 buf[100];
10505 
10506 	*eid++ = WLAN_EID_MULTIPLE_BSSID;
10507 	eid_len_offset = eid++;
10508 	max_bssid_indicator_offset = eid++;
10509 
10510 	tx_xrate_len = hostapd_eid_ext_supp_rates(tx_bss, buf) - buf;
10511 
10512 	for (i = *bss_index; i < hapd->iface->num_bss; i++) {
10513 		struct hostapd_data *bss = hapd->iface->bss[i];
10514 		struct hostapd_bss_config *conf;
10515 		struct hostapd_bss_config *tx_conf = tx_bss->conf;
10516 		u8 *eid_len_pos, *nontx_bss_start = eid;
10517 		const u8 *auth, *rsn = NULL, *rsnx = NULL;
10518 		u8 ie_count = 0, non_inherit_ie[3];
10519 		size_t auth_len = 0, xrate_len;
10520 		u16 capab_info;
10521 		u8 mbssindex = i;
10522 
10523 		if (!bss || !bss->conf || !bss->started ||
10524 		    mbssid_known_bss(i, known_bss, known_bss_len))
10525 			continue;
10526 		conf = bss->conf;
10527 
10528 		*eid++ = WLAN_MBSSID_SUBELEMENT_NONTRANSMITTED_BSSID_PROFILE;
10529 		eid_len_pos = eid++;
10530 
10531 		capab_info = hostapd_own_capab_info(bss);
10532 		*eid++ = WLAN_EID_NONTRANSMITTED_BSSID_CAPA;
10533 		*eid++ = sizeof(capab_info);
10534 		WPA_PUT_LE16(eid, capab_info);
10535 		eid += sizeof(capab_info);
10536 
10537 		*eid++ = WLAN_EID_SSID;
10538 		*eid++ = conf->ssid.ssid_len;
10539 		os_memcpy(eid, conf->ssid.ssid, conf->ssid.ssid_len);
10540 		eid += conf->ssid.ssid_len;
10541 
10542 		if (conf->mbssid_index &&
10543 		    conf->mbssid_index > tx_conf->mbssid_index)
10544 			mbssindex = conf->mbssid_index - tx_conf->mbssid_index;
10545 
10546 		*eid++ = WLAN_EID_MULTIPLE_BSSID_INDEX;
10547 		if (frame_type == WLAN_FC_STYPE_BEACON) {
10548 			*eid++ = 3;
10549 			*eid++ = mbssindex; /* BSSID Index */
10550 			if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED &&
10551 			    (conf->dtim_period % elem_count))
10552 				conf->dtim_period = elem_count;
10553 			*eid++ = conf->dtim_period;
10554 			/* The driver is expected to update the DTIM Count
10555 			 * field for each BSS that corresponds to a
10556 			 * nontransmitted BSSID. The value is initialized to
10557 			 * 0 here so that the DTIM count would be somewhat
10558 			 * functional even if the driver were not to update
10559 			 * this. */
10560 			*eid++ = 0; /* DTIM Count */
10561 		} else {
10562 			/* Probe Request frame does not include DTIM Period and
10563 			 * DTIM Count fields. */
10564 			*eid++ = 1;
10565 			*eid++ = mbssindex; /* BSSID Index */
10566 		}
10567 
10568 		auth = wpa_auth_get_wpa_ie(bss->wpa_auth, &auth_len);
10569 		if (auth) {
10570 			rsn = get_ie(auth, auth_len, WLAN_EID_RSN);
10571 			if (rsn) {
10572 				os_memcpy(eid, rsn, 2 + rsn[1]);
10573 				eid += 2 + rsn[1];
10574 			}
10575 
10576 			rsnx = get_ie(auth, auth_len, WLAN_EID_RSNX);
10577 			if (rsnx) {
10578 				os_memcpy(eid, rsnx, 2 + rsnx[1]);
10579 				eid += 2 + rsnx[1];
10580 			}
10581 		}
10582 
10583 		eid += hostapd_mbssid_ext_capa(bss, tx_bss, eid);
10584 		xrate_len = hostapd_eid_ext_supp_rates(bss, eid) - eid;
10585 		eid += xrate_len;
10586 
10587 		/* List of Element ID values in increasing order */
10588 		if (!rsn && hostapd_wpa_ie(tx_bss, WLAN_EID_RSN))
10589 			non_inherit_ie[ie_count++] = WLAN_EID_RSN;
10590 		if (tx_xrate_len && !xrate_len)
10591 			non_inherit_ie[ie_count++] = WLAN_EID_EXT_SUPP_RATES;
10592 		if (!rsnx && hostapd_wpa_ie(tx_bss, WLAN_EID_RSNX))
10593 			non_inherit_ie[ie_count++] = WLAN_EID_RSNX;
10594 #ifdef CONFIG_IEEE80211BE
10595 		/* For ML Probe Response frame, the solicited hapd's MLE will
10596 		 * be in the frame body */
10597 		if (bss->conf->mld_ap &&
10598 		    (bss != hapd || frame_type != WLAN_FC_STYPE_PROBE_RESP))
10599 			eid = hostapd_eid_eht_basic_ml_common(bss, eid, NULL,
10600 							      true, false);
10601 #endif /* CONFIG_IEEE80211BE */
10602 		if (ie_count) {
10603 			*eid++ = WLAN_EID_EXTENSION;
10604 			*eid++ = 2 + ie_count + 1;
10605 			*eid++ = WLAN_EID_EXT_NON_INHERITANCE;
10606 			*eid++ = ie_count;
10607 			os_memcpy(eid, non_inherit_ie, ie_count);
10608 			eid += ie_count;
10609 			*eid++ = 0; /* No Element ID Extension List */
10610 		}
10611 
10612 		*eid_len_pos = (eid - eid_len_pos) - 1;
10613 
10614 		if (((eid - eid_len_offset) - 1) > 255) {
10615 			eid = nontx_bss_start;
10616 			break;
10617 		}
10618 	}
10619 
10620 	*bss_index = i;
10621 	*max_bssid_indicator_offset = max_bssid_indicator;
10622 	if (*max_bssid_indicator_offset < 1)
10623 		*max_bssid_indicator_offset = 1;
10624 	*eid_len_offset = (eid - eid_len_offset) - 1;
10625 	return eid;
10626 }
10627 
10628 
hostapd_eid_mbssid(struct hostapd_data * hapd_probed,u8 * eid,u8 * end,unsigned int frame_stype,u8 elem_count,u8 ** elem_offset,const u8 * known_bss,size_t known_bss_len,u8 * rnr_eid,u8 * rnr_count,u8 ** rnr_offset,size_t rnr_len)10629 u8 * hostapd_eid_mbssid(struct hostapd_data *hapd_probed, u8 *eid, u8 *end,
10630 			unsigned int frame_stype, u8 elem_count,
10631 			u8 **elem_offset,
10632 			const u8 *known_bss, size_t known_bss_len, u8 *rnr_eid,
10633 			u8 *rnr_count, u8 **rnr_offset, size_t rnr_len)
10634 {
10635 	struct hostapd_data *hapd = hostapd_mbssid_get_tx_bss(hapd_probed);
10636 	size_t bss_index = 1, cur_len = 0;
10637 	u8 elem_index = 0, *rnr_start_eid = rnr_eid;
10638 	bool add_rnr;
10639 
10640 	if (!hapd->iconf->mbssid ||
10641 	    (frame_stype != WLAN_FC_STYPE_BEACON &&
10642 	     frame_stype != WLAN_FC_STYPE_PROBE_RESP))
10643 		return eid;
10644 
10645 	if (frame_stype == WLAN_FC_STYPE_BEACON && !elem_offset) {
10646 		wpa_printf(MSG_INFO,
10647 			   "MBSSID: Insufficient data for Beacon frames");
10648 		return eid;
10649 	}
10650 
10651 	add_rnr = hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED &&
10652 		frame_stype == WLAN_FC_STYPE_BEACON &&
10653 		rnr_eid && rnr_count && rnr_offset && rnr_len;
10654 
10655 	/*
10656 	 * Include the Multiple BSSID element whenever MBSSID is enabled. The
10657 	 * element may include zero or more nontransmitted BSSID profiles.
10658 	 */
10659 	if (hapd->iface->num_bss == 1) {
10660 		if (frame_stype == WLAN_FC_STYPE_BEACON)
10661 			elem_offset[0] = eid;
10662 
10663 		return hostapd_eid_mbssid_elem(
10664 			hapd_probed, eid, end, frame_stype,
10665 			hostapd_max_bssid_indicator(hapd),
10666 			&bss_index, elem_count, known_bss, known_bss_len);
10667 	}
10668 
10669 	while (bss_index < hapd->iface->num_bss) {
10670 		unsigned int rnr_start_count = bss_index;
10671 
10672 		if (frame_stype == WLAN_FC_STYPE_BEACON) {
10673 			if (elem_index == elem_count) {
10674 				wpa_printf(MSG_WARNING,
10675 					   "MBSSID: Larger number of elements than there is room in the provided array");
10676 				break;
10677 			}
10678 
10679 			elem_offset[elem_index] = eid;
10680 			elem_index = elem_index + 1;
10681 		}
10682 		eid = hostapd_eid_mbssid_elem(hapd_probed, eid, end,
10683 					      frame_stype,
10684 					      hostapd_max_bssid_indicator(hapd),
10685 					      &bss_index, elem_count,
10686 					      known_bss, known_bss_len);
10687 
10688 		if (add_rnr) {
10689 			struct mbssid_ie_profiles skip_profiles = {
10690 				rnr_start_count, bss_index
10691 			};
10692 
10693 			rnr_offset[*rnr_count] = rnr_eid;
10694 			*rnr_count = *rnr_count + 1;
10695 			cur_len = 0;
10696 			rnr_eid = hostapd_eid_rnr_iface(
10697 				hapd, hostapd_mbssid_get_tx_bss(hapd),
10698 				rnr_eid, &cur_len, &skip_profiles, false);
10699 			rnr_eid = hostapd_eid_rnr_mlo(
10700 				hostapd_mbssid_get_tx_bss(hapd), frame_stype,
10701 				rnr_eid, &skip_profiles, &cur_len);
10702 		}
10703 	}
10704 
10705 	if (add_rnr && (size_t) (rnr_eid - rnr_start_eid) < rnr_len) {
10706 		rnr_offset[*rnr_count] = rnr_eid;
10707 		*rnr_count = *rnr_count + 1;
10708 		cur_len = 0;
10709 
10710 		if (hapd->conf->rnr)
10711 			rnr_eid = hostapd_eid_nr_db(hapd, rnr_eid, &cur_len);
10712 		if (get_colocation_mode(hapd) == COLOCATED_LOWER_BAND)
10713 			rnr_eid = hostapd_eid_rnr_colocation(hapd, rnr_eid,
10714 							     &cur_len);
10715 	}
10716 
10717 	return eid;
10718 }
10719 
10720 #endif /* CONFIG_NATIVE_WINDOWS */
10721