xref: /freebsd/contrib/wpa/src/ap/ieee802_11_shared.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo  * hostapd / IEEE 802.11 Management
3*a90b9d01SCy Schubert  * Copyright (c) 2002-2024, Jouni Malinen <j@w1.fi>
4f05cddf9SRui Paulo  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
7f05cddf9SRui Paulo  */
8f05cddf9SRui Paulo 
9f05cddf9SRui Paulo #include "utils/includes.h"
10f05cddf9SRui Paulo 
11f05cddf9SRui Paulo #include "utils/common.h"
12f05cddf9SRui Paulo #include "common/ieee802_11_defs.h"
134bc52338SCy Schubert #include "common/ocv.h"
14c1d255d3SCy Schubert #include "common/wpa_ctrl.h"
15f05cddf9SRui Paulo #include "hostapd.h"
16f05cddf9SRui Paulo #include "sta_info.h"
17f05cddf9SRui Paulo #include "ap_config.h"
18f05cddf9SRui Paulo #include "ap_drv_ops.h"
194bc52338SCy Schubert #include "wpa_auth.h"
20*a90b9d01SCy Schubert #include "dpp_hostapd.h"
21f05cddf9SRui Paulo #include "ieee802_11.h"
22f05cddf9SRui Paulo 
23f05cddf9SRui Paulo 
hostapd_eid_timeout_interval(u8 * pos,u8 type,u32 value)24*a90b9d01SCy Schubert static u8 * hostapd_eid_timeout_interval(u8 *pos, u8 type, u32 value)
25*a90b9d01SCy Schubert {
26*a90b9d01SCy Schubert 	*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
27*a90b9d01SCy Schubert 	*pos++ = 5;
28*a90b9d01SCy Schubert 	*pos++ = type;
29*a90b9d01SCy Schubert 	WPA_PUT_LE32(pos, value);
30*a90b9d01SCy Schubert 	pos += 4;
31*a90b9d01SCy Schubert 
32*a90b9d01SCy Schubert 	return pos;
33*a90b9d01SCy Schubert }
34*a90b9d01SCy Schubert 
35*a90b9d01SCy Schubert 
hostapd_eid_assoc_comeback_time(struct hostapd_data * hapd,struct sta_info * sta,u8 * eid)36f05cddf9SRui Paulo u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
37f05cddf9SRui Paulo 				     struct sta_info *sta, u8 *eid)
38f05cddf9SRui Paulo {
39f05cddf9SRui Paulo 	u32 timeout, tu;
405b9c547cSRui Paulo 	struct os_reltime now, passed;
41*a90b9d01SCy Schubert 	u8 type = WLAN_TIMEOUT_ASSOC_COMEBACK;
42f05cddf9SRui Paulo 
435b9c547cSRui Paulo 	os_get_reltime(&now);
445b9c547cSRui Paulo 	os_reltime_sub(&now, &sta->sa_query_start, &passed);
45f05cddf9SRui Paulo 	tu = (passed.sec * 1000000 + passed.usec) / 1024;
46f05cddf9SRui Paulo 	if (hapd->conf->assoc_sa_query_max_timeout > tu)
47f05cddf9SRui Paulo 		timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
48f05cddf9SRui Paulo 	else
49f05cddf9SRui Paulo 		timeout = 0;
50f05cddf9SRui Paulo 	if (timeout < hapd->conf->assoc_sa_query_max_timeout)
51f05cddf9SRui Paulo 		timeout++; /* add some extra time for local timers */
52f05cddf9SRui Paulo 
53*a90b9d01SCy Schubert #ifdef CONFIG_TESTING_OPTIONS
54*a90b9d01SCy Schubert 	if (hapd->conf->test_assoc_comeback_type != -1)
55*a90b9d01SCy Schubert 		type = hapd->conf->test_assoc_comeback_type;
56*a90b9d01SCy Schubert #endif /* CONFIG_TESTING_OPTIONS */
57*a90b9d01SCy Schubert 	return hostapd_eid_timeout_interval(eid, type, timeout);
58f05cddf9SRui Paulo }
59f05cddf9SRui Paulo 
60f05cddf9SRui Paulo 
61f05cddf9SRui Paulo /* MLME-SAQuery.request */
ieee802_11_send_sa_query_req(struct hostapd_data * hapd,const u8 * addr,const u8 * trans_id)62f05cddf9SRui Paulo void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
63f05cddf9SRui Paulo 				  const u8 *addr, const u8 *trans_id)
64f05cddf9SRui Paulo {
65*a90b9d01SCy Schubert #if defined(CONFIG_OCV) || defined(CONFIG_IEEE80211BE)
66*a90b9d01SCy Schubert 	struct sta_info *sta = ap_get_sta(hapd, addr);
67*a90b9d01SCy Schubert #endif /* CONFIG_OCV || CONFIG_IEEE80211BE */
684bc52338SCy Schubert 	struct ieee80211_mgmt *mgmt;
694bc52338SCy Schubert 	u8 *oci_ie = NULL;
704bc52338SCy Schubert 	u8 oci_ie_len = 0;
71f05cddf9SRui Paulo 	u8 *end;
72*a90b9d01SCy Schubert 	const u8 *own_addr = hapd->own_addr;
73f05cddf9SRui Paulo 
74f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
75f05cddf9SRui Paulo 		   MACSTR, MAC2STR(addr));
76f05cddf9SRui Paulo 	wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
77f05cddf9SRui Paulo 		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
78f05cddf9SRui Paulo 
794bc52338SCy Schubert #ifdef CONFIG_OCV
804bc52338SCy Schubert 	if (sta && wpa_auth_uses_ocv(sta->wpa_sm)) {
814bc52338SCy Schubert 		struct wpa_channel_info ci;
824bc52338SCy Schubert 
834bc52338SCy Schubert 		if (hostapd_drv_channel_info(hapd, &ci) != 0) {
844bc52338SCy Schubert 			wpa_printf(MSG_WARNING,
854bc52338SCy Schubert 				   "Failed to get channel info for OCI element in SA Query Request");
864bc52338SCy Schubert 			return;
874bc52338SCy Schubert 		}
88c1d255d3SCy Schubert #ifdef CONFIG_TESTING_OPTIONS
89c1d255d3SCy Schubert 		if (hapd->conf->oci_freq_override_saquery_req) {
90c1d255d3SCy Schubert 			wpa_printf(MSG_INFO,
91c1d255d3SCy Schubert 				   "TEST: Override OCI frequency %d -> %u MHz",
92c1d255d3SCy Schubert 				   ci.frequency,
93c1d255d3SCy Schubert 				   hapd->conf->oci_freq_override_saquery_req);
94c1d255d3SCy Schubert 			ci.frequency =
95c1d255d3SCy Schubert 				hapd->conf->oci_freq_override_saquery_req;
96c1d255d3SCy Schubert 		}
97c1d255d3SCy Schubert #endif /* CONFIG_TESTING_OPTIONS */
984bc52338SCy Schubert 
994bc52338SCy Schubert 		oci_ie_len = OCV_OCI_EXTENDED_LEN;
1004bc52338SCy Schubert 		oci_ie = os_zalloc(oci_ie_len);
1014bc52338SCy Schubert 		if (!oci_ie) {
1024bc52338SCy Schubert 			wpa_printf(MSG_WARNING,
1034bc52338SCy Schubert 				   "Failed to allocate buffer for OCI element in SA Query Request");
1044bc52338SCy Schubert 			return;
1054bc52338SCy Schubert 		}
1064bc52338SCy Schubert 
1074bc52338SCy Schubert 		if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
1084bc52338SCy Schubert 			os_free(oci_ie);
1094bc52338SCy Schubert 			return;
1104bc52338SCy Schubert 		}
1114bc52338SCy Schubert 	}
1124bc52338SCy Schubert #endif /* CONFIG_OCV */
1134bc52338SCy Schubert 
1144bc52338SCy Schubert 	mgmt = os_zalloc(sizeof(*mgmt) + oci_ie_len);
1154bc52338SCy Schubert 	if (!mgmt) {
1164bc52338SCy Schubert 		wpa_printf(MSG_DEBUG,
1174bc52338SCy Schubert 			   "Failed to allocate buffer for SA Query Response frame");
1184bc52338SCy Schubert 		os_free(oci_ie);
1194bc52338SCy Schubert 		return;
1204bc52338SCy Schubert 	}
1214bc52338SCy Schubert 
122*a90b9d01SCy Schubert #ifdef CONFIG_IEEE80211BE
123*a90b9d01SCy Schubert 	if (ap_sta_is_mld(hapd, sta))
124*a90b9d01SCy Schubert 		own_addr = hapd->mld->mld_addr;
125*a90b9d01SCy Schubert #endif /* CONFIG_IEEE80211BE */
126*a90b9d01SCy Schubert 
1274bc52338SCy Schubert 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
128f05cddf9SRui Paulo 					   WLAN_FC_STYPE_ACTION);
1294bc52338SCy Schubert 	os_memcpy(mgmt->da, addr, ETH_ALEN);
130*a90b9d01SCy Schubert 	os_memcpy(mgmt->sa, own_addr, ETH_ALEN);
131*a90b9d01SCy Schubert 	os_memcpy(mgmt->bssid, own_addr, ETH_ALEN);
1324bc52338SCy Schubert 	mgmt->u.action.category = WLAN_ACTION_SA_QUERY;
1334bc52338SCy Schubert 	mgmt->u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1344bc52338SCy Schubert 	os_memcpy(mgmt->u.action.u.sa_query_req.trans_id, trans_id,
135f05cddf9SRui Paulo 		  WLAN_SA_QUERY_TR_ID_LEN);
1364bc52338SCy Schubert 	end = mgmt->u.action.u.sa_query_req.variable;
1374bc52338SCy Schubert #ifdef CONFIG_OCV
1384bc52338SCy Schubert 	if (oci_ie_len > 0) {
1394bc52338SCy Schubert 		os_memcpy(end, oci_ie, oci_ie_len);
1404bc52338SCy Schubert 		end += oci_ie_len;
1414bc52338SCy Schubert 	}
1424bc52338SCy Schubert #endif /* CONFIG_OCV */
143c1d255d3SCy Schubert 	if (hostapd_drv_send_mlme(hapd, mgmt, end - (u8 *) mgmt, 0, NULL, 0, 0)
144c1d255d3SCy Schubert 	    < 0)
1455b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "ieee802_11_send_sa_query_req: send failed");
1464bc52338SCy Schubert 
1474bc52338SCy Schubert 	os_free(mgmt);
1484bc52338SCy Schubert 	os_free(oci_ie);
149f05cddf9SRui Paulo }
150f05cddf9SRui Paulo 
151f05cddf9SRui Paulo 
ieee802_11_send_sa_query_resp(struct hostapd_data * hapd,const u8 * sa,const u8 * trans_id)152f05cddf9SRui Paulo static void ieee802_11_send_sa_query_resp(struct hostapd_data *hapd,
153f05cddf9SRui Paulo 					  const u8 *sa, const u8 *trans_id)
154f05cddf9SRui Paulo {
155f05cddf9SRui Paulo 	struct sta_info *sta;
1564bc52338SCy Schubert 	struct ieee80211_mgmt *resp;
1574bc52338SCy Schubert 	u8 *oci_ie = NULL;
1584bc52338SCy Schubert 	u8 oci_ie_len = 0;
159f05cddf9SRui Paulo 	u8 *end;
160*a90b9d01SCy Schubert 	const u8 *own_addr = hapd->own_addr;
161f05cddf9SRui Paulo 
162f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
163f05cddf9SRui Paulo 		   MACSTR, MAC2STR(sa));
164f05cddf9SRui Paulo 	wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
165f05cddf9SRui Paulo 		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
166f05cddf9SRui Paulo 
167f05cddf9SRui Paulo 	sta = ap_get_sta(hapd, sa);
168f05cddf9SRui Paulo 	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
169f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
170f05cddf9SRui Paulo 			   "from unassociated STA " MACSTR, MAC2STR(sa));
171f05cddf9SRui Paulo 		return;
172f05cddf9SRui Paulo 	}
173f05cddf9SRui Paulo 
1744bc52338SCy Schubert #ifdef CONFIG_OCV
1754bc52338SCy Schubert 	if (wpa_auth_uses_ocv(sta->wpa_sm)) {
1764bc52338SCy Schubert 		struct wpa_channel_info ci;
1774bc52338SCy Schubert 
1784bc52338SCy Schubert 		if (hostapd_drv_channel_info(hapd, &ci) != 0) {
1794bc52338SCy Schubert 			wpa_printf(MSG_WARNING,
1804bc52338SCy Schubert 				   "Failed to get channel info for OCI element in SA Query Response");
1814bc52338SCy Schubert 			return;
1824bc52338SCy Schubert 		}
183c1d255d3SCy Schubert #ifdef CONFIG_TESTING_OPTIONS
184c1d255d3SCy Schubert 		if (hapd->conf->oci_freq_override_saquery_resp) {
185c1d255d3SCy Schubert 			wpa_printf(MSG_INFO,
186c1d255d3SCy Schubert 				   "TEST: Override OCI frequency %d -> %u MHz",
187c1d255d3SCy Schubert 				   ci.frequency,
188c1d255d3SCy Schubert 				   hapd->conf->oci_freq_override_saquery_resp);
189c1d255d3SCy Schubert 			ci.frequency =
190c1d255d3SCy Schubert 				hapd->conf->oci_freq_override_saquery_resp;
191c1d255d3SCy Schubert 		}
192c1d255d3SCy Schubert #endif /* CONFIG_TESTING_OPTIONS */
1934bc52338SCy Schubert 
1944bc52338SCy Schubert 		oci_ie_len = OCV_OCI_EXTENDED_LEN;
1954bc52338SCy Schubert 		oci_ie = os_zalloc(oci_ie_len);
1964bc52338SCy Schubert 		if (!oci_ie) {
1974bc52338SCy Schubert 			wpa_printf(MSG_WARNING,
1984bc52338SCy Schubert 				   "Failed to allocate buffer for for OCI element in SA Query Response");
1994bc52338SCy Schubert 			return;
2004bc52338SCy Schubert 		}
2014bc52338SCy Schubert 
2024bc52338SCy Schubert 		if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
2034bc52338SCy Schubert 			os_free(oci_ie);
2044bc52338SCy Schubert 			return;
2054bc52338SCy Schubert 		}
2064bc52338SCy Schubert 	}
2074bc52338SCy Schubert #endif /* CONFIG_OCV */
2084bc52338SCy Schubert 
2094bc52338SCy Schubert 	resp = os_zalloc(sizeof(*resp) + oci_ie_len);
2104bc52338SCy Schubert 	if (!resp) {
2114bc52338SCy Schubert 		wpa_printf(MSG_DEBUG,
2124bc52338SCy Schubert 			   "Failed to allocate buffer for SA Query Response frame");
2134bc52338SCy Schubert 		os_free(oci_ie);
2144bc52338SCy Schubert 		return;
2154bc52338SCy Schubert 	}
2164bc52338SCy Schubert 
217f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
218f05cddf9SRui Paulo 		   MACSTR, MAC2STR(sa));
219f05cddf9SRui Paulo 
220*a90b9d01SCy Schubert #ifdef CONFIG_IEEE80211BE
221*a90b9d01SCy Schubert 	if (ap_sta_is_mld(hapd, sta))
222*a90b9d01SCy Schubert 		own_addr = hapd->mld->mld_addr;
223*a90b9d01SCy Schubert #endif /* CONFIG_IEEE80211BE */
224*a90b9d01SCy Schubert 
2254bc52338SCy Schubert 	resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
226f05cddf9SRui Paulo 					   WLAN_FC_STYPE_ACTION);
2274bc52338SCy Schubert 	os_memcpy(resp->da, sa, ETH_ALEN);
228*a90b9d01SCy Schubert 	os_memcpy(resp->sa, own_addr, ETH_ALEN);
229*a90b9d01SCy Schubert 	os_memcpy(resp->bssid, own_addr, ETH_ALEN);
2304bc52338SCy Schubert 	resp->u.action.category = WLAN_ACTION_SA_QUERY;
2314bc52338SCy Schubert 	resp->u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
2324bc52338SCy Schubert 	os_memcpy(resp->u.action.u.sa_query_req.trans_id, trans_id,
233f05cddf9SRui Paulo 		  WLAN_SA_QUERY_TR_ID_LEN);
2344bc52338SCy Schubert 	end = resp->u.action.u.sa_query_req.variable;
2354bc52338SCy Schubert #ifdef CONFIG_OCV
2364bc52338SCy Schubert 	if (oci_ie_len > 0) {
2374bc52338SCy Schubert 		os_memcpy(end, oci_ie, oci_ie_len);
2384bc52338SCy Schubert 		end += oci_ie_len;
2394bc52338SCy Schubert 	}
2404bc52338SCy Schubert #endif /* CONFIG_OCV */
241c1d255d3SCy Schubert 	if (hostapd_drv_send_mlme(hapd, resp, end - (u8 *) resp, 0, NULL, 0, 0)
242c1d255d3SCy Schubert 	    < 0)
2435b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "ieee80211_mgmt_sa_query_request: send failed");
2444bc52338SCy Schubert 
2454bc52338SCy Schubert 	os_free(resp);
2464bc52338SCy Schubert 	os_free(oci_ie);
247f05cddf9SRui Paulo }
248f05cddf9SRui Paulo 
249f05cddf9SRui Paulo 
ieee802_11_sa_query_action(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len)2504bc52338SCy Schubert void ieee802_11_sa_query_action(struct hostapd_data *hapd,
2514bc52338SCy Schubert 				const struct ieee80211_mgmt *mgmt,
2524bc52338SCy Schubert 				size_t len)
253f05cddf9SRui Paulo {
254f05cddf9SRui Paulo 	struct sta_info *sta;
255f05cddf9SRui Paulo 	int i;
2564bc52338SCy Schubert 	const u8 *sa = mgmt->sa;
2574bc52338SCy Schubert 	const u8 action_type = mgmt->u.action.u.sa_query_resp.action;
2584bc52338SCy Schubert 	const u8 *trans_id = mgmt->u.action.u.sa_query_resp.trans_id;
2594bc52338SCy Schubert 
2604bc52338SCy Schubert 	if (((const u8 *) mgmt) + len <
2614bc52338SCy Schubert 	    mgmt->u.action.u.sa_query_resp.variable) {
2624bc52338SCy Schubert 		wpa_printf(MSG_DEBUG,
2634bc52338SCy Schubert 			   "IEEE 802.11: Too short SA Query Action frame (len=%lu)",
2644bc52338SCy Schubert 			   (unsigned long) len);
2654bc52338SCy Schubert 		return;
2664bc52338SCy Schubert 	}
267c1d255d3SCy Schubert 	if (is_multicast_ether_addr(mgmt->da)) {
268c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG,
269c1d255d3SCy Schubert 			   "IEEE 802.11: Ignore group-addressed SA Query frame (A1=" MACSTR " A2=" MACSTR ")",
270c1d255d3SCy Schubert 			   MAC2STR(mgmt->da), MAC2STR(mgmt->sa));
271c1d255d3SCy Schubert 		return;
272c1d255d3SCy Schubert 	}
2734bc52338SCy Schubert 
2744bc52338SCy Schubert 	sta = ap_get_sta(hapd, sa);
2754bc52338SCy Schubert 
2764bc52338SCy Schubert #ifdef CONFIG_OCV
2774bc52338SCy Schubert 	if (sta && wpa_auth_uses_ocv(sta->wpa_sm)) {
2784bc52338SCy Schubert 		struct ieee802_11_elems elems;
2794bc52338SCy Schubert 		struct wpa_channel_info ci;
2804bc52338SCy Schubert 		int tx_chanwidth;
2814bc52338SCy Schubert 		int tx_seg1_idx;
2824bc52338SCy Schubert 		size_t ies_len;
2834bc52338SCy Schubert 		const u8 *ies;
2844bc52338SCy Schubert 
2854bc52338SCy Schubert 		ies = mgmt->u.action.u.sa_query_resp.variable;
2864bc52338SCy Schubert 		ies_len = len - (ies - (u8 *) mgmt);
2874bc52338SCy Schubert 		if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) ==
2884bc52338SCy Schubert 		    ParseFailed) {
2894bc52338SCy Schubert 			wpa_printf(MSG_DEBUG,
2904bc52338SCy Schubert 				   "SA Query: Failed to parse elements");
2914bc52338SCy Schubert 			return;
2924bc52338SCy Schubert 		}
2934bc52338SCy Schubert 
2944bc52338SCy Schubert 		if (hostapd_drv_channel_info(hapd, &ci) != 0) {
2954bc52338SCy Schubert 			wpa_printf(MSG_WARNING,
2964bc52338SCy Schubert 				   "Failed to get channel info to validate received OCI in SA Query Action frame");
2974bc52338SCy Schubert 			return;
2984bc52338SCy Schubert 		}
2994bc52338SCy Schubert 
3004bc52338SCy Schubert 		if (get_sta_tx_parameters(sta->wpa_sm,
3014bc52338SCy Schubert 					  channel_width_to_int(ci.chanwidth),
3024bc52338SCy Schubert 					  ci.seg1_idx, &tx_chanwidth,
3034bc52338SCy Schubert 					  &tx_seg1_idx) < 0)
3044bc52338SCy Schubert 			return;
3054bc52338SCy Schubert 
3064bc52338SCy Schubert 		if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
307c1d255d3SCy Schubert 					 tx_chanwidth, tx_seg1_idx) !=
308c1d255d3SCy Schubert 		    OCI_SUCCESS) {
309c1d255d3SCy Schubert 			wpa_msg(hapd->msg_ctx, MSG_INFO, OCV_FAILURE "addr="
310c1d255d3SCy Schubert 				MACSTR " frame=saquery%s error=%s",
311c1d255d3SCy Schubert 				MAC2STR(sa),
312c1d255d3SCy Schubert 				action_type == WLAN_SA_QUERY_REQUEST ?
313c1d255d3SCy Schubert 				"req" : "resp", ocv_errorstr);
3144bc52338SCy Schubert 			return;
3154bc52338SCy Schubert 		}
3164bc52338SCy Schubert 	}
3174bc52338SCy Schubert #endif /* CONFIG_OCV */
318f05cddf9SRui Paulo 
319f05cddf9SRui Paulo 	if (action_type == WLAN_SA_QUERY_REQUEST) {
320c1d255d3SCy Schubert 		if (sta)
321c1d255d3SCy Schubert 			sta->post_csa_sa_query = 0;
322f05cddf9SRui Paulo 		ieee802_11_send_sa_query_resp(hapd, sa, trans_id);
323f05cddf9SRui Paulo 		return;
324f05cddf9SRui Paulo 	}
325f05cddf9SRui Paulo 
326f05cddf9SRui Paulo 	if (action_type != WLAN_SA_QUERY_RESPONSE) {
327f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
328f05cddf9SRui Paulo 			   "Action %d", action_type);
329f05cddf9SRui Paulo 		return;
330f05cddf9SRui Paulo 	}
331f05cddf9SRui Paulo 
332f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
333f05cddf9SRui Paulo 		   MACSTR, MAC2STR(sa));
334f05cddf9SRui Paulo 	wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
335f05cddf9SRui Paulo 		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
336f05cddf9SRui Paulo 
337f05cddf9SRui Paulo 	/* MLME-SAQuery.confirm */
338f05cddf9SRui Paulo 
339f05cddf9SRui Paulo 	if (sta == NULL || sta->sa_query_trans_id == NULL) {
340f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
341f05cddf9SRui Paulo 			   "pending SA Query request found");
342f05cddf9SRui Paulo 		return;
343f05cddf9SRui Paulo 	}
344f05cddf9SRui Paulo 
345f05cddf9SRui Paulo 	for (i = 0; i < sta->sa_query_count; i++) {
346f05cddf9SRui Paulo 		if (os_memcmp(sta->sa_query_trans_id +
347f05cddf9SRui Paulo 			      i * WLAN_SA_QUERY_TR_ID_LEN,
348f05cddf9SRui Paulo 			      trans_id, WLAN_SA_QUERY_TR_ID_LEN) == 0)
349f05cddf9SRui Paulo 			break;
350f05cddf9SRui Paulo 	}
351f05cddf9SRui Paulo 
352f05cddf9SRui Paulo 	if (i >= sta->sa_query_count) {
353f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
354f05cddf9SRui Paulo 			   "transaction identifier found");
355f05cddf9SRui Paulo 		return;
356f05cddf9SRui Paulo 	}
357f05cddf9SRui Paulo 
358f05cddf9SRui Paulo 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
359f05cddf9SRui Paulo 		       HOSTAPD_LEVEL_DEBUG,
360f05cddf9SRui Paulo 		       "Reply to pending SA Query received");
361f05cddf9SRui Paulo 	ap_sta_stop_sa_query(hapd, sta);
362f05cddf9SRui Paulo }
363f05cddf9SRui Paulo 
364f05cddf9SRui Paulo 
hostapd_ext_capab_byte(struct hostapd_data * hapd,u8 * pos,int idx,bool mbssid_complete)365*a90b9d01SCy Schubert static void hostapd_ext_capab_byte(struct hostapd_data *hapd, u8 *pos, int idx,
366*a90b9d01SCy Schubert 				   bool mbssid_complete)
367f05cddf9SRui Paulo {
368f05cddf9SRui Paulo 	*pos = 0x00;
3695b9c547cSRui Paulo 
3705b9c547cSRui Paulo 	switch (idx) {
3715b9c547cSRui Paulo 	case 0: /* Bits 0-7 */
3725b9c547cSRui Paulo 		if (hapd->iconf->obss_interval)
3735b9c547cSRui Paulo 			*pos |= 0x01; /* Bit 0 - Coexistence management */
374780fb4a2SCy Schubert 		if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)
375780fb4a2SCy Schubert 			*pos |= 0x04; /* Bit 2 - Extended Channel Switching */
3765b9c547cSRui Paulo 		break;
3775b9c547cSRui Paulo 	case 1: /* Bits 8-15 */
3785b9c547cSRui Paulo 		if (hapd->conf->proxy_arp)
3795b9c547cSRui Paulo 			*pos |= 0x10; /* Bit 12 - Proxy ARP */
38085732ac8SCy Schubert 		if (hapd->conf->coloc_intf_reporting) {
38185732ac8SCy Schubert 			/* Bit 13 - Collocated Interference Reporting */
38285732ac8SCy Schubert 			*pos |= 0x20;
38385732ac8SCy Schubert 		}
3845b9c547cSRui Paulo 		break;
3855b9c547cSRui Paulo 	case 2: /* Bits 16-23 */
386f05cddf9SRui Paulo 		if (hapd->conf->wnm_sleep_mode)
387f05cddf9SRui Paulo 			*pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
388f05cddf9SRui Paulo 		if (hapd->conf->bss_transition)
389f05cddf9SRui Paulo 			*pos |= 0x08; /* Bit 19 - BSS Transition */
390*a90b9d01SCy Schubert 		if (hapd->iconf->mbssid)
391*a90b9d01SCy Schubert 			*pos |= 0x40; /* Bit 22 - Multiple BSSID */
3925b9c547cSRui Paulo 		break;
3935b9c547cSRui Paulo 	case 3: /* Bits 24-31 */
39485732ac8SCy Schubert #ifdef CONFIG_WNM_AP
395f05cddf9SRui Paulo 		*pos |= 0x02; /* Bit 25 - SSID List */
39685732ac8SCy Schubert #endif /* CONFIG_WNM_AP */
397f05cddf9SRui Paulo 		if (hapd->conf->time_advertisement == 2)
398f05cddf9SRui Paulo 			*pos |= 0x08; /* Bit 27 - UTC TSF Offset */
399f05cddf9SRui Paulo 		if (hapd->conf->interworking)
400f05cddf9SRui Paulo 			*pos |= 0x80; /* Bit 31 - Interworking */
4015b9c547cSRui Paulo 		break;
4025b9c547cSRui Paulo 	case 4: /* Bits 32-39 */
4035b9c547cSRui Paulo 		if (hapd->conf->qos_map_set_len)
4045b9c547cSRui Paulo 			*pos |= 0x01; /* Bit 32 - QoS Map */
405f05cddf9SRui Paulo 		if (hapd->conf->tdls & TDLS_PROHIBIT)
406f05cddf9SRui Paulo 			*pos |= 0x40; /* Bit 38 - TDLS Prohibited */
4075b9c547cSRui Paulo 		if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH) {
4085b9c547cSRui Paulo 			/* Bit 39 - TDLS Channel Switching Prohibited */
4095b9c547cSRui Paulo 			*pos |= 0x80;
4105b9c547cSRui Paulo 		}
4115b9c547cSRui Paulo 		break;
4125b9c547cSRui Paulo 	case 5: /* Bits 40-47 */
4135b9c547cSRui Paulo #ifdef CONFIG_HS20
4145b9c547cSRui Paulo 		if (hapd->conf->hs20)
4155b9c547cSRui Paulo 			*pos |= 0x40; /* Bit 46 - WNM-Notification */
4165b9c547cSRui Paulo #endif /* CONFIG_HS20 */
417780fb4a2SCy Schubert #ifdef CONFIG_MBO
418780fb4a2SCy Schubert 		if (hapd->conf->mbo_enabled)
419780fb4a2SCy Schubert 			*pos |= 0x40; /* Bit 46 - WNM-Notification */
420780fb4a2SCy Schubert #endif /* CONFIG_MBO */
4215b9c547cSRui Paulo 		break;
4225b9c547cSRui Paulo 	case 6: /* Bits 48-55 */
423f05cddf9SRui Paulo 		if (hapd->conf->ssid.utf8_ssid)
424f05cddf9SRui Paulo 			*pos |= 0x01; /* Bit 48 - UTF-8 SSID */
4255b9c547cSRui Paulo 		break;
42685732ac8SCy Schubert 	case 7: /* Bits 56-63 */
42785732ac8SCy Schubert 		break;
428780fb4a2SCy Schubert 	case 8: /* Bits 64-71 */
429780fb4a2SCy Schubert 		if (hapd->conf->ftm_responder)
430780fb4a2SCy Schubert 			*pos |= 0x40; /* Bit 70 - FTM responder */
431780fb4a2SCy Schubert 		if (hapd->conf->ftm_initiator)
432780fb4a2SCy Schubert 			*pos |= 0x80; /* Bit 71 - FTM initiator */
433780fb4a2SCy Schubert 		break;
43485732ac8SCy Schubert 	case 9: /* Bits 72-79 */
43585732ac8SCy Schubert #ifdef CONFIG_FILS
43685732ac8SCy Schubert 		if ((hapd->conf->wpa & WPA_PROTO_RSN) &&
43785732ac8SCy Schubert 		    wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt))
43885732ac8SCy Schubert 			*pos |= 0x01;
43985732ac8SCy Schubert #endif /* CONFIG_FILS */
440c1d255d3SCy Schubert #ifdef CONFIG_IEEE80211AX
441*a90b9d01SCy Schubert 		if (hostapd_get_he_twt_responder(hapd, IEEE80211_MODE_AP))
442c1d255d3SCy Schubert 			*pos |= 0x40; /* Bit 78 - TWT responder */
443c1d255d3SCy Schubert #endif /* CONFIG_IEEE80211AX */
444*a90b9d01SCy Schubert 		if (hostapd_get_ht_vht_twt_responder(hapd))
445*a90b9d01SCy Schubert 			*pos |= 0x40; /* Bit 78 - TWT responder */
44685732ac8SCy Schubert 		break;
4474bc52338SCy Schubert 	case 10: /* Bits 80-87 */
4484bc52338SCy Schubert #ifdef CONFIG_SAE
4494bc52338SCy Schubert 		if (hapd->conf->wpa &&
4504bc52338SCy Schubert 		    wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt)) {
4514bc52338SCy Schubert 			int in_use = hostapd_sae_pw_id_in_use(hapd->conf);
4524bc52338SCy Schubert 
4534bc52338SCy Schubert 			if (in_use)
4544bc52338SCy Schubert 				*pos |= 0x02; /* Bit 81 - SAE Password
4554bc52338SCy Schubert 					       * Identifiers In Use */
4564bc52338SCy Schubert 			if (in_use == 2)
4574bc52338SCy Schubert 				*pos |= 0x04; /* Bit 82 - SAE Password
4584bc52338SCy Schubert 					       * Identifiers Used Exclusively */
4594bc52338SCy Schubert 		}
4604bc52338SCy Schubert #endif /* CONFIG_SAE */
461c1d255d3SCy Schubert 		if (hapd->conf->beacon_prot &&
462c1d255d3SCy Schubert 		    (hapd->iface->drv_flags &
463c1d255d3SCy Schubert 		     WPA_DRIVER_FLAGS_BEACON_PROTECTION))
464c1d255d3SCy Schubert 			*pos |= 0x10; /* Bit 84 - Beacon Protection Enabled */
465*a90b9d01SCy Schubert 		if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED)
466*a90b9d01SCy Schubert 			*pos |= 0x08; /* Bit 83 - Enhanced multiple BSSID */
467*a90b9d01SCy Schubert 		if (mbssid_complete)
468*a90b9d01SCy Schubert 			*pos |= 0x01; /* Bit 80 - Complete List of NonTxBSSID
469*a90b9d01SCy Schubert 				       * Profiles */
470c1d255d3SCy Schubert 		break;
471c1d255d3SCy Schubert 	case 11: /* Bits 88-95 */
472c1d255d3SCy Schubert #ifdef CONFIG_SAE_PK
473c1d255d3SCy Schubert 		if (hapd->conf->wpa &&
474c1d255d3SCy Schubert 		    wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
475c1d255d3SCy Schubert 		    hostapd_sae_pk_exclusively(hapd->conf))
476c1d255d3SCy Schubert 			*pos |= 0x01; /* Bit 88 - SAE PK Exclusively */
477c1d255d3SCy Schubert #endif /* CONFIG_SAE_PK */
4784bc52338SCy Schubert 		break;
4795b9c547cSRui Paulo 	}
4805b9c547cSRui Paulo }
4815b9c547cSRui Paulo 
4825b9c547cSRui Paulo 
hostapd_eid_ext_capab(struct hostapd_data * hapd,u8 * eid,bool mbssid_complete)483*a90b9d01SCy Schubert u8 * hostapd_eid_ext_capab(struct hostapd_data *hapd, u8 *eid,
484*a90b9d01SCy Schubert 			   bool mbssid_complete)
4855b9c547cSRui Paulo {
4865b9c547cSRui Paulo 	u8 *pos = eid;
487c1d255d3SCy Schubert 	u8 len = EXT_CAPA_MAX_LEN, i;
4885b9c547cSRui Paulo 
4895b9c547cSRui Paulo 	if (len < hapd->iface->extended_capa_len)
4905b9c547cSRui Paulo 		len = hapd->iface->extended_capa_len;
4915b9c547cSRui Paulo 
4925b9c547cSRui Paulo 	*pos++ = WLAN_EID_EXT_CAPAB;
4935b9c547cSRui Paulo 	*pos++ = len;
4945b9c547cSRui Paulo 	for (i = 0; i < len; i++, pos++) {
495*a90b9d01SCy Schubert 		hostapd_ext_capab_byte(hapd, pos, i, mbssid_complete);
4965b9c547cSRui Paulo 
4975b9c547cSRui Paulo 		if (i < hapd->iface->extended_capa_len) {
4985b9c547cSRui Paulo 			*pos &= ~hapd->iface->extended_capa_mask[i];
4995b9c547cSRui Paulo 			*pos |= hapd->iface->extended_capa[i];
5005b9c547cSRui Paulo 		}
501c1d255d3SCy Schubert 
502c1d255d3SCy Schubert 		if (i < EXT_CAPA_MAX_LEN) {
503c1d255d3SCy Schubert 			*pos &= ~hapd->conf->ext_capa_mask[i];
504c1d255d3SCy Schubert 			*pos |= hapd->conf->ext_capa[i];
505c1d255d3SCy Schubert 		}
506*a90b9d01SCy Schubert 
507*a90b9d01SCy Schubert 		/* Clear bits 83 and 22 if EMA and MBSSID are not enabled
508*a90b9d01SCy Schubert 		 * otherwise association fails with some clients */
509*a90b9d01SCy Schubert 		if (i == 10 && hapd->iconf->mbssid < ENHANCED_MBSSID_ENABLED)
510*a90b9d01SCy Schubert 			*pos &= ~0x08;
511*a90b9d01SCy Schubert 		if (i == 2 && !hapd->iconf->mbssid)
512*a90b9d01SCy Schubert 			*pos &= ~0x40;
5135b9c547cSRui Paulo 	}
5145b9c547cSRui Paulo 
5155b9c547cSRui Paulo 	while (len > 0 && eid[1 + len] == 0) {
5165b9c547cSRui Paulo 		len--;
5175b9c547cSRui Paulo 		eid[1] = len;
5185b9c547cSRui Paulo 	}
5195b9c547cSRui Paulo 	if (len == 0)
5205b9c547cSRui Paulo 		return eid;
5215b9c547cSRui Paulo 
5225b9c547cSRui Paulo 	return eid + 2 + len;
5235b9c547cSRui Paulo }
5245b9c547cSRui Paulo 
5255b9c547cSRui Paulo 
hostapd_eid_qos_map_set(struct hostapd_data * hapd,u8 * eid)5265b9c547cSRui Paulo u8 * hostapd_eid_qos_map_set(struct hostapd_data *hapd, u8 *eid)
5275b9c547cSRui Paulo {
5285b9c547cSRui Paulo 	u8 *pos = eid;
5295b9c547cSRui Paulo 	u8 len = hapd->conf->qos_map_set_len;
5305b9c547cSRui Paulo 
5315b9c547cSRui Paulo 	if (!len)
5325b9c547cSRui Paulo 		return eid;
5335b9c547cSRui Paulo 
5345b9c547cSRui Paulo 	*pos++ = WLAN_EID_QOS_MAP_SET;
5355b9c547cSRui Paulo 	*pos++ = len;
5365b9c547cSRui Paulo 	os_memcpy(pos, hapd->conf->qos_map_set, len);
5375b9c547cSRui Paulo 	pos += len;
538f05cddf9SRui Paulo 
539f05cddf9SRui Paulo 	return pos;
540f05cddf9SRui Paulo }
541f05cddf9SRui Paulo 
542f05cddf9SRui Paulo 
hostapd_eid_interworking(struct hostapd_data * hapd,u8 * eid)543f05cddf9SRui Paulo u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid)
544f05cddf9SRui Paulo {
545f05cddf9SRui Paulo 	u8 *pos = eid;
546f05cddf9SRui Paulo #ifdef CONFIG_INTERWORKING
547f05cddf9SRui Paulo 	u8 *len;
548f05cddf9SRui Paulo 
549f05cddf9SRui Paulo 	if (!hapd->conf->interworking)
550f05cddf9SRui Paulo 		return eid;
551f05cddf9SRui Paulo 
552f05cddf9SRui Paulo 	*pos++ = WLAN_EID_INTERWORKING;
553f05cddf9SRui Paulo 	len = pos++;
554f05cddf9SRui Paulo 
555f05cddf9SRui Paulo 	*pos = hapd->conf->access_network_type;
556f05cddf9SRui Paulo 	if (hapd->conf->internet)
557f05cddf9SRui Paulo 		*pos |= INTERWORKING_ANO_INTERNET;
558f05cddf9SRui Paulo 	if (hapd->conf->asra)
559f05cddf9SRui Paulo 		*pos |= INTERWORKING_ANO_ASRA;
560f05cddf9SRui Paulo 	if (hapd->conf->esr)
561f05cddf9SRui Paulo 		*pos |= INTERWORKING_ANO_ESR;
562f05cddf9SRui Paulo 	if (hapd->conf->uesa)
563f05cddf9SRui Paulo 		*pos |= INTERWORKING_ANO_UESA;
564f05cddf9SRui Paulo 	pos++;
565f05cddf9SRui Paulo 
566f05cddf9SRui Paulo 	if (hapd->conf->venue_info_set) {
567f05cddf9SRui Paulo 		*pos++ = hapd->conf->venue_group;
568f05cddf9SRui Paulo 		*pos++ = hapd->conf->venue_type;
569f05cddf9SRui Paulo 	}
570f05cddf9SRui Paulo 
571f05cddf9SRui Paulo 	if (!is_zero_ether_addr(hapd->conf->hessid)) {
572f05cddf9SRui Paulo 		os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
573f05cddf9SRui Paulo 		pos += ETH_ALEN;
574f05cddf9SRui Paulo 	}
575f05cddf9SRui Paulo 
576f05cddf9SRui Paulo 	*len = pos - len - 1;
577f05cddf9SRui Paulo #endif /* CONFIG_INTERWORKING */
578f05cddf9SRui Paulo 
579f05cddf9SRui Paulo 	return pos;
580f05cddf9SRui Paulo }
581f05cddf9SRui Paulo 
582f05cddf9SRui Paulo 
hostapd_eid_adv_proto(struct hostapd_data * hapd,u8 * eid)583f05cddf9SRui Paulo u8 * hostapd_eid_adv_proto(struct hostapd_data *hapd, u8 *eid)
584f05cddf9SRui Paulo {
585f05cddf9SRui Paulo 	u8 *pos = eid;
586f05cddf9SRui Paulo #ifdef CONFIG_INTERWORKING
587f05cddf9SRui Paulo 
588f05cddf9SRui Paulo 	/* TODO: Separate configuration for ANQP? */
589f05cddf9SRui Paulo 	if (!hapd->conf->interworking)
590f05cddf9SRui Paulo 		return eid;
591f05cddf9SRui Paulo 
592f05cddf9SRui Paulo 	*pos++ = WLAN_EID_ADV_PROTO;
593f05cddf9SRui Paulo 	*pos++ = 2;
594f05cddf9SRui Paulo 	*pos++ = 0x7F; /* Query Response Length Limit | PAME-BI */
595f05cddf9SRui Paulo 	*pos++ = ACCESS_NETWORK_QUERY_PROTOCOL;
596f05cddf9SRui Paulo #endif /* CONFIG_INTERWORKING */
597f05cddf9SRui Paulo 
598f05cddf9SRui Paulo 	return pos;
599f05cddf9SRui Paulo }
600f05cddf9SRui Paulo 
601f05cddf9SRui Paulo 
hostapd_eid_roaming_consortium(struct hostapd_data * hapd,u8 * eid)602f05cddf9SRui Paulo u8 * hostapd_eid_roaming_consortium(struct hostapd_data *hapd, u8 *eid)
603f05cddf9SRui Paulo {
604f05cddf9SRui Paulo 	u8 *pos = eid;
605f05cddf9SRui Paulo #ifdef CONFIG_INTERWORKING
606f05cddf9SRui Paulo 	u8 *len;
607f05cddf9SRui Paulo 	unsigned int i, count;
608f05cddf9SRui Paulo 
609f05cddf9SRui Paulo 	if (!hapd->conf->interworking ||
610f05cddf9SRui Paulo 	    hapd->conf->roaming_consortium == NULL ||
611f05cddf9SRui Paulo 	    hapd->conf->roaming_consortium_count == 0)
612f05cddf9SRui Paulo 		return eid;
613f05cddf9SRui Paulo 
614f05cddf9SRui Paulo 	*pos++ = WLAN_EID_ROAMING_CONSORTIUM;
615f05cddf9SRui Paulo 	len = pos++;
616f05cddf9SRui Paulo 
617f05cddf9SRui Paulo 	/* Number of ANQP OIs (in addition to the max 3 listed here) */
618f05cddf9SRui Paulo 	if (hapd->conf->roaming_consortium_count > 3 + 255)
619f05cddf9SRui Paulo 		*pos++ = 255;
620f05cddf9SRui Paulo 	else if (hapd->conf->roaming_consortium_count > 3)
621f05cddf9SRui Paulo 		*pos++ = hapd->conf->roaming_consortium_count - 3;
622f05cddf9SRui Paulo 	else
623f05cddf9SRui Paulo 		*pos++ = 0;
624f05cddf9SRui Paulo 
625f05cddf9SRui Paulo 	/* OU #1 and #2 Lengths */
626f05cddf9SRui Paulo 	*pos = hapd->conf->roaming_consortium[0].len;
627f05cddf9SRui Paulo 	if (hapd->conf->roaming_consortium_count > 1)
628f05cddf9SRui Paulo 		*pos |= hapd->conf->roaming_consortium[1].len << 4;
629f05cddf9SRui Paulo 	pos++;
630f05cddf9SRui Paulo 
631f05cddf9SRui Paulo 	if (hapd->conf->roaming_consortium_count > 3)
632f05cddf9SRui Paulo 		count = 3;
633f05cddf9SRui Paulo 	else
634f05cddf9SRui Paulo 		count = hapd->conf->roaming_consortium_count;
635f05cddf9SRui Paulo 
636f05cddf9SRui Paulo 	for (i = 0; i < count; i++) {
637f05cddf9SRui Paulo 		os_memcpy(pos, hapd->conf->roaming_consortium[i].oi,
638f05cddf9SRui Paulo 			  hapd->conf->roaming_consortium[i].len);
639f05cddf9SRui Paulo 		pos += hapd->conf->roaming_consortium[i].len;
640f05cddf9SRui Paulo 	}
641f05cddf9SRui Paulo 
642f05cddf9SRui Paulo 	*len = pos - len - 1;
643f05cddf9SRui Paulo #endif /* CONFIG_INTERWORKING */
644f05cddf9SRui Paulo 
645f05cddf9SRui Paulo 	return pos;
646f05cddf9SRui Paulo }
647f05cddf9SRui Paulo 
648f05cddf9SRui Paulo 
hostapd_eid_time_adv(struct hostapd_data * hapd,u8 * eid)649f05cddf9SRui Paulo u8 * hostapd_eid_time_adv(struct hostapd_data *hapd, u8 *eid)
650f05cddf9SRui Paulo {
651f05cddf9SRui Paulo 	if (hapd->conf->time_advertisement != 2)
652f05cddf9SRui Paulo 		return eid;
653f05cddf9SRui Paulo 
654f05cddf9SRui Paulo 	if (hapd->time_adv == NULL &&
655f05cddf9SRui Paulo 	    hostapd_update_time_adv(hapd) < 0)
656f05cddf9SRui Paulo 		return eid;
657f05cddf9SRui Paulo 
658f05cddf9SRui Paulo 	if (hapd->time_adv == NULL)
659f05cddf9SRui Paulo 		return eid;
660f05cddf9SRui Paulo 
661f05cddf9SRui Paulo 	os_memcpy(eid, wpabuf_head(hapd->time_adv),
662f05cddf9SRui Paulo 		  wpabuf_len(hapd->time_adv));
663f05cddf9SRui Paulo 	eid += wpabuf_len(hapd->time_adv);
664f05cddf9SRui Paulo 
665f05cddf9SRui Paulo 	return eid;
666f05cddf9SRui Paulo }
667f05cddf9SRui Paulo 
668f05cddf9SRui Paulo 
hostapd_eid_time_zone(struct hostapd_data * hapd,u8 * eid)669f05cddf9SRui Paulo u8 * hostapd_eid_time_zone(struct hostapd_data *hapd, u8 *eid)
670f05cddf9SRui Paulo {
671f05cddf9SRui Paulo 	size_t len;
672f05cddf9SRui Paulo 
67385732ac8SCy Schubert 	if (hapd->conf->time_advertisement != 2 || !hapd->conf->time_zone)
674f05cddf9SRui Paulo 		return eid;
675f05cddf9SRui Paulo 
676f05cddf9SRui Paulo 	len = os_strlen(hapd->conf->time_zone);
677f05cddf9SRui Paulo 
678f05cddf9SRui Paulo 	*eid++ = WLAN_EID_TIME_ZONE;
679f05cddf9SRui Paulo 	*eid++ = len;
680f05cddf9SRui Paulo 	os_memcpy(eid, hapd->conf->time_zone, len);
681f05cddf9SRui Paulo 	eid += len;
682f05cddf9SRui Paulo 
683f05cddf9SRui Paulo 	return eid;
684f05cddf9SRui Paulo }
685f05cddf9SRui Paulo 
686f05cddf9SRui Paulo 
hostapd_update_time_adv(struct hostapd_data * hapd)687f05cddf9SRui Paulo int hostapd_update_time_adv(struct hostapd_data *hapd)
688f05cddf9SRui Paulo {
689f05cddf9SRui Paulo 	const int elen = 2 + 1 + 10 + 5 + 1;
690f05cddf9SRui Paulo 	struct os_time t;
691f05cddf9SRui Paulo 	struct os_tm tm;
692f05cddf9SRui Paulo 	u8 *pos;
693f05cddf9SRui Paulo 
694f05cddf9SRui Paulo 	if (hapd->conf->time_advertisement != 2)
695f05cddf9SRui Paulo 		return 0;
696f05cddf9SRui Paulo 
697f05cddf9SRui Paulo 	if (os_get_time(&t) < 0 || os_gmtime(t.sec, &tm) < 0)
698f05cddf9SRui Paulo 		return -1;
699f05cddf9SRui Paulo 
700f05cddf9SRui Paulo 	if (!hapd->time_adv) {
701f05cddf9SRui Paulo 		hapd->time_adv = wpabuf_alloc(elen);
702f05cddf9SRui Paulo 		if (hapd->time_adv == NULL)
703f05cddf9SRui Paulo 			return -1;
704f05cddf9SRui Paulo 		pos = wpabuf_put(hapd->time_adv, elen);
705f05cddf9SRui Paulo 	} else
706f05cddf9SRui Paulo 		pos = wpabuf_mhead_u8(hapd->time_adv);
707f05cddf9SRui Paulo 
708f05cddf9SRui Paulo 	*pos++ = WLAN_EID_TIME_ADVERTISEMENT;
709f05cddf9SRui Paulo 	*pos++ = 1 + 10 + 5 + 1;
710f05cddf9SRui Paulo 
711f05cddf9SRui Paulo 	*pos++ = 2; /* UTC time at which the TSF timer is 0 */
712f05cddf9SRui Paulo 
713f05cddf9SRui Paulo 	/* Time Value at TSF 0 */
714f05cddf9SRui Paulo 	/* FIX: need to calculate this based on the current TSF value */
715f05cddf9SRui Paulo 	WPA_PUT_LE16(pos, tm.year); /* Year */
716f05cddf9SRui Paulo 	pos += 2;
717f05cddf9SRui Paulo 	*pos++ = tm.month; /* Month */
718f05cddf9SRui Paulo 	*pos++ = tm.day; /* Day of month */
719f05cddf9SRui Paulo 	*pos++ = tm.hour; /* Hours */
720f05cddf9SRui Paulo 	*pos++ = tm.min; /* Minutes */
721f05cddf9SRui Paulo 	*pos++ = tm.sec; /* Seconds */
722f05cddf9SRui Paulo 	WPA_PUT_LE16(pos, 0); /* Milliseconds (not used) */
723f05cddf9SRui Paulo 	pos += 2;
724f05cddf9SRui Paulo 	*pos++ = 0; /* Reserved */
725f05cddf9SRui Paulo 
726f05cddf9SRui Paulo 	/* Time Error */
727f05cddf9SRui Paulo 	/* TODO: fill in an estimate on the error */
728f05cddf9SRui Paulo 	*pos++ = 0;
729f05cddf9SRui Paulo 	*pos++ = 0;
730f05cddf9SRui Paulo 	*pos++ = 0;
731f05cddf9SRui Paulo 	*pos++ = 0;
732f05cddf9SRui Paulo 	*pos++ = 0;
733f05cddf9SRui Paulo 
734f05cddf9SRui Paulo 	*pos++ = hapd->time_update_counter++;
735f05cddf9SRui Paulo 
736f05cddf9SRui Paulo 	return 0;
737f05cddf9SRui Paulo }
738f05cddf9SRui Paulo 
739f05cddf9SRui Paulo 
hostapd_eid_bss_max_idle_period(struct hostapd_data * hapd,u8 * eid,u16 value)740*a90b9d01SCy Schubert u8 * hostapd_eid_bss_max_idle_period(struct hostapd_data *hapd, u8 *eid,
741*a90b9d01SCy Schubert 				     u16 value)
742f05cddf9SRui Paulo {
743f05cddf9SRui Paulo 	u8 *pos = eid;
744f05cddf9SRui Paulo 
74585732ac8SCy Schubert #ifdef CONFIG_WNM_AP
746*a90b9d01SCy Schubert 	if (hapd->conf->ap_max_inactivity > 0 &&
747*a90b9d01SCy Schubert 	    hapd->conf->bss_max_idle) {
748f05cddf9SRui Paulo 		unsigned int val;
749f05cddf9SRui Paulo 		*pos++ = WLAN_EID_BSS_MAX_IDLE_PERIOD;
750f05cddf9SRui Paulo 		*pos++ = 3;
751f05cddf9SRui Paulo 		val = hapd->conf->ap_max_inactivity;
752f05cddf9SRui Paulo 		if (val > 68000)
753f05cddf9SRui Paulo 			val = 68000;
754f05cddf9SRui Paulo 		val *= 1000;
755f05cddf9SRui Paulo 		val /= 1024;
756f05cddf9SRui Paulo 		if (val == 0)
757f05cddf9SRui Paulo 			val = 1;
758f05cddf9SRui Paulo 		if (val > 65535)
759f05cddf9SRui Paulo 			val = 65535;
760*a90b9d01SCy Schubert 		if (value)
761*a90b9d01SCy Schubert 			val = value;
762f05cddf9SRui Paulo 		WPA_PUT_LE16(pos, val);
763f05cddf9SRui Paulo 		pos += 2;
764*a90b9d01SCy Schubert 		/* Set the Protected Keep-Alive Required bit based on
765*a90b9d01SCy Schubert 		 * configuration */
766*a90b9d01SCy Schubert 		*pos++ = hapd->conf->bss_max_idle == 2 ? BIT(0) : 0x00;
767f05cddf9SRui Paulo 	}
76885732ac8SCy Schubert #endif /* CONFIG_WNM_AP */
769f05cddf9SRui Paulo 
770f05cddf9SRui Paulo 	return pos;
771f05cddf9SRui Paulo }
772780fb4a2SCy Schubert 
773780fb4a2SCy Schubert 
774780fb4a2SCy Schubert #ifdef CONFIG_MBO
775780fb4a2SCy Schubert 
hostapd_eid_mbo_rssi_assoc_rej(struct hostapd_data * hapd,u8 * eid,size_t len,int delta)7764bc52338SCy Schubert u8 * hostapd_eid_mbo_rssi_assoc_rej(struct hostapd_data *hapd, u8 *eid,
7774bc52338SCy Schubert 				    size_t len, int delta)
7784bc52338SCy Schubert {
7794bc52338SCy Schubert 	u8 mbo[4];
7804bc52338SCy Schubert 
7814bc52338SCy Schubert 	mbo[0] = OCE_ATTR_ID_RSSI_BASED_ASSOC_REJECT;
7824bc52338SCy Schubert 	mbo[1] = 2;
7834bc52338SCy Schubert 	/* Delta RSSI */
7844bc52338SCy Schubert 	mbo[2] = delta;
7854bc52338SCy Schubert 	/* Retry delay */
7864bc52338SCy Schubert 	mbo[3] = hapd->iconf->rssi_reject_assoc_timeout;
7874bc52338SCy Schubert 
7884bc52338SCy Schubert 	return eid + mbo_add_ie(eid, len, mbo, 4);
7894bc52338SCy Schubert }
7904bc52338SCy Schubert 
7914bc52338SCy Schubert 
hostapd_eid_mbo(struct hostapd_data * hapd,u8 * eid,size_t len)792780fb4a2SCy Schubert u8 * hostapd_eid_mbo(struct hostapd_data *hapd, u8 *eid, size_t len)
793780fb4a2SCy Schubert {
79485732ac8SCy Schubert 	u8 mbo[9], *mbo_pos = mbo;
795780fb4a2SCy Schubert 	u8 *pos = eid;
796780fb4a2SCy Schubert 
79785732ac8SCy Schubert 	if (!hapd->conf->mbo_enabled &&
79885732ac8SCy Schubert 	    !OCE_STA_CFON_ENABLED(hapd) && !OCE_AP_ENABLED(hapd))
799780fb4a2SCy Schubert 		return eid;
800780fb4a2SCy Schubert 
80185732ac8SCy Schubert 	if (hapd->conf->mbo_enabled) {
802780fb4a2SCy Schubert 		*mbo_pos++ = MBO_ATTR_ID_AP_CAPA_IND;
803780fb4a2SCy Schubert 		*mbo_pos++ = 1;
804780fb4a2SCy Schubert 		/* Not Cellular aware */
805780fb4a2SCy Schubert 		*mbo_pos++ = 0;
80685732ac8SCy Schubert 	}
807780fb4a2SCy Schubert 
80885732ac8SCy Schubert 	if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
809780fb4a2SCy Schubert 		*mbo_pos++ = MBO_ATTR_ID_ASSOC_DISALLOW;
810780fb4a2SCy Schubert 		*mbo_pos++ = 1;
811780fb4a2SCy Schubert 		*mbo_pos++ = hapd->mbo_assoc_disallow;
812780fb4a2SCy Schubert 	}
813780fb4a2SCy Schubert 
81485732ac8SCy Schubert 	if (OCE_STA_CFON_ENABLED(hapd) || OCE_AP_ENABLED(hapd)) {
81585732ac8SCy Schubert 		u8 ctrl;
81685732ac8SCy Schubert 
81785732ac8SCy Schubert 		ctrl = OCE_RELEASE;
81885732ac8SCy Schubert 		if (OCE_STA_CFON_ENABLED(hapd) && !OCE_AP_ENABLED(hapd))
81985732ac8SCy Schubert 			ctrl |= OCE_IS_STA_CFON;
82085732ac8SCy Schubert 
82185732ac8SCy Schubert 		*mbo_pos++ = OCE_ATTR_ID_CAPA_IND;
82285732ac8SCy Schubert 		*mbo_pos++ = 1;
82385732ac8SCy Schubert 		*mbo_pos++ = ctrl;
82485732ac8SCy Schubert 	}
82585732ac8SCy Schubert 
826780fb4a2SCy Schubert 	pos += mbo_add_ie(pos, len, mbo, mbo_pos - mbo);
827780fb4a2SCy Schubert 
828780fb4a2SCy Schubert 	return pos;
829780fb4a2SCy Schubert }
830780fb4a2SCy Schubert 
831780fb4a2SCy Schubert 
hostapd_mbo_ie_len(struct hostapd_data * hapd)832780fb4a2SCy Schubert u8 hostapd_mbo_ie_len(struct hostapd_data *hapd)
833780fb4a2SCy Schubert {
83485732ac8SCy Schubert 	u8 len;
83585732ac8SCy Schubert 
83685732ac8SCy Schubert 	if (!hapd->conf->mbo_enabled &&
83785732ac8SCy Schubert 	    !OCE_STA_CFON_ENABLED(hapd) && !OCE_AP_ENABLED(hapd))
838780fb4a2SCy Schubert 		return 0;
839780fb4a2SCy Schubert 
840780fb4a2SCy Schubert 	/*
841780fb4a2SCy Schubert 	 * MBO IE header (6) + Capability Indication attribute (3) +
842780fb4a2SCy Schubert 	 * Association Disallowed attribute (3) = 12
843780fb4a2SCy Schubert 	 */
84485732ac8SCy Schubert 	len = 6;
84585732ac8SCy Schubert 	if (hapd->conf->mbo_enabled)
84685732ac8SCy Schubert 		len += 3 + (hapd->mbo_assoc_disallow ? 3 : 0);
84785732ac8SCy Schubert 
84885732ac8SCy Schubert 	/* OCE capability indication attribute (3) */
84985732ac8SCy Schubert 	if (OCE_STA_CFON_ENABLED(hapd) || OCE_AP_ENABLED(hapd))
85085732ac8SCy Schubert 		len += 3;
85185732ac8SCy Schubert 
85285732ac8SCy Schubert 	return len;
853780fb4a2SCy Schubert }
854780fb4a2SCy Schubert 
855780fb4a2SCy Schubert #endif /* CONFIG_MBO */
856780fb4a2SCy Schubert 
857780fb4a2SCy Schubert 
85885732ac8SCy Schubert #ifdef CONFIG_OWE
hostapd_eid_owe_trans_enabled(struct hostapd_data * hapd)85985732ac8SCy Schubert static int hostapd_eid_owe_trans_enabled(struct hostapd_data *hapd)
86085732ac8SCy Schubert {
86185732ac8SCy Schubert 	return hapd->conf->owe_transition_ssid_len > 0 &&
86285732ac8SCy Schubert 		!is_zero_ether_addr(hapd->conf->owe_transition_bssid);
86385732ac8SCy Schubert }
86485732ac8SCy Schubert #endif /* CONFIG_OWE */
86585732ac8SCy Schubert 
86685732ac8SCy Schubert 
hostapd_eid_owe_trans_len(struct hostapd_data * hapd)86785732ac8SCy Schubert size_t hostapd_eid_owe_trans_len(struct hostapd_data *hapd)
86885732ac8SCy Schubert {
86985732ac8SCy Schubert #ifdef CONFIG_OWE
87085732ac8SCy Schubert 	if (!hostapd_eid_owe_trans_enabled(hapd))
87185732ac8SCy Schubert 		return 0;
87285732ac8SCy Schubert 	return 6 + ETH_ALEN + 1 + hapd->conf->owe_transition_ssid_len;
87385732ac8SCy Schubert #else /* CONFIG_OWE */
87485732ac8SCy Schubert 	return 0;
87585732ac8SCy Schubert #endif /* CONFIG_OWE */
87685732ac8SCy Schubert }
87785732ac8SCy Schubert 
87885732ac8SCy Schubert 
hostapd_eid_owe_trans(struct hostapd_data * hapd,u8 * eid,size_t len)87985732ac8SCy Schubert u8 * hostapd_eid_owe_trans(struct hostapd_data *hapd, u8 *eid,
88085732ac8SCy Schubert 				  size_t len)
88185732ac8SCy Schubert {
88285732ac8SCy Schubert #ifdef CONFIG_OWE
88385732ac8SCy Schubert 	u8 *pos = eid;
88485732ac8SCy Schubert 	size_t elen;
88585732ac8SCy Schubert 
88685732ac8SCy Schubert 	if (hapd->conf->owe_transition_ifname[0] &&
88785732ac8SCy Schubert 	    !hostapd_eid_owe_trans_enabled(hapd))
88885732ac8SCy Schubert 		hostapd_owe_trans_get_info(hapd);
88985732ac8SCy Schubert 
89085732ac8SCy Schubert 	if (!hostapd_eid_owe_trans_enabled(hapd))
89185732ac8SCy Schubert 		return pos;
89285732ac8SCy Schubert 
89385732ac8SCy Schubert 	elen = hostapd_eid_owe_trans_len(hapd);
89485732ac8SCy Schubert 	if (len < elen) {
89585732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
89685732ac8SCy Schubert 			   "OWE: Not enough room in the buffer for OWE IE");
89785732ac8SCy Schubert 		return pos;
89885732ac8SCy Schubert 	}
89985732ac8SCy Schubert 
90085732ac8SCy Schubert 	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
90185732ac8SCy Schubert 	*pos++ = elen - 2;
90285732ac8SCy Schubert 	WPA_PUT_BE24(pos, OUI_WFA);
90385732ac8SCy Schubert 	pos += 3;
90485732ac8SCy Schubert 	*pos++ = OWE_OUI_TYPE;
90585732ac8SCy Schubert 	os_memcpy(pos, hapd->conf->owe_transition_bssid, ETH_ALEN);
90685732ac8SCy Schubert 	pos += ETH_ALEN;
90785732ac8SCy Schubert 	*pos++ = hapd->conf->owe_transition_ssid_len;
90885732ac8SCy Schubert 	os_memcpy(pos, hapd->conf->owe_transition_ssid,
90985732ac8SCy Schubert 		  hapd->conf->owe_transition_ssid_len);
91085732ac8SCy Schubert 	pos += hapd->conf->owe_transition_ssid_len;
91185732ac8SCy Schubert 
91285732ac8SCy Schubert 	return pos;
91385732ac8SCy Schubert #else /* CONFIG_OWE */
91485732ac8SCy Schubert 	return eid;
91585732ac8SCy Schubert #endif /* CONFIG_OWE */
91685732ac8SCy Schubert }
91785732ac8SCy Schubert 
91885732ac8SCy Schubert 
hostapd_eid_dpp_cc_len(struct hostapd_data * hapd)919c1d255d3SCy Schubert size_t hostapd_eid_dpp_cc_len(struct hostapd_data *hapd)
920c1d255d3SCy Schubert {
921c1d255d3SCy Schubert #ifdef CONFIG_DPP2
922*a90b9d01SCy Schubert 	if (hostapd_dpp_configurator_connectivity(hapd))
923c1d255d3SCy Schubert 		return 6;
924c1d255d3SCy Schubert #endif /* CONFIG_DPP2 */
925c1d255d3SCy Schubert 	return 0;
926c1d255d3SCy Schubert }
927c1d255d3SCy Schubert 
928c1d255d3SCy Schubert 
hostapd_eid_dpp_cc(struct hostapd_data * hapd,u8 * eid,size_t len)929c1d255d3SCy Schubert u8 * hostapd_eid_dpp_cc(struct hostapd_data *hapd, u8 *eid, size_t len)
930c1d255d3SCy Schubert {
931c1d255d3SCy Schubert 	u8 *pos = eid;
932c1d255d3SCy Schubert 
933c1d255d3SCy Schubert #ifdef CONFIG_DPP2
934*a90b9d01SCy Schubert 	if (!hostapd_dpp_configurator_connectivity(hapd) || len < 6)
935c1d255d3SCy Schubert 		return pos;
936c1d255d3SCy Schubert 
937c1d255d3SCy Schubert 	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
938c1d255d3SCy Schubert 	*pos++ = 4;
939c1d255d3SCy Schubert 	WPA_PUT_BE24(pos, OUI_WFA);
940c1d255d3SCy Schubert 	pos += 3;
941c1d255d3SCy Schubert 	*pos++ = DPP_CC_OUI_TYPE;
942c1d255d3SCy Schubert #endif /* CONFIG_DPP2 */
943c1d255d3SCy Schubert 
944c1d255d3SCy Schubert 	return pos;
945c1d255d3SCy Schubert }
946c1d255d3SCy Schubert 
947c1d255d3SCy Schubert 
ap_copy_sta_supp_op_classes(struct sta_info * sta,const u8 * supp_op_classes,size_t supp_op_classes_len)948780fb4a2SCy Schubert void ap_copy_sta_supp_op_classes(struct sta_info *sta,
949780fb4a2SCy Schubert 				 const u8 *supp_op_classes,
950780fb4a2SCy Schubert 				 size_t supp_op_classes_len)
951780fb4a2SCy Schubert {
952780fb4a2SCy Schubert 	if (!supp_op_classes)
953780fb4a2SCy Schubert 		return;
954780fb4a2SCy Schubert 	os_free(sta->supp_op_classes);
955780fb4a2SCy Schubert 	sta->supp_op_classes = os_malloc(1 + supp_op_classes_len);
956780fb4a2SCy Schubert 	if (!sta->supp_op_classes)
957780fb4a2SCy Schubert 		return;
958780fb4a2SCy Schubert 
959780fb4a2SCy Schubert 	sta->supp_op_classes[0] = supp_op_classes_len;
960780fb4a2SCy Schubert 	os_memcpy(sta->supp_op_classes + 1, supp_op_classes,
961780fb4a2SCy Schubert 		  supp_op_classes_len);
962780fb4a2SCy Schubert }
96385732ac8SCy Schubert 
96485732ac8SCy Schubert 
hostapd_eid_fils_indic(struct hostapd_data * hapd,u8 * eid,int hessid)96585732ac8SCy Schubert u8 * hostapd_eid_fils_indic(struct hostapd_data *hapd, u8 *eid, int hessid)
96685732ac8SCy Schubert {
96785732ac8SCy Schubert 	u8 *pos = eid;
96885732ac8SCy Schubert #ifdef CONFIG_FILS
96985732ac8SCy Schubert 	u8 *len;
97085732ac8SCy Schubert 	u16 fils_info = 0;
97185732ac8SCy Schubert 	size_t realms;
97285732ac8SCy Schubert 	struct fils_realm *realm;
97385732ac8SCy Schubert 
97485732ac8SCy Schubert 	if (!(hapd->conf->wpa & WPA_PROTO_RSN) ||
97585732ac8SCy Schubert 	    !wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt))
97685732ac8SCy Schubert 		return pos;
97785732ac8SCy Schubert 
97885732ac8SCy Schubert 	realms = dl_list_len(&hapd->conf->fils_realms);
97985732ac8SCy Schubert 	if (realms > 7)
98085732ac8SCy Schubert 		realms = 7; /* 3 bit count field limits this to max 7 */
98185732ac8SCy Schubert 
98285732ac8SCy Schubert 	*pos++ = WLAN_EID_FILS_INDICATION;
98385732ac8SCy Schubert 	len = pos++;
98485732ac8SCy Schubert 	/* TODO: B0..B2: Number of Public Key Identifiers */
98585732ac8SCy Schubert 	if (hapd->conf->erp_domain) {
98685732ac8SCy Schubert 		/* B3..B5: Number of Realm Identifiers */
98785732ac8SCy Schubert 		fils_info |= realms << 3;
98885732ac8SCy Schubert 	}
98985732ac8SCy Schubert 	/* TODO: B6: FILS IP Address Configuration */
99085732ac8SCy Schubert 	if (hapd->conf->fils_cache_id_set)
99185732ac8SCy Schubert 		fils_info |= BIT(7);
99285732ac8SCy Schubert 	if (hessid && !is_zero_ether_addr(hapd->conf->hessid))
99385732ac8SCy Schubert 		fils_info |= BIT(8); /* HESSID Included */
99485732ac8SCy Schubert 	/* FILS Shared Key Authentication without PFS Supported */
99585732ac8SCy Schubert 	fils_info |= BIT(9);
99685732ac8SCy Schubert 	if (hapd->conf->fils_dh_group) {
99785732ac8SCy Schubert 		/* FILS Shared Key Authentication with PFS Supported */
99885732ac8SCy Schubert 		fils_info |= BIT(10);
99985732ac8SCy Schubert 	}
100085732ac8SCy Schubert 	/* TODO: B11: FILS Public Key Authentication Supported */
100185732ac8SCy Schubert 	/* B12..B15: Reserved */
100285732ac8SCy Schubert 	WPA_PUT_LE16(pos, fils_info);
100385732ac8SCy Schubert 	pos += 2;
100485732ac8SCy Schubert 	if (hapd->conf->fils_cache_id_set) {
100585732ac8SCy Schubert 		os_memcpy(pos, hapd->conf->fils_cache_id, FILS_CACHE_ID_LEN);
100685732ac8SCy Schubert 		pos += FILS_CACHE_ID_LEN;
100785732ac8SCy Schubert 	}
100885732ac8SCy Schubert 	if (hessid && !is_zero_ether_addr(hapd->conf->hessid)) {
100985732ac8SCy Schubert 		os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
101085732ac8SCy Schubert 		pos += ETH_ALEN;
101185732ac8SCy Schubert 	}
101285732ac8SCy Schubert 
101385732ac8SCy Schubert 	dl_list_for_each(realm, &hapd->conf->fils_realms, struct fils_realm,
101485732ac8SCy Schubert 			 list) {
101585732ac8SCy Schubert 		if (realms == 0)
101685732ac8SCy Schubert 			break;
101785732ac8SCy Schubert 		realms--;
101885732ac8SCy Schubert 		os_memcpy(pos, realm->hash, 2);
101985732ac8SCy Schubert 		pos += 2;
102085732ac8SCy Schubert 	}
102185732ac8SCy Schubert 	*len = pos - len - 1;
102285732ac8SCy Schubert #endif /* CONFIG_FILS */
102385732ac8SCy Schubert 
102485732ac8SCy Schubert 	return pos;
102585732ac8SCy Schubert }
10264bc52338SCy Schubert 
10274bc52338SCy Schubert 
10284bc52338SCy Schubert #ifdef CONFIG_OCV
get_tx_parameters(struct sta_info * sta,int ap_max_chanwidth,int ap_seg1_idx,int * bandwidth,int * seg1_idx)10294bc52338SCy Schubert int get_tx_parameters(struct sta_info *sta, int ap_max_chanwidth,
10304bc52338SCy Schubert 		      int ap_seg1_idx, int *bandwidth, int *seg1_idx)
10314bc52338SCy Schubert {
10324bc52338SCy Schubert 	int ht_40mhz = 0;
10334bc52338SCy Schubert 	int vht_80p80 = 0;
10344bc52338SCy Schubert 	int requested_bw;
10354bc52338SCy Schubert 
10364bc52338SCy Schubert 	if (sta->ht_capabilities)
10374bc52338SCy Schubert 		ht_40mhz = !!(sta->ht_capabilities->ht_capabilities_info &
10384bc52338SCy Schubert 			      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET);
10394bc52338SCy Schubert 
10404bc52338SCy Schubert 	if (sta->vht_operation) {
10414bc52338SCy Schubert 		struct ieee80211_vht_operation *oper = sta->vht_operation;
10424bc52338SCy Schubert 
10434bc52338SCy Schubert 		/*
10444bc52338SCy Schubert 		 * If a VHT Operation element was present, use it to determine
10454bc52338SCy Schubert 		 * the supported channel bandwidth.
10464bc52338SCy Schubert 		 */
1047*a90b9d01SCy Schubert 		if (oper->vht_op_info_chwidth == CHANWIDTH_USE_HT) {
10484bc52338SCy Schubert 			requested_bw = ht_40mhz ? 40 : 20;
10494bc52338SCy Schubert 		} else if (oper->vht_op_info_chan_center_freq_seg1_idx == 0) {
10504bc52338SCy Schubert 			requested_bw = 80;
10514bc52338SCy Schubert 		} else {
10524bc52338SCy Schubert 			int diff;
10534bc52338SCy Schubert 
10544bc52338SCy Schubert 			requested_bw = 160;
10554bc52338SCy Schubert 			diff = abs((int)
10564bc52338SCy Schubert 				   oper->vht_op_info_chan_center_freq_seg0_idx -
10574bc52338SCy Schubert 				   (int)
10584bc52338SCy Schubert 				   oper->vht_op_info_chan_center_freq_seg1_idx);
10594bc52338SCy Schubert 			vht_80p80 = oper->vht_op_info_chan_center_freq_seg1_idx
10604bc52338SCy Schubert 				!= 0 &&	diff > 16;
10614bc52338SCy Schubert 		}
10624bc52338SCy Schubert 	} else if (sta->vht_capabilities) {
10634bc52338SCy Schubert 		struct ieee80211_vht_capabilities *capab;
10644bc52338SCy Schubert 		int vht_chanwidth;
10654bc52338SCy Schubert 
10664bc52338SCy Schubert 		capab = sta->vht_capabilities;
10674bc52338SCy Schubert 
10684bc52338SCy Schubert 		/*
10694bc52338SCy Schubert 		 * If only the VHT Capabilities element is present (e.g., for
10704bc52338SCy Schubert 		 * normal clients), use it to determine the supported channel
10714bc52338SCy Schubert 		 * bandwidth.
10724bc52338SCy Schubert 		 */
10734bc52338SCy Schubert 		vht_chanwidth = capab->vht_capabilities_info &
10744bc52338SCy Schubert 			VHT_CAP_SUPP_CHAN_WIDTH_MASK;
10754bc52338SCy Schubert 		vht_80p80 = capab->vht_capabilities_info &
10764bc52338SCy Schubert 			VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
10774bc52338SCy Schubert 
10784bc52338SCy Schubert 		/* TODO: Also take into account Extended NSS BW Support field */
10794bc52338SCy Schubert 		requested_bw = vht_chanwidth ? 160 : 80;
10804bc52338SCy Schubert 	} else {
10814bc52338SCy Schubert 		requested_bw = ht_40mhz ? 40 : 20;
10824bc52338SCy Schubert 	}
10834bc52338SCy Schubert 
10844bc52338SCy Schubert 	*bandwidth = requested_bw < ap_max_chanwidth ?
10854bc52338SCy Schubert 		requested_bw : ap_max_chanwidth;
10864bc52338SCy Schubert 
10874bc52338SCy Schubert 	*seg1_idx = 0;
10884bc52338SCy Schubert 	if (ap_seg1_idx && vht_80p80)
10894bc52338SCy Schubert 		*seg1_idx = ap_seg1_idx;
10904bc52338SCy Schubert 
10914bc52338SCy Schubert 	return 0;
10924bc52338SCy Schubert }
10934bc52338SCy Schubert #endif /* CONFIG_OCV */
1094c1d255d3SCy Schubert 
1095c1d255d3SCy Schubert 
hostapd_eid_rsnxe(struct hostapd_data * hapd,u8 * eid,size_t len)1096c1d255d3SCy Schubert u8 * hostapd_eid_rsnxe(struct hostapd_data *hapd, u8 *eid, size_t len)
1097c1d255d3SCy Schubert {
1098c1d255d3SCy Schubert 	u8 *pos = eid;
1099c1d255d3SCy Schubert 	bool sae_pk = false;
1100*a90b9d01SCy Schubert 	u32 capab = 0, tmp;
1101c1d255d3SCy Schubert 	size_t flen;
1102c1d255d3SCy Schubert 
1103c1d255d3SCy Schubert 	if (!(hapd->conf->wpa & WPA_PROTO_RSN))
1104c1d255d3SCy Schubert 		return eid;
1105c1d255d3SCy Schubert 
1106c1d255d3SCy Schubert #ifdef CONFIG_SAE_PK
1107c1d255d3SCy Schubert 	sae_pk = hostapd_sae_pk_in_use(hapd->conf);
1108c1d255d3SCy Schubert #endif /* CONFIG_SAE_PK */
1109c1d255d3SCy Schubert 
1110c1d255d3SCy Schubert 	if (wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
1111*a90b9d01SCy Schubert 	    (hapd->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
1112*a90b9d01SCy Schubert 	     hapd->conf->sae_pwe == SAE_PWE_BOTH ||
1113*a90b9d01SCy Schubert 	     hostapd_sae_pw_id_in_use(hapd->conf) || sae_pk ||
1114*a90b9d01SCy Schubert 	     wpa_key_mgmt_sae_ext_key(hapd->conf->wpa_key_mgmt)) &&
1115*a90b9d01SCy Schubert 	    hapd->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK) {
1116c1d255d3SCy Schubert 		capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E);
1117c1d255d3SCy Schubert #ifdef CONFIG_SAE_PK
1118c1d255d3SCy Schubert 		if (sae_pk)
1119c1d255d3SCy Schubert 			capab |= BIT(WLAN_RSNX_CAPAB_SAE_PK);
1120c1d255d3SCy Schubert #endif /* CONFIG_SAE_PK */
1121c1d255d3SCy Schubert 	}
1122c1d255d3SCy Schubert 
1123*a90b9d01SCy Schubert 	if (hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_AP)
1124c1d255d3SCy Schubert 		capab |= BIT(WLAN_RSNX_CAPAB_SECURE_LTF);
1125*a90b9d01SCy Schubert 	if (hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_AP)
1126c1d255d3SCy Schubert 		capab |= BIT(WLAN_RSNX_CAPAB_SECURE_RTT);
1127*a90b9d01SCy Schubert 	if (hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_AP)
1128*a90b9d01SCy Schubert 		capab |= BIT(WLAN_RSNX_CAPAB_URNM_MFPR);
1129*a90b9d01SCy Schubert 	if (hapd->conf->ssid_protection)
1130*a90b9d01SCy Schubert 		capab |= BIT(WLAN_RSNX_CAPAB_SSID_PROTECTION);
1131c1d255d3SCy Schubert 
1132*a90b9d01SCy Schubert 	if (!capab)
1133*a90b9d01SCy Schubert 		return eid; /* no supported extended RSN capabilities */
1134*a90b9d01SCy Schubert 	tmp = capab;
1135*a90b9d01SCy Schubert 	flen = 0;
1136*a90b9d01SCy Schubert 	while (tmp) {
1137*a90b9d01SCy Schubert 		flen++;
1138*a90b9d01SCy Schubert 		tmp >>= 8;
1139*a90b9d01SCy Schubert 	}
1140*a90b9d01SCy Schubert 
1141*a90b9d01SCy Schubert 	if (len < 2 + flen)
1142c1d255d3SCy Schubert 		return eid; /* no supported extended RSN capabilities */
1143c1d255d3SCy Schubert 	capab |= flen - 1; /* bit 0-3 = Field length (n - 1) */
1144c1d255d3SCy Schubert 
1145c1d255d3SCy Schubert 	*pos++ = WLAN_EID_RSNX;
1146c1d255d3SCy Schubert 	*pos++ = flen;
1147*a90b9d01SCy Schubert 	while (capab) {
1148*a90b9d01SCy Schubert 		*pos++ = capab & 0xff;
1149c1d255d3SCy Schubert 		capab >>= 8;
1150*a90b9d01SCy Schubert 	}
1151c1d255d3SCy Schubert 
1152c1d255d3SCy Schubert 	return pos;
1153c1d255d3SCy Schubert }
1154*a90b9d01SCy Schubert 
1155*a90b9d01SCy Schubert 
check_ext_capab(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ext_capab_ie,size_t ext_capab_ie_len)1156*a90b9d01SCy Schubert u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
1157*a90b9d01SCy Schubert 		    const u8 *ext_capab_ie, size_t ext_capab_ie_len)
1158*a90b9d01SCy Schubert {
1159*a90b9d01SCy Schubert 	/* check for QoS Map support */
1160*a90b9d01SCy Schubert 	if (ext_capab_ie_len >= 5) {
1161*a90b9d01SCy Schubert 		if (ext_capab_ie[4] & 0x01)
1162*a90b9d01SCy Schubert 			sta->qos_map_enabled = 1;
1163*a90b9d01SCy Schubert 	}
1164*a90b9d01SCy Schubert 
1165*a90b9d01SCy Schubert 	if (ext_capab_ie_len > 0) {
1166*a90b9d01SCy Schubert 		sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
1167*a90b9d01SCy Schubert 		os_free(sta->ext_capability);
1168*a90b9d01SCy Schubert 		sta->ext_capability = os_malloc(1 + ext_capab_ie_len);
1169*a90b9d01SCy Schubert 		if (sta->ext_capability) {
1170*a90b9d01SCy Schubert 			sta->ext_capability[0] = ext_capab_ie_len;
1171*a90b9d01SCy Schubert 			os_memcpy(sta->ext_capability + 1, ext_capab_ie,
1172*a90b9d01SCy Schubert 				  ext_capab_ie_len);
1173*a90b9d01SCy Schubert 		}
1174*a90b9d01SCy Schubert 	}
1175*a90b9d01SCy Schubert 
1176*a90b9d01SCy Schubert 	return WLAN_STATUS_SUCCESS;
1177*a90b9d01SCy Schubert }
1178*a90b9d01SCy Schubert 
1179*a90b9d01SCy Schubert 
hostapd_ml_get_assoc_sta(struct hostapd_data * hapd,struct sta_info * sta,struct hostapd_data ** assoc_hapd)1180*a90b9d01SCy Schubert struct sta_info * hostapd_ml_get_assoc_sta(struct hostapd_data *hapd,
1181*a90b9d01SCy Schubert 					   struct sta_info *sta,
1182*a90b9d01SCy Schubert 					   struct hostapd_data **assoc_hapd)
1183*a90b9d01SCy Schubert {
1184*a90b9d01SCy Schubert #ifdef CONFIG_IEEE80211BE
1185*a90b9d01SCy Schubert 	struct hostapd_data *other_hapd = NULL;
1186*a90b9d01SCy Schubert 	struct sta_info *tmp_sta;
1187*a90b9d01SCy Schubert 
1188*a90b9d01SCy Schubert 	if (!ap_sta_is_mld(hapd, sta))
1189*a90b9d01SCy Schubert 		return NULL;
1190*a90b9d01SCy Schubert 
1191*a90b9d01SCy Schubert 	*assoc_hapd = hapd;
1192*a90b9d01SCy Schubert 
1193*a90b9d01SCy Schubert 	/* The station is the one on which the association was performed */
1194*a90b9d01SCy Schubert 	if (sta->mld_assoc_link_id == hapd->mld_link_id)
1195*a90b9d01SCy Schubert 		return sta;
1196*a90b9d01SCy Schubert 
1197*a90b9d01SCy Schubert 	other_hapd = hostapd_mld_get_link_bss(hapd, sta->mld_assoc_link_id);
1198*a90b9d01SCy Schubert 	if (!other_hapd) {
1199*a90b9d01SCy Schubert 		wpa_printf(MSG_DEBUG, "MLD: No link match for link_id=%u",
1200*a90b9d01SCy Schubert 			   sta->mld_assoc_link_id);
1201*a90b9d01SCy Schubert 		return sta;
1202*a90b9d01SCy Schubert 	}
1203*a90b9d01SCy Schubert 
1204*a90b9d01SCy Schubert 	/*
1205*a90b9d01SCy Schubert 	 * Iterate over the stations and find the one with the matching link ID
1206*a90b9d01SCy Schubert 	 * and association ID.
1207*a90b9d01SCy Schubert 	 */
1208*a90b9d01SCy Schubert 	for (tmp_sta = other_hapd->sta_list; tmp_sta; tmp_sta = tmp_sta->next) {
1209*a90b9d01SCy Schubert 		if (tmp_sta->mld_assoc_link_id == sta->mld_assoc_link_id &&
1210*a90b9d01SCy Schubert 		    tmp_sta->aid == sta->aid) {
1211*a90b9d01SCy Schubert 			*assoc_hapd = other_hapd;
1212*a90b9d01SCy Schubert 			return tmp_sta;
1213*a90b9d01SCy Schubert 		}
1214*a90b9d01SCy Schubert 	}
1215*a90b9d01SCy Schubert #endif /* CONFIG_IEEE80211BE */
1216*a90b9d01SCy Schubert 
1217*a90b9d01SCy Schubert 	return sta;
1218*a90b9d01SCy Schubert }
1219*a90b9d01SCy Schubert 
1220*a90b9d01SCy Schubert 
hostapd_get_ht_vht_twt_responder(struct hostapd_data * hapd)1221*a90b9d01SCy Schubert bool hostapd_get_ht_vht_twt_responder(struct hostapd_data *hapd)
1222*a90b9d01SCy Schubert {
1223*a90b9d01SCy Schubert 	return hapd->iconf->ht_vht_twt_responder &&
1224*a90b9d01SCy Schubert 		((hapd->iconf->ieee80211n && !hapd->conf->disable_11n) ||
1225*a90b9d01SCy Schubert 		 (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac)) &&
1226*a90b9d01SCy Schubert 		(hapd->iface->drv_flags2 &
1227*a90b9d01SCy Schubert 		 WPA_DRIVER_FLAGS2_HT_VHT_TWT_RESPONDER);
1228*a90b9d01SCy Schubert }
1229