• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / IEEE 802.11 Management
3  * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #ifndef CONFIG_NATIVE_WINDOWS
12 
13 #include "utils/common.h"
14 #include "utils/eloop.h"
15 #include "crypto/crypto.h"
16 #include "crypto/sha256.h"
17 #include "crypto/sha384.h"
18 #include "crypto/sha512.h"
19 #include "crypto/random.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/ieee802_11_common.h"
22 #include "common/wpa_ctrl.h"
23 #include "common/sae.h"
24 #include "common/dpp.h"
25 #include "common/ocv.h"
26 #include "common/wpa_common.h"
27 #include "common/wpa_ctrl.h"
28 #include "common/ptksa_cache.h"
29 #include "radius/radius.h"
30 #include "radius/radius_client.h"
31 #include "p2p/p2p.h"
32 #include "wps/wps.h"
33 #include "fst/fst.h"
34 #include "hostapd.h"
35 #include "beacon.h"
36 #include "ieee802_11_auth.h"
37 #include "sta_info.h"
38 #include "ieee802_1x.h"
39 #include "wpa_auth.h"
40 #include "pmksa_cache_auth.h"
41 #include "wmm.h"
42 #include "ap_list.h"
43 #include "accounting.h"
44 #include "ap_config.h"
45 #include "ap_mlme.h"
46 #include "p2p_hostapd.h"
47 #include "ap_drv_ops.h"
48 #include "wnm_ap.h"
49 #include "hw_features.h"
50 #include "ieee802_11.h"
51 #include "dfs.h"
52 #include "mbo_ap.h"
53 #include "rrm.h"
54 #include "taxonomy.h"
55 #include "fils_hlp.h"
56 #include "dpp_hostapd.h"
57 #include "gas_query_ap.h"
58 #include "comeback_token.h"
59 #include "pasn/pasn_common.h"
60 
61 
62 #ifdef CONFIG_FILS
63 static struct wpabuf *
64 prepare_auth_resp_fils(struct hostapd_data *hapd,
65 		       struct sta_info *sta, u16 *resp,
66 		       struct rsn_pmksa_cache_entry *pmksa,
67 		       struct wpabuf *erp_resp,
68 		       const u8 *msk, size_t msk_len,
69 		       int *is_pub);
70 #endif /* CONFIG_FILS */
71 
72 #ifdef CONFIG_PASN
73 #ifdef CONFIG_FILS
74 
75 static void pasn_fils_auth_resp(struct hostapd_data *hapd,
76 				struct sta_info *sta, u16 status,
77 				struct wpabuf *erp_resp,
78 				const u8 *msk, size_t msk_len);
79 
80 #endif /* CONFIG_FILS */
81 #endif /* CONFIG_PASN */
82 
83 static void handle_auth(struct hostapd_data *hapd,
84 			const struct ieee80211_mgmt *mgmt, size_t len,
85 			int rssi, int from_queue);
86 
87 
hostapd_eid_multi_ap(struct hostapd_data * hapd,u8 * eid)88 u8 * hostapd_eid_multi_ap(struct hostapd_data *hapd, u8 *eid)
89 {
90 	u8 multi_ap_val = 0;
91 
92 	if (!hapd->conf->multi_ap)
93 		return eid;
94 	if (hapd->conf->multi_ap & BACKHAUL_BSS)
95 		multi_ap_val |= MULTI_AP_BACKHAUL_BSS;
96 	if (hapd->conf->multi_ap & FRONTHAUL_BSS)
97 		multi_ap_val |= MULTI_AP_FRONTHAUL_BSS;
98 
99 	return eid + add_multi_ap_ie(eid, 9, multi_ap_val);
100 }
101 
102 
hostapd_eid_supp_rates(struct hostapd_data * hapd,u8 * eid)103 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
104 {
105 	u8 *pos = eid;
106 	int i, num, count;
107 	int h2e_required;
108 
109 	if (hapd->iface->current_rates == NULL)
110 		return eid;
111 
112 	*pos++ = WLAN_EID_SUPP_RATES;
113 	num = hapd->iface->num_rates;
114 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
115 		num++;
116 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
117 		num++;
118 #ifdef CONFIG_IEEE80211AX
119 	if (hapd->iconf->ieee80211ax && hapd->iconf->require_he)
120 		num++;
121 #endif /* CONFIG_IEEE80211AX */
122 	h2e_required = (hapd->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
123 			hostapd_sae_pw_id_in_use(hapd->conf) == 2) &&
124 		hapd->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK &&
125 		wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt);
126 	if (h2e_required)
127 		num++;
128 	if (num > 8) {
129 		/* rest of the rates are encoded in Extended supported
130 		 * rates element */
131 		num = 8;
132 	}
133 
134 	*pos++ = num;
135 	for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
136 	     i++) {
137 		count++;
138 		*pos = hapd->iface->current_rates[i].rate / 5;
139 		if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
140 			*pos |= 0x80;
141 		pos++;
142 	}
143 
144 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
145 		count++;
146 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
147 	}
148 
149 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
150 		count++;
151 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
152 	}
153 
154 #ifdef CONFIG_IEEE80211AX
155 	if (hapd->iconf->ieee80211ax && hapd->iconf->require_he && count < 8) {
156 		count++;
157 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY;
158 	}
159 #endif /* CONFIG_IEEE80211AX */
160 
161 	if (h2e_required && count < 8) {
162 		count++;
163 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY;
164 	}
165 
166 	return pos;
167 }
168 
169 
hostapd_eid_ext_supp_rates(struct hostapd_data * hapd,u8 * eid)170 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
171 {
172 	u8 *pos = eid;
173 	int i, num, count;
174 	int h2e_required;
175 
176 	hapd->conf->xrates_supported = false;
177 	if (hapd->iface->current_rates == NULL)
178 		return eid;
179 
180 	num = hapd->iface->num_rates;
181 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
182 		num++;
183 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
184 		num++;
185 #ifdef CONFIG_IEEE80211AX
186 	if (hapd->iconf->ieee80211ax && hapd->iconf->require_he)
187 		num++;
188 #endif /* CONFIG_IEEE80211AX */
189 	h2e_required = (hapd->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
190 			hostapd_sae_pw_id_in_use(hapd->conf) == 2) &&
191 		hapd->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK &&
192 		wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt);
193 	if (h2e_required)
194 		num++;
195 	if (num <= 8)
196 		return eid;
197 	num -= 8;
198 
199 	*pos++ = WLAN_EID_EXT_SUPP_RATES;
200 	*pos++ = num;
201 	for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
202 	     i++) {
203 		count++;
204 		if (count <= 8)
205 			continue; /* already in SuppRates IE */
206 		*pos = hapd->iface->current_rates[i].rate / 5;
207 		if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
208 			*pos |= 0x80;
209 		pos++;
210 	}
211 
212 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
213 		count++;
214 		if (count > 8)
215 			*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
216 	}
217 
218 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
219 		count++;
220 		if (count > 8)
221 			*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
222 	}
223 
224 #ifdef CONFIG_IEEE80211AX
225 	if (hapd->iconf->ieee80211ax && hapd->iconf->require_he) {
226 		count++;
227 		if (count > 8)
228 			*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY;
229 	}
230 #endif /* CONFIG_IEEE80211AX */
231 
232 	if (h2e_required) {
233 		count++;
234 		if (count > 8)
235 			*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY;
236 	}
237 
238 	hapd->conf->xrates_supported = true;
239 	return pos;
240 }
241 
242 
hostapd_eid_rm_enabled_capab(struct hostapd_data * hapd,u8 * eid,size_t len)243 u8 * hostapd_eid_rm_enabled_capab(struct hostapd_data *hapd, u8 *eid,
244 				  size_t len)
245 {
246 	size_t i;
247 
248 	for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
249 		if (hapd->conf->radio_measurements[i])
250 			break;
251 	}
252 
253 	if (i == RRM_CAPABILITIES_IE_LEN || len < 2 + RRM_CAPABILITIES_IE_LEN)
254 		return eid;
255 
256 	*eid++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
257 	*eid++ = RRM_CAPABILITIES_IE_LEN;
258 	os_memcpy(eid, hapd->conf->radio_measurements, RRM_CAPABILITIES_IE_LEN);
259 
260 	return eid + RRM_CAPABILITIES_IE_LEN;
261 }
262 
263 
hostapd_own_capab_info(struct hostapd_data * hapd)264 u16 hostapd_own_capab_info(struct hostapd_data *hapd)
265 {
266 	int capab = WLAN_CAPABILITY_ESS;
267 	int privacy = 0;
268 	int dfs;
269 	int i;
270 
271 	/* Check if any of configured channels require DFS */
272 	dfs = hostapd_is_dfs_required(hapd->iface);
273 	if (dfs < 0) {
274 		wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
275 			   dfs);
276 		dfs = 0;
277 	}
278 
279 	if (hapd->iface->num_sta_no_short_preamble == 0 &&
280 	    hapd->iconf->preamble == SHORT_PREAMBLE)
281 		capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
282 
283 #ifdef CONFIG_WEP
284 	privacy = hapd->conf->ssid.wep.keys_set;
285 
286 	if (hapd->conf->ieee802_1x &&
287 	    (hapd->conf->default_wep_key_len ||
288 	     hapd->conf->individual_wep_key_len))
289 		privacy = 1;
290 #endif /* CONFIG_WEP */
291 
292 	if (hapd->conf->wpa)
293 		privacy = 1;
294 
295 #ifdef CONFIG_HS20
296 	if (hapd->conf->osen)
297 		privacy = 1;
298 #endif /* CONFIG_HS20 */
299 
300 	if (privacy)
301 		capab |= WLAN_CAPABILITY_PRIVACY;
302 
303 	if (hapd->iface->current_mode &&
304 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
305 	    hapd->iface->num_sta_no_short_slot_time == 0)
306 		capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
307 
308 	/*
309 	 * Currently, Spectrum Management capability bit is set when directly
310 	 * requested in configuration by spectrum_mgmt_required or when AP is
311 	 * running on DFS channel.
312 	 * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
313 	 */
314 	if (hapd->iface->current_mode &&
315 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
316 	    (hapd->iconf->spectrum_mgmt_required || dfs))
317 		capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
318 
319 	for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
320 		if (hapd->conf->radio_measurements[i]) {
321 			capab |= IEEE80211_CAP_RRM;
322 			break;
323 		}
324 	}
325 
326 	return capab;
327 }
328 
329 
330 #ifdef CONFIG_WEP
331 #ifndef CONFIG_NO_RC4
auth_shared_key(struct hostapd_data * hapd,struct sta_info * sta,u16 auth_transaction,const u8 * challenge,int iswep)332 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
333 			   u16 auth_transaction, const u8 *challenge,
334 			   int iswep)
335 {
336 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
337 		       HOSTAPD_LEVEL_DEBUG,
338 		       "authentication (shared key, transaction %d)",
339 		       auth_transaction);
340 
341 	if (auth_transaction == 1) {
342 		if (!sta->challenge) {
343 			/* Generate a pseudo-random challenge */
344 			u8 key[8];
345 
346 			sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
347 			if (sta->challenge == NULL)
348 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
349 
350 			if (os_get_random(key, sizeof(key)) < 0) {
351 				os_free(sta->challenge);
352 				sta->challenge = NULL;
353 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
354 			}
355 
356 			rc4_skip(key, sizeof(key), 0,
357 				 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
358 		}
359 		return 0;
360 	}
361 
362 	if (auth_transaction != 3)
363 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
364 
365 	/* Transaction 3 */
366 	if (!iswep || !sta->challenge || !challenge ||
367 	    os_memcmp_const(sta->challenge, challenge,
368 			    WLAN_AUTH_CHALLENGE_LEN)) {
369 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
370 			       HOSTAPD_LEVEL_INFO,
371 			       "shared key authentication - invalid "
372 			       "challenge-response");
373 		return WLAN_STATUS_CHALLENGE_FAIL;
374 	}
375 
376 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
377 		       HOSTAPD_LEVEL_DEBUG,
378 		       "authentication OK (shared key)");
379 	sta->flags |= WLAN_STA_AUTH;
380 	wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
381 	os_free(sta->challenge);
382 	sta->challenge = NULL;
383 
384 	return 0;
385 }
386 #endif /* CONFIG_NO_RC4 */
387 #endif /* CONFIG_WEP */
388 
389 
send_auth_reply(struct hostapd_data * hapd,struct sta_info * sta,const u8 * dst,const u8 * bssid,u16 auth_alg,u16 auth_transaction,u16 resp,const u8 * ies,size_t ies_len,const char * dbg)390 static int send_auth_reply(struct hostapd_data *hapd, struct sta_info *sta,
391 			   const u8 *dst, const u8 *bssid,
392 			   u16 auth_alg, u16 auth_transaction, u16 resp,
393 			   const u8 *ies, size_t ies_len, const char *dbg)
394 {
395 	struct ieee80211_mgmt *reply;
396 	u8 *buf;
397 	size_t rlen;
398 	int reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
399 
400 	rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
401 	buf = os_zalloc(rlen);
402 	if (buf == NULL)
403 		return -1;
404 
405 	reply = (struct ieee80211_mgmt *) buf;
406 	reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
407 					    WLAN_FC_STYPE_AUTH);
408 	os_memcpy(reply->da, dst, ETH_ALEN);
409 	os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
410 	os_memcpy(reply->bssid, bssid, ETH_ALEN);
411 
412 	reply->u.auth.auth_alg = host_to_le16(auth_alg);
413 	reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
414 	reply->u.auth.status_code = host_to_le16(resp);
415 
416 	if (ies && ies_len)
417 		os_memcpy(reply->u.auth.variable, ies, ies_len);
418 
419 	wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
420 		   " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu) (dbg=%s)",
421 		   MAC2STR(dst), auth_alg, auth_transaction,
422 		   resp, (unsigned long) ies_len, dbg);
423 #ifdef CONFIG_TESTING_OPTIONS
424 #ifdef CONFIG_SAE
425 	if (hapd->conf->sae_confirm_immediate == 2 &&
426 	    auth_alg == WLAN_AUTH_SAE) {
427 		if (auth_transaction == 1 && sta &&
428 		    (resp == WLAN_STATUS_SUCCESS ||
429 		     resp == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
430 		     resp == WLAN_STATUS_SAE_PK)) {
431 			wpa_printf(MSG_DEBUG,
432 				   "TESTING: Postpone SAE Commit transmission until Confirm is ready");
433 			os_free(sta->sae_postponed_commit);
434 			sta->sae_postponed_commit = buf;
435 			sta->sae_postponed_commit_len = rlen;
436 			return WLAN_STATUS_SUCCESS;
437 		}
438 
439 		if (auth_transaction == 2 && sta && sta->sae_postponed_commit) {
440 			wpa_printf(MSG_DEBUG,
441 				   "TESTING: Send postponed SAE Commit first, immediately followed by SAE Confirm");
442 			if (hostapd_drv_send_mlme(hapd,
443 						  sta->sae_postponed_commit,
444 						  sta->sae_postponed_commit_len,
445 						  0, NULL, 0, 0) < 0)
446 				wpa_printf(MSG_INFO, "send_auth_reply: send failed");
447 			os_free(sta->sae_postponed_commit);
448 			sta->sae_postponed_commit = NULL;
449 			sta->sae_postponed_commit_len = 0;
450 		}
451 	}
452 #endif /* CONFIG_SAE */
453 #endif /* CONFIG_TESTING_OPTIONS */
454 	if (hostapd_drv_send_mlme(hapd, reply, rlen, 0, NULL, 0, 0) < 0)
455 		wpa_printf(MSG_INFO, "send_auth_reply: send failed");
456 	else
457 		reply_res = WLAN_STATUS_SUCCESS;
458 
459 	os_free(buf);
460 
461 	return reply_res;
462 }
463 
464 
465 #ifdef CONFIG_IEEE80211R_AP
handle_auth_ft_finish(void * ctx,const u8 * dst,const u8 * bssid,u16 auth_transaction,u16 status,const u8 * ies,size_t ies_len)466 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
467 				  u16 auth_transaction, u16 status,
468 				  const u8 *ies, size_t ies_len)
469 {
470 	struct hostapd_data *hapd = ctx;
471 	struct sta_info *sta;
472 	int reply_res;
473 
474 	reply_res = send_auth_reply(hapd, NULL, dst, bssid, WLAN_AUTH_FT,
475 				    auth_transaction, status, ies, ies_len,
476 				    "auth-ft-finish");
477 
478 	sta = ap_get_sta(hapd, dst);
479 	if (sta == NULL)
480 		return;
481 
482 	if (sta->added_unassoc && (reply_res != WLAN_STATUS_SUCCESS ||
483 				   status != WLAN_STATUS_SUCCESS)) {
484 		hostapd_drv_sta_remove(hapd, sta->addr);
485 		sta->added_unassoc = 0;
486 		return;
487 	}
488 
489 	if (status != WLAN_STATUS_SUCCESS)
490 		return;
491 
492 	hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
493 		       HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
494 	sta->flags |= WLAN_STA_AUTH;
495 	mlme_authenticate_indication(hapd, sta);
496 }
497 #endif /* CONFIG_IEEE80211R_AP */
498 
499 
500 #ifdef CONFIG_SAE
501 
sae_set_state(struct sta_info * sta,enum sae_state state,const char * reason)502 static void sae_set_state(struct sta_info *sta, enum sae_state state,
503 			  const char *reason)
504 {
505 	wpa_printf(MSG_DEBUG, "SAE: State %s -> %s for peer " MACSTR " (%s)",
506 		   sae_state_txt(sta->sae->state), sae_state_txt(state),
507 		   MAC2STR(sta->addr), reason);
508 	sta->sae->state = state;
509 }
510 
511 
sae_get_password(struct hostapd_data * hapd,struct sta_info * sta,const char * rx_id,struct sae_password_entry ** pw_entry,struct sae_pt ** s_pt,const struct sae_pk ** s_pk)512 static const char * sae_get_password(struct hostapd_data *hapd,
513 				     struct sta_info *sta,
514 				     const char *rx_id,
515 				     struct sae_password_entry **pw_entry,
516 				     struct sae_pt **s_pt,
517 				     const struct sae_pk **s_pk)
518 {
519 	const char *password = NULL;
520 	struct sae_password_entry *pw;
521 	struct sae_pt *pt = NULL;
522 	const struct sae_pk *pk = NULL;
523 	struct hostapd_sta_wpa_psk_short *psk = NULL;
524 
525 	for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
526 		if (!is_broadcast_ether_addr(pw->peer_addr) &&
527 		    os_memcmp(pw->peer_addr, sta->addr, ETH_ALEN) != 0)
528 			continue;
529 		if ((rx_id && !pw->identifier) || (!rx_id && pw->identifier))
530 			continue;
531 		if (rx_id && pw->identifier &&
532 		    os_strcmp(rx_id, pw->identifier) != 0)
533 			continue;
534 		password = pw->password;
535 		pt = pw->pt;
536 		if (!(hapd->conf->mesh & MESH_ENABLED))
537 			pk = pw->pk;
538 		break;
539 	}
540 	if (!password) {
541 		password = hapd->conf->ssid.wpa_passphrase;
542 		pt = hapd->conf->ssid.pt;
543 	}
544 
545 	if (!password) {
546 		for (psk = sta->psk; psk; psk = psk->next) {
547 			if (psk->is_passphrase) {
548 				password = psk->passphrase;
549 				break;
550 			}
551 		}
552 	}
553 
554 	if (pw_entry)
555 		*pw_entry = pw;
556 	if (s_pt)
557 		*s_pt = pt;
558 	if (s_pk)
559 		*s_pk = pk;
560 
561 	return password;
562 }
563 
564 
auth_build_sae_commit(struct hostapd_data * hapd,struct sta_info * sta,int update,int status_code)565 static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
566 					     struct sta_info *sta, int update,
567 					     int status_code)
568 {
569 	struct wpabuf *buf;
570 	const char *password = NULL;
571 	struct sae_password_entry *pw;
572 	const char *rx_id = NULL;
573 	int use_pt = 0;
574 	struct sae_pt *pt = NULL;
575 	const struct sae_pk *pk = NULL;
576 
577 	if (sta->sae->tmp) {
578 		rx_id = sta->sae->tmp->pw_id;
579 		use_pt = sta->sae->h2e;
580 #ifdef CONFIG_SAE_PK
581 		os_memcpy(sta->sae->tmp->own_addr, hapd->own_addr, ETH_ALEN);
582 		os_memcpy(sta->sae->tmp->peer_addr, sta->addr, ETH_ALEN);
583 #endif /* CONFIG_SAE_PK */
584 	}
585 
586 	if (rx_id && hapd->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
587 		use_pt = 1;
588 	else if (status_code == WLAN_STATUS_SUCCESS)
589 		use_pt = 0;
590 	else if (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
591 		 status_code == WLAN_STATUS_SAE_PK)
592 		use_pt = 1;
593 
594 	password = sae_get_password(hapd, sta, rx_id, &pw, &pt, &pk);
595 	if (!password || (use_pt && !pt)) {
596 		wpa_printf(MSG_DEBUG, "SAE: No password available");
597 		return NULL;
598 	}
599 
600 	if (update && use_pt &&
601 	    sae_prepare_commit_pt(sta->sae, pt, hapd->own_addr, sta->addr,
602 				  NULL, pk) < 0)
603 		return NULL;
604 
605 	if (update && !use_pt &&
606 	    sae_prepare_commit(hapd->own_addr, sta->addr,
607 			       (u8 *) password, os_strlen(password),
608 			       sta->sae) < 0) {
609 		wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
610 		return NULL;
611 	}
612 
613 	if (pw && pw->vlan_id) {
614 		if (!sta->sae->tmp) {
615 			wpa_printf(MSG_INFO,
616 				   "SAE: No temporary data allocated - cannot store VLAN ID");
617 			return NULL;
618 		}
619 		sta->sae->tmp->vlan_id = pw->vlan_id;
620 	}
621 
622 	buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN +
623 			   (rx_id ? 3 + os_strlen(rx_id) : 0));
624 	if (buf &&
625 	    sae_write_commit(sta->sae, buf, sta->sae->tmp ?
626 			     sta->sae->tmp->anti_clogging_token : NULL,
627 			     rx_id) < 0) {
628 		wpabuf_free(buf);
629 		buf = NULL;
630 	}
631 
632 	return buf;
633 }
634 
635 
auth_build_sae_confirm(struct hostapd_data * hapd,struct sta_info * sta)636 static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
637 					      struct sta_info *sta)
638 {
639 	struct wpabuf *buf;
640 
641 	buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
642 	if (buf == NULL)
643 		return NULL;
644 
645 #ifdef CONFIG_SAE_PK
646 #ifdef CONFIG_TESTING_OPTIONS
647 	if (sta->sae->tmp)
648 		sta->sae->tmp->omit_pk_elem = hapd->conf->sae_pk_omit;
649 #endif /* CONFIG_TESTING_OPTIONS */
650 #endif /* CONFIG_SAE_PK */
651 
652 	if (sae_write_confirm(sta->sae, buf) < 0) {
653 		wpabuf_free(buf);
654 		return NULL;
655 	}
656 
657 	return buf;
658 }
659 
660 
auth_sae_send_commit(struct hostapd_data * hapd,struct sta_info * sta,const u8 * bssid,int update,int status_code)661 static int auth_sae_send_commit(struct hostapd_data *hapd,
662 				struct sta_info *sta,
663 				const u8 *bssid, int update, int status_code)
664 {
665 	struct wpabuf *data;
666 	int reply_res;
667 	u16 status;
668 
669 	data = auth_build_sae_commit(hapd, sta, update, status_code);
670 	if (!data && sta->sae->tmp && sta->sae->tmp->pw_id)
671 		return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
672 	if (data == NULL)
673 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
674 
675 	if (sta->sae->tmp && sta->sae->pk)
676 		status = WLAN_STATUS_SAE_PK;
677 	else if (sta->sae->tmp && sta->sae->h2e)
678 		status = WLAN_STATUS_SAE_HASH_TO_ELEMENT;
679 	else
680 		status = WLAN_STATUS_SUCCESS;
681 #ifdef CONFIG_TESTING_OPTIONS
682 	if (hapd->conf->sae_commit_status >= 0 &&
683 	    hapd->conf->sae_commit_status != status) {
684 		wpa_printf(MSG_INFO,
685 			   "TESTING: Override SAE commit status code %u --> %d",
686 			   status, hapd->conf->sae_commit_status);
687 		status = hapd->conf->sae_commit_status;
688 	}
689 #endif /* CONFIG_TESTING_OPTIONS */
690 	reply_res = send_auth_reply(hapd, sta, sta->addr, bssid,
691 				    WLAN_AUTH_SAE, 1,
692 				    status, wpabuf_head(data),
693 				    wpabuf_len(data), "sae-send-commit");
694 
695 	wpabuf_free(data);
696 
697 	return reply_res;
698 }
699 
700 
auth_sae_send_confirm(struct hostapd_data * hapd,struct sta_info * sta,const u8 * bssid)701 static int auth_sae_send_confirm(struct hostapd_data *hapd,
702 				 struct sta_info *sta,
703 				 const u8 *bssid)
704 {
705 	struct wpabuf *data;
706 	int reply_res;
707 
708 	data = auth_build_sae_confirm(hapd, sta);
709 	if (data == NULL)
710 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
711 
712 	reply_res = send_auth_reply(hapd, sta, sta->addr, bssid,
713 				    WLAN_AUTH_SAE, 2,
714 				    WLAN_STATUS_SUCCESS, wpabuf_head(data),
715 				    wpabuf_len(data), "sae-send-confirm");
716 
717 	wpabuf_free(data);
718 
719 	return reply_res;
720 }
721 
722 #endif /* CONFIG_SAE */
723 
724 
725 #if defined(CONFIG_SAE) || defined(CONFIG_PASN)
726 
use_anti_clogging(struct hostapd_data * hapd)727 static int use_anti_clogging(struct hostapd_data *hapd)
728 {
729 	struct sta_info *sta;
730 	unsigned int open = 0;
731 
732 	if (hapd->conf->anti_clogging_threshold == 0)
733 		return 1;
734 
735 	for (sta = hapd->sta_list; sta; sta = sta->next) {
736 #ifdef CONFIG_SAE
737 		if (sta->sae &&
738 		    (sta->sae->state == SAE_COMMITTED ||
739 		     sta->sae->state == SAE_CONFIRMED))
740 			open++;
741 #endif /* CONFIG_SAE */
742 #ifdef CONFIG_PASN
743 		if (sta->pasn && sta->pasn->ecdh)
744 			open++;
745 #endif /* CONFIG_PASN */
746 		if (open >= hapd->conf->anti_clogging_threshold)
747 			return 1;
748 	}
749 
750 #ifdef CONFIG_SAE
751 	/* In addition to already existing open SAE sessions, check whether
752 	 * there are enough pending commit messages in the processing queue to
753 	 * potentially result in too many open sessions. */
754 	if (open + dl_list_len(&hapd->sae_commit_queue) >=
755 	    hapd->conf->anti_clogging_threshold)
756 		return 1;
757 #endif /* CONFIG_SAE */
758 
759 	return 0;
760 }
761 
762 #endif /* defined(CONFIG_SAE) || defined(CONFIG_PASN) */
763 
764 
765 #ifdef CONFIG_SAE
766 
sae_check_big_sync(struct hostapd_data * hapd,struct sta_info * sta)767 static int sae_check_big_sync(struct hostapd_data *hapd, struct sta_info *sta)
768 {
769 	if (sta->sae->sync > hapd->conf->sae_sync) {
770 		sae_set_state(sta, SAE_NOTHING, "Sync > dot11RSNASAESync");
771 		sta->sae->sync = 0;
772 		return -1;
773 	}
774 	return 0;
775 }
776 
777 
auth_sae_retransmit_timer(void * eloop_ctx,void * eloop_data)778 static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
779 {
780 	struct hostapd_data *hapd = eloop_ctx;
781 	struct sta_info *sta = eloop_data;
782 	int ret;
783 
784 	if (sae_check_big_sync(hapd, sta))
785 		return;
786 	sta->sae->sync++;
787 	wpa_printf(MSG_DEBUG, "SAE: Auth SAE retransmit timer for " MACSTR
788 		   " (sync=%d state=%s)",
789 		   MAC2STR(sta->addr), sta->sae->sync,
790 		   sae_state_txt(sta->sae->state));
791 
792 	switch (sta->sae->state) {
793 	case SAE_COMMITTED:
794 		ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0, -1);
795 		eloop_register_timeout(0,
796 				       hapd->dot11RSNASAERetransPeriod * 1000,
797 				       auth_sae_retransmit_timer, hapd, sta);
798 		break;
799 	case SAE_CONFIRMED:
800 		ret = auth_sae_send_confirm(hapd, sta, hapd->own_addr);
801 		eloop_register_timeout(0,
802 				       hapd->dot11RSNASAERetransPeriod * 1000,
803 				       auth_sae_retransmit_timer, hapd, sta);
804 		break;
805 	default:
806 		ret = -1;
807 		break;
808 	}
809 
810 	if (ret != WLAN_STATUS_SUCCESS)
811 		wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
812 }
813 
814 
sae_clear_retransmit_timer(struct hostapd_data * hapd,struct sta_info * sta)815 void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
816 {
817 	eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
818 }
819 
820 
sae_set_retransmit_timer(struct hostapd_data * hapd,struct sta_info * sta)821 static void sae_set_retransmit_timer(struct hostapd_data *hapd,
822 				     struct sta_info *sta)
823 {
824 	if (!(hapd->conf->mesh & MESH_ENABLED))
825 		return;
826 
827 	eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
828 	eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
829 			       auth_sae_retransmit_timer, hapd, sta);
830 }
831 
832 
sae_sme_send_external_auth_status(struct hostapd_data * hapd,struct sta_info * sta,u16 status)833 static void sae_sme_send_external_auth_status(struct hostapd_data *hapd,
834 					      struct sta_info *sta, u16 status)
835 {
836 	struct external_auth params;
837 
838 	os_memset(&params, 0, sizeof(params));
839 	params.status = status;
840 	params.bssid = sta->addr;
841 	if (status == WLAN_STATUS_SUCCESS && sta->sae &&
842 	    !hapd->conf->disable_pmksa_caching)
843 		params.pmkid = sta->sae->pmkid;
844 
845 	hostapd_drv_send_external_auth_status(hapd, &params);
846 }
847 
848 
sae_accept_sta(struct hostapd_data * hapd,struct sta_info * sta)849 void sae_accept_sta(struct hostapd_data *hapd, struct sta_info *sta)
850 {
851 #ifndef CONFIG_NO_VLAN
852 	struct vlan_description vlan_desc;
853 
854 	if (sta->sae->tmp && sta->sae->tmp->vlan_id > 0) {
855 		wpa_printf(MSG_DEBUG, "SAE: Assign STA " MACSTR
856 			   " to VLAN ID %d",
857 			   MAC2STR(sta->addr), sta->sae->tmp->vlan_id);
858 
859 		os_memset(&vlan_desc, 0, sizeof(vlan_desc));
860 		vlan_desc.notempty = 1;
861 		vlan_desc.untagged = sta->sae->tmp->vlan_id;
862 		if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
863 			wpa_printf(MSG_INFO,
864 				   "Invalid VLAN ID %d in sae_password",
865 				   sta->sae->tmp->vlan_id);
866 			return;
867 		}
868 
869 		if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0 ||
870 		    ap_sta_bind_vlan(hapd, sta) < 0) {
871 			wpa_printf(MSG_INFO,
872 				   "Failed to assign VLAN ID %d from sae_password to "
873 				   MACSTR, sta->sae->tmp->vlan_id,
874 				   MAC2STR(sta->addr));
875 			return;
876 		}
877 	}
878 #endif /* CONFIG_NO_VLAN */
879 
880 	sta->flags |= WLAN_STA_AUTH;
881 	sta->auth_alg = WLAN_AUTH_SAE;
882 	mlme_authenticate_indication(hapd, sta);
883 	wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
884 	sae_set_state(sta, SAE_ACCEPTED, "Accept Confirm");
885 	crypto_bignum_deinit(sta->sae->peer_commit_scalar_accepted, 0);
886 	sta->sae->peer_commit_scalar_accepted = sta->sae->peer_commit_scalar;
887 	sta->sae->peer_commit_scalar = NULL;
888 	wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
889 			       sta->sae->pmk, sta->sae->pmk_len,
890 			       sta->sae->pmkid, sta->sae->akmp);
891 	sae_sme_send_external_auth_status(hapd, sta, WLAN_STATUS_SUCCESS);
892 }
893 
894 
sae_sm_step(struct hostapd_data * hapd,struct sta_info * sta,const u8 * bssid,u16 auth_transaction,u16 status_code,int allow_reuse,int * sta_removed)895 static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
896 		       const u8 *bssid, u16 auth_transaction, u16 status_code,
897 		       int allow_reuse, int *sta_removed)
898 {
899 	int ret;
900 
901 	*sta_removed = 0;
902 
903 	if (auth_transaction != 1 && auth_transaction != 2)
904 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
905 
906 	wpa_printf(MSG_DEBUG, "SAE: Peer " MACSTR " state=%s auth_trans=%u",
907 		   MAC2STR(sta->addr), sae_state_txt(sta->sae->state),
908 		   auth_transaction);
909 	switch (sta->sae->state) {
910 	case SAE_NOTHING:
911 		if (auth_transaction == 1) {
912 			if (sta->sae->tmp) {
913 				sta->sae->h2e =
914 					(status_code ==
915 					 WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
916 					 status_code == WLAN_STATUS_SAE_PK);
917 				sta->sae->pk =
918 					status_code == WLAN_STATUS_SAE_PK;
919 			}
920 			ret = auth_sae_send_commit(hapd, sta, bssid,
921 						   !allow_reuse, status_code);
922 			if (ret)
923 				return ret;
924 			sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
925 
926 			if (sae_process_commit(sta->sae) < 0)
927 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
928 
929 			/*
930 			 * In mesh case, both Commit and Confirm are sent
931 			 * immediately. In infrastructure BSS, by default, only
932 			 * a single Authentication frame (Commit) is expected
933 			 * from the AP here and the second one (Confirm) will
934 			 * be sent once the STA has sent its second
935 			 * Authentication frame (Confirm). This behavior can be
936 			 * overridden with explicit configuration so that the
937 			 * infrastructure BSS case sends both frames together.
938 			 */
939 			if ((hapd->conf->mesh & MESH_ENABLED) ||
940 			    hapd->conf->sae_confirm_immediate) {
941 				/*
942 				 * Send both Commit and Confirm immediately
943 				 * based on SAE finite state machine
944 				 * Nothing -> Confirm transition.
945 				 */
946 				ret = auth_sae_send_confirm(hapd, sta, bssid);
947 				if (ret)
948 					return ret;
949 				sae_set_state(sta, SAE_CONFIRMED,
950 					      "Sent Confirm (mesh)");
951 			} else {
952 				/*
953 				 * For infrastructure BSS, send only the Commit
954 				 * message now to get alternating sequence of
955 				 * Authentication frames between the AP and STA.
956 				 * Confirm will be sent in
957 				 * Committed -> Confirmed/Accepted transition
958 				 * when receiving Confirm from STA.
959 				 */
960 			}
961 			sta->sae->sync = 0;
962 			sae_set_retransmit_timer(hapd, sta);
963 		} else {
964 			hostapd_logger(hapd, sta->addr,
965 				       HOSTAPD_MODULE_IEEE80211,
966 				       HOSTAPD_LEVEL_DEBUG,
967 				       "SAE confirm before commit");
968 		}
969 		break;
970 	case SAE_COMMITTED:
971 		sae_clear_retransmit_timer(hapd, sta);
972 		if (auth_transaction == 1) {
973 			if (sae_process_commit(sta->sae) < 0)
974 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
975 
976 			ret = auth_sae_send_confirm(hapd, sta, bssid);
977 			if (ret)
978 				return ret;
979 			sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
980 			sta->sae->sync = 0;
981 			sae_set_retransmit_timer(hapd, sta);
982 		} else if (hapd->conf->mesh & MESH_ENABLED) {
983 			/*
984 			 * In mesh case, follow SAE finite state machine and
985 			 * send Commit now, if sync count allows.
986 			 */
987 			if (sae_check_big_sync(hapd, sta))
988 				return WLAN_STATUS_SUCCESS;
989 			sta->sae->sync++;
990 
991 			ret = auth_sae_send_commit(hapd, sta, bssid, 0,
992 						   status_code);
993 			if (ret)
994 				return ret;
995 
996 			sae_set_retransmit_timer(hapd, sta);
997 		} else {
998 			/*
999 			 * For instructure BSS, send the postponed Confirm from
1000 			 * Nothing -> Confirmed transition that was reduced to
1001 			 * Nothing -> Committed above.
1002 			 */
1003 			ret = auth_sae_send_confirm(hapd, sta, bssid);
1004 			if (ret)
1005 				return ret;
1006 
1007 			sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
1008 
1009 			/*
1010 			 * Since this was triggered on Confirm RX, run another
1011 			 * step to get to Accepted without waiting for
1012 			 * additional events.
1013 			 */
1014 			return sae_sm_step(hapd, sta, bssid, auth_transaction,
1015 					   WLAN_STATUS_SUCCESS, 0, sta_removed);
1016 		}
1017 		break;
1018 	case SAE_CONFIRMED:
1019 		sae_clear_retransmit_timer(hapd, sta);
1020 		if (auth_transaction == 1) {
1021 			if (sae_check_big_sync(hapd, sta))
1022 				return WLAN_STATUS_SUCCESS;
1023 			sta->sae->sync++;
1024 
1025 			ret = auth_sae_send_commit(hapd, sta, bssid, 1,
1026 						   status_code);
1027 			if (ret)
1028 				return ret;
1029 
1030 			if (sae_process_commit(sta->sae) < 0)
1031 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
1032 
1033 			ret = auth_sae_send_confirm(hapd, sta, bssid);
1034 			if (ret)
1035 				return ret;
1036 
1037 			sae_set_retransmit_timer(hapd, sta);
1038 		} else {
1039 			sta->sae->send_confirm = 0xffff;
1040 			sae_accept_sta(hapd, sta);
1041 		}
1042 		break;
1043 	case SAE_ACCEPTED:
1044 		if (auth_transaction == 1 &&
1045 		    (hapd->conf->mesh & MESH_ENABLED)) {
1046 			wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
1047 				   ") doing reauthentication",
1048 				   MAC2STR(sta->addr));
1049 			wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1050 			ap_free_sta(hapd, sta);
1051 			*sta_removed = 1;
1052 		} else if (auth_transaction == 1) {
1053 			wpa_printf(MSG_DEBUG, "SAE: Start reauthentication");
1054 			ret = auth_sae_send_commit(hapd, sta, bssid, 1,
1055 						   status_code);
1056 			if (ret)
1057 				return ret;
1058 			sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
1059 
1060 			if (sae_process_commit(sta->sae) < 0)
1061 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
1062 			sta->sae->sync = 0;
1063 			sae_set_retransmit_timer(hapd, sta);
1064 		} else {
1065 			if (sae_check_big_sync(hapd, sta))
1066 				return WLAN_STATUS_SUCCESS;
1067 			sta->sae->sync++;
1068 
1069 			ret = auth_sae_send_confirm(hapd, sta, bssid);
1070 			sae_clear_temp_data(sta->sae);
1071 			if (ret)
1072 				return ret;
1073 		}
1074 		break;
1075 	default:
1076 		wpa_printf(MSG_ERROR, "SAE: invalid state %d",
1077 			   sta->sae->state);
1078 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1079 	}
1080 	return WLAN_STATUS_SUCCESS;
1081 }
1082 
1083 
sae_pick_next_group(struct hostapd_data * hapd,struct sta_info * sta)1084 static void sae_pick_next_group(struct hostapd_data *hapd, struct sta_info *sta)
1085 {
1086 	struct sae_data *sae = sta->sae;
1087 	int i, *groups = hapd->conf->sae_groups;
1088 	int default_groups[] = { 19, 0 };
1089 
1090 	if (sae->state != SAE_COMMITTED)
1091 		return;
1092 
1093 	wpa_printf(MSG_DEBUG, "SAE: Previously selected group: %d", sae->group);
1094 
1095 	if (!groups)
1096 		groups = default_groups;
1097 	for (i = 0; groups[i] > 0; i++) {
1098 		if (sae->group == groups[i])
1099 			break;
1100 	}
1101 
1102 	if (groups[i] <= 0) {
1103 		wpa_printf(MSG_DEBUG,
1104 			   "SAE: Previously selected group not found from the current configuration");
1105 		return;
1106 	}
1107 
1108 	for (;;) {
1109 		i++;
1110 		if (groups[i] <= 0) {
1111 			wpa_printf(MSG_DEBUG,
1112 				   "SAE: No alternative group enabled");
1113 			return;
1114 		}
1115 
1116 		if (sae_set_group(sae, groups[i]) < 0)
1117 			continue;
1118 
1119 		break;
1120 	}
1121 	wpa_printf(MSG_DEBUG, "SAE: Selected new group: %d", groups[i]);
1122 }
1123 
1124 
sae_status_success(struct hostapd_data * hapd,u16 status_code)1125 static int sae_status_success(struct hostapd_data *hapd, u16 status_code)
1126 {
1127 	enum sae_pwe sae_pwe = hapd->conf->sae_pwe;
1128 	int id_in_use;
1129 	bool sae_pk = false;
1130 
1131 	id_in_use = hostapd_sae_pw_id_in_use(hapd->conf);
1132 	if (id_in_use == 2 && sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
1133 		sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
1134 	else if (id_in_use == 1 && sae_pwe == SAE_PWE_HUNT_AND_PECK)
1135 		sae_pwe = SAE_PWE_BOTH;
1136 #ifdef CONFIG_SAE_PK
1137 	sae_pk = hostapd_sae_pk_in_use(hapd->conf);
1138 	if (sae_pwe == SAE_PWE_HUNT_AND_PECK && sae_pk)
1139 		sae_pwe = SAE_PWE_BOTH;
1140 #endif /* CONFIG_SAE_PK */
1141 	if (sae_pwe == SAE_PWE_HUNT_AND_PECK &&
1142 	    (hapd->conf->wpa_key_mgmt &
1143 	     (WPA_KEY_MGMT_SAE_EXT_KEY | WPA_KEY_MGMT_FT_SAE_EXT_KEY)))
1144 		sae_pwe = SAE_PWE_BOTH;
1145 
1146 	return ((sae_pwe == SAE_PWE_HUNT_AND_PECK ||
1147 		 sae_pwe == SAE_PWE_FORCE_HUNT_AND_PECK) &&
1148 		status_code == WLAN_STATUS_SUCCESS) ||
1149 		(sae_pwe == SAE_PWE_HASH_TO_ELEMENT &&
1150 		 (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1151 		  (sae_pk && status_code == WLAN_STATUS_SAE_PK))) ||
1152 		(sae_pwe == SAE_PWE_BOTH &&
1153 		 (status_code == WLAN_STATUS_SUCCESS ||
1154 		  status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1155 		  (sae_pk && status_code == WLAN_STATUS_SAE_PK)));
1156 }
1157 
1158 
sae_is_group_enabled(struct hostapd_data * hapd,int group)1159 static int sae_is_group_enabled(struct hostapd_data *hapd, int group)
1160 {
1161 	int *groups = hapd->conf->sae_groups;
1162 	int default_groups[] = { 19, 0 };
1163 	int i;
1164 
1165 	if (!groups)
1166 		groups = default_groups;
1167 
1168 	for (i = 0; groups[i] > 0; i++) {
1169 		if (groups[i] == group)
1170 			return 1;
1171 	}
1172 
1173 	return 0;
1174 }
1175 
1176 
check_sae_rejected_groups(struct hostapd_data * hapd,struct sae_data * sae)1177 static int check_sae_rejected_groups(struct hostapd_data *hapd,
1178 				     struct sae_data *sae)
1179 {
1180 	const struct wpabuf *groups;
1181 	size_t i, count;
1182 	const u8 *pos;
1183 
1184 	if (!sae->tmp)
1185 		return 0;
1186 	groups = sae->tmp->peer_rejected_groups;
1187 	if (!groups)
1188 		return 0;
1189 
1190 	pos = wpabuf_head(groups);
1191 	count = wpabuf_len(groups) / 2;
1192 	for (i = 0; i < count; i++) {
1193 		int enabled;
1194 		u16 group;
1195 
1196 		group = WPA_GET_LE16(pos);
1197 		pos += 2;
1198 		enabled = sae_is_group_enabled(hapd, group);
1199 		wpa_printf(MSG_DEBUG, "SAE: Rejected group %u is %s",
1200 			   group, enabled ? "enabled" : "disabled");
1201 		if (enabled)
1202 			return 1;
1203 	}
1204 
1205 	return 0;
1206 }
1207 
1208 
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)1209 static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
1210 			    const struct ieee80211_mgmt *mgmt, size_t len,
1211 			    u16 auth_transaction, u16 status_code)
1212 {
1213 	int resp = WLAN_STATUS_SUCCESS;
1214 	struct wpabuf *data = NULL;
1215 	int *groups = hapd->conf->sae_groups;
1216 	int default_groups[] = { 19, 0 };
1217 	const u8 *pos, *end;
1218 	int sta_removed = 0;
1219 	bool success_status;
1220 
1221 	if (!groups)
1222 		groups = default_groups;
1223 
1224 #ifdef CONFIG_TESTING_OPTIONS
1225 	if (hapd->conf->sae_reflection_attack && auth_transaction == 1) {
1226 		wpa_printf(MSG_DEBUG, "SAE: TESTING - reflection attack");
1227 		pos = mgmt->u.auth.variable;
1228 		end = ((const u8 *) mgmt) + len;
1229 		resp = status_code;
1230 		send_auth_reply(hapd, sta, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
1231 				auth_transaction, resp, pos, end - pos,
1232 				"auth-sae-reflection-attack");
1233 		goto remove_sta;
1234 	}
1235 
1236 	if (hapd->conf->sae_commit_override && auth_transaction == 1) {
1237 		wpa_printf(MSG_DEBUG, "SAE: TESTING - commit override");
1238 		send_auth_reply(hapd, sta, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
1239 				auth_transaction, resp,
1240 				wpabuf_head(hapd->conf->sae_commit_override),
1241 				wpabuf_len(hapd->conf->sae_commit_override),
1242 				"sae-commit-override");
1243 		goto remove_sta;
1244 	}
1245 #endif /* CONFIG_TESTING_OPTIONS */
1246 	if (!sta->sae) {
1247 		if (auth_transaction != 1 ||
1248 		    !sae_status_success(hapd, status_code)) {
1249 			wpa_printf(MSG_DEBUG, "SAE: Unexpected Status Code %u",
1250 				   status_code);
1251 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1252 			goto reply;
1253 		}
1254 		sta->sae = os_zalloc(sizeof(*sta->sae));
1255 		if (!sta->sae) {
1256 			resp = -1;
1257 			goto remove_sta;
1258 		}
1259 		sae_set_state(sta, SAE_NOTHING, "Init");
1260 		sta->sae->sync = 0;
1261 	}
1262 
1263 	if (sta->mesh_sae_pmksa_caching) {
1264 		wpa_printf(MSG_DEBUG,
1265 			   "SAE: Cancel use of mesh PMKSA caching because peer starts SAE authentication");
1266 		wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1267 		sta->mesh_sae_pmksa_caching = 0;
1268 	}
1269 
1270 	if (auth_transaction == 1) {
1271 		const u8 *token = NULL;
1272 		size_t token_len = 0;
1273 		int allow_reuse = 0;
1274 
1275 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1276 			       HOSTAPD_LEVEL_DEBUG,
1277 			       "start SAE authentication (RX commit, status=%u (%s))",
1278 			       status_code, status2str(status_code));
1279 
1280 		if ((hapd->conf->mesh & MESH_ENABLED) &&
1281 		    status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
1282 		    sta->sae->tmp) {
1283 			pos = mgmt->u.auth.variable;
1284 			end = ((const u8 *) mgmt) + len;
1285 			if (pos + sizeof(le16) > end) {
1286 				wpa_printf(MSG_ERROR,
1287 					   "SAE: Too short anti-clogging token request");
1288 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1289 				goto reply;
1290 			}
1291 			resp = sae_group_allowed(sta->sae, groups,
1292 						 WPA_GET_LE16(pos));
1293 			if (resp != WLAN_STATUS_SUCCESS) {
1294 				wpa_printf(MSG_ERROR,
1295 					   "SAE: Invalid group in anti-clogging token request");
1296 				goto reply;
1297 			}
1298 			pos += sizeof(le16);
1299 
1300 			wpabuf_free(sta->sae->tmp->anti_clogging_token);
1301 			sta->sae->tmp->anti_clogging_token =
1302 				wpabuf_alloc_copy(pos, end - pos);
1303 			if (sta->sae->tmp->anti_clogging_token == NULL) {
1304 				wpa_printf(MSG_ERROR,
1305 					   "SAE: Failed to alloc for anti-clogging token");
1306 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1307 				goto remove_sta;
1308 			}
1309 
1310 			/*
1311 			 * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
1312 			 * is 76, a new Commit Message shall be constructed
1313 			 * with the Anti-Clogging Token from the received
1314 			 * Authentication frame, and the commit-scalar and
1315 			 * COMMIT-ELEMENT previously sent.
1316 			 */
1317 			resp = auth_sae_send_commit(hapd, sta, mgmt->bssid, 0,
1318 						    status_code);
1319 			if (resp != WLAN_STATUS_SUCCESS) {
1320 				wpa_printf(MSG_ERROR,
1321 					   "SAE: Failed to send commit message");
1322 				goto remove_sta;
1323 			}
1324 			sae_set_state(sta, SAE_COMMITTED,
1325 				      "Sent Commit (anti-clogging token case in mesh)");
1326 			sta->sae->sync = 0;
1327 			sae_set_retransmit_timer(hapd, sta);
1328 			return;
1329 		}
1330 
1331 		if ((hapd->conf->mesh & MESH_ENABLED) &&
1332 		    status_code ==
1333 		    WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1334 		    sta->sae->tmp) {
1335 			wpa_printf(MSG_DEBUG,
1336 				   "SAE: Peer did not accept our SAE group");
1337 			sae_pick_next_group(hapd, sta);
1338 			goto remove_sta;
1339 		}
1340 
1341 		if (!sae_status_success(hapd, status_code))
1342 			goto remove_sta;
1343 
1344 		if (!(hapd->conf->mesh & MESH_ENABLED) &&
1345 		    sta->sae->state == SAE_COMMITTED) {
1346 			/* This is needed in the infrastructure BSS case to
1347 			 * address a sequence where a STA entry may remain in
1348 			 * hostapd across two attempts to do SAE authentication
1349 			 * by the same STA. The second attempt may end up trying
1350 			 * to use a different group and that would not be
1351 			 * allowed if we remain in Committed state with the
1352 			 * previously set parameters. */
1353 			pos = mgmt->u.auth.variable;
1354 			end = ((const u8 *) mgmt) + len;
1355 			if (end - pos >= (int) sizeof(le16) &&
1356 			    sae_group_allowed(sta->sae, groups,
1357 					      WPA_GET_LE16(pos)) ==
1358 			    WLAN_STATUS_SUCCESS) {
1359 				/* Do not waste resources deriving the same PWE
1360 				 * again since the same group is reused. */
1361 				sae_set_state(sta, SAE_NOTHING,
1362 					      "Allow previous PWE to be reused");
1363 				allow_reuse = 1;
1364 			} else {
1365 				sae_set_state(sta, SAE_NOTHING,
1366 					      "Clear existing state to allow restart");
1367 				sae_clear_data(sta->sae);
1368 			}
1369 		}
1370 
1371 		resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
1372 					((const u8 *) mgmt) + len -
1373 					mgmt->u.auth.variable, &token,
1374 					&token_len, groups, status_code ==
1375 					WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1376 					status_code == WLAN_STATUS_SAE_PK,
1377 					NULL);
1378 		if (resp == SAE_SILENTLY_DISCARD) {
1379 			wpa_printf(MSG_DEBUG,
1380 				   "SAE: Drop commit message from " MACSTR " due to reflection attack",
1381 				   MAC2STR(sta->addr));
1382 			goto remove_sta;
1383 		}
1384 
1385 		if (resp == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER) {
1386 			wpa_msg(hapd->msg_ctx, MSG_INFO,
1387 				WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER
1388 				MACSTR, MAC2STR(sta->addr));
1389 			sae_clear_retransmit_timer(hapd, sta);
1390 			sae_set_state(sta, SAE_NOTHING,
1391 				      "Unknown Password Identifier");
1392 			goto remove_sta;
1393 		}
1394 
1395 		if (token &&
1396 		    check_comeback_token(hapd->comeback_key,
1397 					 hapd->comeback_pending_idx, sta->addr,
1398 					 token, token_len)
1399 		    < 0) {
1400 			wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
1401 				   "incorrect token from " MACSTR,
1402 				   MAC2STR(sta->addr));
1403 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1404 			goto remove_sta;
1405 		}
1406 
1407 		if (resp != WLAN_STATUS_SUCCESS)
1408 			goto reply;
1409 
1410 		if (check_sae_rejected_groups(hapd, sta->sae)) {
1411 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1412 			goto reply;
1413 		}
1414 
1415 		if (!token && use_anti_clogging(hapd) && !allow_reuse) {
1416 			int h2e = 0;
1417 
1418 			wpa_printf(MSG_DEBUG,
1419 				   "SAE: Request anti-clogging token from "
1420 				   MACSTR, MAC2STR(sta->addr));
1421 			if (sta->sae->tmp)
1422 				h2e = sta->sae->h2e;
1423 			if (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1424 			    status_code == WLAN_STATUS_SAE_PK)
1425 				h2e = 1;
1426 			data = auth_build_token_req(
1427 				&hapd->last_comeback_key_update,
1428 				hapd->comeback_key,
1429 				hapd->comeback_idx,
1430 				hapd->comeback_pending_idx,
1431 				sizeof(hapd->comeback_pending_idx),
1432 				sta->sae->group,
1433 				sta->addr, h2e);
1434 			resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
1435 			if (hapd->conf->mesh & MESH_ENABLED)
1436 				sae_set_state(sta, SAE_NOTHING,
1437 					      "Request anti-clogging token case in mesh");
1438 			goto reply;
1439 		}
1440 
1441 		resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction,
1442 				   status_code, allow_reuse, &sta_removed);
1443 	} else if (auth_transaction == 2) {
1444 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1445 			       HOSTAPD_LEVEL_DEBUG,
1446 			       "SAE authentication (RX confirm, status=%u (%s))",
1447 			       status_code, status2str(status_code));
1448 		if (status_code != WLAN_STATUS_SUCCESS)
1449 			goto remove_sta;
1450 		if (sta->sae->state >= SAE_CONFIRMED ||
1451 		    !(hapd->conf->mesh & MESH_ENABLED)) {
1452 			const u8 *var;
1453 			size_t var_len;
1454 			u16 peer_send_confirm;
1455 
1456 			var = mgmt->u.auth.variable;
1457 			var_len = ((u8 *) mgmt) + len - mgmt->u.auth.variable;
1458 			if (var_len < 2) {
1459 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1460 				goto reply;
1461 			}
1462 
1463 			peer_send_confirm = WPA_GET_LE16(var);
1464 
1465 			if (sta->sae->state == SAE_ACCEPTED &&
1466 			    (peer_send_confirm <= sta->sae->rc ||
1467 			     peer_send_confirm == 0xffff)) {
1468 				wpa_printf(MSG_DEBUG,
1469 					   "SAE: Silently ignore unexpected Confirm from peer "
1470 					   MACSTR
1471 					   " (peer-send-confirm=%u Rc=%u)",
1472 					   MAC2STR(sta->addr),
1473 					   peer_send_confirm, sta->sae->rc);
1474 				return;
1475 			}
1476 
1477 			if (sae_check_confirm(sta->sae, var, var_len,
1478 					      NULL) < 0) {
1479 				resp = WLAN_STATUS_CHALLENGE_FAIL;
1480 				goto reply;
1481 			}
1482 			sta->sae->rc = peer_send_confirm;
1483 		}
1484 		resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction,
1485 				   status_code, 0, &sta_removed);
1486 	} else {
1487 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1488 			       HOSTAPD_LEVEL_DEBUG,
1489 			       "unexpected SAE authentication transaction %u (status=%u (%s))",
1490 			       auth_transaction, status_code,
1491 			       status2str(status_code));
1492 		if (status_code != WLAN_STATUS_SUCCESS)
1493 			goto remove_sta;
1494 		resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1495 	}
1496 
1497 reply:
1498 	if (!sta_removed && resp != WLAN_STATUS_SUCCESS) {
1499 		pos = mgmt->u.auth.variable;
1500 		end = ((const u8 *) mgmt) + len;
1501 
1502 		/* Copy the Finite Cyclic Group field from the request if we
1503 		 * rejected it as unsupported group. */
1504 		if (resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1505 		    !data && end - pos >= 2)
1506 			data = wpabuf_alloc_copy(pos, 2);
1507 
1508 		sae_sme_send_external_auth_status(hapd, sta, resp);
1509 		send_auth_reply(hapd, sta, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
1510 				auth_transaction, resp,
1511 				data ? wpabuf_head(data) : (u8 *) "",
1512 				data ? wpabuf_len(data) : 0, "auth-sae");
1513 	}
1514 
1515 remove_sta:
1516 	if (auth_transaction == 1)
1517 		success_status = sae_status_success(hapd, status_code);
1518 	else
1519 		success_status = status_code == WLAN_STATUS_SUCCESS;
1520 	if (!sta_removed && sta->added_unassoc &&
1521 	    (resp != WLAN_STATUS_SUCCESS || !success_status)) {
1522 		hostapd_drv_sta_remove(hapd, sta->addr);
1523 		sta->added_unassoc = 0;
1524 	}
1525 	wpabuf_free(data);
1526 }
1527 
1528 
1529 /**
1530  * auth_sae_init_committed - Send COMMIT and start SAE in committed state
1531  * @hapd: BSS data for the device initiating the authentication
1532  * @sta: the peer to which commit authentication frame is sent
1533  *
1534  * This function implements Init event handling (IEEE Std 802.11-2012,
1535  * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
1536  * sta->sae structure should be initialized appropriately via a call to
1537  * sae_prepare_commit().
1538  */
auth_sae_init_committed(struct hostapd_data * hapd,struct sta_info * sta)1539 int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
1540 {
1541 	int ret;
1542 
1543 	if (!sta->sae || !sta->sae->tmp)
1544 		return -1;
1545 
1546 	if (sta->sae->state != SAE_NOTHING)
1547 		return -1;
1548 
1549 	ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0, -1);
1550 	if (ret)
1551 		return -1;
1552 
1553 	sae_set_state(sta, SAE_COMMITTED, "Init and sent commit");
1554 	sta->sae->sync = 0;
1555 	sae_set_retransmit_timer(hapd, sta);
1556 
1557 	return 0;
1558 }
1559 
1560 
auth_sae_process_commit(void * eloop_ctx,void * user_ctx)1561 void auth_sae_process_commit(void *eloop_ctx, void *user_ctx)
1562 {
1563 	struct hostapd_data *hapd = eloop_ctx;
1564 	struct hostapd_sae_commit_queue *q;
1565 	unsigned int queue_len;
1566 
1567 	q = dl_list_first(&hapd->sae_commit_queue,
1568 			  struct hostapd_sae_commit_queue, list);
1569 	if (!q)
1570 		return;
1571 	wpa_printf(MSG_DEBUG,
1572 		   "SAE: Process next available message from queue");
1573 	dl_list_del(&q->list);
1574 	handle_auth(hapd, (const struct ieee80211_mgmt *) q->msg, q->len,
1575 		    q->rssi, 1);
1576 	os_free(q);
1577 
1578 	if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
1579 		return;
1580 	queue_len = dl_list_len(&hapd->sae_commit_queue);
1581 	eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
1582 			       hapd, NULL);
1583 }
1584 
1585 
auth_sae_queue(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int rssi)1586 static void auth_sae_queue(struct hostapd_data *hapd,
1587 			   const struct ieee80211_mgmt *mgmt, size_t len,
1588 			   int rssi)
1589 {
1590 	struct hostapd_sae_commit_queue *q, *q2;
1591 	unsigned int queue_len;
1592 	const struct ieee80211_mgmt *mgmt2;
1593 
1594 	queue_len = dl_list_len(&hapd->sae_commit_queue);
1595 	if (queue_len >= 15) {
1596 		wpa_printf(MSG_DEBUG,
1597 			   "SAE: No more room in message queue - drop the new frame from "
1598 			   MACSTR, MAC2STR(mgmt->sa));
1599 		return;
1600 	}
1601 
1602 	wpa_printf(MSG_DEBUG, "SAE: Queue Authentication message from "
1603 		   MACSTR " for processing (queue_len %u)", MAC2STR(mgmt->sa),
1604 		   queue_len);
1605 	q = os_zalloc(sizeof(*q) + len);
1606 	if (!q)
1607 		return;
1608 	q->rssi = rssi;
1609 	q->len = len;
1610 	os_memcpy(q->msg, mgmt, len);
1611 
1612 	/* Check whether there is already a queued Authentication frame from the
1613 	 * same station with the same transaction number and if so, replace that
1614 	 * queue entry with the new one. This avoids issues with a peer that
1615 	 * sends multiple times (e.g., due to frequent SAE retries). There is no
1616 	 * point in us trying to process the old attempts after a new one has
1617 	 * obsoleted them. */
1618 	dl_list_for_each(q2, &hapd->sae_commit_queue,
1619 			 struct hostapd_sae_commit_queue, list) {
1620 		mgmt2 = (const struct ieee80211_mgmt *) q2->msg;
1621 		if (os_memcmp(mgmt->sa, mgmt2->sa, ETH_ALEN) == 0 &&
1622 		    mgmt->u.auth.auth_transaction ==
1623 		    mgmt2->u.auth.auth_transaction) {
1624 			wpa_printf(MSG_DEBUG,
1625 				   "SAE: Replace queued message from same STA with same transaction number");
1626 			dl_list_add(&q2->list, &q->list);
1627 			dl_list_del(&q2->list);
1628 			os_free(q2);
1629 			goto queued;
1630 		}
1631 	}
1632 
1633 	/* No pending identical entry, so add to the end of the queue */
1634 	dl_list_add_tail(&hapd->sae_commit_queue, &q->list);
1635 
1636 queued:
1637 	if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
1638 		return;
1639 	eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
1640 			       hapd, NULL);
1641 }
1642 
1643 
auth_sae_queued_addr(struct hostapd_data * hapd,const u8 * addr)1644 static int auth_sae_queued_addr(struct hostapd_data *hapd, const u8 *addr)
1645 {
1646 	struct hostapd_sae_commit_queue *q;
1647 	const struct ieee80211_mgmt *mgmt;
1648 
1649 	dl_list_for_each(q, &hapd->sae_commit_queue,
1650 			 struct hostapd_sae_commit_queue, list) {
1651 		mgmt = (const struct ieee80211_mgmt *) q->msg;
1652 		if (os_memcmp(addr, mgmt->sa, ETH_ALEN) == 0)
1653 			return 1;
1654 	}
1655 
1656 	return 0;
1657 }
1658 
1659 #endif /* CONFIG_SAE */
1660 
1661 
wpa_res_to_status_code(enum wpa_validate_result res)1662 static u16 wpa_res_to_status_code(enum wpa_validate_result res)
1663 {
1664 	switch (res) {
1665 	case WPA_IE_OK:
1666 		return WLAN_STATUS_SUCCESS;
1667 	case WPA_INVALID_IE:
1668 		return WLAN_STATUS_INVALID_IE;
1669 	case WPA_INVALID_GROUP:
1670 		return WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1671 	case WPA_INVALID_PAIRWISE:
1672 		return WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1673 	case WPA_INVALID_AKMP:
1674 		return WLAN_STATUS_AKMP_NOT_VALID;
1675 	case WPA_NOT_ENABLED:
1676 		return WLAN_STATUS_INVALID_IE;
1677 	case WPA_ALLOC_FAIL:
1678 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1679 	case WPA_MGMT_FRAME_PROTECTION_VIOLATION:
1680 		return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1681 	case WPA_INVALID_MGMT_GROUP_CIPHER:
1682 		return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
1683 	case WPA_INVALID_MDIE:
1684 		return WLAN_STATUS_INVALID_MDIE;
1685 	case WPA_INVALID_PROTO:
1686 		return WLAN_STATUS_INVALID_IE;
1687 	case WPA_INVALID_PMKID:
1688 		return WLAN_STATUS_INVALID_PMKID;
1689 	case WPA_DENIED_OTHER_REASON:
1690 		return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1691 	}
1692 	return WLAN_STATUS_INVALID_IE;
1693 }
1694 
1695 
1696 #ifdef CONFIG_FILS
1697 
1698 static void handle_auth_fils_finish(struct hostapd_data *hapd,
1699 				    struct sta_info *sta, u16 resp,
1700 				    struct wpabuf *data, int pub);
1701 
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))1702 void handle_auth_fils(struct hostapd_data *hapd, struct sta_info *sta,
1703 		      const u8 *pos, size_t len, u16 auth_alg,
1704 		      u16 auth_transaction, u16 status_code,
1705 		      void (*cb)(struct hostapd_data *hapd,
1706 				 struct sta_info *sta, u16 resp,
1707 				 struct wpabuf *data, int pub))
1708 {
1709 	u16 resp = WLAN_STATUS_SUCCESS;
1710 	const u8 *end;
1711 	struct ieee802_11_elems elems;
1712 	enum wpa_validate_result res;
1713 	struct wpa_ie_data rsn;
1714 	struct rsn_pmksa_cache_entry *pmksa = NULL;
1715 
1716 	if (auth_transaction != 1 || status_code != WLAN_STATUS_SUCCESS)
1717 		return;
1718 
1719 	end = pos + len;
1720 
1721 	wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
1722 		    pos, end - pos);
1723 
1724 	/* TODO: FILS PK */
1725 #ifdef CONFIG_FILS_SK_PFS
1726 	if (auth_alg == WLAN_AUTH_FILS_SK_PFS) {
1727 		u16 group;
1728 		struct wpabuf *pub;
1729 		size_t elem_len;
1730 
1731 		/* Using FILS PFS */
1732 
1733 		/* Finite Cyclic Group */
1734 		if (end - pos < 2) {
1735 			wpa_printf(MSG_DEBUG,
1736 				   "FILS: No room for Finite Cyclic Group");
1737 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1738 			goto fail;
1739 		}
1740 		group = WPA_GET_LE16(pos);
1741 		pos += 2;
1742 		if (group != hapd->conf->fils_dh_group) {
1743 			wpa_printf(MSG_DEBUG,
1744 				   "FILS: Unsupported Finite Cyclic Group: %u (expected %u)",
1745 				   group, hapd->conf->fils_dh_group);
1746 			resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1747 			goto fail;
1748 		}
1749 
1750 		crypto_ecdh_deinit(sta->fils_ecdh);
1751 		sta->fils_ecdh = crypto_ecdh_init(group);
1752 		if (!sta->fils_ecdh) {
1753 			wpa_printf(MSG_INFO,
1754 				   "FILS: Could not initialize ECDH with group %d",
1755 				   group);
1756 			resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1757 			goto fail;
1758 		}
1759 
1760 		pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
1761 		if (!pub) {
1762 			wpa_printf(MSG_DEBUG,
1763 				   "FILS: Failed to derive ECDH public key");
1764 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1765 			goto fail;
1766 		}
1767 		elem_len = wpabuf_len(pub);
1768 		wpabuf_free(pub);
1769 
1770 		/* Element */
1771 		if ((size_t) (end - pos) < elem_len) {
1772 			wpa_printf(MSG_DEBUG, "FILS: No room for Element");
1773 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1774 			goto fail;
1775 		}
1776 
1777 		wpabuf_free(sta->fils_g_sta);
1778 		sta->fils_g_sta = wpabuf_alloc_copy(pos, elem_len);
1779 		wpabuf_clear_free(sta->fils_dh_ss);
1780 		sta->fils_dh_ss = crypto_ecdh_set_peerkey(sta->fils_ecdh, 1,
1781 							  pos, elem_len);
1782 		if (!sta->fils_dh_ss) {
1783 			wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
1784 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1785 			goto fail;
1786 		}
1787 		wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", sta->fils_dh_ss);
1788 		pos += elem_len;
1789 	} else {
1790 		crypto_ecdh_deinit(sta->fils_ecdh);
1791 		sta->fils_ecdh = NULL;
1792 		wpabuf_clear_free(sta->fils_dh_ss);
1793 		sta->fils_dh_ss = NULL;
1794 	}
1795 #endif /* CONFIG_FILS_SK_PFS */
1796 
1797 	wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
1798 	if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
1799 		wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
1800 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1801 		goto fail;
1802 	}
1803 
1804 	/* RSNE */
1805 	wpa_hexdump(MSG_DEBUG, "FILS: RSN element",
1806 		    elems.rsn_ie, elems.rsn_ie_len);
1807 	if (!elems.rsn_ie ||
1808 	    wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
1809 				 &rsn) < 0) {
1810 		wpa_printf(MSG_DEBUG, "FILS: No valid RSN element");
1811 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1812 		goto fail;
1813 	}
1814 
1815 	if (!sta->wpa_sm)
1816 		sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr,
1817 						NULL);
1818 	if (!sta->wpa_sm) {
1819 		wpa_printf(MSG_DEBUG,
1820 			   "FILS: Failed to initialize RSN state machine");
1821 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1822 		goto fail;
1823 	}
1824 
1825 	res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1826 				  hapd->iface->freq,
1827 				  elems.rsn_ie - 2, elems.rsn_ie_len + 2,
1828 				  elems.rsnxe ? elems.rsnxe - 2 : NULL,
1829 				  elems.rsnxe ? elems.rsnxe_len + 2 : 0,
1830 				  elems.mdie, elems.mdie_len, NULL, 0);
1831 	resp = wpa_res_to_status_code(res);
1832 	if (resp != WLAN_STATUS_SUCCESS)
1833 		goto fail;
1834 
1835 	if (!elems.fils_nonce) {
1836 		wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
1837 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1838 		goto fail;
1839 	}
1840 	wpa_hexdump(MSG_DEBUG, "FILS: SNonce", elems.fils_nonce,
1841 		    FILS_NONCE_LEN);
1842 	os_memcpy(sta->fils_snonce, elems.fils_nonce, FILS_NONCE_LEN);
1843 
1844 	/* PMKID List */
1845 	if (rsn.pmkid && rsn.num_pmkid > 0) {
1846 		u8 num;
1847 		const u8 *pmkid;
1848 
1849 		wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
1850 			    rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
1851 
1852 		pmkid = rsn.pmkid;
1853 		num = rsn.num_pmkid;
1854 		while (num) {
1855 			wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN);
1856 			pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr,
1857 						   pmkid);
1858 			if (pmksa)
1859 				break;
1860 			pmksa = wpa_auth_pmksa_get_fils_cache_id(hapd->wpa_auth,
1861 								 sta->addr,
1862 								 pmkid);
1863 			if (pmksa)
1864 				break;
1865 			pmkid += PMKID_LEN;
1866 			num--;
1867 		}
1868 	}
1869 	if (pmksa && wpa_auth_sta_key_mgmt(sta->wpa_sm) != pmksa->akmp) {
1870 		wpa_printf(MSG_DEBUG,
1871 			   "FILS: Matching PMKSA cache entry has different AKMP (0x%x != 0x%x) - ignore",
1872 			   wpa_auth_sta_key_mgmt(sta->wpa_sm), pmksa->akmp);
1873 		pmksa = NULL;
1874 	}
1875 	if (pmksa)
1876 		wpa_printf(MSG_DEBUG, "FILS: Found matching PMKSA cache entry");
1877 
1878 	/* FILS Session */
1879 	if (!elems.fils_session) {
1880 		wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
1881 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1882 		goto fail;
1883 	}
1884 	wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
1885 		    FILS_SESSION_LEN);
1886 	os_memcpy(sta->fils_session, elems.fils_session, FILS_SESSION_LEN);
1887 
1888 	/* Wrapped Data */
1889 	if (elems.wrapped_data) {
1890 		wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
1891 			    elems.wrapped_data,
1892 			    elems.wrapped_data_len);
1893 		if (!pmksa) {
1894 #ifndef CONFIG_NO_RADIUS
1895 			if (!sta->eapol_sm) {
1896 				sta->eapol_sm =
1897 					ieee802_1x_alloc_eapol_sm(hapd, sta);
1898 			}
1899 			wpa_printf(MSG_DEBUG,
1900 				   "FILS: Forward EAP-Initiate/Re-auth to authentication server");
1901 			ieee802_1x_encapsulate_radius(
1902 				hapd, sta, elems.wrapped_data,
1903 				elems.wrapped_data_len);
1904 			sta->fils_pending_cb = cb;
1905 			wpa_printf(MSG_DEBUG,
1906 				   "FILS: Will send Authentication frame once the response from authentication server is available");
1907 			sta->flags |= WLAN_STA_PENDING_FILS_ERP;
1908 			/* Calculate pending PMKID here so that we do not need
1909 			 * to maintain a copy of the EAP-Initiate/Reauth
1910 			 * message. */
1911 			if (fils_pmkid_erp(wpa_auth_sta_key_mgmt(sta->wpa_sm),
1912 					   elems.wrapped_data,
1913 					   elems.wrapped_data_len,
1914 					   sta->fils_erp_pmkid) == 0)
1915 				sta->fils_erp_pmkid_set = 1;
1916 			return;
1917 #else /* CONFIG_NO_RADIUS */
1918 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1919 			goto fail;
1920 #endif /* CONFIG_NO_RADIUS */
1921 		}
1922 	}
1923 
1924 fail:
1925 	if (cb) {
1926 		struct wpabuf *data;
1927 		int pub = 0;
1928 
1929 		data = prepare_auth_resp_fils(hapd, sta, &resp, pmksa, NULL,
1930 					      NULL, 0, &pub);
1931 		if (!data) {
1932 			wpa_printf(MSG_DEBUG,
1933 				   "%s: prepare_auth_resp_fils() returned failure",
1934 				   __func__);
1935 		}
1936 
1937 		cb(hapd, sta, resp, data, pub);
1938 	}
1939 }
1940 
1941 
1942 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)1943 prepare_auth_resp_fils(struct hostapd_data *hapd,
1944 		       struct sta_info *sta, u16 *resp,
1945 		       struct rsn_pmksa_cache_entry *pmksa,
1946 		       struct wpabuf *erp_resp,
1947 		       const u8 *msk, size_t msk_len,
1948 		       int *is_pub)
1949 {
1950 	u8 fils_nonce[FILS_NONCE_LEN];
1951 	size_t ielen;
1952 	struct wpabuf *data = NULL;
1953 	const u8 *ie;
1954 	u8 *ie_buf = NULL;
1955 	const u8 *pmk = NULL;
1956 	size_t pmk_len = 0;
1957 	u8 pmk_buf[PMK_LEN_MAX];
1958 	struct wpabuf *pub = NULL;
1959 
1960 	if (*resp != WLAN_STATUS_SUCCESS)
1961 		goto fail;
1962 
1963 	ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
1964 	if (!ie) {
1965 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1966 		goto fail;
1967 	}
1968 
1969 	if (pmksa) {
1970 		/* Add PMKID of the selected PMKSA into RSNE */
1971 		ie_buf = os_malloc(ielen + 2 + 2 + PMKID_LEN);
1972 		if (!ie_buf) {
1973 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1974 			goto fail;
1975 		}
1976 
1977 		os_memcpy(ie_buf, ie, ielen);
1978 		if (wpa_insert_pmkid(ie_buf, &ielen, pmksa->pmkid) < 0) {
1979 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1980 			goto fail;
1981 		}
1982 		ie = ie_buf;
1983 	}
1984 
1985 	if (random_get_bytes(fils_nonce, FILS_NONCE_LEN) < 0) {
1986 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1987 		goto fail;
1988 	}
1989 	wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS Nonce",
1990 		    fils_nonce, FILS_NONCE_LEN);
1991 
1992 #ifdef CONFIG_FILS_SK_PFS
1993 	if (sta->fils_dh_ss && sta->fils_ecdh) {
1994 		pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
1995 		if (!pub) {
1996 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1997 			goto fail;
1998 		}
1999 	}
2000 #endif /* CONFIG_FILS_SK_PFS */
2001 
2002 	data = wpabuf_alloc(1000 + ielen + (pub ? wpabuf_len(pub) : 0));
2003 	if (!data) {
2004 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2005 		goto fail;
2006 	}
2007 
2008 	/* TODO: FILS PK */
2009 #ifdef CONFIG_FILS_SK_PFS
2010 	if (pub) {
2011 		/* Finite Cyclic Group */
2012 		wpabuf_put_le16(data, hapd->conf->fils_dh_group);
2013 
2014 		/* Element */
2015 		wpabuf_put_buf(data, pub);
2016 	}
2017 #endif /* CONFIG_FILS_SK_PFS */
2018 
2019 	/* RSNE */
2020 	wpabuf_put_data(data, ie, ielen);
2021 
2022 	/* MDE when using FILS+FT (already included in ie,ielen with RSNE) */
2023 
2024 #ifdef CONFIG_IEEE80211R_AP
2025 	if (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm))) {
2026 		/* FTE[R1KH-ID,R0KH-ID] when using FILS+FT */
2027 		int res;
2028 
2029 		res = wpa_auth_write_fte(hapd->wpa_auth, sta->wpa_sm,
2030 					 wpabuf_put(data, 0),
2031 					 wpabuf_tailroom(data));
2032 		if (res < 0) {
2033 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2034 			goto fail;
2035 		}
2036 		wpabuf_put(data, res);
2037 	}
2038 #endif /* CONFIG_IEEE80211R_AP */
2039 
2040 	/* FILS Nonce */
2041 	wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
2042 	wpabuf_put_u8(data, 1 + FILS_NONCE_LEN); /* Length */
2043 	/* Element ID Extension */
2044 	wpabuf_put_u8(data, WLAN_EID_EXT_FILS_NONCE);
2045 	wpabuf_put_data(data, fils_nonce, FILS_NONCE_LEN);
2046 
2047 	/* FILS Session */
2048 	wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
2049 	wpabuf_put_u8(data, 1 + FILS_SESSION_LEN); /* Length */
2050 	/* Element ID Extension */
2051 	wpabuf_put_u8(data, WLAN_EID_EXT_FILS_SESSION);
2052 	wpabuf_put_data(data, sta->fils_session, FILS_SESSION_LEN);
2053 
2054 	/* Wrapped Data */
2055 	if (!pmksa && erp_resp) {
2056 		wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
2057 		wpabuf_put_u8(data, 1 + wpabuf_len(erp_resp)); /* Length */
2058 		/* Element ID Extension */
2059 		wpabuf_put_u8(data, WLAN_EID_EXT_WRAPPED_DATA);
2060 		wpabuf_put_buf(data, erp_resp);
2061 
2062 		if (fils_rmsk_to_pmk(wpa_auth_sta_key_mgmt(sta->wpa_sm),
2063 				     msk, msk_len, sta->fils_snonce, fils_nonce,
2064 				     sta->fils_dh_ss ?
2065 				     wpabuf_head(sta->fils_dh_ss) : NULL,
2066 				     sta->fils_dh_ss ?
2067 				     wpabuf_len(sta->fils_dh_ss) : 0,
2068 				     pmk_buf, &pmk_len)) {
2069 			wpa_printf(MSG_DEBUG, "FILS: Failed to derive PMK");
2070 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2071 			wpabuf_free(data);
2072 			data = NULL;
2073 			goto fail;
2074 		}
2075 		pmk = pmk_buf;
2076 
2077 		/* Don't use DHss in PTK derivation if PMKSA caching is not
2078 		 * used. */
2079 		wpabuf_clear_free(sta->fils_dh_ss);
2080 		sta->fils_dh_ss = NULL;
2081 
2082 		if (sta->fils_erp_pmkid_set) {
2083 			/* TODO: get PMKLifetime from WPA parameters */
2084 			unsigned int dot11RSNAConfigPMKLifetime = 43200;
2085 			int session_timeout;
2086 
2087 			session_timeout = dot11RSNAConfigPMKLifetime;
2088 			if (sta->session_timeout_set) {
2089 				struct os_reltime now, diff;
2090 
2091 				os_get_reltime(&now);
2092 				os_reltime_sub(&sta->session_timeout, &now,
2093 					       &diff);
2094 				session_timeout = diff.sec;
2095 			}
2096 
2097 			sta->fils_erp_pmkid_set = 0;
2098 			wpa_auth_add_fils_pmk_pmkid(sta->wpa_sm, pmk, pmk_len,
2099 						    sta->fils_erp_pmkid);
2100 			if (!hapd->conf->disable_pmksa_caching &&
2101 			    wpa_auth_pmksa_add2(
2102 				    hapd->wpa_auth, sta->addr,
2103 				    pmk, pmk_len,
2104 				    sta->fils_erp_pmkid,
2105 				    session_timeout,
2106 				    wpa_auth_sta_key_mgmt(sta->wpa_sm)) < 0) {
2107 				wpa_printf(MSG_ERROR,
2108 					   "FILS: Failed to add PMKSA cache entry based on ERP");
2109 			}
2110 		}
2111 	} else if (pmksa) {
2112 		pmk = pmksa->pmk;
2113 		pmk_len = pmksa->pmk_len;
2114 	}
2115 
2116 	if (!pmk) {
2117 		wpa_printf(MSG_DEBUG, "FILS: No PMK available");
2118 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2119 		wpabuf_free(data);
2120 		data = NULL;
2121 		goto fail;
2122 	}
2123 
2124 	if (fils_auth_pmk_to_ptk(sta->wpa_sm, pmk, pmk_len,
2125 				 sta->fils_snonce, fils_nonce,
2126 				 sta->fils_dh_ss ?
2127 				 wpabuf_head(sta->fils_dh_ss) : NULL,
2128 				 sta->fils_dh_ss ?
2129 				 wpabuf_len(sta->fils_dh_ss) : 0,
2130 				 sta->fils_g_sta, pub) < 0) {
2131 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2132 		wpabuf_free(data);
2133 		data = NULL;
2134 		goto fail;
2135 	}
2136 
2137 fail:
2138 	if (is_pub)
2139 		*is_pub = pub != NULL;
2140 	os_free(ie_buf);
2141 	wpabuf_free(pub);
2142 	wpabuf_clear_free(sta->fils_dh_ss);
2143 	sta->fils_dh_ss = NULL;
2144 #ifdef CONFIG_FILS_SK_PFS
2145 	crypto_ecdh_deinit(sta->fils_ecdh);
2146 	sta->fils_ecdh = NULL;
2147 #endif /* CONFIG_FILS_SK_PFS */
2148 	return data;
2149 }
2150 
2151 
handle_auth_fils_finish(struct hostapd_data * hapd,struct sta_info * sta,u16 resp,struct wpabuf * data,int pub)2152 static void handle_auth_fils_finish(struct hostapd_data *hapd,
2153 				    struct sta_info *sta, u16 resp,
2154 				    struct wpabuf *data, int pub)
2155 {
2156 	u16 auth_alg;
2157 
2158 	auth_alg = (pub ||
2159 		    resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) ?
2160 		WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
2161 	send_auth_reply(hapd, sta, sta->addr, hapd->own_addr, auth_alg, 2, resp,
2162 			data ? wpabuf_head(data) : (u8 *) "",
2163 			data ? wpabuf_len(data) : 0, "auth-fils-finish");
2164 	wpabuf_free(data);
2165 
2166 	if (resp == WLAN_STATUS_SUCCESS) {
2167 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2168 			       HOSTAPD_LEVEL_DEBUG,
2169 			       "authentication OK (FILS)");
2170 		sta->flags |= WLAN_STA_AUTH;
2171 		wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
2172 		sta->auth_alg = pub ? WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
2173 		mlme_authenticate_indication(hapd, sta);
2174 	}
2175 }
2176 
2177 
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)2178 void ieee802_11_finish_fils_auth(struct hostapd_data *hapd,
2179 				 struct sta_info *sta, int success,
2180 				 struct wpabuf *erp_resp,
2181 				 const u8 *msk, size_t msk_len)
2182 {
2183 	u16 resp;
2184 	u32 flags = sta->flags;
2185 
2186 	sta->flags &= ~(WLAN_STA_PENDING_FILS_ERP |
2187 			WLAN_STA_PENDING_PASN_FILS_ERP);
2188 
2189 	resp = success ? WLAN_STATUS_SUCCESS : WLAN_STATUS_UNSPECIFIED_FAILURE;
2190 
2191 	if (flags & WLAN_STA_PENDING_FILS_ERP) {
2192 		struct wpabuf *data;
2193 		int pub = 0;
2194 
2195 		if (!sta->fils_pending_cb)
2196 			return;
2197 
2198 		data = prepare_auth_resp_fils(hapd, sta, &resp, NULL, erp_resp,
2199 					      msk, msk_len, &pub);
2200 		if (!data) {
2201 			wpa_printf(MSG_DEBUG,
2202 				   "%s: prepare_auth_resp_fils() failure",
2203 				   __func__);
2204 		}
2205 		sta->fils_pending_cb(hapd, sta, resp, data, pub);
2206 #ifdef CONFIG_PASN
2207 	} else if (flags & WLAN_STA_PENDING_PASN_FILS_ERP) {
2208 		pasn_fils_auth_resp(hapd, sta, resp, erp_resp,
2209 				    msk, msk_len);
2210 #endif /* CONFIG_PASN */
2211 	}
2212 }
2213 
2214 #endif /* CONFIG_FILS */
2215 
2216 
ieee802_11_allowed_address(struct hostapd_data * hapd,const u8 * addr,const u8 * msg,size_t len,struct radius_sta * info)2217 static int ieee802_11_allowed_address(struct hostapd_data *hapd, const u8 *addr,
2218 				      const u8 *msg, size_t len,
2219 				      struct radius_sta *info)
2220 {
2221 	int res;
2222 
2223 	res = hostapd_allowed_address(hapd, addr, msg, len, info, 0);
2224 
2225 	if (res == HOSTAPD_ACL_REJECT) {
2226 		wpa_printf(MSG_DEBUG, "Station " MACSTR
2227 			   " not allowed to authenticate",
2228 			   MAC2STR(addr));
2229 		return HOSTAPD_ACL_REJECT;
2230 	}
2231 
2232 	if (res == HOSTAPD_ACL_PENDING) {
2233 		wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
2234 			   " waiting for an external authentication",
2235 			   MAC2STR(addr));
2236 		/* Authentication code will re-send the authentication frame
2237 		 * after it has received (and cached) information from the
2238 		 * external source. */
2239 		return HOSTAPD_ACL_PENDING;
2240 	}
2241 
2242 	return res;
2243 }
2244 
2245 
ieee802_11_set_radius_info(struct hostapd_data * hapd,struct sta_info * sta,int res,struct radius_sta * info)2246 int ieee802_11_set_radius_info(struct hostapd_data *hapd, struct sta_info *sta,
2247 			       int res, struct radius_sta *info)
2248 {
2249 	u32 session_timeout = info->session_timeout;
2250 	u32 acct_interim_interval = info->acct_interim_interval;
2251 	struct vlan_description *vlan_id = &info->vlan_id;
2252 	struct hostapd_sta_wpa_psk_short *psk = info->psk;
2253 	char *identity = info->identity;
2254 	char *radius_cui = info->radius_cui;
2255 
2256 	if (vlan_id->notempty &&
2257 	    !hostapd_vlan_valid(hapd->conf->vlan, vlan_id)) {
2258 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
2259 			       HOSTAPD_LEVEL_INFO,
2260 			       "Invalid VLAN %d%s received from RADIUS server",
2261 			       vlan_id->untagged,
2262 			       vlan_id->tagged[0] ? "+" : "");
2263 		return -1;
2264 	}
2265 	if (ap_sta_set_vlan(hapd, sta, vlan_id) < 0)
2266 		return -1;
2267 	if (sta->vlan_id)
2268 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
2269 			       HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
2270 
2271 	hostapd_free_psk_list(sta->psk);
2272 	if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED)
2273 		hostapd_copy_psk_list(&sta->psk, psk);
2274 	else
2275 		sta->psk = NULL;
2276 
2277 	os_free(sta->identity);
2278 	if (identity)
2279 		sta->identity = os_strdup(identity);
2280 	else
2281 		sta->identity = NULL;
2282 
2283 	os_free(sta->radius_cui);
2284 	if (radius_cui)
2285 		sta->radius_cui = os_strdup(radius_cui);
2286 	else
2287 		sta->radius_cui = NULL;
2288 
2289 	if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
2290 		sta->acct_interim_interval = acct_interim_interval;
2291 	if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT) {
2292 		sta->session_timeout_set = 1;
2293 		os_get_reltime(&sta->session_timeout);
2294 		sta->session_timeout.sec += session_timeout;
2295 		ap_sta_session_timeout(hapd, sta, session_timeout);
2296 	} else {
2297 		sta->session_timeout_set = 0;
2298 		ap_sta_no_session_timeout(hapd, sta);
2299 	}
2300 
2301 	return 0;
2302 }
2303 
2304 
2305 #ifdef CONFIG_PASN
2306 #ifdef CONFIG_FILS
2307 
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)2308 static void pasn_fils_auth_resp(struct hostapd_data *hapd,
2309 				struct sta_info *sta, u16 status,
2310 				struct wpabuf *erp_resp,
2311 				const u8 *msk, size_t msk_len)
2312 {
2313 	struct pasn_data *pasn = sta->pasn;
2314 	struct pasn_fils *fils = &pasn->fils;
2315 	u8 pmk[PMK_LEN_MAX];
2316 	size_t pmk_len;
2317 	int ret;
2318 
2319 	wpa_printf(MSG_DEBUG, "PASN: FILS: Handle AS response - status=%u",
2320 		   status);
2321 
2322 	if (status != WLAN_STATUS_SUCCESS)
2323 		goto fail;
2324 
2325 	if (!pasn->secret) {
2326 		wpa_printf(MSG_DEBUG, "PASN: FILS: Missing secret");
2327 		goto fail;
2328 	}
2329 
2330 	if (random_get_bytes(fils->anonce, FILS_NONCE_LEN) < 0) {
2331 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed to get ANonce");
2332 		goto fail;
2333 	}
2334 
2335 	wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS ANonce",
2336 		    fils->anonce, FILS_NONCE_LEN);
2337 
2338 	ret = fils_rmsk_to_pmk(pasn->akmp, msk, msk_len, fils->nonce,
2339 			       fils->anonce, NULL, 0, pmk, &pmk_len);
2340 	if (ret) {
2341 		wpa_printf(MSG_DEBUG, "FILS: Failed to derive PMK");
2342 		goto fail;
2343 	}
2344 
2345 	ret = pasn_pmk_to_ptk(pmk, pmk_len, sta->addr, hapd->own_addr,
2346 			      wpabuf_head(pasn->secret),
2347 			      wpabuf_len(pasn->secret),
2348 			      &sta->pasn->ptk, sta->pasn->akmp,
2349 			      sta->pasn->cipher, sta->pasn->kdk_len);
2350 	if (ret) {
2351 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed to derive PTK");
2352 		goto fail;
2353 	}
2354 
2355 	if (pasn->secure_ltf) {
2356 		ret = wpa_ltf_keyseed(&pasn->ptk, pasn->akmp, pasn->cipher);
2357 		if (ret) {
2358 			wpa_printf(MSG_DEBUG,
2359 				   "PASN: FILS: Failed to derive LTF keyseed");
2360 			goto fail;
2361 		}
2362 	}
2363 
2364 	wpa_printf(MSG_DEBUG, "PASN: PTK successfully derived");
2365 
2366 	wpabuf_free(pasn->secret);
2367 	pasn->secret = NULL;
2368 
2369 	fils->erp_resp = erp_resp;
2370 	ret = handle_auth_pasn_resp(sta->pasn, hapd->own_addr, sta->addr, NULL,
2371 				    WLAN_STATUS_SUCCESS);
2372 	fils->erp_resp = NULL;
2373 
2374 	if (ret) {
2375 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed to send response");
2376 		goto fail;
2377 	}
2378 
2379 	fils->state = PASN_FILS_STATE_COMPLETE;
2380 	return;
2381 fail:
2382 	ap_free_sta(hapd, sta);
2383 }
2384 
2385 
pasn_wd_handle_fils(struct hostapd_data * hapd,struct sta_info * sta,struct wpabuf * wd)2386 static int pasn_wd_handle_fils(struct hostapd_data *hapd, struct sta_info *sta,
2387 			       struct wpabuf *wd)
2388 {
2389 #ifdef CONFIG_NO_RADIUS
2390 	wpa_printf(MSG_DEBUG, "PASN: FILS: RADIUS is not configured. Fail");
2391 	return -1;
2392 #else /* CONFIG_NO_RADIUS */
2393 	struct pasn_data *pasn = sta->pasn;
2394 	struct pasn_fils *fils = &pasn->fils;
2395 	struct ieee802_11_elems elems;
2396 	struct wpa_ie_data rsne_data;
2397 	struct wpabuf *fils_wd;
2398 	const u8 *data;
2399 	size_t buf_len;
2400 	u16 alg, seq, status;
2401 	int ret;
2402 
2403 	if (fils->state != PASN_FILS_STATE_NONE) {
2404 		wpa_printf(MSG_DEBUG, "PASN: FILS: Not expecting wrapped data");
2405 		return -1;
2406 	}
2407 
2408 	if (!wd) {
2409 		wpa_printf(MSG_DEBUG, "PASN: FILS: No wrapped data");
2410 		return -1;
2411 	}
2412 
2413 	data = wpabuf_head_u8(wd);
2414 	buf_len = wpabuf_len(wd);
2415 
2416 	if (buf_len < 6) {
2417 		wpa_printf(MSG_DEBUG, "PASN: FILS: Buffer too short. len=%zu",
2418 			   buf_len);
2419 		return -1;
2420 	}
2421 
2422 	alg = WPA_GET_LE16(data);
2423 	seq = WPA_GET_LE16(data + 2);
2424 	status = WPA_GET_LE16(data + 4);
2425 
2426 	wpa_printf(MSG_DEBUG, "PASN: FILS: alg=%u, seq=%u, status=%u",
2427 		   alg, seq, status);
2428 
2429 	if (alg != WLAN_AUTH_FILS_SK || seq != 1 ||
2430 	    status != WLAN_STATUS_SUCCESS) {
2431 		wpa_printf(MSG_DEBUG,
2432 			   "PASN: FILS: Dropping peer authentication");
2433 		return -1;
2434 	}
2435 
2436 	data += 6;
2437 	buf_len -= 6;
2438 
2439 	if (ieee802_11_parse_elems(data, buf_len, &elems, 1) == ParseFailed) {
2440 		wpa_printf(MSG_DEBUG, "PASN: FILS: Could not parse elements");
2441 		return -1;
2442 	}
2443 
2444 	if (!elems.rsn_ie || !elems.fils_nonce || !elems.fils_nonce ||
2445 	    !elems.wrapped_data || !elems.fils_session) {
2446 		wpa_printf(MSG_DEBUG, "PASN: FILS: Missing IEs");
2447 		return -1;
2448 	}
2449 
2450 	ret = wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
2451 				   &rsne_data);
2452 	if (ret) {
2453 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed parsing RSNE");
2454 		return -1;
2455 	}
2456 
2457 	ret = wpa_pasn_validate_rsne(&rsne_data);
2458 	if (ret) {
2459 		wpa_printf(MSG_DEBUG, "PASN: FILS: Failed validating RSNE");
2460 		return -1;
2461 	}
2462 
2463 	if (rsne_data.num_pmkid) {
2464 		wpa_printf(MSG_DEBUG,
2465 			   "PASN: FILS: Not expecting PMKID in RSNE");
2466 		return -1;
2467 	}
2468 
2469 	wpa_hexdump(MSG_DEBUG, "PASN: FILS: Nonce", elems.fils_nonce,
2470 		    FILS_NONCE_LEN);
2471 	os_memcpy(fils->nonce, elems.fils_nonce, FILS_NONCE_LEN);
2472 
2473 	wpa_hexdump(MSG_DEBUG, "PASN: FILS: Session", elems.fils_session,
2474 		    FILS_SESSION_LEN);
2475 	os_memcpy(fils->session, elems.fils_session, FILS_SESSION_LEN);
2476 
2477 	fils_wd = ieee802_11_defrag(&elems, WLAN_EID_EXTENSION,
2478 				    WLAN_EID_EXT_WRAPPED_DATA);
2479 
2480 	if (!fils_wd) {
2481 		wpa_printf(MSG_DEBUG, "PASN: FILS: Missing wrapped data");
2482 		return -1;
2483 	}
2484 
2485 	if (!sta->eapol_sm)
2486 		sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
2487 
2488 	wpa_printf(MSG_DEBUG,
2489 		   "PASN: FILS: Forward EAP-Initiate/Re-auth to AS");
2490 
2491 	ieee802_1x_encapsulate_radius(hapd, sta, wpabuf_head(fils_wd),
2492 				      wpabuf_len(fils_wd));
2493 
2494 	sta->flags |= WLAN_STA_PENDING_PASN_FILS_ERP;
2495 
2496 	fils->state = PASN_FILS_STATE_PENDING_AS;
2497 
2498 	/*
2499 	 * Calculate pending PMKID here so that we do not need to maintain a
2500 	 * copy of the EAP-Initiate/Reautt message.
2501 	 */
2502 	fils_pmkid_erp(pasn->akmp, wpabuf_head(fils_wd), wpabuf_len(fils_wd),
2503 		       fils->erp_pmkid);
2504 
2505 	wpabuf_free(fils_wd);
2506 	return 0;
2507 #endif /* CONFIG_NO_RADIUS */
2508 }
2509 
2510 #endif /* CONFIG_FILS */
2511 
2512 
hapd_pasn_send_mlme(void * ctx,const u8 * data,size_t data_len,int noack,unsigned int freq,unsigned int wait)2513 static int hapd_pasn_send_mlme(void *ctx, const u8 *data, size_t data_len,
2514 			       int noack, unsigned int freq, unsigned int wait)
2515 {
2516 	struct hostapd_data *hapd = ctx;
2517 
2518 	return hostapd_drv_send_mlme(hapd, data, data_len, 0, NULL, 0, 0);
2519 }
2520 
2521 
hapd_initialize_pasn(struct hostapd_data * hapd,struct sta_info * sta)2522 static void hapd_initialize_pasn(struct hostapd_data *hapd,
2523 				 struct sta_info *sta)
2524 {
2525 	struct pasn_data *pasn = sta->pasn;
2526 
2527 	pasn->cb_ctx = hapd;
2528 	pasn->send_mgmt = hapd_pasn_send_mlme;
2529 	pasn->pasn_groups = hapd->conf->pasn_groups;
2530 	pasn->noauth = hapd->conf->pasn_noauth;
2531 	pasn->wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
2532 	pasn->rsn_pairwise = hapd->conf->rsn_pairwise;
2533 	pasn->derive_kdk = hapd->iface->drv_flags2 &
2534 		WPA_DRIVER_FLAGS2_SEC_LTF_AP;
2535 #ifdef CONFIG_TESTING_OPTIONS
2536 	pasn->corrupt_mic = hapd->conf->pasn_corrupt_mic;
2537 	if (hapd->conf->force_kdk_derivation)
2538 		pasn->derive_kdk = true;
2539 #endif /* CONFIG_TESTING_OPTIONS */
2540 	pasn->use_anti_clogging = use_anti_clogging(hapd);
2541 	pasn->password = sae_get_password(hapd, sta, NULL, NULL, &pasn->pt,
2542 					  NULL);
2543 	pasn->rsn_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &pasn->rsn_ie_len);
2544 	pasn->rsnxe_ie = hostapd_wpa_ie(hapd, WLAN_EID_RSNX);
2545 	pasn->disable_pmksa_caching = hapd->conf->disable_pmksa_caching;
2546 	pasn->pmksa = wpa_auth_get_pmksa_cache(hapd->wpa_auth);
2547 
2548 	pasn->comeback_after = hapd->conf->pasn_comeback_after;
2549 	pasn->comeback_idx = hapd->comeback_idx;
2550 	pasn->comeback_key =  hapd->comeback_key;
2551 	pasn->comeback_pending_idx = hapd->comeback_pending_idx;
2552 	os_memcpy(pasn->bssid, hapd->own_addr, ETH_ALEN);
2553 }
2554 
2555 
pasn_set_keys_from_cache(struct hostapd_data * hapd,const u8 * own_addr,const u8 * sta_addr,int cipher,int akmp)2556 static int pasn_set_keys_from_cache(struct hostapd_data *hapd,
2557 				    const u8 *own_addr, const u8 *sta_addr,
2558 				    int cipher, int akmp)
2559 {
2560 	struct ptksa_cache_entry *entry;
2561 
2562 	entry = ptksa_cache_get(hapd->ptksa, sta_addr, cipher);
2563 	if (!entry) {
2564 		wpa_printf(MSG_DEBUG, "PASN: peer " MACSTR
2565 			   " not present in PTKSA cache", MAC2STR(sta_addr));
2566 		return -1;
2567 	}
2568 
2569 	if (os_memcmp(entry->own_addr, own_addr, ETH_ALEN) != 0) {
2570 		wpa_printf(MSG_DEBUG,
2571 			   "PASN: own addr " MACSTR " and PTKSA entry own addr "
2572 			   MACSTR " differ",
2573 			   MAC2STR(own_addr), MAC2STR(entry->own_addr));
2574 		return -1;
2575 	}
2576 
2577 	wpa_printf(MSG_DEBUG, "PASN: " MACSTR " present in PTKSA cache",
2578 		   MAC2STR(sta_addr));
2579 	hostapd_drv_set_secure_ranging_ctx(hapd, own_addr, sta_addr, cipher,
2580 					   entry->ptk.tk_len, entry->ptk.tk,
2581 					   entry->ptk.ltf_keyseed_len,
2582 					   entry->ptk.ltf_keyseed, 0);
2583 
2584 	return 0;
2585 }
2586 
2587 
hapd_pasn_update_params(struct hostapd_data * hapd,struct sta_info * sta,const struct ieee80211_mgmt * mgmt,size_t len)2588 static void hapd_pasn_update_params(struct hostapd_data *hapd,
2589 				    struct sta_info *sta,
2590 				    const struct ieee80211_mgmt *mgmt,
2591 				    size_t len)
2592 {
2593 	struct pasn_data *pasn = sta->pasn;
2594 	struct ieee802_11_elems elems;
2595 	struct wpa_ie_data rsn_data;
2596 	struct wpa_pasn_params_data pasn_params;
2597 	struct wpabuf *wrapped_data = NULL;
2598 
2599 	if (ieee802_11_parse_elems(mgmt->u.auth.variable,
2600 				   len - offsetof(struct ieee80211_mgmt,
2601 						  u.auth.variable),
2602 				   &elems, 0) == ParseFailed) {
2603 		wpa_printf(MSG_DEBUG,
2604 			   "PASN: Failed parsing Authentication frame");
2605 		return;
2606 	}
2607 
2608 	if (!elems.rsn_ie ||
2609 	    wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
2610 				 &rsn_data)) {
2611 		wpa_printf(MSG_DEBUG, "PASN: Failed parsing RSNE");
2612 		return;
2613 	}
2614 
2615 	if (!(rsn_data.key_mgmt & pasn->wpa_key_mgmt) ||
2616 	    !(rsn_data.pairwise_cipher & pasn->rsn_pairwise)) {
2617 		wpa_printf(MSG_DEBUG, "PASN: Mismatch in AKMP/cipher");
2618 		return;
2619 	}
2620 
2621 	pasn->akmp = rsn_data.key_mgmt;
2622 	pasn->cipher = rsn_data.pairwise_cipher;
2623 
2624 	if (wpa_key_mgmt_ft(pasn->akmp) && rsn_data.num_pmkid) {
2625 #ifdef CONFIG_IEEE80211R_AP
2626 		pasn->pmk_r1_len = 0;
2627 		wpa_ft_fetch_pmk_r1(hapd->wpa_auth, sta->addr,
2628 				    rsn_data.pmkid,
2629 				    pasn->pmk_r1, &pasn->pmk_r1_len, NULL,
2630 				    NULL, NULL, NULL,
2631 				    NULL, NULL, NULL);
2632 #endif /* CONFIG_IEEE80211R_AP */
2633 	}
2634 #ifdef CONFIG_FILS
2635 	if (pasn->akmp != WPA_KEY_MGMT_FILS_SHA256 &&
2636 	    pasn->akmp != WPA_KEY_MGMT_FILS_SHA384)
2637 		return;
2638 	if (!elems.pasn_params ||
2639 	    wpa_pasn_parse_parameter_ie(elems.pasn_params - 3,
2640 					elems.pasn_params_len + 3,
2641 					false, &pasn_params)) {
2642 		wpa_printf(MSG_DEBUG,
2643 			   "PASN: Failed validation of PASN Parameters element");
2644 		return;
2645 	}
2646 	if (pasn_params.wrapped_data_format != WPA_PASN_WRAPPED_DATA_NO) {
2647 		wrapped_data = ieee802_11_defrag(&elems, WLAN_EID_EXTENSION,
2648 						 WLAN_EID_EXT_WRAPPED_DATA);
2649 		if (!wrapped_data) {
2650 			wpa_printf(MSG_DEBUG, "PASN: Missing wrapped data");
2651 			return;
2652 		}
2653 		if (pasn_wd_handle_fils(hapd, sta, wrapped_data))
2654 			wpa_printf(MSG_DEBUG,
2655 				   "PASN: Failed processing FILS wrapped data");
2656 		else
2657 			pasn->fils_wd_valid = true;
2658 	}
2659 	wpabuf_free(wrapped_data);
2660 #endif /* CONFIG_FILS */
2661 }
2662 
2663 
handle_auth_pasn(struct hostapd_data * hapd,struct sta_info * sta,const struct ieee80211_mgmt * mgmt,size_t len,u16 trans_seq,u16 status)2664 static void handle_auth_pasn(struct hostapd_data *hapd, struct sta_info *sta,
2665 			     const struct ieee80211_mgmt *mgmt, size_t len,
2666 			     u16 trans_seq, u16 status)
2667 {
2668 	if (hapd->conf->wpa != WPA_PROTO_RSN) {
2669 		wpa_printf(MSG_INFO, "PASN: RSN is not configured");
2670 		return;
2671 	}
2672 
2673 	wpa_printf(MSG_INFO, "PASN authentication: sta=" MACSTR,
2674 		   MAC2STR(sta->addr));
2675 
2676 	if (trans_seq == 1) {
2677 		if (sta->pasn) {
2678 			wpa_printf(MSG_DEBUG,
2679 				   "PASN: Not expecting transaction == 1");
2680 			return;
2681 		}
2682 
2683 		if (status != WLAN_STATUS_SUCCESS) {
2684 			wpa_printf(MSG_DEBUG,
2685 				   "PASN: Failure status in transaction == 1");
2686 			return;
2687 		}
2688 
2689 		sta->pasn = os_zalloc(sizeof(*sta->pasn));
2690 		if (!sta->pasn) {
2691 			wpa_printf(MSG_DEBUG,
2692 				   "PASN: Failed to allocate PASN context");
2693 			return;
2694 		}
2695 
2696 		hapd_initialize_pasn(hapd, sta);
2697 
2698 		hapd_pasn_update_params(hapd, sta, mgmt, len);
2699 		if (handle_auth_pasn_1(sta->pasn, hapd->own_addr,
2700 				       sta->addr, mgmt, len) < 0)
2701 			ap_free_sta(hapd, sta);
2702 	} else if (trans_seq == 3) {
2703 		if (!sta->pasn) {
2704 			wpa_printf(MSG_DEBUG,
2705 				   "PASN: Not expecting transaction == 3");
2706 			return;
2707 		}
2708 
2709 		if (status != WLAN_STATUS_SUCCESS) {
2710 			wpa_printf(MSG_DEBUG,
2711 				   "PASN: Failure status in transaction == 3");
2712 			ap_free_sta_pasn(hapd, sta);
2713 			return;
2714 		}
2715 
2716 		if (handle_auth_pasn_3(sta->pasn, hapd->own_addr,
2717 				       sta->addr, mgmt, len) == 0) {
2718 			ptksa_cache_add(hapd->ptksa, hapd->own_addr, sta->addr,
2719 					sta->pasn->cipher, 43200,
2720 					&sta->pasn->ptk, NULL, NULL,
2721 					sta->pasn->akmp);
2722 
2723 			pasn_set_keys_from_cache(hapd, hapd->own_addr,
2724 						 sta->addr, sta->pasn->cipher,
2725 						 sta->pasn->akmp);
2726 		}
2727 		ap_free_sta(hapd, sta);
2728 	} else {
2729 		wpa_printf(MSG_DEBUG,
2730 			   "PASN: Invalid transaction %u - ignore", trans_seq);
2731 	}
2732 }
2733 
2734 #endif /* CONFIG_PASN */
2735 
2736 
handle_auth(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int rssi,int from_queue)2737 static void handle_auth(struct hostapd_data *hapd,
2738 			const struct ieee80211_mgmt *mgmt, size_t len,
2739 			int rssi, int from_queue)
2740 {
2741 	u16 auth_alg, auth_transaction, status_code;
2742 	u16 resp = WLAN_STATUS_SUCCESS;
2743 	struct sta_info *sta = NULL;
2744 	int res, reply_res;
2745 	u16 fc;
2746 	const u8 *challenge = NULL;
2747 	u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
2748 	size_t resp_ies_len = 0;
2749 	u16 seq_ctrl;
2750 	struct radius_sta rad_info;
2751 
2752 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
2753 		wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
2754 			   (unsigned long) len);
2755 		return;
2756 	}
2757 
2758 #ifdef CONFIG_TESTING_OPTIONS
2759 	if (hapd->iconf->ignore_auth_probability > 0.0 &&
2760 	    drand48() < hapd->iconf->ignore_auth_probability) {
2761 		wpa_printf(MSG_INFO,
2762 			   "TESTING: ignoring auth frame from " MACSTR,
2763 			   MAC2STR(mgmt->sa));
2764 		return;
2765 	}
2766 #endif /* CONFIG_TESTING_OPTIONS */
2767 
2768 	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2769 	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2770 	status_code = le_to_host16(mgmt->u.auth.status_code);
2771 	fc = le_to_host16(mgmt->frame_control);
2772 	seq_ctrl = le_to_host16(mgmt->seq_ctrl);
2773 
2774 	if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
2775 	    2 + WLAN_AUTH_CHALLENGE_LEN &&
2776 	    mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
2777 	    mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
2778 		challenge = &mgmt->u.auth.variable[2];
2779 
2780 	wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
2781 		   "auth_transaction=%d status_code=%d wep=%d%s "
2782 		   "seq_ctrl=0x%x%s%s",
2783 		   MAC2STR(mgmt->sa), auth_alg, auth_transaction,
2784 		   status_code, !!(fc & WLAN_FC_ISWEP),
2785 		   challenge ? " challenge" : "",
2786 		   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "",
2787 		   from_queue ? " (from queue)" : "");
2788 
2789 #ifdef CONFIG_NO_RC4
2790 	if (auth_alg == WLAN_AUTH_SHARED_KEY) {
2791 		wpa_printf(MSG_INFO,
2792 			   "Unsupported authentication algorithm (%d)",
2793 			   auth_alg);
2794 		resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
2795 		goto fail;
2796 	}
2797 #endif /* CONFIG_NO_RC4 */
2798 
2799 	if (hapd->tkip_countermeasures) {
2800 		wpa_printf(MSG_DEBUG,
2801 			   "Ongoing TKIP countermeasures (Michael MIC failure) - reject authentication");
2802 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2803 		goto fail;
2804 	}
2805 
2806 	if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
2807 	       auth_alg == WLAN_AUTH_OPEN) ||
2808 #ifdef CONFIG_IEEE80211R_AP
2809 	      (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
2810 	       auth_alg == WLAN_AUTH_FT) ||
2811 #endif /* CONFIG_IEEE80211R_AP */
2812 #ifdef CONFIG_SAE
2813 	      (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
2814 	       auth_alg == WLAN_AUTH_SAE) ||
2815 #endif /* CONFIG_SAE */
2816 #ifdef CONFIG_FILS
2817 	      (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
2818 	       auth_alg == WLAN_AUTH_FILS_SK) ||
2819 	      (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
2820 	       hapd->conf->fils_dh_group &&
2821 	       auth_alg == WLAN_AUTH_FILS_SK_PFS) ||
2822 #endif /* CONFIG_FILS */
2823 #ifdef CONFIG_PASN
2824 	      (hapd->conf->wpa &&
2825 	       (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PASN) &&
2826 	       auth_alg == WLAN_AUTH_PASN) ||
2827 #endif /* CONFIG_PASN */
2828 	      ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
2829 	       auth_alg == WLAN_AUTH_SHARED_KEY))) {
2830 		wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
2831 			   auth_alg);
2832 		resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
2833 		goto fail;
2834 	}
2835 
2836 	if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
2837 #ifdef CONFIG_PASN
2838 	      (auth_alg == WLAN_AUTH_PASN && auth_transaction == 3) ||
2839 #endif /* CONFIG_PASN */
2840 	      (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
2841 		wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
2842 			   auth_transaction);
2843 		resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
2844 		goto fail;
2845 	}
2846 
2847 	if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
2848 		wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
2849 			   MAC2STR(mgmt->sa));
2850 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2851 		goto fail;
2852 	}
2853 
2854 	if (hapd->conf->no_auth_if_seen_on) {
2855 		struct hostapd_data *other;
2856 
2857 		other = sta_track_seen_on(hapd->iface, mgmt->sa,
2858 					  hapd->conf->no_auth_if_seen_on);
2859 		if (other) {
2860 			u8 *pos;
2861 			u32 info;
2862 			u8 op_class, channel, phytype;
2863 
2864 			wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
2865 				   MACSTR " since STA has been seen on %s",
2866 				   hapd->conf->iface, MAC2STR(mgmt->sa),
2867 				   hapd->conf->no_auth_if_seen_on);
2868 
2869 			resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
2870 			pos = &resp_ies[0];
2871 			*pos++ = WLAN_EID_NEIGHBOR_REPORT;
2872 			*pos++ = 13;
2873 			os_memcpy(pos, other->own_addr, ETH_ALEN);
2874 			pos += ETH_ALEN;
2875 			info = 0; /* TODO: BSSID Information */
2876 			WPA_PUT_LE32(pos, info);
2877 			pos += 4;
2878 			if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
2879 				phytype = 8; /* dmg */
2880 			else if (other->iconf->ieee80211ac)
2881 				phytype = 9; /* vht */
2882 			else if (other->iconf->ieee80211n)
2883 				phytype = 7; /* ht */
2884 			else if (other->iconf->hw_mode ==
2885 				 HOSTAPD_MODE_IEEE80211A)
2886 				phytype = 4; /* ofdm */
2887 			else if (other->iconf->hw_mode ==
2888 				 HOSTAPD_MODE_IEEE80211G)
2889 				phytype = 6; /* erp */
2890 			else
2891 				phytype = 5; /* hrdsss */
2892 			if (ieee80211_freq_to_channel_ext(
2893 				    hostapd_hw_get_freq(other,
2894 							other->iconf->channel),
2895 				    other->iconf->secondary_channel,
2896 				    other->iconf->ieee80211ac,
2897 				    &op_class, &channel) == NUM_HOSTAPD_MODES) {
2898 				op_class = 0;
2899 				channel = other->iconf->channel;
2900 			}
2901 			*pos++ = op_class;
2902 			*pos++ = channel;
2903 			*pos++ = phytype;
2904 			resp_ies_len = pos - &resp_ies[0];
2905 			goto fail;
2906 		}
2907 	}
2908 
2909 	res = ieee802_11_allowed_address(hapd, mgmt->sa, (const u8 *) mgmt, len,
2910 					 &rad_info);
2911 	if (res == HOSTAPD_ACL_REJECT) {
2912 		wpa_msg(hapd->msg_ctx, MSG_DEBUG,
2913 			"Ignore Authentication frame from " MACSTR
2914 			" due to ACL reject", MAC2STR(mgmt->sa));
2915 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2916 		goto fail;
2917 	}
2918 	if (res == HOSTAPD_ACL_PENDING)
2919 		return;
2920 
2921 #ifdef CONFIG_SAE
2922 	if (auth_alg == WLAN_AUTH_SAE && !from_queue &&
2923 	    (auth_transaction == 1 ||
2924 	     (auth_transaction == 2 && auth_sae_queued_addr(hapd, mgmt->sa)))) {
2925 		/* Handle SAE Authentication commit message through a queue to
2926 		 * provide more control for postponing the needed heavy
2927 		 * processing under a possible DoS attack scenario. In addition,
2928 		 * queue SAE Authentication confirm message if there happens to
2929 		 * be a queued commit message from the same peer. This is needed
2930 		 * to avoid reordering Authentication frames within the same
2931 		 * SAE exchange. */
2932 		auth_sae_queue(hapd, mgmt, len, rssi);
2933 		return;
2934 	}
2935 #endif /* CONFIG_SAE */
2936 
2937 	sta = ap_get_sta(hapd, mgmt->sa);
2938 	if (sta) {
2939 		sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
2940 		sta->ft_over_ds = 0;
2941 		if ((fc & WLAN_FC_RETRY) &&
2942 		    sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2943 		    sta->last_seq_ctrl == seq_ctrl &&
2944 		    sta->last_subtype == WLAN_FC_STYPE_AUTH) {
2945 			hostapd_logger(hapd, sta->addr,
2946 				       HOSTAPD_MODULE_IEEE80211,
2947 				       HOSTAPD_LEVEL_DEBUG,
2948 				       "Drop repeated authentication frame seq_ctrl=0x%x",
2949 				       seq_ctrl);
2950 			return;
2951 		}
2952 #ifdef CONFIG_MESH
2953 		if ((hapd->conf->mesh & MESH_ENABLED) &&
2954 		    sta->plink_state == PLINK_BLOCKED) {
2955 			wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
2956 				   " is blocked - drop Authentication frame",
2957 				   MAC2STR(mgmt->sa));
2958 			return;
2959 		}
2960 #endif /* CONFIG_MESH */
2961 #ifdef CONFIG_PASN
2962 		if (auth_alg == WLAN_AUTH_PASN &&
2963 		    (sta->flags & WLAN_STA_ASSOC)) {
2964 			wpa_printf(MSG_DEBUG,
2965 				   "PASN: auth: Existing station: " MACSTR,
2966 				   MAC2STR(sta->addr));
2967 			return;
2968 		}
2969 #endif /* CONFIG_PASN */
2970 	} else {
2971 #ifdef CONFIG_MESH
2972 		if (hapd->conf->mesh & MESH_ENABLED) {
2973 			/* if the mesh peer is not available, we don't do auth.
2974 			 */
2975 			wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
2976 				   " not yet known - drop Authentication frame",
2977 				   MAC2STR(mgmt->sa));
2978 			/*
2979 			 * Save a copy of the frame so that it can be processed
2980 			 * if a new peer entry is added shortly after this.
2981 			 */
2982 			wpabuf_free(hapd->mesh_pending_auth);
2983 			hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
2984 			os_get_reltime(&hapd->mesh_pending_auth_time);
2985 			return;
2986 		}
2987 #endif /* CONFIG_MESH */
2988 
2989 		sta = ap_sta_add(hapd, mgmt->sa);
2990 		if (!sta) {
2991 			wpa_printf(MSG_DEBUG, "ap_sta_add() failed");
2992 			resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2993 			goto fail;
2994 		}
2995 	}
2996 	sta->last_seq_ctrl = seq_ctrl;
2997 	sta->last_subtype = WLAN_FC_STYPE_AUTH;
2998 #ifdef CONFIG_MBO
2999 	sta->auth_rssi = rssi;
3000 #endif /* CONFIG_MBO */
3001 
3002 	res = ieee802_11_set_radius_info(hapd, sta, res, &rad_info);
3003 	if (res) {
3004 		wpa_printf(MSG_DEBUG, "ieee802_11_set_radius_info() failed");
3005 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3006 		goto fail;
3007 	}
3008 
3009 	sta->flags &= ~WLAN_STA_PREAUTH;
3010 	ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
3011 
3012 	/*
3013 	 * If the driver supports full AP client state, add a station to the
3014 	 * driver before sending authentication reply to make sure the driver
3015 	 * has resources, and not to go through the entire authentication and
3016 	 * association handshake, and fail it at the end.
3017 	 *
3018 	 * If this is not the first transaction, in a multi-step authentication
3019 	 * algorithm, the station already exists in the driver
3020 	 * (sta->added_unassoc = 1) so skip it.
3021 	 *
3022 	 * In mesh mode, the station was already added to the driver when the
3023 	 * NEW_PEER_CANDIDATE event is received.
3024 	 *
3025 	 * If PMF was negotiated for the existing association, skip this to
3026 	 * avoid dropping the STA entry and the associated keys. This is needed
3027 	 * to allow the original connection work until the attempt can complete
3028 	 * (re)association, so that unprotected Authentication frame cannot be
3029 	 * used to bypass PMF protection.
3030 	 *
3031 	 * PASN authentication does not require adding/removing station to the
3032 	 * driver so skip this flow in case of PASN authentication.
3033 	 */
3034 	if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
3035 	    (!(sta->flags & WLAN_STA_MFP) || !ap_sta_is_authorized(sta)) &&
3036 	    !(hapd->conf->mesh & MESH_ENABLED) &&
3037 	    !(sta->added_unassoc) && auth_alg != WLAN_AUTH_PASN) {
3038 		if (ap_sta_re_add(hapd, sta) < 0) {
3039 			resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
3040 			goto fail;
3041 		}
3042 	}
3043 
3044 	switch (auth_alg) {
3045 	case WLAN_AUTH_OPEN:
3046 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3047 			       HOSTAPD_LEVEL_DEBUG,
3048 			       "authentication OK (open system)");
3049 		sta->flags |= WLAN_STA_AUTH;
3050 		wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
3051 		sta->auth_alg = WLAN_AUTH_OPEN;
3052 		mlme_authenticate_indication(hapd, sta);
3053 		break;
3054 #ifdef CONFIG_WEP
3055 #ifndef CONFIG_NO_RC4
3056 	case WLAN_AUTH_SHARED_KEY:
3057 		resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
3058 				       fc & WLAN_FC_ISWEP);
3059 		if (resp != 0)
3060 			wpa_printf(MSG_DEBUG,
3061 				   "auth_shared_key() failed: status=%d", resp);
3062 		sta->auth_alg = WLAN_AUTH_SHARED_KEY;
3063 		mlme_authenticate_indication(hapd, sta);
3064 		if (sta->challenge && auth_transaction == 1) {
3065 			resp_ies[0] = WLAN_EID_CHALLENGE;
3066 			resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
3067 			os_memcpy(resp_ies + 2, sta->challenge,
3068 				  WLAN_AUTH_CHALLENGE_LEN);
3069 			resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
3070 		}
3071 		break;
3072 #endif /* CONFIG_NO_RC4 */
3073 #endif /* CONFIG_WEP */
3074 #ifdef CONFIG_IEEE80211R_AP
3075 	case WLAN_AUTH_FT:
3076 		sta->auth_alg = WLAN_AUTH_FT;
3077 		if (sta->wpa_sm == NULL)
3078 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
3079 							sta->addr, NULL);
3080 		if (sta->wpa_sm == NULL) {
3081 			wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
3082 				   "state machine");
3083 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3084 			goto fail;
3085 		}
3086 		wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
3087 				    auth_transaction, mgmt->u.auth.variable,
3088 				    len - IEEE80211_HDRLEN -
3089 				    sizeof(mgmt->u.auth),
3090 				    handle_auth_ft_finish, hapd);
3091 		/* handle_auth_ft_finish() callback will complete auth. */
3092 		return;
3093 #endif /* CONFIG_IEEE80211R_AP */
3094 #ifdef CONFIG_SAE
3095 	case WLAN_AUTH_SAE:
3096 #ifdef CONFIG_MESH
3097 		if (status_code == WLAN_STATUS_SUCCESS &&
3098 		    hapd->conf->mesh & MESH_ENABLED) {
3099 			if (sta->wpa_sm == NULL)
3100 				sta->wpa_sm =
3101 					wpa_auth_sta_init(hapd->wpa_auth,
3102 							  sta->addr, NULL);
3103 			if (sta->wpa_sm == NULL) {
3104 				wpa_printf(MSG_DEBUG,
3105 					   "SAE: Failed to initialize WPA state machine");
3106 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3107 				goto fail;
3108 			}
3109 		}
3110 #endif /* CONFIG_MESH */
3111 		handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
3112 				status_code);
3113 		return;
3114 #endif /* CONFIG_SAE */
3115 #ifdef CONFIG_FILS
3116 	case WLAN_AUTH_FILS_SK:
3117 	case WLAN_AUTH_FILS_SK_PFS:
3118 		handle_auth_fils(hapd, sta, mgmt->u.auth.variable,
3119 				 len - IEEE80211_HDRLEN - sizeof(mgmt->u.auth),
3120 				 auth_alg, auth_transaction, status_code,
3121 				 handle_auth_fils_finish);
3122 		return;
3123 #endif /* CONFIG_FILS */
3124 #ifdef CONFIG_PASN
3125 	case WLAN_AUTH_PASN:
3126 		handle_auth_pasn(hapd, sta, mgmt, len, auth_transaction,
3127 				 status_code);
3128 		return;
3129 #endif /* CONFIG_PASN */
3130 	}
3131 
3132  fail:
3133 	reply_res = send_auth_reply(hapd, sta, mgmt->sa, mgmt->bssid, auth_alg,
3134 				    auth_alg == WLAN_AUTH_SAE ?
3135 				    auth_transaction : auth_transaction + 1,
3136 				    resp, resp_ies, resp_ies_len,
3137 				    "handle-auth");
3138 
3139 	if (sta && sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
3140 					  reply_res != WLAN_STATUS_SUCCESS)) {
3141 		hostapd_drv_sta_remove(hapd, sta->addr);
3142 		sta->added_unassoc = 0;
3143 	}
3144 }
3145 
3146 
hostapd_max_bssid_indicator(struct hostapd_data * hapd)3147 static u8 hostapd_max_bssid_indicator(struct hostapd_data *hapd)
3148 {
3149 	size_t num_bss_nontx;
3150 	u8 max_bssid_ind = 0;
3151 
3152 	if (!hapd->iconf->mbssid || hapd->iface->num_bss <= 1)
3153 		return 0;
3154 
3155 	num_bss_nontx = hapd->iface->num_bss - 1;
3156 	while (num_bss_nontx > 0) {
3157 		max_bssid_ind++;
3158 		num_bss_nontx >>= 1;
3159 	}
3160 	return max_bssid_ind;
3161 }
3162 
3163 
hostapd_get_aid(struct hostapd_data * hapd,struct sta_info * sta)3164 int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
3165 {
3166 	int i, j = 32, aid;
3167 
3168 	/* get a unique AID */
3169 	if (sta->aid > 0) {
3170 		wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
3171 		return 0;
3172 	}
3173 
3174 	if (TEST_FAIL())
3175 		return -1;
3176 
3177 	for (i = 0; i < AID_WORDS; i++) {
3178 		if (hapd->sta_aid[i] == (u32) -1)
3179 			continue;
3180 		for (j = 0; j < 32; j++) {
3181 			if (!(hapd->sta_aid[i] & BIT(j)))
3182 				break;
3183 		}
3184 		if (j < 32)
3185 			break;
3186 	}
3187 	if (j == 32)
3188 		return -1;
3189 	aid = i * 32 + j + (1 << hostapd_max_bssid_indicator(hapd));
3190 	if (aid > 2007)
3191 		return -1;
3192 
3193 	sta->aid = aid;
3194 	hapd->sta_aid[i] |= BIT(j);
3195 	wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
3196 	return 0;
3197 }
3198 
3199 
check_ssid(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ssid_ie,size_t ssid_ie_len)3200 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
3201 		      const u8 *ssid_ie, size_t ssid_ie_len)
3202 {
3203 	if (ssid_ie == NULL)
3204 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3205 
3206 	if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
3207 	    os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
3208 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3209 			       HOSTAPD_LEVEL_INFO,
3210 			       "Station tried to associate with unknown SSID "
3211 			       "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
3212 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3213 	}
3214 
3215 	return WLAN_STATUS_SUCCESS;
3216 }
3217 
3218 
check_wmm(struct hostapd_data * hapd,struct sta_info * sta,const u8 * wmm_ie,size_t wmm_ie_len)3219 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
3220 		     const u8 *wmm_ie, size_t wmm_ie_len)
3221 {
3222 	sta->flags &= ~WLAN_STA_WMM;
3223 	sta->qosinfo = 0;
3224 	if (wmm_ie && hapd->conf->wmm_enabled) {
3225 		struct wmm_information_element *wmm;
3226 
3227 		if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
3228 			hostapd_logger(hapd, sta->addr,
3229 				       HOSTAPD_MODULE_WPA,
3230 				       HOSTAPD_LEVEL_DEBUG,
3231 				       "invalid WMM element in association "
3232 				       "request");
3233 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
3234 		}
3235 
3236 		sta->flags |= WLAN_STA_WMM;
3237 		wmm = (struct wmm_information_element *) wmm_ie;
3238 		sta->qosinfo = wmm->qos_info;
3239 	}
3240 	return WLAN_STATUS_SUCCESS;
3241 }
3242 
check_multi_ap(struct hostapd_data * hapd,struct sta_info * sta,const u8 * multi_ap_ie,size_t multi_ap_len)3243 static u16 check_multi_ap(struct hostapd_data *hapd, struct sta_info *sta,
3244 			  const u8 *multi_ap_ie, size_t multi_ap_len)
3245 {
3246 	u8 multi_ap_value = 0;
3247 
3248 	sta->flags &= ~WLAN_STA_MULTI_AP;
3249 
3250 	if (!hapd->conf->multi_ap)
3251 		return WLAN_STATUS_SUCCESS;
3252 
3253 	if (multi_ap_ie) {
3254 		const u8 *multi_ap_subelem;
3255 
3256 		multi_ap_subelem = get_ie(multi_ap_ie + 4,
3257 					  multi_ap_len - 4,
3258 					  MULTI_AP_SUB_ELEM_TYPE);
3259 		if (multi_ap_subelem && multi_ap_subelem[1] == 1) {
3260 			multi_ap_value = multi_ap_subelem[2];
3261 		} else {
3262 			hostapd_logger(hapd, sta->addr,
3263 				       HOSTAPD_MODULE_IEEE80211,
3264 				       HOSTAPD_LEVEL_INFO,
3265 				       "Multi-AP IE has missing or invalid Multi-AP subelement");
3266 			return WLAN_STATUS_INVALID_IE;
3267 		}
3268 	}
3269 
3270 	if (multi_ap_value && multi_ap_value != MULTI_AP_BACKHAUL_STA)
3271 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3272 			       HOSTAPD_LEVEL_INFO,
3273 			       "Multi-AP IE with unexpected value 0x%02x",
3274 			       multi_ap_value);
3275 
3276 	if (!(multi_ap_value & MULTI_AP_BACKHAUL_STA)) {
3277 		if (hapd->conf->multi_ap & FRONTHAUL_BSS)
3278 			return WLAN_STATUS_SUCCESS;
3279 
3280 		hostapd_logger(hapd, sta->addr,
3281 			       HOSTAPD_MODULE_IEEE80211,
3282 			       HOSTAPD_LEVEL_INFO,
3283 			       "Non-Multi-AP STA tries to associate with backhaul-only BSS");
3284 		return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
3285 	}
3286 
3287 	if (!(hapd->conf->multi_ap & BACKHAUL_BSS))
3288 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3289 			       HOSTAPD_LEVEL_DEBUG,
3290 			       "Backhaul STA tries to associate with fronthaul-only BSS");
3291 
3292 	sta->flags |= WLAN_STA_MULTI_AP;
3293 	return WLAN_STATUS_SUCCESS;
3294 }
3295 
3296 
copy_supp_rates(struct hostapd_data * hapd,struct sta_info * sta,struct ieee802_11_elems * elems)3297 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
3298 			   struct ieee802_11_elems *elems)
3299 {
3300 	/* Supported rates not used in IEEE 802.11ad/DMG */
3301 	if (hapd->iface->current_mode &&
3302 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD)
3303 		return WLAN_STATUS_SUCCESS;
3304 
3305 	if (!elems->supp_rates) {
3306 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3307 			       HOSTAPD_LEVEL_DEBUG,
3308 			       "No supported rates element in AssocReq");
3309 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3310 	}
3311 
3312 	if (elems->supp_rates_len + elems->ext_supp_rates_len >
3313 	    sizeof(sta->supported_rates)) {
3314 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3315 			       HOSTAPD_LEVEL_DEBUG,
3316 			       "Invalid supported rates element length %d+%d",
3317 			       elems->supp_rates_len,
3318 			       elems->ext_supp_rates_len);
3319 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3320 	}
3321 
3322 	sta->supported_rates_len = merge_byte_arrays(
3323 		sta->supported_rates, sizeof(sta->supported_rates),
3324 		elems->supp_rates, elems->supp_rates_len,
3325 		elems->ext_supp_rates, elems->ext_supp_rates_len);
3326 
3327 	return WLAN_STATUS_SUCCESS;
3328 }
3329 
3330 
3331 #ifdef CONFIG_OWE
3332 
owe_group_supported(struct hostapd_data * hapd,u16 group)3333 static int owe_group_supported(struct hostapd_data *hapd, u16 group)
3334 {
3335 	int i;
3336 	int *groups = hapd->conf->owe_groups;
3337 
3338 	if (group != 19 && group != 20 && group != 21)
3339 		return 0;
3340 
3341 	if (!groups)
3342 		return 1;
3343 
3344 	for (i = 0; groups[i] > 0; i++) {
3345 		if (groups[i] == group)
3346 			return 1;
3347 	}
3348 
3349 	return 0;
3350 }
3351 
3352 
owe_process_assoc_req(struct hostapd_data * hapd,struct sta_info * sta,const u8 * owe_dh,u8 owe_dh_len)3353 static u16 owe_process_assoc_req(struct hostapd_data *hapd,
3354 				 struct sta_info *sta, const u8 *owe_dh,
3355 				 u8 owe_dh_len)
3356 {
3357 	struct wpabuf *secret, *pub, *hkey;
3358 	int res;
3359 	u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
3360 	const char *info = "OWE Key Generation";
3361 	const u8 *addr[2];
3362 	size_t len[2];
3363 	u16 group;
3364 	size_t hash_len, prime_len;
3365 
3366 	if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
3367 		wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
3368 		return WLAN_STATUS_SUCCESS;
3369 	}
3370 
3371 	group = WPA_GET_LE16(owe_dh);
3372 	if (!owe_group_supported(hapd, group)) {
3373 		wpa_printf(MSG_DEBUG, "OWE: Unsupported DH group %u", group);
3374 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
3375 	}
3376 	if (group == 19)
3377 		prime_len = 32;
3378 	else if (group == 20)
3379 		prime_len = 48;
3380 	else if (group == 21)
3381 		prime_len = 66;
3382 	else
3383 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
3384 
3385 	if (sta->owe_group == group && sta->owe_ecdh) {
3386 		/* This is a workaround for mac80211 behavior of retransmitting
3387 		 * the Association Request frames multiple times if the link
3388 		 * layer retries (i.e., seq# remains same) fail. The mac80211
3389 		 * initiated retransmission will use a different seq# and as
3390 		 * such, will go through duplicate detection. If we were to
3391 		 * change our DH key for that attempt, there would be two
3392 		 * different DH shared secrets and the STA would likely select
3393 		 * the wrong one. */
3394 		wpa_printf(MSG_DEBUG,
3395 			   "OWE: Try to reuse own previous DH key since the STA tried to go through OWE association again");
3396 	} else {
3397 		crypto_ecdh_deinit(sta->owe_ecdh);
3398 		sta->owe_ecdh = crypto_ecdh_init(group);
3399 	}
3400 	if (!sta->owe_ecdh)
3401 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
3402 	sta->owe_group = group;
3403 
3404 	secret = crypto_ecdh_set_peerkey(sta->owe_ecdh, 0, owe_dh + 2,
3405 					 owe_dh_len - 2);
3406 	secret = wpabuf_zeropad(secret, prime_len);
3407 	if (!secret) {
3408 		wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
3409 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3410 	}
3411 	wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
3412 
3413 	/* prk = HKDF-extract(C | A | group, z) */
3414 
3415 	pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
3416 	if (!pub) {
3417 		wpabuf_clear_free(secret);
3418 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3419 	}
3420 
3421 	/* PMKID = Truncate-128(Hash(C | A)) */
3422 	addr[0] = owe_dh + 2;
3423 	len[0] = owe_dh_len - 2;
3424 	addr[1] = wpabuf_head(pub);
3425 	len[1] = wpabuf_len(pub);
3426 	if (group == 19) {
3427 		res = sha256_vector(2, addr, len, pmkid);
3428 		hash_len = SHA256_MAC_LEN;
3429 	} else if (group == 20) {
3430 		res = sha384_vector(2, addr, len, pmkid);
3431 		hash_len = SHA384_MAC_LEN;
3432 	} else if (group == 21) {
3433 		res = sha512_vector(2, addr, len, pmkid);
3434 		hash_len = SHA512_MAC_LEN;
3435 	} else {
3436 		wpabuf_free(pub);
3437 		wpabuf_clear_free(secret);
3438 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3439 	}
3440 	pub = wpabuf_zeropad(pub, prime_len);
3441 	if (res < 0 || !pub) {
3442 		wpabuf_free(pub);
3443 		wpabuf_clear_free(secret);
3444 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3445 	}
3446 
3447 	hkey = wpabuf_alloc(owe_dh_len - 2 + wpabuf_len(pub) + 2);
3448 	if (!hkey) {
3449 		wpabuf_free(pub);
3450 		wpabuf_clear_free(secret);
3451 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3452 	}
3453 
3454 	wpabuf_put_data(hkey, owe_dh + 2, owe_dh_len - 2); /* C */
3455 	wpabuf_put_buf(hkey, pub); /* A */
3456 	wpabuf_free(pub);
3457 	wpabuf_put_le16(hkey, group); /* group */
3458 	if (group == 19)
3459 		res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
3460 				  wpabuf_head(secret), wpabuf_len(secret), prk);
3461 	else if (group == 20)
3462 		res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
3463 				  wpabuf_head(secret), wpabuf_len(secret), prk);
3464 	else if (group == 21)
3465 		res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
3466 				  wpabuf_head(secret), wpabuf_len(secret), prk);
3467 	wpabuf_clear_free(hkey);
3468 	wpabuf_clear_free(secret);
3469 	if (res < 0)
3470 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3471 
3472 	wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
3473 
3474 	/* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
3475 
3476 	os_free(sta->owe_pmk);
3477 	sta->owe_pmk = os_malloc(hash_len);
3478 	if (!sta->owe_pmk) {
3479 		os_memset(prk, 0, SHA512_MAC_LEN);
3480 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3481 	}
3482 
3483 	if (group == 19)
3484 		res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
3485 				      os_strlen(info), sta->owe_pmk, hash_len);
3486 	else if (group == 20)
3487 		res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
3488 				      os_strlen(info), sta->owe_pmk, hash_len);
3489 	else if (group == 21)
3490 		res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
3491 				      os_strlen(info), sta->owe_pmk, hash_len);
3492 	os_memset(prk, 0, SHA512_MAC_LEN);
3493 	if (res < 0) {
3494 		os_free(sta->owe_pmk);
3495 		sta->owe_pmk = NULL;
3496 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3497 	}
3498 	sta->owe_pmk_len = hash_len;
3499 
3500 	wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sta->owe_pmk, sta->owe_pmk_len);
3501 	wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
3502 	wpa_auth_pmksa_add2(hapd->wpa_auth, sta->addr, sta->owe_pmk,
3503 			    sta->owe_pmk_len, pmkid, 0, WPA_KEY_MGMT_OWE);
3504 
3505 	return WLAN_STATUS_SUCCESS;
3506 }
3507 
3508 
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)3509 u16 owe_validate_request(struct hostapd_data *hapd, const u8 *peer,
3510 			 const u8 *rsn_ie, size_t rsn_ie_len,
3511 			 const u8 *owe_dh, size_t owe_dh_len)
3512 {
3513 	struct wpa_ie_data data;
3514 	int res;
3515 
3516 	if (!rsn_ie || rsn_ie_len < 2) {
3517 		wpa_printf(MSG_DEBUG, "OWE: Invalid RSNE from " MACSTR,
3518 			   MAC2STR(peer));
3519 		return WLAN_STATUS_INVALID_IE;
3520 	}
3521 	rsn_ie -= 2;
3522 	rsn_ie_len += 2;
3523 
3524 	res = wpa_parse_wpa_ie_rsn(rsn_ie, rsn_ie_len, &data);
3525 	if (res) {
3526 		wpa_printf(MSG_DEBUG, "Failed to parse RSNE from " MACSTR
3527 			   " (res=%d)", MAC2STR(peer), res);
3528 		wpa_hexdump(MSG_DEBUG, "RSNE", rsn_ie, rsn_ie_len);
3529 		return wpa_res_to_status_code(res);
3530 	}
3531 	if (!(data.key_mgmt & WPA_KEY_MGMT_OWE)) {
3532 		wpa_printf(MSG_DEBUG,
3533 			   "OWE: Unexpected key mgmt 0x%x from " MACSTR,
3534 			   (unsigned int) data.key_mgmt, MAC2STR(peer));
3535 		return WLAN_STATUS_AKMP_NOT_VALID;
3536 	}
3537 	if (!owe_dh) {
3538 		wpa_printf(MSG_DEBUG,
3539 			   "OWE: No Diffie-Hellman Parameter element from "
3540 			   MACSTR, MAC2STR(peer));
3541 		return WLAN_STATUS_AKMP_NOT_VALID;
3542 	}
3543 
3544 	return WLAN_STATUS_SUCCESS;
3545 }
3546 
3547 
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)3548 u16 owe_process_rsn_ie(struct hostapd_data *hapd,
3549 		       struct sta_info *sta,
3550 		       const u8 *rsn_ie, size_t rsn_ie_len,
3551 		       const u8 *owe_dh, size_t owe_dh_len)
3552 {
3553 	u16 status;
3554 	u8 *owe_buf, ie[256 * 2];
3555 	size_t ie_len = 0;
3556 	enum wpa_validate_result res;
3557 
3558 	if (!rsn_ie || rsn_ie_len < 2) {
3559 		wpa_printf(MSG_DEBUG, "OWE: No RSNE in (Re)AssocReq");
3560 		status = WLAN_STATUS_INVALID_IE;
3561 		goto end;
3562 	}
3563 
3564 	if (!sta->wpa_sm)
3565 		sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,	sta->addr,
3566 						NULL);
3567 	if (!sta->wpa_sm) {
3568 		wpa_printf(MSG_WARNING,
3569 			   "OWE: Failed to initialize WPA state machine");
3570 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
3571 		goto end;
3572 	}
3573 	rsn_ie -= 2;
3574 	rsn_ie_len += 2;
3575 	res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
3576 				  hapd->iface->freq, rsn_ie, rsn_ie_len,
3577 				  NULL, 0, NULL, 0, owe_dh, owe_dh_len);
3578 	status = wpa_res_to_status_code(res);
3579 	if (status != WLAN_STATUS_SUCCESS)
3580 		goto end;
3581 	status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
3582 	if (status != WLAN_STATUS_SUCCESS)
3583 		goto end;
3584 	owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, ie, sizeof(ie),
3585 						NULL, 0);
3586 	if (!owe_buf) {
3587 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
3588 		goto end;
3589 	}
3590 
3591 	if (sta->owe_ecdh) {
3592 		struct wpabuf *pub;
3593 
3594 		pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
3595 		if (!pub) {
3596 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
3597 			goto end;
3598 		}
3599 
3600 		/* OWE Diffie-Hellman Parameter element */
3601 		*owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
3602 		*owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
3603 		*owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
3604 							 */
3605 		WPA_PUT_LE16(owe_buf, sta->owe_group);
3606 		owe_buf += 2;
3607 		os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
3608 		owe_buf += wpabuf_len(pub);
3609 		wpabuf_free(pub);
3610 		sta->external_dh_updated = 1;
3611 	}
3612 	ie_len = owe_buf - ie;
3613 
3614 end:
3615 	wpa_printf(MSG_DEBUG, "OWE: Update status %d, ie len %d for peer "
3616 			      MACSTR, status, (unsigned int) ie_len,
3617 			      MAC2STR(sta->addr));
3618 	hostapd_drv_update_dh_ie(hapd, sta->addr, status,
3619 				 status == WLAN_STATUS_SUCCESS ? ie : NULL,
3620 				 ie_len);
3621 
3622 	return status;
3623 }
3624 
3625 #endif /* CONFIG_OWE */
3626 
3627 
check_sa_query(struct hostapd_data * hapd,struct sta_info * sta,int reassoc)3628 static bool check_sa_query(struct hostapd_data *hapd, struct sta_info *sta,
3629 			   int reassoc)
3630 {
3631 	if ((sta->flags &
3632 	     (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED)) !=
3633 	    (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED))
3634 		return false;
3635 
3636 	if (!sta->sa_query_timed_out && sta->sa_query_count > 0)
3637 		ap_check_sa_query_timeout(hapd, sta);
3638 
3639 	if (!sta->sa_query_timed_out &&
3640 	    (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
3641 		/*
3642 		 * STA has already been associated with MFP and SA Query timeout
3643 		 * has not been reached. Reject the association attempt
3644 		 * temporarily and start SA Query, if one is not pending.
3645 		 */
3646 		if (sta->sa_query_count == 0)
3647 			ap_sta_start_sa_query(hapd, sta);
3648 
3649 		return true;
3650 	}
3651 
3652 	return false;
3653 }
3654 
3655 
__check_assoc_ies(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ies,size_t ies_len,struct ieee802_11_elems * elems,int reassoc)3656 static int __check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
3657 			     const u8 *ies, size_t ies_len,
3658 			     struct ieee802_11_elems *elems, int reassoc)
3659 {
3660 	int resp;
3661 	const u8 *wpa_ie;
3662 	size_t wpa_ie_len;
3663 	const u8 *p2p_dev_addr = NULL;
3664 
3665 	resp = check_ssid(hapd, sta, elems->ssid, elems->ssid_len);
3666 	if (resp != WLAN_STATUS_SUCCESS)
3667 		return resp;
3668 	resp = check_wmm(hapd, sta, elems->wmm, elems->wmm_len);
3669 	if (resp != WLAN_STATUS_SUCCESS)
3670 		return resp;
3671 	resp = check_ext_capab(hapd, sta, elems->ext_capab,
3672 			       elems->ext_capab_len);
3673 	if (resp != WLAN_STATUS_SUCCESS)
3674 		return resp;
3675 	resp = copy_supp_rates(hapd, sta, elems);
3676 	if (resp != WLAN_STATUS_SUCCESS)
3677 		return resp;
3678 
3679 	resp = check_multi_ap(hapd, sta, elems->multi_ap, elems->multi_ap_len);
3680 	if (resp != WLAN_STATUS_SUCCESS)
3681 		return resp;
3682 
3683 	resp = copy_sta_ht_capab(hapd, sta, elems->ht_capabilities);
3684 	if (resp != WLAN_STATUS_SUCCESS)
3685 		return resp;
3686 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
3687 	    !(sta->flags & WLAN_STA_HT)) {
3688 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3689 			       HOSTAPD_LEVEL_INFO, "Station does not support "
3690 			       "mandatory HT PHY - reject association");
3691 		return WLAN_STATUS_ASSOC_DENIED_NO_HT;
3692 	}
3693 
3694 #ifdef CONFIG_IEEE80211AC
3695 	if (hapd->iconf->ieee80211ac) {
3696 		resp = copy_sta_vht_capab(hapd, sta, elems->vht_capabilities);
3697 		if (resp != WLAN_STATUS_SUCCESS)
3698 			return resp;
3699 
3700 		resp = set_sta_vht_opmode(hapd, sta, elems->opmode_notif);
3701 		if (resp != WLAN_STATUS_SUCCESS)
3702 			return resp;
3703 	}
3704 
3705 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
3706 	    !(sta->flags & WLAN_STA_VHT)) {
3707 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3708 			       HOSTAPD_LEVEL_INFO, "Station does not support "
3709 			       "mandatory VHT PHY - reject association");
3710 		return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
3711 	}
3712 
3713 	if (hapd->conf->vendor_vht && !elems->vht_capabilities) {
3714 		resp = copy_sta_vendor_vht(hapd, sta, elems->vendor_vht,
3715 					   elems->vendor_vht_len);
3716 		if (resp != WLAN_STATUS_SUCCESS)
3717 			return resp;
3718 	}
3719 #endif /* CONFIG_IEEE80211AC */
3720 #ifdef CONFIG_IEEE80211AX
3721 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
3722 		resp = copy_sta_he_capab(hapd, sta, IEEE80211_MODE_AP,
3723 					 elems->he_capabilities,
3724 					 elems->he_capabilities_len);
3725 		if (resp != WLAN_STATUS_SUCCESS)
3726 			return resp;
3727 
3728 		if (hapd->iconf->require_he && !(sta->flags & WLAN_STA_HE)) {
3729 			hostapd_logger(hapd, sta->addr,
3730 				       HOSTAPD_MODULE_IEEE80211,
3731 				       HOSTAPD_LEVEL_INFO,
3732 				       "Station does not support mandatory HE PHY - reject association");
3733 			return WLAN_STATUS_DENIED_HE_NOT_SUPPORTED;
3734 		}
3735 
3736 		if (is_6ghz_op_class(hapd->iconf->op_class)) {
3737 			if (!(sta->flags & WLAN_STA_HE)) {
3738 				hostapd_logger(hapd, sta->addr,
3739 					       HOSTAPD_MODULE_IEEE80211,
3740 					       HOSTAPD_LEVEL_INFO,
3741 					       "Station does not support mandatory HE PHY - reject association");
3742 				return WLAN_STATUS_DENIED_HE_NOT_SUPPORTED;
3743 			}
3744 			resp = copy_sta_he_6ghz_capab(hapd, sta,
3745 						      elems->he_6ghz_band_cap);
3746 			if (resp != WLAN_STATUS_SUCCESS)
3747 				return resp;
3748 		}
3749 	}
3750 #endif /* CONFIG_IEEE80211AX */
3751 #ifdef CONFIG_IEEE80211BE
3752 	if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be) {
3753 		resp = copy_sta_eht_capab(hapd, sta, IEEE80211_MODE_AP,
3754 					  elems->he_capabilities,
3755 					  elems->he_capabilities_len,
3756 					  elems->eht_capabilities,
3757 					  elems->eht_capabilities_len);
3758 		if (resp != WLAN_STATUS_SUCCESS)
3759 			return resp;
3760 	}
3761 #endif /* CONFIG_IEEE80211BE */
3762 
3763 #ifdef CONFIG_P2P
3764 	if (elems->p2p && ies && ies_len) {
3765 		wpabuf_free(sta->p2p_ie);
3766 		sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
3767 							  P2P_IE_VENDOR_TYPE);
3768 		if (sta->p2p_ie)
3769 			p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
3770 	} else {
3771 		wpabuf_free(sta->p2p_ie);
3772 		sta->p2p_ie = NULL;
3773 	}
3774 #endif /* CONFIG_P2P */
3775 
3776 	if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems->rsn_ie) {
3777 		wpa_ie = elems->rsn_ie;
3778 		wpa_ie_len = elems->rsn_ie_len;
3779 	} else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
3780 		   elems->wpa_ie) {
3781 		wpa_ie = elems->wpa_ie;
3782 		wpa_ie_len = elems->wpa_ie_len;
3783 	} else {
3784 		wpa_ie = NULL;
3785 		wpa_ie_len = 0;
3786 	}
3787 
3788 #ifdef CONFIG_WPS
3789 	sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
3790 	if (hapd->conf->wps_state && elems->wps_ie && ies && ies_len) {
3791 		wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
3792 			   "Request - assume WPS is used");
3793 		if (check_sa_query(hapd, sta, reassoc))
3794 			return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
3795 		sta->flags |= WLAN_STA_WPS;
3796 		wpabuf_free(sta->wps_ie);
3797 		sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
3798 							  WPS_IE_VENDOR_TYPE);
3799 		if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
3800 			wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
3801 			sta->flags |= WLAN_STA_WPS2;
3802 		}
3803 		wpa_ie = NULL;
3804 		wpa_ie_len = 0;
3805 		if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
3806 			wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
3807 				   "(Re)Association Request - reject");
3808 			return WLAN_STATUS_INVALID_IE;
3809 		}
3810 	} else if (hapd->conf->wps_state && wpa_ie == NULL) {
3811 		wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
3812 			   "(Re)Association Request - possible WPS use");
3813 		sta->flags |= WLAN_STA_MAYBE_WPS;
3814 	} else
3815 #endif /* CONFIG_WPS */
3816 	if (hapd->conf->wpa && wpa_ie == NULL) {
3817 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3818 			       HOSTAPD_LEVEL_INFO,
3819 			       "No WPA/RSN IE in association request");
3820 		return WLAN_STATUS_INVALID_IE;
3821 	}
3822 
3823 	if (hapd->conf->wpa && wpa_ie) {
3824 		enum wpa_validate_result res;
3825 
3826 		if (check_sa_query(hapd, sta, reassoc))
3827 			return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
3828 
3829 		wpa_ie -= 2;
3830 		wpa_ie_len += 2;
3831 		if (sta->wpa_sm == NULL)
3832 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
3833 							sta->addr,
3834 							p2p_dev_addr);
3835 		if (sta->wpa_sm == NULL) {
3836 			wpa_printf(MSG_WARNING, "Failed to initialize WPA "
3837 				   "state machine");
3838 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
3839 		}
3840 		wpa_auth_set_auth_alg(sta->wpa_sm, sta->auth_alg);
3841 		res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
3842 					  hapd->iface->freq,
3843 					  wpa_ie, wpa_ie_len,
3844 					  elems->rsnxe ? elems->rsnxe - 2 :
3845 					  NULL,
3846 					  elems->rsnxe ? elems->rsnxe_len + 2 :
3847 					  0,
3848 					  elems->mdie, elems->mdie_len,
3849 					  elems->owe_dh, elems->owe_dh_len);
3850 		resp = wpa_res_to_status_code(res);
3851 		if (resp != WLAN_STATUS_SUCCESS)
3852 			return resp;
3853 
3854 		if (wpa_auth_uses_mfp(sta->wpa_sm))
3855 			sta->flags |= WLAN_STA_MFP;
3856 		else
3857 			sta->flags &= ~WLAN_STA_MFP;
3858 
3859 #ifdef CONFIG_IEEE80211R_AP
3860 		if (sta->auth_alg == WLAN_AUTH_FT) {
3861 			if (!reassoc) {
3862 				wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
3863 					   "to use association (not "
3864 					   "re-association) with FT auth_alg",
3865 					   MAC2STR(sta->addr));
3866 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
3867 			}
3868 
3869 			resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
3870 						       ies_len);
3871 			if (resp != WLAN_STATUS_SUCCESS)
3872 				return resp;
3873 		}
3874 #endif /* CONFIG_IEEE80211R_AP */
3875 
3876 #ifdef CONFIG_SAE
3877 		if (wpa_auth_uses_sae(sta->wpa_sm) && sta->sae &&
3878 		    sta->sae->state == SAE_ACCEPTED)
3879 			wpa_auth_add_sae_pmkid(sta->wpa_sm, sta->sae->pmkid);
3880 
3881 		if (wpa_auth_uses_sae(sta->wpa_sm) &&
3882 		    sta->auth_alg == WLAN_AUTH_OPEN) {
3883 			struct rsn_pmksa_cache_entry *sa;
3884 			sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
3885 			if (!sa || !wpa_key_mgmt_sae(sa->akmp)) {
3886 				wpa_printf(MSG_DEBUG,
3887 					   "SAE: No PMKSA cache entry found for "
3888 					   MACSTR, MAC2STR(sta->addr));
3889 				return WLAN_STATUS_INVALID_PMKID;
3890 			}
3891 			wpa_printf(MSG_DEBUG, "SAE: " MACSTR
3892 				   " using PMKSA caching", MAC2STR(sta->addr));
3893 		} else if (wpa_auth_uses_sae(sta->wpa_sm) &&
3894 			   sta->auth_alg != WLAN_AUTH_SAE &&
3895 			   !(sta->auth_alg == WLAN_AUTH_FT &&
3896 			     wpa_auth_uses_ft_sae(sta->wpa_sm))) {
3897 			wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
3898 				   "SAE AKM after non-SAE auth_alg %u",
3899 				   MAC2STR(sta->addr), sta->auth_alg);
3900 			return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
3901 		}
3902 
3903 		if (hapd->conf->sae_pwe == SAE_PWE_BOTH &&
3904 		    sta->auth_alg == WLAN_AUTH_SAE &&
3905 		    sta->sae && !sta->sae->h2e &&
3906 		    ieee802_11_rsnx_capab_len(elems->rsnxe, elems->rsnxe_len,
3907 					      WLAN_RSNX_CAPAB_SAE_H2E)) {
3908 			wpa_printf(MSG_INFO, "SAE: " MACSTR
3909 				   " indicates support for SAE H2E, but did not use it",
3910 				   MAC2STR(sta->addr));
3911 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
3912 		}
3913 #endif /* CONFIG_SAE */
3914 
3915 #ifdef CONFIG_OWE
3916 		if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
3917 		    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
3918 		    elems->owe_dh) {
3919 			resp = owe_process_assoc_req(hapd, sta, elems->owe_dh,
3920 						     elems->owe_dh_len);
3921 			if (resp != WLAN_STATUS_SUCCESS)
3922 				return resp;
3923 		}
3924 #endif /* CONFIG_OWE */
3925 
3926 #ifdef CONFIG_DPP2
3927 		dpp_pfs_free(sta->dpp_pfs);
3928 		sta->dpp_pfs = NULL;
3929 
3930 		if (DPP_VERSION > 1 &&
3931 		    (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
3932 		    hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
3933 		    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
3934 		    elems->owe_dh) {
3935 			sta->dpp_pfs = dpp_pfs_init(
3936 				wpabuf_head(hapd->conf->dpp_netaccesskey),
3937 				wpabuf_len(hapd->conf->dpp_netaccesskey));
3938 			if (!sta->dpp_pfs) {
3939 				wpa_printf(MSG_DEBUG,
3940 					   "DPP: Could not initialize PFS");
3941 				/* Try to continue without PFS */
3942 				goto pfs_fail;
3943 			}
3944 
3945 			if (dpp_pfs_process(sta->dpp_pfs, elems->owe_dh,
3946 					    elems->owe_dh_len) < 0) {
3947 				dpp_pfs_free(sta->dpp_pfs);
3948 				sta->dpp_pfs = NULL;
3949 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
3950 			}
3951 		}
3952 
3953 		wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
3954 				   sta->dpp_pfs->secret : NULL);
3955 	pfs_fail:
3956 #endif /* CONFIG_DPP2 */
3957 
3958 		if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
3959 		    wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
3960 			hostapd_logger(hapd, sta->addr,
3961 				       HOSTAPD_MODULE_IEEE80211,
3962 				       HOSTAPD_LEVEL_INFO,
3963 				       "Station tried to use TKIP with HT "
3964 				       "association");
3965 			return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
3966 		}
3967 #ifdef CONFIG_HS20
3968 	} else if (hapd->conf->osen) {
3969 		if (!elems->osen) {
3970 			hostapd_logger(
3971 				hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3972 				HOSTAPD_LEVEL_INFO,
3973 				"No HS 2.0 OSEN element in association request");
3974 			return WLAN_STATUS_INVALID_IE;
3975 		}
3976 
3977 		wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
3978 		if (sta->wpa_sm == NULL)
3979 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
3980 							sta->addr, NULL);
3981 		if (sta->wpa_sm == NULL) {
3982 			wpa_printf(MSG_WARNING, "Failed to initialize WPA "
3983 				   "state machine");
3984 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
3985 		}
3986 		if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
3987 				      elems->osen - 2, elems->osen_len + 2) < 0)
3988 			return WLAN_STATUS_INVALID_IE;
3989 #endif /* CONFIG_HS20 */
3990 	} else
3991 		wpa_auth_sta_no_wpa(sta->wpa_sm);
3992 
3993 #ifdef CONFIG_P2P
3994 	p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
3995 #endif /* CONFIG_P2P */
3996 
3997 #ifdef CONFIG_HS20
3998 	wpabuf_free(sta->hs20_ie);
3999 	if (elems->hs20 && elems->hs20_len > 4) {
4000 		int release;
4001 
4002 		sta->hs20_ie = wpabuf_alloc_copy(elems->hs20 + 4,
4003 						 elems->hs20_len - 4);
4004 		release = ((elems->hs20[4] >> 4) & 0x0f) + 1;
4005 		if (release >= 2 && !wpa_auth_uses_mfp(sta->wpa_sm) &&
4006 		    hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
4007 			wpa_printf(MSG_DEBUG,
4008 				   "HS 2.0: PMF not negotiated by release %d station "
4009 				   MACSTR, release, MAC2STR(sta->addr));
4010 			return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
4011 		}
4012 	} else {
4013 		sta->hs20_ie = NULL;
4014 	}
4015 
4016 	wpabuf_free(sta->roaming_consortium);
4017 	if (elems->roaming_cons_sel)
4018 		sta->roaming_consortium = wpabuf_alloc_copy(
4019 			elems->roaming_cons_sel + 4,
4020 			elems->roaming_cons_sel_len - 4);
4021 	else
4022 		sta->roaming_consortium = NULL;
4023 #endif /* CONFIG_HS20 */
4024 
4025 #ifdef CONFIG_FST
4026 	wpabuf_free(sta->mb_ies);
4027 	if (hapd->iface->fst)
4028 		sta->mb_ies = mb_ies_by_info(&elems->mb_ies);
4029 	else
4030 		sta->mb_ies = NULL;
4031 #endif /* CONFIG_FST */
4032 
4033 #ifdef CONFIG_MBO
4034 	mbo_ap_check_sta_assoc(hapd, sta, elems);
4035 
4036 	if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
4037 	    elems->mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
4038 	    hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
4039 		wpa_printf(MSG_INFO,
4040 			   "MBO: Reject WPA2 association without PMF");
4041 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
4042 	}
4043 #endif /* CONFIG_MBO */
4044 
4045 #if defined(CONFIG_FILS) && defined(CONFIG_OCV)
4046 	if (wpa_auth_uses_ocv(sta->wpa_sm) &&
4047 	    (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4048 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4049 	     sta->auth_alg == WLAN_AUTH_FILS_PK)) {
4050 		struct wpa_channel_info ci;
4051 		int tx_chanwidth;
4052 		int tx_seg1_idx;
4053 		enum oci_verify_result res;
4054 
4055 		if (hostapd_drv_channel_info(hapd, &ci) != 0) {
4056 			wpa_printf(MSG_WARNING,
4057 				   "Failed to get channel info to validate received OCI in FILS (Re)Association Request frame");
4058 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
4059 		}
4060 
4061 		if (get_sta_tx_parameters(sta->wpa_sm,
4062 					  channel_width_to_int(ci.chanwidth),
4063 					  ci.seg1_idx, &tx_chanwidth,
4064 					  &tx_seg1_idx) < 0)
4065 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
4066 
4067 		res = ocv_verify_tx_params(elems->oci, elems->oci_len, &ci,
4068 					   tx_chanwidth, tx_seg1_idx);
4069 		if (wpa_auth_uses_ocv(sta->wpa_sm) == 2 &&
4070 		    res == OCI_NOT_FOUND) {
4071 			/* Work around misbehaving STAs */
4072 			wpa_printf(MSG_INFO,
4073 				   "FILS: Disable OCV with a STA that does not send OCI");
4074 			wpa_auth_set_ocv(sta->wpa_sm, 0);
4075 		} else if (res != OCI_SUCCESS) {
4076 			wpa_printf(MSG_WARNING, "FILS: OCV failed: %s",
4077 				   ocv_errorstr);
4078 			wpa_msg(hapd->msg_ctx, MSG_INFO, OCV_FAILURE "addr="
4079 				MACSTR " frame=fils-reassoc-req error=%s",
4080 				MAC2STR(sta->addr), ocv_errorstr);
4081 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
4082 		}
4083 	}
4084 #endif /* CONFIG_FILS && CONFIG_OCV */
4085 
4086 	ap_copy_sta_supp_op_classes(sta, elems->supp_op_classes,
4087 				    elems->supp_op_classes_len);
4088 
4089 	if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
4090 	    elems->rrm_enabled &&
4091 	    elems->rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
4092 		os_memcpy(sta->rrm_enabled_capa, elems->rrm_enabled,
4093 			  sizeof(sta->rrm_enabled_capa));
4094 
4095 	if (elems->power_capab) {
4096 		sta->min_tx_power = elems->power_capab[0];
4097 		sta->max_tx_power = elems->power_capab[1];
4098 		sta->power_capab = 1;
4099 	} else {
4100 		sta->power_capab = 0;
4101 	}
4102 
4103 	return WLAN_STATUS_SUCCESS;
4104 }
4105 
4106 
check_assoc_ies(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ies,size_t ies_len,int reassoc)4107 static int check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
4108 			   const u8 *ies, size_t ies_len, int reassoc)
4109 {
4110 	struct ieee802_11_elems elems;
4111 
4112 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
4113 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4114 			       HOSTAPD_LEVEL_INFO,
4115 			       "Station sent an invalid association request");
4116 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
4117 	}
4118 
4119 	return __check_assoc_ies(hapd, sta, ies, ies_len, &elems, reassoc);
4120 }
4121 
4122 
send_deauth(struct hostapd_data * hapd,const u8 * addr,u16 reason_code)4123 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
4124 			u16 reason_code)
4125 {
4126 	int send_len;
4127 	struct ieee80211_mgmt reply;
4128 
4129 	os_memset(&reply, 0, sizeof(reply));
4130 	reply.frame_control =
4131 		IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
4132 	os_memcpy(reply.da, addr, ETH_ALEN);
4133 	os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
4134 	os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
4135 
4136 	send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
4137 	reply.u.deauth.reason_code = host_to_le16(reason_code);
4138 
4139 	if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0, NULL, 0, 0) < 0)
4140 		wpa_printf(MSG_INFO, "Failed to send deauth: %s",
4141 			   strerror(errno));
4142 }
4143 
4144 
add_associated_sta(struct hostapd_data * hapd,struct sta_info * sta,int reassoc)4145 static int add_associated_sta(struct hostapd_data *hapd,
4146 			      struct sta_info *sta, int reassoc)
4147 {
4148 	struct ieee80211_ht_capabilities ht_cap;
4149 	struct ieee80211_vht_capabilities vht_cap;
4150 	struct ieee80211_he_capabilities he_cap;
4151 	struct ieee80211_eht_capabilities eht_cap;
4152 	int set = 1;
4153 
4154 	/*
4155 	 * Remove the STA entry to ensure the STA PS state gets cleared and
4156 	 * configuration gets updated. This is relevant for cases, such as
4157 	 * FT-over-the-DS, where a station re-associates back to the same AP but
4158 	 * skips the authentication flow, or if working with a driver that
4159 	 * does not support full AP client state.
4160 	 *
4161 	 * Skip this if the STA has already completed FT reassociation and the
4162 	 * TK has been configured since the TX/RX PN must not be reset to 0 for
4163 	 * the same key.
4164 	 *
4165 	 * FT-over-the-DS has a special case where the STA entry (and as such,
4166 	 * the TK) has not yet been configured to the driver depending on which
4167 	 * driver interface is used. For that case, allow add-STA operation to
4168 	 * be used (instead of set-STA). This is needed to allow mac80211-based
4169 	 * drivers to accept the STA parameter configuration. Since this is
4170 	 * after a new FT-over-DS exchange, a new TK has been derived, so key
4171 	 * reinstallation is not a concern for this case.
4172 	 */
4173 	wpa_printf(MSG_DEBUG, "Add associated STA " MACSTR
4174 		   " (added_unassoc=%d auth_alg=%u ft_over_ds=%u reassoc=%d authorized=%d ft_tk=%d fils_tk=%d)",
4175 		   MAC2STR(sta->addr), sta->added_unassoc, sta->auth_alg,
4176 		   sta->ft_over_ds, reassoc,
4177 		   !!(sta->flags & WLAN_STA_AUTHORIZED),
4178 		   wpa_auth_sta_ft_tk_already_set(sta->wpa_sm),
4179 		   wpa_auth_sta_fils_tk_already_set(sta->wpa_sm));
4180 
4181 	if (!sta->added_unassoc &&
4182 	    (!(sta->flags & WLAN_STA_AUTHORIZED) ||
4183 	     (reassoc && sta->ft_over_ds && sta->auth_alg == WLAN_AUTH_FT) ||
4184 	     (!wpa_auth_sta_ft_tk_already_set(sta->wpa_sm) &&
4185 	      !wpa_auth_sta_fils_tk_already_set(sta->wpa_sm)))) {
4186 		hostapd_drv_sta_remove(hapd, sta->addr);
4187 		wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
4188 		set = 0;
4189 
4190 		 /* Do not allow the FT-over-DS exception to be used more than
4191 		  * once per authentication exchange to guarantee a new TK is
4192 		  * used here */
4193 		sta->ft_over_ds = 0;
4194 	}
4195 
4196 	if (sta->flags & WLAN_STA_HT)
4197 		hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
4198 #ifdef CONFIG_IEEE80211AC
4199 	if (sta->flags & WLAN_STA_VHT)
4200 		hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
4201 #endif /* CONFIG_IEEE80211AC */
4202 #ifdef CONFIG_IEEE80211AX
4203 	if (sta->flags & WLAN_STA_HE) {
4204 		hostapd_get_he_capab(hapd, sta->he_capab, &he_cap,
4205 				     sta->he_capab_len);
4206 	}
4207 #endif /* CONFIG_IEEE80211AX */
4208 #ifdef CONFIG_IEEE80211BE
4209 	if (sta->flags & WLAN_STA_EHT)
4210 		hostapd_get_eht_capab(hapd, sta->eht_capab, &eht_cap,
4211 				      sta->eht_capab_len);
4212 #endif /* CONFIG_IEEE80211BE */
4213 
4214 	/*
4215 	 * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
4216 	 * will be set when the ACK frame for the (Re)Association Response frame
4217 	 * is processed (TX status driver event).
4218 	 */
4219 	if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
4220 			    sta->supported_rates, sta->supported_rates_len,
4221 			    sta->listen_interval,
4222 			    sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
4223 			    sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
4224 			    sta->flags & WLAN_STA_HE ? &he_cap : NULL,
4225 			    sta->flags & WLAN_STA_HE ? sta->he_capab_len : 0,
4226 			    sta->flags & WLAN_STA_EHT ? &eht_cap : NULL,
4227 			    sta->flags & WLAN_STA_EHT ? sta->eht_capab_len : 0,
4228 			    sta->he_6ghz_capab,
4229 			    sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
4230 			    sta->vht_opmode, sta->p2p_ie ? 1 : 0,
4231 			    set)) {
4232 		hostapd_logger(hapd, sta->addr,
4233 			       HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
4234 			       "Could not %s STA to kernel driver",
4235 			       set ? "set" : "add");
4236 
4237 		if (sta->added_unassoc) {
4238 			hostapd_drv_sta_remove(hapd, sta->addr);
4239 			sta->added_unassoc = 0;
4240 		}
4241 
4242 		return -1;
4243 	}
4244 
4245 	sta->added_unassoc = 0;
4246 
4247 	return 0;
4248 }
4249 
4250 
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)4251 static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
4252 			   const u8 *addr, u16 status_code, int reassoc,
4253 			   const u8 *ies, size_t ies_len, int rssi,
4254 			   int omit_rsnxe)
4255 {
4256 	int send_len;
4257 	u8 *buf;
4258 	size_t buflen;
4259 	struct ieee80211_mgmt *reply;
4260 	u8 *p;
4261 	u16 res = WLAN_STATUS_SUCCESS;
4262 
4263 	buflen = sizeof(struct ieee80211_mgmt) + 1024;
4264 #ifdef CONFIG_FILS
4265 	if (sta && sta->fils_hlp_resp)
4266 		buflen += wpabuf_len(sta->fils_hlp_resp);
4267 	if (sta)
4268 		buflen += 150;
4269 #endif /* CONFIG_FILS */
4270 #ifdef CONFIG_OWE
4271 	if (sta && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
4272 		buflen += 150;
4273 #endif /* CONFIG_OWE */
4274 #ifdef CONFIG_DPP2
4275 	if (sta && sta->dpp_pfs)
4276 		buflen += 5 + sta->dpp_pfs->curve->prime_len;
4277 #endif /* CONFIG_DPP2 */
4278 #ifdef CONFIG_IEEE80211BE
4279 	if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be) {
4280 		buflen += hostapd_eid_eht_capab_len(hapd, IEEE80211_MODE_AP);
4281 		buflen += 3 + sizeof(struct ieee80211_eht_operation);
4282 		if (hapd->iconf->punct_bitmap)
4283 			buflen += EHT_OPER_DISABLED_SUBCHAN_BITMAP_SIZE;
4284 	}
4285 #endif /* CONFIG_IEEE80211BE */
4286 
4287 	buf = os_zalloc(buflen);
4288 	if (!buf) {
4289 		res = WLAN_STATUS_UNSPECIFIED_FAILURE;
4290 		goto done;
4291 	}
4292 	reply = (struct ieee80211_mgmt *) buf;
4293 	reply->frame_control =
4294 		IEEE80211_FC(WLAN_FC_TYPE_MGMT,
4295 			     (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
4296 			      WLAN_FC_STYPE_ASSOC_RESP));
4297 	os_memcpy(reply->da, addr, ETH_ALEN);
4298 	os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
4299 	os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
4300 
4301 	send_len = IEEE80211_HDRLEN;
4302 	send_len += sizeof(reply->u.assoc_resp);
4303 	reply->u.assoc_resp.capab_info =
4304 		host_to_le16(hostapd_own_capab_info(hapd));
4305 	reply->u.assoc_resp.status_code = host_to_le16(status_code);
4306 
4307 	reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0) |
4308 					       BIT(14) | BIT(15));
4309 	/* Supported rates */
4310 	p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
4311 	/* Extended supported rates */
4312 	p = hostapd_eid_ext_supp_rates(hapd, p);
4313 
4314 	/* Radio measurement capabilities */
4315 	p = hostapd_eid_rm_enabled_capab(hapd, p, buf + buflen - p);
4316 
4317 #ifdef CONFIG_MBO
4318 	if (status_code == WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
4319 	    rssi != 0) {
4320 		int delta = hapd->iconf->rssi_reject_assoc_rssi - rssi;
4321 
4322 		p = hostapd_eid_mbo_rssi_assoc_rej(hapd, p, buf + buflen - p,
4323 						   delta);
4324 	}
4325 #endif /* CONFIG_MBO */
4326 
4327 #ifdef CONFIG_IEEE80211R_AP
4328 	if (sta && status_code == WLAN_STATUS_SUCCESS) {
4329 		/* IEEE 802.11r: Mobility Domain Information, Fast BSS
4330 		 * Transition Information, RSN, [RIC Response] */
4331 		p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
4332 						buf + buflen - p,
4333 						sta->auth_alg, ies, ies_len,
4334 						omit_rsnxe);
4335 		if (!p) {
4336 			wpa_printf(MSG_DEBUG,
4337 				   "FT: Failed to write AssocResp IEs");
4338 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
4339 			goto done;
4340 		}
4341 	}
4342 #endif /* CONFIG_IEEE80211R_AP */
4343 #ifdef CONFIG_FILS
4344 	if (sta && status_code == WLAN_STATUS_SUCCESS &&
4345 	    (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4346 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4347 	     sta->auth_alg == WLAN_AUTH_FILS_PK))
4348 		p = wpa_auth_write_assoc_resp_fils(sta->wpa_sm, p,
4349 						   buf + buflen - p,
4350 						   ies, ies_len);
4351 #endif /* CONFIG_FILS */
4352 
4353 #ifdef CONFIG_OWE
4354 	if (sta && status_code == WLAN_STATUS_SUCCESS &&
4355 	    (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
4356 		p = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, p,
4357 						  buf + buflen - p,
4358 						  ies, ies_len);
4359 #endif /* CONFIG_OWE */
4360 
4361 	if (sta && status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
4362 		p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
4363 
4364 	p = hostapd_eid_ht_capabilities(hapd, p);
4365 	p = hostapd_eid_ht_operation(hapd, p);
4366 
4367 #ifdef CONFIG_IEEE80211AC
4368 	if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac &&
4369 	    !is_6ghz_op_class(hapd->iconf->op_class)) {
4370 		u32 nsts = 0, sta_nsts;
4371 
4372 		if (sta && hapd->conf->use_sta_nsts && sta->vht_capabilities) {
4373 			struct ieee80211_vht_capabilities *capa;
4374 
4375 			nsts = (hapd->iface->conf->vht_capab >>
4376 				VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
4377 			capa = sta->vht_capabilities;
4378 			sta_nsts = (le_to_host32(capa->vht_capabilities_info) >>
4379 				    VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
4380 
4381 			if (nsts < sta_nsts)
4382 				nsts = 0;
4383 			else
4384 				nsts = sta_nsts;
4385 		}
4386 		p = hostapd_eid_vht_capabilities(hapd, p, nsts);
4387 		p = hostapd_eid_vht_operation(hapd, p);
4388 	}
4389 #endif /* CONFIG_IEEE80211AC */
4390 
4391 #ifdef CONFIG_IEEE80211AX
4392 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
4393 		p = hostapd_eid_he_capab(hapd, p, IEEE80211_MODE_AP);
4394 		p = hostapd_eid_he_operation(hapd, p);
4395 		p = hostapd_eid_cca(hapd, p);
4396 		p = hostapd_eid_spatial_reuse(hapd, p);
4397 		p = hostapd_eid_he_mu_edca_parameter_set(hapd, p);
4398 		p = hostapd_eid_he_6ghz_band_cap(hapd, p);
4399 	}
4400 #endif /* CONFIG_IEEE80211AX */
4401 
4402 	p = hostapd_eid_ext_capab(hapd, p, false);
4403 	p = hostapd_eid_bss_max_idle_period(hapd, p);
4404 	if (sta && sta->qos_map_enabled)
4405 		p = hostapd_eid_qos_map_set(hapd, p);
4406 
4407 #ifdef CONFIG_FST
4408 	if (hapd->iface->fst_ies) {
4409 		os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
4410 			  wpabuf_len(hapd->iface->fst_ies));
4411 		p += wpabuf_len(hapd->iface->fst_ies);
4412 	}
4413 #endif /* CONFIG_FST */
4414 
4415 #ifdef CONFIG_TESTING_OPTIONS
4416 	if (hapd->conf->rsnxe_override_ft &&
4417 	    buf + buflen - p >=
4418 	    (long int) wpabuf_len(hapd->conf->rsnxe_override_ft) &&
4419 	    sta && sta->auth_alg == WLAN_AUTH_FT) {
4420 		wpa_printf(MSG_DEBUG, "TESTING: RSNXE FT override");
4421 		os_memcpy(p, wpabuf_head(hapd->conf->rsnxe_override_ft),
4422 			  wpabuf_len(hapd->conf->rsnxe_override_ft));
4423 		p += wpabuf_len(hapd->conf->rsnxe_override_ft);
4424 		goto rsnxe_done;
4425 	}
4426 #endif /* CONFIG_TESTING_OPTIONS */
4427 	if (!omit_rsnxe)
4428 		p = hostapd_eid_rsnxe(hapd, p, buf + buflen - p);
4429 #ifdef CONFIG_TESTING_OPTIONS
4430 rsnxe_done:
4431 #endif /* CONFIG_TESTING_OPTIONS */
4432 
4433 #ifdef CONFIG_IEEE80211BE
4434 	if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be) {
4435 		p = hostapd_eid_eht_capab(hapd, p, IEEE80211_MODE_AP);
4436 		p = hostapd_eid_eht_operation(hapd, p);
4437 	}
4438 #endif /* CONFIG_IEEE80211BE */
4439 
4440 #ifdef CONFIG_OWE
4441 	if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
4442 	    sta && sta->owe_ecdh && status_code == WLAN_STATUS_SUCCESS &&
4443 	    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
4444 	    !wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
4445 		struct wpabuf *pub;
4446 
4447 		pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
4448 		if (!pub) {
4449 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
4450 			goto done;
4451 		}
4452 		/* OWE Diffie-Hellman Parameter element */
4453 		*p++ = WLAN_EID_EXTENSION; /* Element ID */
4454 		*p++ = 1 + 2 + wpabuf_len(pub); /* Length */
4455 		*p++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension */
4456 		WPA_PUT_LE16(p, sta->owe_group);
4457 		p += 2;
4458 		os_memcpy(p, wpabuf_head(pub), wpabuf_len(pub));
4459 		p += wpabuf_len(pub);
4460 		wpabuf_free(pub);
4461 	}
4462 #endif /* CONFIG_OWE */
4463 
4464 #ifdef CONFIG_DPP2
4465 	if (DPP_VERSION > 1 && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
4466 	    sta && sta->dpp_pfs && status_code == WLAN_STATUS_SUCCESS &&
4467 	    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP) {
4468 		os_memcpy(p, wpabuf_head(sta->dpp_pfs->ie),
4469 			  wpabuf_len(sta->dpp_pfs->ie));
4470 		p += wpabuf_len(sta->dpp_pfs->ie);
4471 	}
4472 #endif /* CONFIG_DPP2 */
4473 
4474 #ifdef CONFIG_IEEE80211AC
4475 	if (sta && hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
4476 		p = hostapd_eid_vendor_vht(hapd, p);
4477 #endif /* CONFIG_IEEE80211AC */
4478 
4479 	if (sta && (sta->flags & WLAN_STA_WMM))
4480 		p = hostapd_eid_wmm(hapd, p);
4481 
4482 #ifdef CONFIG_WPS
4483 	if (sta &&
4484 	    ((sta->flags & WLAN_STA_WPS) ||
4485 	     ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa))) {
4486 		struct wpabuf *wps = wps_build_assoc_resp_ie();
4487 		if (wps) {
4488 			os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
4489 			p += wpabuf_len(wps);
4490 			wpabuf_free(wps);
4491 		}
4492 	}
4493 #endif /* CONFIG_WPS */
4494 
4495 	if (sta && (sta->flags & WLAN_STA_MULTI_AP))
4496 		p = hostapd_eid_multi_ap(hapd, p);
4497 
4498 #ifdef CONFIG_P2P
4499 	if (sta && sta->p2p_ie && hapd->p2p_group) {
4500 		struct wpabuf *p2p_resp_ie;
4501 		enum p2p_status_code status;
4502 		switch (status_code) {
4503 		case WLAN_STATUS_SUCCESS:
4504 			status = P2P_SC_SUCCESS;
4505 			break;
4506 		case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
4507 			status = P2P_SC_FAIL_LIMIT_REACHED;
4508 			break;
4509 		default:
4510 			status = P2P_SC_FAIL_INVALID_PARAMS;
4511 			break;
4512 		}
4513 		p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
4514 		if (p2p_resp_ie) {
4515 			os_memcpy(p, wpabuf_head(p2p_resp_ie),
4516 				  wpabuf_len(p2p_resp_ie));
4517 			p += wpabuf_len(p2p_resp_ie);
4518 			wpabuf_free(p2p_resp_ie);
4519 		}
4520 	}
4521 #endif /* CONFIG_P2P */
4522 
4523 #ifdef CONFIG_P2P_MANAGER
4524 	if (hapd->conf->p2p & P2P_MANAGE)
4525 		p = hostapd_eid_p2p_manage(hapd, p);
4526 #endif /* CONFIG_P2P_MANAGER */
4527 
4528 	p = hostapd_eid_mbo(hapd, p, buf + buflen - p);
4529 
4530 	if (hapd->conf->assocresp_elements &&
4531 	    (size_t) (buf + buflen - p) >=
4532 	    wpabuf_len(hapd->conf->assocresp_elements)) {
4533 		os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
4534 			  wpabuf_len(hapd->conf->assocresp_elements));
4535 		p += wpabuf_len(hapd->conf->assocresp_elements);
4536 	}
4537 
4538 	send_len += p - reply->u.assoc_resp.variable;
4539 
4540 #ifdef CONFIG_FILS
4541 	if (sta &&
4542 	    (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4543 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4544 	     sta->auth_alg == WLAN_AUTH_FILS_PK) &&
4545 	    status_code == WLAN_STATUS_SUCCESS) {
4546 		struct ieee802_11_elems elems;
4547 
4548 		if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
4549 		    ParseFailed || !elems.fils_session) {
4550 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
4551 			goto done;
4552 		}
4553 
4554 		/* FILS Session */
4555 		*p++ = WLAN_EID_EXTENSION; /* Element ID */
4556 		*p++ = 1 + FILS_SESSION_LEN; /* Length */
4557 		*p++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
4558 		os_memcpy(p, elems.fils_session, FILS_SESSION_LEN);
4559 		send_len += 2 + 1 + FILS_SESSION_LEN;
4560 
4561 		send_len = fils_encrypt_assoc(sta->wpa_sm, buf, send_len,
4562 					      buflen, sta->fils_hlp_resp);
4563 		if (send_len < 0) {
4564 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
4565 			goto done;
4566 		}
4567 	}
4568 #endif /* CONFIG_FILS */
4569 
4570 	if (hostapd_drv_send_mlme(hapd, reply, send_len, 0, NULL, 0, 0) < 0) {
4571 		wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
4572 			   strerror(errno));
4573 		res = WLAN_STATUS_UNSPECIFIED_FAILURE;
4574 	}
4575 
4576 done:
4577 	os_free(buf);
4578 	return res;
4579 }
4580 
4581 
4582 #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)4583 u8 * owe_assoc_req_process(struct hostapd_data *hapd, struct sta_info *sta,
4584 			   const u8 *owe_dh, u8 owe_dh_len,
4585 			   u8 *owe_buf, size_t owe_buf_len, u16 *status)
4586 {
4587 #ifdef CONFIG_TESTING_OPTIONS
4588 	if (hapd->conf->own_ie_override) {
4589 		wpa_printf(MSG_DEBUG, "OWE: Using IE override");
4590 		*status = WLAN_STATUS_SUCCESS;
4591 		return wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
4592 						     owe_buf_len, NULL, 0);
4593 	}
4594 #endif /* CONFIG_TESTING_OPTIONS */
4595 
4596 	if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
4597 		wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
4598 		owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
4599 							owe_buf_len, NULL, 0);
4600 		*status = WLAN_STATUS_SUCCESS;
4601 		return owe_buf;
4602 	}
4603 
4604 	if (sta->owe_pmk && sta->external_dh_updated) {
4605 		wpa_printf(MSG_DEBUG, "OWE: Using previously derived PMK");
4606 		*status = WLAN_STATUS_SUCCESS;
4607 		return owe_buf;
4608 	}
4609 
4610 	*status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
4611 	if (*status != WLAN_STATUS_SUCCESS)
4612 		return NULL;
4613 
4614 	owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
4615 						owe_buf_len, NULL, 0);
4616 
4617 	if (sta->owe_ecdh && owe_buf) {
4618 		struct wpabuf *pub;
4619 
4620 		pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
4621 		if (!pub) {
4622 			*status = WLAN_STATUS_UNSPECIFIED_FAILURE;
4623 			return owe_buf;
4624 		}
4625 
4626 		/* OWE Diffie-Hellman Parameter element */
4627 		*owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
4628 		*owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
4629 		*owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
4630 							 */
4631 		WPA_PUT_LE16(owe_buf, sta->owe_group);
4632 		owe_buf += 2;
4633 		os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
4634 		owe_buf += wpabuf_len(pub);
4635 		wpabuf_free(pub);
4636 	}
4637 
4638 	return owe_buf;
4639 }
4640 #endif /* CONFIG_OWE */
4641 
4642 
4643 #ifdef CONFIG_FILS
4644 
fils_hlp_finish_assoc(struct hostapd_data * hapd,struct sta_info * sta)4645 void fils_hlp_finish_assoc(struct hostapd_data *hapd, struct sta_info *sta)
4646 {
4647 	u16 reply_res;
4648 
4649 	wpa_printf(MSG_DEBUG, "FILS: Finish association with " MACSTR,
4650 		   MAC2STR(sta->addr));
4651 	eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4652 	if (!sta->fils_pending_assoc_req)
4653 		return;
4654 	reply_res = send_assoc_resp(hapd, sta, sta->addr, WLAN_STATUS_SUCCESS,
4655 				    sta->fils_pending_assoc_is_reassoc,
4656 				    sta->fils_pending_assoc_req,
4657 				    sta->fils_pending_assoc_req_len, 0, 0);
4658 	os_free(sta->fils_pending_assoc_req);
4659 	sta->fils_pending_assoc_req = NULL;
4660 	sta->fils_pending_assoc_req_len = 0;
4661 	wpabuf_free(sta->fils_hlp_resp);
4662 	sta->fils_hlp_resp = NULL;
4663 	wpabuf_free(sta->hlp_dhcp_discover);
4664 	sta->hlp_dhcp_discover = NULL;
4665 
4666 	/*
4667 	 * Remove the station in case transmission of a success response fails.
4668 	 * At this point the station was already added associated to the driver.
4669 	 */
4670 	if (reply_res != WLAN_STATUS_SUCCESS)
4671 		hostapd_drv_sta_remove(hapd, sta->addr);
4672 }
4673 
4674 
fils_hlp_timeout(void * eloop_ctx,void * eloop_data)4675 void fils_hlp_timeout(void *eloop_ctx, void *eloop_data)
4676 {
4677 	struct hostapd_data *hapd = eloop_ctx;
4678 	struct sta_info *sta = eloop_data;
4679 
4680 	wpa_printf(MSG_DEBUG,
4681 		   "FILS: HLP response timeout - continue with association response for "
4682 		   MACSTR, MAC2STR(sta->addr));
4683 	if (sta->fils_drv_assoc_finish)
4684 		hostapd_notify_assoc_fils_finish(hapd, sta);
4685 	else
4686 		fils_hlp_finish_assoc(hapd, sta);
4687 }
4688 
4689 #endif /* CONFIG_FILS */
4690 
4691 
handle_assoc(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int reassoc,int rssi)4692 static void handle_assoc(struct hostapd_data *hapd,
4693 			 const struct ieee80211_mgmt *mgmt, size_t len,
4694 			 int reassoc, int rssi)
4695 {
4696 	u16 capab_info, listen_interval, seq_ctrl, fc;
4697 	int resp = WLAN_STATUS_SUCCESS;
4698 	u16 reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
4699 	const u8 *pos;
4700 	int left, i;
4701 	struct sta_info *sta;
4702 	u8 *tmp = NULL;
4703 #ifdef CONFIG_FILS
4704 	int delay_assoc = 0;
4705 #endif /* CONFIG_FILS */
4706 	int omit_rsnxe = 0;
4707 
4708 	if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
4709 				      sizeof(mgmt->u.assoc_req))) {
4710 		wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
4711 			   reassoc, (unsigned long) len);
4712 		return;
4713 	}
4714 
4715 #ifdef CONFIG_TESTING_OPTIONS
4716 	if (reassoc) {
4717 		if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
4718 		    drand48() < hapd->iconf->ignore_reassoc_probability) {
4719 			wpa_printf(MSG_INFO,
4720 				   "TESTING: ignoring reassoc request from "
4721 				   MACSTR, MAC2STR(mgmt->sa));
4722 			return;
4723 		}
4724 	} else {
4725 		if (hapd->iconf->ignore_assoc_probability > 0.0 &&
4726 		    drand48() < hapd->iconf->ignore_assoc_probability) {
4727 			wpa_printf(MSG_INFO,
4728 				   "TESTING: ignoring assoc request from "
4729 				   MACSTR, MAC2STR(mgmt->sa));
4730 			return;
4731 		}
4732 	}
4733 #endif /* CONFIG_TESTING_OPTIONS */
4734 
4735 	fc = le_to_host16(mgmt->frame_control);
4736 	seq_ctrl = le_to_host16(mgmt->seq_ctrl);
4737 
4738 	if (reassoc) {
4739 		capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
4740 		listen_interval = le_to_host16(
4741 			mgmt->u.reassoc_req.listen_interval);
4742 		wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
4743 			   " capab_info=0x%02x listen_interval=%d current_ap="
4744 			   MACSTR " seq_ctrl=0x%x%s",
4745 			   MAC2STR(mgmt->sa), capab_info, listen_interval,
4746 			   MAC2STR(mgmt->u.reassoc_req.current_ap),
4747 			   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
4748 		left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
4749 		pos = mgmt->u.reassoc_req.variable;
4750 	} else {
4751 		capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
4752 		listen_interval = le_to_host16(
4753 			mgmt->u.assoc_req.listen_interval);
4754 		wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
4755 			   " capab_info=0x%02x listen_interval=%d "
4756 			   "seq_ctrl=0x%x%s",
4757 			   MAC2STR(mgmt->sa), capab_info, listen_interval,
4758 			   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
4759 		left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
4760 		pos = mgmt->u.assoc_req.variable;
4761 	}
4762 
4763 	sta = ap_get_sta(hapd, mgmt->sa);
4764 #ifdef CONFIG_IEEE80211R_AP
4765 	if (sta && sta->auth_alg == WLAN_AUTH_FT &&
4766 	    (sta->flags & WLAN_STA_AUTH) == 0) {
4767 		wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
4768 			   "prior to authentication since it is using "
4769 			   "over-the-DS FT", MAC2STR(mgmt->sa));
4770 
4771 		/*
4772 		 * Mark station as authenticated, to avoid adding station
4773 		 * entry in the driver as associated and not authenticated
4774 		 */
4775 		sta->flags |= WLAN_STA_AUTH;
4776 	} else
4777 #endif /* CONFIG_IEEE80211R_AP */
4778 	if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
4779 		if (hapd->iface->current_mode &&
4780 		    hapd->iface->current_mode->mode ==
4781 			HOSTAPD_MODE_IEEE80211AD) {
4782 			int acl_res;
4783 			struct radius_sta info;
4784 
4785 			acl_res = ieee802_11_allowed_address(hapd, mgmt->sa,
4786 							     (const u8 *) mgmt,
4787 							     len, &info);
4788 			if (acl_res == HOSTAPD_ACL_REJECT) {
4789 				wpa_msg(hapd->msg_ctx, MSG_DEBUG,
4790 					"Ignore Association Request frame from "
4791 					MACSTR " due to ACL reject",
4792 					MAC2STR(mgmt->sa));
4793 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4794 				goto fail;
4795 			}
4796 			if (acl_res == HOSTAPD_ACL_PENDING)
4797 				return;
4798 
4799 			/* DMG/IEEE 802.11ad does not use authentication.
4800 			 * Allocate sta entry upon association. */
4801 			sta = ap_sta_add(hapd, mgmt->sa);
4802 			if (!sta) {
4803 				hostapd_logger(hapd, mgmt->sa,
4804 					       HOSTAPD_MODULE_IEEE80211,
4805 					       HOSTAPD_LEVEL_INFO,
4806 					       "Failed to add STA");
4807 				resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4808 				goto fail;
4809 			}
4810 
4811 			acl_res = ieee802_11_set_radius_info(
4812 				hapd, sta, acl_res, &info);
4813 			if (acl_res) {
4814 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4815 				goto fail;
4816 			}
4817 
4818 			hostapd_logger(hapd, sta->addr,
4819 				       HOSTAPD_MODULE_IEEE80211,
4820 				       HOSTAPD_LEVEL_DEBUG,
4821 				       "Skip authentication for DMG/IEEE 802.11ad");
4822 			sta->flags |= WLAN_STA_AUTH;
4823 			wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
4824 			sta->auth_alg = WLAN_AUTH_OPEN;
4825 		} else {
4826 			hostapd_logger(hapd, mgmt->sa,
4827 				       HOSTAPD_MODULE_IEEE80211,
4828 				       HOSTAPD_LEVEL_INFO,
4829 				       "Station tried to associate before authentication (aid=%d flags=0x%x)",
4830 				       sta ? sta->aid : -1,
4831 				       sta ? sta->flags : 0);
4832 			send_deauth(hapd, mgmt->sa,
4833 				    WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
4834 			return;
4835 		}
4836 	}
4837 
4838 	if ((fc & WLAN_FC_RETRY) &&
4839 	    sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4840 	    sta->last_seq_ctrl == seq_ctrl &&
4841 	    sta->last_subtype == (reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
4842 				  WLAN_FC_STYPE_ASSOC_REQ)) {
4843 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4844 			       HOSTAPD_LEVEL_DEBUG,
4845 			       "Drop repeated association frame seq_ctrl=0x%x",
4846 			       seq_ctrl);
4847 		return;
4848 	}
4849 	sta->last_seq_ctrl = seq_ctrl;
4850 	sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
4851 		WLAN_FC_STYPE_ASSOC_REQ;
4852 
4853 	if (hapd->tkip_countermeasures) {
4854 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4855 		goto fail;
4856 	}
4857 
4858 	if (listen_interval > hapd->conf->max_listen_interval) {
4859 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4860 			       HOSTAPD_LEVEL_DEBUG,
4861 			       "Too large Listen Interval (%d)",
4862 			       listen_interval);
4863 		resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
4864 		goto fail;
4865 	}
4866 
4867 #ifdef CONFIG_MBO
4868 	if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
4869 		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4870 		goto fail;
4871 	}
4872 
4873 	if (hapd->iconf->rssi_reject_assoc_rssi && rssi &&
4874 	    rssi < hapd->iconf->rssi_reject_assoc_rssi &&
4875 	    (sta->auth_rssi == 0 ||
4876 	     sta->auth_rssi < hapd->iconf->rssi_reject_assoc_rssi)) {
4877 		resp = WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS;
4878 		goto fail;
4879 	}
4880 #endif /* CONFIG_MBO */
4881 
4882 	/*
4883 	 * sta->capability is used in check_assoc_ies() for RRM enabled
4884 	 * capability element.
4885 	 */
4886 	sta->capability = capab_info;
4887 
4888 #ifdef CONFIG_FILS
4889 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4890 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4891 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
4892 		int res;
4893 
4894 		/* The end of the payload is encrypted. Need to decrypt it
4895 		 * before parsing. */
4896 
4897 		tmp = os_memdup(pos, left);
4898 		if (!tmp) {
4899 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4900 			goto fail;
4901 		}
4902 
4903 		res = fils_decrypt_assoc(sta->wpa_sm, sta->fils_session, mgmt,
4904 					 len, tmp, left);
4905 		if (res < 0) {
4906 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4907 			goto fail;
4908 		}
4909 		pos = tmp;
4910 		left = res;
4911 	}
4912 #endif /* CONFIG_FILS */
4913 
4914 	/* followed by SSID and Supported rates; and HT capabilities if 802.11n
4915 	 * is used */
4916 	resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
4917 	if (resp != WLAN_STATUS_SUCCESS)
4918 		goto fail;
4919 	omit_rsnxe = !get_ie(pos, left, WLAN_EID_RSNX);
4920 
4921 	if (hostapd_get_aid(hapd, sta) < 0) {
4922 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4923 			       HOSTAPD_LEVEL_INFO, "No room for more AIDs");
4924 		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4925 		goto fail;
4926 	}
4927 
4928 	sta->listen_interval = listen_interval;
4929 
4930 	if (hapd->iface->current_mode &&
4931 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
4932 		sta->flags |= WLAN_STA_NONERP;
4933 	for (i = 0; i < sta->supported_rates_len; i++) {
4934 		if ((sta->supported_rates[i] & 0x7f) > 22) {
4935 			sta->flags &= ~WLAN_STA_NONERP;
4936 			break;
4937 		}
4938 	}
4939 	if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
4940 		sta->nonerp_set = 1;
4941 		hapd->iface->num_sta_non_erp++;
4942 		if (hapd->iface->num_sta_non_erp == 1)
4943 			ieee802_11_set_beacons(hapd->iface);
4944 	}
4945 
4946 	if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
4947 	    !sta->no_short_slot_time_set) {
4948 		sta->no_short_slot_time_set = 1;
4949 		hapd->iface->num_sta_no_short_slot_time++;
4950 		if (hapd->iface->current_mode &&
4951 		    hapd->iface->current_mode->mode ==
4952 		    HOSTAPD_MODE_IEEE80211G &&
4953 		    hapd->iface->num_sta_no_short_slot_time == 1)
4954 			ieee802_11_set_beacons(hapd->iface);
4955 	}
4956 
4957 	if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4958 		sta->flags |= WLAN_STA_SHORT_PREAMBLE;
4959 	else
4960 		sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
4961 
4962 	if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
4963 	    !sta->no_short_preamble_set) {
4964 		sta->no_short_preamble_set = 1;
4965 		hapd->iface->num_sta_no_short_preamble++;
4966 		if (hapd->iface->current_mode &&
4967 		    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
4968 		    && hapd->iface->num_sta_no_short_preamble == 1)
4969 			ieee802_11_set_beacons(hapd->iface);
4970 	}
4971 
4972 	update_ht_state(hapd, sta);
4973 
4974 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4975 		       HOSTAPD_LEVEL_DEBUG,
4976 		       "association OK (aid %d)", sta->aid);
4977 	/* Station will be marked associated, after it acknowledges AssocResp
4978 	 */
4979 	sta->flags |= WLAN_STA_ASSOC_REQ_OK;
4980 
4981 	if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
4982 		wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
4983 			   "SA Query procedure", reassoc ? "re" : "");
4984 		/* TODO: Send a protected Disassociate frame to the STA using
4985 		 * the old key and Reason Code "Previous Authentication no
4986 		 * longer valid". Make sure this is only sent protected since
4987 		 * unprotected frame would be received by the STA that is now
4988 		 * trying to associate.
4989 		 */
4990 	}
4991 
4992 	/* Make sure that the previously registered inactivity timer will not
4993 	 * remove the STA immediately. */
4994 	sta->timeout_next = STA_NULLFUNC;
4995 
4996 #ifdef CONFIG_TAXONOMY
4997 	taxonomy_sta_info_assoc_req(hapd, sta, pos, left);
4998 #endif /* CONFIG_TAXONOMY */
4999 
5000 	sta->pending_wds_enable = 0;
5001 
5002 #ifdef CONFIG_FILS
5003 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
5004 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
5005 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
5006 		if (fils_process_hlp(hapd, sta, pos, left) > 0)
5007 			delay_assoc = 1;
5008 	}
5009 #endif /* CONFIG_FILS */
5010 
5011  fail:
5012 
5013 	/*
5014 	 * In case of a successful response, add the station to the driver.
5015 	 * Otherwise, the kernel may ignore Data frames before we process the
5016 	 * ACK frame (TX status). In case of a failure, this station will be
5017 	 * removed.
5018 	 *
5019 	 * Note that this is not compliant with the IEEE 802.11 standard that
5020 	 * states that a non-AP station should transition into the
5021 	 * authenticated/associated state only after the station acknowledges
5022 	 * the (Re)Association Response frame. However, still do this as:
5023 	 *
5024 	 * 1. In case the station does not acknowledge the (Re)Association
5025 	 *    Response frame, it will be removed.
5026 	 * 2. Data frames will be dropped in the kernel until the station is
5027 	 *    set into authorized state, and there are no significant known
5028 	 *    issues with processing other non-Data Class 3 frames during this
5029 	 *    window.
5030 	 */
5031 	if (resp == WLAN_STATUS_SUCCESS && sta &&
5032 	    add_associated_sta(hapd, sta, reassoc))
5033 		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
5034 
5035 #ifdef CONFIG_FILS
5036 	if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS &&
5037 	    eloop_is_timeout_registered(fils_hlp_timeout, hapd, sta) &&
5038 	    sta->fils_pending_assoc_req) {
5039 		/* Do not reschedule fils_hlp_timeout in case the station
5040 		 * retransmits (Re)Association Request frame while waiting for
5041 		 * the previously started FILS HLP wait, so that the timeout can
5042 		 * be determined from the first pending attempt. */
5043 		wpa_printf(MSG_DEBUG,
5044 			   "FILS: Continue waiting for HLP processing before sending (Re)Association Response frame to "
5045 			   MACSTR, MAC2STR(sta->addr));
5046 		os_free(tmp);
5047 		return;
5048 	}
5049 	if (sta) {
5050 		eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
5051 		os_free(sta->fils_pending_assoc_req);
5052 		sta->fils_pending_assoc_req = NULL;
5053 		sta->fils_pending_assoc_req_len = 0;
5054 		wpabuf_free(sta->fils_hlp_resp);
5055 		sta->fils_hlp_resp = NULL;
5056 	}
5057 	if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS) {
5058 		sta->fils_pending_assoc_req = tmp;
5059 		sta->fils_pending_assoc_req_len = left;
5060 		sta->fils_pending_assoc_is_reassoc = reassoc;
5061 		sta->fils_drv_assoc_finish = 0;
5062 		wpa_printf(MSG_DEBUG,
5063 			   "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
5064 			   MACSTR, MAC2STR(sta->addr));
5065 		eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
5066 		eloop_register_timeout(0, hapd->conf->fils_hlp_wait_time * 1024,
5067 				       fils_hlp_timeout, hapd, sta);
5068 		return;
5069 	}
5070 #endif /* CONFIG_FILS */
5071 
5072 	if (resp >= 0)
5073 		reply_res = send_assoc_resp(hapd, sta, mgmt->sa, resp, reassoc,
5074 					    pos, left, rssi, omit_rsnxe);
5075 	os_free(tmp);
5076 
5077 	/*
5078 	 * Remove the station in case transmission of a success response fails
5079 	 * (the STA was added associated to the driver) or if the station was
5080 	 * previously added unassociated.
5081 	 */
5082 	if (sta && ((reply_res != WLAN_STATUS_SUCCESS &&
5083 		     resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc)) {
5084 		hostapd_drv_sta_remove(hapd, sta->addr);
5085 		sta->added_unassoc = 0;
5086 	}
5087 }
5088 
5089 
handle_disassoc(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len)5090 static void handle_disassoc(struct hostapd_data *hapd,
5091 			    const struct ieee80211_mgmt *mgmt, size_t len)
5092 {
5093 	struct sta_info *sta;
5094 
5095 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
5096 		wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
5097 			   (unsigned long) len);
5098 		return;
5099 	}
5100 
5101 	wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
5102 		   MAC2STR(mgmt->sa),
5103 		   le_to_host16(mgmt->u.disassoc.reason_code));
5104 
5105 	sta = ap_get_sta(hapd, mgmt->sa);
5106 	if (sta == NULL) {
5107 		wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
5108 			   MAC2STR(mgmt->sa));
5109 		return;
5110 	}
5111 
5112 	ap_sta_set_authorized(hapd, sta, 0);
5113 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
5114 	sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
5115 	hostapd_set_sta_flags(hapd, sta);
5116 	wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
5117 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5118 		       HOSTAPD_LEVEL_INFO, "disassociated");
5119 	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
5120 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
5121 	/* Stop Accounting and IEEE 802.1X sessions, but leave the STA
5122 	 * authenticated. */
5123 	accounting_sta_stop(hapd, sta);
5124 	ieee802_1x_free_station(hapd, sta);
5125 	if (sta->ipaddr)
5126 		hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
5127 	ap_sta_ip6addr_del(hapd, sta);
5128 	hostapd_drv_sta_remove(hapd, sta->addr);
5129 	sta->added_unassoc = 0;
5130 
5131 	if (sta->timeout_next == STA_NULLFUNC ||
5132 	    sta->timeout_next == STA_DISASSOC) {
5133 		sta->timeout_next = STA_DEAUTH;
5134 		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
5135 		eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
5136 				       hapd, sta);
5137 	}
5138 
5139 	mlme_disassociate_indication(
5140 		hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
5141 
5142 	/* DMG/IEEE 802.11ad does not use deauthication. Deallocate sta upon
5143 	 * disassociation. */
5144 	if (hapd->iface->current_mode &&
5145 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
5146 		sta->flags &= ~WLAN_STA_AUTH;
5147 		wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
5148 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5149 			       HOSTAPD_LEVEL_DEBUG, "deauthenticated");
5150 		ap_free_sta(hapd, sta);
5151 	}
5152 }
5153 
5154 
handle_deauth(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len)5155 static void handle_deauth(struct hostapd_data *hapd,
5156 			  const struct ieee80211_mgmt *mgmt, size_t len)
5157 {
5158 	struct sta_info *sta;
5159 
5160 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
5161 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
5162 			"payload (len=%lu)", (unsigned long) len);
5163 		return;
5164 	}
5165 
5166 	wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
5167 		" reason_code=%d",
5168 		MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
5169 
5170 	/* Clear the PTKSA cache entries for PASN */
5171 	ptksa_cache_flush(hapd->ptksa, mgmt->sa, WPA_CIPHER_NONE);
5172 
5173 	sta = ap_get_sta(hapd, mgmt->sa);
5174 	if (sta == NULL) {
5175 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
5176 			"to deauthenticate, but it is not authenticated",
5177 			MAC2STR(mgmt->sa));
5178 		return;
5179 	}
5180 
5181 	ap_sta_set_authorized(hapd, sta, 0);
5182 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
5183 	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
5184 			WLAN_STA_ASSOC_REQ_OK);
5185 	hostapd_set_sta_flags(hapd, sta);
5186 	wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
5187 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5188 		       HOSTAPD_LEVEL_DEBUG, "deauthenticated");
5189 	mlme_deauthenticate_indication(
5190 		hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
5191 	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
5192 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
5193 	ap_free_sta(hapd, sta);
5194 }
5195 
5196 
handle_beacon(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,struct hostapd_frame_info * fi)5197 static void handle_beacon(struct hostapd_data *hapd,
5198 			  const struct ieee80211_mgmt *mgmt, size_t len,
5199 			  struct hostapd_frame_info *fi)
5200 {
5201 	struct ieee802_11_elems elems;
5202 
5203 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
5204 		wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
5205 			   (unsigned long) len);
5206 		return;
5207 	}
5208 
5209 	(void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
5210 				      len - (IEEE80211_HDRLEN +
5211 					     sizeof(mgmt->u.beacon)), &elems,
5212 				      0);
5213 
5214 	ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
5215 }
5216 
5217 
robust_action_frame(u8 category)5218 static int robust_action_frame(u8 category)
5219 {
5220 	return category != WLAN_ACTION_PUBLIC &&
5221 		category != WLAN_ACTION_HT;
5222 }
5223 
5224 
handle_action(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,unsigned int freq)5225 static int handle_action(struct hostapd_data *hapd,
5226 			 const struct ieee80211_mgmt *mgmt, size_t len,
5227 			 unsigned int freq)
5228 {
5229 	struct sta_info *sta;
5230 	u8 *action __maybe_unused;
5231 
5232 	if (len < IEEE80211_HDRLEN + 2 + 1) {
5233 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
5234 			       HOSTAPD_LEVEL_DEBUG,
5235 			       "handle_action - too short payload (len=%lu)",
5236 			       (unsigned long) len);
5237 		return 0;
5238 	}
5239 
5240 	action = (u8 *) &mgmt->u.action.u;
5241 	wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
5242 		   " da " MACSTR " len %d freq %u",
5243 		   mgmt->u.action.category, *action,
5244 		   MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) len, freq);
5245 
5246 	sta = ap_get_sta(hapd, mgmt->sa);
5247 
5248 	if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
5249 	    (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
5250 		wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
5251 			   "frame (category=%u) from unassociated STA " MACSTR,
5252 			   mgmt->u.action.category, MAC2STR(mgmt->sa));
5253 		return 0;
5254 	}
5255 
5256 	if (sta && (sta->flags & WLAN_STA_MFP) &&
5257 	    !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
5258 	    robust_action_frame(mgmt->u.action.category)) {
5259 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
5260 			       HOSTAPD_LEVEL_DEBUG,
5261 			       "Dropped unprotected Robust Action frame from "
5262 			       "an MFP STA");
5263 		return 0;
5264 	}
5265 
5266 	if (sta) {
5267 		u16 fc = le_to_host16(mgmt->frame_control);
5268 		u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
5269 
5270 		if ((fc & WLAN_FC_RETRY) &&
5271 		    sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
5272 		    sta->last_seq_ctrl == seq_ctrl &&
5273 		    sta->last_subtype == WLAN_FC_STYPE_ACTION) {
5274 			hostapd_logger(hapd, sta->addr,
5275 				       HOSTAPD_MODULE_IEEE80211,
5276 				       HOSTAPD_LEVEL_DEBUG,
5277 				       "Drop repeated action frame seq_ctrl=0x%x",
5278 				       seq_ctrl);
5279 			return 1;
5280 		}
5281 
5282 		sta->last_seq_ctrl = seq_ctrl;
5283 		sta->last_subtype = WLAN_FC_STYPE_ACTION;
5284 	}
5285 
5286 	switch (mgmt->u.action.category) {
5287 #ifdef CONFIG_IEEE80211R_AP
5288 	case WLAN_ACTION_FT:
5289 		if (!sta ||
5290 		    wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
5291 				     len - IEEE80211_HDRLEN))
5292 			break;
5293 		return 1;
5294 #endif /* CONFIG_IEEE80211R_AP */
5295 	case WLAN_ACTION_WMM:
5296 		hostapd_wmm_action(hapd, mgmt, len);
5297 		return 1;
5298 	case WLAN_ACTION_SA_QUERY:
5299 		ieee802_11_sa_query_action(hapd, mgmt, len);
5300 		return 1;
5301 #ifdef CONFIG_WNM_AP
5302 	case WLAN_ACTION_WNM:
5303 		ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
5304 		return 1;
5305 #endif /* CONFIG_WNM_AP */
5306 #ifdef CONFIG_FST
5307 	case WLAN_ACTION_FST:
5308 		if (hapd->iface->fst)
5309 			fst_rx_action(hapd->iface->fst, mgmt, len);
5310 		else
5311 			wpa_printf(MSG_DEBUG,
5312 				   "FST: Ignore FST Action frame - no FST attached");
5313 		return 1;
5314 #endif /* CONFIG_FST */
5315 	case WLAN_ACTION_PUBLIC:
5316 	case WLAN_ACTION_PROTECTED_DUAL:
5317 		if (len >= IEEE80211_HDRLEN + 2 &&
5318 		    mgmt->u.action.u.public_action.action ==
5319 		    WLAN_PA_20_40_BSS_COEX) {
5320 			hostapd_2040_coex_action(hapd, mgmt, len);
5321 			return 1;
5322 		}
5323 #ifdef CONFIG_DPP
5324 		if (len >= IEEE80211_HDRLEN + 6 &&
5325 		    mgmt->u.action.u.vs_public_action.action ==
5326 		    WLAN_PA_VENDOR_SPECIFIC &&
5327 		    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
5328 		    OUI_WFA &&
5329 		    mgmt->u.action.u.vs_public_action.variable[0] ==
5330 		    DPP_OUI_TYPE) {
5331 			const u8 *pos, *end;
5332 
5333 			pos = mgmt->u.action.u.vs_public_action.oui;
5334 			end = ((const u8 *) mgmt) + len;
5335 			hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
5336 					      freq);
5337 			return 1;
5338 		}
5339 		if (len >= IEEE80211_HDRLEN + 2 &&
5340 		    (mgmt->u.action.u.public_action.action ==
5341 		     WLAN_PA_GAS_INITIAL_RESP ||
5342 		     mgmt->u.action.u.public_action.action ==
5343 		     WLAN_PA_GAS_COMEBACK_RESP)) {
5344 			const u8 *pos, *end;
5345 
5346 			pos = &mgmt->u.action.u.public_action.action;
5347 			end = ((const u8 *) mgmt) + len;
5348 			if (gas_query_ap_rx(hapd->gas, mgmt->sa,
5349 					    mgmt->u.action.category,
5350 					    pos, end - pos, freq) == 0)
5351 				return 1;
5352 		}
5353 #endif /* CONFIG_DPP */
5354 		if (hapd->public_action_cb) {
5355 			hapd->public_action_cb(hapd->public_action_cb_ctx,
5356 					       (u8 *) mgmt, len, freq);
5357 		}
5358 		if (hapd->public_action_cb2) {
5359 			hapd->public_action_cb2(hapd->public_action_cb2_ctx,
5360 						(u8 *) mgmt, len, freq);
5361 		}
5362 		if (hapd->public_action_cb || hapd->public_action_cb2)
5363 			return 1;
5364 		break;
5365 	case WLAN_ACTION_VENDOR_SPECIFIC:
5366 		if (hapd->vendor_action_cb) {
5367 			if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
5368 						   (u8 *) mgmt, len, freq) == 0)
5369 				return 1;
5370 		}
5371 		break;
5372 	case WLAN_ACTION_RADIO_MEASUREMENT:
5373 		hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
5374 		return 1;
5375 	}
5376 
5377 	hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
5378 		       HOSTAPD_LEVEL_DEBUG,
5379 		       "handle_action - unknown action category %d or invalid "
5380 		       "frame",
5381 		       mgmt->u.action.category);
5382 	if (!is_multicast_ether_addr(mgmt->da) &&
5383 	    !(mgmt->u.action.category & 0x80) &&
5384 	    !is_multicast_ether_addr(mgmt->sa)) {
5385 		struct ieee80211_mgmt *resp;
5386 
5387 		/*
5388 		 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
5389 		 * Return the Action frame to the source without change
5390 		 * except that MSB of the Category set to 1.
5391 		 */
5392 		wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
5393 			   "frame back to sender");
5394 		resp = os_memdup(mgmt, len);
5395 		if (resp == NULL)
5396 			return 0;
5397 		os_memcpy(resp->da, resp->sa, ETH_ALEN);
5398 		os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
5399 		os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
5400 		resp->u.action.category |= 0x80;
5401 
5402 		if (hostapd_drv_send_mlme(hapd, resp, len, 0, NULL, 0, 0) < 0) {
5403 			wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
5404 				   "Action frame");
5405 		}
5406 		os_free(resp);
5407 	}
5408 
5409 	return 1;
5410 }
5411 
5412 
5413 /**
5414  * notify_mgmt_frame - Notify of Management frames on the control interface
5415  * @hapd: hostapd BSS data structure (the BSS to which the Management frame was
5416  * sent to)
5417  * @buf: Management frame data (starting from the IEEE 802.11 header)
5418  * @len: Length of frame data in octets
5419  *
5420  * Notify the control interface of any received Management frame.
5421  */
notify_mgmt_frame(struct hostapd_data * hapd,const u8 * buf,size_t len)5422 static void notify_mgmt_frame(struct hostapd_data *hapd, const u8 *buf,
5423 			      size_t len)
5424 {
5425 
5426 	int hex_len = len * 2 + 1;
5427 	char *hex = os_malloc(hex_len);
5428 
5429 	if (hex) {
5430 		wpa_snprintf_hex(hex, hex_len, buf, len);
5431 		wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO,
5432 			     AP_MGMT_FRAME_RECEIVED "buf=%s", hex);
5433 		os_free(hex);
5434 	}
5435 }
5436 
5437 
5438 /**
5439  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
5440  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
5441  * sent to)
5442  * @buf: management frame data (starting from IEEE 802.11 header)
5443  * @len: length of frame data in octets
5444  * @fi: meta data about received frame (signal level, etc.)
5445  *
5446  * Process all incoming IEEE 802.11 management frames. This will be called for
5447  * each frame received from the kernel driver through wlan#ap interface. In
5448  * addition, it can be called to re-inserted pending frames (e.g., when using
5449  * external RADIUS server as an MAC ACL).
5450  */
ieee802_11_mgmt(struct hostapd_data * hapd,const u8 * buf,size_t len,struct hostapd_frame_info * fi)5451 int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
5452 		    struct hostapd_frame_info *fi)
5453 {
5454 	struct ieee80211_mgmt *mgmt;
5455 	u16 fc, stype;
5456 	int ret = 0;
5457 	unsigned int freq;
5458 	int ssi_signal = fi ? fi->ssi_signal : 0;
5459 
5460 	if (len < 24)
5461 		return 0;
5462 
5463 	if (fi && fi->freq)
5464 		freq = fi->freq;
5465 	else
5466 		freq = hapd->iface->freq;
5467 
5468 	mgmt = (struct ieee80211_mgmt *) buf;
5469 	fc = le_to_host16(mgmt->frame_control);
5470 	stype = WLAN_FC_GET_STYPE(fc);
5471 
5472 	if (is_multicast_ether_addr(mgmt->sa) ||
5473 	    is_zero_ether_addr(mgmt->sa) ||
5474 	    os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
5475 		/* Do not process any frames with unexpected/invalid SA so that
5476 		 * we do not add any state for unexpected STA addresses or end
5477 		 * up sending out frames to unexpected destination. */
5478 		wpa_printf(MSG_DEBUG, "MGMT: Invalid SA=" MACSTR
5479 			   " in received frame - ignore this frame silently",
5480 			   MAC2STR(mgmt->sa));
5481 		return 0;
5482 	}
5483 
5484 	if (stype == WLAN_FC_STYPE_BEACON) {
5485 		handle_beacon(hapd, mgmt, len, fi);
5486 		return 1;
5487 	}
5488 
5489 	if (!is_broadcast_ether_addr(mgmt->bssid) &&
5490 #ifdef CONFIG_P2P
5491 	    /* Invitation responses can be sent with the peer MAC as BSSID */
5492 	    !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
5493 	      stype == WLAN_FC_STYPE_ACTION) &&
5494 #endif /* CONFIG_P2P */
5495 #ifdef CONFIG_MESH
5496 	    !(hapd->conf->mesh & MESH_ENABLED) &&
5497 #endif /* CONFIG_MESH */
5498 	    os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
5499 		wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
5500 			   MAC2STR(mgmt->bssid));
5501 		return 0;
5502 	}
5503 
5504 	if (hapd->iface->state != HAPD_IFACE_ENABLED) {
5505 		wpa_printf(MSG_DEBUG, "MGMT: Ignore management frame while interface is not enabled (SA=" MACSTR " DA=" MACSTR " subtype=%u)",
5506 			   MAC2STR(mgmt->sa), MAC2STR(mgmt->da), stype);
5507 		return 1;
5508 	}
5509 
5510 	if (stype == WLAN_FC_STYPE_PROBE_REQ) {
5511 		handle_probe_req(hapd, mgmt, len, ssi_signal);
5512 		return 1;
5513 	}
5514 
5515 	if ((!is_broadcast_ether_addr(mgmt->da) ||
5516 	     stype != WLAN_FC_STYPE_ACTION) &&
5517 	    os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
5518 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
5519 			       HOSTAPD_LEVEL_DEBUG,
5520 			       "MGMT: DA=" MACSTR " not our address",
5521 			       MAC2STR(mgmt->da));
5522 		return 0;
5523 	}
5524 
5525 	if (hapd->iconf->track_sta_max_num)
5526 		sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
5527 
5528 	if (hapd->conf->notify_mgmt_frames)
5529 		notify_mgmt_frame(hapd, buf, len);
5530 
5531 	switch (stype) {
5532 	case WLAN_FC_STYPE_AUTH:
5533 		wpa_printf(MSG_DEBUG, "mgmt::auth");
5534 		handle_auth(hapd, mgmt, len, ssi_signal, 0);
5535 		ret = 1;
5536 		break;
5537 	case WLAN_FC_STYPE_ASSOC_REQ:
5538 		wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
5539 		handle_assoc(hapd, mgmt, len, 0, ssi_signal);
5540 		ret = 1;
5541 		break;
5542 	case WLAN_FC_STYPE_REASSOC_REQ:
5543 		wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
5544 		handle_assoc(hapd, mgmt, len, 1, ssi_signal);
5545 		ret = 1;
5546 		break;
5547 	case WLAN_FC_STYPE_DISASSOC:
5548 		wpa_printf(MSG_DEBUG, "mgmt::disassoc");
5549 		handle_disassoc(hapd, mgmt, len);
5550 		ret = 1;
5551 		break;
5552 	case WLAN_FC_STYPE_DEAUTH:
5553 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
5554 		handle_deauth(hapd, mgmt, len);
5555 		ret = 1;
5556 		break;
5557 	case WLAN_FC_STYPE_ACTION:
5558 		wpa_printf(MSG_DEBUG, "mgmt::action");
5559 		ret = handle_action(hapd, mgmt, len, freq);
5560 		break;
5561 	default:
5562 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
5563 			       HOSTAPD_LEVEL_DEBUG,
5564 			       "unknown mgmt frame subtype %d", stype);
5565 		break;
5566 	}
5567 
5568 	return ret;
5569 }
5570 
5571 
handle_auth_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)5572 static void handle_auth_cb(struct hostapd_data *hapd,
5573 			   const struct ieee80211_mgmt *mgmt,
5574 			   size_t len, int ok)
5575 {
5576 	u16 auth_alg, auth_transaction, status_code;
5577 	struct sta_info *sta;
5578 	bool success_status;
5579 
5580 	sta = ap_get_sta(hapd, mgmt->da);
5581 	if (!sta) {
5582 		wpa_printf(MSG_DEBUG, "handle_auth_cb: STA " MACSTR
5583 			   " not found",
5584 			   MAC2STR(mgmt->da));
5585 		return;
5586 	}
5587 
5588 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
5589 		wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
5590 			   (unsigned long) len);
5591 		auth_alg = 0;
5592 		auth_transaction = 0;
5593 		status_code = WLAN_STATUS_UNSPECIFIED_FAILURE;
5594 		goto fail;
5595 	}
5596 
5597 	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5598 	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
5599 	status_code = le_to_host16(mgmt->u.auth.status_code);
5600 
5601 	if (!ok) {
5602 		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
5603 			       HOSTAPD_LEVEL_NOTICE,
5604 			       "did not acknowledge authentication response");
5605 		goto fail;
5606 	}
5607 
5608 	if (status_code == WLAN_STATUS_SUCCESS &&
5609 	    ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
5610 	     (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
5611 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5612 			       HOSTAPD_LEVEL_INFO, "authenticated");
5613 		sta->flags |= WLAN_STA_AUTH;
5614 		if (sta->added_unassoc)
5615 			hostapd_set_sta_flags(hapd, sta);
5616 		return;
5617 	}
5618 
5619 fail:
5620 	success_status = status_code == WLAN_STATUS_SUCCESS;
5621 #ifdef CONFIG_SAE
5622 	if (auth_alg == WLAN_AUTH_SAE && auth_transaction == 1)
5623 		success_status = sae_status_success(hapd, status_code);
5624 #endif /* CONFIG_SAE */
5625 	if (!success_status && sta->added_unassoc) {
5626 		hostapd_drv_sta_remove(hapd, sta->addr);
5627 		sta->added_unassoc = 0;
5628 	}
5629 }
5630 
5631 
hostapd_set_wds_encryption(struct hostapd_data * hapd,struct sta_info * sta,char * ifname_wds)5632 static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
5633 				       struct sta_info *sta,
5634 				       char *ifname_wds)
5635 {
5636 #ifdef CONFIG_WEP
5637 	int i;
5638 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
5639 
5640 	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
5641 		return;
5642 
5643 	for (i = 0; i < 4; i++) {
5644 		if (ssid->wep.key[i] &&
5645 		    hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
5646 					0, i == ssid->wep.idx, NULL, 0,
5647 					ssid->wep.key[i], ssid->wep.len[i],
5648 					i == ssid->wep.idx ?
5649 					KEY_FLAG_GROUP_RX_TX_DEFAULT :
5650 					KEY_FLAG_GROUP_RX_TX)) {
5651 			wpa_printf(MSG_WARNING,
5652 				   "Could not set WEP keys for WDS interface; %s",
5653 				   ifname_wds);
5654 			break;
5655 		}
5656 	}
5657 #endif /* CONFIG_WEP */
5658 }
5659 
5660 
handle_assoc_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int reassoc,int ok)5661 static void handle_assoc_cb(struct hostapd_data *hapd,
5662 			    const struct ieee80211_mgmt *mgmt,
5663 			    size_t len, int reassoc, int ok)
5664 {
5665 	u16 status;
5666 	struct sta_info *sta;
5667 	int new_assoc = 1;
5668 
5669 	sta = ap_get_sta(hapd, mgmt->da);
5670 	if (!sta) {
5671 		wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
5672 			   MAC2STR(mgmt->da));
5673 		return;
5674 	}
5675 
5676 	if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
5677 				      sizeof(mgmt->u.assoc_resp))) {
5678 		wpa_printf(MSG_INFO,
5679 			   "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
5680 			   reassoc, (unsigned long) len);
5681 		hostapd_drv_sta_remove(hapd, sta->addr);
5682 		return;
5683 	}
5684 
5685 	if (reassoc)
5686 		status = le_to_host16(mgmt->u.reassoc_resp.status_code);
5687 	else
5688 		status = le_to_host16(mgmt->u.assoc_resp.status_code);
5689 
5690 	if (!ok) {
5691 		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
5692 			       HOSTAPD_LEVEL_DEBUG,
5693 			       "did not acknowledge association response");
5694 		sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
5695 		/* The STA is added only in case of SUCCESS */
5696 		if (status == WLAN_STATUS_SUCCESS)
5697 			hostapd_drv_sta_remove(hapd, sta->addr);
5698 
5699 		return;
5700 	}
5701 
5702 	if (status != WLAN_STATUS_SUCCESS)
5703 		return;
5704 
5705 	/* Stop previous accounting session, if one is started, and allocate
5706 	 * new session id for the new session. */
5707 	accounting_sta_stop(hapd, sta);
5708 
5709 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5710 		       HOSTAPD_LEVEL_INFO,
5711 		       "associated (aid %d)",
5712 		       sta->aid);
5713 
5714 	if (sta->flags & WLAN_STA_ASSOC)
5715 		new_assoc = 0;
5716 	sta->flags |= WLAN_STA_ASSOC;
5717 	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
5718 	if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
5719 	     !hapd->conf->osen) ||
5720 	    sta->auth_alg == WLAN_AUTH_FILS_SK ||
5721 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
5722 	    sta->auth_alg == WLAN_AUTH_FILS_PK ||
5723 	    sta->auth_alg == WLAN_AUTH_FT) {
5724 		/*
5725 		 * Open, static WEP, FT protocol, or FILS; no separate
5726 		 * authorization step.
5727 		 */
5728 		ap_sta_set_authorized(hapd, sta, 1);
5729 	}
5730 
5731 	if (reassoc)
5732 		mlme_reassociate_indication(hapd, sta);
5733 	else
5734 		mlme_associate_indication(hapd, sta);
5735 
5736 	sta->sa_query_timed_out = 0;
5737 
5738 	if (sta->eapol_sm == NULL) {
5739 		/*
5740 		 * This STA does not use RADIUS server for EAP authentication,
5741 		 * so bind it to the selected VLAN interface now, since the
5742 		 * interface selection is not going to change anymore.
5743 		 */
5744 		if (ap_sta_bind_vlan(hapd, sta) < 0)
5745 			return;
5746 	} else if (sta->vlan_id) {
5747 		/* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
5748 		if (ap_sta_bind_vlan(hapd, sta) < 0)
5749 			return;
5750 	}
5751 
5752 	hostapd_set_sta_flags(hapd, sta);
5753 
5754 	if (!(sta->flags & WLAN_STA_WDS) && sta->pending_wds_enable) {
5755 		wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for STA "
5756 			   MACSTR " based on pending request",
5757 			   MAC2STR(sta->addr));
5758 		sta->pending_wds_enable = 0;
5759 		sta->flags |= WLAN_STA_WDS;
5760 	}
5761 
5762 	/* WPS not supported on backhaul BSS. Disable 4addr mode on fronthaul */
5763 	if ((sta->flags & WLAN_STA_WDS) ||
5764 	    (sta->flags & WLAN_STA_MULTI_AP &&
5765 	     !(hapd->conf->multi_ap & FRONTHAUL_BSS) &&
5766 	     !(sta->flags & WLAN_STA_WPS))) {
5767 		int ret;
5768 		char ifname_wds[IFNAMSIZ + 1];
5769 
5770 		wpa_printf(MSG_DEBUG, "Reenable 4-address WDS mode for STA "
5771 			   MACSTR " (aid %u)",
5772 			   MAC2STR(sta->addr), sta->aid);
5773 		ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
5774 					  sta->aid, 1);
5775 		if (!ret)
5776 			hostapd_set_wds_encryption(hapd, sta, ifname_wds);
5777 	}
5778 
5779 	if (sta->auth_alg == WLAN_AUTH_FT)
5780 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
5781 	else
5782 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
5783 	hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
5784 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
5785 
5786 #ifdef CONFIG_FILS
5787 	if ((sta->auth_alg == WLAN_AUTH_FILS_SK ||
5788 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
5789 	     sta->auth_alg == WLAN_AUTH_FILS_PK) &&
5790 	    fils_set_tk(sta->wpa_sm) < 0) {
5791 		wpa_printf(MSG_DEBUG, "FILS: TK configuration failed");
5792 		ap_sta_disconnect(hapd, sta, sta->addr,
5793 				  WLAN_REASON_UNSPECIFIED);
5794 		return;
5795 	}
5796 #endif /* CONFIG_FILS */
5797 
5798 	if (sta->pending_eapol_rx) {
5799 		struct os_reltime now, age;
5800 
5801 		os_get_reltime(&now);
5802 		os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
5803 		if (age.sec == 0 && age.usec < 200000) {
5804 			wpa_printf(MSG_DEBUG,
5805 				   "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
5806 				   MAC2STR(sta->addr));
5807 			ieee802_1x_receive(
5808 				hapd, mgmt->da,
5809 				wpabuf_head(sta->pending_eapol_rx->buf),
5810 				wpabuf_len(sta->pending_eapol_rx->buf),
5811 				sta->pending_eapol_rx->encrypted);
5812 		}
5813 		wpabuf_free(sta->pending_eapol_rx->buf);
5814 		os_free(sta->pending_eapol_rx);
5815 		sta->pending_eapol_rx = NULL;
5816 	}
5817 }
5818 
5819 
handle_deauth_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)5820 static void handle_deauth_cb(struct hostapd_data *hapd,
5821 			     const struct ieee80211_mgmt *mgmt,
5822 			     size_t len, int ok)
5823 {
5824 	struct sta_info *sta;
5825 	if (is_multicast_ether_addr(mgmt->da))
5826 		return;
5827 	sta = ap_get_sta(hapd, mgmt->da);
5828 	if (!sta) {
5829 		wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
5830 			   " not found", MAC2STR(mgmt->da));
5831 		return;
5832 	}
5833 	if (ok)
5834 		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
5835 			   MAC2STR(sta->addr));
5836 	else
5837 		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
5838 			   "deauth", MAC2STR(sta->addr));
5839 
5840 	ap_sta_deauth_cb(hapd, sta);
5841 }
5842 
5843 
handle_disassoc_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)5844 static void handle_disassoc_cb(struct hostapd_data *hapd,
5845 			       const struct ieee80211_mgmt *mgmt,
5846 			       size_t len, int ok)
5847 {
5848 	struct sta_info *sta;
5849 	if (is_multicast_ether_addr(mgmt->da))
5850 		return;
5851 	sta = ap_get_sta(hapd, mgmt->da);
5852 	if (!sta) {
5853 		wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
5854 			   " not found", MAC2STR(mgmt->da));
5855 		return;
5856 	}
5857 	if (ok)
5858 		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
5859 			   MAC2STR(sta->addr));
5860 	else
5861 		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
5862 			   "disassoc", MAC2STR(sta->addr));
5863 
5864 	ap_sta_disassoc_cb(hapd, sta);
5865 }
5866 
5867 
handle_action_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)5868 static void handle_action_cb(struct hostapd_data *hapd,
5869 			     const struct ieee80211_mgmt *mgmt,
5870 			     size_t len, int ok)
5871 {
5872 	struct sta_info *sta;
5873 	const struct rrm_measurement_report_element *report;
5874 
5875 #ifdef CONFIG_DPP
5876 	if (len >= IEEE80211_HDRLEN + 6 &&
5877 	    mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
5878 	    mgmt->u.action.u.vs_public_action.action ==
5879 	    WLAN_PA_VENDOR_SPECIFIC &&
5880 	    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
5881 	    OUI_WFA &&
5882 	    mgmt->u.action.u.vs_public_action.variable[0] ==
5883 	    DPP_OUI_TYPE) {
5884 		const u8 *pos, *end;
5885 
5886 		pos = &mgmt->u.action.u.vs_public_action.variable[1];
5887 		end = ((const u8 *) mgmt) + len;
5888 		hostapd_dpp_tx_status(hapd, mgmt->da, pos, end - pos, ok);
5889 		return;
5890 	}
5891 	if (len >= IEEE80211_HDRLEN + 2 &&
5892 	    mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
5893 	    (mgmt->u.action.u.public_action.action ==
5894 	     WLAN_PA_GAS_INITIAL_REQ ||
5895 	     mgmt->u.action.u.public_action.action ==
5896 	     WLAN_PA_GAS_COMEBACK_REQ)) {
5897 		const u8 *pos, *end;
5898 
5899 		pos = mgmt->u.action.u.public_action.variable;
5900 		end = ((const u8 *) mgmt) + len;
5901 		gas_query_ap_tx_status(hapd->gas, mgmt->da, pos, end - pos, ok);
5902 		return;
5903 	}
5904 #endif /* CONFIG_DPP */
5905 	if (is_multicast_ether_addr(mgmt->da))
5906 		return;
5907 	sta = ap_get_sta(hapd, mgmt->da);
5908 	if (!sta) {
5909 		wpa_printf(MSG_DEBUG, "handle_action_cb: STA " MACSTR
5910 			   " not found", MAC2STR(mgmt->da));
5911 		return;
5912 	}
5913 
5914 #ifdef CONFIG_HS20
5915 	if (ok && len >= IEEE80211_HDRLEN + 2 &&
5916 	    mgmt->u.action.category == WLAN_ACTION_WNM &&
5917 	    mgmt->u.action.u.vs_public_action.action == WNM_NOTIFICATION_REQ &&
5918 	    sta->hs20_deauth_on_ack) {
5919 		wpa_printf(MSG_DEBUG, "HS 2.0: Deauthenticate STA " MACSTR
5920 			   " on acknowledging the WNM-Notification",
5921 			   MAC2STR(sta->addr));
5922 		ap_sta_session_timeout(hapd, sta, 0);
5923 		return;
5924 	}
5925 #endif /* CONFIG_HS20 */
5926 
5927 	if (len < 24 + 5 + sizeof(*report))
5928 		return;
5929 	report = (const struct rrm_measurement_report_element *)
5930 		&mgmt->u.action.u.rrm.variable[2];
5931 	if (mgmt->u.action.category == WLAN_ACTION_RADIO_MEASUREMENT &&
5932 	    mgmt->u.action.u.rrm.action == WLAN_RRM_RADIO_MEASUREMENT_REQUEST &&
5933 	    report->eid == WLAN_EID_MEASURE_REQUEST &&
5934 	    report->len >= 3 &&
5935 	    report->type == MEASURE_TYPE_BEACON)
5936 		hostapd_rrm_beacon_req_tx_status(hapd, mgmt, len, ok);
5937 }
5938 
5939 
5940 /**
5941  * ieee802_11_mgmt_cb - Process management frame TX status callback
5942  * @hapd: hostapd BSS data structure (the BSS from which the management frame
5943  * was sent from)
5944  * @buf: management frame data (starting from IEEE 802.11 header)
5945  * @len: length of frame data in octets
5946  * @stype: management frame subtype from frame control field
5947  * @ok: Whether the frame was ACK'ed
5948  */
ieee802_11_mgmt_cb(struct hostapd_data * hapd,const u8 * buf,size_t len,u16 stype,int ok)5949 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
5950 			u16 stype, int ok)
5951 {
5952 	const struct ieee80211_mgmt *mgmt;
5953 	mgmt = (const struct ieee80211_mgmt *) buf;
5954 
5955 #ifdef CONFIG_TESTING_OPTIONS
5956 	if (hapd->ext_mgmt_frame_handling) {
5957 		size_t hex_len = 2 * len + 1;
5958 		char *hex = os_malloc(hex_len);
5959 
5960 		if (hex) {
5961 			wpa_snprintf_hex(hex, hex_len, buf, len);
5962 			wpa_msg(hapd->msg_ctx, MSG_INFO,
5963 				"MGMT-TX-STATUS stype=%u ok=%d buf=%s",
5964 				stype, ok, hex);
5965 			os_free(hex);
5966 		}
5967 		return;
5968 	}
5969 #endif /* CONFIG_TESTING_OPTIONS */
5970 
5971 	switch (stype) {
5972 	case WLAN_FC_STYPE_AUTH:
5973 		wpa_printf(MSG_DEBUG, "mgmt::auth cb");
5974 		handle_auth_cb(hapd, mgmt, len, ok);
5975 		break;
5976 	case WLAN_FC_STYPE_ASSOC_RESP:
5977 		wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
5978 		handle_assoc_cb(hapd, mgmt, len, 0, ok);
5979 		break;
5980 	case WLAN_FC_STYPE_REASSOC_RESP:
5981 		wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
5982 		handle_assoc_cb(hapd, mgmt, len, 1, ok);
5983 		break;
5984 	case WLAN_FC_STYPE_PROBE_RESP:
5985 		wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
5986 		break;
5987 	case WLAN_FC_STYPE_DEAUTH:
5988 		wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
5989 		handle_deauth_cb(hapd, mgmt, len, ok);
5990 		break;
5991 	case WLAN_FC_STYPE_DISASSOC:
5992 		wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
5993 		handle_disassoc_cb(hapd, mgmt, len, ok);
5994 		break;
5995 	case WLAN_FC_STYPE_ACTION:
5996 		wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
5997 		handle_action_cb(hapd, mgmt, len, ok);
5998 		break;
5999 	default:
6000 		wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
6001 		break;
6002 	}
6003 }
6004 
6005 
ieee802_11_get_mib(struct hostapd_data * hapd,char * buf,size_t buflen)6006 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
6007 {
6008 	/* TODO */
6009 	return 0;
6010 }
6011 
6012 
ieee802_11_get_mib_sta(struct hostapd_data * hapd,struct sta_info * sta,char * buf,size_t buflen)6013 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
6014 			   char *buf, size_t buflen)
6015 {
6016 	/* TODO */
6017 	return 0;
6018 }
6019 
6020 
hostapd_tx_status(struct hostapd_data * hapd,const u8 * addr,const u8 * buf,size_t len,int ack)6021 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
6022 		       const u8 *buf, size_t len, int ack)
6023 {
6024 	struct sta_info *sta;
6025 	struct hostapd_iface *iface = hapd->iface;
6026 
6027 	sta = ap_get_sta(hapd, addr);
6028 	if (sta == NULL && iface->num_bss > 1) {
6029 		size_t j;
6030 		for (j = 0; j < iface->num_bss; j++) {
6031 			hapd = iface->bss[j];
6032 			sta = ap_get_sta(hapd, addr);
6033 			if (sta)
6034 				break;
6035 		}
6036 	}
6037 	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
6038 		return;
6039 	if (sta->flags & WLAN_STA_PENDING_POLL) {
6040 		wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
6041 			   "activity poll", MAC2STR(sta->addr),
6042 			   ack ? "ACKed" : "did not ACK");
6043 		if (ack)
6044 			sta->flags &= ~WLAN_STA_PENDING_POLL;
6045 	}
6046 
6047 	ieee802_1x_tx_status(hapd, sta, buf, len, ack);
6048 }
6049 
6050 
hostapd_eapol_tx_status(struct hostapd_data * hapd,const u8 * dst,const u8 * data,size_t len,int ack)6051 void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
6052 			     const u8 *data, size_t len, int ack)
6053 {
6054 	struct sta_info *sta;
6055 	struct hostapd_iface *iface = hapd->iface;
6056 
6057 	sta = ap_get_sta(hapd, dst);
6058 	if (sta == NULL && iface->num_bss > 1) {
6059 		size_t j;
6060 		for (j = 0; j < iface->num_bss; j++) {
6061 			hapd = iface->bss[j];
6062 			sta = ap_get_sta(hapd, dst);
6063 			if (sta)
6064 				break;
6065 		}
6066 	}
6067 	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
6068 		wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
6069 			   MACSTR " that is not currently associated",
6070 			   MAC2STR(dst));
6071 		return;
6072 	}
6073 
6074 	ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
6075 }
6076 
6077 
hostapd_client_poll_ok(struct hostapd_data * hapd,const u8 * addr)6078 void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
6079 {
6080 	struct sta_info *sta;
6081 	struct hostapd_iface *iface = hapd->iface;
6082 
6083 	sta = ap_get_sta(hapd, addr);
6084 	if (sta == NULL && iface->num_bss > 1) {
6085 		size_t j;
6086 		for (j = 0; j < iface->num_bss; j++) {
6087 			hapd = iface->bss[j];
6088 			sta = ap_get_sta(hapd, addr);
6089 			if (sta)
6090 				break;
6091 		}
6092 	}
6093 	if (sta == NULL)
6094 		return;
6095 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
6096 		MAC2STR(sta->addr));
6097 	if (!(sta->flags & WLAN_STA_PENDING_POLL))
6098 		return;
6099 
6100 	wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
6101 		   "activity poll", MAC2STR(sta->addr));
6102 	sta->flags &= ~WLAN_STA_PENDING_POLL;
6103 }
6104 
6105 
ieee802_11_rx_from_unknown(struct hostapd_data * hapd,const u8 * src,int wds)6106 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
6107 				int wds)
6108 {
6109 	struct sta_info *sta;
6110 
6111 	sta = ap_get_sta(hapd, src);
6112 	if (sta &&
6113 	    ((sta->flags & WLAN_STA_ASSOC) ||
6114 	     ((sta->flags & WLAN_STA_ASSOC_REQ_OK) && wds))) {
6115 		if (!hapd->conf->wds_sta)
6116 			return;
6117 
6118 		if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK)) ==
6119 		    WLAN_STA_ASSOC_REQ_OK) {
6120 			wpa_printf(MSG_DEBUG,
6121 				   "Postpone 4-address WDS mode enabling for STA "
6122 				   MACSTR " since TX status for AssocResp is not yet known",
6123 				   MAC2STR(sta->addr));
6124 			sta->pending_wds_enable = 1;
6125 			return;
6126 		}
6127 
6128 		if (wds && !(sta->flags & WLAN_STA_WDS)) {
6129 			int ret;
6130 			char ifname_wds[IFNAMSIZ + 1];
6131 
6132 			wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
6133 				   "STA " MACSTR " (aid %u)",
6134 				   MAC2STR(sta->addr), sta->aid);
6135 			sta->flags |= WLAN_STA_WDS;
6136 			ret = hostapd_set_wds_sta(hapd, ifname_wds,
6137 						  sta->addr, sta->aid, 1);
6138 			if (!ret)
6139 				hostapd_set_wds_encryption(hapd, sta,
6140 							   ifname_wds);
6141 		}
6142 		return;
6143 	}
6144 
6145 	wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
6146 		   MACSTR, MAC2STR(src));
6147 	if (is_multicast_ether_addr(src) || is_zero_ether_addr(src) ||
6148 	    os_memcmp(src, hapd->own_addr, ETH_ALEN) == 0) {
6149 		/* Broadcast bit set in SA or unexpected SA?! Ignore the frame
6150 		 * silently. */
6151 		return;
6152 	}
6153 
6154 	if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
6155 		wpa_printf(MSG_DEBUG, "Association Response to the STA has "
6156 			   "already been sent, but no TX status yet known - "
6157 			   "ignore Class 3 frame issue with " MACSTR,
6158 			   MAC2STR(src));
6159 		return;
6160 	}
6161 
6162 	if (sta && (sta->flags & WLAN_STA_AUTH))
6163 		hostapd_drv_sta_disassoc(
6164 			hapd, src,
6165 			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
6166 	else
6167 		hostapd_drv_sta_deauth(
6168 			hapd, src,
6169 			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
6170 }
6171 
6172 
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)6173 static u8 * hostapd_add_tpe_info(u8 *eid, u8 tx_pwr_count,
6174 				 enum max_tx_pwr_interpretation tx_pwr_intrpn,
6175 				 u8 tx_pwr_cat, u8 tx_pwr)
6176 {
6177 	int i;
6178 
6179 	*eid++ = WLAN_EID_TRANSMIT_POWER_ENVELOPE; /* Element ID */
6180 	*eid++ = 2 + tx_pwr_count; /* Length */
6181 
6182 	/*
6183 	 * Transmit Power Information field
6184 	 *	bits 0-2 : Maximum Transmit Power Count
6185 	 *	bits 3-5 : Maximum Transmit Power Interpretation
6186 	 *	bits 6-7 : Maximum Transmit Power Category
6187 	 */
6188 	*eid++ = tx_pwr_count | (tx_pwr_intrpn << 3) | (tx_pwr_cat << 6);
6189 
6190 	/* Maximum Transmit Power field */
6191 	for (i = 0; i <= tx_pwr_count; i++)
6192 		*eid++ = tx_pwr;
6193 
6194 	return eid;
6195 }
6196 
6197 
6198 /*
6199  * TODO: Extract power limits from channel data after 6G regulatory
6200  *	support.
6201  */
6202 #define REG_PSD_MAX_TXPOWER_FOR_DEFAULT_CLIENT      (-1) /* dBm/MHz */
6203 #define REG_PSD_MAX_TXPOWER_FOR_SUBORDINATE_CLIENT  5    /* dBm/MHz */
6204 
hostapd_eid_txpower_envelope(struct hostapd_data * hapd,u8 * eid)6205 u8 * hostapd_eid_txpower_envelope(struct hostapd_data *hapd, u8 *eid)
6206 {
6207 	struct hostapd_iface *iface = hapd->iface;
6208 	struct hostapd_config *iconf = iface->conf;
6209 	struct hostapd_hw_modes *mode = iface->current_mode;
6210 	struct hostapd_channel_data *chan;
6211 	int dfs, i;
6212 	u8 channel, tx_pwr_count, local_pwr_constraint;
6213 	int max_tx_power;
6214 	u8 tx_pwr;
6215 
6216 	if (!mode)
6217 		return eid;
6218 
6219 	if (ieee80211_freq_to_chan(iface->freq, &channel) == NUM_HOSTAPD_MODES)
6220 		return eid;
6221 
6222 	for (i = 0; i < mode->num_channels; i++) {
6223 		if (mode->channels[i].freq == iface->freq)
6224 			break;
6225 	}
6226 	if (i == mode->num_channels)
6227 		return eid;
6228 
6229 #ifdef CONFIG_IEEE80211AX
6230 	/* IEEE Std 802.11ax-2021, Annex E.2.7 (6 GHz band in the United
6231 	 * States): An AP that is an Indoor Access Point per regulatory rules
6232 	 * shall send at least two Transmit Power Envelope elements in Beacon
6233 	 * and Probe Response frames as follows:
6234 	 *  - Maximum Transmit Power Category subfield = Default;
6235 	 *	Unit interpretation = Regulatory client EIRP PSD
6236 	 *  - Maximum Transmit Power Category subfield = Subordinate Device;
6237 	 *	Unit interpretation = Regulatory client EIRP PSD
6238 	 */
6239 	if (is_6ghz_op_class(iconf->op_class)) {
6240 		enum max_tx_pwr_interpretation tx_pwr_intrpn;
6241 
6242 		/* Same Maximum Transmit Power for all 20 MHz bands */
6243 		tx_pwr_count = 0;
6244 		tx_pwr_intrpn = REGULATORY_CLIENT_EIRP_PSD;
6245 
6246 		/* Default Transmit Power Envelope for Global Operating Class */
6247 		tx_pwr = REG_PSD_MAX_TXPOWER_FOR_DEFAULT_CLIENT * 2;
6248 		eid = hostapd_add_tpe_info(eid, tx_pwr_count, tx_pwr_intrpn,
6249 					   REG_DEFAULT_CLIENT, tx_pwr);
6250 
6251 		/* Indoor Access Point must include an additional TPE for
6252 		 * subordinate devices */
6253 		if (iconf->he_6ghz_reg_pwr_type == HE_6GHZ_INDOOR_AP) {
6254 			/* TODO: Extract PSD limits from channel data */
6255 			tx_pwr = REG_PSD_MAX_TXPOWER_FOR_SUBORDINATE_CLIENT * 2;
6256 			eid = hostapd_add_tpe_info(eid, tx_pwr_count,
6257 						   tx_pwr_intrpn,
6258 						   REG_SUBORDINATE_CLIENT,
6259 						   tx_pwr);
6260 		}
6261 
6262 		return eid;
6263 	}
6264 #endif /* CONFIG_IEEE80211AX */
6265 
6266 	switch (hostapd_get_oper_chwidth(iconf)) {
6267 	case CONF_OPER_CHWIDTH_USE_HT:
6268 		if (iconf->secondary_channel == 0) {
6269 			/* Max Transmit Power count = 0 (20 MHz) */
6270 			tx_pwr_count = 0;
6271 		} else {
6272 			/* Max Transmit Power count = 1 (20, 40 MHz) */
6273 			tx_pwr_count = 1;
6274 		}
6275 		break;
6276 	case CONF_OPER_CHWIDTH_80MHZ:
6277 		/* Max Transmit Power count = 2 (20, 40, and 80 MHz) */
6278 		tx_pwr_count = 2;
6279 		break;
6280 	case CONF_OPER_CHWIDTH_80P80MHZ:
6281 	case CONF_OPER_CHWIDTH_160MHZ:
6282 		/* Max Transmit Power count = 3 (20, 40, 80, 160/80+80 MHz) */
6283 		tx_pwr_count = 3;
6284 		break;
6285 	default:
6286 		return eid;
6287 	}
6288 
6289 	/*
6290 	 * Below local_pwr_constraint logic is referred from
6291 	 * hostapd_eid_pwr_constraint.
6292 	 *
6293 	 * Check if DFS is required by regulatory.
6294 	 */
6295 	dfs = hostapd_is_dfs_required(hapd->iface);
6296 	if (dfs < 0)
6297 		dfs = 0;
6298 
6299 	/*
6300 	 * In order to meet regulations when TPC is not implemented using
6301 	 * a transmit power that is below the legal maximum (including any
6302 	 * mitigation factor) should help. In this case, indicate 3 dB below
6303 	 * maximum allowed transmit power.
6304 	 */
6305 	if (hapd->iconf->local_pwr_constraint == -1)
6306 		local_pwr_constraint = (dfs == 0) ? 0 : 3;
6307 	else
6308 		local_pwr_constraint = hapd->iconf->local_pwr_constraint;
6309 
6310 	/*
6311 	 * A STA that is not an AP shall use a transmit power less than or
6312 	 * equal to the local maximum transmit power level for the channel.
6313 	 * The local maximum transmit power can be calculated from the formula:
6314 	 * local max TX pwr = max TX pwr - local pwr constraint
6315 	 * Where max TX pwr is maximum transmit power level specified for
6316 	 * channel in Country element and local pwr constraint is specified
6317 	 * for channel in this Power Constraint element.
6318 	 */
6319 	chan = &mode->channels[i];
6320 	max_tx_power = chan->max_tx_power - local_pwr_constraint;
6321 
6322 	/*
6323 	 * Local Maximum Transmit power is encoded as two's complement
6324 	 * with a 0.5 dB step.
6325 	 */
6326 	max_tx_power *= 2; /* in 0.5 dB steps */
6327 	if (max_tx_power > 127) {
6328 		/* 63.5 has special meaning of 63.5 dBm or higher */
6329 		max_tx_power = 127;
6330 	}
6331 	if (max_tx_power < -128)
6332 		max_tx_power = -128;
6333 	if (max_tx_power < 0)
6334 		tx_pwr = 0x80 + max_tx_power + 128;
6335 	else
6336 		tx_pwr = max_tx_power;
6337 
6338 	return hostapd_add_tpe_info(eid, tx_pwr_count, LOCAL_EIRP,
6339 				    0 /* Reserved for bands other than 6 GHz */,
6340 				    tx_pwr);
6341 }
6342 
6343 
hostapd_eid_wb_chsw_wrapper(struct hostapd_data * hapd,u8 * eid)6344 u8 * hostapd_eid_wb_chsw_wrapper(struct hostapd_data *hapd, u8 *eid)
6345 {
6346 	u8 bw, chan1, chan2 = 0;
6347 	int freq1;
6348 
6349 	if (!hapd->cs_freq_params.channel ||
6350 	    (!hapd->cs_freq_params.vht_enabled &&
6351 	     !hapd->cs_freq_params.he_enabled &&
6352 	     !hapd->cs_freq_params.eht_enabled))
6353 		return eid;
6354 
6355 	/* bandwidth: 0: 40, 1: 80, 2: 160, 3: 80+80, 4: 320 */
6356 	switch (hapd->cs_freq_params.bandwidth) {
6357 	case 40:
6358 		bw = 0;
6359 		break;
6360 	case 80:
6361 		/* check if it's 80+80 */
6362 		if (!hapd->cs_freq_params.center_freq2)
6363 			bw = 1;
6364 		else
6365 			bw = 3;
6366 		break;
6367 	case 160:
6368 		bw = 2;
6369 		break;
6370 	case 320:
6371 		bw = 4;
6372 		break;
6373 	default:
6374 		/* not valid VHT bandwidth or not in CSA */
6375 		return eid;
6376 	}
6377 
6378 	freq1 = hapd->cs_freq_params.center_freq1 ?
6379 		hapd->cs_freq_params.center_freq1 :
6380 		hapd->cs_freq_params.freq;
6381 	if (ieee80211_freq_to_chan(freq1, &chan1) !=
6382 	    HOSTAPD_MODE_IEEE80211A)
6383 		return eid;
6384 
6385 	if (hapd->cs_freq_params.center_freq2 &&
6386 	    ieee80211_freq_to_chan(hapd->cs_freq_params.center_freq2,
6387 				   &chan2) != HOSTAPD_MODE_IEEE80211A)
6388 		return eid;
6389 
6390 	*eid++ = WLAN_EID_CHANNEL_SWITCH_WRAPPER;
6391 	*eid++ = 5; /* Length of Channel Switch Wrapper */
6392 	*eid++ = WLAN_EID_WIDE_BW_CHSWITCH;
6393 	*eid++ = 3; /* Length of Wide Bandwidth Channel Switch element */
6394 	*eid++ = bw; /* New Channel Width */
6395 	*eid++ = chan1; /* New Channel Center Frequency Segment 0 */
6396 	*eid++ = chan2; /* New Channel Center Frequency Segment 1 */
6397 
6398 	return eid;
6399 }
6400 
6401 
hostapd_eid_nr_db_len(struct hostapd_data * hapd,size_t * current_len)6402 static size_t hostapd_eid_nr_db_len(struct hostapd_data *hapd,
6403 				    size_t *current_len)
6404 {
6405 	struct hostapd_neighbor_entry *nr;
6406 	size_t total_len = 0, len = *current_len;
6407 
6408 	dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
6409 			 list) {
6410 		if (!nr->nr || wpabuf_len(nr->nr) < 12)
6411 			continue;
6412 
6413 		if (nr->short_ssid == hapd->conf->ssid.short_ssid)
6414 			continue;
6415 
6416 		/* Start a new element */
6417 		if (!len ||
6418 		    len + RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN > 255) {
6419 			len = RNR_HEADER_LEN;
6420 			total_len += RNR_HEADER_LEN;
6421 		}
6422 
6423 		len += RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN;
6424 		total_len += RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN;
6425 	}
6426 
6427 	*current_len = len;
6428 	return total_len;
6429 }
6430 
6431 
6432 struct mbssid_ie_profiles {
6433 	u8 start;
6434 	u8 end;
6435 };
6436 
6437 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)6438 hostapd_eid_rnr_iface_len(struct hostapd_data *hapd,
6439 			  struct hostapd_data *reporting_hapd,
6440 			  size_t *current_len,
6441 			  struct mbssid_ie_profiles *skip_profiles)
6442 {
6443 	size_t total_len = 0, len = *current_len;
6444 	int tbtt_count = 0;
6445 	size_t i, start = 0;
6446 
6447 	while (start < hapd->iface->num_bss) {
6448 		if (!len ||
6449 		    len + RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN > 255) {
6450 			len = RNR_HEADER_LEN;
6451 			total_len += RNR_HEADER_LEN;
6452 		}
6453 
6454 		len += RNR_TBTT_HEADER_LEN;
6455 		total_len += RNR_TBTT_HEADER_LEN;
6456 
6457 		for (i = start; i < hapd->iface->num_bss; i++) {
6458 			struct hostapd_data *bss = hapd->iface->bss[i];
6459 
6460 			if (!bss || !bss->conf || !bss->started)
6461 				continue;
6462 
6463 			if (bss == reporting_hapd ||
6464 			    bss->conf->ignore_broadcast_ssid)
6465 				continue;
6466 
6467 			if (skip_profiles &&
6468 			    i >= skip_profiles->start && i < skip_profiles->end)
6469 				continue;
6470 
6471 			if (len + RNR_TBTT_INFO_LEN > 255 ||
6472 			    tbtt_count >= RNR_TBTT_INFO_COUNT_MAX)
6473 				break;
6474 
6475 			len += RNR_TBTT_INFO_LEN;
6476 			total_len += RNR_TBTT_INFO_LEN;
6477 			tbtt_count++;
6478 		}
6479 		start = i;
6480 	}
6481 
6482 	if (!tbtt_count)
6483 		total_len = 0;
6484 	else
6485 		*current_len = len;
6486 
6487 	return total_len;
6488 }
6489 
6490 
6491 enum colocation_mode {
6492 	NO_COLOCATED_6GHZ,
6493 	STANDALONE_6GHZ,
6494 	COLOCATED_6GHZ,
6495 	COLOCATED_LOWER_BAND,
6496 };
6497 
get_colocation_mode(struct hostapd_data * hapd)6498 static enum colocation_mode get_colocation_mode(struct hostapd_data *hapd)
6499 {
6500 	u8 i;
6501 	bool is_6ghz = is_6ghz_op_class(hapd->iconf->op_class);
6502 
6503 	if (!hapd->iface || !hapd->iface->interfaces)
6504 		return NO_COLOCATED_6GHZ;
6505 
6506 	if (is_6ghz && hapd->iface->interfaces->count == 1)
6507 		return STANDALONE_6GHZ;
6508 
6509 	for (i = 0; i < hapd->iface->interfaces->count; i++) {
6510 		struct hostapd_iface *iface;
6511 		bool is_colocated_6ghz;
6512 
6513 		iface = hapd->iface->interfaces->iface[i];
6514 		if (iface == hapd->iface || !iface || !iface->conf)
6515 			continue;
6516 
6517 		is_colocated_6ghz = is_6ghz_op_class(iface->conf->op_class);
6518 		if (!is_6ghz && is_colocated_6ghz)
6519 			return COLOCATED_LOWER_BAND;
6520 		if (is_6ghz && !is_colocated_6ghz)
6521 			return COLOCATED_6GHZ;
6522 	}
6523 
6524 	if (is_6ghz)
6525 		return STANDALONE_6GHZ;
6526 
6527 	return NO_COLOCATED_6GHZ;
6528 }
6529 
6530 
hostapd_eid_rnr_colocation_len(struct hostapd_data * hapd,size_t * current_len)6531 static size_t hostapd_eid_rnr_colocation_len(struct hostapd_data *hapd,
6532 					     size_t *current_len)
6533 {
6534 	struct hostapd_iface *iface;
6535 	size_t len = 0;
6536 	size_t i;
6537 
6538 	if (!hapd->iface || !hapd->iface->interfaces)
6539 		return 0;
6540 
6541 	for (i = 0; i < hapd->iface->interfaces->count; i++) {
6542 		iface = hapd->iface->interfaces->iface[i];
6543 
6544 		if (iface == hapd->iface ||
6545 		    !is_6ghz_op_class(iface->conf->op_class))
6546 			continue;
6547 
6548 		len += hostapd_eid_rnr_iface_len(iface->bss[0], hapd,
6549 						 current_len, NULL);
6550 	}
6551 
6552 	return len;
6553 }
6554 
6555 
hostapd_eid_rnr_len(struct hostapd_data * hapd,u32 type)6556 size_t hostapd_eid_rnr_len(struct hostapd_data *hapd, u32 type)
6557 {
6558 	size_t total_len = 0, current_len = 0;
6559 	enum colocation_mode mode = get_colocation_mode(hapd);
6560 
6561 	switch (type) {
6562 	case WLAN_FC_STYPE_BEACON:
6563 		if (hapd->conf->rnr)
6564 			total_len += hostapd_eid_nr_db_len(hapd, &current_len);
6565 		/* fallthrough */
6566 
6567 	case WLAN_FC_STYPE_PROBE_RESP:
6568 		if (mode == COLOCATED_LOWER_BAND)
6569 			total_len += hostapd_eid_rnr_colocation_len(
6570 				hapd, &current_len);
6571 
6572 		if (hapd->conf->rnr && hapd->iface->num_bss > 1 &&
6573 		    !hapd->iconf->mbssid)
6574 			total_len += hostapd_eid_rnr_iface_len(hapd, hapd,
6575 							       &current_len,
6576 							       NULL);
6577 		break;
6578 
6579 	case WLAN_FC_STYPE_ACTION:
6580 		if (hapd->iface->num_bss > 1 && mode == STANDALONE_6GHZ)
6581 			total_len += hostapd_eid_rnr_iface_len(hapd, hapd,
6582 							       &current_len,
6583 							       NULL);
6584 		break;
6585 
6586 	default:
6587 		break;
6588 	}
6589 
6590 	return total_len;
6591 }
6592 
6593 
hostapd_eid_nr_db(struct hostapd_data * hapd,u8 * eid,size_t * current_len)6594 static u8 * hostapd_eid_nr_db(struct hostapd_data *hapd, u8 *eid,
6595 			      size_t *current_len)
6596 {
6597 	struct hostapd_neighbor_entry *nr;
6598 	size_t len = *current_len;
6599 	u8 *size_offset = (eid - len) + 1;
6600 
6601 	dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
6602 			 list) {
6603 		if (!nr->nr || wpabuf_len(nr->nr) < 12)
6604 			continue;
6605 
6606 		if (nr->short_ssid == hapd->conf->ssid.short_ssid)
6607 			continue;
6608 
6609 		/* Start a new element */
6610 		if (!len ||
6611 		    len + RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN > 255) {
6612 			*eid++ = WLAN_EID_REDUCED_NEIGHBOR_REPORT;
6613 			size_offset = eid++;
6614 			len = RNR_HEADER_LEN;
6615 		}
6616 
6617 		/* TBTT Information Header subfield (2 octets) */
6618 		*eid++ = 0;
6619 		/* TBTT Information Length */
6620 		*eid++ = RNR_TBTT_INFO_LEN;
6621 		/* Operating Class */
6622 		*eid++ = wpabuf_head_u8(nr->nr)[10];
6623 		/* Channel Number */
6624 		*eid++ = wpabuf_head_u8(nr->nr)[11];
6625 		len += RNR_TBTT_HEADER_LEN;
6626 		/* TBTT Information Set */
6627 		/* TBTT Information field */
6628 		/* Neighbor AP TBTT Offset */
6629 		*eid++ = RNR_NEIGHBOR_AP_OFFSET_UNKNOWN;
6630 		/* BSSID */
6631 		os_memcpy(eid, nr->bssid, ETH_ALEN);
6632 		eid += ETH_ALEN;
6633 		/* Short SSID */
6634 		os_memcpy(eid, &nr->short_ssid, 4);
6635 		eid += 4;
6636 		/* BSS parameters */
6637 		*eid++ = nr->bss_parameters;
6638 		/* 20 MHz PSD */
6639 		*eid++ = RNR_20_MHZ_PSD_MAX_TXPOWER - 1;
6640 		len += RNR_TBTT_INFO_LEN;
6641 		*size_offset = (eid - size_offset) - 1;
6642 	}
6643 
6644 	*current_len = len;
6645 	return eid;
6646 }
6647 
6648 
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)6649 static u8 * hostapd_eid_rnr_iface(struct hostapd_data *hapd,
6650 				  struct hostapd_data *reporting_hapd,
6651 				  u8 *eid, size_t *current_len,
6652 				  struct mbssid_ie_profiles *skip_profiles)
6653 {
6654 	struct hostapd_data *bss;
6655 	struct hostapd_iface *iface = hapd->iface;
6656 	size_t i, start = 0;
6657 	size_t len = *current_len;
6658 	u8 *tbtt_count_pos, *eid_start = eid, *size_offset = (eid - len) + 1;
6659 	u8 tbtt_count = 0, op_class, channel, bss_param;
6660 
6661 	if (!(iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA) || !iface->freq)
6662 		return eid;
6663 
6664 	if (ieee80211_freq_to_channel_ext(iface->freq,
6665 					  hapd->iconf->secondary_channel,
6666 					  hostapd_get_oper_chwidth(hapd->iconf),
6667 					  &op_class, &channel) ==
6668 	    NUM_HOSTAPD_MODES)
6669 		return eid;
6670 
6671 	while (start < iface->num_bss) {
6672 		if (!len ||
6673 		    len + RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN > 255) {
6674 			eid_start = eid;
6675 			*eid++ = WLAN_EID_REDUCED_NEIGHBOR_REPORT;
6676 			size_offset = eid++;
6677 			len = RNR_HEADER_LEN;
6678 			tbtt_count = 0;
6679 		}
6680 
6681 		tbtt_count_pos = eid++;
6682 		*eid++ = RNR_TBTT_INFO_LEN;
6683 		*eid++ = op_class;
6684 		*eid++ = hapd->iconf->channel;
6685 		len += RNR_TBTT_HEADER_LEN;
6686 
6687 		for (i = start; i < iface->num_bss; i++) {
6688 			bss_param = 0;
6689 			bss = iface->bss[i];
6690 			if (!bss || !bss->conf || !bss->started)
6691 				continue;
6692 
6693 			if (bss == reporting_hapd ||
6694 			    bss->conf->ignore_broadcast_ssid)
6695 				continue;
6696 
6697 			if (skip_profiles &&
6698 			    i >= skip_profiles->start && i < skip_profiles->end)
6699 				continue;
6700 
6701 			if (len + RNR_TBTT_INFO_LEN > 255 ||
6702 			    tbtt_count >= RNR_TBTT_INFO_COUNT_MAX)
6703 				break;
6704 
6705 			*eid++ = RNR_NEIGHBOR_AP_OFFSET_UNKNOWN;
6706 			os_memcpy(eid, bss->own_addr, ETH_ALEN);
6707 			eid += ETH_ALEN;
6708 			os_memcpy(eid, &bss->conf->ssid.short_ssid, 4);
6709 			eid += 4;
6710 			if (bss->conf->ssid.short_ssid ==
6711 			    reporting_hapd->conf->ssid.short_ssid)
6712 				bss_param |= RNR_BSS_PARAM_SAME_SSID;
6713 
6714 			if (iface->conf->mbssid != MBSSID_DISABLED &&
6715 			    iface->num_bss > 1) {
6716 				bss_param |= RNR_BSS_PARAM_MULTIPLE_BSSID;
6717 				if (bss == hostapd_mbssid_get_tx_bss(hapd))
6718 					bss_param |=
6719 						RNR_BSS_PARAM_TRANSMITTED_BSSID;
6720 			}
6721 
6722 			if (is_6ghz_op_class(hapd->iconf->op_class) &&
6723 			    bss->conf->unsol_bcast_probe_resp_interval)
6724 				bss_param |=
6725 					RNR_BSS_PARAM_UNSOLIC_PROBE_RESP_ACTIVE;
6726 
6727 			bss_param |= RNR_BSS_PARAM_CO_LOCATED;
6728 
6729 			*eid++ = bss_param;
6730 			*eid++ = RNR_20_MHZ_PSD_MAX_TXPOWER - 1;
6731 			len += RNR_TBTT_INFO_LEN;
6732 			tbtt_count += 1;
6733 		}
6734 
6735 		start = i;
6736 		*tbtt_count_pos = RNR_TBTT_INFO_COUNT(tbtt_count - 1);
6737 		*size_offset = (eid - size_offset) - 1;
6738 	}
6739 
6740 	if (tbtt_count == 0)
6741 		return eid_start;
6742 
6743 	*current_len = len;
6744 	return eid;
6745 }
6746 
6747 
hostapd_eid_rnr_colocation(struct hostapd_data * hapd,u8 * eid,size_t * current_len)6748 static u8 * hostapd_eid_rnr_colocation(struct hostapd_data *hapd, u8 *eid,
6749 				       size_t *current_len)
6750 {
6751 	struct hostapd_iface *iface;
6752 	size_t i;
6753 
6754 	if (!hapd->iface || !hapd->iface->interfaces)
6755 		return eid;
6756 
6757 	for (i = 0; i < hapd->iface->interfaces->count; i++) {
6758 		iface = hapd->iface->interfaces->iface[i];
6759 
6760 		if (iface == hapd->iface ||
6761 		    !is_6ghz_op_class(iface->conf->op_class))
6762 			continue;
6763 
6764 		eid = hostapd_eid_rnr_iface(iface->bss[0], hapd, eid,
6765 					    current_len, NULL);
6766 	}
6767 
6768 	return eid;
6769 }
6770 
6771 
hostapd_eid_rnr(struct hostapd_data * hapd,u8 * eid,u32 type)6772 u8 * hostapd_eid_rnr(struct hostapd_data *hapd, u8 *eid, u32 type)
6773 {
6774 	u8 *eid_start = eid;
6775 	size_t current_len = 0;
6776 	enum colocation_mode mode = get_colocation_mode(hapd);
6777 
6778 	switch (type) {
6779 	case WLAN_FC_STYPE_BEACON:
6780 		if (hapd->conf->rnr)
6781 			eid = hostapd_eid_nr_db(hapd, eid, &current_len);
6782 		/* fallthrough */
6783 
6784 	case WLAN_FC_STYPE_PROBE_RESP:
6785 		if (mode == COLOCATED_LOWER_BAND)
6786 			eid = hostapd_eid_rnr_colocation(hapd, eid,
6787 							 &current_len);
6788 
6789 		if (hapd->conf->rnr && hapd->iface->num_bss > 1 &&
6790 		    !hapd->iconf->mbssid)
6791 			eid = hostapd_eid_rnr_iface(hapd, hapd, eid,
6792 						    &current_len, NULL);
6793 		break;
6794 
6795 	case WLAN_FC_STYPE_ACTION:
6796 		if (hapd->iface->num_bss > 1 && mode == STANDALONE_6GHZ)
6797 			eid = hostapd_eid_rnr_iface(hapd, hapd,	eid,
6798 						    &current_len, NULL);
6799 		break;
6800 
6801 	default:
6802 		return eid_start;
6803 	}
6804 
6805 	if (eid == eid_start + 2)
6806 		return eid_start;
6807 
6808 	return eid;
6809 }
6810 
6811 
mbssid_known_bss(unsigned int i,const u8 * known_bss,size_t known_bss_len)6812 static bool mbssid_known_bss(unsigned int i, const u8 *known_bss,
6813 			     size_t known_bss_len)
6814 {
6815 	if (!known_bss || known_bss_len <= i / 8)
6816 		return false;
6817 	known_bss = &known_bss[i / 8];
6818 	return *known_bss & (u8) (BIT(i % 8));
6819 }
6820 
6821 
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)6822 static size_t hostapd_eid_mbssid_elem_len(struct hostapd_data *hapd,
6823 					  u32 frame_type, size_t *bss_index,
6824 					  const u8 *known_bss,
6825 					  size_t known_bss_len)
6826 {
6827 	struct hostapd_data *tx_bss = hostapd_mbssid_get_tx_bss(hapd);
6828 	size_t len = 3, i;
6829 
6830 	for (i = *bss_index; i < hapd->iface->num_bss; i++) {
6831 		struct hostapd_data *bss = hapd->iface->bss[i];
6832 		const u8 *auth, *rsn = NULL, *rsnx = NULL;
6833 		size_t nontx_profile_len, auth_len;
6834 		u8 ie_count = 0;
6835 
6836 		if (!bss || !bss->conf || !bss->started ||
6837 		    mbssid_known_bss(i, known_bss, known_bss_len))
6838 			continue;
6839 
6840 		/*
6841 		 * Sublement ID: 1 octet
6842 		 * Length: 1 octet
6843 		 * Nontransmitted capabilities: 4 octets
6844 		 * SSID element: 2 + variable
6845 		 * Multiple BSSID Index Element: 3 octets (+2 octets in beacons)
6846 		 * Fixed length = 1 + 1 + 4 + 2 + 3 = 11
6847 		 */
6848 		nontx_profile_len = 11 + bss->conf->ssid.ssid_len;
6849 
6850 		if (frame_type == WLAN_FC_STYPE_BEACON)
6851 			nontx_profile_len += 2;
6852 
6853 		auth = wpa_auth_get_wpa_ie(bss->wpa_auth, &auth_len);
6854 		if (auth) {
6855 			rsn = get_ie(auth, auth_len, WLAN_EID_RSN);
6856 			if (rsn)
6857 				nontx_profile_len += 2 + rsn[1];
6858 
6859 			rsnx = get_ie(auth, auth_len, WLAN_EID_RSNX);
6860 			if (rsnx)
6861 				nontx_profile_len += 2 + rsnx[1];
6862 		}
6863 		if (!rsn && hostapd_wpa_ie(tx_bss, WLAN_EID_RSN))
6864 			ie_count++;
6865 		if (!rsnx && hostapd_wpa_ie(tx_bss, WLAN_EID_RSNX))
6866 			ie_count++;
6867 		if (bss->conf->xrates_supported)
6868 			nontx_profile_len += 8;
6869 		else if (hapd->conf->xrates_supported)
6870 			ie_count++;
6871 		if (ie_count)
6872 			nontx_profile_len += 4 + ie_count;
6873 
6874 		if (len + nontx_profile_len > 255)
6875 			break;
6876 
6877 		len += nontx_profile_len;
6878 	}
6879 
6880 	*bss_index = i;
6881 	return len;
6882 }
6883 
6884 
hostapd_eid_mbssid_len(struct hostapd_data * hapd,u32 frame_type,u8 * elem_count,const u8 * known_bss,size_t known_bss_len,size_t * rnr_len)6885 size_t hostapd_eid_mbssid_len(struct hostapd_data *hapd, u32 frame_type,
6886 			      u8 *elem_count, const u8 *known_bss,
6887 			      size_t known_bss_len, size_t *rnr_len)
6888 {
6889 	size_t len = 0, bss_index = 1;
6890 
6891 	if (!hapd->iconf->mbssid || hapd->iface->num_bss <= 1 ||
6892 	    (frame_type != WLAN_FC_STYPE_BEACON &&
6893 	     frame_type != WLAN_FC_STYPE_PROBE_RESP))
6894 		return 0;
6895 
6896 	if (frame_type == WLAN_FC_STYPE_BEACON) {
6897 		if (!elem_count) {
6898 			wpa_printf(MSG_INFO,
6899 				   "MBSSID: Insufficient data for Beacon frames");
6900 			return 0;
6901 		}
6902 		*elem_count = 0;
6903 	}
6904 
6905 	while (bss_index < hapd->iface->num_bss) {
6906 		size_t rnr_count = bss_index;
6907 
6908 		len += hostapd_eid_mbssid_elem_len(hapd, frame_type,
6909 						   &bss_index, known_bss,
6910 						   known_bss_len);
6911 
6912 		if (frame_type == WLAN_FC_STYPE_BEACON)
6913 			*elem_count += 1;
6914 		if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED && rnr_len) {
6915 			size_t rnr_cur_len = 0;
6916 			struct mbssid_ie_profiles skip_profiles = {
6917 				rnr_count, bss_index
6918 			};
6919 
6920 			*rnr_len += hostapd_eid_rnr_iface_len(
6921 				hapd, hostapd_mbssid_get_tx_bss(hapd),
6922 				&rnr_cur_len, &skip_profiles);
6923 		}
6924 	}
6925 
6926 	if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED && rnr_len)
6927 		*rnr_len += hostapd_eid_rnr_len(hapd, frame_type);
6928 
6929 	return len;
6930 }
6931 
6932 
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)6933 static u8 * hostapd_eid_mbssid_elem(struct hostapd_data *hapd, u8 *eid, u8 *end,
6934 				    u32 frame_type, u8 max_bssid_indicator,
6935 				    size_t *bss_index, u8 elem_count,
6936 				    const u8 *known_bss, size_t known_bss_len)
6937 {
6938 	struct hostapd_data *tx_bss = hostapd_mbssid_get_tx_bss(hapd);
6939 	size_t i;
6940 	u8 *eid_len_offset, *max_bssid_indicator_offset;
6941 
6942 	*eid++ = WLAN_EID_MULTIPLE_BSSID;
6943 	eid_len_offset = eid++;
6944 	max_bssid_indicator_offset = eid++;
6945 
6946 	for (i = *bss_index; i < hapd->iface->num_bss; i++) {
6947 		struct hostapd_data *bss = hapd->iface->bss[i];
6948 		struct hostapd_bss_config *conf;
6949 		u8 *eid_len_pos, *nontx_bss_start = eid;
6950 		const u8 *auth, *rsn = NULL, *rsnx = NULL;
6951 		u8 ie_count = 0, non_inherit_ie[3];
6952 		size_t auth_len = 0;
6953 		u16 capab_info;
6954 
6955 		if (!bss || !bss->conf || !bss->started ||
6956 		    mbssid_known_bss(i, known_bss, known_bss_len))
6957 			continue;
6958 		conf = bss->conf;
6959 
6960 		*eid++ = WLAN_MBSSID_SUBELEMENT_NONTRANSMITTED_BSSID_PROFILE;
6961 		eid_len_pos = eid++;
6962 
6963 		capab_info = hostapd_own_capab_info(bss);
6964 		*eid++ = WLAN_EID_NONTRANSMITTED_BSSID_CAPA;
6965 		*eid++ = sizeof(capab_info);
6966 		WPA_PUT_LE16(eid, capab_info);
6967 		eid += sizeof(capab_info);
6968 
6969 		*eid++ = WLAN_EID_SSID;
6970 		*eid++ = conf->ssid.ssid_len;
6971 		os_memcpy(eid, conf->ssid.ssid, conf->ssid.ssid_len);
6972 		eid += conf->ssid.ssid_len;
6973 
6974 		*eid++ = WLAN_EID_MULTIPLE_BSSID_INDEX;
6975 		if (frame_type == WLAN_FC_STYPE_BEACON) {
6976 			*eid++ = 3;
6977 			*eid++ = i; /* BSSID Index */
6978 			if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED &&
6979 			    (conf->dtim_period % elem_count))
6980 				conf->dtim_period = elem_count;
6981 			*eid++ = conf->dtim_period;
6982 			*eid++ = 0xFF; /* DTIM Count */
6983 		} else {
6984 			/* Probe Request frame does not include DTIM Period and
6985 			 * DTIM Count fields. */
6986 			*eid++ = 1;
6987 			*eid++ = i; /* BSSID Index */
6988 		}
6989 
6990 		auth = wpa_auth_get_wpa_ie(bss->wpa_auth, &auth_len);
6991 		if (auth) {
6992 			rsn = get_ie(auth, auth_len, WLAN_EID_RSN);
6993 			if (rsn) {
6994 				os_memcpy(eid, rsn, 2 + rsn[1]);
6995 				eid += 2 + rsn[1];
6996 			}
6997 
6998 			rsnx = get_ie(auth, auth_len, WLAN_EID_RSNX);
6999 			if (rsnx) {
7000 				os_memcpy(eid, rsnx, 2 + rsnx[1]);
7001 				eid += 2 + rsnx[1];
7002 			}
7003 		}
7004 		if (!rsn && hostapd_wpa_ie(tx_bss, WLAN_EID_RSN))
7005 			non_inherit_ie[ie_count++] = WLAN_EID_RSN;
7006 		if (!rsnx && hostapd_wpa_ie(tx_bss, WLAN_EID_RSNX))
7007 			non_inherit_ie[ie_count++] = WLAN_EID_RSNX;
7008 		if (hapd->conf->xrates_supported &&
7009 		    !bss->conf->xrates_supported)
7010 			non_inherit_ie[ie_count++] = WLAN_EID_EXT_SUPP_RATES;
7011 		if (ie_count) {
7012 			*eid++ = WLAN_EID_EXTENSION;
7013 			*eid++ = 2 + ie_count;
7014 			*eid++ = WLAN_EID_EXT_NON_INHERITANCE;
7015 			*eid++ = ie_count;
7016 			os_memcpy(eid, non_inherit_ie, ie_count);
7017 			eid += ie_count;
7018 		}
7019 
7020 		*eid_len_pos = (eid - eid_len_pos) - 1;
7021 
7022 		if (((eid - eid_len_offset) - 1) > 255) {
7023 			eid = nontx_bss_start;
7024 			break;
7025 		}
7026 	}
7027 
7028 	*bss_index = i;
7029 	*max_bssid_indicator_offset = max_bssid_indicator;
7030 	if (*max_bssid_indicator_offset < 1)
7031 		*max_bssid_indicator_offset = 1;
7032 	*eid_len_offset = (eid - eid_len_offset) - 1;
7033 	return eid;
7034 }
7035 
7036 
hostapd_eid_mbssid(struct hostapd_data * hapd,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)7037 u8 * hostapd_eid_mbssid(struct hostapd_data *hapd, u8 *eid, u8 *end,
7038 			unsigned int frame_stype, u8 elem_count,
7039 			u8 **elem_offset,
7040 			const u8 *known_bss, size_t known_bss_len, u8 *rnr_eid,
7041 			u8 *rnr_count, u8 **rnr_offset, size_t rnr_len)
7042 {
7043 	size_t bss_index = 1, cur_len = 0;
7044 	u8 elem_index = 0, *rnr_start_eid = rnr_eid;
7045 	bool add_rnr;
7046 
7047 	if (!hapd->iconf->mbssid || hapd->iface->num_bss <= 1 ||
7048 	    (frame_stype != WLAN_FC_STYPE_BEACON &&
7049 	     frame_stype != WLAN_FC_STYPE_PROBE_RESP))
7050 		return eid;
7051 
7052 	if (frame_stype == WLAN_FC_STYPE_BEACON && !elem_offset) {
7053 		wpa_printf(MSG_INFO,
7054 			   "MBSSID: Insufficient data for Beacon frames");
7055 		return eid;
7056 	}
7057 
7058 	add_rnr = hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED &&
7059 		frame_stype == WLAN_FC_STYPE_BEACON &&
7060 		rnr_eid && rnr_count && rnr_offset && rnr_len;
7061 
7062 	while (bss_index < hapd->iface->num_bss) {
7063 		unsigned int rnr_start_count = bss_index;
7064 
7065 		if (frame_stype == WLAN_FC_STYPE_BEACON) {
7066 			if (elem_index == elem_count) {
7067 				wpa_printf(MSG_WARNING,
7068 					   "MBSSID: Larger number of elements than there is room in the provided array");
7069 				break;
7070 			}
7071 
7072 			elem_offset[elem_index] = eid;
7073 			elem_index = elem_index + 1;
7074 		}
7075 		eid = hostapd_eid_mbssid_elem(hapd, eid, end, frame_stype,
7076 					      hostapd_max_bssid_indicator(hapd),
7077 					      &bss_index, elem_count,
7078 					      known_bss, known_bss_len);
7079 
7080 		if (add_rnr) {
7081 			struct mbssid_ie_profiles skip_profiles = {
7082 				rnr_start_count, bss_index
7083 			};
7084 
7085 			rnr_offset[*rnr_count] = rnr_eid;
7086 			*rnr_count = *rnr_count + 1;
7087 			cur_len = 0;
7088 			rnr_eid = hostapd_eid_rnr_iface(
7089 				hapd, hostapd_mbssid_get_tx_bss(hapd),
7090 				rnr_eid, &cur_len, &skip_profiles);
7091 		}
7092 	}
7093 
7094 	if (add_rnr && (size_t) (rnr_eid - rnr_start_eid) < rnr_len) {
7095 		rnr_offset[*rnr_count] = rnr_eid;
7096 		*rnr_count = *rnr_count + 1;
7097 		cur_len = 0;
7098 
7099 		if (hapd->conf->rnr)
7100 			rnr_eid = hostapd_eid_nr_db(hapd, rnr_eid, &cur_len);
7101 		if (get_colocation_mode(hapd) == COLOCATED_LOWER_BAND)
7102 			rnr_eid = hostapd_eid_rnr_colocation(hapd, rnr_eid,
7103 							     &cur_len);
7104 	}
7105 
7106 	return eid;
7107 }
7108 
7109 
punct_update_legacy_bw_80(u8 bitmap,u8 pri_chan,u8 * seg0)7110 static void punct_update_legacy_bw_80(u8 bitmap, u8 pri_chan, u8 *seg0)
7111 {
7112 	u8 first_chan = *seg0 - 6, sec_chan;
7113 
7114 	switch (bitmap) {
7115 	case 0x6:
7116 		*seg0 = 0;
7117 		return;
7118 	case 0x8:
7119 	case 0x4:
7120 	case 0x2:
7121 	case 0x1:
7122 	case 0xC:
7123 	case 0x3:
7124 		if (pri_chan < *seg0)
7125 			*seg0 -= 4;
7126 		else
7127 			*seg0 += 4;
7128 		break;
7129 	}
7130 
7131 	if (pri_chan < *seg0)
7132 		sec_chan = pri_chan + 4;
7133 	else
7134 		sec_chan = pri_chan - 4;
7135 
7136 	if (bitmap & BIT((sec_chan - first_chan) / 4))
7137 		*seg0 = 0;
7138 }
7139 
7140 
punct_update_legacy_bw_160(u8 bitmap,u8 pri,enum oper_chan_width * width,u8 * seg0)7141 static void punct_update_legacy_bw_160(u8 bitmap, u8 pri,
7142 				       enum oper_chan_width *width, u8 *seg0)
7143 {
7144 	if (pri < *seg0) {
7145 		*seg0 -= 8;
7146 		if (bitmap & 0x0F) {
7147 			*width = 0;
7148 			punct_update_legacy_bw_80(bitmap & 0xF, pri, seg0);
7149 		}
7150 	} else {
7151 		*seg0 += 8;
7152 		if (bitmap & 0xF0) {
7153 			*width = 0;
7154 			punct_update_legacy_bw_80((bitmap & 0xF0) >> 4, pri,
7155 						  seg0);
7156 		}
7157 	}
7158 }
7159 
7160 
punct_update_legacy_bw(u16 bitmap,u8 pri,enum oper_chan_width * width,u8 * seg0,u8 * seg1)7161 void punct_update_legacy_bw(u16 bitmap, u8 pri, enum oper_chan_width *width,
7162 			    u8 *seg0, u8 *seg1)
7163 {
7164 	if (*width == CONF_OPER_CHWIDTH_80MHZ && (bitmap & 0xF)) {
7165 		*width = CONF_OPER_CHWIDTH_USE_HT;
7166 		punct_update_legacy_bw_80(bitmap & 0xF, pri, seg0);
7167 	}
7168 
7169 	if (*width == CONF_OPER_CHWIDTH_160MHZ && (bitmap & 0xFF)) {
7170 		*width = CONF_OPER_CHWIDTH_80MHZ;
7171 		*seg1 = 0;
7172 		punct_update_legacy_bw_160(bitmap & 0xFF, pri, width, seg0);
7173 	}
7174 
7175 	/* TODO: 320 MHz */
7176 }
7177 
7178 #endif /* CONFIG_NATIVE_WINDOWS */
7179