13ff40c12SJohn Marino /*
23ff40c12SJohn Marino  * hostapd / IEEE 802.11 Management
3*a1157835SDaniel Fojt  * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
43ff40c12SJohn Marino  *
53ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
63ff40c12SJohn Marino  * See README for more details.
73ff40c12SJohn Marino  */
83ff40c12SJohn Marino 
93ff40c12SJohn Marino #include "utils/includes.h"
103ff40c12SJohn Marino 
113ff40c12SJohn Marino #ifndef CONFIG_NATIVE_WINDOWS
123ff40c12SJohn Marino 
133ff40c12SJohn Marino #include "utils/common.h"
143ff40c12SJohn Marino #include "utils/eloop.h"
153ff40c12SJohn Marino #include "crypto/crypto.h"
163ff40c12SJohn Marino #include "crypto/sha256.h"
17*a1157835SDaniel Fojt #include "crypto/sha384.h"
18*a1157835SDaniel Fojt #include "crypto/sha512.h"
193ff40c12SJohn Marino #include "crypto/random.h"
203ff40c12SJohn Marino #include "common/ieee802_11_defs.h"
213ff40c12SJohn Marino #include "common/ieee802_11_common.h"
223ff40c12SJohn Marino #include "common/wpa_ctrl.h"
233ff40c12SJohn Marino #include "common/sae.h"
24*a1157835SDaniel Fojt #include "common/dpp.h"
25*a1157835SDaniel Fojt #include "common/ocv.h"
26*a1157835SDaniel Fojt #include "common/wpa_common.h"
273ff40c12SJohn Marino #include "radius/radius.h"
283ff40c12SJohn Marino #include "radius/radius_client.h"
293ff40c12SJohn Marino #include "p2p/p2p.h"
303ff40c12SJohn Marino #include "wps/wps.h"
31*a1157835SDaniel Fojt #include "fst/fst.h"
323ff40c12SJohn Marino #include "hostapd.h"
333ff40c12SJohn Marino #include "beacon.h"
343ff40c12SJohn Marino #include "ieee802_11_auth.h"
353ff40c12SJohn Marino #include "sta_info.h"
363ff40c12SJohn Marino #include "ieee802_1x.h"
373ff40c12SJohn Marino #include "wpa_auth.h"
38*a1157835SDaniel Fojt #include "pmksa_cache_auth.h"
393ff40c12SJohn Marino #include "wmm.h"
403ff40c12SJohn Marino #include "ap_list.h"
413ff40c12SJohn Marino #include "accounting.h"
423ff40c12SJohn Marino #include "ap_config.h"
433ff40c12SJohn Marino #include "ap_mlme.h"
443ff40c12SJohn Marino #include "p2p_hostapd.h"
453ff40c12SJohn Marino #include "ap_drv_ops.h"
463ff40c12SJohn Marino #include "wnm_ap.h"
47*a1157835SDaniel Fojt #include "hw_features.h"
483ff40c12SJohn Marino #include "ieee802_11.h"
49*a1157835SDaniel Fojt #include "dfs.h"
50*a1157835SDaniel Fojt #include "mbo_ap.h"
51*a1157835SDaniel Fojt #include "rrm.h"
52*a1157835SDaniel Fojt #include "taxonomy.h"
53*a1157835SDaniel Fojt #include "fils_hlp.h"
54*a1157835SDaniel Fojt #include "dpp_hostapd.h"
55*a1157835SDaniel Fojt #include "gas_query_ap.h"
56*a1157835SDaniel Fojt 
57*a1157835SDaniel Fojt 
58*a1157835SDaniel Fojt #ifdef CONFIG_FILS
59*a1157835SDaniel Fojt static struct wpabuf *
60*a1157835SDaniel Fojt prepare_auth_resp_fils(struct hostapd_data *hapd,
61*a1157835SDaniel Fojt 		       struct sta_info *sta, u16 *resp,
62*a1157835SDaniel Fojt 		       struct rsn_pmksa_cache_entry *pmksa,
63*a1157835SDaniel Fojt 		       struct wpabuf *erp_resp,
64*a1157835SDaniel Fojt 		       const u8 *msk, size_t msk_len,
65*a1157835SDaniel Fojt 		       int *is_pub);
66*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
67*a1157835SDaniel Fojt static void handle_auth(struct hostapd_data *hapd,
68*a1157835SDaniel Fojt 			const struct ieee80211_mgmt *mgmt, size_t len,
69*a1157835SDaniel Fojt 			int rssi, int from_queue);
70*a1157835SDaniel Fojt 
71*a1157835SDaniel Fojt 
hostapd_eid_multi_ap(struct hostapd_data * hapd,u8 * eid)72*a1157835SDaniel Fojt u8 * hostapd_eid_multi_ap(struct hostapd_data *hapd, u8 *eid)
73*a1157835SDaniel Fojt {
74*a1157835SDaniel Fojt 	u8 multi_ap_val = 0;
75*a1157835SDaniel Fojt 
76*a1157835SDaniel Fojt 	if (!hapd->conf->multi_ap)
77*a1157835SDaniel Fojt 		return eid;
78*a1157835SDaniel Fojt 	if (hapd->conf->multi_ap & BACKHAUL_BSS)
79*a1157835SDaniel Fojt 		multi_ap_val |= MULTI_AP_BACKHAUL_BSS;
80*a1157835SDaniel Fojt 	if (hapd->conf->multi_ap & FRONTHAUL_BSS)
81*a1157835SDaniel Fojt 		multi_ap_val |= MULTI_AP_FRONTHAUL_BSS;
82*a1157835SDaniel Fojt 
83*a1157835SDaniel Fojt 	return eid + add_multi_ap_ie(eid, 9, multi_ap_val);
84*a1157835SDaniel Fojt }
853ff40c12SJohn Marino 
863ff40c12SJohn Marino 
hostapd_eid_supp_rates(struct hostapd_data * hapd,u8 * eid)873ff40c12SJohn Marino u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
883ff40c12SJohn Marino {
893ff40c12SJohn Marino 	u8 *pos = eid;
903ff40c12SJohn Marino 	int i, num, count;
913ff40c12SJohn Marino 
923ff40c12SJohn Marino 	if (hapd->iface->current_rates == NULL)
933ff40c12SJohn Marino 		return eid;
943ff40c12SJohn Marino 
953ff40c12SJohn Marino 	*pos++ = WLAN_EID_SUPP_RATES;
963ff40c12SJohn Marino 	num = hapd->iface->num_rates;
973ff40c12SJohn Marino 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
983ff40c12SJohn Marino 		num++;
993ff40c12SJohn Marino 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
1003ff40c12SJohn Marino 		num++;
1013ff40c12SJohn Marino 	if (num > 8) {
1023ff40c12SJohn Marino 		/* rest of the rates are encoded in Extended supported
1033ff40c12SJohn Marino 		 * rates element */
1043ff40c12SJohn Marino 		num = 8;
1053ff40c12SJohn Marino 	}
1063ff40c12SJohn Marino 
1073ff40c12SJohn Marino 	*pos++ = num;
1083ff40c12SJohn Marino 	for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
1093ff40c12SJohn Marino 	     i++) {
1103ff40c12SJohn Marino 		count++;
1113ff40c12SJohn Marino 		*pos = hapd->iface->current_rates[i].rate / 5;
1123ff40c12SJohn Marino 		if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
1133ff40c12SJohn Marino 			*pos |= 0x80;
1143ff40c12SJohn Marino 		pos++;
1153ff40c12SJohn Marino 	}
1163ff40c12SJohn Marino 
1173ff40c12SJohn Marino 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
1183ff40c12SJohn Marino 		count++;
1193ff40c12SJohn Marino 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
1203ff40c12SJohn Marino 	}
1213ff40c12SJohn Marino 
1223ff40c12SJohn Marino 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
1233ff40c12SJohn Marino 		count++;
1243ff40c12SJohn Marino 		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
1253ff40c12SJohn Marino 	}
1263ff40c12SJohn Marino 
1273ff40c12SJohn Marino 	return pos;
1283ff40c12SJohn Marino }
1293ff40c12SJohn Marino 
1303ff40c12SJohn Marino 
hostapd_eid_ext_supp_rates(struct hostapd_data * hapd,u8 * eid)1313ff40c12SJohn Marino u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
1323ff40c12SJohn Marino {
1333ff40c12SJohn Marino 	u8 *pos = eid;
1343ff40c12SJohn Marino 	int i, num, count;
1353ff40c12SJohn Marino 
1363ff40c12SJohn Marino 	if (hapd->iface->current_rates == NULL)
1373ff40c12SJohn Marino 		return eid;
1383ff40c12SJohn Marino 
1393ff40c12SJohn Marino 	num = hapd->iface->num_rates;
1403ff40c12SJohn Marino 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
1413ff40c12SJohn Marino 		num++;
1423ff40c12SJohn Marino 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
1433ff40c12SJohn Marino 		num++;
1443ff40c12SJohn Marino 	if (num <= 8)
1453ff40c12SJohn Marino 		return eid;
1463ff40c12SJohn Marino 	num -= 8;
1473ff40c12SJohn Marino 
1483ff40c12SJohn Marino 	*pos++ = WLAN_EID_EXT_SUPP_RATES;
1493ff40c12SJohn Marino 	*pos++ = num;
1503ff40c12SJohn Marino 	for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
1513ff40c12SJohn Marino 	     i++) {
1523ff40c12SJohn Marino 		count++;
1533ff40c12SJohn Marino 		if (count <= 8)
1543ff40c12SJohn Marino 			continue; /* already in SuppRates IE */
1553ff40c12SJohn Marino 		*pos = hapd->iface->current_rates[i].rate / 5;
1563ff40c12SJohn Marino 		if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
1573ff40c12SJohn Marino 			*pos |= 0x80;
1583ff40c12SJohn Marino 		pos++;
1593ff40c12SJohn Marino 	}
1603ff40c12SJohn Marino 
1613ff40c12SJohn Marino 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
1623ff40c12SJohn Marino 		count++;
1633ff40c12SJohn Marino 		if (count > 8)
1643ff40c12SJohn Marino 			*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
1653ff40c12SJohn Marino 	}
1663ff40c12SJohn Marino 
1673ff40c12SJohn Marino 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
1683ff40c12SJohn Marino 		count++;
1693ff40c12SJohn Marino 		if (count > 8)
1703ff40c12SJohn Marino 			*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
1713ff40c12SJohn Marino 	}
1723ff40c12SJohn Marino 
1733ff40c12SJohn Marino 	return pos;
1743ff40c12SJohn Marino }
1753ff40c12SJohn Marino 
1763ff40c12SJohn Marino 
hostapd_own_capab_info(struct hostapd_data * hapd)177*a1157835SDaniel Fojt u16 hostapd_own_capab_info(struct hostapd_data *hapd)
1783ff40c12SJohn Marino {
1793ff40c12SJohn Marino 	int capab = WLAN_CAPABILITY_ESS;
1803ff40c12SJohn Marino 	int privacy;
181*a1157835SDaniel Fojt 	int dfs;
182*a1157835SDaniel Fojt 	int i;
183*a1157835SDaniel Fojt 
184*a1157835SDaniel Fojt 	/* Check if any of configured channels require DFS */
185*a1157835SDaniel Fojt 	dfs = hostapd_is_dfs_required(hapd->iface);
186*a1157835SDaniel Fojt 	if (dfs < 0) {
187*a1157835SDaniel Fojt 		wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
188*a1157835SDaniel Fojt 			   dfs);
189*a1157835SDaniel Fojt 		dfs = 0;
190*a1157835SDaniel Fojt 	}
1913ff40c12SJohn Marino 
1923ff40c12SJohn Marino 	if (hapd->iface->num_sta_no_short_preamble == 0 &&
1933ff40c12SJohn Marino 	    hapd->iconf->preamble == SHORT_PREAMBLE)
1943ff40c12SJohn Marino 		capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
1953ff40c12SJohn Marino 
1963ff40c12SJohn Marino 	privacy = hapd->conf->ssid.wep.keys_set;
1973ff40c12SJohn Marino 
1983ff40c12SJohn Marino 	if (hapd->conf->ieee802_1x &&
1993ff40c12SJohn Marino 	    (hapd->conf->default_wep_key_len ||
2003ff40c12SJohn Marino 	     hapd->conf->individual_wep_key_len))
2013ff40c12SJohn Marino 		privacy = 1;
2023ff40c12SJohn Marino 
2033ff40c12SJohn Marino 	if (hapd->conf->wpa)
2043ff40c12SJohn Marino 		privacy = 1;
2053ff40c12SJohn Marino 
206*a1157835SDaniel Fojt #ifdef CONFIG_HS20
207*a1157835SDaniel Fojt 	if (hapd->conf->osen)
208*a1157835SDaniel Fojt 		privacy = 1;
209*a1157835SDaniel Fojt #endif /* CONFIG_HS20 */
2103ff40c12SJohn Marino 
2113ff40c12SJohn Marino 	if (privacy)
2123ff40c12SJohn Marino 		capab |= WLAN_CAPABILITY_PRIVACY;
2133ff40c12SJohn Marino 
2143ff40c12SJohn Marino 	if (hapd->iface->current_mode &&
2153ff40c12SJohn Marino 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
2163ff40c12SJohn Marino 	    hapd->iface->num_sta_no_short_slot_time == 0)
2173ff40c12SJohn Marino 		capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
2183ff40c12SJohn Marino 
219*a1157835SDaniel Fojt 	/*
220*a1157835SDaniel Fojt 	 * Currently, Spectrum Management capability bit is set when directly
221*a1157835SDaniel Fojt 	 * requested in configuration by spectrum_mgmt_required or when AP is
222*a1157835SDaniel Fojt 	 * running on DFS channel.
223*a1157835SDaniel Fojt 	 * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
224*a1157835SDaniel Fojt 	 */
225*a1157835SDaniel Fojt 	if (hapd->iface->current_mode &&
226*a1157835SDaniel Fojt 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
227*a1157835SDaniel Fojt 	    (hapd->iconf->spectrum_mgmt_required || dfs))
228*a1157835SDaniel Fojt 		capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
229*a1157835SDaniel Fojt 
230*a1157835SDaniel Fojt 	for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
231*a1157835SDaniel Fojt 		if (hapd->conf->radio_measurements[i]) {
232*a1157835SDaniel Fojt 			capab |= IEEE80211_CAP_RRM;
233*a1157835SDaniel Fojt 			break;
234*a1157835SDaniel Fojt 		}
235*a1157835SDaniel Fojt 	}
236*a1157835SDaniel Fojt 
2373ff40c12SJohn Marino 	return capab;
2383ff40c12SJohn Marino }
2393ff40c12SJohn Marino 
2403ff40c12SJohn Marino 
241*a1157835SDaniel Fojt #ifndef CONFIG_NO_RC4
auth_shared_key(struct hostapd_data * hapd,struct sta_info * sta,u16 auth_transaction,const u8 * challenge,int iswep)2423ff40c12SJohn Marino static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
2433ff40c12SJohn Marino 			   u16 auth_transaction, const u8 *challenge,
2443ff40c12SJohn Marino 			   int iswep)
2453ff40c12SJohn Marino {
2463ff40c12SJohn Marino 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2473ff40c12SJohn Marino 		       HOSTAPD_LEVEL_DEBUG,
2483ff40c12SJohn Marino 		       "authentication (shared key, transaction %d)",
2493ff40c12SJohn Marino 		       auth_transaction);
2503ff40c12SJohn Marino 
2513ff40c12SJohn Marino 	if (auth_transaction == 1) {
2523ff40c12SJohn Marino 		if (!sta->challenge) {
2533ff40c12SJohn Marino 			/* Generate a pseudo-random challenge */
2543ff40c12SJohn Marino 			u8 key[8];
255*a1157835SDaniel Fojt 
2563ff40c12SJohn Marino 			sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
2573ff40c12SJohn Marino 			if (sta->challenge == NULL)
2583ff40c12SJohn Marino 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
2593ff40c12SJohn Marino 
260*a1157835SDaniel Fojt 			if (os_get_random(key, sizeof(key)) < 0) {
261*a1157835SDaniel Fojt 				os_free(sta->challenge);
262*a1157835SDaniel Fojt 				sta->challenge = NULL;
263*a1157835SDaniel Fojt 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
264*a1157835SDaniel Fojt 			}
265*a1157835SDaniel Fojt 
2663ff40c12SJohn Marino 			rc4_skip(key, sizeof(key), 0,
2673ff40c12SJohn Marino 				 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
2683ff40c12SJohn Marino 		}
2693ff40c12SJohn Marino 		return 0;
2703ff40c12SJohn Marino 	}
2713ff40c12SJohn Marino 
2723ff40c12SJohn Marino 	if (auth_transaction != 3)
2733ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2743ff40c12SJohn Marino 
2753ff40c12SJohn Marino 	/* Transaction 3 */
2763ff40c12SJohn Marino 	if (!iswep || !sta->challenge || !challenge ||
277*a1157835SDaniel Fojt 	    os_memcmp_const(sta->challenge, challenge,
278*a1157835SDaniel Fojt 			    WLAN_AUTH_CHALLENGE_LEN)) {
2793ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2803ff40c12SJohn Marino 			       HOSTAPD_LEVEL_INFO,
2813ff40c12SJohn Marino 			       "shared key authentication - invalid "
2823ff40c12SJohn Marino 			       "challenge-response");
2833ff40c12SJohn Marino 		return WLAN_STATUS_CHALLENGE_FAIL;
2843ff40c12SJohn Marino 	}
2853ff40c12SJohn Marino 
2863ff40c12SJohn Marino 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2873ff40c12SJohn Marino 		       HOSTAPD_LEVEL_DEBUG,
2883ff40c12SJohn Marino 		       "authentication OK (shared key)");
2893ff40c12SJohn Marino 	sta->flags |= WLAN_STA_AUTH;
2903ff40c12SJohn Marino 	wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
2913ff40c12SJohn Marino 	os_free(sta->challenge);
2923ff40c12SJohn Marino 	sta->challenge = NULL;
2933ff40c12SJohn Marino 
2943ff40c12SJohn Marino 	return 0;
2953ff40c12SJohn Marino }
296*a1157835SDaniel Fojt #endif /* CONFIG_NO_RC4 */
2973ff40c12SJohn Marino 
2983ff40c12SJohn Marino 
send_auth_reply(struct hostapd_data * hapd,const u8 * dst,const u8 * bssid,u16 auth_alg,u16 auth_transaction,u16 resp,const u8 * ies,size_t ies_len,const char * dbg)299*a1157835SDaniel Fojt static int send_auth_reply(struct hostapd_data *hapd,
3003ff40c12SJohn Marino 			   const u8 *dst, const u8 *bssid,
3013ff40c12SJohn Marino 			   u16 auth_alg, u16 auth_transaction, u16 resp,
302*a1157835SDaniel Fojt 			   const u8 *ies, size_t ies_len, const char *dbg)
3033ff40c12SJohn Marino {
3043ff40c12SJohn Marino 	struct ieee80211_mgmt *reply;
3053ff40c12SJohn Marino 	u8 *buf;
3063ff40c12SJohn Marino 	size_t rlen;
307*a1157835SDaniel Fojt 	int reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3083ff40c12SJohn Marino 
3093ff40c12SJohn Marino 	rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
3103ff40c12SJohn Marino 	buf = os_zalloc(rlen);
3113ff40c12SJohn Marino 	if (buf == NULL)
312*a1157835SDaniel Fojt 		return -1;
3133ff40c12SJohn Marino 
3143ff40c12SJohn Marino 	reply = (struct ieee80211_mgmt *) buf;
3153ff40c12SJohn Marino 	reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
3163ff40c12SJohn Marino 					    WLAN_FC_STYPE_AUTH);
3173ff40c12SJohn Marino 	os_memcpy(reply->da, dst, ETH_ALEN);
3183ff40c12SJohn Marino 	os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
3193ff40c12SJohn Marino 	os_memcpy(reply->bssid, bssid, ETH_ALEN);
3203ff40c12SJohn Marino 
3213ff40c12SJohn Marino 	reply->u.auth.auth_alg = host_to_le16(auth_alg);
3223ff40c12SJohn Marino 	reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
3233ff40c12SJohn Marino 	reply->u.auth.status_code = host_to_le16(resp);
3243ff40c12SJohn Marino 
3253ff40c12SJohn Marino 	if (ies && ies_len)
3263ff40c12SJohn Marino 		os_memcpy(reply->u.auth.variable, ies, ies_len);
3273ff40c12SJohn Marino 
3283ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
329*a1157835SDaniel Fojt 		   " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu) (dbg=%s)",
3303ff40c12SJohn Marino 		   MAC2STR(dst), auth_alg, auth_transaction,
331*a1157835SDaniel Fojt 		   resp, (unsigned long) ies_len, dbg);
3323ff40c12SJohn Marino 	if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
333*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "send_auth_reply: send failed");
334*a1157835SDaniel Fojt 	else
335*a1157835SDaniel Fojt 		reply_res = WLAN_STATUS_SUCCESS;
3363ff40c12SJohn Marino 
3373ff40c12SJohn Marino 	os_free(buf);
338*a1157835SDaniel Fojt 
339*a1157835SDaniel Fojt 	return reply_res;
3403ff40c12SJohn Marino }
3413ff40c12SJohn Marino 
3423ff40c12SJohn Marino 
343*a1157835SDaniel Fojt #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)3443ff40c12SJohn Marino static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
3453ff40c12SJohn Marino 				  u16 auth_transaction, u16 status,
3463ff40c12SJohn Marino 				  const u8 *ies, size_t ies_len)
3473ff40c12SJohn Marino {
3483ff40c12SJohn Marino 	struct hostapd_data *hapd = ctx;
3493ff40c12SJohn Marino 	struct sta_info *sta;
350*a1157835SDaniel Fojt 	int reply_res;
3513ff40c12SJohn Marino 
352*a1157835SDaniel Fojt 	reply_res = send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT,
353*a1157835SDaniel Fojt 				    auth_transaction, status, ies, ies_len,
354*a1157835SDaniel Fojt 				    "auth-ft-finish");
3553ff40c12SJohn Marino 
3563ff40c12SJohn Marino 	sta = ap_get_sta(hapd, dst);
3573ff40c12SJohn Marino 	if (sta == NULL)
3583ff40c12SJohn Marino 		return;
3593ff40c12SJohn Marino 
360*a1157835SDaniel Fojt 	if (sta->added_unassoc && (reply_res != WLAN_STATUS_SUCCESS ||
361*a1157835SDaniel Fojt 				   status != WLAN_STATUS_SUCCESS)) {
362*a1157835SDaniel Fojt 		hostapd_drv_sta_remove(hapd, sta->addr);
363*a1157835SDaniel Fojt 		sta->added_unassoc = 0;
364*a1157835SDaniel Fojt 		return;
365*a1157835SDaniel Fojt 	}
366*a1157835SDaniel Fojt 
367*a1157835SDaniel Fojt 	if (status != WLAN_STATUS_SUCCESS)
368*a1157835SDaniel Fojt 		return;
369*a1157835SDaniel Fojt 
3703ff40c12SJohn Marino 	hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
3713ff40c12SJohn Marino 		       HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
3723ff40c12SJohn Marino 	sta->flags |= WLAN_STA_AUTH;
3733ff40c12SJohn Marino 	mlme_authenticate_indication(hapd, sta);
3743ff40c12SJohn Marino }
375*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211R_AP */
3763ff40c12SJohn Marino 
3773ff40c12SJohn Marino 
3783ff40c12SJohn Marino #ifdef CONFIG_SAE
3793ff40c12SJohn Marino 
sae_set_state(struct sta_info * sta,enum sae_state state,const char * reason)380*a1157835SDaniel Fojt static void sae_set_state(struct sta_info *sta, enum sae_state state,
381*a1157835SDaniel Fojt 			  const char *reason)
382*a1157835SDaniel Fojt {
383*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SAE: State %s -> %s for peer " MACSTR " (%s)",
384*a1157835SDaniel Fojt 		   sae_state_txt(sta->sae->state), sae_state_txt(state),
385*a1157835SDaniel Fojt 		   MAC2STR(sta->addr), reason);
386*a1157835SDaniel Fojt 	sta->sae->state = state;
387*a1157835SDaniel Fojt }
388*a1157835SDaniel Fojt 
389*a1157835SDaniel Fojt 
auth_build_sae_commit(struct hostapd_data * hapd,struct sta_info * sta,int update)390*a1157835SDaniel Fojt static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
391*a1157835SDaniel Fojt 					     struct sta_info *sta, int update)
3923ff40c12SJohn Marino {
3933ff40c12SJohn Marino 	struct wpabuf *buf;
394*a1157835SDaniel Fojt 	const char *password = NULL;
395*a1157835SDaniel Fojt 	struct sae_password_entry *pw;
396*a1157835SDaniel Fojt 	const char *rx_id = NULL;
3973ff40c12SJohn Marino 
398*a1157835SDaniel Fojt 	if (sta->sae->tmp)
399*a1157835SDaniel Fojt 		rx_id = sta->sae->tmp->pw_id;
400*a1157835SDaniel Fojt 
401*a1157835SDaniel Fojt 	for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
402*a1157835SDaniel Fojt 		if (!is_broadcast_ether_addr(pw->peer_addr) &&
403*a1157835SDaniel Fojt 		    os_memcmp(pw->peer_addr, sta->addr, ETH_ALEN) != 0)
404*a1157835SDaniel Fojt 			continue;
405*a1157835SDaniel Fojt 		if ((rx_id && !pw->identifier) || (!rx_id && pw->identifier))
406*a1157835SDaniel Fojt 			continue;
407*a1157835SDaniel Fojt 		if (rx_id && pw->identifier &&
408*a1157835SDaniel Fojt 		    os_strcmp(rx_id, pw->identifier) != 0)
409*a1157835SDaniel Fojt 			continue;
410*a1157835SDaniel Fojt 		password = pw->password;
411*a1157835SDaniel Fojt 		break;
412*a1157835SDaniel Fojt 	}
413*a1157835SDaniel Fojt 	if (!password)
414*a1157835SDaniel Fojt 		password = hapd->conf->ssid.wpa_passphrase;
415*a1157835SDaniel Fojt 	if (!password) {
4163ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: No password available");
4173ff40c12SJohn Marino 		return NULL;
4183ff40c12SJohn Marino 	}
4193ff40c12SJohn Marino 
420*a1157835SDaniel Fojt 	if (update &&
421*a1157835SDaniel Fojt 	    sae_prepare_commit(hapd->own_addr, sta->addr,
422*a1157835SDaniel Fojt 			       (u8 *) password, os_strlen(password), rx_id,
4233ff40c12SJohn Marino 			       sta->sae) < 0) {
4243ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
4253ff40c12SJohn Marino 		return NULL;
4263ff40c12SJohn Marino 	}
4273ff40c12SJohn Marino 
428*a1157835SDaniel Fojt 	if (pw && pw->vlan_id) {
429*a1157835SDaniel Fojt 		if (!sta->sae->tmp) {
430*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
431*a1157835SDaniel Fojt 				   "SAE: No temporary data allocated - cannot store VLAN ID");
4323ff40c12SJohn Marino 			return NULL;
4333ff40c12SJohn Marino 		}
434*a1157835SDaniel Fojt 		sta->sae->tmp->vlan_id = pw->vlan_id;
435*a1157835SDaniel Fojt 	}
4363ff40c12SJohn Marino 
437*a1157835SDaniel Fojt 	buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN +
438*a1157835SDaniel Fojt 			   (rx_id ? 3 + os_strlen(rx_id) : 0));
4393ff40c12SJohn Marino 	if (buf == NULL)
4403ff40c12SJohn Marino 		return NULL;
441*a1157835SDaniel Fojt 	sae_write_commit(sta->sae, buf, sta->sae->tmp ?
442*a1157835SDaniel Fojt 			 sta->sae->tmp->anti_clogging_token : NULL, rx_id);
4433ff40c12SJohn Marino 
4443ff40c12SJohn Marino 	return buf;
4453ff40c12SJohn Marino }
4463ff40c12SJohn Marino 
4473ff40c12SJohn Marino 
auth_build_sae_confirm(struct hostapd_data * hapd,struct sta_info * sta)4483ff40c12SJohn Marino static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
4493ff40c12SJohn Marino 					      struct sta_info *sta)
4503ff40c12SJohn Marino {
4513ff40c12SJohn Marino 	struct wpabuf *buf;
4523ff40c12SJohn Marino 
4533ff40c12SJohn Marino 	buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
4543ff40c12SJohn Marino 	if (buf == NULL)
4553ff40c12SJohn Marino 		return NULL;
4563ff40c12SJohn Marino 
4573ff40c12SJohn Marino 	sae_write_confirm(sta->sae, buf);
4583ff40c12SJohn Marino 
4593ff40c12SJohn Marino 	return buf;
4603ff40c12SJohn Marino }
4613ff40c12SJohn Marino 
4623ff40c12SJohn Marino 
auth_sae_send_commit(struct hostapd_data * hapd,struct sta_info * sta,const u8 * bssid,int update)463*a1157835SDaniel Fojt static int auth_sae_send_commit(struct hostapd_data *hapd,
464*a1157835SDaniel Fojt 				struct sta_info *sta,
465*a1157835SDaniel Fojt 				const u8 *bssid, int update)
466*a1157835SDaniel Fojt {
467*a1157835SDaniel Fojt 	struct wpabuf *data;
468*a1157835SDaniel Fojt 	int reply_res;
469*a1157835SDaniel Fojt 
470*a1157835SDaniel Fojt 	data = auth_build_sae_commit(hapd, sta, update);
471*a1157835SDaniel Fojt 	if (!data && sta->sae->tmp && sta->sae->tmp->pw_id)
472*a1157835SDaniel Fojt 		return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
473*a1157835SDaniel Fojt 	if (data == NULL)
474*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
475*a1157835SDaniel Fojt 
476*a1157835SDaniel Fojt 	reply_res = send_auth_reply(hapd, sta->addr, bssid, WLAN_AUTH_SAE, 1,
477*a1157835SDaniel Fojt 				    WLAN_STATUS_SUCCESS, wpabuf_head(data),
478*a1157835SDaniel Fojt 				    wpabuf_len(data), "sae-send-commit");
479*a1157835SDaniel Fojt 
480*a1157835SDaniel Fojt 	wpabuf_free(data);
481*a1157835SDaniel Fojt 
482*a1157835SDaniel Fojt 	return reply_res;
483*a1157835SDaniel Fojt }
484*a1157835SDaniel Fojt 
485*a1157835SDaniel Fojt 
auth_sae_send_confirm(struct hostapd_data * hapd,struct sta_info * sta,const u8 * bssid)486*a1157835SDaniel Fojt static int auth_sae_send_confirm(struct hostapd_data *hapd,
487*a1157835SDaniel Fojt 				 struct sta_info *sta,
488*a1157835SDaniel Fojt 				 const u8 *bssid)
489*a1157835SDaniel Fojt {
490*a1157835SDaniel Fojt 	struct wpabuf *data;
491*a1157835SDaniel Fojt 	int reply_res;
492*a1157835SDaniel Fojt 
493*a1157835SDaniel Fojt 	data = auth_build_sae_confirm(hapd, sta);
494*a1157835SDaniel Fojt 	if (data == NULL)
495*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
496*a1157835SDaniel Fojt 
497*a1157835SDaniel Fojt 	reply_res = send_auth_reply(hapd, sta->addr, bssid, WLAN_AUTH_SAE, 2,
498*a1157835SDaniel Fojt 				    WLAN_STATUS_SUCCESS, wpabuf_head(data),
499*a1157835SDaniel Fojt 				    wpabuf_len(data), "sae-send-confirm");
500*a1157835SDaniel Fojt 
501*a1157835SDaniel Fojt 	wpabuf_free(data);
502*a1157835SDaniel Fojt 
503*a1157835SDaniel Fojt 	return reply_res;
504*a1157835SDaniel Fojt }
505*a1157835SDaniel Fojt 
506*a1157835SDaniel Fojt 
use_sae_anti_clogging(struct hostapd_data * hapd)5073ff40c12SJohn Marino static int use_sae_anti_clogging(struct hostapd_data *hapd)
5083ff40c12SJohn Marino {
5093ff40c12SJohn Marino 	struct sta_info *sta;
5103ff40c12SJohn Marino 	unsigned int open = 0;
5113ff40c12SJohn Marino 
5123ff40c12SJohn Marino 	if (hapd->conf->sae_anti_clogging_threshold == 0)
5133ff40c12SJohn Marino 		return 1;
5143ff40c12SJohn Marino 
5153ff40c12SJohn Marino 	for (sta = hapd->sta_list; sta; sta = sta->next) {
5163ff40c12SJohn Marino 		if (!sta->sae)
5173ff40c12SJohn Marino 			continue;
5183ff40c12SJohn Marino 		if (sta->sae->state != SAE_COMMITTED &&
5193ff40c12SJohn Marino 		    sta->sae->state != SAE_CONFIRMED)
5203ff40c12SJohn Marino 			continue;
5213ff40c12SJohn Marino 		open++;
5223ff40c12SJohn Marino 		if (open >= hapd->conf->sae_anti_clogging_threshold)
5233ff40c12SJohn Marino 			return 1;
5243ff40c12SJohn Marino 	}
5253ff40c12SJohn Marino 
526*a1157835SDaniel Fojt 	/* In addition to already existing open SAE sessions, check whether
527*a1157835SDaniel Fojt 	 * there are enough pending commit messages in the processing queue to
528*a1157835SDaniel Fojt 	 * potentially result in too many open sessions. */
529*a1157835SDaniel Fojt 	if (open + dl_list_len(&hapd->sae_commit_queue) >=
530*a1157835SDaniel Fojt 	    hapd->conf->sae_anti_clogging_threshold)
531*a1157835SDaniel Fojt 		return 1;
532*a1157835SDaniel Fojt 
5333ff40c12SJohn Marino 	return 0;
5343ff40c12SJohn Marino }
5353ff40c12SJohn Marino 
5363ff40c12SJohn Marino 
sae_token_hash(struct hostapd_data * hapd,const u8 * addr)537*a1157835SDaniel Fojt static u8 sae_token_hash(struct hostapd_data *hapd, const u8 *addr)
538*a1157835SDaniel Fojt {
539*a1157835SDaniel Fojt 	u8 hash[SHA256_MAC_LEN];
540*a1157835SDaniel Fojt 
541*a1157835SDaniel Fojt 	hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
542*a1157835SDaniel Fojt 		    addr, ETH_ALEN, hash);
543*a1157835SDaniel Fojt 	return hash[0];
544*a1157835SDaniel Fojt }
545*a1157835SDaniel Fojt 
546*a1157835SDaniel Fojt 
check_sae_token(struct hostapd_data * hapd,const u8 * addr,const u8 * token,size_t token_len)5473ff40c12SJohn Marino static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
5483ff40c12SJohn Marino 			   const u8 *token, size_t token_len)
5493ff40c12SJohn Marino {
5503ff40c12SJohn Marino 	u8 mac[SHA256_MAC_LEN];
551*a1157835SDaniel Fojt 	const u8 *addrs[2];
552*a1157835SDaniel Fojt 	size_t len[2];
553*a1157835SDaniel Fojt 	u16 token_idx;
554*a1157835SDaniel Fojt 	u8 idx;
5553ff40c12SJohn Marino 
5563ff40c12SJohn Marino 	if (token_len != SHA256_MAC_LEN)
5573ff40c12SJohn Marino 		return -1;
558*a1157835SDaniel Fojt 	idx = sae_token_hash(hapd, addr);
559*a1157835SDaniel Fojt 	token_idx = hapd->sae_pending_token_idx[idx];
560*a1157835SDaniel Fojt 	if (token_idx == 0 || token_idx != WPA_GET_BE16(token)) {
561*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: Invalid anti-clogging token from "
562*a1157835SDaniel Fojt 			   MACSTR " - token_idx 0x%04x, expected 0x%04x",
563*a1157835SDaniel Fojt 			   MAC2STR(addr), WPA_GET_BE16(token), token_idx);
5643ff40c12SJohn Marino 		return -1;
565*a1157835SDaniel Fojt 	}
566*a1157835SDaniel Fojt 
567*a1157835SDaniel Fojt 	addrs[0] = addr;
568*a1157835SDaniel Fojt 	len[0] = ETH_ALEN;
569*a1157835SDaniel Fojt 	addrs[1] = token;
570*a1157835SDaniel Fojt 	len[1] = 2;
571*a1157835SDaniel Fojt 	if (hmac_sha256_vector(hapd->sae_token_key, sizeof(hapd->sae_token_key),
572*a1157835SDaniel Fojt 			       2, addrs, len, mac) < 0 ||
573*a1157835SDaniel Fojt 	    os_memcmp_const(token + 2, &mac[2], SHA256_MAC_LEN - 2) != 0)
574*a1157835SDaniel Fojt 		return -1;
575*a1157835SDaniel Fojt 
576*a1157835SDaniel Fojt 	hapd->sae_pending_token_idx[idx] = 0; /* invalidate used token */
5773ff40c12SJohn Marino 
5783ff40c12SJohn Marino 	return 0;
5793ff40c12SJohn Marino }
5803ff40c12SJohn Marino 
5813ff40c12SJohn Marino 
auth_build_token_req(struct hostapd_data * hapd,int group,const u8 * addr)5823ff40c12SJohn Marino static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
583*a1157835SDaniel Fojt 					    int group, const u8 *addr)
5843ff40c12SJohn Marino {
5853ff40c12SJohn Marino 	struct wpabuf *buf;
5863ff40c12SJohn Marino 	u8 *token;
5873ff40c12SJohn Marino 	struct os_reltime now;
588*a1157835SDaniel Fojt 	u8 idx[2];
589*a1157835SDaniel Fojt 	const u8 *addrs[2];
590*a1157835SDaniel Fojt 	size_t len[2];
591*a1157835SDaniel Fojt 	u8 p_idx;
592*a1157835SDaniel Fojt 	u16 token_idx;
5933ff40c12SJohn Marino 
5943ff40c12SJohn Marino 	os_get_reltime(&now);
5953ff40c12SJohn Marino 	if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
596*a1157835SDaniel Fojt 	    os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60) ||
597*a1157835SDaniel Fojt 	    hapd->sae_token_idx == 0xffff) {
5983ff40c12SJohn Marino 		if (random_get_bytes(hapd->sae_token_key,
5993ff40c12SJohn Marino 				     sizeof(hapd->sae_token_key)) < 0)
6003ff40c12SJohn Marino 			return NULL;
6013ff40c12SJohn Marino 		wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
6023ff40c12SJohn Marino 			    hapd->sae_token_key, sizeof(hapd->sae_token_key));
6033ff40c12SJohn Marino 		hapd->last_sae_token_key_update = now;
604*a1157835SDaniel Fojt 		hapd->sae_token_idx = 0;
605*a1157835SDaniel Fojt 		os_memset(hapd->sae_pending_token_idx, 0,
606*a1157835SDaniel Fojt 			  sizeof(hapd->sae_pending_token_idx));
6073ff40c12SJohn Marino 	}
6083ff40c12SJohn Marino 
609*a1157835SDaniel Fojt 	buf = wpabuf_alloc(sizeof(le16) + SHA256_MAC_LEN);
6103ff40c12SJohn Marino 	if (buf == NULL)
6113ff40c12SJohn Marino 		return NULL;
6123ff40c12SJohn Marino 
613*a1157835SDaniel Fojt 	wpabuf_put_le16(buf, group); /* Finite Cyclic Group */
614*a1157835SDaniel Fojt 
615*a1157835SDaniel Fojt 	p_idx = sae_token_hash(hapd, addr);
616*a1157835SDaniel Fojt 	token_idx = hapd->sae_pending_token_idx[p_idx];
617*a1157835SDaniel Fojt 	if (!token_idx) {
618*a1157835SDaniel Fojt 		hapd->sae_token_idx++;
619*a1157835SDaniel Fojt 		token_idx = hapd->sae_token_idx;
620*a1157835SDaniel Fojt 		hapd->sae_pending_token_idx[p_idx] = token_idx;
621*a1157835SDaniel Fojt 	}
622*a1157835SDaniel Fojt 	WPA_PUT_BE16(idx, token_idx);
6233ff40c12SJohn Marino 	token = wpabuf_put(buf, SHA256_MAC_LEN);
624*a1157835SDaniel Fojt 	addrs[0] = addr;
625*a1157835SDaniel Fojt 	len[0] = ETH_ALEN;
626*a1157835SDaniel Fojt 	addrs[1] = idx;
627*a1157835SDaniel Fojt 	len[1] = sizeof(idx);
628*a1157835SDaniel Fojt 	if (hmac_sha256_vector(hapd->sae_token_key, sizeof(hapd->sae_token_key),
629*a1157835SDaniel Fojt 			       2, addrs, len, token) < 0) {
630*a1157835SDaniel Fojt 		wpabuf_free(buf);
631*a1157835SDaniel Fojt 		return NULL;
632*a1157835SDaniel Fojt 	}
633*a1157835SDaniel Fojt 	WPA_PUT_BE16(token, token_idx);
6343ff40c12SJohn Marino 
6353ff40c12SJohn Marino 	return buf;
6363ff40c12SJohn Marino }
6373ff40c12SJohn Marino 
6383ff40c12SJohn Marino 
sae_check_big_sync(struct hostapd_data * hapd,struct sta_info * sta)639*a1157835SDaniel Fojt static int sae_check_big_sync(struct hostapd_data *hapd, struct sta_info *sta)
640*a1157835SDaniel Fojt {
641*a1157835SDaniel Fojt 	if (sta->sae->sync > hapd->conf->sae_sync) {
642*a1157835SDaniel Fojt 		sae_set_state(sta, SAE_NOTHING, "Sync > dot11RSNASAESync");
643*a1157835SDaniel Fojt 		sta->sae->sync = 0;
644*a1157835SDaniel Fojt 		return -1;
645*a1157835SDaniel Fojt 	}
646*a1157835SDaniel Fojt 	return 0;
647*a1157835SDaniel Fojt }
648*a1157835SDaniel Fojt 
649*a1157835SDaniel Fojt 
auth_sae_retransmit_timer(void * eloop_ctx,void * eloop_data)650*a1157835SDaniel Fojt static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
651*a1157835SDaniel Fojt {
652*a1157835SDaniel Fojt 	struct hostapd_data *hapd = eloop_ctx;
653*a1157835SDaniel Fojt 	struct sta_info *sta = eloop_data;
654*a1157835SDaniel Fojt 	int ret;
655*a1157835SDaniel Fojt 
656*a1157835SDaniel Fojt 	if (sae_check_big_sync(hapd, sta))
657*a1157835SDaniel Fojt 		return;
658*a1157835SDaniel Fojt 	sta->sae->sync++;
659*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SAE: Auth SAE retransmit timer for " MACSTR
660*a1157835SDaniel Fojt 		   " (sync=%d state=%s)",
661*a1157835SDaniel Fojt 		   MAC2STR(sta->addr), sta->sae->sync,
662*a1157835SDaniel Fojt 		   sae_state_txt(sta->sae->state));
663*a1157835SDaniel Fojt 
664*a1157835SDaniel Fojt 	switch (sta->sae->state) {
665*a1157835SDaniel Fojt 	case SAE_COMMITTED:
666*a1157835SDaniel Fojt 		ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
667*a1157835SDaniel Fojt 		eloop_register_timeout(0,
668*a1157835SDaniel Fojt 				       hapd->dot11RSNASAERetransPeriod * 1000,
669*a1157835SDaniel Fojt 				       auth_sae_retransmit_timer, hapd, sta);
670*a1157835SDaniel Fojt 		break;
671*a1157835SDaniel Fojt 	case SAE_CONFIRMED:
672*a1157835SDaniel Fojt 		ret = auth_sae_send_confirm(hapd, sta, hapd->own_addr);
673*a1157835SDaniel Fojt 		eloop_register_timeout(0,
674*a1157835SDaniel Fojt 				       hapd->dot11RSNASAERetransPeriod * 1000,
675*a1157835SDaniel Fojt 				       auth_sae_retransmit_timer, hapd, sta);
676*a1157835SDaniel Fojt 		break;
677*a1157835SDaniel Fojt 	default:
678*a1157835SDaniel Fojt 		ret = -1;
679*a1157835SDaniel Fojt 		break;
680*a1157835SDaniel Fojt 	}
681*a1157835SDaniel Fojt 
682*a1157835SDaniel Fojt 	if (ret != WLAN_STATUS_SUCCESS)
683*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
684*a1157835SDaniel Fojt }
685*a1157835SDaniel Fojt 
686*a1157835SDaniel Fojt 
sae_clear_retransmit_timer(struct hostapd_data * hapd,struct sta_info * sta)687*a1157835SDaniel Fojt void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
688*a1157835SDaniel Fojt {
689*a1157835SDaniel Fojt 	eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
690*a1157835SDaniel Fojt }
691*a1157835SDaniel Fojt 
692*a1157835SDaniel Fojt 
sae_set_retransmit_timer(struct hostapd_data * hapd,struct sta_info * sta)693*a1157835SDaniel Fojt static void sae_set_retransmit_timer(struct hostapd_data *hapd,
694*a1157835SDaniel Fojt 				     struct sta_info *sta)
695*a1157835SDaniel Fojt {
696*a1157835SDaniel Fojt 	if (!(hapd->conf->mesh & MESH_ENABLED))
697*a1157835SDaniel Fojt 		return;
698*a1157835SDaniel Fojt 
699*a1157835SDaniel Fojt 	eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
700*a1157835SDaniel Fojt 	eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
701*a1157835SDaniel Fojt 			       auth_sae_retransmit_timer, hapd, sta);
702*a1157835SDaniel Fojt }
703*a1157835SDaniel Fojt 
704*a1157835SDaniel Fojt 
sae_sme_send_external_auth_status(struct hostapd_data * hapd,struct sta_info * sta,u16 status)705*a1157835SDaniel Fojt static void sae_sme_send_external_auth_status(struct hostapd_data *hapd,
706*a1157835SDaniel Fojt 					      struct sta_info *sta, u16 status)
707*a1157835SDaniel Fojt {
708*a1157835SDaniel Fojt 	struct external_auth params;
709*a1157835SDaniel Fojt 
710*a1157835SDaniel Fojt 	os_memset(&params, 0, sizeof(params));
711*a1157835SDaniel Fojt 	params.status = status;
712*a1157835SDaniel Fojt 	params.bssid = sta->addr;
713*a1157835SDaniel Fojt 	if (status == WLAN_STATUS_SUCCESS && sta->sae &&
714*a1157835SDaniel Fojt 	    !hapd->conf->disable_pmksa_caching)
715*a1157835SDaniel Fojt 		params.pmkid = sta->sae->pmkid;
716*a1157835SDaniel Fojt 
717*a1157835SDaniel Fojt 	hostapd_drv_send_external_auth_status(hapd, &params);
718*a1157835SDaniel Fojt }
719*a1157835SDaniel Fojt 
720*a1157835SDaniel Fojt 
sae_accept_sta(struct hostapd_data * hapd,struct sta_info * sta)721*a1157835SDaniel Fojt void sae_accept_sta(struct hostapd_data *hapd, struct sta_info *sta)
722*a1157835SDaniel Fojt {
723*a1157835SDaniel Fojt #ifndef CONFIG_NO_VLAN
724*a1157835SDaniel Fojt 	struct vlan_description vlan_desc;
725*a1157835SDaniel Fojt 
726*a1157835SDaniel Fojt 	if (sta->sae->tmp && sta->sae->tmp->vlan_id > 0) {
727*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: Assign STA " MACSTR
728*a1157835SDaniel Fojt 			   " to VLAN ID %d",
729*a1157835SDaniel Fojt 			   MAC2STR(sta->addr), sta->sae->tmp->vlan_id);
730*a1157835SDaniel Fojt 
731*a1157835SDaniel Fojt 		os_memset(&vlan_desc, 0, sizeof(vlan_desc));
732*a1157835SDaniel Fojt 		vlan_desc.notempty = 1;
733*a1157835SDaniel Fojt 		vlan_desc.untagged = sta->sae->tmp->vlan_id;
734*a1157835SDaniel Fojt 		if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
735*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
736*a1157835SDaniel Fojt 				   "Invalid VLAN ID %d in sae_password",
737*a1157835SDaniel Fojt 				   sta->sae->tmp->vlan_id);
738*a1157835SDaniel Fojt 			return;
739*a1157835SDaniel Fojt 		}
740*a1157835SDaniel Fojt 
741*a1157835SDaniel Fojt 		if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0 ||
742*a1157835SDaniel Fojt 		    ap_sta_bind_vlan(hapd, sta) < 0) {
743*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
744*a1157835SDaniel Fojt 				   "Failed to assign VLAN ID %d from sae_password to "
745*a1157835SDaniel Fojt 				   MACSTR, sta->sae->tmp->vlan_id,
746*a1157835SDaniel Fojt 				   MAC2STR(sta->addr));
747*a1157835SDaniel Fojt 			return;
748*a1157835SDaniel Fojt 		}
749*a1157835SDaniel Fojt 	}
750*a1157835SDaniel Fojt #endif /* CONFIG_NO_VLAN */
751*a1157835SDaniel Fojt 
752*a1157835SDaniel Fojt 	sta->flags |= WLAN_STA_AUTH;
753*a1157835SDaniel Fojt 	sta->auth_alg = WLAN_AUTH_SAE;
754*a1157835SDaniel Fojt 	mlme_authenticate_indication(hapd, sta);
755*a1157835SDaniel Fojt 	wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
756*a1157835SDaniel Fojt 	sae_set_state(sta, SAE_ACCEPTED, "Accept Confirm");
757*a1157835SDaniel Fojt 	wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
758*a1157835SDaniel Fojt 			       sta->sae->pmk, sta->sae->pmkid);
759*a1157835SDaniel Fojt 	sae_sme_send_external_auth_status(hapd, sta, WLAN_STATUS_SUCCESS);
760*a1157835SDaniel Fojt }
761*a1157835SDaniel Fojt 
762*a1157835SDaniel Fojt 
sae_sm_step(struct hostapd_data * hapd,struct sta_info * sta,const u8 * bssid,u8 auth_transaction,int allow_reuse,int * sta_removed)763*a1157835SDaniel Fojt static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
764*a1157835SDaniel Fojt 		       const u8 *bssid, u8 auth_transaction, int allow_reuse,
765*a1157835SDaniel Fojt 		       int *sta_removed)
766*a1157835SDaniel Fojt {
767*a1157835SDaniel Fojt 	int ret;
768*a1157835SDaniel Fojt 
769*a1157835SDaniel Fojt 	*sta_removed = 0;
770*a1157835SDaniel Fojt 
771*a1157835SDaniel Fojt 	if (auth_transaction != 1 && auth_transaction != 2)
772*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
773*a1157835SDaniel Fojt 
774*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SAE: Peer " MACSTR " state=%s auth_trans=%u",
775*a1157835SDaniel Fojt 		   MAC2STR(sta->addr), sae_state_txt(sta->sae->state),
776*a1157835SDaniel Fojt 		   auth_transaction);
777*a1157835SDaniel Fojt 	switch (sta->sae->state) {
778*a1157835SDaniel Fojt 	case SAE_NOTHING:
779*a1157835SDaniel Fojt 		if (auth_transaction == 1) {
780*a1157835SDaniel Fojt 			ret = auth_sae_send_commit(hapd, sta, bssid,
781*a1157835SDaniel Fojt 						   !allow_reuse);
782*a1157835SDaniel Fojt 			if (ret)
783*a1157835SDaniel Fojt 				return ret;
784*a1157835SDaniel Fojt 			sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
785*a1157835SDaniel Fojt 
786*a1157835SDaniel Fojt 			if (sae_process_commit(sta->sae) < 0)
787*a1157835SDaniel Fojt 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
788*a1157835SDaniel Fojt 
789*a1157835SDaniel Fojt 			/*
790*a1157835SDaniel Fojt 			 * In mesh case, both Commit and Confirm can be sent
791*a1157835SDaniel Fojt 			 * immediately. In infrastructure BSS, only a single
792*a1157835SDaniel Fojt 			 * Authentication frame (Commit) is expected from the AP
793*a1157835SDaniel Fojt 			 * here and the second one (Confirm) will be sent once
794*a1157835SDaniel Fojt 			 * the STA has sent its second Authentication frame
795*a1157835SDaniel Fojt 			 * (Confirm).
796*a1157835SDaniel Fojt 			 */
797*a1157835SDaniel Fojt 			if (hapd->conf->mesh & MESH_ENABLED) {
798*a1157835SDaniel Fojt 				/*
799*a1157835SDaniel Fojt 				 * Send both Commit and Confirm immediately
800*a1157835SDaniel Fojt 				 * based on SAE finite state machine
801*a1157835SDaniel Fojt 				 * Nothing -> Confirm transition.
802*a1157835SDaniel Fojt 				 */
803*a1157835SDaniel Fojt 				ret = auth_sae_send_confirm(hapd, sta, bssid);
804*a1157835SDaniel Fojt 				if (ret)
805*a1157835SDaniel Fojt 					return ret;
806*a1157835SDaniel Fojt 				sae_set_state(sta, SAE_CONFIRMED,
807*a1157835SDaniel Fojt 					      "Sent Confirm (mesh)");
808*a1157835SDaniel Fojt 			} else {
809*a1157835SDaniel Fojt 				/*
810*a1157835SDaniel Fojt 				 * For infrastructure BSS, send only the Commit
811*a1157835SDaniel Fojt 				 * message now to get alternating sequence of
812*a1157835SDaniel Fojt 				 * Authentication frames between the AP and STA.
813*a1157835SDaniel Fojt 				 * Confirm will be sent in
814*a1157835SDaniel Fojt 				 * Committed -> Confirmed/Accepted transition
815*a1157835SDaniel Fojt 				 * when receiving Confirm from STA.
816*a1157835SDaniel Fojt 				 */
817*a1157835SDaniel Fojt 			}
818*a1157835SDaniel Fojt 			sta->sae->sync = 0;
819*a1157835SDaniel Fojt 			sae_set_retransmit_timer(hapd, sta);
820*a1157835SDaniel Fojt 		} else {
821*a1157835SDaniel Fojt 			hostapd_logger(hapd, sta->addr,
822*a1157835SDaniel Fojt 				       HOSTAPD_MODULE_IEEE80211,
823*a1157835SDaniel Fojt 				       HOSTAPD_LEVEL_DEBUG,
824*a1157835SDaniel Fojt 				       "SAE confirm before commit");
825*a1157835SDaniel Fojt 		}
826*a1157835SDaniel Fojt 		break;
827*a1157835SDaniel Fojt 	case SAE_COMMITTED:
828*a1157835SDaniel Fojt 		sae_clear_retransmit_timer(hapd, sta);
829*a1157835SDaniel Fojt 		if (auth_transaction == 1) {
830*a1157835SDaniel Fojt 			if (sae_process_commit(sta->sae) < 0)
831*a1157835SDaniel Fojt 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
832*a1157835SDaniel Fojt 
833*a1157835SDaniel Fojt 			ret = auth_sae_send_confirm(hapd, sta, bssid);
834*a1157835SDaniel Fojt 			if (ret)
835*a1157835SDaniel Fojt 				return ret;
836*a1157835SDaniel Fojt 			sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
837*a1157835SDaniel Fojt 			sta->sae->sync = 0;
838*a1157835SDaniel Fojt 			sae_set_retransmit_timer(hapd, sta);
839*a1157835SDaniel Fojt 		} else if (hapd->conf->mesh & MESH_ENABLED) {
840*a1157835SDaniel Fojt 			/*
841*a1157835SDaniel Fojt 			 * In mesh case, follow SAE finite state machine and
842*a1157835SDaniel Fojt 			 * send Commit now, if sync count allows.
843*a1157835SDaniel Fojt 			 */
844*a1157835SDaniel Fojt 			if (sae_check_big_sync(hapd, sta))
845*a1157835SDaniel Fojt 				return WLAN_STATUS_SUCCESS;
846*a1157835SDaniel Fojt 			sta->sae->sync++;
847*a1157835SDaniel Fojt 
848*a1157835SDaniel Fojt 			ret = auth_sae_send_commit(hapd, sta, bssid, 0);
849*a1157835SDaniel Fojt 			if (ret)
850*a1157835SDaniel Fojt 				return ret;
851*a1157835SDaniel Fojt 
852*a1157835SDaniel Fojt 			sae_set_retransmit_timer(hapd, sta);
853*a1157835SDaniel Fojt 		} else {
854*a1157835SDaniel Fojt 			/*
855*a1157835SDaniel Fojt 			 * For instructure BSS, send the postponed Confirm from
856*a1157835SDaniel Fojt 			 * Nothing -> Confirmed transition that was reduced to
857*a1157835SDaniel Fojt 			 * Nothing -> Committed above.
858*a1157835SDaniel Fojt 			 */
859*a1157835SDaniel Fojt 			ret = auth_sae_send_confirm(hapd, sta, bssid);
860*a1157835SDaniel Fojt 			if (ret)
861*a1157835SDaniel Fojt 				return ret;
862*a1157835SDaniel Fojt 
863*a1157835SDaniel Fojt 			sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
864*a1157835SDaniel Fojt 
865*a1157835SDaniel Fojt 			/*
866*a1157835SDaniel Fojt 			 * Since this was triggered on Confirm RX, run another
867*a1157835SDaniel Fojt 			 * step to get to Accepted without waiting for
868*a1157835SDaniel Fojt 			 * additional events.
869*a1157835SDaniel Fojt 			 */
870*a1157835SDaniel Fojt 			return sae_sm_step(hapd, sta, bssid, auth_transaction,
871*a1157835SDaniel Fojt 					   0, sta_removed);
872*a1157835SDaniel Fojt 		}
873*a1157835SDaniel Fojt 		break;
874*a1157835SDaniel Fojt 	case SAE_CONFIRMED:
875*a1157835SDaniel Fojt 		sae_clear_retransmit_timer(hapd, sta);
876*a1157835SDaniel Fojt 		if (auth_transaction == 1) {
877*a1157835SDaniel Fojt 			if (sae_check_big_sync(hapd, sta))
878*a1157835SDaniel Fojt 				return WLAN_STATUS_SUCCESS;
879*a1157835SDaniel Fojt 			sta->sae->sync++;
880*a1157835SDaniel Fojt 
881*a1157835SDaniel Fojt 			ret = auth_sae_send_commit(hapd, sta, bssid, 1);
882*a1157835SDaniel Fojt 			if (ret)
883*a1157835SDaniel Fojt 				return ret;
884*a1157835SDaniel Fojt 
885*a1157835SDaniel Fojt 			if (sae_process_commit(sta->sae) < 0)
886*a1157835SDaniel Fojt 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
887*a1157835SDaniel Fojt 
888*a1157835SDaniel Fojt 			ret = auth_sae_send_confirm(hapd, sta, bssid);
889*a1157835SDaniel Fojt 			if (ret)
890*a1157835SDaniel Fojt 				return ret;
891*a1157835SDaniel Fojt 
892*a1157835SDaniel Fojt 			sae_set_retransmit_timer(hapd, sta);
893*a1157835SDaniel Fojt 		} else {
894*a1157835SDaniel Fojt 			sta->sae->send_confirm = 0xffff;
895*a1157835SDaniel Fojt 			sae_accept_sta(hapd, sta);
896*a1157835SDaniel Fojt 		}
897*a1157835SDaniel Fojt 		break;
898*a1157835SDaniel Fojt 	case SAE_ACCEPTED:
899*a1157835SDaniel Fojt 		if (auth_transaction == 1 &&
900*a1157835SDaniel Fojt 		    (hapd->conf->mesh & MESH_ENABLED)) {
901*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
902*a1157835SDaniel Fojt 				   ") doing reauthentication",
903*a1157835SDaniel Fojt 				   MAC2STR(sta->addr));
904*a1157835SDaniel Fojt 			wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
905*a1157835SDaniel Fojt 			ap_free_sta(hapd, sta);
906*a1157835SDaniel Fojt 			*sta_removed = 1;
907*a1157835SDaniel Fojt 		} else if (auth_transaction == 1) {
908*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "SAE: Start reauthentication");
909*a1157835SDaniel Fojt 			ret = auth_sae_send_commit(hapd, sta, bssid, 1);
910*a1157835SDaniel Fojt 			if (ret)
911*a1157835SDaniel Fojt 				return ret;
912*a1157835SDaniel Fojt 			sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
913*a1157835SDaniel Fojt 
914*a1157835SDaniel Fojt 			if (sae_process_commit(sta->sae) < 0)
915*a1157835SDaniel Fojt 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
916*a1157835SDaniel Fojt 			sta->sae->sync = 0;
917*a1157835SDaniel Fojt 			sae_set_retransmit_timer(hapd, sta);
918*a1157835SDaniel Fojt 		} else {
919*a1157835SDaniel Fojt 			if (sae_check_big_sync(hapd, sta))
920*a1157835SDaniel Fojt 				return WLAN_STATUS_SUCCESS;
921*a1157835SDaniel Fojt 			sta->sae->sync++;
922*a1157835SDaniel Fojt 
923*a1157835SDaniel Fojt 			ret = auth_sae_send_confirm(hapd, sta, bssid);
924*a1157835SDaniel Fojt 			sae_clear_temp_data(sta->sae);
925*a1157835SDaniel Fojt 			if (ret)
926*a1157835SDaniel Fojt 				return ret;
927*a1157835SDaniel Fojt 		}
928*a1157835SDaniel Fojt 		break;
929*a1157835SDaniel Fojt 	default:
930*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "SAE: invalid state %d",
931*a1157835SDaniel Fojt 			   sta->sae->state);
932*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
933*a1157835SDaniel Fojt 	}
934*a1157835SDaniel Fojt 	return WLAN_STATUS_SUCCESS;
935*a1157835SDaniel Fojt }
936*a1157835SDaniel Fojt 
937*a1157835SDaniel Fojt 
sae_pick_next_group(struct hostapd_data * hapd,struct sta_info * sta)938*a1157835SDaniel Fojt static void sae_pick_next_group(struct hostapd_data *hapd, struct sta_info *sta)
939*a1157835SDaniel Fojt {
940*a1157835SDaniel Fojt 	struct sae_data *sae = sta->sae;
941*a1157835SDaniel Fojt 	int i, *groups = hapd->conf->sae_groups;
942*a1157835SDaniel Fojt 	int default_groups[] = { 19, 0 };
943*a1157835SDaniel Fojt 
944*a1157835SDaniel Fojt 	if (sae->state != SAE_COMMITTED)
945*a1157835SDaniel Fojt 		return;
946*a1157835SDaniel Fojt 
947*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SAE: Previously selected group: %d", sae->group);
948*a1157835SDaniel Fojt 
949*a1157835SDaniel Fojt 	if (!groups)
950*a1157835SDaniel Fojt 		groups = default_groups;
951*a1157835SDaniel Fojt 	for (i = 0; groups[i] > 0; i++) {
952*a1157835SDaniel Fojt 		if (sae->group == groups[i])
953*a1157835SDaniel Fojt 			break;
954*a1157835SDaniel Fojt 	}
955*a1157835SDaniel Fojt 
956*a1157835SDaniel Fojt 	if (groups[i] <= 0) {
957*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
958*a1157835SDaniel Fojt 			   "SAE: Previously selected group not found from the current configuration");
959*a1157835SDaniel Fojt 		return;
960*a1157835SDaniel Fojt 	}
961*a1157835SDaniel Fojt 
962*a1157835SDaniel Fojt 	for (;;) {
963*a1157835SDaniel Fojt 		i++;
964*a1157835SDaniel Fojt 		if (groups[i] <= 0) {
965*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
966*a1157835SDaniel Fojt 				   "SAE: No alternative group enabled");
967*a1157835SDaniel Fojt 			return;
968*a1157835SDaniel Fojt 		}
969*a1157835SDaniel Fojt 
970*a1157835SDaniel Fojt 		if (sae_set_group(sae, groups[i]) < 0)
971*a1157835SDaniel Fojt 			continue;
972*a1157835SDaniel Fojt 
973*a1157835SDaniel Fojt 		break;
974*a1157835SDaniel Fojt 	}
975*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SAE: Selected new group: %d", groups[i]);
976*a1157835SDaniel Fojt }
977*a1157835SDaniel Fojt 
978*a1157835SDaniel Fojt 
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)9793ff40c12SJohn Marino static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
9803ff40c12SJohn Marino 			    const struct ieee80211_mgmt *mgmt, size_t len,
981*a1157835SDaniel Fojt 			    u16 auth_transaction, u16 status_code)
9823ff40c12SJohn Marino {
983*a1157835SDaniel Fojt 	int resp = WLAN_STATUS_SUCCESS;
9843ff40c12SJohn Marino 	struct wpabuf *data = NULL;
985*a1157835SDaniel Fojt 	int *groups = hapd->conf->sae_groups;
986*a1157835SDaniel Fojt 	int default_groups[] = { 19, 0 };
987*a1157835SDaniel Fojt 	const u8 *pos, *end;
988*a1157835SDaniel Fojt 	int sta_removed = 0;
9893ff40c12SJohn Marino 
990*a1157835SDaniel Fojt 	if (!groups)
991*a1157835SDaniel Fojt 		groups = default_groups;
992*a1157835SDaniel Fojt 
993*a1157835SDaniel Fojt #ifdef CONFIG_TESTING_OPTIONS
994*a1157835SDaniel Fojt 	if (hapd->conf->sae_reflection_attack && auth_transaction == 1) {
995*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: TESTING - reflection attack");
996*a1157835SDaniel Fojt 		pos = mgmt->u.auth.variable;
997*a1157835SDaniel Fojt 		end = ((const u8 *) mgmt) + len;
998*a1157835SDaniel Fojt 		send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
999*a1157835SDaniel Fojt 				auth_transaction, resp, pos, end - pos,
1000*a1157835SDaniel Fojt 				"auth-sae-reflection-attack");
1001*a1157835SDaniel Fojt 		goto remove_sta;
1002*a1157835SDaniel Fojt 	}
1003*a1157835SDaniel Fojt 
1004*a1157835SDaniel Fojt 	if (hapd->conf->sae_commit_override && auth_transaction == 1) {
1005*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: TESTING - commit override");
1006*a1157835SDaniel Fojt 		send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
1007*a1157835SDaniel Fojt 				auth_transaction, resp,
1008*a1157835SDaniel Fojt 				wpabuf_head(hapd->conf->sae_commit_override),
1009*a1157835SDaniel Fojt 				wpabuf_len(hapd->conf->sae_commit_override),
1010*a1157835SDaniel Fojt 				"sae-commit-override");
1011*a1157835SDaniel Fojt 		goto remove_sta;
1012*a1157835SDaniel Fojt 	}
1013*a1157835SDaniel Fojt #endif /* CONFIG_TESTING_OPTIONS */
10143ff40c12SJohn Marino 	if (!sta->sae) {
1015*a1157835SDaniel Fojt 		if (auth_transaction != 1 ||
1016*a1157835SDaniel Fojt 		    status_code != WLAN_STATUS_SUCCESS) {
1017*a1157835SDaniel Fojt 			resp = -1;
1018*a1157835SDaniel Fojt 			goto remove_sta;
1019*a1157835SDaniel Fojt 		}
10203ff40c12SJohn Marino 		sta->sae = os_zalloc(sizeof(*sta->sae));
1021*a1157835SDaniel Fojt 		if (!sta->sae) {
1022*a1157835SDaniel Fojt 			resp = -1;
1023*a1157835SDaniel Fojt 			goto remove_sta;
1024*a1157835SDaniel Fojt 		}
1025*a1157835SDaniel Fojt 		sae_set_state(sta, SAE_NOTHING, "Init");
1026*a1157835SDaniel Fojt 		sta->sae->sync = 0;
1027*a1157835SDaniel Fojt 	}
1028*a1157835SDaniel Fojt 
1029*a1157835SDaniel Fojt 	if (sta->mesh_sae_pmksa_caching) {
1030*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1031*a1157835SDaniel Fojt 			   "SAE: Cancel use of mesh PMKSA caching because peer starts SAE authentication");
1032*a1157835SDaniel Fojt 		wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1033*a1157835SDaniel Fojt 		sta->mesh_sae_pmksa_caching = 0;
10343ff40c12SJohn Marino 	}
10353ff40c12SJohn Marino 
10363ff40c12SJohn Marino 	if (auth_transaction == 1) {
10373ff40c12SJohn Marino 		const u8 *token = NULL;
10383ff40c12SJohn Marino 		size_t token_len = 0;
1039*a1157835SDaniel Fojt 		int allow_reuse = 0;
1040*a1157835SDaniel Fojt 
10413ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
10423ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
1043*a1157835SDaniel Fojt 			       "start SAE authentication (RX commit, status=%u (%s))",
1044*a1157835SDaniel Fojt 			       status_code, status2str(status_code));
1045*a1157835SDaniel Fojt 
1046*a1157835SDaniel Fojt 		if ((hapd->conf->mesh & MESH_ENABLED) &&
1047*a1157835SDaniel Fojt 		    status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
1048*a1157835SDaniel Fojt 		    sta->sae->tmp) {
1049*a1157835SDaniel Fojt 			pos = mgmt->u.auth.variable;
1050*a1157835SDaniel Fojt 			end = ((const u8 *) mgmt) + len;
1051*a1157835SDaniel Fojt 			if (pos + sizeof(le16) > end) {
1052*a1157835SDaniel Fojt 				wpa_printf(MSG_ERROR,
1053*a1157835SDaniel Fojt 					   "SAE: Too short anti-clogging token request");
1054*a1157835SDaniel Fojt 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1055*a1157835SDaniel Fojt 				goto reply;
1056*a1157835SDaniel Fojt 			}
1057*a1157835SDaniel Fojt 			resp = sae_group_allowed(sta->sae, groups,
1058*a1157835SDaniel Fojt 						 WPA_GET_LE16(pos));
1059*a1157835SDaniel Fojt 			if (resp != WLAN_STATUS_SUCCESS) {
1060*a1157835SDaniel Fojt 				wpa_printf(MSG_ERROR,
1061*a1157835SDaniel Fojt 					   "SAE: Invalid group in anti-clogging token request");
1062*a1157835SDaniel Fojt 				goto reply;
1063*a1157835SDaniel Fojt 			}
1064*a1157835SDaniel Fojt 			pos += sizeof(le16);
1065*a1157835SDaniel Fojt 
1066*a1157835SDaniel Fojt 			wpabuf_free(sta->sae->tmp->anti_clogging_token);
1067*a1157835SDaniel Fojt 			sta->sae->tmp->anti_clogging_token =
1068*a1157835SDaniel Fojt 				wpabuf_alloc_copy(pos, end - pos);
1069*a1157835SDaniel Fojt 			if (sta->sae->tmp->anti_clogging_token == NULL) {
1070*a1157835SDaniel Fojt 				wpa_printf(MSG_ERROR,
1071*a1157835SDaniel Fojt 					   "SAE: Failed to alloc for anti-clogging token");
1072*a1157835SDaniel Fojt 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1073*a1157835SDaniel Fojt 				goto remove_sta;
1074*a1157835SDaniel Fojt 			}
1075*a1157835SDaniel Fojt 
1076*a1157835SDaniel Fojt 			/*
1077*a1157835SDaniel Fojt 			 * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
1078*a1157835SDaniel Fojt 			 * is 76, a new Commit Message shall be constructed
1079*a1157835SDaniel Fojt 			 * with the Anti-Clogging Token from the received
1080*a1157835SDaniel Fojt 			 * Authentication frame, and the commit-scalar and
1081*a1157835SDaniel Fojt 			 * COMMIT-ELEMENT previously sent.
1082*a1157835SDaniel Fojt 			 */
1083*a1157835SDaniel Fojt 			resp = auth_sae_send_commit(hapd, sta, mgmt->bssid, 0);
1084*a1157835SDaniel Fojt 			if (resp != WLAN_STATUS_SUCCESS) {
1085*a1157835SDaniel Fojt 				wpa_printf(MSG_ERROR,
1086*a1157835SDaniel Fojt 					   "SAE: Failed to send commit message");
1087*a1157835SDaniel Fojt 				goto remove_sta;
1088*a1157835SDaniel Fojt 			}
1089*a1157835SDaniel Fojt 			sae_set_state(sta, SAE_COMMITTED,
1090*a1157835SDaniel Fojt 				      "Sent Commit (anti-clogging token case in mesh)");
1091*a1157835SDaniel Fojt 			sta->sae->sync = 0;
1092*a1157835SDaniel Fojt 			sae_set_retransmit_timer(hapd, sta);
1093*a1157835SDaniel Fojt 			return;
1094*a1157835SDaniel Fojt 		}
1095*a1157835SDaniel Fojt 
1096*a1157835SDaniel Fojt 		if ((hapd->conf->mesh & MESH_ENABLED) &&
1097*a1157835SDaniel Fojt 		    status_code ==
1098*a1157835SDaniel Fojt 		    WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1099*a1157835SDaniel Fojt 		    sta->sae->tmp) {
1100*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1101*a1157835SDaniel Fojt 				   "SAE: Peer did not accept our SAE group");
1102*a1157835SDaniel Fojt 			sae_pick_next_group(hapd, sta);
1103*a1157835SDaniel Fojt 			goto remove_sta;
1104*a1157835SDaniel Fojt 		}
1105*a1157835SDaniel Fojt 
1106*a1157835SDaniel Fojt 		if (status_code != WLAN_STATUS_SUCCESS)
1107*a1157835SDaniel Fojt 			goto remove_sta;
1108*a1157835SDaniel Fojt 
1109*a1157835SDaniel Fojt 		if (!(hapd->conf->mesh & MESH_ENABLED) &&
1110*a1157835SDaniel Fojt 		    sta->sae->state == SAE_COMMITTED) {
1111*a1157835SDaniel Fojt 			/* This is needed in the infrastructure BSS case to
1112*a1157835SDaniel Fojt 			 * address a sequence where a STA entry may remain in
1113*a1157835SDaniel Fojt 			 * hostapd across two attempts to do SAE authentication
1114*a1157835SDaniel Fojt 			 * by the same STA. The second attempt may end up trying
1115*a1157835SDaniel Fojt 			 * to use a different group and that would not be
1116*a1157835SDaniel Fojt 			 * allowed if we remain in Committed state with the
1117*a1157835SDaniel Fojt 			 * previously set parameters. */
1118*a1157835SDaniel Fojt 			pos = mgmt->u.auth.variable;
1119*a1157835SDaniel Fojt 			end = ((const u8 *) mgmt) + len;
1120*a1157835SDaniel Fojt 			if (end - pos >= (int) sizeof(le16) &&
1121*a1157835SDaniel Fojt 			    sae_group_allowed(sta->sae, groups,
1122*a1157835SDaniel Fojt 					      WPA_GET_LE16(pos)) ==
1123*a1157835SDaniel Fojt 			    WLAN_STATUS_SUCCESS) {
1124*a1157835SDaniel Fojt 				/* Do not waste resources deriving the same PWE
1125*a1157835SDaniel Fojt 				 * again since the same group is reused. */
1126*a1157835SDaniel Fojt 				sae_set_state(sta, SAE_NOTHING,
1127*a1157835SDaniel Fojt 					      "Allow previous PWE to be reused");
1128*a1157835SDaniel Fojt 				allow_reuse = 1;
1129*a1157835SDaniel Fojt 			} else {
1130*a1157835SDaniel Fojt 				sae_set_state(sta, SAE_NOTHING,
1131*a1157835SDaniel Fojt 					      "Clear existing state to allow restart");
1132*a1157835SDaniel Fojt 				sae_clear_data(sta->sae);
1133*a1157835SDaniel Fojt 			}
1134*a1157835SDaniel Fojt 		}
1135*a1157835SDaniel Fojt 
11363ff40c12SJohn Marino 		resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
11373ff40c12SJohn Marino 					((const u8 *) mgmt) + len -
11383ff40c12SJohn Marino 					mgmt->u.auth.variable, &token,
1139*a1157835SDaniel Fojt 					&token_len, groups);
1140*a1157835SDaniel Fojt 		if (resp == SAE_SILENTLY_DISCARD) {
1141*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1142*a1157835SDaniel Fojt 				   "SAE: Drop commit message from " MACSTR " due to reflection attack",
1143*a1157835SDaniel Fojt 				   MAC2STR(sta->addr));
1144*a1157835SDaniel Fojt 			goto remove_sta;
1145*a1157835SDaniel Fojt 		}
1146*a1157835SDaniel Fojt 
1147*a1157835SDaniel Fojt 		if (resp == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER) {
1148*a1157835SDaniel Fojt 			wpa_msg(hapd->msg_ctx, MSG_INFO,
1149*a1157835SDaniel Fojt 				WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER
1150*a1157835SDaniel Fojt 				MACSTR, MAC2STR(sta->addr));
1151*a1157835SDaniel Fojt 			sae_clear_retransmit_timer(hapd, sta);
1152*a1157835SDaniel Fojt 			sae_set_state(sta, SAE_NOTHING,
1153*a1157835SDaniel Fojt 				      "Unknown Password Identifier");
1154*a1157835SDaniel Fojt 			goto remove_sta;
1155*a1157835SDaniel Fojt 		}
1156*a1157835SDaniel Fojt 
11573ff40c12SJohn Marino 		if (token && check_sae_token(hapd, sta->addr, token, token_len)
11583ff40c12SJohn Marino 		    < 0) {
11593ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
11603ff40c12SJohn Marino 				   "incorrect token from " MACSTR,
11613ff40c12SJohn Marino 				   MAC2STR(sta->addr));
1162*a1157835SDaniel Fojt 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1163*a1157835SDaniel Fojt 			goto remove_sta;
1164*a1157835SDaniel Fojt 		}
1165*a1157835SDaniel Fojt 
1166*a1157835SDaniel Fojt 		if (resp != WLAN_STATUS_SUCCESS)
1167*a1157835SDaniel Fojt 			goto reply;
1168*a1157835SDaniel Fojt 
1169*a1157835SDaniel Fojt 		if (!token && use_sae_anti_clogging(hapd) && !allow_reuse) {
1170*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1171*a1157835SDaniel Fojt 				   "SAE: Request anti-clogging token from "
1172*a1157835SDaniel Fojt 				   MACSTR, MAC2STR(sta->addr));
1173*a1157835SDaniel Fojt 			data = auth_build_token_req(hapd, sta->sae->group,
1174*a1157835SDaniel Fojt 						    sta->addr);
1175*a1157835SDaniel Fojt 			resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
1176*a1157835SDaniel Fojt 			if (hapd->conf->mesh & MESH_ENABLED)
1177*a1157835SDaniel Fojt 				sae_set_state(sta, SAE_NOTHING,
1178*a1157835SDaniel Fojt 					      "Request anti-clogging token case in mesh");
1179*a1157835SDaniel Fojt 			goto reply;
1180*a1157835SDaniel Fojt 		}
1181*a1157835SDaniel Fojt 
1182*a1157835SDaniel Fojt 		resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction,
1183*a1157835SDaniel Fojt 				   allow_reuse, &sta_removed);
1184*a1157835SDaniel Fojt 	} else if (auth_transaction == 2) {
1185*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1186*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_DEBUG,
1187*a1157835SDaniel Fojt 			       "SAE authentication (RX confirm, status=%u (%s))",
1188*a1157835SDaniel Fojt 			       status_code, status2str(status_code));
1189*a1157835SDaniel Fojt 		if (status_code != WLAN_STATUS_SUCCESS)
1190*a1157835SDaniel Fojt 			goto remove_sta;
1191*a1157835SDaniel Fojt 		if (sta->sae->state >= SAE_CONFIRMED ||
1192*a1157835SDaniel Fojt 		    !(hapd->conf->mesh & MESH_ENABLED)) {
1193*a1157835SDaniel Fojt 			const u8 *var;
1194*a1157835SDaniel Fojt 			size_t var_len;
1195*a1157835SDaniel Fojt 			u16 peer_send_confirm;
1196*a1157835SDaniel Fojt 
1197*a1157835SDaniel Fojt 			var = mgmt->u.auth.variable;
1198*a1157835SDaniel Fojt 			var_len = ((u8 *) mgmt) + len - mgmt->u.auth.variable;
1199*a1157835SDaniel Fojt 			if (var_len < 2) {
1200*a1157835SDaniel Fojt 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1201*a1157835SDaniel Fojt 				goto reply;
1202*a1157835SDaniel Fojt 			}
1203*a1157835SDaniel Fojt 
1204*a1157835SDaniel Fojt 			peer_send_confirm = WPA_GET_LE16(var);
1205*a1157835SDaniel Fojt 
1206*a1157835SDaniel Fojt 			if (sta->sae->state == SAE_ACCEPTED &&
1207*a1157835SDaniel Fojt 			    (peer_send_confirm <= sta->sae->rc ||
1208*a1157835SDaniel Fojt 			     peer_send_confirm == 0xffff)) {
1209*a1157835SDaniel Fojt 				wpa_printf(MSG_DEBUG,
1210*a1157835SDaniel Fojt 					   "SAE: Silently ignore unexpected Confirm from peer "
1211*a1157835SDaniel Fojt 					   MACSTR
1212*a1157835SDaniel Fojt 					   " (peer-send-confirm=%u Rc=%u)",
1213*a1157835SDaniel Fojt 					   MAC2STR(sta->addr),
1214*a1157835SDaniel Fojt 					   peer_send_confirm, sta->sae->rc);
12153ff40c12SJohn Marino 				return;
12163ff40c12SJohn Marino 			}
12173ff40c12SJohn Marino 
1218*a1157835SDaniel Fojt 			if (sae_check_confirm(sta->sae, var, var_len) < 0) {
12193ff40c12SJohn Marino 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1220*a1157835SDaniel Fojt 				goto reply;
12213ff40c12SJohn Marino 			}
1222*a1157835SDaniel Fojt 			sta->sae->rc = peer_send_confirm;
12233ff40c12SJohn Marino 		}
1224*a1157835SDaniel Fojt 		resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction, 0,
1225*a1157835SDaniel Fojt 			&sta_removed);
12263ff40c12SJohn Marino 	} else {
12273ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
12283ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
1229*a1157835SDaniel Fojt 			       "unexpected SAE authentication transaction %u (status=%u (%s))",
1230*a1157835SDaniel Fojt 			       auth_transaction, status_code,
1231*a1157835SDaniel Fojt 			       status2str(status_code));
1232*a1157835SDaniel Fojt 		if (status_code != WLAN_STATUS_SUCCESS)
1233*a1157835SDaniel Fojt 			goto remove_sta;
12343ff40c12SJohn Marino 		resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
12353ff40c12SJohn Marino 	}
12363ff40c12SJohn Marino 
1237*a1157835SDaniel Fojt reply:
1238*a1157835SDaniel Fojt 	if (!sta_removed && resp != WLAN_STATUS_SUCCESS) {
1239*a1157835SDaniel Fojt 		pos = mgmt->u.auth.variable;
1240*a1157835SDaniel Fojt 		end = ((const u8 *) mgmt) + len;
12413ff40c12SJohn Marino 
1242*a1157835SDaniel Fojt 		/* Copy the Finite Cyclic Group field from the request if we
1243*a1157835SDaniel Fojt 		 * rejected it as unsupported group. */
1244*a1157835SDaniel Fojt 		if (resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1245*a1157835SDaniel Fojt 		    !data && end - pos >= 2)
1246*a1157835SDaniel Fojt 			data = wpabuf_alloc_copy(pos, 2);
1247*a1157835SDaniel Fojt 
1248*a1157835SDaniel Fojt 		sae_sme_send_external_auth_status(hapd, sta, resp);
12493ff40c12SJohn Marino 		send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
12503ff40c12SJohn Marino 				auth_transaction, resp,
12513ff40c12SJohn Marino 				data ? wpabuf_head(data) : (u8 *) "",
1252*a1157835SDaniel Fojt 				data ? wpabuf_len(data) : 0, "auth-sae");
1253*a1157835SDaniel Fojt 	}
1254*a1157835SDaniel Fojt 
1255*a1157835SDaniel Fojt remove_sta:
1256*a1157835SDaniel Fojt 	if (!sta_removed && sta->added_unassoc &&
1257*a1157835SDaniel Fojt 	    (resp != WLAN_STATUS_SUCCESS ||
1258*a1157835SDaniel Fojt 	     status_code != WLAN_STATUS_SUCCESS)) {
1259*a1157835SDaniel Fojt 		hostapd_drv_sta_remove(hapd, sta->addr);
1260*a1157835SDaniel Fojt 		sta->added_unassoc = 0;
1261*a1157835SDaniel Fojt 	}
12623ff40c12SJohn Marino 	wpabuf_free(data);
12633ff40c12SJohn Marino }
1264*a1157835SDaniel Fojt 
1265*a1157835SDaniel Fojt 
1266*a1157835SDaniel Fojt /**
1267*a1157835SDaniel Fojt  * auth_sae_init_committed - Send COMMIT and start SAE in committed state
1268*a1157835SDaniel Fojt  * @hapd: BSS data for the device initiating the authentication
1269*a1157835SDaniel Fojt  * @sta: the peer to which commit authentication frame is sent
1270*a1157835SDaniel Fojt  *
1271*a1157835SDaniel Fojt  * This function implements Init event handling (IEEE Std 802.11-2012,
1272*a1157835SDaniel Fojt  * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
1273*a1157835SDaniel Fojt  * sta->sae structure should be initialized appropriately via a call to
1274*a1157835SDaniel Fojt  * sae_prepare_commit().
1275*a1157835SDaniel Fojt  */
auth_sae_init_committed(struct hostapd_data * hapd,struct sta_info * sta)1276*a1157835SDaniel Fojt int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
1277*a1157835SDaniel Fojt {
1278*a1157835SDaniel Fojt 	int ret;
1279*a1157835SDaniel Fojt 
1280*a1157835SDaniel Fojt 	if (!sta->sae || !sta->sae->tmp)
1281*a1157835SDaniel Fojt 		return -1;
1282*a1157835SDaniel Fojt 
1283*a1157835SDaniel Fojt 	if (sta->sae->state != SAE_NOTHING)
1284*a1157835SDaniel Fojt 		return -1;
1285*a1157835SDaniel Fojt 
1286*a1157835SDaniel Fojt 	ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
1287*a1157835SDaniel Fojt 	if (ret)
1288*a1157835SDaniel Fojt 		return -1;
1289*a1157835SDaniel Fojt 
1290*a1157835SDaniel Fojt 	sae_set_state(sta, SAE_COMMITTED, "Init and sent commit");
1291*a1157835SDaniel Fojt 	sta->sae->sync = 0;
1292*a1157835SDaniel Fojt 	sae_set_retransmit_timer(hapd, sta);
1293*a1157835SDaniel Fojt 
1294*a1157835SDaniel Fojt 	return 0;
1295*a1157835SDaniel Fojt }
1296*a1157835SDaniel Fojt 
1297*a1157835SDaniel Fojt 
auth_sae_process_commit(void * eloop_ctx,void * user_ctx)1298*a1157835SDaniel Fojt void auth_sae_process_commit(void *eloop_ctx, void *user_ctx)
1299*a1157835SDaniel Fojt {
1300*a1157835SDaniel Fojt 	struct hostapd_data *hapd = eloop_ctx;
1301*a1157835SDaniel Fojt 	struct hostapd_sae_commit_queue *q;
1302*a1157835SDaniel Fojt 	unsigned int queue_len;
1303*a1157835SDaniel Fojt 
1304*a1157835SDaniel Fojt 	q = dl_list_first(&hapd->sae_commit_queue,
1305*a1157835SDaniel Fojt 			  struct hostapd_sae_commit_queue, list);
1306*a1157835SDaniel Fojt 	if (!q)
1307*a1157835SDaniel Fojt 		return;
1308*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
1309*a1157835SDaniel Fojt 		   "SAE: Process next available message from queue");
1310*a1157835SDaniel Fojt 	dl_list_del(&q->list);
1311*a1157835SDaniel Fojt 	handle_auth(hapd, (const struct ieee80211_mgmt *) q->msg, q->len,
1312*a1157835SDaniel Fojt 		    q->rssi, 1);
1313*a1157835SDaniel Fojt 	os_free(q);
1314*a1157835SDaniel Fojt 
1315*a1157835SDaniel Fojt 	if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
1316*a1157835SDaniel Fojt 		return;
1317*a1157835SDaniel Fojt 	queue_len = dl_list_len(&hapd->sae_commit_queue);
1318*a1157835SDaniel Fojt 	eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
1319*a1157835SDaniel Fojt 			       hapd, NULL);
1320*a1157835SDaniel Fojt }
1321*a1157835SDaniel Fojt 
1322*a1157835SDaniel Fojt 
auth_sae_queue(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int rssi)1323*a1157835SDaniel Fojt static void auth_sae_queue(struct hostapd_data *hapd,
1324*a1157835SDaniel Fojt 			   const struct ieee80211_mgmt *mgmt, size_t len,
1325*a1157835SDaniel Fojt 			   int rssi)
1326*a1157835SDaniel Fojt {
1327*a1157835SDaniel Fojt 	struct hostapd_sae_commit_queue *q, *q2;
1328*a1157835SDaniel Fojt 	unsigned int queue_len;
1329*a1157835SDaniel Fojt 	const struct ieee80211_mgmt *mgmt2;
1330*a1157835SDaniel Fojt 
1331*a1157835SDaniel Fojt 	queue_len = dl_list_len(&hapd->sae_commit_queue);
1332*a1157835SDaniel Fojt 	if (queue_len >= 15) {
1333*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1334*a1157835SDaniel Fojt 			   "SAE: No more room in message queue - drop the new frame from "
1335*a1157835SDaniel Fojt 			   MACSTR, MAC2STR(mgmt->sa));
1336*a1157835SDaniel Fojt 		return;
1337*a1157835SDaniel Fojt 	}
1338*a1157835SDaniel Fojt 
1339*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SAE: Queue Authentication message from "
1340*a1157835SDaniel Fojt 		   MACSTR " for processing (queue_len %u)", MAC2STR(mgmt->sa),
1341*a1157835SDaniel Fojt 		   queue_len);
1342*a1157835SDaniel Fojt 	q = os_zalloc(sizeof(*q) + len);
1343*a1157835SDaniel Fojt 	if (!q)
1344*a1157835SDaniel Fojt 		return;
1345*a1157835SDaniel Fojt 	q->rssi = rssi;
1346*a1157835SDaniel Fojt 	q->len = len;
1347*a1157835SDaniel Fojt 	os_memcpy(q->msg, mgmt, len);
1348*a1157835SDaniel Fojt 
1349*a1157835SDaniel Fojt 	/* Check whether there is already a queued Authentication frame from the
1350*a1157835SDaniel Fojt 	 * same station with the same transaction number and if so, replace that
1351*a1157835SDaniel Fojt 	 * queue entry with the new one. This avoids issues with a peer that
1352*a1157835SDaniel Fojt 	 * sends multiple times (e.g., due to frequent SAE retries). There is no
1353*a1157835SDaniel Fojt 	 * point in us trying to process the old attempts after a new one has
1354*a1157835SDaniel Fojt 	 * obsoleted them. */
1355*a1157835SDaniel Fojt 	dl_list_for_each(q2, &hapd->sae_commit_queue,
1356*a1157835SDaniel Fojt 			 struct hostapd_sae_commit_queue, list) {
1357*a1157835SDaniel Fojt 		mgmt2 = (const struct ieee80211_mgmt *) q2->msg;
1358*a1157835SDaniel Fojt 		if (os_memcmp(mgmt->sa, mgmt2->sa, ETH_ALEN) == 0 &&
1359*a1157835SDaniel Fojt 		    mgmt->u.auth.auth_transaction ==
1360*a1157835SDaniel Fojt 		    mgmt2->u.auth.auth_transaction) {
1361*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1362*a1157835SDaniel Fojt 				   "SAE: Replace queued message from same STA with same transaction number");
1363*a1157835SDaniel Fojt 			dl_list_add(&q2->list, &q->list);
1364*a1157835SDaniel Fojt 			dl_list_del(&q2->list);
1365*a1157835SDaniel Fojt 			os_free(q2);
1366*a1157835SDaniel Fojt 			goto queued;
1367*a1157835SDaniel Fojt 		}
1368*a1157835SDaniel Fojt 	}
1369*a1157835SDaniel Fojt 
1370*a1157835SDaniel Fojt 	/* No pending identical entry, so add to the end of the queue */
1371*a1157835SDaniel Fojt 	dl_list_add_tail(&hapd->sae_commit_queue, &q->list);
1372*a1157835SDaniel Fojt 
1373*a1157835SDaniel Fojt queued:
1374*a1157835SDaniel Fojt 	if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
1375*a1157835SDaniel Fojt 		return;
1376*a1157835SDaniel Fojt 	eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
1377*a1157835SDaniel Fojt 			       hapd, NULL);
1378*a1157835SDaniel Fojt }
1379*a1157835SDaniel Fojt 
1380*a1157835SDaniel Fojt 
auth_sae_queued_addr(struct hostapd_data * hapd,const u8 * addr)1381*a1157835SDaniel Fojt static int auth_sae_queued_addr(struct hostapd_data *hapd, const u8 *addr)
1382*a1157835SDaniel Fojt {
1383*a1157835SDaniel Fojt 	struct hostapd_sae_commit_queue *q;
1384*a1157835SDaniel Fojt 	const struct ieee80211_mgmt *mgmt;
1385*a1157835SDaniel Fojt 
1386*a1157835SDaniel Fojt 	dl_list_for_each(q, &hapd->sae_commit_queue,
1387*a1157835SDaniel Fojt 			 struct hostapd_sae_commit_queue, list) {
1388*a1157835SDaniel Fojt 		mgmt = (const struct ieee80211_mgmt *) q->msg;
1389*a1157835SDaniel Fojt 		if (os_memcmp(addr, mgmt->sa, ETH_ALEN) == 0)
1390*a1157835SDaniel Fojt 			return 1;
1391*a1157835SDaniel Fojt 	}
1392*a1157835SDaniel Fojt 
1393*a1157835SDaniel Fojt 	return 0;
1394*a1157835SDaniel Fojt }
1395*a1157835SDaniel Fojt 
13963ff40c12SJohn Marino #endif /* CONFIG_SAE */
13973ff40c12SJohn Marino 
13983ff40c12SJohn Marino 
wpa_res_to_status_code(int res)1399*a1157835SDaniel Fojt static u16 wpa_res_to_status_code(int res)
1400*a1157835SDaniel Fojt {
1401*a1157835SDaniel Fojt 	if (res == WPA_INVALID_GROUP)
1402*a1157835SDaniel Fojt 		return WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1403*a1157835SDaniel Fojt 	if (res == WPA_INVALID_PAIRWISE)
1404*a1157835SDaniel Fojt 		return WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1405*a1157835SDaniel Fojt 	if (res == WPA_INVALID_AKMP)
1406*a1157835SDaniel Fojt 		return WLAN_STATUS_AKMP_NOT_VALID;
1407*a1157835SDaniel Fojt 	if (res == WPA_ALLOC_FAIL)
1408*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1409*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211W
1410*a1157835SDaniel Fojt 	if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1411*a1157835SDaniel Fojt 		return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1412*a1157835SDaniel Fojt 	if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
1413*a1157835SDaniel Fojt 		return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
1414*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211W */
1415*a1157835SDaniel Fojt 	if (res == WPA_INVALID_MDIE)
1416*a1157835SDaniel Fojt 		return WLAN_STATUS_INVALID_MDIE;
1417*a1157835SDaniel Fojt 	if (res == WPA_INVALID_PMKID)
1418*a1157835SDaniel Fojt 		return WLAN_STATUS_INVALID_PMKID;
1419*a1157835SDaniel Fojt 	if (res != WPA_IE_OK)
1420*a1157835SDaniel Fojt 		return WLAN_STATUS_INVALID_IE;
1421*a1157835SDaniel Fojt 	return WLAN_STATUS_SUCCESS;
1422*a1157835SDaniel Fojt }
1423*a1157835SDaniel Fojt 
1424*a1157835SDaniel Fojt 
1425*a1157835SDaniel Fojt #ifdef CONFIG_FILS
1426*a1157835SDaniel Fojt 
1427*a1157835SDaniel Fojt static void handle_auth_fils_finish(struct hostapd_data *hapd,
1428*a1157835SDaniel Fojt 				    struct sta_info *sta, u16 resp,
1429*a1157835SDaniel Fojt 				    struct wpabuf *data, int pub);
1430*a1157835SDaniel Fojt 
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))1431*a1157835SDaniel Fojt void handle_auth_fils(struct hostapd_data *hapd, struct sta_info *sta,
1432*a1157835SDaniel Fojt 		      const u8 *pos, size_t len, u16 auth_alg,
1433*a1157835SDaniel Fojt 		      u16 auth_transaction, u16 status_code,
1434*a1157835SDaniel Fojt 		      void (*cb)(struct hostapd_data *hapd,
1435*a1157835SDaniel Fojt 				 struct sta_info *sta, u16 resp,
1436*a1157835SDaniel Fojt 				 struct wpabuf *data, int pub))
1437*a1157835SDaniel Fojt {
1438*a1157835SDaniel Fojt 	u16 resp = WLAN_STATUS_SUCCESS;
1439*a1157835SDaniel Fojt 	const u8 *end;
1440*a1157835SDaniel Fojt 	struct ieee802_11_elems elems;
1441*a1157835SDaniel Fojt 	int res;
1442*a1157835SDaniel Fojt 	struct wpa_ie_data rsn;
1443*a1157835SDaniel Fojt 	struct rsn_pmksa_cache_entry *pmksa = NULL;
1444*a1157835SDaniel Fojt 
1445*a1157835SDaniel Fojt 	if (auth_transaction != 1 || status_code != WLAN_STATUS_SUCCESS)
1446*a1157835SDaniel Fojt 		return;
1447*a1157835SDaniel Fojt 
1448*a1157835SDaniel Fojt 	end = pos + len;
1449*a1157835SDaniel Fojt 
1450*a1157835SDaniel Fojt 	wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
1451*a1157835SDaniel Fojt 		    pos, end - pos);
1452*a1157835SDaniel Fojt 
1453*a1157835SDaniel Fojt 	/* TODO: FILS PK */
1454*a1157835SDaniel Fojt #ifdef CONFIG_FILS_SK_PFS
1455*a1157835SDaniel Fojt 	if (auth_alg == WLAN_AUTH_FILS_SK_PFS) {
1456*a1157835SDaniel Fojt 		u16 group;
1457*a1157835SDaniel Fojt 		struct wpabuf *pub;
1458*a1157835SDaniel Fojt 		size_t elem_len;
1459*a1157835SDaniel Fojt 
1460*a1157835SDaniel Fojt 		/* Using FILS PFS */
1461*a1157835SDaniel Fojt 
1462*a1157835SDaniel Fojt 		/* Finite Cyclic Group */
1463*a1157835SDaniel Fojt 		if (end - pos < 2) {
1464*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1465*a1157835SDaniel Fojt 				   "FILS: No room for Finite Cyclic Group");
1466*a1157835SDaniel Fojt 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1467*a1157835SDaniel Fojt 			goto fail;
1468*a1157835SDaniel Fojt 		}
1469*a1157835SDaniel Fojt 		group = WPA_GET_LE16(pos);
1470*a1157835SDaniel Fojt 		pos += 2;
1471*a1157835SDaniel Fojt 		if (group != hapd->conf->fils_dh_group) {
1472*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1473*a1157835SDaniel Fojt 				   "FILS: Unsupported Finite Cyclic Group: %u (expected %u)",
1474*a1157835SDaniel Fojt 				   group, hapd->conf->fils_dh_group);
1475*a1157835SDaniel Fojt 			resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1476*a1157835SDaniel Fojt 			goto fail;
1477*a1157835SDaniel Fojt 		}
1478*a1157835SDaniel Fojt 
1479*a1157835SDaniel Fojt 		crypto_ecdh_deinit(sta->fils_ecdh);
1480*a1157835SDaniel Fojt 		sta->fils_ecdh = crypto_ecdh_init(group);
1481*a1157835SDaniel Fojt 		if (!sta->fils_ecdh) {
1482*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
1483*a1157835SDaniel Fojt 				   "FILS: Could not initialize ECDH with group %d",
1484*a1157835SDaniel Fojt 				   group);
1485*a1157835SDaniel Fojt 			resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1486*a1157835SDaniel Fojt 			goto fail;
1487*a1157835SDaniel Fojt 		}
1488*a1157835SDaniel Fojt 
1489*a1157835SDaniel Fojt 		pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
1490*a1157835SDaniel Fojt 		if (!pub) {
1491*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1492*a1157835SDaniel Fojt 				   "FILS: Failed to derive ECDH public key");
1493*a1157835SDaniel Fojt 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1494*a1157835SDaniel Fojt 			goto fail;
1495*a1157835SDaniel Fojt 		}
1496*a1157835SDaniel Fojt 		elem_len = wpabuf_len(pub);
1497*a1157835SDaniel Fojt 		wpabuf_free(pub);
1498*a1157835SDaniel Fojt 
1499*a1157835SDaniel Fojt 		/* Element */
1500*a1157835SDaniel Fojt 		if ((size_t) (end - pos) < elem_len) {
1501*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "FILS: No room for Element");
1502*a1157835SDaniel Fojt 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1503*a1157835SDaniel Fojt 			goto fail;
1504*a1157835SDaniel Fojt 		}
1505*a1157835SDaniel Fojt 
1506*a1157835SDaniel Fojt 		wpabuf_free(sta->fils_g_sta);
1507*a1157835SDaniel Fojt 		sta->fils_g_sta = wpabuf_alloc_copy(pos, elem_len);
1508*a1157835SDaniel Fojt 		wpabuf_clear_free(sta->fils_dh_ss);
1509*a1157835SDaniel Fojt 		sta->fils_dh_ss = crypto_ecdh_set_peerkey(sta->fils_ecdh, 1,
1510*a1157835SDaniel Fojt 							  pos, elem_len);
1511*a1157835SDaniel Fojt 		if (!sta->fils_dh_ss) {
1512*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
1513*a1157835SDaniel Fojt 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1514*a1157835SDaniel Fojt 			goto fail;
1515*a1157835SDaniel Fojt 		}
1516*a1157835SDaniel Fojt 		wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", sta->fils_dh_ss);
1517*a1157835SDaniel Fojt 		pos += elem_len;
1518*a1157835SDaniel Fojt 	} else {
1519*a1157835SDaniel Fojt 		crypto_ecdh_deinit(sta->fils_ecdh);
1520*a1157835SDaniel Fojt 		sta->fils_ecdh = NULL;
1521*a1157835SDaniel Fojt 		wpabuf_clear_free(sta->fils_dh_ss);
1522*a1157835SDaniel Fojt 		sta->fils_dh_ss = NULL;
1523*a1157835SDaniel Fojt 	}
1524*a1157835SDaniel Fojt #endif /* CONFIG_FILS_SK_PFS */
1525*a1157835SDaniel Fojt 
1526*a1157835SDaniel Fojt 	wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
1527*a1157835SDaniel Fojt 	if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
1528*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
1529*a1157835SDaniel Fojt 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1530*a1157835SDaniel Fojt 		goto fail;
1531*a1157835SDaniel Fojt 	}
1532*a1157835SDaniel Fojt 
1533*a1157835SDaniel Fojt 	/* RSNE */
1534*a1157835SDaniel Fojt 	wpa_hexdump(MSG_DEBUG, "FILS: RSN element",
1535*a1157835SDaniel Fojt 		    elems.rsn_ie, elems.rsn_ie_len);
1536*a1157835SDaniel Fojt 	if (!elems.rsn_ie ||
1537*a1157835SDaniel Fojt 	    wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
1538*a1157835SDaniel Fojt 				 &rsn) < 0) {
1539*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "FILS: No valid RSN element");
1540*a1157835SDaniel Fojt 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1541*a1157835SDaniel Fojt 		goto fail;
1542*a1157835SDaniel Fojt 	}
1543*a1157835SDaniel Fojt 
1544*a1157835SDaniel Fojt 	if (!sta->wpa_sm)
1545*a1157835SDaniel Fojt 		sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr,
1546*a1157835SDaniel Fojt 						NULL);
1547*a1157835SDaniel Fojt 	if (!sta->wpa_sm) {
1548*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1549*a1157835SDaniel Fojt 			   "FILS: Failed to initialize RSN state machine");
1550*a1157835SDaniel Fojt 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1551*a1157835SDaniel Fojt 		goto fail;
1552*a1157835SDaniel Fojt 	}
1553*a1157835SDaniel Fojt 
1554*a1157835SDaniel Fojt 	res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1555*a1157835SDaniel Fojt 				  hapd->iface->freq,
1556*a1157835SDaniel Fojt 				  elems.rsn_ie - 2, elems.rsn_ie_len + 2,
1557*a1157835SDaniel Fojt 				  elems.mdie, elems.mdie_len, NULL, 0);
1558*a1157835SDaniel Fojt 	resp = wpa_res_to_status_code(res);
1559*a1157835SDaniel Fojt 	if (resp != WLAN_STATUS_SUCCESS)
1560*a1157835SDaniel Fojt 		goto fail;
1561*a1157835SDaniel Fojt 
1562*a1157835SDaniel Fojt 	if (!elems.fils_nonce) {
1563*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
1564*a1157835SDaniel Fojt 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1565*a1157835SDaniel Fojt 		goto fail;
1566*a1157835SDaniel Fojt 	}
1567*a1157835SDaniel Fojt 	wpa_hexdump(MSG_DEBUG, "FILS: SNonce", elems.fils_nonce,
1568*a1157835SDaniel Fojt 		    FILS_NONCE_LEN);
1569*a1157835SDaniel Fojt 	os_memcpy(sta->fils_snonce, elems.fils_nonce, FILS_NONCE_LEN);
1570*a1157835SDaniel Fojt 
1571*a1157835SDaniel Fojt 	/* PMKID List */
1572*a1157835SDaniel Fojt 	if (rsn.pmkid && rsn.num_pmkid > 0) {
1573*a1157835SDaniel Fojt 		u8 num;
1574*a1157835SDaniel Fojt 		const u8 *pmkid;
1575*a1157835SDaniel Fojt 
1576*a1157835SDaniel Fojt 		wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
1577*a1157835SDaniel Fojt 			    rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
1578*a1157835SDaniel Fojt 
1579*a1157835SDaniel Fojt 		pmkid = rsn.pmkid;
1580*a1157835SDaniel Fojt 		num = rsn.num_pmkid;
1581*a1157835SDaniel Fojt 		while (num) {
1582*a1157835SDaniel Fojt 			wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN);
1583*a1157835SDaniel Fojt 			pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr,
1584*a1157835SDaniel Fojt 						   pmkid);
1585*a1157835SDaniel Fojt 			if (pmksa)
1586*a1157835SDaniel Fojt 				break;
1587*a1157835SDaniel Fojt 			pmksa = wpa_auth_pmksa_get_fils_cache_id(hapd->wpa_auth,
1588*a1157835SDaniel Fojt 								 sta->addr,
1589*a1157835SDaniel Fojt 								 pmkid);
1590*a1157835SDaniel Fojt 			if (pmksa)
1591*a1157835SDaniel Fojt 				break;
1592*a1157835SDaniel Fojt 			pmkid += PMKID_LEN;
1593*a1157835SDaniel Fojt 			num--;
1594*a1157835SDaniel Fojt 		}
1595*a1157835SDaniel Fojt 	}
1596*a1157835SDaniel Fojt 	if (pmksa && wpa_auth_sta_key_mgmt(sta->wpa_sm) != pmksa->akmp) {
1597*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1598*a1157835SDaniel Fojt 			   "FILS: Matching PMKSA cache entry has different AKMP (0x%x != 0x%x) - ignore",
1599*a1157835SDaniel Fojt 			   wpa_auth_sta_key_mgmt(sta->wpa_sm), pmksa->akmp);
1600*a1157835SDaniel Fojt 		pmksa = NULL;
1601*a1157835SDaniel Fojt 	}
1602*a1157835SDaniel Fojt 	if (pmksa)
1603*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "FILS: Found matching PMKSA cache entry");
1604*a1157835SDaniel Fojt 
1605*a1157835SDaniel Fojt 	/* FILS Session */
1606*a1157835SDaniel Fojt 	if (!elems.fils_session) {
1607*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
1608*a1157835SDaniel Fojt 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1609*a1157835SDaniel Fojt 		goto fail;
1610*a1157835SDaniel Fojt 	}
1611*a1157835SDaniel Fojt 	wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
1612*a1157835SDaniel Fojt 		    FILS_SESSION_LEN);
1613*a1157835SDaniel Fojt 	os_memcpy(sta->fils_session, elems.fils_session, FILS_SESSION_LEN);
1614*a1157835SDaniel Fojt 
1615*a1157835SDaniel Fojt 	/* FILS Wrapped Data */
1616*a1157835SDaniel Fojt 	if (elems.fils_wrapped_data) {
1617*a1157835SDaniel Fojt 		wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
1618*a1157835SDaniel Fojt 			    elems.fils_wrapped_data,
1619*a1157835SDaniel Fojt 			    elems.fils_wrapped_data_len);
1620*a1157835SDaniel Fojt 		if (!pmksa) {
1621*a1157835SDaniel Fojt #ifndef CONFIG_NO_RADIUS
1622*a1157835SDaniel Fojt 			if (!sta->eapol_sm) {
1623*a1157835SDaniel Fojt 				sta->eapol_sm =
1624*a1157835SDaniel Fojt 					ieee802_1x_alloc_eapol_sm(hapd, sta);
1625*a1157835SDaniel Fojt 			}
1626*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1627*a1157835SDaniel Fojt 				   "FILS: Forward EAP-Initiate/Re-auth to authentication server");
1628*a1157835SDaniel Fojt 			ieee802_1x_encapsulate_radius(
1629*a1157835SDaniel Fojt 				hapd, sta, elems.fils_wrapped_data,
1630*a1157835SDaniel Fojt 				elems.fils_wrapped_data_len);
1631*a1157835SDaniel Fojt 			sta->fils_pending_cb = cb;
1632*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1633*a1157835SDaniel Fojt 				   "FILS: Will send Authentication frame once the response from authentication server is available");
1634*a1157835SDaniel Fojt 			sta->flags |= WLAN_STA_PENDING_FILS_ERP;
1635*a1157835SDaniel Fojt 			/* Calculate pending PMKID here so that we do not need
1636*a1157835SDaniel Fojt 			 * to maintain a copy of the EAP-Initiate/Reauth
1637*a1157835SDaniel Fojt 			 * message. */
1638*a1157835SDaniel Fojt 			if (fils_pmkid_erp(wpa_auth_sta_key_mgmt(sta->wpa_sm),
1639*a1157835SDaniel Fojt 					   elems.fils_wrapped_data,
1640*a1157835SDaniel Fojt 					   elems.fils_wrapped_data_len,
1641*a1157835SDaniel Fojt 					   sta->fils_erp_pmkid) == 0)
1642*a1157835SDaniel Fojt 				sta->fils_erp_pmkid_set = 1;
1643*a1157835SDaniel Fojt 			return;
1644*a1157835SDaniel Fojt #else /* CONFIG_NO_RADIUS */
1645*a1157835SDaniel Fojt 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1646*a1157835SDaniel Fojt 			goto fail;
1647*a1157835SDaniel Fojt #endif /* CONFIG_NO_RADIUS */
1648*a1157835SDaniel Fojt 		}
1649*a1157835SDaniel Fojt 	}
1650*a1157835SDaniel Fojt 
1651*a1157835SDaniel Fojt fail:
1652*a1157835SDaniel Fojt 	if (cb) {
1653*a1157835SDaniel Fojt 		struct wpabuf *data;
1654*a1157835SDaniel Fojt 		int pub = 0;
1655*a1157835SDaniel Fojt 
1656*a1157835SDaniel Fojt 		data = prepare_auth_resp_fils(hapd, sta, &resp, pmksa, NULL,
1657*a1157835SDaniel Fojt 					      NULL, 0, &pub);
1658*a1157835SDaniel Fojt 		if (!data) {
1659*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1660*a1157835SDaniel Fojt 				   "%s: prepare_auth_resp_fils() returned failure",
1661*a1157835SDaniel Fojt 				   __func__);
1662*a1157835SDaniel Fojt 		}
1663*a1157835SDaniel Fojt 
1664*a1157835SDaniel Fojt 		cb(hapd, sta, resp, data, pub);
1665*a1157835SDaniel Fojt 	}
1666*a1157835SDaniel Fojt }
1667*a1157835SDaniel Fojt 
1668*a1157835SDaniel Fojt 
1669*a1157835SDaniel Fojt 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)1670*a1157835SDaniel Fojt prepare_auth_resp_fils(struct hostapd_data *hapd,
1671*a1157835SDaniel Fojt 		       struct sta_info *sta, u16 *resp,
1672*a1157835SDaniel Fojt 		       struct rsn_pmksa_cache_entry *pmksa,
1673*a1157835SDaniel Fojt 		       struct wpabuf *erp_resp,
1674*a1157835SDaniel Fojt 		       const u8 *msk, size_t msk_len,
1675*a1157835SDaniel Fojt 		       int *is_pub)
1676*a1157835SDaniel Fojt {
1677*a1157835SDaniel Fojt 	u8 fils_nonce[FILS_NONCE_LEN];
1678*a1157835SDaniel Fojt 	size_t ielen;
1679*a1157835SDaniel Fojt 	struct wpabuf *data = NULL;
1680*a1157835SDaniel Fojt 	const u8 *ie;
1681*a1157835SDaniel Fojt 	u8 *ie_buf = NULL;
1682*a1157835SDaniel Fojt 	const u8 *pmk = NULL;
1683*a1157835SDaniel Fojt 	size_t pmk_len = 0;
1684*a1157835SDaniel Fojt 	u8 pmk_buf[PMK_LEN_MAX];
1685*a1157835SDaniel Fojt 	struct wpabuf *pub = NULL;
1686*a1157835SDaniel Fojt 
1687*a1157835SDaniel Fojt 	if (*resp != WLAN_STATUS_SUCCESS)
1688*a1157835SDaniel Fojt 		goto fail;
1689*a1157835SDaniel Fojt 
1690*a1157835SDaniel Fojt 	ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
1691*a1157835SDaniel Fojt 	if (!ie) {
1692*a1157835SDaniel Fojt 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1693*a1157835SDaniel Fojt 		goto fail;
1694*a1157835SDaniel Fojt 	}
1695*a1157835SDaniel Fojt 
1696*a1157835SDaniel Fojt 	if (pmksa) {
1697*a1157835SDaniel Fojt 		/* Add PMKID of the selected PMKSA into RSNE */
1698*a1157835SDaniel Fojt 		ie_buf = os_malloc(ielen + 2 + 2 + PMKID_LEN);
1699*a1157835SDaniel Fojt 		if (!ie_buf) {
1700*a1157835SDaniel Fojt 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1701*a1157835SDaniel Fojt 			goto fail;
1702*a1157835SDaniel Fojt 		}
1703*a1157835SDaniel Fojt 
1704*a1157835SDaniel Fojt 		os_memcpy(ie_buf, ie, ielen);
1705*a1157835SDaniel Fojt 		if (wpa_insert_pmkid(ie_buf, &ielen, pmksa->pmkid) < 0) {
1706*a1157835SDaniel Fojt 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1707*a1157835SDaniel Fojt 			goto fail;
1708*a1157835SDaniel Fojt 		}
1709*a1157835SDaniel Fojt 		ie = ie_buf;
1710*a1157835SDaniel Fojt 	}
1711*a1157835SDaniel Fojt 
1712*a1157835SDaniel Fojt 	if (random_get_bytes(fils_nonce, FILS_NONCE_LEN) < 0) {
1713*a1157835SDaniel Fojt 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1714*a1157835SDaniel Fojt 		goto fail;
1715*a1157835SDaniel Fojt 	}
1716*a1157835SDaniel Fojt 	wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS Nonce",
1717*a1157835SDaniel Fojt 		    fils_nonce, FILS_NONCE_LEN);
1718*a1157835SDaniel Fojt 
1719*a1157835SDaniel Fojt #ifdef CONFIG_FILS_SK_PFS
1720*a1157835SDaniel Fojt 	if (sta->fils_dh_ss && sta->fils_ecdh) {
1721*a1157835SDaniel Fojt 		pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
1722*a1157835SDaniel Fojt 		if (!pub) {
1723*a1157835SDaniel Fojt 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1724*a1157835SDaniel Fojt 			goto fail;
1725*a1157835SDaniel Fojt 		}
1726*a1157835SDaniel Fojt 	}
1727*a1157835SDaniel Fojt #endif /* CONFIG_FILS_SK_PFS */
1728*a1157835SDaniel Fojt 
1729*a1157835SDaniel Fojt 	data = wpabuf_alloc(1000 + ielen + (pub ? wpabuf_len(pub) : 0));
1730*a1157835SDaniel Fojt 	if (!data) {
1731*a1157835SDaniel Fojt 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1732*a1157835SDaniel Fojt 		goto fail;
1733*a1157835SDaniel Fojt 	}
1734*a1157835SDaniel Fojt 
1735*a1157835SDaniel Fojt 	/* TODO: FILS PK */
1736*a1157835SDaniel Fojt #ifdef CONFIG_FILS_SK_PFS
1737*a1157835SDaniel Fojt 	if (pub) {
1738*a1157835SDaniel Fojt 		/* Finite Cyclic Group */
1739*a1157835SDaniel Fojt 		wpabuf_put_le16(data, hapd->conf->fils_dh_group);
1740*a1157835SDaniel Fojt 
1741*a1157835SDaniel Fojt 		/* Element */
1742*a1157835SDaniel Fojt 		wpabuf_put_buf(data, pub);
1743*a1157835SDaniel Fojt 	}
1744*a1157835SDaniel Fojt #endif /* CONFIG_FILS_SK_PFS */
1745*a1157835SDaniel Fojt 
1746*a1157835SDaniel Fojt 	/* RSNE */
1747*a1157835SDaniel Fojt 	wpabuf_put_data(data, ie, ielen);
1748*a1157835SDaniel Fojt 
1749*a1157835SDaniel Fojt 	/* MDE when using FILS+FT (already included in ie,ielen with RSNE) */
1750*a1157835SDaniel Fojt 
1751*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211R_AP
1752*a1157835SDaniel Fojt 	if (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm))) {
1753*a1157835SDaniel Fojt 		/* FTE[R1KH-ID,R0KH-ID] when using FILS+FT */
1754*a1157835SDaniel Fojt 		int res;
1755*a1157835SDaniel Fojt 		int use_sha384 = wpa_key_mgmt_sha384(
1756*a1157835SDaniel Fojt 			wpa_auth_sta_key_mgmt(sta->wpa_sm));
1757*a1157835SDaniel Fojt 
1758*a1157835SDaniel Fojt 		res = wpa_auth_write_fte(hapd->wpa_auth, use_sha384,
1759*a1157835SDaniel Fojt 					 wpabuf_put(data, 0),
1760*a1157835SDaniel Fojt 					 wpabuf_tailroom(data));
1761*a1157835SDaniel Fojt 		if (res < 0) {
1762*a1157835SDaniel Fojt 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1763*a1157835SDaniel Fojt 			goto fail;
1764*a1157835SDaniel Fojt 		}
1765*a1157835SDaniel Fojt 		wpabuf_put(data, res);
1766*a1157835SDaniel Fojt 	}
1767*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211R_AP */
1768*a1157835SDaniel Fojt 
1769*a1157835SDaniel Fojt 	/* FILS Nonce */
1770*a1157835SDaniel Fojt 	wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
1771*a1157835SDaniel Fojt 	wpabuf_put_u8(data, 1 + FILS_NONCE_LEN); /* Length */
1772*a1157835SDaniel Fojt 	/* Element ID Extension */
1773*a1157835SDaniel Fojt 	wpabuf_put_u8(data, WLAN_EID_EXT_FILS_NONCE);
1774*a1157835SDaniel Fojt 	wpabuf_put_data(data, fils_nonce, FILS_NONCE_LEN);
1775*a1157835SDaniel Fojt 
1776*a1157835SDaniel Fojt 	/* FILS Session */
1777*a1157835SDaniel Fojt 	wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
1778*a1157835SDaniel Fojt 	wpabuf_put_u8(data, 1 + FILS_SESSION_LEN); /* Length */
1779*a1157835SDaniel Fojt 	/* Element ID Extension */
1780*a1157835SDaniel Fojt 	wpabuf_put_u8(data, WLAN_EID_EXT_FILS_SESSION);
1781*a1157835SDaniel Fojt 	wpabuf_put_data(data, sta->fils_session, FILS_SESSION_LEN);
1782*a1157835SDaniel Fojt 
1783*a1157835SDaniel Fojt 	/* FILS Wrapped Data */
1784*a1157835SDaniel Fojt 	if (!pmksa && erp_resp) {
1785*a1157835SDaniel Fojt 		wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
1786*a1157835SDaniel Fojt 		wpabuf_put_u8(data, 1 + wpabuf_len(erp_resp)); /* Length */
1787*a1157835SDaniel Fojt 		/* Element ID Extension */
1788*a1157835SDaniel Fojt 		wpabuf_put_u8(data, WLAN_EID_EXT_FILS_WRAPPED_DATA);
1789*a1157835SDaniel Fojt 		wpabuf_put_buf(data, erp_resp);
1790*a1157835SDaniel Fojt 
1791*a1157835SDaniel Fojt 		if (fils_rmsk_to_pmk(wpa_auth_sta_key_mgmt(sta->wpa_sm),
1792*a1157835SDaniel Fojt 				     msk, msk_len, sta->fils_snonce, fils_nonce,
1793*a1157835SDaniel Fojt 				     sta->fils_dh_ss ?
1794*a1157835SDaniel Fojt 				     wpabuf_head(sta->fils_dh_ss) : NULL,
1795*a1157835SDaniel Fojt 				     sta->fils_dh_ss ?
1796*a1157835SDaniel Fojt 				     wpabuf_len(sta->fils_dh_ss) : 0,
1797*a1157835SDaniel Fojt 				     pmk_buf, &pmk_len)) {
1798*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "FILS: Failed to derive PMK");
1799*a1157835SDaniel Fojt 			*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1800*a1157835SDaniel Fojt 			wpabuf_free(data);
1801*a1157835SDaniel Fojt 			data = NULL;
1802*a1157835SDaniel Fojt 			goto fail;
1803*a1157835SDaniel Fojt 		}
1804*a1157835SDaniel Fojt 		pmk = pmk_buf;
1805*a1157835SDaniel Fojt 
1806*a1157835SDaniel Fojt 		/* Don't use DHss in PTK derivation if PMKSA caching is not
1807*a1157835SDaniel Fojt 		 * used. */
1808*a1157835SDaniel Fojt 		wpabuf_clear_free(sta->fils_dh_ss);
1809*a1157835SDaniel Fojt 		sta->fils_dh_ss = NULL;
1810*a1157835SDaniel Fojt 
1811*a1157835SDaniel Fojt 		if (sta->fils_erp_pmkid_set) {
1812*a1157835SDaniel Fojt 			/* TODO: get PMKLifetime from WPA parameters */
1813*a1157835SDaniel Fojt 			unsigned int dot11RSNAConfigPMKLifetime = 43200;
1814*a1157835SDaniel Fojt 			int session_timeout;
1815*a1157835SDaniel Fojt 
1816*a1157835SDaniel Fojt 			session_timeout = dot11RSNAConfigPMKLifetime;
1817*a1157835SDaniel Fojt 			if (sta->session_timeout_set) {
1818*a1157835SDaniel Fojt 				struct os_reltime now, diff;
1819*a1157835SDaniel Fojt 
1820*a1157835SDaniel Fojt 				os_get_reltime(&now);
1821*a1157835SDaniel Fojt 				os_reltime_sub(&sta->session_timeout, &now,
1822*a1157835SDaniel Fojt 					       &diff);
1823*a1157835SDaniel Fojt 				session_timeout = diff.sec;
1824*a1157835SDaniel Fojt 			}
1825*a1157835SDaniel Fojt 
1826*a1157835SDaniel Fojt 			sta->fils_erp_pmkid_set = 0;
1827*a1157835SDaniel Fojt 			wpa_auth_add_fils_pmk_pmkid(sta->wpa_sm, pmk, pmk_len,
1828*a1157835SDaniel Fojt 						    sta->fils_erp_pmkid);
1829*a1157835SDaniel Fojt 			if (!hapd->conf->disable_pmksa_caching &&
1830*a1157835SDaniel Fojt 			    wpa_auth_pmksa_add2(
1831*a1157835SDaniel Fojt 				    hapd->wpa_auth, sta->addr,
1832*a1157835SDaniel Fojt 				    pmk, pmk_len,
1833*a1157835SDaniel Fojt 				    sta->fils_erp_pmkid,
1834*a1157835SDaniel Fojt 				    session_timeout,
1835*a1157835SDaniel Fojt 				    wpa_auth_sta_key_mgmt(sta->wpa_sm)) < 0) {
1836*a1157835SDaniel Fojt 				wpa_printf(MSG_ERROR,
1837*a1157835SDaniel Fojt 					   "FILS: Failed to add PMKSA cache entry based on ERP");
1838*a1157835SDaniel Fojt 			}
1839*a1157835SDaniel Fojt 		}
1840*a1157835SDaniel Fojt 	} else if (pmksa) {
1841*a1157835SDaniel Fojt 		pmk = pmksa->pmk;
1842*a1157835SDaniel Fojt 		pmk_len = pmksa->pmk_len;
1843*a1157835SDaniel Fojt 	}
1844*a1157835SDaniel Fojt 
1845*a1157835SDaniel Fojt 	if (!pmk) {
1846*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "FILS: No PMK available");
1847*a1157835SDaniel Fojt 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1848*a1157835SDaniel Fojt 		wpabuf_free(data);
1849*a1157835SDaniel Fojt 		data = NULL;
1850*a1157835SDaniel Fojt 		goto fail;
1851*a1157835SDaniel Fojt 	}
1852*a1157835SDaniel Fojt 
1853*a1157835SDaniel Fojt 	if (fils_auth_pmk_to_ptk(sta->wpa_sm, pmk, pmk_len,
1854*a1157835SDaniel Fojt 				 sta->fils_snonce, fils_nonce,
1855*a1157835SDaniel Fojt 				 sta->fils_dh_ss ?
1856*a1157835SDaniel Fojt 				 wpabuf_head(sta->fils_dh_ss) : NULL,
1857*a1157835SDaniel Fojt 				 sta->fils_dh_ss ?
1858*a1157835SDaniel Fojt 				 wpabuf_len(sta->fils_dh_ss) : 0,
1859*a1157835SDaniel Fojt 				 sta->fils_g_sta, pub) < 0) {
1860*a1157835SDaniel Fojt 		*resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1861*a1157835SDaniel Fojt 		wpabuf_free(data);
1862*a1157835SDaniel Fojt 		data = NULL;
1863*a1157835SDaniel Fojt 		goto fail;
1864*a1157835SDaniel Fojt 	}
1865*a1157835SDaniel Fojt 
1866*a1157835SDaniel Fojt fail:
1867*a1157835SDaniel Fojt 	if (is_pub)
1868*a1157835SDaniel Fojt 		*is_pub = pub != NULL;
1869*a1157835SDaniel Fojt 	os_free(ie_buf);
1870*a1157835SDaniel Fojt 	wpabuf_free(pub);
1871*a1157835SDaniel Fojt 	wpabuf_clear_free(sta->fils_dh_ss);
1872*a1157835SDaniel Fojt 	sta->fils_dh_ss = NULL;
1873*a1157835SDaniel Fojt #ifdef CONFIG_FILS_SK_PFS
1874*a1157835SDaniel Fojt 	crypto_ecdh_deinit(sta->fils_ecdh);
1875*a1157835SDaniel Fojt 	sta->fils_ecdh = NULL;
1876*a1157835SDaniel Fojt #endif /* CONFIG_FILS_SK_PFS */
1877*a1157835SDaniel Fojt 	return data;
1878*a1157835SDaniel Fojt }
1879*a1157835SDaniel Fojt 
1880*a1157835SDaniel Fojt 
handle_auth_fils_finish(struct hostapd_data * hapd,struct sta_info * sta,u16 resp,struct wpabuf * data,int pub)1881*a1157835SDaniel Fojt static void handle_auth_fils_finish(struct hostapd_data *hapd,
1882*a1157835SDaniel Fojt 				    struct sta_info *sta, u16 resp,
1883*a1157835SDaniel Fojt 				    struct wpabuf *data, int pub)
1884*a1157835SDaniel Fojt {
1885*a1157835SDaniel Fojt 	u16 auth_alg;
1886*a1157835SDaniel Fojt 
1887*a1157835SDaniel Fojt 	auth_alg = (pub ||
1888*a1157835SDaniel Fojt 		    resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) ?
1889*a1157835SDaniel Fojt 		WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
1890*a1157835SDaniel Fojt 	send_auth_reply(hapd, sta->addr, hapd->own_addr, auth_alg, 2, resp,
1891*a1157835SDaniel Fojt 			data ? wpabuf_head(data) : (u8 *) "",
1892*a1157835SDaniel Fojt 			data ? wpabuf_len(data) : 0, "auth-fils-finish");
1893*a1157835SDaniel Fojt 	wpabuf_free(data);
1894*a1157835SDaniel Fojt 
1895*a1157835SDaniel Fojt 	if (resp == WLAN_STATUS_SUCCESS) {
1896*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1897*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_DEBUG,
1898*a1157835SDaniel Fojt 			       "authentication OK (FILS)");
1899*a1157835SDaniel Fojt 		sta->flags |= WLAN_STA_AUTH;
1900*a1157835SDaniel Fojt 		wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1901*a1157835SDaniel Fojt 		sta->auth_alg = pub ? WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
1902*a1157835SDaniel Fojt 		mlme_authenticate_indication(hapd, sta);
1903*a1157835SDaniel Fojt 	}
1904*a1157835SDaniel Fojt }
1905*a1157835SDaniel Fojt 
1906*a1157835SDaniel Fojt 
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)1907*a1157835SDaniel Fojt void ieee802_11_finish_fils_auth(struct hostapd_data *hapd,
1908*a1157835SDaniel Fojt 				 struct sta_info *sta, int success,
1909*a1157835SDaniel Fojt 				 struct wpabuf *erp_resp,
1910*a1157835SDaniel Fojt 				 const u8 *msk, size_t msk_len)
1911*a1157835SDaniel Fojt {
1912*a1157835SDaniel Fojt 	struct wpabuf *data;
1913*a1157835SDaniel Fojt 	int pub = 0;
1914*a1157835SDaniel Fojt 	u16 resp;
1915*a1157835SDaniel Fojt 
1916*a1157835SDaniel Fojt 	sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
1917*a1157835SDaniel Fojt 
1918*a1157835SDaniel Fojt 	if (!sta->fils_pending_cb)
1919*a1157835SDaniel Fojt 		return;
1920*a1157835SDaniel Fojt 	resp = success ? WLAN_STATUS_SUCCESS : WLAN_STATUS_UNSPECIFIED_FAILURE;
1921*a1157835SDaniel Fojt 	data = prepare_auth_resp_fils(hapd, sta, &resp, NULL, erp_resp,
1922*a1157835SDaniel Fojt 				      msk, msk_len, &pub);
1923*a1157835SDaniel Fojt 	if (!data) {
1924*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1925*a1157835SDaniel Fojt 			   "%s: prepare_auth_resp_fils() returned failure",
1926*a1157835SDaniel Fojt 			   __func__);
1927*a1157835SDaniel Fojt 	}
1928*a1157835SDaniel Fojt 	sta->fils_pending_cb(hapd, sta, resp, data, pub);
1929*a1157835SDaniel Fojt }
1930*a1157835SDaniel Fojt 
1931*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
1932*a1157835SDaniel Fojt 
1933*a1157835SDaniel Fojt 
1934*a1157835SDaniel Fojt int
ieee802_11_allowed_address(struct hostapd_data * hapd,const u8 * addr,const u8 * msg,size_t len,u32 * session_timeout,u32 * acct_interim_interval,struct vlan_description * vlan_id,struct hostapd_sta_wpa_psk_short ** psk,char ** identity,char ** radius_cui,int is_probe_req)1935*a1157835SDaniel Fojt ieee802_11_allowed_address(struct hostapd_data *hapd, const u8 *addr,
1936*a1157835SDaniel Fojt 			   const u8 *msg, size_t len, u32 *session_timeout,
1937*a1157835SDaniel Fojt 			   u32 *acct_interim_interval,
1938*a1157835SDaniel Fojt 			   struct vlan_description *vlan_id,
1939*a1157835SDaniel Fojt 			   struct hostapd_sta_wpa_psk_short **psk,
1940*a1157835SDaniel Fojt 			   char **identity, char **radius_cui, int is_probe_req)
1941*a1157835SDaniel Fojt {
1942*a1157835SDaniel Fojt 	int res;
1943*a1157835SDaniel Fojt 
1944*a1157835SDaniel Fojt 	os_memset(vlan_id, 0, sizeof(*vlan_id));
1945*a1157835SDaniel Fojt 	res = hostapd_allowed_address(hapd, addr, msg, len,
1946*a1157835SDaniel Fojt 				      session_timeout, acct_interim_interval,
1947*a1157835SDaniel Fojt 				      vlan_id, psk, identity, radius_cui,
1948*a1157835SDaniel Fojt 				      is_probe_req);
1949*a1157835SDaniel Fojt 
1950*a1157835SDaniel Fojt 	if (res == HOSTAPD_ACL_REJECT) {
1951*a1157835SDaniel Fojt 		if (!is_probe_req)
1952*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1953*a1157835SDaniel Fojt 				   "Station " MACSTR
1954*a1157835SDaniel Fojt 				   " not allowed to authenticate",
1955*a1157835SDaniel Fojt 				   MAC2STR(addr));
1956*a1157835SDaniel Fojt 		return HOSTAPD_ACL_REJECT;
1957*a1157835SDaniel Fojt 	}
1958*a1157835SDaniel Fojt 
1959*a1157835SDaniel Fojt 	if (res == HOSTAPD_ACL_PENDING) {
1960*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
1961*a1157835SDaniel Fojt 			   " waiting for an external authentication",
1962*a1157835SDaniel Fojt 			   MAC2STR(addr));
1963*a1157835SDaniel Fojt 		/* Authentication code will re-send the authentication frame
1964*a1157835SDaniel Fojt 		 * after it has received (and cached) information from the
1965*a1157835SDaniel Fojt 		 * external source. */
1966*a1157835SDaniel Fojt 		return HOSTAPD_ACL_PENDING;
1967*a1157835SDaniel Fojt 	}
1968*a1157835SDaniel Fojt 
1969*a1157835SDaniel Fojt 	return res;
1970*a1157835SDaniel Fojt }
1971*a1157835SDaniel Fojt 
1972*a1157835SDaniel Fojt 
1973*a1157835SDaniel Fojt static int
ieee802_11_set_radius_info(struct hostapd_data * hapd,struct sta_info * sta,int res,u32 session_timeout,u32 acct_interim_interval,struct vlan_description * vlan_id,struct hostapd_sta_wpa_psk_short ** psk,char ** identity,char ** radius_cui)1974*a1157835SDaniel Fojt ieee802_11_set_radius_info(struct hostapd_data *hapd, struct sta_info *sta,
1975*a1157835SDaniel Fojt 			   int res, u32 session_timeout,
1976*a1157835SDaniel Fojt 			   u32 acct_interim_interval,
1977*a1157835SDaniel Fojt 			   struct vlan_description *vlan_id,
1978*a1157835SDaniel Fojt 			   struct hostapd_sta_wpa_psk_short **psk,
1979*a1157835SDaniel Fojt 			   char **identity, char **radius_cui)
1980*a1157835SDaniel Fojt {
1981*a1157835SDaniel Fojt 	if (vlan_id->notempty &&
1982*a1157835SDaniel Fojt 	    !hostapd_vlan_valid(hapd->conf->vlan, vlan_id)) {
1983*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1984*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_INFO,
1985*a1157835SDaniel Fojt 			       "Invalid VLAN %d%s received from RADIUS server",
1986*a1157835SDaniel Fojt 			       vlan_id->untagged,
1987*a1157835SDaniel Fojt 			       vlan_id->tagged[0] ? "+" : "");
1988*a1157835SDaniel Fojt 		return -1;
1989*a1157835SDaniel Fojt 	}
1990*a1157835SDaniel Fojt 	if (ap_sta_set_vlan(hapd, sta, vlan_id) < 0)
1991*a1157835SDaniel Fojt 		return -1;
1992*a1157835SDaniel Fojt 	if (sta->vlan_id)
1993*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1994*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
1995*a1157835SDaniel Fojt 
1996*a1157835SDaniel Fojt 	hostapd_free_psk_list(sta->psk);
1997*a1157835SDaniel Fojt 	if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
1998*a1157835SDaniel Fojt 		sta->psk = *psk;
1999*a1157835SDaniel Fojt 		*psk = NULL;
2000*a1157835SDaniel Fojt 	} else {
2001*a1157835SDaniel Fojt 		sta->psk = NULL;
2002*a1157835SDaniel Fojt 	}
2003*a1157835SDaniel Fojt 
2004*a1157835SDaniel Fojt 	os_free(sta->identity);
2005*a1157835SDaniel Fojt 	sta->identity = *identity;
2006*a1157835SDaniel Fojt 	*identity = NULL;
2007*a1157835SDaniel Fojt 
2008*a1157835SDaniel Fojt 	os_free(sta->radius_cui);
2009*a1157835SDaniel Fojt 	sta->radius_cui = *radius_cui;
2010*a1157835SDaniel Fojt 	*radius_cui = NULL;
2011*a1157835SDaniel Fojt 
2012*a1157835SDaniel Fojt 	if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
2013*a1157835SDaniel Fojt 		sta->acct_interim_interval = acct_interim_interval;
2014*a1157835SDaniel Fojt 	if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT) {
2015*a1157835SDaniel Fojt 		sta->session_timeout_set = 1;
2016*a1157835SDaniel Fojt 		os_get_reltime(&sta->session_timeout);
2017*a1157835SDaniel Fojt 		sta->session_timeout.sec += session_timeout;
2018*a1157835SDaniel Fojt 		ap_sta_session_timeout(hapd, sta, session_timeout);
2019*a1157835SDaniel Fojt 	} else {
2020*a1157835SDaniel Fojt 		sta->session_timeout_set = 0;
2021*a1157835SDaniel Fojt 		ap_sta_no_session_timeout(hapd, sta);
2022*a1157835SDaniel Fojt 	}
2023*a1157835SDaniel Fojt 
2024*a1157835SDaniel Fojt 	return 0;
2025*a1157835SDaniel Fojt }
2026*a1157835SDaniel Fojt 
2027*a1157835SDaniel Fojt 
handle_auth(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int rssi,int from_queue)20283ff40c12SJohn Marino static void handle_auth(struct hostapd_data *hapd,
2029*a1157835SDaniel Fojt 			const struct ieee80211_mgmt *mgmt, size_t len,
2030*a1157835SDaniel Fojt 			int rssi, int from_queue)
20313ff40c12SJohn Marino {
20323ff40c12SJohn Marino 	u16 auth_alg, auth_transaction, status_code;
20333ff40c12SJohn Marino 	u16 resp = WLAN_STATUS_SUCCESS;
20343ff40c12SJohn Marino 	struct sta_info *sta = NULL;
2035*a1157835SDaniel Fojt 	int res, reply_res;
20363ff40c12SJohn Marino 	u16 fc;
20373ff40c12SJohn Marino 	const u8 *challenge = NULL;
20383ff40c12SJohn Marino 	u32 session_timeout, acct_interim_interval;
2039*a1157835SDaniel Fojt 	struct vlan_description vlan_id;
20403ff40c12SJohn Marino 	struct hostapd_sta_wpa_psk_short *psk = NULL;
20413ff40c12SJohn Marino 	u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
20423ff40c12SJohn Marino 	size_t resp_ies_len = 0;
20433ff40c12SJohn Marino 	char *identity = NULL;
20443ff40c12SJohn Marino 	char *radius_cui = NULL;
2045*a1157835SDaniel Fojt 	u16 seq_ctrl;
20463ff40c12SJohn Marino 
20473ff40c12SJohn Marino 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
20483ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
20493ff40c12SJohn Marino 			   (unsigned long) len);
20503ff40c12SJohn Marino 		return;
20513ff40c12SJohn Marino 	}
20523ff40c12SJohn Marino 
20533ff40c12SJohn Marino #ifdef CONFIG_TESTING_OPTIONS
2054*a1157835SDaniel Fojt 	if (hapd->iconf->ignore_auth_probability > 0.0 &&
20553ff40c12SJohn Marino 	    drand48() < hapd->iconf->ignore_auth_probability) {
20563ff40c12SJohn Marino 		wpa_printf(MSG_INFO,
20573ff40c12SJohn Marino 			   "TESTING: ignoring auth frame from " MACSTR,
20583ff40c12SJohn Marino 			   MAC2STR(mgmt->sa));
20593ff40c12SJohn Marino 		return;
20603ff40c12SJohn Marino 	}
20613ff40c12SJohn Marino #endif /* CONFIG_TESTING_OPTIONS */
20623ff40c12SJohn Marino 
20633ff40c12SJohn Marino 	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
20643ff40c12SJohn Marino 	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
20653ff40c12SJohn Marino 	status_code = le_to_host16(mgmt->u.auth.status_code);
20663ff40c12SJohn Marino 	fc = le_to_host16(mgmt->frame_control);
2067*a1157835SDaniel Fojt 	seq_ctrl = le_to_host16(mgmt->seq_ctrl);
20683ff40c12SJohn Marino 
20693ff40c12SJohn Marino 	if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
20703ff40c12SJohn Marino 	    2 + WLAN_AUTH_CHALLENGE_LEN &&
20713ff40c12SJohn Marino 	    mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
20723ff40c12SJohn Marino 	    mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
20733ff40c12SJohn Marino 		challenge = &mgmt->u.auth.variable[2];
20743ff40c12SJohn Marino 
20753ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
2076*a1157835SDaniel Fojt 		   "auth_transaction=%d status_code=%d wep=%d%s "
2077*a1157835SDaniel Fojt 		   "seq_ctrl=0x%x%s%s",
20783ff40c12SJohn Marino 		   MAC2STR(mgmt->sa), auth_alg, auth_transaction,
20793ff40c12SJohn Marino 		   status_code, !!(fc & WLAN_FC_ISWEP),
2080*a1157835SDaniel Fojt 		   challenge ? " challenge" : "",
2081*a1157835SDaniel Fojt 		   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "",
2082*a1157835SDaniel Fojt 		   from_queue ? " (from queue)" : "");
2083*a1157835SDaniel Fojt 
2084*a1157835SDaniel Fojt #ifdef CONFIG_NO_RC4
2085*a1157835SDaniel Fojt 	if (auth_alg == WLAN_AUTH_SHARED_KEY) {
2086*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
2087*a1157835SDaniel Fojt 			   "Unsupported authentication algorithm (%d)",
2088*a1157835SDaniel Fojt 			   auth_alg);
2089*a1157835SDaniel Fojt 		resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
2090*a1157835SDaniel Fojt 		goto fail;
2091*a1157835SDaniel Fojt 	}
2092*a1157835SDaniel Fojt #endif /* CONFIG_NO_RC4 */
20933ff40c12SJohn Marino 
20943ff40c12SJohn Marino 	if (hapd->tkip_countermeasures) {
2095*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
2096*a1157835SDaniel Fojt 			   "Ongoing TKIP countermeasures (Michael MIC failure) - reject authentication");
2097*a1157835SDaniel Fojt 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
20983ff40c12SJohn Marino 		goto fail;
20993ff40c12SJohn Marino 	}
21003ff40c12SJohn Marino 
21013ff40c12SJohn Marino 	if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
21023ff40c12SJohn Marino 	       auth_alg == WLAN_AUTH_OPEN) ||
2103*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211R_AP
21043ff40c12SJohn Marino 	      (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
21053ff40c12SJohn Marino 	       auth_alg == WLAN_AUTH_FT) ||
2106*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211R_AP */
21073ff40c12SJohn Marino #ifdef CONFIG_SAE
21083ff40c12SJohn Marino 	      (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
21093ff40c12SJohn Marino 	       auth_alg == WLAN_AUTH_SAE) ||
21103ff40c12SJohn Marino #endif /* CONFIG_SAE */
2111*a1157835SDaniel Fojt #ifdef CONFIG_FILS
2112*a1157835SDaniel Fojt 	      (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
2113*a1157835SDaniel Fojt 	       auth_alg == WLAN_AUTH_FILS_SK) ||
2114*a1157835SDaniel Fojt 	      (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
2115*a1157835SDaniel Fojt 	       hapd->conf->fils_dh_group &&
2116*a1157835SDaniel Fojt 	       auth_alg == WLAN_AUTH_FILS_SK_PFS) ||
2117*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
21183ff40c12SJohn Marino 	      ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
21193ff40c12SJohn Marino 	       auth_alg == WLAN_AUTH_SHARED_KEY))) {
21203ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
21213ff40c12SJohn Marino 			   auth_alg);
21223ff40c12SJohn Marino 		resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
21233ff40c12SJohn Marino 		goto fail;
21243ff40c12SJohn Marino 	}
21253ff40c12SJohn Marino 
21263ff40c12SJohn Marino 	if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
21273ff40c12SJohn Marino 	      (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
21283ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
21293ff40c12SJohn Marino 			   auth_transaction);
21303ff40c12SJohn Marino 		resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
21313ff40c12SJohn Marino 		goto fail;
21323ff40c12SJohn Marino 	}
21333ff40c12SJohn Marino 
21343ff40c12SJohn Marino 	if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
21353ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
21363ff40c12SJohn Marino 			   MAC2STR(mgmt->sa));
21373ff40c12SJohn Marino 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
21383ff40c12SJohn Marino 		goto fail;
21393ff40c12SJohn Marino 	}
21403ff40c12SJohn Marino 
2141*a1157835SDaniel Fojt 	if (hapd->conf->no_auth_if_seen_on) {
2142*a1157835SDaniel Fojt 		struct hostapd_data *other;
21433ff40c12SJohn Marino 
2144*a1157835SDaniel Fojt 		other = sta_track_seen_on(hapd->iface, mgmt->sa,
2145*a1157835SDaniel Fojt 					  hapd->conf->no_auth_if_seen_on);
2146*a1157835SDaniel Fojt 		if (other) {
2147*a1157835SDaniel Fojt 			u8 *pos;
2148*a1157835SDaniel Fojt 			u32 info;
2149*a1157835SDaniel Fojt 			u8 op_class, channel, phytype;
2150*a1157835SDaniel Fojt 
2151*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
2152*a1157835SDaniel Fojt 				   MACSTR " since STA has been seen on %s",
2153*a1157835SDaniel Fojt 				   hapd->conf->iface, MAC2STR(mgmt->sa),
2154*a1157835SDaniel Fojt 				   hapd->conf->no_auth_if_seen_on);
2155*a1157835SDaniel Fojt 
2156*a1157835SDaniel Fojt 			resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
2157*a1157835SDaniel Fojt 			pos = &resp_ies[0];
2158*a1157835SDaniel Fojt 			*pos++ = WLAN_EID_NEIGHBOR_REPORT;
2159*a1157835SDaniel Fojt 			*pos++ = 13;
2160*a1157835SDaniel Fojt 			os_memcpy(pos, other->own_addr, ETH_ALEN);
2161*a1157835SDaniel Fojt 			pos += ETH_ALEN;
2162*a1157835SDaniel Fojt 			info = 0; /* TODO: BSSID Information */
2163*a1157835SDaniel Fojt 			WPA_PUT_LE32(pos, info);
2164*a1157835SDaniel Fojt 			pos += 4;
2165*a1157835SDaniel Fojt 			if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
2166*a1157835SDaniel Fojt 				phytype = 8; /* dmg */
2167*a1157835SDaniel Fojt 			else if (other->iconf->ieee80211ac)
2168*a1157835SDaniel Fojt 				phytype = 9; /* vht */
2169*a1157835SDaniel Fojt 			else if (other->iconf->ieee80211n)
2170*a1157835SDaniel Fojt 				phytype = 7; /* ht */
2171*a1157835SDaniel Fojt 			else if (other->iconf->hw_mode ==
2172*a1157835SDaniel Fojt 				 HOSTAPD_MODE_IEEE80211A)
2173*a1157835SDaniel Fojt 				phytype = 4; /* ofdm */
2174*a1157835SDaniel Fojt 			else if (other->iconf->hw_mode ==
2175*a1157835SDaniel Fojt 				 HOSTAPD_MODE_IEEE80211G)
2176*a1157835SDaniel Fojt 				phytype = 6; /* erp */
2177*a1157835SDaniel Fojt 			else
2178*a1157835SDaniel Fojt 				phytype = 5; /* hrdsss */
2179*a1157835SDaniel Fojt 			if (ieee80211_freq_to_channel_ext(
2180*a1157835SDaniel Fojt 				    hostapd_hw_get_freq(other,
2181*a1157835SDaniel Fojt 							other->iconf->channel),
2182*a1157835SDaniel Fojt 				    other->iconf->secondary_channel,
2183*a1157835SDaniel Fojt 				    other->iconf->ieee80211ac,
2184*a1157835SDaniel Fojt 				    &op_class, &channel) == NUM_HOSTAPD_MODES) {
2185*a1157835SDaniel Fojt 				op_class = 0;
2186*a1157835SDaniel Fojt 				channel = other->iconf->channel;
2187*a1157835SDaniel Fojt 			}
2188*a1157835SDaniel Fojt 			*pos++ = op_class;
2189*a1157835SDaniel Fojt 			*pos++ = channel;
2190*a1157835SDaniel Fojt 			*pos++ = phytype;
2191*a1157835SDaniel Fojt 			resp_ies_len = pos - &resp_ies[0];
2192*a1157835SDaniel Fojt 			goto fail;
2193*a1157835SDaniel Fojt 		}
2194*a1157835SDaniel Fojt 	}
2195*a1157835SDaniel Fojt 
2196*a1157835SDaniel Fojt 	res = ieee802_11_allowed_address(
2197*a1157835SDaniel Fojt 		hapd, mgmt->sa, (const u8 *) mgmt, len, &session_timeout,
2198*a1157835SDaniel Fojt 		&acct_interim_interval, &vlan_id, &psk, &identity, &radius_cui,
2199*a1157835SDaniel Fojt 		0);
22003ff40c12SJohn Marino 	if (res == HOSTAPD_ACL_REJECT) {
2201*a1157835SDaniel Fojt 		wpa_msg(hapd->msg_ctx, MSG_DEBUG,
2202*a1157835SDaniel Fojt 			"Ignore Authentication frame from " MACSTR
2203*a1157835SDaniel Fojt 			" due to ACL reject", MAC2STR(mgmt->sa));
22043ff40c12SJohn Marino 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
22053ff40c12SJohn Marino 		goto fail;
22063ff40c12SJohn Marino 	}
2207*a1157835SDaniel Fojt 	if (res == HOSTAPD_ACL_PENDING)
2208*a1157835SDaniel Fojt 		return;
2209*a1157835SDaniel Fojt 
2210*a1157835SDaniel Fojt #ifdef CONFIG_SAE
2211*a1157835SDaniel Fojt 	if (auth_alg == WLAN_AUTH_SAE && !from_queue &&
2212*a1157835SDaniel Fojt 	    (auth_transaction == 1 ||
2213*a1157835SDaniel Fojt 	     (auth_transaction == 2 && auth_sae_queued_addr(hapd, mgmt->sa)))) {
2214*a1157835SDaniel Fojt 		/* Handle SAE Authentication commit message through a queue to
2215*a1157835SDaniel Fojt 		 * provide more control for postponing the needed heavy
2216*a1157835SDaniel Fojt 		 * processing under a possible DoS attack scenario. In addition,
2217*a1157835SDaniel Fojt 		 * queue SAE Authentication confirm message if there happens to
2218*a1157835SDaniel Fojt 		 * be a queued commit message from the same peer. This is needed
2219*a1157835SDaniel Fojt 		 * to avoid reordering Authentication frames within the same
2220*a1157835SDaniel Fojt 		 * SAE exchange. */
2221*a1157835SDaniel Fojt 		auth_sae_queue(hapd, mgmt, len, rssi);
22223ff40c12SJohn Marino 		return;
22233ff40c12SJohn Marino 	}
2224*a1157835SDaniel Fojt #endif /* CONFIG_SAE */
2225*a1157835SDaniel Fojt 
2226*a1157835SDaniel Fojt 	sta = ap_get_sta(hapd, mgmt->sa);
2227*a1157835SDaniel Fojt 	if (sta) {
2228*a1157835SDaniel Fojt 		sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
2229*a1157835SDaniel Fojt 		sta->ft_over_ds = 0;
2230*a1157835SDaniel Fojt 		if ((fc & WLAN_FC_RETRY) &&
2231*a1157835SDaniel Fojt 		    sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2232*a1157835SDaniel Fojt 		    sta->last_seq_ctrl == seq_ctrl &&
2233*a1157835SDaniel Fojt 		    sta->last_subtype == WLAN_FC_STYPE_AUTH) {
2234*a1157835SDaniel Fojt 			hostapd_logger(hapd, sta->addr,
2235*a1157835SDaniel Fojt 				       HOSTAPD_MODULE_IEEE80211,
2236*a1157835SDaniel Fojt 				       HOSTAPD_LEVEL_DEBUG,
2237*a1157835SDaniel Fojt 				       "Drop repeated authentication frame seq_ctrl=0x%x",
2238*a1157835SDaniel Fojt 				       seq_ctrl);
2239*a1157835SDaniel Fojt 			return;
2240*a1157835SDaniel Fojt 		}
2241*a1157835SDaniel Fojt #ifdef CONFIG_MESH
2242*a1157835SDaniel Fojt 		if ((hapd->conf->mesh & MESH_ENABLED) &&
2243*a1157835SDaniel Fojt 		    sta->plink_state == PLINK_BLOCKED) {
2244*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
2245*a1157835SDaniel Fojt 				   " is blocked - drop Authentication frame",
2246*a1157835SDaniel Fojt 				   MAC2STR(mgmt->sa));
2247*a1157835SDaniel Fojt 			return;
2248*a1157835SDaniel Fojt 		}
2249*a1157835SDaniel Fojt #endif /* CONFIG_MESH */
2250*a1157835SDaniel Fojt 	} else {
2251*a1157835SDaniel Fojt #ifdef CONFIG_MESH
2252*a1157835SDaniel Fojt 		if (hapd->conf->mesh & MESH_ENABLED) {
2253*a1157835SDaniel Fojt 			/* if the mesh peer is not available, we don't do auth.
2254*a1157835SDaniel Fojt 			 */
2255*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
2256*a1157835SDaniel Fojt 				   " not yet known - drop Authentication frame",
2257*a1157835SDaniel Fojt 				   MAC2STR(mgmt->sa));
2258*a1157835SDaniel Fojt 			/*
2259*a1157835SDaniel Fojt 			 * Save a copy of the frame so that it can be processed
2260*a1157835SDaniel Fojt 			 * if a new peer entry is added shortly after this.
2261*a1157835SDaniel Fojt 			 */
2262*a1157835SDaniel Fojt 			wpabuf_free(hapd->mesh_pending_auth);
2263*a1157835SDaniel Fojt 			hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
2264*a1157835SDaniel Fojt 			os_get_reltime(&hapd->mesh_pending_auth_time);
2265*a1157835SDaniel Fojt 			return;
2266*a1157835SDaniel Fojt 		}
2267*a1157835SDaniel Fojt #endif /* CONFIG_MESH */
22683ff40c12SJohn Marino 
22693ff40c12SJohn Marino 		sta = ap_sta_add(hapd, mgmt->sa);
22703ff40c12SJohn Marino 		if (!sta) {
2271*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "ap_sta_add() failed");
22723ff40c12SJohn Marino 			resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
22733ff40c12SJohn Marino 			goto fail;
22743ff40c12SJohn Marino 		}
2275*a1157835SDaniel Fojt 	}
2276*a1157835SDaniel Fojt 	sta->last_seq_ctrl = seq_ctrl;
2277*a1157835SDaniel Fojt 	sta->last_subtype = WLAN_FC_STYPE_AUTH;
2278*a1157835SDaniel Fojt #ifdef CONFIG_MBO
2279*a1157835SDaniel Fojt 	sta->auth_rssi = rssi;
2280*a1157835SDaniel Fojt #endif /* CONFIG_MBO */
22813ff40c12SJohn Marino 
2282*a1157835SDaniel Fojt 	res = ieee802_11_set_radius_info(
2283*a1157835SDaniel Fojt 		hapd, sta, res, session_timeout, acct_interim_interval,
2284*a1157835SDaniel Fojt 		&vlan_id, &psk, &identity, &radius_cui);
2285*a1157835SDaniel Fojt 	if (res) {
2286*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "ieee802_11_set_radius_info() failed");
22873ff40c12SJohn Marino 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
22883ff40c12SJohn Marino 		goto fail;
22893ff40c12SJohn Marino 	}
22903ff40c12SJohn Marino 
22913ff40c12SJohn Marino 	sta->flags &= ~WLAN_STA_PREAUTH;
22923ff40c12SJohn Marino 	ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
22933ff40c12SJohn Marino 
2294*a1157835SDaniel Fojt 	/*
2295*a1157835SDaniel Fojt 	 * If the driver supports full AP client state, add a station to the
2296*a1157835SDaniel Fojt 	 * driver before sending authentication reply to make sure the driver
2297*a1157835SDaniel Fojt 	 * has resources, and not to go through the entire authentication and
2298*a1157835SDaniel Fojt 	 * association handshake, and fail it at the end.
2299*a1157835SDaniel Fojt 	 *
2300*a1157835SDaniel Fojt 	 * If this is not the first transaction, in a multi-step authentication
2301*a1157835SDaniel Fojt 	 * algorithm, the station already exists in the driver
2302*a1157835SDaniel Fojt 	 * (sta->added_unassoc = 1) so skip it.
2303*a1157835SDaniel Fojt 	 *
2304*a1157835SDaniel Fojt 	 * In mesh mode, the station was already added to the driver when the
2305*a1157835SDaniel Fojt 	 * NEW_PEER_CANDIDATE event is received.
2306*a1157835SDaniel Fojt 	 *
2307*a1157835SDaniel Fojt 	 * If PMF was negotiated for the existing association, skip this to
2308*a1157835SDaniel Fojt 	 * avoid dropping the STA entry and the associated keys. This is needed
2309*a1157835SDaniel Fojt 	 * to allow the original connection work until the attempt can complete
2310*a1157835SDaniel Fojt 	 * (re)association, so that unprotected Authentication frame cannot be
2311*a1157835SDaniel Fojt 	 * used to bypass PMF protection.
2312*a1157835SDaniel Fojt 	 */
2313*a1157835SDaniel Fojt 	if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
2314*a1157835SDaniel Fojt 	    (!(sta->flags & WLAN_STA_MFP) || !ap_sta_is_authorized(sta)) &&
2315*a1157835SDaniel Fojt 	    !(hapd->conf->mesh & MESH_ENABLED) &&
2316*a1157835SDaniel Fojt 	    !(sta->added_unassoc)) {
2317*a1157835SDaniel Fojt 		/*
2318*a1157835SDaniel Fojt 		 * If a station that is already associated to the AP, is trying
2319*a1157835SDaniel Fojt 		 * to authenticate again, remove the STA entry, in order to make
2320*a1157835SDaniel Fojt 		 * sure the STA PS state gets cleared and configuration gets
2321*a1157835SDaniel Fojt 		 * updated. To handle this, station's added_unassoc flag is
2322*a1157835SDaniel Fojt 		 * cleared once the station has completed association.
2323*a1157835SDaniel Fojt 		 */
2324*a1157835SDaniel Fojt 		ap_sta_set_authorized(hapd, sta, 0);
2325*a1157835SDaniel Fojt 		hostapd_drv_sta_remove(hapd, sta->addr);
2326*a1157835SDaniel Fojt 		sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH |
2327*a1157835SDaniel Fojt 				WLAN_STA_AUTHORIZED);
2328*a1157835SDaniel Fojt 
2329*a1157835SDaniel Fojt 		if (hostapd_sta_add(hapd, sta->addr, 0, 0,
2330*a1157835SDaniel Fojt 				    sta->supported_rates,
2331*a1157835SDaniel Fojt 				    sta->supported_rates_len,
2332*a1157835SDaniel Fojt 				    0, NULL, NULL, NULL, 0,
2333*a1157835SDaniel Fojt 				    sta->flags, 0, 0, 0, 0)) {
2334*a1157835SDaniel Fojt 			hostapd_logger(hapd, sta->addr,
2335*a1157835SDaniel Fojt 				       HOSTAPD_MODULE_IEEE80211,
2336*a1157835SDaniel Fojt 				       HOSTAPD_LEVEL_NOTICE,
2337*a1157835SDaniel Fojt 				       "Could not add STA to kernel driver");
2338*a1157835SDaniel Fojt 			resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2339*a1157835SDaniel Fojt 			goto fail;
2340*a1157835SDaniel Fojt 		}
2341*a1157835SDaniel Fojt 
2342*a1157835SDaniel Fojt 		sta->added_unassoc = 1;
2343*a1157835SDaniel Fojt 	}
23443ff40c12SJohn Marino 
23453ff40c12SJohn Marino 	switch (auth_alg) {
23463ff40c12SJohn Marino 	case WLAN_AUTH_OPEN:
23473ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
23483ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
23493ff40c12SJohn Marino 			       "authentication OK (open system)");
23503ff40c12SJohn Marino 		sta->flags |= WLAN_STA_AUTH;
23513ff40c12SJohn Marino 		wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
23523ff40c12SJohn Marino 		sta->auth_alg = WLAN_AUTH_OPEN;
23533ff40c12SJohn Marino 		mlme_authenticate_indication(hapd, sta);
23543ff40c12SJohn Marino 		break;
2355*a1157835SDaniel Fojt #ifndef CONFIG_NO_RC4
23563ff40c12SJohn Marino 	case WLAN_AUTH_SHARED_KEY:
23573ff40c12SJohn Marino 		resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
23583ff40c12SJohn Marino 				       fc & WLAN_FC_ISWEP);
2359*a1157835SDaniel Fojt 		if (resp != 0)
2360*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
2361*a1157835SDaniel Fojt 				   "auth_shared_key() failed: status=%d", resp);
23623ff40c12SJohn Marino 		sta->auth_alg = WLAN_AUTH_SHARED_KEY;
23633ff40c12SJohn Marino 		mlme_authenticate_indication(hapd, sta);
23643ff40c12SJohn Marino 		if (sta->challenge && auth_transaction == 1) {
23653ff40c12SJohn Marino 			resp_ies[0] = WLAN_EID_CHALLENGE;
23663ff40c12SJohn Marino 			resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
23673ff40c12SJohn Marino 			os_memcpy(resp_ies + 2, sta->challenge,
23683ff40c12SJohn Marino 				  WLAN_AUTH_CHALLENGE_LEN);
23693ff40c12SJohn Marino 			resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
23703ff40c12SJohn Marino 		}
23713ff40c12SJohn Marino 		break;
2372*a1157835SDaniel Fojt #endif /* CONFIG_NO_RC4 */
2373*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211R_AP
23743ff40c12SJohn Marino 	case WLAN_AUTH_FT:
23753ff40c12SJohn Marino 		sta->auth_alg = WLAN_AUTH_FT;
23763ff40c12SJohn Marino 		if (sta->wpa_sm == NULL)
23773ff40c12SJohn Marino 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
23783ff40c12SJohn Marino 							sta->addr, NULL);
23793ff40c12SJohn Marino 		if (sta->wpa_sm == NULL) {
23803ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
23813ff40c12SJohn Marino 				   "state machine");
23823ff40c12SJohn Marino 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
23833ff40c12SJohn Marino 			goto fail;
23843ff40c12SJohn Marino 		}
23853ff40c12SJohn Marino 		wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
23863ff40c12SJohn Marino 				    auth_transaction, mgmt->u.auth.variable,
23873ff40c12SJohn Marino 				    len - IEEE80211_HDRLEN -
23883ff40c12SJohn Marino 				    sizeof(mgmt->u.auth),
23893ff40c12SJohn Marino 				    handle_auth_ft_finish, hapd);
23903ff40c12SJohn Marino 		/* handle_auth_ft_finish() callback will complete auth. */
23913ff40c12SJohn Marino 		return;
2392*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211R_AP */
23933ff40c12SJohn Marino #ifdef CONFIG_SAE
23943ff40c12SJohn Marino 	case WLAN_AUTH_SAE:
2395*a1157835SDaniel Fojt #ifdef CONFIG_MESH
2396*a1157835SDaniel Fojt 		if (status_code == WLAN_STATUS_SUCCESS &&
2397*a1157835SDaniel Fojt 		    hapd->conf->mesh & MESH_ENABLED) {
2398*a1157835SDaniel Fojt 			if (sta->wpa_sm == NULL)
2399*a1157835SDaniel Fojt 				sta->wpa_sm =
2400*a1157835SDaniel Fojt 					wpa_auth_sta_init(hapd->wpa_auth,
2401*a1157835SDaniel Fojt 							  sta->addr, NULL);
2402*a1157835SDaniel Fojt 			if (sta->wpa_sm == NULL) {
2403*a1157835SDaniel Fojt 				wpa_printf(MSG_DEBUG,
2404*a1157835SDaniel Fojt 					   "SAE: Failed to initialize WPA state machine");
2405*a1157835SDaniel Fojt 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2406*a1157835SDaniel Fojt 				goto fail;
2407*a1157835SDaniel Fojt 			}
2408*a1157835SDaniel Fojt 		}
2409*a1157835SDaniel Fojt #endif /* CONFIG_MESH */
2410*a1157835SDaniel Fojt 		handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
2411*a1157835SDaniel Fojt 				status_code);
24123ff40c12SJohn Marino 		return;
24133ff40c12SJohn Marino #endif /* CONFIG_SAE */
2414*a1157835SDaniel Fojt #ifdef CONFIG_FILS
2415*a1157835SDaniel Fojt 	case WLAN_AUTH_FILS_SK:
2416*a1157835SDaniel Fojt 	case WLAN_AUTH_FILS_SK_PFS:
2417*a1157835SDaniel Fojt 		handle_auth_fils(hapd, sta, mgmt->u.auth.variable,
2418*a1157835SDaniel Fojt 				 len - IEEE80211_HDRLEN - sizeof(mgmt->u.auth),
2419*a1157835SDaniel Fojt 				 auth_alg, auth_transaction, status_code,
2420*a1157835SDaniel Fojt 				 handle_auth_fils_finish);
2421*a1157835SDaniel Fojt 		return;
2422*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
24233ff40c12SJohn Marino 	}
24243ff40c12SJohn Marino 
24253ff40c12SJohn Marino  fail:
24263ff40c12SJohn Marino 	os_free(identity);
24273ff40c12SJohn Marino 	os_free(radius_cui);
24283ff40c12SJohn Marino 	hostapd_free_psk_list(psk);
24293ff40c12SJohn Marino 
2430*a1157835SDaniel Fojt 	reply_res = send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
2431*a1157835SDaniel Fojt 				    auth_transaction + 1, resp, resp_ies,
2432*a1157835SDaniel Fojt 				    resp_ies_len, "handle-auth");
2433*a1157835SDaniel Fojt 
2434*a1157835SDaniel Fojt 	if (sta && sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
2435*a1157835SDaniel Fojt 					  reply_res != WLAN_STATUS_SUCCESS)) {
2436*a1157835SDaniel Fojt 		hostapd_drv_sta_remove(hapd, sta->addr);
2437*a1157835SDaniel Fojt 		sta->added_unassoc = 0;
2438*a1157835SDaniel Fojt 	}
24393ff40c12SJohn Marino }
24403ff40c12SJohn Marino 
24413ff40c12SJohn Marino 
hostapd_get_aid(struct hostapd_data * hapd,struct sta_info * sta)2442*a1157835SDaniel Fojt int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
24433ff40c12SJohn Marino {
24443ff40c12SJohn Marino 	int i, j = 32, aid;
24453ff40c12SJohn Marino 
24463ff40c12SJohn Marino 	/* get a unique AID */
24473ff40c12SJohn Marino 	if (sta->aid > 0) {
24483ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
24493ff40c12SJohn Marino 		return 0;
24503ff40c12SJohn Marino 	}
24513ff40c12SJohn Marino 
2452*a1157835SDaniel Fojt 	if (TEST_FAIL())
2453*a1157835SDaniel Fojt 		return -1;
2454*a1157835SDaniel Fojt 
24553ff40c12SJohn Marino 	for (i = 0; i < AID_WORDS; i++) {
24563ff40c12SJohn Marino 		if (hapd->sta_aid[i] == (u32) -1)
24573ff40c12SJohn Marino 			continue;
24583ff40c12SJohn Marino 		for (j = 0; j < 32; j++) {
24593ff40c12SJohn Marino 			if (!(hapd->sta_aid[i] & BIT(j)))
24603ff40c12SJohn Marino 				break;
24613ff40c12SJohn Marino 		}
24623ff40c12SJohn Marino 		if (j < 32)
24633ff40c12SJohn Marino 			break;
24643ff40c12SJohn Marino 	}
24653ff40c12SJohn Marino 	if (j == 32)
24663ff40c12SJohn Marino 		return -1;
24673ff40c12SJohn Marino 	aid = i * 32 + j + 1;
24683ff40c12SJohn Marino 	if (aid > 2007)
24693ff40c12SJohn Marino 		return -1;
24703ff40c12SJohn Marino 
24713ff40c12SJohn Marino 	sta->aid = aid;
24723ff40c12SJohn Marino 	hapd->sta_aid[i] |= BIT(j);
24733ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
24743ff40c12SJohn Marino 	return 0;
24753ff40c12SJohn Marino }
24763ff40c12SJohn Marino 
24773ff40c12SJohn Marino 
check_ssid(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ssid_ie,size_t ssid_ie_len)24783ff40c12SJohn Marino static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
24793ff40c12SJohn Marino 		      const u8 *ssid_ie, size_t ssid_ie_len)
24803ff40c12SJohn Marino {
24813ff40c12SJohn Marino 	if (ssid_ie == NULL)
24823ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
24833ff40c12SJohn Marino 
24843ff40c12SJohn Marino 	if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
24853ff40c12SJohn Marino 	    os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
24863ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
24873ff40c12SJohn Marino 			       HOSTAPD_LEVEL_INFO,
24883ff40c12SJohn Marino 			       "Station tried to associate with unknown SSID "
2489*a1157835SDaniel Fojt 			       "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
24903ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
24913ff40c12SJohn Marino 	}
24923ff40c12SJohn Marino 
24933ff40c12SJohn Marino 	return WLAN_STATUS_SUCCESS;
24943ff40c12SJohn Marino }
24953ff40c12SJohn Marino 
24963ff40c12SJohn Marino 
check_wmm(struct hostapd_data * hapd,struct sta_info * sta,const u8 * wmm_ie,size_t wmm_ie_len)24973ff40c12SJohn Marino static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
24983ff40c12SJohn Marino 		     const u8 *wmm_ie, size_t wmm_ie_len)
24993ff40c12SJohn Marino {
25003ff40c12SJohn Marino 	sta->flags &= ~WLAN_STA_WMM;
25013ff40c12SJohn Marino 	sta->qosinfo = 0;
25023ff40c12SJohn Marino 	if (wmm_ie && hapd->conf->wmm_enabled) {
25033ff40c12SJohn Marino 		struct wmm_information_element *wmm;
25043ff40c12SJohn Marino 
25053ff40c12SJohn Marino 		if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
25063ff40c12SJohn Marino 			hostapd_logger(hapd, sta->addr,
25073ff40c12SJohn Marino 				       HOSTAPD_MODULE_WPA,
25083ff40c12SJohn Marino 				       HOSTAPD_LEVEL_DEBUG,
25093ff40c12SJohn Marino 				       "invalid WMM element in association "
25103ff40c12SJohn Marino 				       "request");
25113ff40c12SJohn Marino 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
25123ff40c12SJohn Marino 		}
25133ff40c12SJohn Marino 
25143ff40c12SJohn Marino 		sta->flags |= WLAN_STA_WMM;
25153ff40c12SJohn Marino 		wmm = (struct wmm_information_element *) wmm_ie;
25163ff40c12SJohn Marino 		sta->qosinfo = wmm->qos_info;
25173ff40c12SJohn Marino 	}
25183ff40c12SJohn Marino 	return WLAN_STATUS_SUCCESS;
25193ff40c12SJohn Marino }
25203ff40c12SJohn Marino 
check_multi_ap(struct hostapd_data * hapd,struct sta_info * sta,const u8 * multi_ap_ie,size_t multi_ap_len)2521*a1157835SDaniel Fojt static u16 check_multi_ap(struct hostapd_data *hapd, struct sta_info *sta,
2522*a1157835SDaniel Fojt 			  const u8 *multi_ap_ie, size_t multi_ap_len)
2523*a1157835SDaniel Fojt {
2524*a1157835SDaniel Fojt 	u8 multi_ap_value = 0;
2525*a1157835SDaniel Fojt 
2526*a1157835SDaniel Fojt 	sta->flags &= ~WLAN_STA_MULTI_AP;
2527*a1157835SDaniel Fojt 
2528*a1157835SDaniel Fojt 	if (!hapd->conf->multi_ap)
2529*a1157835SDaniel Fojt 		return WLAN_STATUS_SUCCESS;
2530*a1157835SDaniel Fojt 
2531*a1157835SDaniel Fojt 	if (multi_ap_ie) {
2532*a1157835SDaniel Fojt 		const u8 *multi_ap_subelem;
2533*a1157835SDaniel Fojt 
2534*a1157835SDaniel Fojt 		multi_ap_subelem = get_ie(multi_ap_ie + 4,
2535*a1157835SDaniel Fojt 					  multi_ap_len - 4,
2536*a1157835SDaniel Fojt 					  MULTI_AP_SUB_ELEM_TYPE);
2537*a1157835SDaniel Fojt 		if (multi_ap_subelem && multi_ap_subelem[1] == 1) {
2538*a1157835SDaniel Fojt 			multi_ap_value = multi_ap_subelem[2];
2539*a1157835SDaniel Fojt 		} else {
2540*a1157835SDaniel Fojt 			hostapd_logger(hapd, sta->addr,
2541*a1157835SDaniel Fojt 				       HOSTAPD_MODULE_IEEE80211,
2542*a1157835SDaniel Fojt 				       HOSTAPD_LEVEL_INFO,
2543*a1157835SDaniel Fojt 				       "Multi-AP IE has missing or invalid Multi-AP subelement");
2544*a1157835SDaniel Fojt 			return WLAN_STATUS_INVALID_IE;
2545*a1157835SDaniel Fojt 		}
2546*a1157835SDaniel Fojt 	}
2547*a1157835SDaniel Fojt 
2548*a1157835SDaniel Fojt 	if (multi_ap_value && multi_ap_value != MULTI_AP_BACKHAUL_STA)
2549*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2550*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_INFO,
2551*a1157835SDaniel Fojt 			       "Multi-AP IE with unexpected value 0x%02x",
2552*a1157835SDaniel Fojt 			       multi_ap_value);
2553*a1157835SDaniel Fojt 
2554*a1157835SDaniel Fojt 	if (!(multi_ap_value & MULTI_AP_BACKHAUL_STA)) {
2555*a1157835SDaniel Fojt 		if (hapd->conf->multi_ap & FRONTHAUL_BSS)
2556*a1157835SDaniel Fojt 			return WLAN_STATUS_SUCCESS;
2557*a1157835SDaniel Fojt 
2558*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr,
2559*a1157835SDaniel Fojt 			       HOSTAPD_MODULE_IEEE80211,
2560*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_INFO,
2561*a1157835SDaniel Fojt 			       "Non-Multi-AP STA tries to associate with backhaul-only BSS");
2562*a1157835SDaniel Fojt 		return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
2563*a1157835SDaniel Fojt 	}
2564*a1157835SDaniel Fojt 
2565*a1157835SDaniel Fojt 	if (!(hapd->conf->multi_ap & BACKHAUL_BSS))
2566*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2567*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_DEBUG,
2568*a1157835SDaniel Fojt 			       "Backhaul STA tries to associate with fronthaul-only BSS");
2569*a1157835SDaniel Fojt 
2570*a1157835SDaniel Fojt 	sta->flags |= WLAN_STA_MULTI_AP;
2571*a1157835SDaniel Fojt 	return WLAN_STATUS_SUCCESS;
2572*a1157835SDaniel Fojt }
2573*a1157835SDaniel Fojt 
25743ff40c12SJohn Marino 
copy_supp_rates(struct hostapd_data * hapd,struct sta_info * sta,struct ieee802_11_elems * elems)25753ff40c12SJohn Marino static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
25763ff40c12SJohn Marino 			   struct ieee802_11_elems *elems)
25773ff40c12SJohn Marino {
2578*a1157835SDaniel Fojt 	/* Supported rates not used in IEEE 802.11ad/DMG */
2579*a1157835SDaniel Fojt 	if (hapd->iface->current_mode &&
2580*a1157835SDaniel Fojt 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD)
2581*a1157835SDaniel Fojt 		return WLAN_STATUS_SUCCESS;
2582*a1157835SDaniel Fojt 
25833ff40c12SJohn Marino 	if (!elems->supp_rates) {
25843ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
25853ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
25863ff40c12SJohn Marino 			       "No supported rates element in AssocReq");
25873ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
25883ff40c12SJohn Marino 	}
25893ff40c12SJohn Marino 
25903ff40c12SJohn Marino 	if (elems->supp_rates_len + elems->ext_supp_rates_len >
25913ff40c12SJohn Marino 	    sizeof(sta->supported_rates)) {
25923ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
25933ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
25943ff40c12SJohn Marino 			       "Invalid supported rates element length %d+%d",
25953ff40c12SJohn Marino 			       elems->supp_rates_len,
25963ff40c12SJohn Marino 			       elems->ext_supp_rates_len);
25973ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
25983ff40c12SJohn Marino 	}
25993ff40c12SJohn Marino 
26003ff40c12SJohn Marino 	sta->supported_rates_len = merge_byte_arrays(
26013ff40c12SJohn Marino 		sta->supported_rates, sizeof(sta->supported_rates),
26023ff40c12SJohn Marino 		elems->supp_rates, elems->supp_rates_len,
26033ff40c12SJohn Marino 		elems->ext_supp_rates, elems->ext_supp_rates_len);
26043ff40c12SJohn Marino 
26053ff40c12SJohn Marino 	return WLAN_STATUS_SUCCESS;
26063ff40c12SJohn Marino }
26073ff40c12SJohn Marino 
26083ff40c12SJohn Marino 
check_ext_capab(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ext_capab_ie,size_t ext_capab_ie_len)26093ff40c12SJohn Marino static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
26103ff40c12SJohn Marino 			   const u8 *ext_capab_ie, size_t ext_capab_ie_len)
26113ff40c12SJohn Marino {
26123ff40c12SJohn Marino #ifdef CONFIG_INTERWORKING
26133ff40c12SJohn Marino 	/* check for QoS Map support */
26143ff40c12SJohn Marino 	if (ext_capab_ie_len >= 5) {
26153ff40c12SJohn Marino 		if (ext_capab_ie[4] & 0x01)
26163ff40c12SJohn Marino 			sta->qos_map_enabled = 1;
26173ff40c12SJohn Marino 	}
26183ff40c12SJohn Marino #endif /* CONFIG_INTERWORKING */
26193ff40c12SJohn Marino 
2620*a1157835SDaniel Fojt 	if (ext_capab_ie_len > 0) {
2621*a1157835SDaniel Fojt 		sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
2622*a1157835SDaniel Fojt 		os_free(sta->ext_capability);
2623*a1157835SDaniel Fojt 		sta->ext_capability = os_malloc(1 + ext_capab_ie_len);
2624*a1157835SDaniel Fojt 		if (sta->ext_capability) {
2625*a1157835SDaniel Fojt 			sta->ext_capability[0] = ext_capab_ie_len;
2626*a1157835SDaniel Fojt 			os_memcpy(sta->ext_capability + 1, ext_capab_ie,
2627*a1157835SDaniel Fojt 				  ext_capab_ie_len);
2628*a1157835SDaniel Fojt 		}
2629*a1157835SDaniel Fojt 	}
2630*a1157835SDaniel Fojt 
26313ff40c12SJohn Marino 	return WLAN_STATUS_SUCCESS;
26323ff40c12SJohn Marino }
26333ff40c12SJohn Marino 
26343ff40c12SJohn Marino 
2635*a1157835SDaniel Fojt #ifdef CONFIG_OWE
2636*a1157835SDaniel Fojt 
owe_group_supported(struct hostapd_data * hapd,u16 group)2637*a1157835SDaniel Fojt static int owe_group_supported(struct hostapd_data *hapd, u16 group)
2638*a1157835SDaniel Fojt {
2639*a1157835SDaniel Fojt 	int i;
2640*a1157835SDaniel Fojt 	int *groups = hapd->conf->owe_groups;
2641*a1157835SDaniel Fojt 
2642*a1157835SDaniel Fojt 	if (group != 19 && group != 20 && group != 21)
2643*a1157835SDaniel Fojt 		return 0;
2644*a1157835SDaniel Fojt 
2645*a1157835SDaniel Fojt 	if (!groups)
2646*a1157835SDaniel Fojt 		return 1;
2647*a1157835SDaniel Fojt 
2648*a1157835SDaniel Fojt 	for (i = 0; groups[i] > 0; i++) {
2649*a1157835SDaniel Fojt 		if (groups[i] == group)
2650*a1157835SDaniel Fojt 			return 1;
2651*a1157835SDaniel Fojt 	}
2652*a1157835SDaniel Fojt 
2653*a1157835SDaniel Fojt 	return 0;
2654*a1157835SDaniel Fojt }
2655*a1157835SDaniel Fojt 
2656*a1157835SDaniel Fojt 
owe_process_assoc_req(struct hostapd_data * hapd,struct sta_info * sta,const u8 * owe_dh,u8 owe_dh_len)2657*a1157835SDaniel Fojt static u16 owe_process_assoc_req(struct hostapd_data *hapd,
2658*a1157835SDaniel Fojt 				 struct sta_info *sta, const u8 *owe_dh,
2659*a1157835SDaniel Fojt 				 u8 owe_dh_len)
2660*a1157835SDaniel Fojt {
2661*a1157835SDaniel Fojt 	struct wpabuf *secret, *pub, *hkey;
2662*a1157835SDaniel Fojt 	int res;
2663*a1157835SDaniel Fojt 	u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
2664*a1157835SDaniel Fojt 	const char *info = "OWE Key Generation";
2665*a1157835SDaniel Fojt 	const u8 *addr[2];
2666*a1157835SDaniel Fojt 	size_t len[2];
2667*a1157835SDaniel Fojt 	u16 group;
2668*a1157835SDaniel Fojt 	size_t hash_len, prime_len;
2669*a1157835SDaniel Fojt 
2670*a1157835SDaniel Fojt 	if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
2671*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
2672*a1157835SDaniel Fojt 		return WLAN_STATUS_SUCCESS;
2673*a1157835SDaniel Fojt 	}
2674*a1157835SDaniel Fojt 
2675*a1157835SDaniel Fojt 	group = WPA_GET_LE16(owe_dh);
2676*a1157835SDaniel Fojt 	if (!owe_group_supported(hapd, group)) {
2677*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "OWE: Unsupported DH group %u", group);
2678*a1157835SDaniel Fojt 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2679*a1157835SDaniel Fojt 	}
2680*a1157835SDaniel Fojt 	if (group == 19)
2681*a1157835SDaniel Fojt 		prime_len = 32;
2682*a1157835SDaniel Fojt 	else if (group == 20)
2683*a1157835SDaniel Fojt 		prime_len = 48;
2684*a1157835SDaniel Fojt 	else if (group == 21)
2685*a1157835SDaniel Fojt 		prime_len = 66;
2686*a1157835SDaniel Fojt 	else
2687*a1157835SDaniel Fojt 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2688*a1157835SDaniel Fojt 
2689*a1157835SDaniel Fojt 	crypto_ecdh_deinit(sta->owe_ecdh);
2690*a1157835SDaniel Fojt 	sta->owe_ecdh = crypto_ecdh_init(group);
2691*a1157835SDaniel Fojt 	if (!sta->owe_ecdh)
2692*a1157835SDaniel Fojt 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2693*a1157835SDaniel Fojt 	sta->owe_group = group;
2694*a1157835SDaniel Fojt 
2695*a1157835SDaniel Fojt 	secret = crypto_ecdh_set_peerkey(sta->owe_ecdh, 0, owe_dh + 2,
2696*a1157835SDaniel Fojt 					 owe_dh_len - 2);
2697*a1157835SDaniel Fojt 	secret = wpabuf_zeropad(secret, prime_len);
2698*a1157835SDaniel Fojt 	if (!secret) {
2699*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
2700*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2701*a1157835SDaniel Fojt 	}
2702*a1157835SDaniel Fojt 	wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
2703*a1157835SDaniel Fojt 
2704*a1157835SDaniel Fojt 	/* prk = HKDF-extract(C | A | group, z) */
2705*a1157835SDaniel Fojt 
2706*a1157835SDaniel Fojt 	pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
2707*a1157835SDaniel Fojt 	if (!pub) {
2708*a1157835SDaniel Fojt 		wpabuf_clear_free(secret);
2709*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2710*a1157835SDaniel Fojt 	}
2711*a1157835SDaniel Fojt 
2712*a1157835SDaniel Fojt 	/* PMKID = Truncate-128(Hash(C | A)) */
2713*a1157835SDaniel Fojt 	addr[0] = owe_dh + 2;
2714*a1157835SDaniel Fojt 	len[0] = owe_dh_len - 2;
2715*a1157835SDaniel Fojt 	addr[1] = wpabuf_head(pub);
2716*a1157835SDaniel Fojt 	len[1] = wpabuf_len(pub);
2717*a1157835SDaniel Fojt 	if (group == 19) {
2718*a1157835SDaniel Fojt 		res = sha256_vector(2, addr, len, pmkid);
2719*a1157835SDaniel Fojt 		hash_len = SHA256_MAC_LEN;
2720*a1157835SDaniel Fojt 	} else if (group == 20) {
2721*a1157835SDaniel Fojt 		res = sha384_vector(2, addr, len, pmkid);
2722*a1157835SDaniel Fojt 		hash_len = SHA384_MAC_LEN;
2723*a1157835SDaniel Fojt 	} else if (group == 21) {
2724*a1157835SDaniel Fojt 		res = sha512_vector(2, addr, len, pmkid);
2725*a1157835SDaniel Fojt 		hash_len = SHA512_MAC_LEN;
2726*a1157835SDaniel Fojt 	} else {
2727*a1157835SDaniel Fojt 		wpabuf_free(pub);
2728*a1157835SDaniel Fojt 		wpabuf_clear_free(secret);
2729*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2730*a1157835SDaniel Fojt 	}
2731*a1157835SDaniel Fojt 	pub = wpabuf_zeropad(pub, prime_len);
2732*a1157835SDaniel Fojt 	if (res < 0 || !pub) {
2733*a1157835SDaniel Fojt 		wpabuf_free(pub);
2734*a1157835SDaniel Fojt 		wpabuf_clear_free(secret);
2735*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2736*a1157835SDaniel Fojt 	}
2737*a1157835SDaniel Fojt 
2738*a1157835SDaniel Fojt 	hkey = wpabuf_alloc(owe_dh_len - 2 + wpabuf_len(pub) + 2);
2739*a1157835SDaniel Fojt 	if (!hkey) {
2740*a1157835SDaniel Fojt 		wpabuf_free(pub);
2741*a1157835SDaniel Fojt 		wpabuf_clear_free(secret);
2742*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2743*a1157835SDaniel Fojt 	}
2744*a1157835SDaniel Fojt 
2745*a1157835SDaniel Fojt 	wpabuf_put_data(hkey, owe_dh + 2, owe_dh_len - 2); /* C */
2746*a1157835SDaniel Fojt 	wpabuf_put_buf(hkey, pub); /* A */
2747*a1157835SDaniel Fojt 	wpabuf_free(pub);
2748*a1157835SDaniel Fojt 	wpabuf_put_le16(hkey, group); /* group */
2749*a1157835SDaniel Fojt 	if (group == 19)
2750*a1157835SDaniel Fojt 		res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
2751*a1157835SDaniel Fojt 				  wpabuf_head(secret), wpabuf_len(secret), prk);
2752*a1157835SDaniel Fojt 	else if (group == 20)
2753*a1157835SDaniel Fojt 		res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
2754*a1157835SDaniel Fojt 				  wpabuf_head(secret), wpabuf_len(secret), prk);
2755*a1157835SDaniel Fojt 	else if (group == 21)
2756*a1157835SDaniel Fojt 		res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
2757*a1157835SDaniel Fojt 				  wpabuf_head(secret), wpabuf_len(secret), prk);
2758*a1157835SDaniel Fojt 	wpabuf_clear_free(hkey);
2759*a1157835SDaniel Fojt 	wpabuf_clear_free(secret);
2760*a1157835SDaniel Fojt 	if (res < 0)
2761*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2762*a1157835SDaniel Fojt 
2763*a1157835SDaniel Fojt 	wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
2764*a1157835SDaniel Fojt 
2765*a1157835SDaniel Fojt 	/* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
2766*a1157835SDaniel Fojt 
2767*a1157835SDaniel Fojt 	os_free(sta->owe_pmk);
2768*a1157835SDaniel Fojt 	sta->owe_pmk = os_malloc(hash_len);
2769*a1157835SDaniel Fojt 	if (!sta->owe_pmk) {
2770*a1157835SDaniel Fojt 		os_memset(prk, 0, SHA512_MAC_LEN);
2771*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2772*a1157835SDaniel Fojt 	}
2773*a1157835SDaniel Fojt 
2774*a1157835SDaniel Fojt 	if (group == 19)
2775*a1157835SDaniel Fojt 		res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
2776*a1157835SDaniel Fojt 				      os_strlen(info), sta->owe_pmk, hash_len);
2777*a1157835SDaniel Fojt 	else if (group == 20)
2778*a1157835SDaniel Fojt 		res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
2779*a1157835SDaniel Fojt 				      os_strlen(info), sta->owe_pmk, hash_len);
2780*a1157835SDaniel Fojt 	else if (group == 21)
2781*a1157835SDaniel Fojt 		res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
2782*a1157835SDaniel Fojt 				      os_strlen(info), sta->owe_pmk, hash_len);
2783*a1157835SDaniel Fojt 	os_memset(prk, 0, SHA512_MAC_LEN);
2784*a1157835SDaniel Fojt 	if (res < 0) {
2785*a1157835SDaniel Fojt 		os_free(sta->owe_pmk);
2786*a1157835SDaniel Fojt 		sta->owe_pmk = NULL;
2787*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2788*a1157835SDaniel Fojt 	}
2789*a1157835SDaniel Fojt 	sta->owe_pmk_len = hash_len;
2790*a1157835SDaniel Fojt 
2791*a1157835SDaniel Fojt 	wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sta->owe_pmk, sta->owe_pmk_len);
2792*a1157835SDaniel Fojt 	wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
2793*a1157835SDaniel Fojt 	wpa_auth_pmksa_add2(hapd->wpa_auth, sta->addr, sta->owe_pmk,
2794*a1157835SDaniel Fojt 			    sta->owe_pmk_len, pmkid, 0, WPA_KEY_MGMT_OWE);
2795*a1157835SDaniel Fojt 
2796*a1157835SDaniel Fojt 	return WLAN_STATUS_SUCCESS;
2797*a1157835SDaniel Fojt }
2798*a1157835SDaniel Fojt 
2799*a1157835SDaniel Fojt 
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)2800*a1157835SDaniel Fojt u16 owe_validate_request(struct hostapd_data *hapd, const u8 *peer,
2801*a1157835SDaniel Fojt 			 const u8 *rsn_ie, size_t rsn_ie_len,
2802*a1157835SDaniel Fojt 			 const u8 *owe_dh, size_t owe_dh_len)
2803*a1157835SDaniel Fojt {
2804*a1157835SDaniel Fojt 	struct wpa_ie_data data;
2805*a1157835SDaniel Fojt 	int res;
2806*a1157835SDaniel Fojt 
2807*a1157835SDaniel Fojt 	if (!rsn_ie || rsn_ie_len < 2) {
2808*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "OWE: Invalid RSNE from " MACSTR,
2809*a1157835SDaniel Fojt 			   MAC2STR(peer));
2810*a1157835SDaniel Fojt 		return WLAN_STATUS_INVALID_IE;
2811*a1157835SDaniel Fojt 	}
2812*a1157835SDaniel Fojt 	rsn_ie -= 2;
2813*a1157835SDaniel Fojt 	rsn_ie_len += 2;
2814*a1157835SDaniel Fojt 
2815*a1157835SDaniel Fojt 	res = wpa_parse_wpa_ie_rsn(rsn_ie, rsn_ie_len, &data);
2816*a1157835SDaniel Fojt 	if (res) {
2817*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "Failed to parse RSNE from " MACSTR
2818*a1157835SDaniel Fojt 			   " (res=%d)", MAC2STR(peer), res);
2819*a1157835SDaniel Fojt 		wpa_hexdump(MSG_DEBUG, "RSNE", rsn_ie, rsn_ie_len);
2820*a1157835SDaniel Fojt 		return wpa_res_to_status_code(res);
2821*a1157835SDaniel Fojt 	}
2822*a1157835SDaniel Fojt 	if (!(data.key_mgmt & WPA_KEY_MGMT_OWE)) {
2823*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
2824*a1157835SDaniel Fojt 			   "OWE: Unexpected key mgmt 0x%x from " MACSTR,
2825*a1157835SDaniel Fojt 			   (unsigned int) data.key_mgmt, MAC2STR(peer));
2826*a1157835SDaniel Fojt 		return WLAN_STATUS_AKMP_NOT_VALID;
2827*a1157835SDaniel Fojt 	}
2828*a1157835SDaniel Fojt 	if (!owe_dh) {
2829*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
2830*a1157835SDaniel Fojt 			   "OWE: No Diffie-Hellman Parameter element from "
2831*a1157835SDaniel Fojt 			   MACSTR, MAC2STR(peer));
2832*a1157835SDaniel Fojt 		return WLAN_STATUS_AKMP_NOT_VALID;
2833*a1157835SDaniel Fojt 	}
2834*a1157835SDaniel Fojt 
2835*a1157835SDaniel Fojt 	return WLAN_STATUS_SUCCESS;
2836*a1157835SDaniel Fojt }
2837*a1157835SDaniel Fojt 
2838*a1157835SDaniel Fojt 
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)2839*a1157835SDaniel Fojt u16 owe_process_rsn_ie(struct hostapd_data *hapd,
2840*a1157835SDaniel Fojt 		       struct sta_info *sta,
2841*a1157835SDaniel Fojt 		       const u8 *rsn_ie, size_t rsn_ie_len,
2842*a1157835SDaniel Fojt 		       const u8 *owe_dh, size_t owe_dh_len)
2843*a1157835SDaniel Fojt {
2844*a1157835SDaniel Fojt 	u16 status;
2845*a1157835SDaniel Fojt 	u8 *owe_buf, ie[256 * 2];
2846*a1157835SDaniel Fojt 	size_t ie_len = 0;
2847*a1157835SDaniel Fojt 	int res;
2848*a1157835SDaniel Fojt 
2849*a1157835SDaniel Fojt 	if (!rsn_ie || rsn_ie_len < 2) {
2850*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "OWE: No RSNE in (Re)AssocReq");
2851*a1157835SDaniel Fojt 		status = WLAN_STATUS_INVALID_IE;
2852*a1157835SDaniel Fojt 		goto end;
2853*a1157835SDaniel Fojt 	}
2854*a1157835SDaniel Fojt 
2855*a1157835SDaniel Fojt 	if (!sta->wpa_sm)
2856*a1157835SDaniel Fojt 		sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,	sta->addr,
2857*a1157835SDaniel Fojt 						NULL);
2858*a1157835SDaniel Fojt 	if (!sta->wpa_sm) {
2859*a1157835SDaniel Fojt 		wpa_printf(MSG_WARNING,
2860*a1157835SDaniel Fojt 			   "OWE: Failed to initialize WPA state machine");
2861*a1157835SDaniel Fojt 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2862*a1157835SDaniel Fojt 		goto end;
2863*a1157835SDaniel Fojt 	}
2864*a1157835SDaniel Fojt 	rsn_ie -= 2;
2865*a1157835SDaniel Fojt 	rsn_ie_len += 2;
2866*a1157835SDaniel Fojt 	res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
2867*a1157835SDaniel Fojt 				  hapd->iface->freq, rsn_ie, rsn_ie_len,
2868*a1157835SDaniel Fojt 				  NULL, 0, owe_dh, owe_dh_len);
2869*a1157835SDaniel Fojt 	status = wpa_res_to_status_code(res);
2870*a1157835SDaniel Fojt 	if (status != WLAN_STATUS_SUCCESS)
2871*a1157835SDaniel Fojt 		goto end;
2872*a1157835SDaniel Fojt 	status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
2873*a1157835SDaniel Fojt 	if (status != WLAN_STATUS_SUCCESS)
2874*a1157835SDaniel Fojt 		goto end;
2875*a1157835SDaniel Fojt 	owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, ie, sizeof(ie),
2876*a1157835SDaniel Fojt 						NULL, 0);
2877*a1157835SDaniel Fojt 	if (!owe_buf) {
2878*a1157835SDaniel Fojt 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2879*a1157835SDaniel Fojt 		goto end;
2880*a1157835SDaniel Fojt 	}
2881*a1157835SDaniel Fojt 
2882*a1157835SDaniel Fojt 	if (sta->owe_ecdh) {
2883*a1157835SDaniel Fojt 		struct wpabuf *pub;
2884*a1157835SDaniel Fojt 
2885*a1157835SDaniel Fojt 		pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
2886*a1157835SDaniel Fojt 		if (!pub) {
2887*a1157835SDaniel Fojt 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2888*a1157835SDaniel Fojt 			goto end;
2889*a1157835SDaniel Fojt 		}
2890*a1157835SDaniel Fojt 
2891*a1157835SDaniel Fojt 		/* OWE Diffie-Hellman Parameter element */
2892*a1157835SDaniel Fojt 		*owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
2893*a1157835SDaniel Fojt 		*owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
2894*a1157835SDaniel Fojt 		*owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
2895*a1157835SDaniel Fojt 							 */
2896*a1157835SDaniel Fojt 		WPA_PUT_LE16(owe_buf, sta->owe_group);
2897*a1157835SDaniel Fojt 		owe_buf += 2;
2898*a1157835SDaniel Fojt 		os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
2899*a1157835SDaniel Fojt 		owe_buf += wpabuf_len(pub);
2900*a1157835SDaniel Fojt 		wpabuf_free(pub);
2901*a1157835SDaniel Fojt 		sta->external_dh_updated = 1;
2902*a1157835SDaniel Fojt 	}
2903*a1157835SDaniel Fojt 	ie_len = owe_buf - ie;
2904*a1157835SDaniel Fojt 
2905*a1157835SDaniel Fojt end:
2906*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "OWE: Update status %d, ie len %d for peer "
2907*a1157835SDaniel Fojt 			      MACSTR, status, (unsigned int) ie_len,
2908*a1157835SDaniel Fojt 			      MAC2STR(sta->addr));
2909*a1157835SDaniel Fojt 	hostapd_drv_update_dh_ie(hapd, sta->addr, status,
2910*a1157835SDaniel Fojt 				 status == WLAN_STATUS_SUCCESS ? ie : NULL,
2911*a1157835SDaniel Fojt 				 ie_len);
2912*a1157835SDaniel Fojt 
2913*a1157835SDaniel Fojt 	return status;
2914*a1157835SDaniel Fojt }
2915*a1157835SDaniel Fojt 
2916*a1157835SDaniel Fojt #endif /* CONFIG_OWE */
2917*a1157835SDaniel Fojt 
2918*a1157835SDaniel Fojt 
check_assoc_ies(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ies,size_t ies_len,int reassoc)29193ff40c12SJohn Marino static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
29203ff40c12SJohn Marino 			   const u8 *ies, size_t ies_len, int reassoc)
29213ff40c12SJohn Marino {
29223ff40c12SJohn Marino 	struct ieee802_11_elems elems;
29233ff40c12SJohn Marino 	u16 resp;
29243ff40c12SJohn Marino 	const u8 *wpa_ie;
29253ff40c12SJohn Marino 	size_t wpa_ie_len;
29263ff40c12SJohn Marino 	const u8 *p2p_dev_addr = NULL;
29273ff40c12SJohn Marino 
29283ff40c12SJohn Marino 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
29293ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
29303ff40c12SJohn Marino 			       HOSTAPD_LEVEL_INFO, "Station sent an invalid "
29313ff40c12SJohn Marino 			       "association request");
29323ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
29333ff40c12SJohn Marino 	}
29343ff40c12SJohn Marino 
29353ff40c12SJohn Marino 	resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
29363ff40c12SJohn Marino 	if (resp != WLAN_STATUS_SUCCESS)
29373ff40c12SJohn Marino 		return resp;
29383ff40c12SJohn Marino 	resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
29393ff40c12SJohn Marino 	if (resp != WLAN_STATUS_SUCCESS)
29403ff40c12SJohn Marino 		return resp;
29413ff40c12SJohn Marino 	resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
29423ff40c12SJohn Marino 	if (resp != WLAN_STATUS_SUCCESS)
29433ff40c12SJohn Marino 		return resp;
29443ff40c12SJohn Marino 	resp = copy_supp_rates(hapd, sta, &elems);
29453ff40c12SJohn Marino 	if (resp != WLAN_STATUS_SUCCESS)
29463ff40c12SJohn Marino 		return resp;
2947*a1157835SDaniel Fojt 
2948*a1157835SDaniel Fojt 	resp = check_multi_ap(hapd, sta, elems.multi_ap, elems.multi_ap_len);
2949*a1157835SDaniel Fojt 	if (resp != WLAN_STATUS_SUCCESS)
2950*a1157835SDaniel Fojt 		return resp;
2951*a1157835SDaniel Fojt 
29523ff40c12SJohn Marino #ifdef CONFIG_IEEE80211N
2953*a1157835SDaniel Fojt 	resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
29543ff40c12SJohn Marino 	if (resp != WLAN_STATUS_SUCCESS)
29553ff40c12SJohn Marino 		return resp;
29563ff40c12SJohn Marino 	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
29573ff40c12SJohn Marino 	    !(sta->flags & WLAN_STA_HT)) {
29583ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
29593ff40c12SJohn Marino 			       HOSTAPD_LEVEL_INFO, "Station does not support "
29603ff40c12SJohn Marino 			       "mandatory HT PHY - reject association");
29613ff40c12SJohn Marino 		return WLAN_STATUS_ASSOC_DENIED_NO_HT;
29623ff40c12SJohn Marino 	}
29633ff40c12SJohn Marino #endif /* CONFIG_IEEE80211N */
29643ff40c12SJohn Marino 
29653ff40c12SJohn Marino #ifdef CONFIG_IEEE80211AC
2966*a1157835SDaniel Fojt 	if (hapd->iconf->ieee80211ac) {
2967*a1157835SDaniel Fojt 		resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
29683ff40c12SJohn Marino 		if (resp != WLAN_STATUS_SUCCESS)
29693ff40c12SJohn Marino 			return resp;
2970*a1157835SDaniel Fojt 
2971*a1157835SDaniel Fojt 		resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
2972*a1157835SDaniel Fojt 		if (resp != WLAN_STATUS_SUCCESS)
2973*a1157835SDaniel Fojt 			return resp;
2974*a1157835SDaniel Fojt 	}
2975*a1157835SDaniel Fojt 
29763ff40c12SJohn Marino 	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
29773ff40c12SJohn Marino 	    !(sta->flags & WLAN_STA_VHT)) {
29783ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
29793ff40c12SJohn Marino 			       HOSTAPD_LEVEL_INFO, "Station does not support "
29803ff40c12SJohn Marino 			       "mandatory VHT PHY - reject association");
29813ff40c12SJohn Marino 		return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
29823ff40c12SJohn Marino 	}
2983*a1157835SDaniel Fojt 
2984*a1157835SDaniel Fojt 	if (hapd->conf->vendor_vht && !elems.vht_capabilities) {
2985*a1157835SDaniel Fojt 		resp = copy_sta_vendor_vht(hapd, sta, elems.vendor_vht,
2986*a1157835SDaniel Fojt 					   elems.vendor_vht_len);
2987*a1157835SDaniel Fojt 		if (resp != WLAN_STATUS_SUCCESS)
2988*a1157835SDaniel Fojt 			return resp;
2989*a1157835SDaniel Fojt 	}
29903ff40c12SJohn Marino #endif /* CONFIG_IEEE80211AC */
2991*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211AX
2992*a1157835SDaniel Fojt 	if (hapd->iconf->ieee80211ax) {
2993*a1157835SDaniel Fojt 		resp = copy_sta_he_capab(hapd, sta, IEEE80211_MODE_AP,
2994*a1157835SDaniel Fojt 					 elems.he_capabilities,
2995*a1157835SDaniel Fojt 					 elems.he_capabilities_len);
2996*a1157835SDaniel Fojt 		if (resp != WLAN_STATUS_SUCCESS)
2997*a1157835SDaniel Fojt 			return resp;
2998*a1157835SDaniel Fojt 	}
2999*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211AX */
30003ff40c12SJohn Marino 
30013ff40c12SJohn Marino #ifdef CONFIG_P2P
30023ff40c12SJohn Marino 	if (elems.p2p) {
30033ff40c12SJohn Marino 		wpabuf_free(sta->p2p_ie);
30043ff40c12SJohn Marino 		sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
30053ff40c12SJohn Marino 							  P2P_IE_VENDOR_TYPE);
30063ff40c12SJohn Marino 		if (sta->p2p_ie)
30073ff40c12SJohn Marino 			p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
30083ff40c12SJohn Marino 	} else {
30093ff40c12SJohn Marino 		wpabuf_free(sta->p2p_ie);
30103ff40c12SJohn Marino 		sta->p2p_ie = NULL;
30113ff40c12SJohn Marino 	}
30123ff40c12SJohn Marino #endif /* CONFIG_P2P */
30133ff40c12SJohn Marino 
30143ff40c12SJohn Marino 	if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
30153ff40c12SJohn Marino 		wpa_ie = elems.rsn_ie;
30163ff40c12SJohn Marino 		wpa_ie_len = elems.rsn_ie_len;
30173ff40c12SJohn Marino 	} else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
30183ff40c12SJohn Marino 		   elems.wpa_ie) {
30193ff40c12SJohn Marino 		wpa_ie = elems.wpa_ie;
30203ff40c12SJohn Marino 		wpa_ie_len = elems.wpa_ie_len;
30213ff40c12SJohn Marino 	} else {
30223ff40c12SJohn Marino 		wpa_ie = NULL;
30233ff40c12SJohn Marino 		wpa_ie_len = 0;
30243ff40c12SJohn Marino 	}
30253ff40c12SJohn Marino 
30263ff40c12SJohn Marino #ifdef CONFIG_WPS
30273ff40c12SJohn Marino 	sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
30283ff40c12SJohn Marino 	if (hapd->conf->wps_state && elems.wps_ie) {
30293ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
30303ff40c12SJohn Marino 			   "Request - assume WPS is used");
30313ff40c12SJohn Marino 		sta->flags |= WLAN_STA_WPS;
30323ff40c12SJohn Marino 		wpabuf_free(sta->wps_ie);
30333ff40c12SJohn Marino 		sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
30343ff40c12SJohn Marino 							  WPS_IE_VENDOR_TYPE);
30353ff40c12SJohn Marino 		if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
30363ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
30373ff40c12SJohn Marino 			sta->flags |= WLAN_STA_WPS2;
30383ff40c12SJohn Marino 		}
30393ff40c12SJohn Marino 		wpa_ie = NULL;
30403ff40c12SJohn Marino 		wpa_ie_len = 0;
30413ff40c12SJohn Marino 		if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
30423ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
30433ff40c12SJohn Marino 				   "(Re)Association Request - reject");
30443ff40c12SJohn Marino 			return WLAN_STATUS_INVALID_IE;
30453ff40c12SJohn Marino 		}
30463ff40c12SJohn Marino 	} else if (hapd->conf->wps_state && wpa_ie == NULL) {
30473ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
30483ff40c12SJohn Marino 			   "(Re)Association Request - possible WPS use");
30493ff40c12SJohn Marino 		sta->flags |= WLAN_STA_MAYBE_WPS;
30503ff40c12SJohn Marino 	} else
30513ff40c12SJohn Marino #endif /* CONFIG_WPS */
30523ff40c12SJohn Marino 	if (hapd->conf->wpa && wpa_ie == NULL) {
30533ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
30543ff40c12SJohn Marino 			       HOSTAPD_LEVEL_INFO,
30553ff40c12SJohn Marino 			       "No WPA/RSN IE in association request");
30563ff40c12SJohn Marino 		return WLAN_STATUS_INVALID_IE;
30573ff40c12SJohn Marino 	}
30583ff40c12SJohn Marino 
30593ff40c12SJohn Marino 	if (hapd->conf->wpa && wpa_ie) {
30603ff40c12SJohn Marino 		int res;
30613ff40c12SJohn Marino 		wpa_ie -= 2;
30623ff40c12SJohn Marino 		wpa_ie_len += 2;
30633ff40c12SJohn Marino 		if (sta->wpa_sm == NULL)
30643ff40c12SJohn Marino 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
30653ff40c12SJohn Marino 							sta->addr,
30663ff40c12SJohn Marino 							p2p_dev_addr);
30673ff40c12SJohn Marino 		if (sta->wpa_sm == NULL) {
30683ff40c12SJohn Marino 			wpa_printf(MSG_WARNING, "Failed to initialize WPA "
30693ff40c12SJohn Marino 				   "state machine");
30703ff40c12SJohn Marino 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
30713ff40c12SJohn Marino 		}
3072*a1157835SDaniel Fojt 		wpa_auth_set_auth_alg(sta->wpa_sm, sta->auth_alg);
30733ff40c12SJohn Marino 		res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
3074*a1157835SDaniel Fojt 					  hapd->iface->freq,
30753ff40c12SJohn Marino 					  wpa_ie, wpa_ie_len,
3076*a1157835SDaniel Fojt 					  elems.mdie, elems.mdie_len,
3077*a1157835SDaniel Fojt 					  elems.owe_dh, elems.owe_dh_len);
3078*a1157835SDaniel Fojt 		resp = wpa_res_to_status_code(res);
30793ff40c12SJohn Marino 		if (resp != WLAN_STATUS_SUCCESS)
30803ff40c12SJohn Marino 			return resp;
30813ff40c12SJohn Marino #ifdef CONFIG_IEEE80211W
3082*a1157835SDaniel Fojt 		if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
3083*a1157835SDaniel Fojt 		    (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
3084*a1157835SDaniel Fojt 		    !sta->sa_query_timed_out &&
30853ff40c12SJohn Marino 		    sta->sa_query_count > 0)
30863ff40c12SJohn Marino 			ap_check_sa_query_timeout(hapd, sta);
3087*a1157835SDaniel Fojt 		if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
3088*a1157835SDaniel Fojt 		    (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
3089*a1157835SDaniel Fojt 		    !sta->sa_query_timed_out &&
30903ff40c12SJohn Marino 		    (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
30913ff40c12SJohn Marino 			/*
30923ff40c12SJohn Marino 			 * STA has already been associated with MFP and SA
30933ff40c12SJohn Marino 			 * Query timeout has not been reached. Reject the
30943ff40c12SJohn Marino 			 * association attempt temporarily and start SA Query,
30953ff40c12SJohn Marino 			 * if one is not pending.
30963ff40c12SJohn Marino 			 */
30973ff40c12SJohn Marino 
30983ff40c12SJohn Marino 			if (sta->sa_query_count == 0)
30993ff40c12SJohn Marino 				ap_sta_start_sa_query(hapd, sta);
31003ff40c12SJohn Marino 
31013ff40c12SJohn Marino 			return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
31023ff40c12SJohn Marino 		}
31033ff40c12SJohn Marino 
31043ff40c12SJohn Marino 		if (wpa_auth_uses_mfp(sta->wpa_sm))
31053ff40c12SJohn Marino 			sta->flags |= WLAN_STA_MFP;
31063ff40c12SJohn Marino 		else
31073ff40c12SJohn Marino 			sta->flags &= ~WLAN_STA_MFP;
31083ff40c12SJohn Marino #endif /* CONFIG_IEEE80211W */
31093ff40c12SJohn Marino 
3110*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211R_AP
31113ff40c12SJohn Marino 		if (sta->auth_alg == WLAN_AUTH_FT) {
31123ff40c12SJohn Marino 			if (!reassoc) {
31133ff40c12SJohn Marino 				wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
31143ff40c12SJohn Marino 					   "to use association (not "
31153ff40c12SJohn Marino 					   "re-association) with FT auth_alg",
31163ff40c12SJohn Marino 					   MAC2STR(sta->addr));
31173ff40c12SJohn Marino 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
31183ff40c12SJohn Marino 			}
31193ff40c12SJohn Marino 
31203ff40c12SJohn Marino 			resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
31213ff40c12SJohn Marino 						       ies_len);
31223ff40c12SJohn Marino 			if (resp != WLAN_STATUS_SUCCESS)
31233ff40c12SJohn Marino 				return resp;
31243ff40c12SJohn Marino 		}
3125*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211R_AP */
31263ff40c12SJohn Marino 
31273ff40c12SJohn Marino #ifdef CONFIG_SAE
3128*a1157835SDaniel Fojt 		if (wpa_auth_uses_sae(sta->wpa_sm) && sta->sae &&
3129*a1157835SDaniel Fojt 		    sta->sae->state == SAE_ACCEPTED)
3130*a1157835SDaniel Fojt 			wpa_auth_add_sae_pmkid(sta->wpa_sm, sta->sae->pmkid);
3131*a1157835SDaniel Fojt 
31323ff40c12SJohn Marino 		if (wpa_auth_uses_sae(sta->wpa_sm) &&
3133*a1157835SDaniel Fojt 		    sta->auth_alg == WLAN_AUTH_OPEN) {
3134*a1157835SDaniel Fojt 			struct rsn_pmksa_cache_entry *sa;
3135*a1157835SDaniel Fojt 			sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
3136*a1157835SDaniel Fojt 			if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
3137*a1157835SDaniel Fojt 				wpa_printf(MSG_DEBUG,
3138*a1157835SDaniel Fojt 					   "SAE: No PMKSA cache entry found for "
3139*a1157835SDaniel Fojt 					   MACSTR, MAC2STR(sta->addr));
3140*a1157835SDaniel Fojt 				return WLAN_STATUS_INVALID_PMKID;
3141*a1157835SDaniel Fojt 			}
3142*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "SAE: " MACSTR
3143*a1157835SDaniel Fojt 				   " using PMKSA caching", MAC2STR(sta->addr));
3144*a1157835SDaniel Fojt 		} else if (wpa_auth_uses_sae(sta->wpa_sm) &&
31453ff40c12SJohn Marino 			   sta->auth_alg != WLAN_AUTH_SAE &&
31463ff40c12SJohn Marino 			   !(sta->auth_alg == WLAN_AUTH_FT &&
31473ff40c12SJohn Marino 			     wpa_auth_uses_ft_sae(sta->wpa_sm))) {
31483ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
31493ff40c12SJohn Marino 				   "SAE AKM after non-SAE auth_alg %u",
31503ff40c12SJohn Marino 				   MAC2STR(sta->addr), sta->auth_alg);
31513ff40c12SJohn Marino 			return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
31523ff40c12SJohn Marino 		}
31533ff40c12SJohn Marino #endif /* CONFIG_SAE */
31543ff40c12SJohn Marino 
3155*a1157835SDaniel Fojt #ifdef CONFIG_OWE
3156*a1157835SDaniel Fojt 		if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
3157*a1157835SDaniel Fojt 		    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
3158*a1157835SDaniel Fojt 		    elems.owe_dh) {
3159*a1157835SDaniel Fojt 			resp = owe_process_assoc_req(hapd, sta, elems.owe_dh,
3160*a1157835SDaniel Fojt 						     elems.owe_dh_len);
3161*a1157835SDaniel Fojt 			if (resp != WLAN_STATUS_SUCCESS)
3162*a1157835SDaniel Fojt 				return resp;
3163*a1157835SDaniel Fojt 		}
3164*a1157835SDaniel Fojt #endif /* CONFIG_OWE */
3165*a1157835SDaniel Fojt 
3166*a1157835SDaniel Fojt #ifdef CONFIG_DPP2
3167*a1157835SDaniel Fojt 		dpp_pfs_free(sta->dpp_pfs);
3168*a1157835SDaniel Fojt 		sta->dpp_pfs = NULL;
3169*a1157835SDaniel Fojt 
3170*a1157835SDaniel Fojt 		if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
3171*a1157835SDaniel Fojt 		    hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
3172*a1157835SDaniel Fojt 		    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
3173*a1157835SDaniel Fojt 		    elems.owe_dh) {
3174*a1157835SDaniel Fojt 			sta->dpp_pfs = dpp_pfs_init(
3175*a1157835SDaniel Fojt 				wpabuf_head(hapd->conf->dpp_netaccesskey),
3176*a1157835SDaniel Fojt 				wpabuf_len(hapd->conf->dpp_netaccesskey));
3177*a1157835SDaniel Fojt 			if (!sta->dpp_pfs) {
3178*a1157835SDaniel Fojt 				wpa_printf(MSG_DEBUG,
3179*a1157835SDaniel Fojt 					   "DPP: Could not initialize PFS");
3180*a1157835SDaniel Fojt 				/* Try to continue without PFS */
3181*a1157835SDaniel Fojt 				goto pfs_fail;
3182*a1157835SDaniel Fojt 			}
3183*a1157835SDaniel Fojt 
3184*a1157835SDaniel Fojt 			if (dpp_pfs_process(sta->dpp_pfs, elems.owe_dh,
3185*a1157835SDaniel Fojt 					    elems.owe_dh_len) < 0) {
3186*a1157835SDaniel Fojt 				dpp_pfs_free(sta->dpp_pfs);
3187*a1157835SDaniel Fojt 				sta->dpp_pfs = NULL;
3188*a1157835SDaniel Fojt 				return WLAN_STATUS_UNSPECIFIED_FAILURE;
3189*a1157835SDaniel Fojt 			}
3190*a1157835SDaniel Fojt 		}
3191*a1157835SDaniel Fojt 
3192*a1157835SDaniel Fojt 		wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
3193*a1157835SDaniel Fojt 				   sta->dpp_pfs->secret : NULL);
3194*a1157835SDaniel Fojt 	pfs_fail:
3195*a1157835SDaniel Fojt #endif /* CONFIG_DPP2 */
3196*a1157835SDaniel Fojt 
31973ff40c12SJohn Marino #ifdef CONFIG_IEEE80211N
31983ff40c12SJohn Marino 		if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
31993ff40c12SJohn Marino 		    wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
32003ff40c12SJohn Marino 			hostapd_logger(hapd, sta->addr,
32013ff40c12SJohn Marino 				       HOSTAPD_MODULE_IEEE80211,
32023ff40c12SJohn Marino 				       HOSTAPD_LEVEL_INFO,
32033ff40c12SJohn Marino 				       "Station tried to use TKIP with HT "
32043ff40c12SJohn Marino 				       "association");
32053ff40c12SJohn Marino 			return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
32063ff40c12SJohn Marino 		}
32073ff40c12SJohn Marino #endif /* CONFIG_IEEE80211N */
3208*a1157835SDaniel Fojt #ifdef CONFIG_HS20
3209*a1157835SDaniel Fojt 	} else if (hapd->conf->osen) {
3210*a1157835SDaniel Fojt 		if (elems.osen == NULL) {
3211*a1157835SDaniel Fojt 			hostapd_logger(
3212*a1157835SDaniel Fojt 				hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3213*a1157835SDaniel Fojt 				HOSTAPD_LEVEL_INFO,
3214*a1157835SDaniel Fojt 				"No HS 2.0 OSEN element in association request");
3215*a1157835SDaniel Fojt 			return WLAN_STATUS_INVALID_IE;
3216*a1157835SDaniel Fojt 		}
3217*a1157835SDaniel Fojt 
3218*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
3219*a1157835SDaniel Fojt 		if (sta->wpa_sm == NULL)
3220*a1157835SDaniel Fojt 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
3221*a1157835SDaniel Fojt 							sta->addr, NULL);
3222*a1157835SDaniel Fojt 		if (sta->wpa_sm == NULL) {
3223*a1157835SDaniel Fojt 			wpa_printf(MSG_WARNING, "Failed to initialize WPA "
3224*a1157835SDaniel Fojt 				   "state machine");
3225*a1157835SDaniel Fojt 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
3226*a1157835SDaniel Fojt 		}
3227*a1157835SDaniel Fojt 		if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
3228*a1157835SDaniel Fojt 				      elems.osen - 2, elems.osen_len + 2) < 0)
3229*a1157835SDaniel Fojt 			return WLAN_STATUS_INVALID_IE;
3230*a1157835SDaniel Fojt #endif /* CONFIG_HS20 */
32313ff40c12SJohn Marino 	} else
32323ff40c12SJohn Marino 		wpa_auth_sta_no_wpa(sta->wpa_sm);
32333ff40c12SJohn Marino 
32343ff40c12SJohn Marino #ifdef CONFIG_P2P
32353ff40c12SJohn Marino 	p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
32363ff40c12SJohn Marino #endif /* CONFIG_P2P */
32373ff40c12SJohn Marino 
32383ff40c12SJohn Marino #ifdef CONFIG_HS20
32393ff40c12SJohn Marino 	wpabuf_free(sta->hs20_ie);
32403ff40c12SJohn Marino 	if (elems.hs20 && elems.hs20_len > 4) {
3241*a1157835SDaniel Fojt 		int release;
3242*a1157835SDaniel Fojt 
32433ff40c12SJohn Marino 		sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
32443ff40c12SJohn Marino 						 elems.hs20_len - 4);
3245*a1157835SDaniel Fojt 		release = ((elems.hs20[4] >> 4) & 0x0f) + 1;
3246*a1157835SDaniel Fojt 		if (release >= 2 && !wpa_auth_uses_mfp(sta->wpa_sm)) {
3247*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
3248*a1157835SDaniel Fojt 				   "HS 2.0: PMF not negotiated by release %d station "
3249*a1157835SDaniel Fojt 				   MACSTR, release, MAC2STR(sta->addr));
3250*a1157835SDaniel Fojt 			return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
3251*a1157835SDaniel Fojt 		}
3252*a1157835SDaniel Fojt 	} else {
32533ff40c12SJohn Marino 		sta->hs20_ie = NULL;
3254*a1157835SDaniel Fojt 	}
3255*a1157835SDaniel Fojt 
3256*a1157835SDaniel Fojt 	wpabuf_free(sta->roaming_consortium);
3257*a1157835SDaniel Fojt 	if (elems.roaming_cons_sel)
3258*a1157835SDaniel Fojt 		sta->roaming_consortium = wpabuf_alloc_copy(
3259*a1157835SDaniel Fojt 			elems.roaming_cons_sel + 4,
3260*a1157835SDaniel Fojt 			elems.roaming_cons_sel_len - 4);
3261*a1157835SDaniel Fojt 	else
3262*a1157835SDaniel Fojt 		sta->roaming_consortium = NULL;
32633ff40c12SJohn Marino #endif /* CONFIG_HS20 */
32643ff40c12SJohn Marino 
3265*a1157835SDaniel Fojt #ifdef CONFIG_FST
3266*a1157835SDaniel Fojt 	wpabuf_free(sta->mb_ies);
3267*a1157835SDaniel Fojt 	if (hapd->iface->fst)
3268*a1157835SDaniel Fojt 		sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
3269*a1157835SDaniel Fojt 	else
3270*a1157835SDaniel Fojt 		sta->mb_ies = NULL;
3271*a1157835SDaniel Fojt #endif /* CONFIG_FST */
3272*a1157835SDaniel Fojt 
3273*a1157835SDaniel Fojt #ifdef CONFIG_MBO
3274*a1157835SDaniel Fojt 	mbo_ap_check_sta_assoc(hapd, sta, &elems);
3275*a1157835SDaniel Fojt 
3276*a1157835SDaniel Fojt 	if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
3277*a1157835SDaniel Fojt 	    elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
3278*a1157835SDaniel Fojt 	    hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3279*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
3280*a1157835SDaniel Fojt 			   "MBO: Reject WPA2 association without PMF");
3281*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3282*a1157835SDaniel Fojt 	}
3283*a1157835SDaniel Fojt #endif /* CONFIG_MBO */
3284*a1157835SDaniel Fojt 
3285*a1157835SDaniel Fojt #if defined(CONFIG_FILS) && defined(CONFIG_OCV)
3286*a1157835SDaniel Fojt 	if (wpa_auth_uses_ocv(sta->wpa_sm) &&
3287*a1157835SDaniel Fojt 	    (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3288*a1157835SDaniel Fojt 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3289*a1157835SDaniel Fojt 	     sta->auth_alg == WLAN_AUTH_FILS_PK)) {
3290*a1157835SDaniel Fojt 		struct wpa_channel_info ci;
3291*a1157835SDaniel Fojt 		int tx_chanwidth;
3292*a1157835SDaniel Fojt 		int tx_seg1_idx;
3293*a1157835SDaniel Fojt 
3294*a1157835SDaniel Fojt 		if (hostapd_drv_channel_info(hapd, &ci) != 0) {
3295*a1157835SDaniel Fojt 			wpa_printf(MSG_WARNING,
3296*a1157835SDaniel Fojt 				   "Failed to get channel info to validate received OCI in FILS (Re)Association Request frame");
3297*a1157835SDaniel Fojt 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
3298*a1157835SDaniel Fojt 		}
3299*a1157835SDaniel Fojt 
3300*a1157835SDaniel Fojt 		if (get_sta_tx_parameters(sta->wpa_sm,
3301*a1157835SDaniel Fojt 					  channel_width_to_int(ci.chanwidth),
3302*a1157835SDaniel Fojt 					  ci.seg1_idx, &tx_chanwidth,
3303*a1157835SDaniel Fojt 					  &tx_seg1_idx) < 0)
3304*a1157835SDaniel Fojt 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
3305*a1157835SDaniel Fojt 
3306*a1157835SDaniel Fojt 		if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
3307*a1157835SDaniel Fojt 					 tx_chanwidth, tx_seg1_idx) != 0) {
3308*a1157835SDaniel Fojt 			wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
3309*a1157835SDaniel Fojt 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
3310*a1157835SDaniel Fojt 		}
3311*a1157835SDaniel Fojt 	}
3312*a1157835SDaniel Fojt #endif /* CONFIG_FILS && CONFIG_OCV */
3313*a1157835SDaniel Fojt 
3314*a1157835SDaniel Fojt 	ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
3315*a1157835SDaniel Fojt 				    elems.supp_op_classes_len);
3316*a1157835SDaniel Fojt 
3317*a1157835SDaniel Fojt 	if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
3318*a1157835SDaniel Fojt 	    elems.rrm_enabled &&
3319*a1157835SDaniel Fojt 	    elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
3320*a1157835SDaniel Fojt 		os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
3321*a1157835SDaniel Fojt 			  sizeof(sta->rrm_enabled_capa));
3322*a1157835SDaniel Fojt 
3323*a1157835SDaniel Fojt 	if (elems.power_capab) {
3324*a1157835SDaniel Fojt 		sta->min_tx_power = elems.power_capab[0];
3325*a1157835SDaniel Fojt 		sta->max_tx_power = elems.power_capab[1];
3326*a1157835SDaniel Fojt 		sta->power_capab = 1;
3327*a1157835SDaniel Fojt 	} else {
3328*a1157835SDaniel Fojt 		sta->power_capab = 0;
3329*a1157835SDaniel Fojt 	}
3330*a1157835SDaniel Fojt 
33313ff40c12SJohn Marino 	return WLAN_STATUS_SUCCESS;
33323ff40c12SJohn Marino }
33333ff40c12SJohn Marino 
33343ff40c12SJohn Marino 
send_deauth(struct hostapd_data * hapd,const u8 * addr,u16 reason_code)33353ff40c12SJohn Marino static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
33363ff40c12SJohn Marino 			u16 reason_code)
33373ff40c12SJohn Marino {
33383ff40c12SJohn Marino 	int send_len;
33393ff40c12SJohn Marino 	struct ieee80211_mgmt reply;
33403ff40c12SJohn Marino 
33413ff40c12SJohn Marino 	os_memset(&reply, 0, sizeof(reply));
33423ff40c12SJohn Marino 	reply.frame_control =
33433ff40c12SJohn Marino 		IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
33443ff40c12SJohn Marino 	os_memcpy(reply.da, addr, ETH_ALEN);
33453ff40c12SJohn Marino 	os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
33463ff40c12SJohn Marino 	os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
33473ff40c12SJohn Marino 
33483ff40c12SJohn Marino 	send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
33493ff40c12SJohn Marino 	reply.u.deauth.reason_code = host_to_le16(reason_code);
33503ff40c12SJohn Marino 
33513ff40c12SJohn Marino 	if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
33523ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "Failed to send deauth: %s",
33533ff40c12SJohn Marino 			   strerror(errno));
33543ff40c12SJohn Marino }
33553ff40c12SJohn Marino 
33563ff40c12SJohn Marino 
add_associated_sta(struct hostapd_data * hapd,struct sta_info * sta,int reassoc)3357*a1157835SDaniel Fojt static int add_associated_sta(struct hostapd_data *hapd,
3358*a1157835SDaniel Fojt 			      struct sta_info *sta, int reassoc)
3359*a1157835SDaniel Fojt {
3360*a1157835SDaniel Fojt 	struct ieee80211_ht_capabilities ht_cap;
3361*a1157835SDaniel Fojt 	struct ieee80211_vht_capabilities vht_cap;
3362*a1157835SDaniel Fojt 	struct ieee80211_he_capabilities he_cap;
3363*a1157835SDaniel Fojt 	int set = 1;
3364*a1157835SDaniel Fojt 
3365*a1157835SDaniel Fojt 	/*
3366*a1157835SDaniel Fojt 	 * Remove the STA entry to ensure the STA PS state gets cleared and
3367*a1157835SDaniel Fojt 	 * configuration gets updated. This is relevant for cases, such as
3368*a1157835SDaniel Fojt 	 * FT-over-the-DS, where a station re-associates back to the same AP but
3369*a1157835SDaniel Fojt 	 * skips the authentication flow, or if working with a driver that
3370*a1157835SDaniel Fojt 	 * does not support full AP client state.
3371*a1157835SDaniel Fojt 	 *
3372*a1157835SDaniel Fojt 	 * Skip this if the STA has already completed FT reassociation and the
3373*a1157835SDaniel Fojt 	 * TK has been configured since the TX/RX PN must not be reset to 0 for
3374*a1157835SDaniel Fojt 	 * the same key.
3375*a1157835SDaniel Fojt 	 *
3376*a1157835SDaniel Fojt 	 * FT-over-the-DS has a special case where the STA entry (and as such,
3377*a1157835SDaniel Fojt 	 * the TK) has not yet been configured to the driver depending on which
3378*a1157835SDaniel Fojt 	 * driver interface is used. For that case, allow add-STA operation to
3379*a1157835SDaniel Fojt 	 * be used (instead of set-STA). This is needed to allow mac80211-based
3380*a1157835SDaniel Fojt 	 * drivers to accept the STA parameter configuration. Since this is
3381*a1157835SDaniel Fojt 	 * after a new FT-over-DS exchange, a new TK has been derived, so key
3382*a1157835SDaniel Fojt 	 * reinstallation is not a concern for this case.
3383*a1157835SDaniel Fojt 	 */
3384*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "Add associated STA " MACSTR
3385*a1157835SDaniel Fojt 		   " (added_unassoc=%d auth_alg=%u ft_over_ds=%u reassoc=%d authorized=%d ft_tk=%d fils_tk=%d)",
3386*a1157835SDaniel Fojt 		   MAC2STR(sta->addr), sta->added_unassoc, sta->auth_alg,
3387*a1157835SDaniel Fojt 		   sta->ft_over_ds, reassoc,
3388*a1157835SDaniel Fojt 		   !!(sta->flags & WLAN_STA_AUTHORIZED),
3389*a1157835SDaniel Fojt 		   wpa_auth_sta_ft_tk_already_set(sta->wpa_sm),
3390*a1157835SDaniel Fojt 		   wpa_auth_sta_fils_tk_already_set(sta->wpa_sm));
3391*a1157835SDaniel Fojt 
3392*a1157835SDaniel Fojt 	if (!sta->added_unassoc &&
3393*a1157835SDaniel Fojt 	    (!(sta->flags & WLAN_STA_AUTHORIZED) ||
3394*a1157835SDaniel Fojt 	     (reassoc && sta->ft_over_ds && sta->auth_alg == WLAN_AUTH_FT) ||
3395*a1157835SDaniel Fojt 	     (!wpa_auth_sta_ft_tk_already_set(sta->wpa_sm) &&
3396*a1157835SDaniel Fojt 	      !wpa_auth_sta_fils_tk_already_set(sta->wpa_sm)))) {
3397*a1157835SDaniel Fojt 		hostapd_drv_sta_remove(hapd, sta->addr);
3398*a1157835SDaniel Fojt 		wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
3399*a1157835SDaniel Fojt 		set = 0;
3400*a1157835SDaniel Fojt 
3401*a1157835SDaniel Fojt 		 /* Do not allow the FT-over-DS exception to be used more than
3402*a1157835SDaniel Fojt 		  * once per authentication exchange to guarantee a new TK is
3403*a1157835SDaniel Fojt 		  * used here */
3404*a1157835SDaniel Fojt 		sta->ft_over_ds = 0;
3405*a1157835SDaniel Fojt 	}
3406*a1157835SDaniel Fojt 
3407*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211N
3408*a1157835SDaniel Fojt 	if (sta->flags & WLAN_STA_HT)
3409*a1157835SDaniel Fojt 		hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
3410*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211N */
3411*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211AC
3412*a1157835SDaniel Fojt 	if (sta->flags & WLAN_STA_VHT)
3413*a1157835SDaniel Fojt 		hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
3414*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211AC */
3415*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211AX
3416*a1157835SDaniel Fojt 	if (sta->flags & WLAN_STA_HE) {
3417*a1157835SDaniel Fojt 		hostapd_get_he_capab(hapd, sta->he_capab, &he_cap,
3418*a1157835SDaniel Fojt 				     sta->he_capab_len);
3419*a1157835SDaniel Fojt 	}
3420*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211AX */
3421*a1157835SDaniel Fojt 
3422*a1157835SDaniel Fojt 	/*
3423*a1157835SDaniel Fojt 	 * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
3424*a1157835SDaniel Fojt 	 * will be set when the ACK frame for the (Re)Association Response frame
3425*a1157835SDaniel Fojt 	 * is processed (TX status driver event).
3426*a1157835SDaniel Fojt 	 */
3427*a1157835SDaniel Fojt 	if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
3428*a1157835SDaniel Fojt 			    sta->supported_rates, sta->supported_rates_len,
3429*a1157835SDaniel Fojt 			    sta->listen_interval,
3430*a1157835SDaniel Fojt 			    sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
3431*a1157835SDaniel Fojt 			    sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
3432*a1157835SDaniel Fojt 			    sta->flags & WLAN_STA_HE ? &he_cap : NULL,
3433*a1157835SDaniel Fojt 			    sta->flags & WLAN_STA_HE ? sta->he_capab_len : 0,
3434*a1157835SDaniel Fojt 			    sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
3435*a1157835SDaniel Fojt 			    sta->vht_opmode, sta->p2p_ie ? 1 : 0,
3436*a1157835SDaniel Fojt 			    set)) {
3437*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr,
3438*a1157835SDaniel Fojt 			       HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
3439*a1157835SDaniel Fojt 			       "Could not %s STA to kernel driver",
3440*a1157835SDaniel Fojt 			       set ? "set" : "add");
3441*a1157835SDaniel Fojt 
3442*a1157835SDaniel Fojt 		if (sta->added_unassoc) {
3443*a1157835SDaniel Fojt 			hostapd_drv_sta_remove(hapd, sta->addr);
3444*a1157835SDaniel Fojt 			sta->added_unassoc = 0;
3445*a1157835SDaniel Fojt 		}
3446*a1157835SDaniel Fojt 
3447*a1157835SDaniel Fojt 		return -1;
3448*a1157835SDaniel Fojt 	}
3449*a1157835SDaniel Fojt 
3450*a1157835SDaniel Fojt 	sta->added_unassoc = 0;
3451*a1157835SDaniel Fojt 
3452*a1157835SDaniel Fojt 	return 0;
3453*a1157835SDaniel Fojt }
3454*a1157835SDaniel Fojt 
3455*a1157835SDaniel Fojt 
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)3456*a1157835SDaniel Fojt static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
3457*a1157835SDaniel Fojt 			   const u8 *addr, u16 status_code, int reassoc,
3458*a1157835SDaniel Fojt 			   const u8 *ies, size_t ies_len, int rssi)
34593ff40c12SJohn Marino {
34603ff40c12SJohn Marino 	int send_len;
3461*a1157835SDaniel Fojt 	u8 *buf;
3462*a1157835SDaniel Fojt 	size_t buflen;
34633ff40c12SJohn Marino 	struct ieee80211_mgmt *reply;
34643ff40c12SJohn Marino 	u8 *p;
3465*a1157835SDaniel Fojt 	u16 res = WLAN_STATUS_SUCCESS;
34663ff40c12SJohn Marino 
3467*a1157835SDaniel Fojt 	buflen = sizeof(struct ieee80211_mgmt) + 1024;
3468*a1157835SDaniel Fojt #ifdef CONFIG_FILS
3469*a1157835SDaniel Fojt 	if (sta && sta->fils_hlp_resp)
3470*a1157835SDaniel Fojt 		buflen += wpabuf_len(sta->fils_hlp_resp);
3471*a1157835SDaniel Fojt 	if (sta)
3472*a1157835SDaniel Fojt 		buflen += 150;
3473*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
3474*a1157835SDaniel Fojt #ifdef CONFIG_OWE
3475*a1157835SDaniel Fojt 	if (sta && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
3476*a1157835SDaniel Fojt 		buflen += 150;
3477*a1157835SDaniel Fojt #endif /* CONFIG_OWE */
3478*a1157835SDaniel Fojt #ifdef CONFIG_DPP2
3479*a1157835SDaniel Fojt 	if (sta && sta->dpp_pfs)
3480*a1157835SDaniel Fojt 		buflen += 5 + sta->dpp_pfs->curve->prime_len;
3481*a1157835SDaniel Fojt #endif /* CONFIG_DPP2 */
3482*a1157835SDaniel Fojt 	buf = os_zalloc(buflen);
3483*a1157835SDaniel Fojt 	if (!buf) {
3484*a1157835SDaniel Fojt 		res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3485*a1157835SDaniel Fojt 		goto done;
3486*a1157835SDaniel Fojt 	}
34873ff40c12SJohn Marino 	reply = (struct ieee80211_mgmt *) buf;
34883ff40c12SJohn Marino 	reply->frame_control =
34893ff40c12SJohn Marino 		IEEE80211_FC(WLAN_FC_TYPE_MGMT,
34903ff40c12SJohn Marino 			     (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
34913ff40c12SJohn Marino 			      WLAN_FC_STYPE_ASSOC_RESP));
3492*a1157835SDaniel Fojt 	os_memcpy(reply->da, addr, ETH_ALEN);
34933ff40c12SJohn Marino 	os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
34943ff40c12SJohn Marino 	os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
34953ff40c12SJohn Marino 
34963ff40c12SJohn Marino 	send_len = IEEE80211_HDRLEN;
34973ff40c12SJohn Marino 	send_len += sizeof(reply->u.assoc_resp);
34983ff40c12SJohn Marino 	reply->u.assoc_resp.capab_info =
3499*a1157835SDaniel Fojt 		host_to_le16(hostapd_own_capab_info(hapd));
35003ff40c12SJohn Marino 	reply->u.assoc_resp.status_code = host_to_le16(status_code);
3501*a1157835SDaniel Fojt 
3502*a1157835SDaniel Fojt 	reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0) |
3503*a1157835SDaniel Fojt 					       BIT(14) | BIT(15));
35043ff40c12SJohn Marino 	/* Supported rates */
35053ff40c12SJohn Marino 	p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
35063ff40c12SJohn Marino 	/* Extended supported rates */
35073ff40c12SJohn Marino 	p = hostapd_eid_ext_supp_rates(hapd, p);
35083ff40c12SJohn Marino 
3509*a1157835SDaniel Fojt #ifdef CONFIG_MBO
3510*a1157835SDaniel Fojt 	if (status_code == WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
3511*a1157835SDaniel Fojt 	    rssi != 0) {
3512*a1157835SDaniel Fojt 		int delta = hapd->iconf->rssi_reject_assoc_rssi - rssi;
3513*a1157835SDaniel Fojt 
3514*a1157835SDaniel Fojt 		p = hostapd_eid_mbo_rssi_assoc_rej(hapd, p, buf + buflen - p,
3515*a1157835SDaniel Fojt 						   delta);
3516*a1157835SDaniel Fojt 	}
3517*a1157835SDaniel Fojt #endif /* CONFIG_MBO */
3518*a1157835SDaniel Fojt 
3519*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211R_AP
3520*a1157835SDaniel Fojt 	if (sta && status_code == WLAN_STATUS_SUCCESS) {
35213ff40c12SJohn Marino 		/* IEEE 802.11r: Mobility Domain Information, Fast BSS
35223ff40c12SJohn Marino 		 * Transition Information, RSN, [RIC Response] */
35233ff40c12SJohn Marino 		p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
3524*a1157835SDaniel Fojt 						buf + buflen - p,
35253ff40c12SJohn Marino 						sta->auth_alg, ies, ies_len);
3526*a1157835SDaniel Fojt 		if (!p) {
3527*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
3528*a1157835SDaniel Fojt 				   "FT: Failed to write AssocResp IEs");
3529*a1157835SDaniel Fojt 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3530*a1157835SDaniel Fojt 			goto done;
35313ff40c12SJohn Marino 		}
3532*a1157835SDaniel Fojt 	}
3533*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211R_AP */
3534*a1157835SDaniel Fojt #ifdef CONFIG_FILS
3535*a1157835SDaniel Fojt 	if (sta && status_code == WLAN_STATUS_SUCCESS &&
3536*a1157835SDaniel Fojt 	    (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3537*a1157835SDaniel Fojt 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3538*a1157835SDaniel Fojt 	     sta->auth_alg == WLAN_AUTH_FILS_PK))
3539*a1157835SDaniel Fojt 		p = wpa_auth_write_assoc_resp_fils(sta->wpa_sm, p,
3540*a1157835SDaniel Fojt 						   buf + buflen - p,
3541*a1157835SDaniel Fojt 						   ies, ies_len);
3542*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
3543*a1157835SDaniel Fojt 
3544*a1157835SDaniel Fojt #ifdef CONFIG_OWE
3545*a1157835SDaniel Fojt 	if (sta && status_code == WLAN_STATUS_SUCCESS &&
3546*a1157835SDaniel Fojt 	    (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
3547*a1157835SDaniel Fojt 		p = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, p,
3548*a1157835SDaniel Fojt 						  buf + buflen - p,
3549*a1157835SDaniel Fojt 						  ies, ies_len);
3550*a1157835SDaniel Fojt #endif /* CONFIG_OWE */
35513ff40c12SJohn Marino 
35523ff40c12SJohn Marino #ifdef CONFIG_IEEE80211W
3553*a1157835SDaniel Fojt 	if (sta && status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
35543ff40c12SJohn Marino 		p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
35553ff40c12SJohn Marino #endif /* CONFIG_IEEE80211W */
35563ff40c12SJohn Marino 
35573ff40c12SJohn Marino #ifdef CONFIG_IEEE80211N
35583ff40c12SJohn Marino 	p = hostapd_eid_ht_capabilities(hapd, p);
35593ff40c12SJohn Marino 	p = hostapd_eid_ht_operation(hapd, p);
35603ff40c12SJohn Marino #endif /* CONFIG_IEEE80211N */
35613ff40c12SJohn Marino 
35623ff40c12SJohn Marino #ifdef CONFIG_IEEE80211AC
3563*a1157835SDaniel Fojt 	if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
3564*a1157835SDaniel Fojt 		u32 nsts = 0, sta_nsts;
3565*a1157835SDaniel Fojt 
3566*a1157835SDaniel Fojt 		if (sta && hapd->conf->use_sta_nsts && sta->vht_capabilities) {
3567*a1157835SDaniel Fojt 			struct ieee80211_vht_capabilities *capa;
3568*a1157835SDaniel Fojt 
3569*a1157835SDaniel Fojt 			nsts = (hapd->iface->conf->vht_capab >>
3570*a1157835SDaniel Fojt 				VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
3571*a1157835SDaniel Fojt 			capa = sta->vht_capabilities;
3572*a1157835SDaniel Fojt 			sta_nsts = (le_to_host32(capa->vht_capabilities_info) >>
3573*a1157835SDaniel Fojt 				    VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
3574*a1157835SDaniel Fojt 
3575*a1157835SDaniel Fojt 			if (nsts < sta_nsts)
3576*a1157835SDaniel Fojt 				nsts = 0;
3577*a1157835SDaniel Fojt 			else
3578*a1157835SDaniel Fojt 				nsts = sta_nsts;
3579*a1157835SDaniel Fojt 		}
3580*a1157835SDaniel Fojt 		p = hostapd_eid_vht_capabilities(hapd, p, nsts);
35813ff40c12SJohn Marino 		p = hostapd_eid_vht_operation(hapd, p);
3582*a1157835SDaniel Fojt 	}
35833ff40c12SJohn Marino #endif /* CONFIG_IEEE80211AC */
35843ff40c12SJohn Marino 
3585*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211AX
3586*a1157835SDaniel Fojt 	if (hapd->iconf->ieee80211ax) {
3587*a1157835SDaniel Fojt 		p = hostapd_eid_he_capab(hapd, p, IEEE80211_MODE_AP);
3588*a1157835SDaniel Fojt 		p = hostapd_eid_he_operation(hapd, p);
3589*a1157835SDaniel Fojt 		p = hostapd_eid_spatial_reuse(hapd, p);
3590*a1157835SDaniel Fojt 		p = hostapd_eid_he_mu_edca_parameter_set(hapd, p);
3591*a1157835SDaniel Fojt 	}
3592*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211AX */
3593*a1157835SDaniel Fojt 
35943ff40c12SJohn Marino 	p = hostapd_eid_ext_capab(hapd, p);
35953ff40c12SJohn Marino 	p = hostapd_eid_bss_max_idle_period(hapd, p);
3596*a1157835SDaniel Fojt 	if (sta && sta->qos_map_enabled)
35973ff40c12SJohn Marino 		p = hostapd_eid_qos_map_set(hapd, p);
35983ff40c12SJohn Marino 
3599*a1157835SDaniel Fojt #ifdef CONFIG_FST
3600*a1157835SDaniel Fojt 	if (hapd->iface->fst_ies) {
3601*a1157835SDaniel Fojt 		os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
3602*a1157835SDaniel Fojt 			  wpabuf_len(hapd->iface->fst_ies));
3603*a1157835SDaniel Fojt 		p += wpabuf_len(hapd->iface->fst_ies);
3604*a1157835SDaniel Fojt 	}
3605*a1157835SDaniel Fojt #endif /* CONFIG_FST */
3606*a1157835SDaniel Fojt 
3607*a1157835SDaniel Fojt #ifdef CONFIG_OWE
3608*a1157835SDaniel Fojt 	if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
3609*a1157835SDaniel Fojt 	    sta && sta->owe_ecdh && status_code == WLAN_STATUS_SUCCESS &&
3610*a1157835SDaniel Fojt 	    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE) {
3611*a1157835SDaniel Fojt 		struct wpabuf *pub;
3612*a1157835SDaniel Fojt 
3613*a1157835SDaniel Fojt 		pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
3614*a1157835SDaniel Fojt 		if (!pub) {
3615*a1157835SDaniel Fojt 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3616*a1157835SDaniel Fojt 			goto done;
3617*a1157835SDaniel Fojt 		}
3618*a1157835SDaniel Fojt 		/* OWE Diffie-Hellman Parameter element */
3619*a1157835SDaniel Fojt 		*p++ = WLAN_EID_EXTENSION; /* Element ID */
3620*a1157835SDaniel Fojt 		*p++ = 1 + 2 + wpabuf_len(pub); /* Length */
3621*a1157835SDaniel Fojt 		*p++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension */
3622*a1157835SDaniel Fojt 		WPA_PUT_LE16(p, sta->owe_group);
3623*a1157835SDaniel Fojt 		p += 2;
3624*a1157835SDaniel Fojt 		os_memcpy(p, wpabuf_head(pub), wpabuf_len(pub));
3625*a1157835SDaniel Fojt 		p += wpabuf_len(pub);
3626*a1157835SDaniel Fojt 		wpabuf_free(pub);
3627*a1157835SDaniel Fojt 	}
3628*a1157835SDaniel Fojt #endif /* CONFIG_OWE */
3629*a1157835SDaniel Fojt 
3630*a1157835SDaniel Fojt #ifdef CONFIG_DPP2
3631*a1157835SDaniel Fojt 	if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
3632*a1157835SDaniel Fojt 	    sta && sta->dpp_pfs && status_code == WLAN_STATUS_SUCCESS &&
3633*a1157835SDaniel Fojt 	    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP) {
3634*a1157835SDaniel Fojt 		os_memcpy(p, wpabuf_head(sta->dpp_pfs->ie),
3635*a1157835SDaniel Fojt 			  wpabuf_len(sta->dpp_pfs->ie));
3636*a1157835SDaniel Fojt 		p += wpabuf_len(sta->dpp_pfs->ie);
3637*a1157835SDaniel Fojt 	}
3638*a1157835SDaniel Fojt #endif /* CONFIG_DPP2 */
3639*a1157835SDaniel Fojt 
3640*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211AC
3641*a1157835SDaniel Fojt 	if (sta && hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
3642*a1157835SDaniel Fojt 		p = hostapd_eid_vendor_vht(hapd, p);
3643*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211AC */
3644*a1157835SDaniel Fojt 
3645*a1157835SDaniel Fojt 	if (sta && (sta->flags & WLAN_STA_WMM))
36463ff40c12SJohn Marino 		p = hostapd_eid_wmm(hapd, p);
36473ff40c12SJohn Marino 
36483ff40c12SJohn Marino #ifdef CONFIG_WPS
3649*a1157835SDaniel Fojt 	if (sta &&
3650*a1157835SDaniel Fojt 	    ((sta->flags & WLAN_STA_WPS) ||
3651*a1157835SDaniel Fojt 	     ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa))) {
36523ff40c12SJohn Marino 		struct wpabuf *wps = wps_build_assoc_resp_ie();
36533ff40c12SJohn Marino 		if (wps) {
36543ff40c12SJohn Marino 			os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
36553ff40c12SJohn Marino 			p += wpabuf_len(wps);
36563ff40c12SJohn Marino 			wpabuf_free(wps);
36573ff40c12SJohn Marino 		}
36583ff40c12SJohn Marino 	}
36593ff40c12SJohn Marino #endif /* CONFIG_WPS */
36603ff40c12SJohn Marino 
3661*a1157835SDaniel Fojt 	if (sta && (sta->flags & WLAN_STA_MULTI_AP))
3662*a1157835SDaniel Fojt 		p = hostapd_eid_multi_ap(hapd, p);
3663*a1157835SDaniel Fojt 
36643ff40c12SJohn Marino #ifdef CONFIG_P2P
3665*a1157835SDaniel Fojt 	if (sta && sta->p2p_ie && hapd->p2p_group) {
36663ff40c12SJohn Marino 		struct wpabuf *p2p_resp_ie;
36673ff40c12SJohn Marino 		enum p2p_status_code status;
36683ff40c12SJohn Marino 		switch (status_code) {
36693ff40c12SJohn Marino 		case WLAN_STATUS_SUCCESS:
36703ff40c12SJohn Marino 			status = P2P_SC_SUCCESS;
36713ff40c12SJohn Marino 			break;
36723ff40c12SJohn Marino 		case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
36733ff40c12SJohn Marino 			status = P2P_SC_FAIL_LIMIT_REACHED;
36743ff40c12SJohn Marino 			break;
36753ff40c12SJohn Marino 		default:
36763ff40c12SJohn Marino 			status = P2P_SC_FAIL_INVALID_PARAMS;
36773ff40c12SJohn Marino 			break;
36783ff40c12SJohn Marino 		}
36793ff40c12SJohn Marino 		p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
36803ff40c12SJohn Marino 		if (p2p_resp_ie) {
36813ff40c12SJohn Marino 			os_memcpy(p, wpabuf_head(p2p_resp_ie),
36823ff40c12SJohn Marino 				  wpabuf_len(p2p_resp_ie));
36833ff40c12SJohn Marino 			p += wpabuf_len(p2p_resp_ie);
36843ff40c12SJohn Marino 			wpabuf_free(p2p_resp_ie);
36853ff40c12SJohn Marino 		}
36863ff40c12SJohn Marino 	}
36873ff40c12SJohn Marino #endif /* CONFIG_P2P */
36883ff40c12SJohn Marino 
36893ff40c12SJohn Marino #ifdef CONFIG_P2P_MANAGER
36903ff40c12SJohn Marino 	if (hapd->conf->p2p & P2P_MANAGE)
36913ff40c12SJohn Marino 		p = hostapd_eid_p2p_manage(hapd, p);
36923ff40c12SJohn Marino #endif /* CONFIG_P2P_MANAGER */
36933ff40c12SJohn Marino 
3694*a1157835SDaniel Fojt 	p = hostapd_eid_mbo(hapd, p, buf + buflen - p);
3695*a1157835SDaniel Fojt 
3696*a1157835SDaniel Fojt 	if (hapd->conf->assocresp_elements &&
3697*a1157835SDaniel Fojt 	    (size_t) (buf + buflen - p) >=
3698*a1157835SDaniel Fojt 	    wpabuf_len(hapd->conf->assocresp_elements)) {
3699*a1157835SDaniel Fojt 		os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
3700*a1157835SDaniel Fojt 			  wpabuf_len(hapd->conf->assocresp_elements));
3701*a1157835SDaniel Fojt 		p += wpabuf_len(hapd->conf->assocresp_elements);
3702*a1157835SDaniel Fojt 	}
3703*a1157835SDaniel Fojt 
37043ff40c12SJohn Marino 	send_len += p - reply->u.assoc_resp.variable;
37053ff40c12SJohn Marino 
3706*a1157835SDaniel Fojt #ifdef CONFIG_FILS
3707*a1157835SDaniel Fojt 	if (sta &&
3708*a1157835SDaniel Fojt 	    (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3709*a1157835SDaniel Fojt 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3710*a1157835SDaniel Fojt 	     sta->auth_alg == WLAN_AUTH_FILS_PK) &&
3711*a1157835SDaniel Fojt 	    status_code == WLAN_STATUS_SUCCESS) {
3712*a1157835SDaniel Fojt 		struct ieee802_11_elems elems;
3713*a1157835SDaniel Fojt 
3714*a1157835SDaniel Fojt 		if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
3715*a1157835SDaniel Fojt 		    ParseFailed || !elems.fils_session) {
3716*a1157835SDaniel Fojt 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3717*a1157835SDaniel Fojt 			goto done;
3718*a1157835SDaniel Fojt 		}
3719*a1157835SDaniel Fojt 
3720*a1157835SDaniel Fojt 		/* FILS Session */
3721*a1157835SDaniel Fojt 		*p++ = WLAN_EID_EXTENSION; /* Element ID */
3722*a1157835SDaniel Fojt 		*p++ = 1 + FILS_SESSION_LEN; /* Length */
3723*a1157835SDaniel Fojt 		*p++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
3724*a1157835SDaniel Fojt 		os_memcpy(p, elems.fils_session, FILS_SESSION_LEN);
3725*a1157835SDaniel Fojt 		send_len += 2 + 1 + FILS_SESSION_LEN;
3726*a1157835SDaniel Fojt 
3727*a1157835SDaniel Fojt 		send_len = fils_encrypt_assoc(sta->wpa_sm, buf, send_len,
3728*a1157835SDaniel Fojt 					      buflen, sta->fils_hlp_resp);
3729*a1157835SDaniel Fojt 		if (send_len < 0) {
3730*a1157835SDaniel Fojt 			res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3731*a1157835SDaniel Fojt 			goto done;
3732*a1157835SDaniel Fojt 		}
3733*a1157835SDaniel Fojt 	}
3734*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
3735*a1157835SDaniel Fojt 
3736*a1157835SDaniel Fojt 	if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0) {
37373ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
37383ff40c12SJohn Marino 			   strerror(errno));
3739*a1157835SDaniel Fojt 		res = WLAN_STATUS_UNSPECIFIED_FAILURE;
37403ff40c12SJohn Marino 	}
37413ff40c12SJohn Marino 
3742*a1157835SDaniel Fojt done:
3743*a1157835SDaniel Fojt 	os_free(buf);
3744*a1157835SDaniel Fojt 	return res;
3745*a1157835SDaniel Fojt }
3746*a1157835SDaniel Fojt 
3747*a1157835SDaniel Fojt 
3748*a1157835SDaniel Fojt #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 * reason)3749*a1157835SDaniel Fojt u8 * owe_assoc_req_process(struct hostapd_data *hapd, struct sta_info *sta,
3750*a1157835SDaniel Fojt 			   const u8 *owe_dh, u8 owe_dh_len,
3751*a1157835SDaniel Fojt 			   u8 *owe_buf, size_t owe_buf_len, u16 *reason)
3752*a1157835SDaniel Fojt {
3753*a1157835SDaniel Fojt #ifdef CONFIG_TESTING_OPTIONS
3754*a1157835SDaniel Fojt 	if (hapd->conf->own_ie_override) {
3755*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "OWE: Using IE override");
3756*a1157835SDaniel Fojt 		*reason = WLAN_STATUS_SUCCESS;
3757*a1157835SDaniel Fojt 		return wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3758*a1157835SDaniel Fojt 						     owe_buf_len, NULL, 0);
3759*a1157835SDaniel Fojt 	}
3760*a1157835SDaniel Fojt #endif /* CONFIG_TESTING_OPTIONS */
3761*a1157835SDaniel Fojt 
3762*a1157835SDaniel Fojt 	if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
3763*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
3764*a1157835SDaniel Fojt 		owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3765*a1157835SDaniel Fojt 							owe_buf_len, NULL, 0);
3766*a1157835SDaniel Fojt 		*reason = WLAN_STATUS_SUCCESS;
3767*a1157835SDaniel Fojt 		return owe_buf;
3768*a1157835SDaniel Fojt 	}
3769*a1157835SDaniel Fojt 
3770*a1157835SDaniel Fojt 	if (sta->owe_pmk && sta->external_dh_updated) {
3771*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "OWE: Using previously derived PMK");
3772*a1157835SDaniel Fojt 		*reason = WLAN_STATUS_SUCCESS;
3773*a1157835SDaniel Fojt 		return owe_buf;
3774*a1157835SDaniel Fojt 	}
3775*a1157835SDaniel Fojt 
3776*a1157835SDaniel Fojt 	*reason = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
3777*a1157835SDaniel Fojt 	if (*reason != WLAN_STATUS_SUCCESS)
3778*a1157835SDaniel Fojt 		return NULL;
3779*a1157835SDaniel Fojt 
3780*a1157835SDaniel Fojt 	owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3781*a1157835SDaniel Fojt 						owe_buf_len, NULL, 0);
3782*a1157835SDaniel Fojt 
3783*a1157835SDaniel Fojt 	if (sta->owe_ecdh && owe_buf) {
3784*a1157835SDaniel Fojt 		struct wpabuf *pub;
3785*a1157835SDaniel Fojt 
3786*a1157835SDaniel Fojt 		pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
3787*a1157835SDaniel Fojt 		if (!pub) {
3788*a1157835SDaniel Fojt 			*reason = WLAN_STATUS_UNSPECIFIED_FAILURE;
3789*a1157835SDaniel Fojt 			return owe_buf;
3790*a1157835SDaniel Fojt 		}
3791*a1157835SDaniel Fojt 
3792*a1157835SDaniel Fojt 		/* OWE Diffie-Hellman Parameter element */
3793*a1157835SDaniel Fojt 		*owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
3794*a1157835SDaniel Fojt 		*owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
3795*a1157835SDaniel Fojt 		*owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
3796*a1157835SDaniel Fojt 							 */
3797*a1157835SDaniel Fojt 		WPA_PUT_LE16(owe_buf, sta->owe_group);
3798*a1157835SDaniel Fojt 		owe_buf += 2;
3799*a1157835SDaniel Fojt 		os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
3800*a1157835SDaniel Fojt 		owe_buf += wpabuf_len(pub);
3801*a1157835SDaniel Fojt 		wpabuf_free(pub);
3802*a1157835SDaniel Fojt 	}
3803*a1157835SDaniel Fojt 
3804*a1157835SDaniel Fojt 	return owe_buf;
3805*a1157835SDaniel Fojt }
3806*a1157835SDaniel Fojt #endif /* CONFIG_OWE */
3807*a1157835SDaniel Fojt 
3808*a1157835SDaniel Fojt 
3809*a1157835SDaniel Fojt #ifdef CONFIG_FILS
3810*a1157835SDaniel Fojt 
fils_hlp_finish_assoc(struct hostapd_data * hapd,struct sta_info * sta)3811*a1157835SDaniel Fojt void fils_hlp_finish_assoc(struct hostapd_data *hapd, struct sta_info *sta)
3812*a1157835SDaniel Fojt {
3813*a1157835SDaniel Fojt 	u16 reply_res;
3814*a1157835SDaniel Fojt 
3815*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "FILS: Finish association with " MACSTR,
3816*a1157835SDaniel Fojt 		   MAC2STR(sta->addr));
3817*a1157835SDaniel Fojt 	eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
3818*a1157835SDaniel Fojt 	if (!sta->fils_pending_assoc_req)
3819*a1157835SDaniel Fojt 		return;
3820*a1157835SDaniel Fojt 	reply_res = send_assoc_resp(hapd, sta, sta->addr, WLAN_STATUS_SUCCESS,
3821*a1157835SDaniel Fojt 				    sta->fils_pending_assoc_is_reassoc,
3822*a1157835SDaniel Fojt 				    sta->fils_pending_assoc_req,
3823*a1157835SDaniel Fojt 				    sta->fils_pending_assoc_req_len, 0);
3824*a1157835SDaniel Fojt 	os_free(sta->fils_pending_assoc_req);
3825*a1157835SDaniel Fojt 	sta->fils_pending_assoc_req = NULL;
3826*a1157835SDaniel Fojt 	sta->fils_pending_assoc_req_len = 0;
3827*a1157835SDaniel Fojt 	wpabuf_free(sta->fils_hlp_resp);
3828*a1157835SDaniel Fojt 	sta->fils_hlp_resp = NULL;
3829*a1157835SDaniel Fojt 	wpabuf_free(sta->hlp_dhcp_discover);
3830*a1157835SDaniel Fojt 	sta->hlp_dhcp_discover = NULL;
3831*a1157835SDaniel Fojt 
3832*a1157835SDaniel Fojt 	/*
3833*a1157835SDaniel Fojt 	 * Remove the station in case transmission of a success response fails.
3834*a1157835SDaniel Fojt 	 * At this point the station was already added associated to the driver.
3835*a1157835SDaniel Fojt 	 */
3836*a1157835SDaniel Fojt 	if (reply_res != WLAN_STATUS_SUCCESS)
3837*a1157835SDaniel Fojt 		hostapd_drv_sta_remove(hapd, sta->addr);
3838*a1157835SDaniel Fojt }
3839*a1157835SDaniel Fojt 
3840*a1157835SDaniel Fojt 
fils_hlp_timeout(void * eloop_ctx,void * eloop_data)3841*a1157835SDaniel Fojt void fils_hlp_timeout(void *eloop_ctx, void *eloop_data)
3842*a1157835SDaniel Fojt {
3843*a1157835SDaniel Fojt 	struct hostapd_data *hapd = eloop_ctx;
3844*a1157835SDaniel Fojt 	struct sta_info *sta = eloop_data;
3845*a1157835SDaniel Fojt 
3846*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
3847*a1157835SDaniel Fojt 		   "FILS: HLP response timeout - continue with association response for "
3848*a1157835SDaniel Fojt 		   MACSTR, MAC2STR(sta->addr));
3849*a1157835SDaniel Fojt 	if (sta->fils_drv_assoc_finish)
3850*a1157835SDaniel Fojt 		hostapd_notify_assoc_fils_finish(hapd, sta);
3851*a1157835SDaniel Fojt 	else
3852*a1157835SDaniel Fojt 		fils_hlp_finish_assoc(hapd, sta);
3853*a1157835SDaniel Fojt }
3854*a1157835SDaniel Fojt 
3855*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
3856*a1157835SDaniel Fojt 
38573ff40c12SJohn Marino 
handle_assoc(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int reassoc,int rssi)38583ff40c12SJohn Marino static void handle_assoc(struct hostapd_data *hapd,
38593ff40c12SJohn Marino 			 const struct ieee80211_mgmt *mgmt, size_t len,
3860*a1157835SDaniel Fojt 			 int reassoc, int rssi)
38613ff40c12SJohn Marino {
3862*a1157835SDaniel Fojt 	u16 capab_info, listen_interval, seq_ctrl, fc;
3863*a1157835SDaniel Fojt 	u16 resp = WLAN_STATUS_SUCCESS, reply_res;
38643ff40c12SJohn Marino 	const u8 *pos;
38653ff40c12SJohn Marino 	int left, i;
38663ff40c12SJohn Marino 	struct sta_info *sta;
3867*a1157835SDaniel Fojt 	u8 *tmp = NULL;
3868*a1157835SDaniel Fojt 	struct hostapd_sta_wpa_psk_short *psk = NULL;
3869*a1157835SDaniel Fojt 	char *identity = NULL;
3870*a1157835SDaniel Fojt 	char *radius_cui = NULL;
3871*a1157835SDaniel Fojt #ifdef CONFIG_FILS
3872*a1157835SDaniel Fojt 	int delay_assoc = 0;
3873*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
38743ff40c12SJohn Marino 
38753ff40c12SJohn Marino 	if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
38763ff40c12SJohn Marino 				      sizeof(mgmt->u.assoc_req))) {
38773ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
38783ff40c12SJohn Marino 			   reassoc, (unsigned long) len);
38793ff40c12SJohn Marino 		return;
38803ff40c12SJohn Marino 	}
38813ff40c12SJohn Marino 
38823ff40c12SJohn Marino #ifdef CONFIG_TESTING_OPTIONS
38833ff40c12SJohn Marino 	if (reassoc) {
3884*a1157835SDaniel Fojt 		if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
38853ff40c12SJohn Marino 		    drand48() < hapd->iconf->ignore_reassoc_probability) {
38863ff40c12SJohn Marino 			wpa_printf(MSG_INFO,
38873ff40c12SJohn Marino 				   "TESTING: ignoring reassoc request from "
38883ff40c12SJohn Marino 				   MACSTR, MAC2STR(mgmt->sa));
38893ff40c12SJohn Marino 			return;
38903ff40c12SJohn Marino 		}
38913ff40c12SJohn Marino 	} else {
3892*a1157835SDaniel Fojt 		if (hapd->iconf->ignore_assoc_probability > 0.0 &&
38933ff40c12SJohn Marino 		    drand48() < hapd->iconf->ignore_assoc_probability) {
38943ff40c12SJohn Marino 			wpa_printf(MSG_INFO,
38953ff40c12SJohn Marino 				   "TESTING: ignoring assoc request from "
38963ff40c12SJohn Marino 				   MACSTR, MAC2STR(mgmt->sa));
38973ff40c12SJohn Marino 			return;
38983ff40c12SJohn Marino 		}
38993ff40c12SJohn Marino 	}
39003ff40c12SJohn Marino #endif /* CONFIG_TESTING_OPTIONS */
39013ff40c12SJohn Marino 
3902*a1157835SDaniel Fojt 	fc = le_to_host16(mgmt->frame_control);
3903*a1157835SDaniel Fojt 	seq_ctrl = le_to_host16(mgmt->seq_ctrl);
3904*a1157835SDaniel Fojt 
39053ff40c12SJohn Marino 	if (reassoc) {
39063ff40c12SJohn Marino 		capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
39073ff40c12SJohn Marino 		listen_interval = le_to_host16(
39083ff40c12SJohn Marino 			mgmt->u.reassoc_req.listen_interval);
39093ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
39103ff40c12SJohn Marino 			   " capab_info=0x%02x listen_interval=%d current_ap="
3911*a1157835SDaniel Fojt 			   MACSTR " seq_ctrl=0x%x%s",
39123ff40c12SJohn Marino 			   MAC2STR(mgmt->sa), capab_info, listen_interval,
3913*a1157835SDaniel Fojt 			   MAC2STR(mgmt->u.reassoc_req.current_ap),
3914*a1157835SDaniel Fojt 			   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
39153ff40c12SJohn Marino 		left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
39163ff40c12SJohn Marino 		pos = mgmt->u.reassoc_req.variable;
39173ff40c12SJohn Marino 	} else {
39183ff40c12SJohn Marino 		capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
39193ff40c12SJohn Marino 		listen_interval = le_to_host16(
39203ff40c12SJohn Marino 			mgmt->u.assoc_req.listen_interval);
39213ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
3922*a1157835SDaniel Fojt 			   " capab_info=0x%02x listen_interval=%d "
3923*a1157835SDaniel Fojt 			   "seq_ctrl=0x%x%s",
3924*a1157835SDaniel Fojt 			   MAC2STR(mgmt->sa), capab_info, listen_interval,
3925*a1157835SDaniel Fojt 			   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
39263ff40c12SJohn Marino 		left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
39273ff40c12SJohn Marino 		pos = mgmt->u.assoc_req.variable;
39283ff40c12SJohn Marino 	}
39293ff40c12SJohn Marino 
39303ff40c12SJohn Marino 	sta = ap_get_sta(hapd, mgmt->sa);
3931*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211R_AP
39323ff40c12SJohn Marino 	if (sta && sta->auth_alg == WLAN_AUTH_FT &&
39333ff40c12SJohn Marino 	    (sta->flags & WLAN_STA_AUTH) == 0) {
39343ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
39353ff40c12SJohn Marino 			   "prior to authentication since it is using "
39363ff40c12SJohn Marino 			   "over-the-DS FT", MAC2STR(mgmt->sa));
3937*a1157835SDaniel Fojt 
3938*a1157835SDaniel Fojt 		/*
3939*a1157835SDaniel Fojt 		 * Mark station as authenticated, to avoid adding station
3940*a1157835SDaniel Fojt 		 * entry in the driver as associated and not authenticated
3941*a1157835SDaniel Fojt 		 */
3942*a1157835SDaniel Fojt 		sta->flags |= WLAN_STA_AUTH;
39433ff40c12SJohn Marino 	} else
3944*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211R_AP */
39453ff40c12SJohn Marino 	if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
3946*a1157835SDaniel Fojt 		if (hapd->iface->current_mode &&
3947*a1157835SDaniel Fojt 		    hapd->iface->current_mode->mode ==
3948*a1157835SDaniel Fojt 			HOSTAPD_MODE_IEEE80211AD) {
3949*a1157835SDaniel Fojt 			int acl_res;
3950*a1157835SDaniel Fojt 			u32 session_timeout, acct_interim_interval;
3951*a1157835SDaniel Fojt 			struct vlan_description vlan_id;
3952*a1157835SDaniel Fojt 
3953*a1157835SDaniel Fojt 			acl_res = ieee802_11_allowed_address(
3954*a1157835SDaniel Fojt 				hapd, mgmt->sa, (const u8 *) mgmt, len,
3955*a1157835SDaniel Fojt 				&session_timeout, &acct_interim_interval,
3956*a1157835SDaniel Fojt 				&vlan_id, &psk, &identity, &radius_cui, 0);
3957*a1157835SDaniel Fojt 			if (acl_res == HOSTAPD_ACL_REJECT) {
3958*a1157835SDaniel Fojt 				wpa_msg(hapd->msg_ctx, MSG_DEBUG,
3959*a1157835SDaniel Fojt 					"Ignore Association Request frame from "
3960*a1157835SDaniel Fojt 					MACSTR " due to ACL reject",
3961*a1157835SDaniel Fojt 					MAC2STR(mgmt->sa));
3962*a1157835SDaniel Fojt 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3963*a1157835SDaniel Fojt 				goto fail;
3964*a1157835SDaniel Fojt 			}
3965*a1157835SDaniel Fojt 			if (acl_res == HOSTAPD_ACL_PENDING)
3966*a1157835SDaniel Fojt 				return;
3967*a1157835SDaniel Fojt 
3968*a1157835SDaniel Fojt 			/* DMG/IEEE 802.11ad does not use authentication.
3969*a1157835SDaniel Fojt 			 * Allocate sta entry upon association. */
3970*a1157835SDaniel Fojt 			sta = ap_sta_add(hapd, mgmt->sa);
3971*a1157835SDaniel Fojt 			if (!sta) {
3972*a1157835SDaniel Fojt 				hostapd_logger(hapd, mgmt->sa,
3973*a1157835SDaniel Fojt 					       HOSTAPD_MODULE_IEEE80211,
3974*a1157835SDaniel Fojt 					       HOSTAPD_LEVEL_INFO,
3975*a1157835SDaniel Fojt 					       "Failed to add STA");
3976*a1157835SDaniel Fojt 				resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
3977*a1157835SDaniel Fojt 				goto fail;
3978*a1157835SDaniel Fojt 			}
3979*a1157835SDaniel Fojt 
3980*a1157835SDaniel Fojt 			acl_res = ieee802_11_set_radius_info(
3981*a1157835SDaniel Fojt 				hapd, sta, acl_res, session_timeout,
3982*a1157835SDaniel Fojt 				acct_interim_interval, &vlan_id, &psk,
3983*a1157835SDaniel Fojt 				&identity, &radius_cui);
3984*a1157835SDaniel Fojt 			if (acl_res) {
3985*a1157835SDaniel Fojt 				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3986*a1157835SDaniel Fojt 				goto fail;
3987*a1157835SDaniel Fojt 			}
3988*a1157835SDaniel Fojt 
3989*a1157835SDaniel Fojt 			hostapd_logger(hapd, sta->addr,
3990*a1157835SDaniel Fojt 				       HOSTAPD_MODULE_IEEE80211,
3991*a1157835SDaniel Fojt 				       HOSTAPD_LEVEL_DEBUG,
3992*a1157835SDaniel Fojt 				       "Skip authentication for DMG/IEEE 802.11ad");
3993*a1157835SDaniel Fojt 			sta->flags |= WLAN_STA_AUTH;
3994*a1157835SDaniel Fojt 			wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
3995*a1157835SDaniel Fojt 			sta->auth_alg = WLAN_AUTH_OPEN;
3996*a1157835SDaniel Fojt 		} else {
3997*a1157835SDaniel Fojt 			hostapd_logger(hapd, mgmt->sa,
3998*a1157835SDaniel Fojt 				       HOSTAPD_MODULE_IEEE80211,
3999*a1157835SDaniel Fojt 				       HOSTAPD_LEVEL_INFO,
4000*a1157835SDaniel Fojt 				       "Station tried to associate before authentication (aid=%d flags=0x%x)",
40013ff40c12SJohn Marino 				       sta ? sta->aid : -1,
40023ff40c12SJohn Marino 				       sta ? sta->flags : 0);
40033ff40c12SJohn Marino 			send_deauth(hapd, mgmt->sa,
40043ff40c12SJohn Marino 				    WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
40053ff40c12SJohn Marino 			return;
40063ff40c12SJohn Marino 		}
4007*a1157835SDaniel Fojt 	}
4008*a1157835SDaniel Fojt 
4009*a1157835SDaniel Fojt 	if ((fc & WLAN_FC_RETRY) &&
4010*a1157835SDaniel Fojt 	    sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4011*a1157835SDaniel Fojt 	    sta->last_seq_ctrl == seq_ctrl &&
4012*a1157835SDaniel Fojt 	    sta->last_subtype == (reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
4013*a1157835SDaniel Fojt 				  WLAN_FC_STYPE_ASSOC_REQ)) {
4014*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4015*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_DEBUG,
4016*a1157835SDaniel Fojt 			       "Drop repeated association frame seq_ctrl=0x%x",
4017*a1157835SDaniel Fojt 			       seq_ctrl);
4018*a1157835SDaniel Fojt 		return;
4019*a1157835SDaniel Fojt 	}
4020*a1157835SDaniel Fojt 	sta->last_seq_ctrl = seq_ctrl;
4021*a1157835SDaniel Fojt 	sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
4022*a1157835SDaniel Fojt 		WLAN_FC_STYPE_ASSOC_REQ;
40233ff40c12SJohn Marino 
40243ff40c12SJohn Marino 	if (hapd->tkip_countermeasures) {
4025*a1157835SDaniel Fojt 		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
40263ff40c12SJohn Marino 		goto fail;
40273ff40c12SJohn Marino 	}
40283ff40c12SJohn Marino 
40293ff40c12SJohn Marino 	if (listen_interval > hapd->conf->max_listen_interval) {
40303ff40c12SJohn Marino 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
40313ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
40323ff40c12SJohn Marino 			       "Too large Listen Interval (%d)",
40333ff40c12SJohn Marino 			       listen_interval);
40343ff40c12SJohn Marino 		resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
40353ff40c12SJohn Marino 		goto fail;
40363ff40c12SJohn Marino 	}
40373ff40c12SJohn Marino 
4038*a1157835SDaniel Fojt #ifdef CONFIG_MBO
4039*a1157835SDaniel Fojt 	if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
4040*a1157835SDaniel Fojt 		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4041*a1157835SDaniel Fojt 		goto fail;
4042*a1157835SDaniel Fojt 	}
4043*a1157835SDaniel Fojt 
4044*a1157835SDaniel Fojt 	if (hapd->iconf->rssi_reject_assoc_rssi && rssi &&
4045*a1157835SDaniel Fojt 	    rssi < hapd->iconf->rssi_reject_assoc_rssi &&
4046*a1157835SDaniel Fojt 	    (sta->auth_rssi == 0 ||
4047*a1157835SDaniel Fojt 	     sta->auth_rssi < hapd->iconf->rssi_reject_assoc_rssi)) {
4048*a1157835SDaniel Fojt 		resp = WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS;
4049*a1157835SDaniel Fojt 		goto fail;
4050*a1157835SDaniel Fojt 	}
4051*a1157835SDaniel Fojt #endif /* CONFIG_MBO */
4052*a1157835SDaniel Fojt 
4053*a1157835SDaniel Fojt 	/*
4054*a1157835SDaniel Fojt 	 * sta->capability is used in check_assoc_ies() for RRM enabled
4055*a1157835SDaniel Fojt 	 * capability element.
4056*a1157835SDaniel Fojt 	 */
4057*a1157835SDaniel Fojt 	sta->capability = capab_info;
4058*a1157835SDaniel Fojt 
4059*a1157835SDaniel Fojt #ifdef CONFIG_FILS
4060*a1157835SDaniel Fojt 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4061*a1157835SDaniel Fojt 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4062*a1157835SDaniel Fojt 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
4063*a1157835SDaniel Fojt 		int res;
4064*a1157835SDaniel Fojt 
4065*a1157835SDaniel Fojt 		/* The end of the payload is encrypted. Need to decrypt it
4066*a1157835SDaniel Fojt 		 * before parsing. */
4067*a1157835SDaniel Fojt 
4068*a1157835SDaniel Fojt 		tmp = os_memdup(pos, left);
4069*a1157835SDaniel Fojt 		if (!tmp) {
4070*a1157835SDaniel Fojt 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4071*a1157835SDaniel Fojt 			goto fail;
4072*a1157835SDaniel Fojt 		}
4073*a1157835SDaniel Fojt 
4074*a1157835SDaniel Fojt 		res = fils_decrypt_assoc(sta->wpa_sm, sta->fils_session, mgmt,
4075*a1157835SDaniel Fojt 					 len, tmp, left);
4076*a1157835SDaniel Fojt 		if (res < 0) {
4077*a1157835SDaniel Fojt 			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4078*a1157835SDaniel Fojt 			goto fail;
4079*a1157835SDaniel Fojt 		}
4080*a1157835SDaniel Fojt 		pos = tmp;
4081*a1157835SDaniel Fojt 		left = res;
4082*a1157835SDaniel Fojt 	}
4083*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
4084*a1157835SDaniel Fojt 
40853ff40c12SJohn Marino 	/* followed by SSID and Supported rates; and HT capabilities if 802.11n
40863ff40c12SJohn Marino 	 * is used */
40873ff40c12SJohn Marino 	resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
40883ff40c12SJohn Marino 	if (resp != WLAN_STATUS_SUCCESS)
40893ff40c12SJohn Marino 		goto fail;
40903ff40c12SJohn Marino 
40913ff40c12SJohn Marino 	if (hostapd_get_aid(hapd, sta) < 0) {
40923ff40c12SJohn Marino 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
40933ff40c12SJohn Marino 			       HOSTAPD_LEVEL_INFO, "No room for more AIDs");
40943ff40c12SJohn Marino 		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
40953ff40c12SJohn Marino 		goto fail;
40963ff40c12SJohn Marino 	}
40973ff40c12SJohn Marino 
40983ff40c12SJohn Marino 	sta->listen_interval = listen_interval;
40993ff40c12SJohn Marino 
4100*a1157835SDaniel Fojt 	if (hapd->iface->current_mode &&
4101*a1157835SDaniel Fojt 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
41023ff40c12SJohn Marino 		sta->flags |= WLAN_STA_NONERP;
41033ff40c12SJohn Marino 	for (i = 0; i < sta->supported_rates_len; i++) {
41043ff40c12SJohn Marino 		if ((sta->supported_rates[i] & 0x7f) > 22) {
41053ff40c12SJohn Marino 			sta->flags &= ~WLAN_STA_NONERP;
41063ff40c12SJohn Marino 			break;
41073ff40c12SJohn Marino 		}
41083ff40c12SJohn Marino 	}
41093ff40c12SJohn Marino 	if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
41103ff40c12SJohn Marino 		sta->nonerp_set = 1;
41113ff40c12SJohn Marino 		hapd->iface->num_sta_non_erp++;
41123ff40c12SJohn Marino 		if (hapd->iface->num_sta_non_erp == 1)
41133ff40c12SJohn Marino 			ieee802_11_set_beacons(hapd->iface);
41143ff40c12SJohn Marino 	}
41153ff40c12SJohn Marino 
41163ff40c12SJohn Marino 	if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
41173ff40c12SJohn Marino 	    !sta->no_short_slot_time_set) {
41183ff40c12SJohn Marino 		sta->no_short_slot_time_set = 1;
41193ff40c12SJohn Marino 		hapd->iface->num_sta_no_short_slot_time++;
4120*a1157835SDaniel Fojt 		if (hapd->iface->current_mode &&
4121*a1157835SDaniel Fojt 		    hapd->iface->current_mode->mode ==
41223ff40c12SJohn Marino 		    HOSTAPD_MODE_IEEE80211G &&
41233ff40c12SJohn Marino 		    hapd->iface->num_sta_no_short_slot_time == 1)
41243ff40c12SJohn Marino 			ieee802_11_set_beacons(hapd->iface);
41253ff40c12SJohn Marino 	}
41263ff40c12SJohn Marino 
41273ff40c12SJohn Marino 	if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
41283ff40c12SJohn Marino 		sta->flags |= WLAN_STA_SHORT_PREAMBLE;
41293ff40c12SJohn Marino 	else
41303ff40c12SJohn Marino 		sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
41313ff40c12SJohn Marino 
41323ff40c12SJohn Marino 	if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
41333ff40c12SJohn Marino 	    !sta->no_short_preamble_set) {
41343ff40c12SJohn Marino 		sta->no_short_preamble_set = 1;
41353ff40c12SJohn Marino 		hapd->iface->num_sta_no_short_preamble++;
4136*a1157835SDaniel Fojt 		if (hapd->iface->current_mode &&
4137*a1157835SDaniel Fojt 		    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
41383ff40c12SJohn Marino 		    && hapd->iface->num_sta_no_short_preamble == 1)
41393ff40c12SJohn Marino 			ieee802_11_set_beacons(hapd->iface);
41403ff40c12SJohn Marino 	}
41413ff40c12SJohn Marino 
41423ff40c12SJohn Marino #ifdef CONFIG_IEEE80211N
41433ff40c12SJohn Marino 	update_ht_state(hapd, sta);
41443ff40c12SJohn Marino #endif /* CONFIG_IEEE80211N */
41453ff40c12SJohn Marino 
41463ff40c12SJohn Marino 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
41473ff40c12SJohn Marino 		       HOSTAPD_LEVEL_DEBUG,
41483ff40c12SJohn Marino 		       "association OK (aid %d)", sta->aid);
41493ff40c12SJohn Marino 	/* Station will be marked associated, after it acknowledges AssocResp
41503ff40c12SJohn Marino 	 */
41513ff40c12SJohn Marino 	sta->flags |= WLAN_STA_ASSOC_REQ_OK;
41523ff40c12SJohn Marino 
41533ff40c12SJohn Marino #ifdef CONFIG_IEEE80211W
41543ff40c12SJohn Marino 	if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
41553ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
41563ff40c12SJohn Marino 			   "SA Query procedure", reassoc ? "re" : "");
41573ff40c12SJohn Marino 		/* TODO: Send a protected Disassociate frame to the STA using
41583ff40c12SJohn Marino 		 * the old key and Reason Code "Previous Authentication no
41593ff40c12SJohn Marino 		 * longer valid". Make sure this is only sent protected since
41603ff40c12SJohn Marino 		 * unprotected frame would be received by the STA that is now
41613ff40c12SJohn Marino 		 * trying to associate.
41623ff40c12SJohn Marino 		 */
41633ff40c12SJohn Marino 	}
41643ff40c12SJohn Marino #endif /* CONFIG_IEEE80211W */
41653ff40c12SJohn Marino 
41663ff40c12SJohn Marino 	/* Make sure that the previously registered inactivity timer will not
41673ff40c12SJohn Marino 	 * remove the STA immediately. */
41683ff40c12SJohn Marino 	sta->timeout_next = STA_NULLFUNC;
41693ff40c12SJohn Marino 
4170*a1157835SDaniel Fojt #ifdef CONFIG_TAXONOMY
4171*a1157835SDaniel Fojt 	taxonomy_sta_info_assoc_req(hapd, sta, pos, left);
4172*a1157835SDaniel Fojt #endif /* CONFIG_TAXONOMY */
4173*a1157835SDaniel Fojt 
4174*a1157835SDaniel Fojt 	sta->pending_wds_enable = 0;
4175*a1157835SDaniel Fojt 
4176*a1157835SDaniel Fojt #ifdef CONFIG_FILS
4177*a1157835SDaniel Fojt 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4178*a1157835SDaniel Fojt 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4179*a1157835SDaniel Fojt 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
4180*a1157835SDaniel Fojt 		if (fils_process_hlp(hapd, sta, pos, left) > 0)
4181*a1157835SDaniel Fojt 			delay_assoc = 1;
4182*a1157835SDaniel Fojt 	}
4183*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
4184*a1157835SDaniel Fojt 
41853ff40c12SJohn Marino  fail:
4186*a1157835SDaniel Fojt 	os_free(identity);
4187*a1157835SDaniel Fojt 	os_free(radius_cui);
4188*a1157835SDaniel Fojt 	hostapd_free_psk_list(psk);
4189*a1157835SDaniel Fojt 
4190*a1157835SDaniel Fojt 	/*
4191*a1157835SDaniel Fojt 	 * In case of a successful response, add the station to the driver.
4192*a1157835SDaniel Fojt 	 * Otherwise, the kernel may ignore Data frames before we process the
4193*a1157835SDaniel Fojt 	 * ACK frame (TX status). In case of a failure, this station will be
4194*a1157835SDaniel Fojt 	 * removed.
4195*a1157835SDaniel Fojt 	 *
4196*a1157835SDaniel Fojt 	 * Note that this is not compliant with the IEEE 802.11 standard that
4197*a1157835SDaniel Fojt 	 * states that a non-AP station should transition into the
4198*a1157835SDaniel Fojt 	 * authenticated/associated state only after the station acknowledges
4199*a1157835SDaniel Fojt 	 * the (Re)Association Response frame. However, still do this as:
4200*a1157835SDaniel Fojt 	 *
4201*a1157835SDaniel Fojt 	 * 1. In case the station does not acknowledge the (Re)Association
4202*a1157835SDaniel Fojt 	 *    Response frame, it will be removed.
4203*a1157835SDaniel Fojt 	 * 2. Data frames will be dropped in the kernel until the station is
4204*a1157835SDaniel Fojt 	 *    set into authorized state, and there are no significant known
4205*a1157835SDaniel Fojt 	 *    issues with processing other non-Data Class 3 frames during this
4206*a1157835SDaniel Fojt 	 *    window.
4207*a1157835SDaniel Fojt 	 */
4208*a1157835SDaniel Fojt 	if (resp == WLAN_STATUS_SUCCESS && sta &&
4209*a1157835SDaniel Fojt 	    add_associated_sta(hapd, sta, reassoc))
4210*a1157835SDaniel Fojt 		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4211*a1157835SDaniel Fojt 
4212*a1157835SDaniel Fojt #ifdef CONFIG_FILS
4213*a1157835SDaniel Fojt 	if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS &&
4214*a1157835SDaniel Fojt 	    eloop_is_timeout_registered(fils_hlp_timeout, hapd, sta) &&
4215*a1157835SDaniel Fojt 	    sta->fils_pending_assoc_req) {
4216*a1157835SDaniel Fojt 		/* Do not reschedule fils_hlp_timeout in case the station
4217*a1157835SDaniel Fojt 		 * retransmits (Re)Association Request frame while waiting for
4218*a1157835SDaniel Fojt 		 * the previously started FILS HLP wait, so that the timeout can
4219*a1157835SDaniel Fojt 		 * be determined from the first pending attempt. */
4220*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
4221*a1157835SDaniel Fojt 			   "FILS: Continue waiting for HLP processing before sending (Re)Association Response frame to "
4222*a1157835SDaniel Fojt 			   MACSTR, MAC2STR(sta->addr));
4223*a1157835SDaniel Fojt 		os_free(tmp);
4224*a1157835SDaniel Fojt 		return;
4225*a1157835SDaniel Fojt 	}
4226*a1157835SDaniel Fojt 	if (sta) {
4227*a1157835SDaniel Fojt 		eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4228*a1157835SDaniel Fojt 		os_free(sta->fils_pending_assoc_req);
4229*a1157835SDaniel Fojt 		sta->fils_pending_assoc_req = NULL;
4230*a1157835SDaniel Fojt 		sta->fils_pending_assoc_req_len = 0;
4231*a1157835SDaniel Fojt 		wpabuf_free(sta->fils_hlp_resp);
4232*a1157835SDaniel Fojt 		sta->fils_hlp_resp = NULL;
4233*a1157835SDaniel Fojt 	}
4234*a1157835SDaniel Fojt 	if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS) {
4235*a1157835SDaniel Fojt 		sta->fils_pending_assoc_req = tmp;
4236*a1157835SDaniel Fojt 		sta->fils_pending_assoc_req_len = left;
4237*a1157835SDaniel Fojt 		sta->fils_pending_assoc_is_reassoc = reassoc;
4238*a1157835SDaniel Fojt 		sta->fils_drv_assoc_finish = 0;
4239*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
4240*a1157835SDaniel Fojt 			   "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
4241*a1157835SDaniel Fojt 			   MACSTR, MAC2STR(sta->addr));
4242*a1157835SDaniel Fojt 		eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4243*a1157835SDaniel Fojt 		eloop_register_timeout(0, hapd->conf->fils_hlp_wait_time * 1024,
4244*a1157835SDaniel Fojt 				       fils_hlp_timeout, hapd, sta);
4245*a1157835SDaniel Fojt 		return;
4246*a1157835SDaniel Fojt 	}
4247*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
4248*a1157835SDaniel Fojt 
4249*a1157835SDaniel Fojt 	reply_res = send_assoc_resp(hapd, sta, mgmt->sa, resp, reassoc, pos,
4250*a1157835SDaniel Fojt 				    left, rssi);
4251*a1157835SDaniel Fojt 	os_free(tmp);
4252*a1157835SDaniel Fojt 
4253*a1157835SDaniel Fojt 	/*
4254*a1157835SDaniel Fojt 	 * Remove the station in case tranmission of a success response fails
4255*a1157835SDaniel Fojt 	 * (the STA was added associated to the driver) or if the station was
4256*a1157835SDaniel Fojt 	 * previously added unassociated.
4257*a1157835SDaniel Fojt 	 */
4258*a1157835SDaniel Fojt 	if (sta && ((reply_res != WLAN_STATUS_SUCCESS &&
4259*a1157835SDaniel Fojt 		     resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc)) {
4260*a1157835SDaniel Fojt 		hostapd_drv_sta_remove(hapd, sta->addr);
4261*a1157835SDaniel Fojt 		sta->added_unassoc = 0;
4262*a1157835SDaniel Fojt 	}
42633ff40c12SJohn Marino }
42643ff40c12SJohn Marino 
42653ff40c12SJohn Marino 
handle_disassoc(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len)42663ff40c12SJohn Marino static void handle_disassoc(struct hostapd_data *hapd,
42673ff40c12SJohn Marino 			    const struct ieee80211_mgmt *mgmt, size_t len)
42683ff40c12SJohn Marino {
42693ff40c12SJohn Marino 	struct sta_info *sta;
42703ff40c12SJohn Marino 
42713ff40c12SJohn Marino 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
42723ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
42733ff40c12SJohn Marino 			   (unsigned long) len);
42743ff40c12SJohn Marino 		return;
42753ff40c12SJohn Marino 	}
42763ff40c12SJohn Marino 
42773ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
42783ff40c12SJohn Marino 		   MAC2STR(mgmt->sa),
42793ff40c12SJohn Marino 		   le_to_host16(mgmt->u.disassoc.reason_code));
42803ff40c12SJohn Marino 
42813ff40c12SJohn Marino 	sta = ap_get_sta(hapd, mgmt->sa);
42823ff40c12SJohn Marino 	if (sta == NULL) {
42833ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
42843ff40c12SJohn Marino 			   MAC2STR(mgmt->sa));
42853ff40c12SJohn Marino 		return;
42863ff40c12SJohn Marino 	}
42873ff40c12SJohn Marino 
42883ff40c12SJohn Marino 	ap_sta_set_authorized(hapd, sta, 0);
4289*a1157835SDaniel Fojt 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
42903ff40c12SJohn Marino 	sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
42913ff40c12SJohn Marino 	wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
42923ff40c12SJohn Marino 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
42933ff40c12SJohn Marino 		       HOSTAPD_LEVEL_INFO, "disassociated");
42943ff40c12SJohn Marino 	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
42953ff40c12SJohn Marino 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
42963ff40c12SJohn Marino 	/* Stop Accounting and IEEE 802.1X sessions, but leave the STA
42973ff40c12SJohn Marino 	 * authenticated. */
42983ff40c12SJohn Marino 	accounting_sta_stop(hapd, sta);
4299*a1157835SDaniel Fojt 	ieee802_1x_free_station(hapd, sta);
4300*a1157835SDaniel Fojt 	if (sta->ipaddr)
4301*a1157835SDaniel Fojt 		hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
4302*a1157835SDaniel Fojt 	ap_sta_ip6addr_del(hapd, sta);
43033ff40c12SJohn Marino 	hostapd_drv_sta_remove(hapd, sta->addr);
4304*a1157835SDaniel Fojt 	sta->added_unassoc = 0;
43053ff40c12SJohn Marino 
43063ff40c12SJohn Marino 	if (sta->timeout_next == STA_NULLFUNC ||
43073ff40c12SJohn Marino 	    sta->timeout_next == STA_DISASSOC) {
43083ff40c12SJohn Marino 		sta->timeout_next = STA_DEAUTH;
43093ff40c12SJohn Marino 		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
43103ff40c12SJohn Marino 		eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
43113ff40c12SJohn Marino 				       hapd, sta);
43123ff40c12SJohn Marino 	}
43133ff40c12SJohn Marino 
43143ff40c12SJohn Marino 	mlme_disassociate_indication(
43153ff40c12SJohn Marino 		hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
4316*a1157835SDaniel Fojt 
4317*a1157835SDaniel Fojt 	/* DMG/IEEE 802.11ad does not use deauthication. Deallocate sta upon
4318*a1157835SDaniel Fojt 	 * disassociation. */
4319*a1157835SDaniel Fojt 	if (hapd->iface->current_mode &&
4320*a1157835SDaniel Fojt 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
4321*a1157835SDaniel Fojt 		sta->flags &= ~WLAN_STA_AUTH;
4322*a1157835SDaniel Fojt 		wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
4323*a1157835SDaniel Fojt 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4324*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_DEBUG, "deauthenticated");
4325*a1157835SDaniel Fojt 		ap_free_sta(hapd, sta);
4326*a1157835SDaniel Fojt 	}
43273ff40c12SJohn Marino }
43283ff40c12SJohn Marino 
43293ff40c12SJohn Marino 
handle_deauth(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len)43303ff40c12SJohn Marino static void handle_deauth(struct hostapd_data *hapd,
43313ff40c12SJohn Marino 			  const struct ieee80211_mgmt *mgmt, size_t len)
43323ff40c12SJohn Marino {
43333ff40c12SJohn Marino 	struct sta_info *sta;
43343ff40c12SJohn Marino 
43353ff40c12SJohn Marino 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
43363ff40c12SJohn Marino 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
43373ff40c12SJohn Marino 			"payload (len=%lu)", (unsigned long) len);
43383ff40c12SJohn Marino 		return;
43393ff40c12SJohn Marino 	}
43403ff40c12SJohn Marino 
43413ff40c12SJohn Marino 	wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
43423ff40c12SJohn Marino 		" reason_code=%d",
43433ff40c12SJohn Marino 		MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
43443ff40c12SJohn Marino 
43453ff40c12SJohn Marino 	sta = ap_get_sta(hapd, mgmt->sa);
43463ff40c12SJohn Marino 	if (sta == NULL) {
43473ff40c12SJohn Marino 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
43483ff40c12SJohn Marino 			"to deauthenticate, but it is not authenticated",
43493ff40c12SJohn Marino 			MAC2STR(mgmt->sa));
43503ff40c12SJohn Marino 		return;
43513ff40c12SJohn Marino 	}
43523ff40c12SJohn Marino 
43533ff40c12SJohn Marino 	ap_sta_set_authorized(hapd, sta, 0);
4354*a1157835SDaniel Fojt 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
43553ff40c12SJohn Marino 	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
43563ff40c12SJohn Marino 			WLAN_STA_ASSOC_REQ_OK);
43573ff40c12SJohn Marino 	wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
43583ff40c12SJohn Marino 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
43593ff40c12SJohn Marino 		       HOSTAPD_LEVEL_DEBUG, "deauthenticated");
43603ff40c12SJohn Marino 	mlme_deauthenticate_indication(
43613ff40c12SJohn Marino 		hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
43623ff40c12SJohn Marino 	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
43633ff40c12SJohn Marino 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
43643ff40c12SJohn Marino 	ap_free_sta(hapd, sta);
43653ff40c12SJohn Marino }
43663ff40c12SJohn Marino 
43673ff40c12SJohn Marino 
handle_beacon(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,struct hostapd_frame_info * fi)43683ff40c12SJohn Marino static void handle_beacon(struct hostapd_data *hapd,
43693ff40c12SJohn Marino 			  const struct ieee80211_mgmt *mgmt, size_t len,
43703ff40c12SJohn Marino 			  struct hostapd_frame_info *fi)
43713ff40c12SJohn Marino {
43723ff40c12SJohn Marino 	struct ieee802_11_elems elems;
43733ff40c12SJohn Marino 
43743ff40c12SJohn Marino 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
43753ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
43763ff40c12SJohn Marino 			   (unsigned long) len);
43773ff40c12SJohn Marino 		return;
43783ff40c12SJohn Marino 	}
43793ff40c12SJohn Marino 
43803ff40c12SJohn Marino 	(void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
43813ff40c12SJohn Marino 				      len - (IEEE80211_HDRLEN +
43823ff40c12SJohn Marino 					     sizeof(mgmt->u.beacon)), &elems,
43833ff40c12SJohn Marino 				      0);
43843ff40c12SJohn Marino 
43853ff40c12SJohn Marino 	ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
43863ff40c12SJohn Marino }
43873ff40c12SJohn Marino 
43883ff40c12SJohn Marino 
43893ff40c12SJohn Marino #ifdef CONFIG_IEEE80211W
robust_action_frame(u8 category)43903ff40c12SJohn Marino static int robust_action_frame(u8 category)
43913ff40c12SJohn Marino {
43923ff40c12SJohn Marino 	return category != WLAN_ACTION_PUBLIC &&
43933ff40c12SJohn Marino 		category != WLAN_ACTION_HT;
43943ff40c12SJohn Marino }
43953ff40c12SJohn Marino #endif /* CONFIG_IEEE80211W */
43963ff40c12SJohn Marino 
43973ff40c12SJohn Marino 
handle_action(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,unsigned int freq)43983ff40c12SJohn Marino static int handle_action(struct hostapd_data *hapd,
4399*a1157835SDaniel Fojt 			 const struct ieee80211_mgmt *mgmt, size_t len,
4400*a1157835SDaniel Fojt 			 unsigned int freq)
44013ff40c12SJohn Marino {
44023ff40c12SJohn Marino 	struct sta_info *sta;
4403*a1157835SDaniel Fojt 	u8 *action __maybe_unused;
44043ff40c12SJohn Marino 
4405*a1157835SDaniel Fojt 	if (len < IEEE80211_HDRLEN + 2 + 1) {
44063ff40c12SJohn Marino 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
44073ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
44083ff40c12SJohn Marino 			       "handle_action - too short payload (len=%lu)",
44093ff40c12SJohn Marino 			       (unsigned long) len);
44103ff40c12SJohn Marino 		return 0;
44113ff40c12SJohn Marino 	}
44123ff40c12SJohn Marino 
4413*a1157835SDaniel Fojt 	action = (u8 *) &mgmt->u.action.u;
4414*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
4415*a1157835SDaniel Fojt 		   " da " MACSTR " len %d freq %u",
4416*a1157835SDaniel Fojt 		   mgmt->u.action.category, *action,
4417*a1157835SDaniel Fojt 		   MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) len, freq);
4418*a1157835SDaniel Fojt 
4419*a1157835SDaniel Fojt 	sta = ap_get_sta(hapd, mgmt->sa);
4420*a1157835SDaniel Fojt 
44213ff40c12SJohn Marino 	if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
44223ff40c12SJohn Marino 	    (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
44233ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
44243ff40c12SJohn Marino 			   "frame (category=%u) from unassociated STA " MACSTR,
4425*a1157835SDaniel Fojt 			   mgmt->u.action.category, MAC2STR(mgmt->sa));
44263ff40c12SJohn Marino 		return 0;
44273ff40c12SJohn Marino 	}
44283ff40c12SJohn Marino 
44293ff40c12SJohn Marino #ifdef CONFIG_IEEE80211W
44303ff40c12SJohn Marino 	if (sta && (sta->flags & WLAN_STA_MFP) &&
44313ff40c12SJohn Marino 	    !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
44323ff40c12SJohn Marino 	    robust_action_frame(mgmt->u.action.category)) {
44333ff40c12SJohn Marino 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
44343ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
44353ff40c12SJohn Marino 			       "Dropped unprotected Robust Action frame from "
44363ff40c12SJohn Marino 			       "an MFP STA");
44373ff40c12SJohn Marino 		return 0;
44383ff40c12SJohn Marino 	}
44393ff40c12SJohn Marino #endif /* CONFIG_IEEE80211W */
44403ff40c12SJohn Marino 
4441*a1157835SDaniel Fojt 	if (sta) {
4442*a1157835SDaniel Fojt 		u16 fc = le_to_host16(mgmt->frame_control);
4443*a1157835SDaniel Fojt 		u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
4444*a1157835SDaniel Fojt 
4445*a1157835SDaniel Fojt 		if ((fc & WLAN_FC_RETRY) &&
4446*a1157835SDaniel Fojt 		    sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4447*a1157835SDaniel Fojt 		    sta->last_seq_ctrl == seq_ctrl &&
4448*a1157835SDaniel Fojt 		    sta->last_subtype == WLAN_FC_STYPE_ACTION) {
4449*a1157835SDaniel Fojt 			hostapd_logger(hapd, sta->addr,
4450*a1157835SDaniel Fojt 				       HOSTAPD_MODULE_IEEE80211,
4451*a1157835SDaniel Fojt 				       HOSTAPD_LEVEL_DEBUG,
4452*a1157835SDaniel Fojt 				       "Drop repeated action frame seq_ctrl=0x%x",
4453*a1157835SDaniel Fojt 				       seq_ctrl);
4454*a1157835SDaniel Fojt 			return 1;
4455*a1157835SDaniel Fojt 		}
4456*a1157835SDaniel Fojt 
4457*a1157835SDaniel Fojt 		sta->last_seq_ctrl = seq_ctrl;
4458*a1157835SDaniel Fojt 		sta->last_subtype = WLAN_FC_STYPE_ACTION;
4459*a1157835SDaniel Fojt 	}
4460*a1157835SDaniel Fojt 
44613ff40c12SJohn Marino 	switch (mgmt->u.action.category) {
4462*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211R_AP
44633ff40c12SJohn Marino 	case WLAN_ACTION_FT:
4464*a1157835SDaniel Fojt 		if (!sta ||
4465*a1157835SDaniel Fojt 		    wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
44663ff40c12SJohn Marino 				     len - IEEE80211_HDRLEN))
44673ff40c12SJohn Marino 			break;
44683ff40c12SJohn Marino 		return 1;
4469*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211R_AP */
44703ff40c12SJohn Marino 	case WLAN_ACTION_WMM:
44713ff40c12SJohn Marino 		hostapd_wmm_action(hapd, mgmt, len);
44723ff40c12SJohn Marino 		return 1;
44733ff40c12SJohn Marino #ifdef CONFIG_IEEE80211W
44743ff40c12SJohn Marino 	case WLAN_ACTION_SA_QUERY:
4475*a1157835SDaniel Fojt 		ieee802_11_sa_query_action(hapd, mgmt, len);
4476*a1157835SDaniel Fojt 		return 1;
44773ff40c12SJohn Marino #endif /* CONFIG_IEEE80211W */
4478*a1157835SDaniel Fojt #ifdef CONFIG_WNM_AP
44793ff40c12SJohn Marino 	case WLAN_ACTION_WNM:
44803ff40c12SJohn Marino 		ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
44813ff40c12SJohn Marino 		return 1;
4482*a1157835SDaniel Fojt #endif /* CONFIG_WNM_AP */
4483*a1157835SDaniel Fojt #ifdef CONFIG_FST
4484*a1157835SDaniel Fojt 	case WLAN_ACTION_FST:
4485*a1157835SDaniel Fojt 		if (hapd->iface->fst)
4486*a1157835SDaniel Fojt 			fst_rx_action(hapd->iface->fst, mgmt, len);
4487*a1157835SDaniel Fojt 		else
4488*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
4489*a1157835SDaniel Fojt 				   "FST: Ignore FST Action frame - no FST attached");
4490*a1157835SDaniel Fojt 		return 1;
4491*a1157835SDaniel Fojt #endif /* CONFIG_FST */
44923ff40c12SJohn Marino 	case WLAN_ACTION_PUBLIC:
44933ff40c12SJohn Marino 	case WLAN_ACTION_PROTECTED_DUAL:
4494*a1157835SDaniel Fojt #ifdef CONFIG_IEEE80211N
4495*a1157835SDaniel Fojt 		if (len >= IEEE80211_HDRLEN + 2 &&
4496*a1157835SDaniel Fojt 		    mgmt->u.action.u.public_action.action ==
4497*a1157835SDaniel Fojt 		    WLAN_PA_20_40_BSS_COEX) {
4498*a1157835SDaniel Fojt 			hostapd_2040_coex_action(hapd, mgmt, len);
4499*a1157835SDaniel Fojt 			return 1;
4500*a1157835SDaniel Fojt 		}
4501*a1157835SDaniel Fojt #endif /* CONFIG_IEEE80211N */
4502*a1157835SDaniel Fojt #ifdef CONFIG_DPP
4503*a1157835SDaniel Fojt 		if (len >= IEEE80211_HDRLEN + 6 &&
4504*a1157835SDaniel Fojt 		    mgmt->u.action.u.vs_public_action.action ==
4505*a1157835SDaniel Fojt 		    WLAN_PA_VENDOR_SPECIFIC &&
4506*a1157835SDaniel Fojt 		    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
4507*a1157835SDaniel Fojt 		    OUI_WFA &&
4508*a1157835SDaniel Fojt 		    mgmt->u.action.u.vs_public_action.variable[0] ==
4509*a1157835SDaniel Fojt 		    DPP_OUI_TYPE) {
4510*a1157835SDaniel Fojt 			const u8 *pos, *end;
4511*a1157835SDaniel Fojt 
4512*a1157835SDaniel Fojt 			pos = mgmt->u.action.u.vs_public_action.oui;
4513*a1157835SDaniel Fojt 			end = ((const u8 *) mgmt) + len;
4514*a1157835SDaniel Fojt 			hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
4515*a1157835SDaniel Fojt 					      freq);
4516*a1157835SDaniel Fojt 			return 1;
4517*a1157835SDaniel Fojt 		}
4518*a1157835SDaniel Fojt 		if (len >= IEEE80211_HDRLEN + 2 &&
4519*a1157835SDaniel Fojt 		    (mgmt->u.action.u.public_action.action ==
4520*a1157835SDaniel Fojt 		     WLAN_PA_GAS_INITIAL_RESP ||
4521*a1157835SDaniel Fojt 		     mgmt->u.action.u.public_action.action ==
4522*a1157835SDaniel Fojt 		     WLAN_PA_GAS_COMEBACK_RESP)) {
4523*a1157835SDaniel Fojt 			const u8 *pos, *end;
4524*a1157835SDaniel Fojt 
4525*a1157835SDaniel Fojt 			pos = &mgmt->u.action.u.public_action.action;
4526*a1157835SDaniel Fojt 			end = ((const u8 *) mgmt) + len;
4527*a1157835SDaniel Fojt 			gas_query_ap_rx(hapd->gas, mgmt->sa,
4528*a1157835SDaniel Fojt 					mgmt->u.action.category,
4529*a1157835SDaniel Fojt 					pos, end - pos, hapd->iface->freq);
4530*a1157835SDaniel Fojt 			return 1;
4531*a1157835SDaniel Fojt 		}
4532*a1157835SDaniel Fojt #endif /* CONFIG_DPP */
45333ff40c12SJohn Marino 		if (hapd->public_action_cb) {
45343ff40c12SJohn Marino 			hapd->public_action_cb(hapd->public_action_cb_ctx,
45353ff40c12SJohn Marino 					       (u8 *) mgmt, len,
45363ff40c12SJohn Marino 					       hapd->iface->freq);
45373ff40c12SJohn Marino 		}
45383ff40c12SJohn Marino 		if (hapd->public_action_cb2) {
45393ff40c12SJohn Marino 			hapd->public_action_cb2(hapd->public_action_cb2_ctx,
45403ff40c12SJohn Marino 						(u8 *) mgmt, len,
45413ff40c12SJohn Marino 						hapd->iface->freq);
45423ff40c12SJohn Marino 		}
45433ff40c12SJohn Marino 		if (hapd->public_action_cb || hapd->public_action_cb2)
45443ff40c12SJohn Marino 			return 1;
45453ff40c12SJohn Marino 		break;
45463ff40c12SJohn Marino 	case WLAN_ACTION_VENDOR_SPECIFIC:
45473ff40c12SJohn Marino 		if (hapd->vendor_action_cb) {
45483ff40c12SJohn Marino 			if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
45493ff40c12SJohn Marino 						   (u8 *) mgmt, len,
45503ff40c12SJohn Marino 						   hapd->iface->freq) == 0)
45513ff40c12SJohn Marino 				return 1;
45523ff40c12SJohn Marino 		}
45533ff40c12SJohn Marino 		break;
4554*a1157835SDaniel Fojt 	case WLAN_ACTION_RADIO_MEASUREMENT:
4555*a1157835SDaniel Fojt 		hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
4556*a1157835SDaniel Fojt 		return 1;
45573ff40c12SJohn Marino 	}
45583ff40c12SJohn Marino 
45593ff40c12SJohn Marino 	hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
45603ff40c12SJohn Marino 		       HOSTAPD_LEVEL_DEBUG,
45613ff40c12SJohn Marino 		       "handle_action - unknown action category %d or invalid "
45623ff40c12SJohn Marino 		       "frame",
45633ff40c12SJohn Marino 		       mgmt->u.action.category);
4564*a1157835SDaniel Fojt 	if (!is_multicast_ether_addr(mgmt->da) &&
4565*a1157835SDaniel Fojt 	    !(mgmt->u.action.category & 0x80) &&
4566*a1157835SDaniel Fojt 	    !is_multicast_ether_addr(mgmt->sa)) {
45673ff40c12SJohn Marino 		struct ieee80211_mgmt *resp;
45683ff40c12SJohn Marino 
45693ff40c12SJohn Marino 		/*
45703ff40c12SJohn Marino 		 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
45713ff40c12SJohn Marino 		 * Return the Action frame to the source without change
45723ff40c12SJohn Marino 		 * except that MSB of the Category set to 1.
45733ff40c12SJohn Marino 		 */
45743ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
45753ff40c12SJohn Marino 			   "frame back to sender");
4576*a1157835SDaniel Fojt 		resp = os_memdup(mgmt, len);
45773ff40c12SJohn Marino 		if (resp == NULL)
45783ff40c12SJohn Marino 			return 0;
45793ff40c12SJohn Marino 		os_memcpy(resp->da, resp->sa, ETH_ALEN);
45803ff40c12SJohn Marino 		os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
45813ff40c12SJohn Marino 		os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
45823ff40c12SJohn Marino 		resp->u.action.category |= 0x80;
45833ff40c12SJohn Marino 
45843ff40c12SJohn Marino 		if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
45853ff40c12SJohn Marino 			wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
45863ff40c12SJohn Marino 				   "Action frame");
45873ff40c12SJohn Marino 		}
45883ff40c12SJohn Marino 		os_free(resp);
45893ff40c12SJohn Marino 	}
45903ff40c12SJohn Marino 
45913ff40c12SJohn Marino 	return 1;
45923ff40c12SJohn Marino }
45933ff40c12SJohn Marino 
45943ff40c12SJohn Marino 
45953ff40c12SJohn Marino /**
45963ff40c12SJohn Marino  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
45973ff40c12SJohn Marino  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
45983ff40c12SJohn Marino  * sent to)
45993ff40c12SJohn Marino  * @buf: management frame data (starting from IEEE 802.11 header)
46003ff40c12SJohn Marino  * @len: length of frame data in octets
46013ff40c12SJohn Marino  * @fi: meta data about received frame (signal level, etc.)
46023ff40c12SJohn Marino  *
46033ff40c12SJohn Marino  * Process all incoming IEEE 802.11 management frames. This will be called for
46043ff40c12SJohn Marino  * each frame received from the kernel driver through wlan#ap interface. In
46053ff40c12SJohn Marino  * addition, it can be called to re-inserted pending frames (e.g., when using
46063ff40c12SJohn Marino  * external RADIUS server as an MAC ACL).
46073ff40c12SJohn Marino  */
ieee802_11_mgmt(struct hostapd_data * hapd,const u8 * buf,size_t len,struct hostapd_frame_info * fi)46083ff40c12SJohn Marino int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
46093ff40c12SJohn Marino 		    struct hostapd_frame_info *fi)
46103ff40c12SJohn Marino {
46113ff40c12SJohn Marino 	struct ieee80211_mgmt *mgmt;
46123ff40c12SJohn Marino 	u16 fc, stype;
46133ff40c12SJohn Marino 	int ret = 0;
4614*a1157835SDaniel Fojt 	unsigned int freq;
4615*a1157835SDaniel Fojt 	int ssi_signal = fi ? fi->ssi_signal : 0;
46163ff40c12SJohn Marino 
46173ff40c12SJohn Marino 	if (len < 24)
46183ff40c12SJohn Marino 		return 0;
46193ff40c12SJohn Marino 
4620*a1157835SDaniel Fojt 	if (fi && fi->freq)
4621*a1157835SDaniel Fojt 		freq = fi->freq;
4622*a1157835SDaniel Fojt 	else
4623*a1157835SDaniel Fojt 		freq = hapd->iface->freq;
4624*a1157835SDaniel Fojt 
46253ff40c12SJohn Marino 	mgmt = (struct ieee80211_mgmt *) buf;
46263ff40c12SJohn Marino 	fc = le_to_host16(mgmt->frame_control);
46273ff40c12SJohn Marino 	stype = WLAN_FC_GET_STYPE(fc);
46283ff40c12SJohn Marino 
46293ff40c12SJohn Marino 	if (stype == WLAN_FC_STYPE_BEACON) {
46303ff40c12SJohn Marino 		handle_beacon(hapd, mgmt, len, fi);
46313ff40c12SJohn Marino 		return 1;
46323ff40c12SJohn Marino 	}
46333ff40c12SJohn Marino 
4634*a1157835SDaniel Fojt 	if (!is_broadcast_ether_addr(mgmt->bssid) &&
46353ff40c12SJohn Marino #ifdef CONFIG_P2P
46363ff40c12SJohn Marino 	    /* Invitation responses can be sent with the peer MAC as BSSID */
46373ff40c12SJohn Marino 	    !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
46383ff40c12SJohn Marino 	      stype == WLAN_FC_STYPE_ACTION) &&
46393ff40c12SJohn Marino #endif /* CONFIG_P2P */
4640*a1157835SDaniel Fojt #ifdef CONFIG_MESH
4641*a1157835SDaniel Fojt 	    !(hapd->conf->mesh & MESH_ENABLED) &&
4642*a1157835SDaniel Fojt #endif /* CONFIG_MESH */
46433ff40c12SJohn Marino 	    os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
46443ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
46453ff40c12SJohn Marino 			   MAC2STR(mgmt->bssid));
46463ff40c12SJohn Marino 		return 0;
46473ff40c12SJohn Marino 	}
46483ff40c12SJohn Marino 
46493ff40c12SJohn Marino 
46503ff40c12SJohn Marino 	if (stype == WLAN_FC_STYPE_PROBE_REQ) {
4651*a1157835SDaniel Fojt 		handle_probe_req(hapd, mgmt, len, ssi_signal);
46523ff40c12SJohn Marino 		return 1;
46533ff40c12SJohn Marino 	}
46543ff40c12SJohn Marino 
4655*a1157835SDaniel Fojt 	if ((!is_broadcast_ether_addr(mgmt->da) ||
4656*a1157835SDaniel Fojt 	     stype != WLAN_FC_STYPE_ACTION) &&
4657*a1157835SDaniel Fojt 	    os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
46583ff40c12SJohn Marino 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
46593ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
46603ff40c12SJohn Marino 			       "MGMT: DA=" MACSTR " not our address",
46613ff40c12SJohn Marino 			       MAC2STR(mgmt->da));
46623ff40c12SJohn Marino 		return 0;
46633ff40c12SJohn Marino 	}
46643ff40c12SJohn Marino 
4665*a1157835SDaniel Fojt 	if (hapd->iconf->track_sta_max_num)
4666*a1157835SDaniel Fojt 		sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
4667*a1157835SDaniel Fojt 
46683ff40c12SJohn Marino 	switch (stype) {
46693ff40c12SJohn Marino 	case WLAN_FC_STYPE_AUTH:
46703ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::auth");
4671*a1157835SDaniel Fojt 		handle_auth(hapd, mgmt, len, ssi_signal, 0);
46723ff40c12SJohn Marino 		ret = 1;
46733ff40c12SJohn Marino 		break;
46743ff40c12SJohn Marino 	case WLAN_FC_STYPE_ASSOC_REQ:
46753ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
4676*a1157835SDaniel Fojt 		handle_assoc(hapd, mgmt, len, 0, ssi_signal);
46773ff40c12SJohn Marino 		ret = 1;
46783ff40c12SJohn Marino 		break;
46793ff40c12SJohn Marino 	case WLAN_FC_STYPE_REASSOC_REQ:
46803ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
4681*a1157835SDaniel Fojt 		handle_assoc(hapd, mgmt, len, 1, ssi_signal);
46823ff40c12SJohn Marino 		ret = 1;
46833ff40c12SJohn Marino 		break;
46843ff40c12SJohn Marino 	case WLAN_FC_STYPE_DISASSOC:
46853ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::disassoc");
46863ff40c12SJohn Marino 		handle_disassoc(hapd, mgmt, len);
46873ff40c12SJohn Marino 		ret = 1;
46883ff40c12SJohn Marino 		break;
46893ff40c12SJohn Marino 	case WLAN_FC_STYPE_DEAUTH:
46903ff40c12SJohn Marino 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
46913ff40c12SJohn Marino 		handle_deauth(hapd, mgmt, len);
46923ff40c12SJohn Marino 		ret = 1;
46933ff40c12SJohn Marino 		break;
46943ff40c12SJohn Marino 	case WLAN_FC_STYPE_ACTION:
46953ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::action");
4696*a1157835SDaniel Fojt 		ret = handle_action(hapd, mgmt, len, freq);
46973ff40c12SJohn Marino 		break;
46983ff40c12SJohn Marino 	default:
46993ff40c12SJohn Marino 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
47003ff40c12SJohn Marino 			       HOSTAPD_LEVEL_DEBUG,
47013ff40c12SJohn Marino 			       "unknown mgmt frame subtype %d", stype);
47023ff40c12SJohn Marino 		break;
47033ff40c12SJohn Marino 	}
47043ff40c12SJohn Marino 
47053ff40c12SJohn Marino 	return ret;
47063ff40c12SJohn Marino }
47073ff40c12SJohn Marino 
47083ff40c12SJohn Marino 
handle_auth_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)47093ff40c12SJohn Marino static void handle_auth_cb(struct hostapd_data *hapd,
47103ff40c12SJohn Marino 			   const struct ieee80211_mgmt *mgmt,
47113ff40c12SJohn Marino 			   size_t len, int ok)
47123ff40c12SJohn Marino {
47133ff40c12SJohn Marino 	u16 auth_alg, auth_transaction, status_code;
47143ff40c12SJohn Marino 	struct sta_info *sta;
47153ff40c12SJohn Marino 
4716*a1157835SDaniel Fojt 	sta = ap_get_sta(hapd, mgmt->da);
4717*a1157835SDaniel Fojt 	if (!sta) {
4718*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "handle_auth_cb: STA " MACSTR
4719*a1157835SDaniel Fojt 			   " not found",
4720*a1157835SDaniel Fojt 			   MAC2STR(mgmt->da));
47213ff40c12SJohn Marino 		return;
47223ff40c12SJohn Marino 	}
47233ff40c12SJohn Marino 
47243ff40c12SJohn Marino 	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
47253ff40c12SJohn Marino 	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
47263ff40c12SJohn Marino 	status_code = le_to_host16(mgmt->u.auth.status_code);
47273ff40c12SJohn Marino 
4728*a1157835SDaniel Fojt 	if (!ok) {
4729*a1157835SDaniel Fojt 		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
4730*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_NOTICE,
4731*a1157835SDaniel Fojt 			       "did not acknowledge authentication response");
4732*a1157835SDaniel Fojt 		goto fail;
4733*a1157835SDaniel Fojt 	}
4734*a1157835SDaniel Fojt 
4735*a1157835SDaniel Fojt 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
4736*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
4737*a1157835SDaniel Fojt 			   (unsigned long) len);
4738*a1157835SDaniel Fojt 		goto fail;
47393ff40c12SJohn Marino 	}
47403ff40c12SJohn Marino 
47413ff40c12SJohn Marino 	if (status_code == WLAN_STATUS_SUCCESS &&
47423ff40c12SJohn Marino 	    ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
47433ff40c12SJohn Marino 	     (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
47443ff40c12SJohn Marino 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
47453ff40c12SJohn Marino 			       HOSTAPD_LEVEL_INFO, "authenticated");
47463ff40c12SJohn Marino 		sta->flags |= WLAN_STA_AUTH;
4747*a1157835SDaniel Fojt 		if (sta->added_unassoc)
4748*a1157835SDaniel Fojt 			hostapd_set_sta_flags(hapd, sta);
4749*a1157835SDaniel Fojt 		return;
4750*a1157835SDaniel Fojt 	}
4751*a1157835SDaniel Fojt 
4752*a1157835SDaniel Fojt fail:
4753*a1157835SDaniel Fojt 	if (status_code != WLAN_STATUS_SUCCESS && sta->added_unassoc) {
4754*a1157835SDaniel Fojt 		hostapd_drv_sta_remove(hapd, sta->addr);
4755*a1157835SDaniel Fojt 		sta->added_unassoc = 0;
47563ff40c12SJohn Marino 	}
47573ff40c12SJohn Marino }
47583ff40c12SJohn Marino 
47593ff40c12SJohn Marino 
hostapd_set_wds_encryption(struct hostapd_data * hapd,struct sta_info * sta,char * ifname_wds)47603ff40c12SJohn Marino static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
47613ff40c12SJohn Marino 				       struct sta_info *sta,
47623ff40c12SJohn Marino 				       char *ifname_wds)
47633ff40c12SJohn Marino {
47643ff40c12SJohn Marino 	int i;
4765*a1157835SDaniel Fojt 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
47663ff40c12SJohn Marino 
47673ff40c12SJohn Marino 	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
47683ff40c12SJohn Marino 		return;
47693ff40c12SJohn Marino 
47703ff40c12SJohn Marino 	for (i = 0; i < 4; i++) {
47713ff40c12SJohn Marino 		if (ssid->wep.key[i] &&
47723ff40c12SJohn Marino 		    hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
47733ff40c12SJohn Marino 					i == ssid->wep.idx, NULL, 0,
47743ff40c12SJohn Marino 					ssid->wep.key[i], ssid->wep.len[i])) {
47753ff40c12SJohn Marino 			wpa_printf(MSG_WARNING,
47763ff40c12SJohn Marino 				   "Could not set WEP keys for WDS interface; %s",
47773ff40c12SJohn Marino 				   ifname_wds);
47783ff40c12SJohn Marino 			break;
47793ff40c12SJohn Marino 		}
47803ff40c12SJohn Marino 	}
47813ff40c12SJohn Marino }
47823ff40c12SJohn Marino 
47833ff40c12SJohn Marino 
handle_assoc_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int reassoc,int ok)47843ff40c12SJohn Marino static void handle_assoc_cb(struct hostapd_data *hapd,
47853ff40c12SJohn Marino 			    const struct ieee80211_mgmt *mgmt,
47863ff40c12SJohn Marino 			    size_t len, int reassoc, int ok)
47873ff40c12SJohn Marino {
47883ff40c12SJohn Marino 	u16 status;
47893ff40c12SJohn Marino 	struct sta_info *sta;
47903ff40c12SJohn Marino 	int new_assoc = 1;
47913ff40c12SJohn Marino 
47923ff40c12SJohn Marino 	sta = ap_get_sta(hapd, mgmt->da);
47933ff40c12SJohn Marino 	if (!sta) {
47943ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
47953ff40c12SJohn Marino 			   MAC2STR(mgmt->da));
47963ff40c12SJohn Marino 		return;
47973ff40c12SJohn Marino 	}
47983ff40c12SJohn Marino 
4799*a1157835SDaniel Fojt 	if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
4800*a1157835SDaniel Fojt 				      sizeof(mgmt->u.assoc_resp))) {
4801*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
4802*a1157835SDaniel Fojt 			   "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
4803*a1157835SDaniel Fojt 			   reassoc, (unsigned long) len);
4804*a1157835SDaniel Fojt 		hostapd_drv_sta_remove(hapd, sta->addr);
48053ff40c12SJohn Marino 		return;
48063ff40c12SJohn Marino 	}
48073ff40c12SJohn Marino 
48083ff40c12SJohn Marino 	if (reassoc)
48093ff40c12SJohn Marino 		status = le_to_host16(mgmt->u.reassoc_resp.status_code);
48103ff40c12SJohn Marino 	else
48113ff40c12SJohn Marino 		status = le_to_host16(mgmt->u.assoc_resp.status_code);
48123ff40c12SJohn Marino 
4813*a1157835SDaniel Fojt 	if (!ok) {
4814*a1157835SDaniel Fojt 		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
4815*a1157835SDaniel Fojt 			       HOSTAPD_LEVEL_DEBUG,
4816*a1157835SDaniel Fojt 			       "did not acknowledge association response");
4817*a1157835SDaniel Fojt 		sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
4818*a1157835SDaniel Fojt 		/* The STA is added only in case of SUCCESS */
4819*a1157835SDaniel Fojt 		if (status == WLAN_STATUS_SUCCESS)
4820*a1157835SDaniel Fojt 			hostapd_drv_sta_remove(hapd, sta->addr);
4821*a1157835SDaniel Fojt 
4822*a1157835SDaniel Fojt 		return;
4823*a1157835SDaniel Fojt 	}
4824*a1157835SDaniel Fojt 
48253ff40c12SJohn Marino 	if (status != WLAN_STATUS_SUCCESS)
4826*a1157835SDaniel Fojt 		return;
48273ff40c12SJohn Marino 
48283ff40c12SJohn Marino 	/* Stop previous accounting session, if one is started, and allocate
48293ff40c12SJohn Marino 	 * new session id for the new session. */
48303ff40c12SJohn Marino 	accounting_sta_stop(hapd, sta);
48313ff40c12SJohn Marino 
48323ff40c12SJohn Marino 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
48333ff40c12SJohn Marino 		       HOSTAPD_LEVEL_INFO,
48343ff40c12SJohn Marino 		       "associated (aid %d)",
48353ff40c12SJohn Marino 		       sta->aid);
48363ff40c12SJohn Marino 
48373ff40c12SJohn Marino 	if (sta->flags & WLAN_STA_ASSOC)
48383ff40c12SJohn Marino 		new_assoc = 0;
48393ff40c12SJohn Marino 	sta->flags |= WLAN_STA_ASSOC;
48403ff40c12SJohn Marino 	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
4841*a1157835SDaniel Fojt 	if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
4842*a1157835SDaniel Fojt 	     !hapd->conf->osen) ||
4843*a1157835SDaniel Fojt 	    sta->auth_alg == WLAN_AUTH_FILS_SK ||
4844*a1157835SDaniel Fojt 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4845*a1157835SDaniel Fojt 	    sta->auth_alg == WLAN_AUTH_FILS_PK ||
48463ff40c12SJohn Marino 	    sta->auth_alg == WLAN_AUTH_FT) {
48473ff40c12SJohn Marino 		/*
4848*a1157835SDaniel Fojt 		 * Open, static WEP, FT protocol, or FILS; no separate
4849*a1157835SDaniel Fojt 		 * authorization step.
48503ff40c12SJohn Marino 		 */
48513ff40c12SJohn Marino 		ap_sta_set_authorized(hapd, sta, 1);
48523ff40c12SJohn Marino 	}
48533ff40c12SJohn Marino 
48543ff40c12SJohn Marino 	if (reassoc)
48553ff40c12SJohn Marino 		mlme_reassociate_indication(hapd, sta);
48563ff40c12SJohn Marino 	else
48573ff40c12SJohn Marino 		mlme_associate_indication(hapd, sta);
48583ff40c12SJohn Marino 
48593ff40c12SJohn Marino #ifdef CONFIG_IEEE80211W
48603ff40c12SJohn Marino 	sta->sa_query_timed_out = 0;
48613ff40c12SJohn Marino #endif /* CONFIG_IEEE80211W */
48623ff40c12SJohn Marino 
48633ff40c12SJohn Marino 	if (sta->eapol_sm == NULL) {
48643ff40c12SJohn Marino 		/*
48653ff40c12SJohn Marino 		 * This STA does not use RADIUS server for EAP authentication,
48663ff40c12SJohn Marino 		 * so bind it to the selected VLAN interface now, since the
48673ff40c12SJohn Marino 		 * interface selection is not going to change anymore.
48683ff40c12SJohn Marino 		 */
4869*a1157835SDaniel Fojt 		if (ap_sta_bind_vlan(hapd, sta) < 0)
4870*a1157835SDaniel Fojt 			return;
48713ff40c12SJohn Marino 	} else if (sta->vlan_id) {
48723ff40c12SJohn Marino 		/* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
4873*a1157835SDaniel Fojt 		if (ap_sta_bind_vlan(hapd, sta) < 0)
4874*a1157835SDaniel Fojt 			return;
48753ff40c12SJohn Marino 	}
48763ff40c12SJohn Marino 
48773ff40c12SJohn Marino 	hostapd_set_sta_flags(hapd, sta);
48783ff40c12SJohn Marino 
4879*a1157835SDaniel Fojt 	if (!(sta->flags & WLAN_STA_WDS) && sta->pending_wds_enable) {
4880*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for STA "
4881*a1157835SDaniel Fojt 			   MACSTR " based on pending request",
4882*a1157835SDaniel Fojt 			   MAC2STR(sta->addr));
4883*a1157835SDaniel Fojt 		sta->pending_wds_enable = 0;
4884*a1157835SDaniel Fojt 		sta->flags |= WLAN_STA_WDS;
4885*a1157835SDaniel Fojt 	}
4886*a1157835SDaniel Fojt 
4887*a1157835SDaniel Fojt 	if (sta->flags & (WLAN_STA_WDS | WLAN_STA_MULTI_AP)) {
4888*a1157835SDaniel Fojt 		int ret;
4889*a1157835SDaniel Fojt 		char ifname_wds[IFNAMSIZ + 1];
4890*a1157835SDaniel Fojt 
4891*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "Reenable 4-address WDS mode for STA "
4892*a1157835SDaniel Fojt 			   MACSTR " (aid %u)",
4893*a1157835SDaniel Fojt 			   MAC2STR(sta->addr), sta->aid);
4894*a1157835SDaniel Fojt 		ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
4895*a1157835SDaniel Fojt 					  sta->aid, 1);
4896*a1157835SDaniel Fojt 		if (!ret)
4897*a1157835SDaniel Fojt 			hostapd_set_wds_encryption(hapd, sta, ifname_wds);
4898*a1157835SDaniel Fojt 	}
4899*a1157835SDaniel Fojt 
49003ff40c12SJohn Marino 	if (sta->auth_alg == WLAN_AUTH_FT)
49013ff40c12SJohn Marino 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
49023ff40c12SJohn Marino 	else
49033ff40c12SJohn Marino 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
49043ff40c12SJohn Marino 	hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
49053ff40c12SJohn Marino 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
49063ff40c12SJohn Marino 
4907*a1157835SDaniel Fojt #ifdef CONFIG_FILS
4908*a1157835SDaniel Fojt 	if ((sta->auth_alg == WLAN_AUTH_FILS_SK ||
4909*a1157835SDaniel Fojt 	     sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4910*a1157835SDaniel Fojt 	     sta->auth_alg == WLAN_AUTH_FILS_PK) &&
4911*a1157835SDaniel Fojt 	    fils_set_tk(sta->wpa_sm) < 0) {
4912*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "FILS: TK configuration failed");
4913*a1157835SDaniel Fojt 		ap_sta_disconnect(hapd, sta, sta->addr,
4914*a1157835SDaniel Fojt 				  WLAN_REASON_UNSPECIFIED);
4915*a1157835SDaniel Fojt 		return;
4916*a1157835SDaniel Fojt 	}
4917*a1157835SDaniel Fojt #endif /* CONFIG_FILS */
4918*a1157835SDaniel Fojt 
4919*a1157835SDaniel Fojt 	if (sta->pending_eapol_rx) {
4920*a1157835SDaniel Fojt 		struct os_reltime now, age;
4921*a1157835SDaniel Fojt 
4922*a1157835SDaniel Fojt 		os_get_reltime(&now);
4923*a1157835SDaniel Fojt 		os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
4924*a1157835SDaniel Fojt 		if (age.sec == 0 && age.usec < 200000) {
4925*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
4926*a1157835SDaniel Fojt 				   "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
4927*a1157835SDaniel Fojt 				   MAC2STR(sta->addr));
4928*a1157835SDaniel Fojt 			ieee802_1x_receive(
4929*a1157835SDaniel Fojt 				hapd, mgmt->da,
4930*a1157835SDaniel Fojt 				wpabuf_head(sta->pending_eapol_rx->buf),
4931*a1157835SDaniel Fojt 				wpabuf_len(sta->pending_eapol_rx->buf));
4932*a1157835SDaniel Fojt 		}
4933*a1157835SDaniel Fojt 		wpabuf_free(sta->pending_eapol_rx->buf);
4934*a1157835SDaniel Fojt 		os_free(sta->pending_eapol_rx);
4935*a1157835SDaniel Fojt 		sta->pending_eapol_rx = NULL;
49363ff40c12SJohn Marino 	}
49373ff40c12SJohn Marino }
49383ff40c12SJohn Marino 
49393ff40c12SJohn Marino 
handle_deauth_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)49403ff40c12SJohn Marino static void handle_deauth_cb(struct hostapd_data *hapd,
49413ff40c12SJohn Marino 			     const struct ieee80211_mgmt *mgmt,
49423ff40c12SJohn Marino 			     size_t len, int ok)
49433ff40c12SJohn Marino {
49443ff40c12SJohn Marino 	struct sta_info *sta;
4945*a1157835SDaniel Fojt 	if (is_multicast_ether_addr(mgmt->da))
49463ff40c12SJohn Marino 		return;
49473ff40c12SJohn Marino 	sta = ap_get_sta(hapd, mgmt->da);
49483ff40c12SJohn Marino 	if (!sta) {
49493ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
49503ff40c12SJohn Marino 			   " not found", MAC2STR(mgmt->da));
49513ff40c12SJohn Marino 		return;
49523ff40c12SJohn Marino 	}
49533ff40c12SJohn Marino 	if (ok)
49543ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
49553ff40c12SJohn Marino 			   MAC2STR(sta->addr));
49563ff40c12SJohn Marino 	else
49573ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
49583ff40c12SJohn Marino 			   "deauth", MAC2STR(sta->addr));
49593ff40c12SJohn Marino 
49603ff40c12SJohn Marino 	ap_sta_deauth_cb(hapd, sta);
49613ff40c12SJohn Marino }
49623ff40c12SJohn Marino 
49633ff40c12SJohn Marino 
handle_disassoc_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)49643ff40c12SJohn Marino static void handle_disassoc_cb(struct hostapd_data *hapd,
49653ff40c12SJohn Marino 			       const struct ieee80211_mgmt *mgmt,
49663ff40c12SJohn Marino 			       size_t len, int ok)
49673ff40c12SJohn Marino {
49683ff40c12SJohn Marino 	struct sta_info *sta;
4969*a1157835SDaniel Fojt 	if (is_multicast_ether_addr(mgmt->da))
49703ff40c12SJohn Marino 		return;
49713ff40c12SJohn Marino 	sta = ap_get_sta(hapd, mgmt->da);
49723ff40c12SJohn Marino 	if (!sta) {
49733ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
49743ff40c12SJohn Marino 			   " not found", MAC2STR(mgmt->da));
49753ff40c12SJohn Marino 		return;
49763ff40c12SJohn Marino 	}
49773ff40c12SJohn Marino 	if (ok)
49783ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
49793ff40c12SJohn Marino 			   MAC2STR(sta->addr));
49803ff40c12SJohn Marino 	else
49813ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
49823ff40c12SJohn Marino 			   "disassoc", MAC2STR(sta->addr));
49833ff40c12SJohn Marino 
49843ff40c12SJohn Marino 	ap_sta_disassoc_cb(hapd, sta);
49853ff40c12SJohn Marino }
49863ff40c12SJohn Marino 
49873ff40c12SJohn Marino 
handle_action_cb(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ok)4988*a1157835SDaniel Fojt static void handle_action_cb(struct hostapd_data *hapd,
4989*a1157835SDaniel Fojt 			     const struct ieee80211_mgmt *mgmt,
4990*a1157835SDaniel Fojt 			     size_t len, int ok)
4991*a1157835SDaniel Fojt {
4992*a1157835SDaniel Fojt 	struct sta_info *sta;
4993*a1157835SDaniel Fojt 	const struct rrm_measurement_report_element *report;
4994*a1157835SDaniel Fojt 
4995*a1157835SDaniel Fojt 	if (is_multicast_ether_addr(mgmt->da))
4996*a1157835SDaniel Fojt 		return;
4997*a1157835SDaniel Fojt #ifdef CONFIG_DPP
4998*a1157835SDaniel Fojt 	if (len >= IEEE80211_HDRLEN + 6 &&
4999*a1157835SDaniel Fojt 	    mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
5000*a1157835SDaniel Fojt 	    mgmt->u.action.u.vs_public_action.action ==
5001*a1157835SDaniel Fojt 	    WLAN_PA_VENDOR_SPECIFIC &&
5002*a1157835SDaniel Fojt 	    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
5003*a1157835SDaniel Fojt 	    OUI_WFA &&
5004*a1157835SDaniel Fojt 	    mgmt->u.action.u.vs_public_action.variable[0] ==
5005*a1157835SDaniel Fojt 	    DPP_OUI_TYPE) {
5006*a1157835SDaniel Fojt 		const u8 *pos, *end;
5007*a1157835SDaniel Fojt 
5008*a1157835SDaniel Fojt 		pos = &mgmt->u.action.u.vs_public_action.variable[1];
5009*a1157835SDaniel Fojt 		end = ((const u8 *) mgmt) + len;
5010*a1157835SDaniel Fojt 		hostapd_dpp_tx_status(hapd, mgmt->da, pos, end - pos, ok);
5011*a1157835SDaniel Fojt 		return;
5012*a1157835SDaniel Fojt 	}
5013*a1157835SDaniel Fojt 	if (len >= IEEE80211_HDRLEN + 2 &&
5014*a1157835SDaniel Fojt 	    mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
5015*a1157835SDaniel Fojt 	    (mgmt->u.action.u.public_action.action ==
5016*a1157835SDaniel Fojt 	     WLAN_PA_GAS_INITIAL_REQ ||
5017*a1157835SDaniel Fojt 	     mgmt->u.action.u.public_action.action ==
5018*a1157835SDaniel Fojt 	     WLAN_PA_GAS_COMEBACK_REQ)) {
5019*a1157835SDaniel Fojt 		const u8 *pos, *end;
5020*a1157835SDaniel Fojt 
5021*a1157835SDaniel Fojt 		pos = mgmt->u.action.u.public_action.variable;
5022*a1157835SDaniel Fojt 		end = ((const u8 *) mgmt) + len;
5023*a1157835SDaniel Fojt 		gas_query_ap_tx_status(hapd->gas, mgmt->da, pos, end - pos, ok);
5024*a1157835SDaniel Fojt 		return;
5025*a1157835SDaniel Fojt 	}
5026*a1157835SDaniel Fojt #endif /* CONFIG_DPP */
5027*a1157835SDaniel Fojt 	sta = ap_get_sta(hapd, mgmt->da);
5028*a1157835SDaniel Fojt 	if (!sta) {
5029*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "handle_action_cb: STA " MACSTR
5030*a1157835SDaniel Fojt 			   " not found", MAC2STR(mgmt->da));
5031*a1157835SDaniel Fojt 		return;
5032*a1157835SDaniel Fojt 	}
5033*a1157835SDaniel Fojt 
5034*a1157835SDaniel Fojt 	if (len < 24 + 5 + sizeof(*report))
5035*a1157835SDaniel Fojt 		return;
5036*a1157835SDaniel Fojt 	report = (const struct rrm_measurement_report_element *)
5037*a1157835SDaniel Fojt 		&mgmt->u.action.u.rrm.variable[2];
5038*a1157835SDaniel Fojt 	if (mgmt->u.action.category == WLAN_ACTION_RADIO_MEASUREMENT &&
5039*a1157835SDaniel Fojt 	    mgmt->u.action.u.rrm.action == WLAN_RRM_RADIO_MEASUREMENT_REQUEST &&
5040*a1157835SDaniel Fojt 	    report->eid == WLAN_EID_MEASURE_REQUEST &&
5041*a1157835SDaniel Fojt 	    report->len >= 3 &&
5042*a1157835SDaniel Fojt 	    report->type == MEASURE_TYPE_BEACON)
5043*a1157835SDaniel Fojt 		hostapd_rrm_beacon_req_tx_status(hapd, mgmt, len, ok);
5044*a1157835SDaniel Fojt }
5045*a1157835SDaniel Fojt 
5046*a1157835SDaniel Fojt 
50473ff40c12SJohn Marino /**
50483ff40c12SJohn Marino  * ieee802_11_mgmt_cb - Process management frame TX status callback
50493ff40c12SJohn Marino  * @hapd: hostapd BSS data structure (the BSS from which the management frame
50503ff40c12SJohn Marino  * was sent from)
50513ff40c12SJohn Marino  * @buf: management frame data (starting from IEEE 802.11 header)
50523ff40c12SJohn Marino  * @len: length of frame data in octets
50533ff40c12SJohn Marino  * @stype: management frame subtype from frame control field
50543ff40c12SJohn Marino  * @ok: Whether the frame was ACK'ed
50553ff40c12SJohn Marino  */
ieee802_11_mgmt_cb(struct hostapd_data * hapd,const u8 * buf,size_t len,u16 stype,int ok)50563ff40c12SJohn Marino void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
50573ff40c12SJohn Marino 			u16 stype, int ok)
50583ff40c12SJohn Marino {
50593ff40c12SJohn Marino 	const struct ieee80211_mgmt *mgmt;
50603ff40c12SJohn Marino 	mgmt = (const struct ieee80211_mgmt *) buf;
50613ff40c12SJohn Marino 
50623ff40c12SJohn Marino #ifdef CONFIG_TESTING_OPTIONS
50633ff40c12SJohn Marino 	if (hapd->ext_mgmt_frame_handling) {
5064*a1157835SDaniel Fojt 		size_t hex_len = 2 * len + 1;
5065*a1157835SDaniel Fojt 		char *hex = os_malloc(hex_len);
5066*a1157835SDaniel Fojt 
5067*a1157835SDaniel Fojt 		if (hex) {
5068*a1157835SDaniel Fojt 			wpa_snprintf_hex(hex, hex_len, buf, len);
5069*a1157835SDaniel Fojt 			wpa_msg(hapd->msg_ctx, MSG_INFO,
5070*a1157835SDaniel Fojt 				"MGMT-TX-STATUS stype=%u ok=%d buf=%s",
5071*a1157835SDaniel Fojt 				stype, ok, hex);
5072*a1157835SDaniel Fojt 			os_free(hex);
5073*a1157835SDaniel Fojt 		}
50743ff40c12SJohn Marino 		return;
50753ff40c12SJohn Marino 	}
50763ff40c12SJohn Marino #endif /* CONFIG_TESTING_OPTIONS */
50773ff40c12SJohn Marino 
50783ff40c12SJohn Marino 	switch (stype) {
50793ff40c12SJohn Marino 	case WLAN_FC_STYPE_AUTH:
50803ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::auth cb");
50813ff40c12SJohn Marino 		handle_auth_cb(hapd, mgmt, len, ok);
50823ff40c12SJohn Marino 		break;
50833ff40c12SJohn Marino 	case WLAN_FC_STYPE_ASSOC_RESP:
50843ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
50853ff40c12SJohn Marino 		handle_assoc_cb(hapd, mgmt, len, 0, ok);
50863ff40c12SJohn Marino 		break;
50873ff40c12SJohn Marino 	case WLAN_FC_STYPE_REASSOC_RESP:
50883ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
50893ff40c12SJohn Marino 		handle_assoc_cb(hapd, mgmt, len, 1, ok);
50903ff40c12SJohn Marino 		break;
50913ff40c12SJohn Marino 	case WLAN_FC_STYPE_PROBE_RESP:
5092*a1157835SDaniel Fojt 		wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
50933ff40c12SJohn Marino 		break;
50943ff40c12SJohn Marino 	case WLAN_FC_STYPE_DEAUTH:
50953ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
50963ff40c12SJohn Marino 		handle_deauth_cb(hapd, mgmt, len, ok);
50973ff40c12SJohn Marino 		break;
50983ff40c12SJohn Marino 	case WLAN_FC_STYPE_DISASSOC:
50993ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
51003ff40c12SJohn Marino 		handle_disassoc_cb(hapd, mgmt, len, ok);
51013ff40c12SJohn Marino 		break;
51023ff40c12SJohn Marino 	case WLAN_FC_STYPE_ACTION:
5103*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
5104*a1157835SDaniel Fojt 		handle_action_cb(hapd, mgmt, len, ok);
51053ff40c12SJohn Marino 		break;
51063ff40c12SJohn Marino 	default:
51073ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
51083ff40c12SJohn Marino 		break;
51093ff40c12SJohn Marino 	}
51103ff40c12SJohn Marino }
51113ff40c12SJohn Marino 
51123ff40c12SJohn Marino 
ieee802_11_get_mib(struct hostapd_data * hapd,char * buf,size_t buflen)51133ff40c12SJohn Marino int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
51143ff40c12SJohn Marino {
51153ff40c12SJohn Marino 	/* TODO */
51163ff40c12SJohn Marino 	return 0;
51173ff40c12SJohn Marino }
51183ff40c12SJohn Marino 
51193ff40c12SJohn Marino 
ieee802_11_get_mib_sta(struct hostapd_data * hapd,struct sta_info * sta,char * buf,size_t buflen)51203ff40c12SJohn Marino int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
51213ff40c12SJohn Marino 			   char *buf, size_t buflen)
51223ff40c12SJohn Marino {
51233ff40c12SJohn Marino 	/* TODO */
51243ff40c12SJohn Marino 	return 0;
51253ff40c12SJohn Marino }
51263ff40c12SJohn Marino 
51273ff40c12SJohn Marino 
hostapd_tx_status(struct hostapd_data * hapd,const u8 * addr,const u8 * buf,size_t len,int ack)51283ff40c12SJohn Marino void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
51293ff40c12SJohn Marino 		       const u8 *buf, size_t len, int ack)
51303ff40c12SJohn Marino {
51313ff40c12SJohn Marino 	struct sta_info *sta;
51323ff40c12SJohn Marino 	struct hostapd_iface *iface = hapd->iface;
51333ff40c12SJohn Marino 
51343ff40c12SJohn Marino 	sta = ap_get_sta(hapd, addr);
51353ff40c12SJohn Marino 	if (sta == NULL && iface->num_bss > 1) {
51363ff40c12SJohn Marino 		size_t j;
51373ff40c12SJohn Marino 		for (j = 0; j < iface->num_bss; j++) {
51383ff40c12SJohn Marino 			hapd = iface->bss[j];
51393ff40c12SJohn Marino 			sta = ap_get_sta(hapd, addr);
51403ff40c12SJohn Marino 			if (sta)
51413ff40c12SJohn Marino 				break;
51423ff40c12SJohn Marino 		}
51433ff40c12SJohn Marino 	}
51443ff40c12SJohn Marino 	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
51453ff40c12SJohn Marino 		return;
51463ff40c12SJohn Marino 	if (sta->flags & WLAN_STA_PENDING_POLL) {
51473ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
51483ff40c12SJohn Marino 			   "activity poll", MAC2STR(sta->addr),
51493ff40c12SJohn Marino 			   ack ? "ACKed" : "did not ACK");
51503ff40c12SJohn Marino 		if (ack)
51513ff40c12SJohn Marino 			sta->flags &= ~WLAN_STA_PENDING_POLL;
51523ff40c12SJohn Marino 	}
51533ff40c12SJohn Marino 
51543ff40c12SJohn Marino 	ieee802_1x_tx_status(hapd, sta, buf, len, ack);
51553ff40c12SJohn Marino }
51563ff40c12SJohn Marino 
51573ff40c12SJohn Marino 
hostapd_eapol_tx_status(struct hostapd_data * hapd,const u8 * dst,const u8 * data,size_t len,int ack)51583ff40c12SJohn Marino void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
51593ff40c12SJohn Marino 			     const u8 *data, size_t len, int ack)
51603ff40c12SJohn Marino {
51613ff40c12SJohn Marino 	struct sta_info *sta;
51623ff40c12SJohn Marino 	struct hostapd_iface *iface = hapd->iface;
51633ff40c12SJohn Marino 
51643ff40c12SJohn Marino 	sta = ap_get_sta(hapd, dst);
51653ff40c12SJohn Marino 	if (sta == NULL && iface->num_bss > 1) {
51663ff40c12SJohn Marino 		size_t j;
51673ff40c12SJohn Marino 		for (j = 0; j < iface->num_bss; j++) {
51683ff40c12SJohn Marino 			hapd = iface->bss[j];
51693ff40c12SJohn Marino 			sta = ap_get_sta(hapd, dst);
51703ff40c12SJohn Marino 			if (sta)
51713ff40c12SJohn Marino 				break;
51723ff40c12SJohn Marino 		}
51733ff40c12SJohn Marino 	}
51743ff40c12SJohn Marino 	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
51753ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
51763ff40c12SJohn Marino 			   MACSTR " that is not currently associated",
51773ff40c12SJohn Marino 			   MAC2STR(dst));
51783ff40c12SJohn Marino 		return;
51793ff40c12SJohn Marino 	}
51803ff40c12SJohn Marino 
51813ff40c12SJohn Marino 	ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
51823ff40c12SJohn Marino }
51833ff40c12SJohn Marino 
51843ff40c12SJohn Marino 
hostapd_client_poll_ok(struct hostapd_data * hapd,const u8 * addr)51853ff40c12SJohn Marino void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
51863ff40c12SJohn Marino {
51873ff40c12SJohn Marino 	struct sta_info *sta;
51883ff40c12SJohn Marino 	struct hostapd_iface *iface = hapd->iface;
51893ff40c12SJohn Marino 
51903ff40c12SJohn Marino 	sta = ap_get_sta(hapd, addr);
51913ff40c12SJohn Marino 	if (sta == NULL && iface->num_bss > 1) {
51923ff40c12SJohn Marino 		size_t j;
51933ff40c12SJohn Marino 		for (j = 0; j < iface->num_bss; j++) {
51943ff40c12SJohn Marino 			hapd = iface->bss[j];
51953ff40c12SJohn Marino 			sta = ap_get_sta(hapd, addr);
51963ff40c12SJohn Marino 			if (sta)
51973ff40c12SJohn Marino 				break;
51983ff40c12SJohn Marino 		}
51993ff40c12SJohn Marino 	}
52003ff40c12SJohn Marino 	if (sta == NULL)
52013ff40c12SJohn Marino 		return;
5202*a1157835SDaniel Fojt 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
5203*a1157835SDaniel Fojt 		MAC2STR(sta->addr));
52043ff40c12SJohn Marino 	if (!(sta->flags & WLAN_STA_PENDING_POLL))
52053ff40c12SJohn Marino 		return;
52063ff40c12SJohn Marino 
52073ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
52083ff40c12SJohn Marino 		   "activity poll", MAC2STR(sta->addr));
52093ff40c12SJohn Marino 	sta->flags &= ~WLAN_STA_PENDING_POLL;
52103ff40c12SJohn Marino }
52113ff40c12SJohn Marino 
52123ff40c12SJohn Marino 
ieee802_11_rx_from_unknown(struct hostapd_data * hapd,const u8 * src,int wds)52133ff40c12SJohn Marino void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
52143ff40c12SJohn Marino 				int wds)
52153ff40c12SJohn Marino {
52163ff40c12SJohn Marino 	struct sta_info *sta;
52173ff40c12SJohn Marino 
52183ff40c12SJohn Marino 	sta = ap_get_sta(hapd, src);
5219*a1157835SDaniel Fojt 	if (sta &&
5220*a1157835SDaniel Fojt 	    ((sta->flags & WLAN_STA_ASSOC) ||
5221*a1157835SDaniel Fojt 	     ((sta->flags & WLAN_STA_ASSOC_REQ_OK) && wds))) {
52223ff40c12SJohn Marino 		if (!hapd->conf->wds_sta)
52233ff40c12SJohn Marino 			return;
52243ff40c12SJohn Marino 
5225*a1157835SDaniel Fojt 		if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK)) ==
5226*a1157835SDaniel Fojt 		    WLAN_STA_ASSOC_REQ_OK) {
5227*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
5228*a1157835SDaniel Fojt 				   "Postpone 4-address WDS mode enabling for STA "
5229*a1157835SDaniel Fojt 				   MACSTR " since TX status for AssocResp is not yet known",
5230*a1157835SDaniel Fojt 				   MAC2STR(sta->addr));
5231*a1157835SDaniel Fojt 			sta->pending_wds_enable = 1;
5232*a1157835SDaniel Fojt 			return;
5233*a1157835SDaniel Fojt 		}
5234*a1157835SDaniel Fojt 
52353ff40c12SJohn Marino 		if (wds && !(sta->flags & WLAN_STA_WDS)) {
52363ff40c12SJohn Marino 			int ret;
52373ff40c12SJohn Marino 			char ifname_wds[IFNAMSIZ + 1];
52383ff40c12SJohn Marino 
52393ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
52403ff40c12SJohn Marino 				   "STA " MACSTR " (aid %u)",
52413ff40c12SJohn Marino 				   MAC2STR(sta->addr), sta->aid);
52423ff40c12SJohn Marino 			sta->flags |= WLAN_STA_WDS;
52433ff40c12SJohn Marino 			ret = hostapd_set_wds_sta(hapd, ifname_wds,
52443ff40c12SJohn Marino 						  sta->addr, sta->aid, 1);
52453ff40c12SJohn Marino 			if (!ret)
52463ff40c12SJohn Marino 				hostapd_set_wds_encryption(hapd, sta,
52473ff40c12SJohn Marino 							   ifname_wds);
52483ff40c12SJohn Marino 		}
52493ff40c12SJohn Marino 		return;
52503ff40c12SJohn Marino 	}
52513ff40c12SJohn Marino 
52523ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
52533ff40c12SJohn Marino 		   MACSTR, MAC2STR(src));
5254*a1157835SDaniel Fojt 	if (is_multicast_ether_addr(src)) {
52553ff40c12SJohn Marino 		/* Broadcast bit set in SA?! Ignore the frame silently. */
52563ff40c12SJohn Marino 		return;
52573ff40c12SJohn Marino 	}
52583ff40c12SJohn Marino 
52593ff40c12SJohn Marino 	if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
52603ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "Association Response to the STA has "
52613ff40c12SJohn Marino 			   "already been sent, but no TX status yet known - "
52623ff40c12SJohn Marino 			   "ignore Class 3 frame issue with " MACSTR,
52633ff40c12SJohn Marino 			   MAC2STR(src));
52643ff40c12SJohn Marino 		return;
52653ff40c12SJohn Marino 	}
52663ff40c12SJohn Marino 
52673ff40c12SJohn Marino 	if (sta && (sta->flags & WLAN_STA_AUTH))
52683ff40c12SJohn Marino 		hostapd_drv_sta_disassoc(
52693ff40c12SJohn Marino 			hapd, src,
52703ff40c12SJohn Marino 			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
52713ff40c12SJohn Marino 	else
52723ff40c12SJohn Marino 		hostapd_drv_sta_deauth(
52733ff40c12SJohn Marino 			hapd, src,
52743ff40c12SJohn Marino 			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
52753ff40c12SJohn Marino }
52763ff40c12SJohn Marino 
52773ff40c12SJohn Marino 
52783ff40c12SJohn Marino #endif /* CONFIG_NATIVE_WINDOWS */
5279