xref: /freebsd/contrib/wpa/src/ap/wpa_auth_glue.c (revision d21e322d563e0fd1f92c22205c2ced4bcd22dc23)
1 /*
2  * hostapd / WPA authenticator glue code
3  * Copyright (c) 2002-2022, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/list.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/sae.h"
16 #include "common/wpa_ctrl.h"
17 #include "common/ptksa_cache.h"
18 #include "crypto/sha1.h"
19 #include "eapol_auth/eapol_auth_sm.h"
20 #include "eapol_auth/eapol_auth_sm_i.h"
21 #include "eap_server/eap.h"
22 #include "l2_packet/l2_packet.h"
23 #include "eth_p_oui.h"
24 #include "hostapd.h"
25 #include "ieee802_1x.h"
26 #include "preauth_auth.h"
27 #include "sta_info.h"
28 #include "tkip_countermeasures.h"
29 #include "ap_drv_ops.h"
30 #include "ap_config.h"
31 #include "ieee802_11.h"
32 #include "ieee802_11_auth.h"
33 #include "pmksa_cache_auth.h"
34 #include "wpa_auth.h"
35 #include "wpa_auth_glue.h"
36 
37 
38 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
39 				  struct hostapd_config *iconf,
40 				  struct wpa_auth_config *wconf)
41 {
42 	int sae_pw_id;
43 
44 	os_memset(wconf, 0, sizeof(*wconf));
45 	wconf->wpa = conf->wpa;
46 	wconf->extended_key_id = conf->extended_key_id;
47 	wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
48 	wconf->wpa_pairwise = conf->wpa_pairwise;
49 	wconf->wpa_group = conf->wpa_group;
50 	wconf->wpa_group_rekey = conf->wpa_group_rekey;
51 	wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
52 	wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
53 	wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
54 	wconf->wpa_group_update_count = conf->wpa_group_update_count;
55 	wconf->wpa_disable_eapol_key_retries =
56 		conf->wpa_disable_eapol_key_retries;
57 	wconf->wpa_pairwise_update_count = conf->wpa_pairwise_update_count;
58 	wconf->rsn_pairwise = conf->rsn_pairwise;
59 	wconf->rsn_preauth = conf->rsn_preauth;
60 	wconf->eapol_version = conf->eapol_version;
61 #ifdef CONFIG_MACSEC
62 	if (wconf->eapol_version > 2)
63 		wconf->eapol_version = 2;
64 #endif /* CONFIG_MACSEC */
65 	wconf->wmm_enabled = conf->wmm_enabled;
66 	wconf->wmm_uapsd = conf->wmm_uapsd;
67 	wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
68 #ifdef CONFIG_OCV
69 	wconf->ocv = conf->ocv;
70 #endif /* CONFIG_OCV */
71 	wconf->okc = conf->okc;
72 	wconf->ieee80211w = conf->ieee80211w;
73 	wconf->beacon_prot = conf->beacon_prot;
74 	wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
75 	wconf->sae_require_mfp = conf->sae_require_mfp;
76 	wconf->ssid_protection = conf->ssid_protection;
77 	wconf->ssid_len = conf->ssid.ssid_len;
78 	if (wconf->ssid_len > SSID_MAX_LEN)
79 		wconf->ssid_len = SSID_MAX_LEN;
80 	os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
81 #ifdef CONFIG_IEEE80211R_AP
82 	os_memcpy(wconf->mobility_domain, conf->mobility_domain,
83 		  MOBILITY_DOMAIN_ID_LEN);
84 	if (conf->nas_identifier &&
85 	    os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
86 		wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
87 		os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
88 			  wconf->r0_key_holder_len);
89 	}
90 	os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
91 	wconf->r0_key_lifetime = conf->r0_key_lifetime;
92 	wconf->r1_max_key_lifetime = conf->r1_max_key_lifetime;
93 	wconf->reassociation_deadline = conf->reassociation_deadline;
94 	wconf->rkh_pos_timeout = conf->rkh_pos_timeout;
95 	wconf->rkh_neg_timeout = conf->rkh_neg_timeout;
96 	wconf->rkh_pull_timeout = conf->rkh_pull_timeout;
97 	wconf->rkh_pull_retries = conf->rkh_pull_retries;
98 	wconf->r0kh_list = &conf->r0kh_list;
99 	wconf->r1kh_list = &conf->r1kh_list;
100 	wconf->pmk_r1_push = conf->pmk_r1_push;
101 	wconf->ft_over_ds = conf->ft_over_ds;
102 	wconf->ft_psk_generate_local = conf->ft_psk_generate_local;
103 #endif /* CONFIG_IEEE80211R_AP */
104 #ifdef CONFIG_HS20
105 	wconf->disable_gtk = conf->disable_dgaf;
106 	if (conf->osen) {
107 		wconf->disable_gtk = 1;
108 		wconf->wpa = WPA_PROTO_OSEN;
109 		wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
110 		wconf->wpa_pairwise = 0;
111 		wconf->wpa_group = WPA_CIPHER_CCMP;
112 		wconf->rsn_pairwise = WPA_CIPHER_CCMP;
113 		wconf->rsn_preauth = 0;
114 		wconf->disable_pmksa_caching = 1;
115 		wconf->ieee80211w = 1;
116 	}
117 #endif /* CONFIG_HS20 */
118 #ifdef CONFIG_TESTING_OPTIONS
119 	wconf->corrupt_gtk_rekey_mic_probability =
120 		iconf->corrupt_gtk_rekey_mic_probability;
121 	wconf->delay_eapol_tx = iconf->delay_eapol_tx;
122 	if (conf->own_ie_override &&
123 	    wpabuf_len(conf->own_ie_override) <= MAX_OWN_IE_OVERRIDE) {
124 		wconf->own_ie_override_len = wpabuf_len(conf->own_ie_override);
125 		os_memcpy(wconf->own_ie_override,
126 			  wpabuf_head(conf->own_ie_override),
127 			  wconf->own_ie_override_len);
128 	}
129 	if (conf->rsne_override_eapol &&
130 	    wpabuf_len(conf->rsne_override_eapol) <= MAX_OWN_IE_OVERRIDE) {
131 		wconf->rsne_override_eapol_set = 1;
132 		wconf->rsne_override_eapol_len =
133 			wpabuf_len(conf->rsne_override_eapol);
134 		os_memcpy(wconf->rsne_override_eapol,
135 			  wpabuf_head(conf->rsne_override_eapol),
136 			  wconf->rsne_override_eapol_len);
137 	}
138 	if (conf->rsnxe_override_eapol &&
139 	    wpabuf_len(conf->rsnxe_override_eapol) <= MAX_OWN_IE_OVERRIDE) {
140 		wconf->rsnxe_override_eapol_set = 1;
141 		wconf->rsnxe_override_eapol_len =
142 			wpabuf_len(conf->rsnxe_override_eapol);
143 		os_memcpy(wconf->rsnxe_override_eapol,
144 			  wpabuf_head(conf->rsnxe_override_eapol),
145 			  wconf->rsnxe_override_eapol_len);
146 	}
147 	if (conf->rsne_override_ft &&
148 	    wpabuf_len(conf->rsne_override_ft) <= MAX_OWN_IE_OVERRIDE) {
149 		wconf->rsne_override_ft_set = 1;
150 		wconf->rsne_override_ft_len =
151 			wpabuf_len(conf->rsne_override_ft);
152 		os_memcpy(wconf->rsne_override_ft,
153 			  wpabuf_head(conf->rsne_override_ft),
154 			  wconf->rsne_override_ft_len);
155 	}
156 	if (conf->rsnxe_override_ft &&
157 	    wpabuf_len(conf->rsnxe_override_ft) <= MAX_OWN_IE_OVERRIDE) {
158 		wconf->rsnxe_override_ft_set = 1;
159 		wconf->rsnxe_override_ft_len =
160 			wpabuf_len(conf->rsnxe_override_ft);
161 		os_memcpy(wconf->rsnxe_override_ft,
162 			  wpabuf_head(conf->rsnxe_override_ft),
163 			  wconf->rsnxe_override_ft_len);
164 	}
165 	if (conf->gtk_rsc_override &&
166 	    wpabuf_len(conf->gtk_rsc_override) > 0 &&
167 	    wpabuf_len(conf->gtk_rsc_override) <= WPA_KEY_RSC_LEN) {
168 		os_memcpy(wconf->gtk_rsc_override,
169 			  wpabuf_head(conf->gtk_rsc_override),
170 			  wpabuf_len(conf->gtk_rsc_override));
171 		wconf->gtk_rsc_override_set = 1;
172 	}
173 	if (conf->igtk_rsc_override &&
174 	    wpabuf_len(conf->igtk_rsc_override) > 0 &&
175 	    wpabuf_len(conf->igtk_rsc_override) <= WPA_KEY_RSC_LEN) {
176 		os_memcpy(wconf->igtk_rsc_override,
177 			  wpabuf_head(conf->igtk_rsc_override),
178 			  wpabuf_len(conf->igtk_rsc_override));
179 		wconf->igtk_rsc_override_set = 1;
180 	}
181 	wconf->ft_rsnxe_used = conf->ft_rsnxe_used;
182 	wconf->oci_freq_override_eapol_m3 = conf->oci_freq_override_eapol_m3;
183 	wconf->oci_freq_override_eapol_g1 = conf->oci_freq_override_eapol_g1;
184 	wconf->oci_freq_override_ft_assoc = conf->oci_freq_override_ft_assoc;
185 	wconf->oci_freq_override_fils_assoc =
186 		conf->oci_freq_override_fils_assoc;
187 
188 	if (conf->eapol_m1_elements)
189 		wconf->eapol_m1_elements = wpabuf_dup(conf->eapol_m1_elements);
190 	if (conf->eapol_m3_elements)
191 		wconf->eapol_m3_elements = wpabuf_dup(conf->eapol_m3_elements);
192 	wconf->eapol_m3_no_encrypt = conf->eapol_m3_no_encrypt;
193 #endif /* CONFIG_TESTING_OPTIONS */
194 #ifdef CONFIG_P2P
195 	os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
196 	os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
197 	os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
198 	os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
199 #endif /* CONFIG_P2P */
200 #ifdef CONFIG_FILS
201 	wconf->fils_cache_id_set = conf->fils_cache_id_set;
202 	os_memcpy(wconf->fils_cache_id, conf->fils_cache_id,
203 		  FILS_CACHE_ID_LEN);
204 #endif /* CONFIG_FILS */
205 	wconf->sae_pwe = conf->sae_pwe;
206 	sae_pw_id = hostapd_sae_pw_id_in_use(conf);
207 	if (sae_pw_id == 2 && wconf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
208 		wconf->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
209 	else if (sae_pw_id == 1 && wconf->sae_pwe == SAE_PWE_HUNT_AND_PECK)
210 		wconf->sae_pwe = SAE_PWE_BOTH;
211 #ifdef CONFIG_SAE_PK
212 	wconf->sae_pk = hostapd_sae_pk_in_use(conf);
213 #endif /* CONFIG_SAE_PK */
214 #ifdef CONFIG_OWE
215 	wconf->owe_ptk_workaround = conf->owe_ptk_workaround;
216 #endif /* CONFIG_OWE */
217 	wconf->transition_disable = conf->transition_disable;
218 #ifdef CONFIG_DPP2
219 	wconf->dpp_pfs = conf->dpp_pfs;
220 #endif /* CONFIG_DPP2 */
221 #ifdef CONFIG_PASN
222 #ifdef CONFIG_TESTING_OPTIONS
223 	wconf->force_kdk_derivation = conf->force_kdk_derivation;
224 #endif /* CONFIG_TESTING_OPTIONS */
225 #endif /* CONFIG_PASN */
226 
227 	wconf->radius_psk = conf->wpa_psk_radius == PSK_RADIUS_DURING_4WAY_HS;
228 	wconf->no_disconnect_on_group_keyerror =
229 		conf->bss_max_idle && conf->ap_max_inactivity &&
230 		conf->no_disconnect_on_group_keyerror;
231 }
232 
233 
234 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
235 				    logger_level level, const char *txt)
236 {
237 #ifndef CONFIG_NO_HOSTAPD_LOGGER
238 	struct hostapd_data *hapd = ctx;
239 	int hlevel;
240 
241 	switch (level) {
242 	case LOGGER_WARNING:
243 		hlevel = HOSTAPD_LEVEL_WARNING;
244 		break;
245 	case LOGGER_INFO:
246 		hlevel = HOSTAPD_LEVEL_INFO;
247 		break;
248 	case LOGGER_DEBUG:
249 	default:
250 		hlevel = HOSTAPD_LEVEL_DEBUG;
251 		break;
252 	}
253 
254 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
255 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
256 }
257 
258 
259 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
260 					u16 reason)
261 {
262 	struct hostapd_data *hapd = ctx;
263 	wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
264 		   "STA " MACSTR " reason %d",
265 		   __func__, MAC2STR(addr), reason);
266 	ap_sta_disconnect(hapd, NULL, addr, reason);
267 }
268 
269 
270 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
271 {
272 	struct hostapd_data *hapd = ctx;
273 	return michael_mic_failure(hapd, addr, 0);
274 }
275 
276 
277 static void hostapd_wpa_auth_psk_failure_report(void *ctx, const u8 *addr)
278 {
279 	struct hostapd_data *hapd = ctx;
280 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
281 		MAC2STR(addr));
282 }
283 
284 
285 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
286 				       wpa_eapol_variable var, int value)
287 {
288 	struct hostapd_data *hapd = ctx;
289 	struct sta_info *sta = ap_get_sta(hapd, addr);
290 	if (sta == NULL)
291 		return;
292 	switch (var) {
293 	case WPA_EAPOL_portEnabled:
294 		ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
295 		break;
296 	case WPA_EAPOL_portValid:
297 		ieee802_1x_notify_port_valid(sta->eapol_sm, value);
298 		break;
299 	case WPA_EAPOL_authorized:
300 		ieee802_1x_set_sta_authorized(hapd, sta, value);
301 		break;
302 	case WPA_EAPOL_portControl_Auto:
303 		if (sta->eapol_sm)
304 			sta->eapol_sm->portControl = Auto;
305 		break;
306 	case WPA_EAPOL_keyRun:
307 		if (sta->eapol_sm)
308 			sta->eapol_sm->keyRun = value;
309 		break;
310 	case WPA_EAPOL_keyAvailable:
311 		if (sta->eapol_sm)
312 			sta->eapol_sm->eap_if->eapKeyAvailable = value;
313 		break;
314 	case WPA_EAPOL_keyDone:
315 		if (sta->eapol_sm)
316 			sta->eapol_sm->keyDone = value;
317 		break;
318 	case WPA_EAPOL_inc_EapolFramesTx:
319 		if (sta->eapol_sm)
320 			sta->eapol_sm->dot1xAuthEapolFramesTx++;
321 		break;
322 	}
323 }
324 
325 
326 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
327 				      wpa_eapol_variable var)
328 {
329 	struct hostapd_data *hapd = ctx;
330 	struct sta_info *sta = ap_get_sta(hapd, addr);
331 	if (sta == NULL || sta->eapol_sm == NULL)
332 		return -1;
333 	switch (var) {
334 	case WPA_EAPOL_keyRun:
335 		return sta->eapol_sm->keyRun;
336 	case WPA_EAPOL_keyAvailable:
337 		return sta->eapol_sm->eap_if->eapKeyAvailable;
338 	default:
339 		return -1;
340 	}
341 }
342 
343 
344 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
345 					   const u8 *p2p_dev_addr,
346 					   const u8 *prev_psk, size_t *psk_len,
347 					   int *vlan_id)
348 {
349 	struct hostapd_data *hapd = ctx;
350 	struct sta_info *sta = ap_get_sta(hapd, addr);
351 	const u8 *psk;
352 
353 	if (vlan_id)
354 		*vlan_id = 0;
355 	if (psk_len)
356 		*psk_len = PMK_LEN;
357 
358 #ifdef CONFIG_SAE
359 	if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
360 		if (!sta->sae || prev_psk)
361 			return NULL;
362 		if (psk_len)
363 			*psk_len = sta->sae->pmk_len;
364 		return sta->sae->pmk;
365 	}
366 	if (sta && wpa_auth_uses_sae(sta->wpa_sm)) {
367 		wpa_printf(MSG_DEBUG,
368 			   "No PSK for STA trying to use SAE with PMKSA caching");
369 		return NULL;
370 	}
371 #endif /* CONFIG_SAE */
372 
373 #ifdef CONFIG_OWE
374 	if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
375 	    sta && sta->owe_pmk) {
376 		if (psk_len)
377 			*psk_len = sta->owe_pmk_len;
378 		return sta->owe_pmk;
379 	}
380 	if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) && sta) {
381 		struct rsn_pmksa_cache_entry *sa;
382 
383 		sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
384 		if (sa && sa->akmp == WPA_KEY_MGMT_OWE) {
385 			if (psk_len)
386 				*psk_len = sa->pmk_len;
387 			return sa->pmk;
388 		}
389 	}
390 #endif /* CONFIG_OWE */
391 
392 	psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk,
393 			      vlan_id);
394 	/*
395 	 * This is about to iterate over all psks, prev_psk gives the last
396 	 * returned psk which should not be returned again.
397 	 * logic list (all hostapd_get_psk; all sta->psk)
398 	 */
399 	if (sta && sta->psk && !psk) {
400 		struct hostapd_sta_wpa_psk_short *pos;
401 
402 		if (vlan_id)
403 			*vlan_id = 0;
404 		psk = sta->psk->psk;
405 		for (pos = sta->psk; pos; pos = pos->next) {
406 			if (pos->is_passphrase) {
407 				if (pbkdf2_sha1(pos->passphrase,
408 						hapd->conf->ssid.ssid,
409 						hapd->conf->ssid.ssid_len, 4096,
410 						pos->psk, PMK_LEN) != 0) {
411 					wpa_printf(MSG_WARNING,
412 						   "Error in pbkdf2_sha1()");
413 					continue;
414 				}
415 				pos->is_passphrase = 0;
416 			}
417 			if (pos->psk == prev_psk) {
418 				psk = pos->next ? pos->next->psk : NULL;
419 				break;
420 			}
421 		}
422 	}
423 	return psk;
424 }
425 
426 
427 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
428 				    size_t *len)
429 {
430 	struct hostapd_data *hapd = ctx;
431 	const u8 *key;
432 	size_t keylen;
433 	struct sta_info *sta;
434 
435 	sta = ap_get_sta(hapd, addr);
436 	if (sta == NULL) {
437 		wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
438 		return -1;
439 	}
440 
441 	key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
442 	if (key == NULL) {
443 		wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
444 			   sta->eapol_sm);
445 		return -1;
446 	}
447 
448 	if (keylen > *len)
449 		keylen = *len;
450 	os_memcpy(msk, key, keylen);
451 	*len = keylen;
452 
453 	return 0;
454 }
455 
456 
457 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
458 				    const u8 *addr, int idx, u8 *key,
459 				    size_t key_len, enum key_flag key_flag)
460 {
461 	struct hostapd_data *hapd = ctx;
462 	const char *ifname = hapd->conf->iface;
463 
464 	if (vlan_id > 0) {
465 		ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
466 		if (!ifname) {
467 			if (!(hapd->iface->drv_flags &
468 			      WPA_DRIVER_FLAGS_VLAN_OFFLOAD))
469 				return -1;
470 			ifname = hapd->conf->iface;
471 		}
472 	}
473 
474 #ifdef CONFIG_TESTING_OPTIONS
475 	if (key_flag & KEY_FLAG_MODIFY) {
476 		/* We are updating an already installed key. Don't overwrite
477 		 * the already stored key information with zeros.
478 		 */
479 	} else if (addr && !is_broadcast_ether_addr(addr)) {
480 		struct sta_info *sta;
481 
482 		sta = ap_get_sta(hapd, addr);
483 		if (sta) {
484 			sta->last_tk_alg = alg;
485 			sta->last_tk_key_idx = idx;
486 			if (key)
487 				os_memcpy(sta->last_tk, key, key_len);
488 			sta->last_tk_len = key_len;
489 		}
490 	} else if (alg == WPA_ALG_BIP_CMAC_128 ||
491 		   alg == WPA_ALG_BIP_GMAC_128 ||
492 		   alg == WPA_ALG_BIP_GMAC_256 ||
493 		   alg == WPA_ALG_BIP_CMAC_256) {
494 		if (idx == 4 || idx == 5) {
495 			hapd->last_igtk_alg = alg;
496 			hapd->last_igtk_key_idx = idx;
497 			if (key)
498 				os_memcpy(hapd->last_igtk, key, key_len);
499 			hapd->last_igtk_len = key_len;
500 		} else if (idx == 6 || idx == 7) {
501 			hapd->last_bigtk_alg = alg;
502 			hapd->last_bigtk_key_idx = idx;
503 			if (key)
504 				os_memcpy(hapd->last_bigtk, key, key_len);
505 			hapd->last_bigtk_len = key_len;
506 		}
507 	} else {
508 		hapd->last_gtk_alg = alg;
509 		hapd->last_gtk_key_idx = idx;
510 		if (key)
511 			os_memcpy(hapd->last_gtk, key, key_len);
512 		hapd->last_gtk_len = key_len;
513 	}
514 #endif /* CONFIG_TESTING_OPTIONS */
515 	return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, vlan_id, 1,
516 				   NULL, 0, key, key_len, key_flag);
517 }
518 
519 
520 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
521 				       u8 *seq)
522 {
523 	struct hostapd_data *hapd = ctx;
524 	int link_id = -1;
525 
526 #ifdef CONFIG_IEEE80211BE
527 	if (hapd->conf->mld_ap && idx)
528 		link_id = hapd->mld_link_id;
529 #endif /* CONFIG_IEEE80211BE */
530 	return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, link_id,
531 				  seq);
532 }
533 
534 
535 int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
536 				const u8 *data, size_t data_len,
537 				int encrypt)
538 {
539 	struct hostapd_data *hapd = ctx;
540 	struct sta_info *sta;
541 	u32 flags = 0;
542 	int link_id = -1;
543 
544 #ifdef CONFIG_IEEE80211BE
545 	link_id = hapd->conf->mld_ap ? hapd->mld_link_id : -1;
546 #endif /* CONFIG_IEEE80211BE */
547 
548 #ifdef CONFIG_TESTING_OPTIONS
549 	if (hapd->ext_eapol_frame_io) {
550 		size_t hex_len = 2 * data_len + 1;
551 		char *hex = os_malloc(hex_len);
552 
553 		if (hex == NULL)
554 			return -1;
555 		wpa_snprintf_hex(hex, hex_len, data, data_len);
556 		wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
557 			MAC2STR(addr), hex);
558 		os_free(hex);
559 		return 0;
560 	}
561 #endif /* CONFIG_TESTING_OPTIONS */
562 
563 	sta = ap_get_sta(hapd, addr);
564 	if (sta) {
565 		flags = hostapd_sta_flags_to_drv(sta->flags);
566 #ifdef CONFIG_IEEE80211BE
567 		if (ap_sta_is_mld(hapd, sta) &&
568 		    (sta->flags & WLAN_STA_AUTHORIZED))
569 			link_id = -1;
570 #endif /* CONFIG_IEEE80211BE */
571 	}
572 
573 	return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
574 					   encrypt, flags, link_id);
575 }
576 
577 
578 static int hostapd_wpa_auth_get_sta_count(void *ctx)
579 {
580 	struct hostapd_data *hapd = ctx;
581 
582 	return hapd->num_sta;
583 }
584 
585 
586 static int hostapd_wpa_auth_for_each_sta(
587 	void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
588 	void *cb_ctx)
589 {
590 	struct hostapd_data *hapd = ctx;
591 	struct sta_info *sta;
592 
593 	for (sta = hapd->sta_list; sta; sta = sta->next) {
594 		if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
595 			return 1;
596 	}
597 	return 0;
598 }
599 
600 
601 struct wpa_auth_iface_iter_data {
602 	int (*cb)(struct wpa_authenticator *sm, void *ctx);
603 	void *cb_ctx;
604 };
605 
606 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
607 {
608 	struct wpa_auth_iface_iter_data *data = ctx;
609 	size_t i;
610 	for (i = 0; i < iface->num_bss; i++) {
611 		if (iface->bss[i]->wpa_auth &&
612 		    data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
613 			return 1;
614 	}
615 	return 0;
616 }
617 
618 
619 static int hostapd_wpa_auth_for_each_auth(
620 	void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
621 	void *cb_ctx)
622 {
623 	struct hostapd_data *hapd = ctx;
624 	struct wpa_auth_iface_iter_data data;
625 	if (hapd->iface->interfaces == NULL ||
626 	    hapd->iface->interfaces->for_each_interface == NULL)
627 		return -1;
628 	data.cb = cb;
629 	data.cb_ctx = cb_ctx;
630 	return hapd->iface->interfaces->for_each_interface(
631 		hapd->iface->interfaces, wpa_auth_iface_iter, &data);
632 }
633 
634 
635 #ifdef CONFIG_IEEE80211R_AP
636 
637 struct wpa_ft_rrb_rx_later_data {
638 	struct dl_list list;
639 	u8 addr[ETH_ALEN];
640 	size_t data_len;
641 	/* followed by data_len octets of data */
642 };
643 
644 static void hostapd_wpa_ft_rrb_rx_later(void *eloop_ctx, void *timeout_ctx)
645 {
646 	struct hostapd_data *hapd = eloop_ctx;
647 	struct wpa_ft_rrb_rx_later_data *data, *n;
648 
649 	dl_list_for_each_safe(data, n, &hapd->l2_queue,
650 			      struct wpa_ft_rrb_rx_later_data, list) {
651 		if (hapd->wpa_auth) {
652 			wpa_ft_rrb_rx(hapd->wpa_auth, data->addr,
653 				      (const u8 *) (data + 1),
654 				      data->data_len);
655 		}
656 		dl_list_del(&data->list);
657 		os_free(data);
658 	}
659 }
660 
661 
662 struct wpa_auth_ft_iface_iter_data {
663 	struct hostapd_data *src_hapd;
664 	const u8 *dst;
665 	const u8 *data;
666 	size_t data_len;
667 };
668 
669 
670 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
671 {
672 	struct wpa_auth_ft_iface_iter_data *idata = ctx;
673 	struct wpa_ft_rrb_rx_later_data *data;
674 	struct hostapd_data *hapd;
675 	size_t j;
676 
677 	for (j = 0; j < iface->num_bss; j++) {
678 		hapd = iface->bss[j];
679 		if (hapd == idata->src_hapd ||
680 		    !hapd->wpa_auth ||
681 		    !ether_addr_equal(hapd->own_addr, idata->dst))
682 			continue;
683 
684 		wpa_printf(MSG_DEBUG,
685 			   "FT: Send RRB data directly to locally managed BSS "
686 			   MACSTR "@%s -> " MACSTR "@%s",
687 			   MAC2STR(idata->src_hapd->own_addr),
688 			   idata->src_hapd->conf->iface,
689 			   MAC2STR(hapd->own_addr), hapd->conf->iface);
690 
691 		/* Defer wpa_ft_rrb_rx() until next eloop step as this is
692 		 * when it would be triggered when reading from a socket.
693 		 * This avoids
694 		 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
695 		 * that is calling hapd0:recv handler from within
696 		 * hapd0:send directly.
697 		 */
698 		data = os_zalloc(sizeof(*data) + idata->data_len);
699 		if (!data)
700 			return 1;
701 
702 		os_memcpy(data->addr, idata->src_hapd->own_addr, ETH_ALEN);
703 		os_memcpy(data + 1, idata->data, idata->data_len);
704 		data->data_len = idata->data_len;
705 
706 		dl_list_add(&hapd->l2_queue, &data->list);
707 
708 		if (!eloop_is_timeout_registered(hostapd_wpa_ft_rrb_rx_later,
709 						 hapd, NULL))
710 			eloop_register_timeout(0, 0,
711 					       hostapd_wpa_ft_rrb_rx_later,
712 					       hapd, NULL);
713 
714 		return 1;
715 	}
716 
717 	return 0;
718 }
719 
720 #endif /* CONFIG_IEEE80211R_AP */
721 
722 
723 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
724 				       const u8 *data, size_t data_len)
725 {
726 	struct hostapd_data *hapd = ctx;
727 	struct l2_ethhdr *buf;
728 	int ret;
729 
730 #ifdef CONFIG_TESTING_OPTIONS
731 	if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
732 		size_t hex_len = 2 * data_len + 1;
733 		char *hex = os_malloc(hex_len);
734 
735 		if (hex == NULL)
736 			return -1;
737 		wpa_snprintf_hex(hex, hex_len, data, data_len);
738 		wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
739 			MAC2STR(dst), hex);
740 		os_free(hex);
741 		return 0;
742 	}
743 #endif /* CONFIG_TESTING_OPTIONS */
744 
745 #ifdef CONFIG_IEEE80211R_AP
746 	if (proto == ETH_P_RRB && hapd->iface->interfaces &&
747 	    hapd->iface->interfaces->for_each_interface) {
748 		int res;
749 		struct wpa_auth_ft_iface_iter_data idata;
750 		idata.src_hapd = hapd;
751 		idata.dst = dst;
752 		idata.data = data;
753 		idata.data_len = data_len;
754 		res = hapd->iface->interfaces->for_each_interface(
755 			hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
756 			&idata);
757 		if (res == 1)
758 			return data_len;
759 	}
760 #endif /* CONFIG_IEEE80211R_AP */
761 
762 	if (hapd->l2 == NULL)
763 		return -1;
764 
765 	buf = os_malloc(sizeof(*buf) + data_len);
766 	if (buf == NULL)
767 		return -1;
768 	os_memcpy(buf->h_dest, dst, ETH_ALEN);
769 	os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
770 	buf->h_proto = host_to_be16(proto);
771 	os_memcpy(buf + 1, data, data_len);
772 	ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
773 			     sizeof(*buf) + data_len);
774 	os_free(buf);
775 	return ret;
776 }
777 
778 
779 #ifdef CONFIG_ETH_P_OUI
780 static struct eth_p_oui_ctx * hostapd_wpa_get_oui(struct hostapd_data *hapd,
781 						  u8 oui_suffix)
782 {
783 	switch (oui_suffix) {
784 #ifdef CONFIG_IEEE80211R_AP
785 	case FT_PACKET_R0KH_R1KH_PULL:
786 		return hapd->oui_pull;
787 	case FT_PACKET_R0KH_R1KH_RESP:
788 		return hapd->oui_resp;
789 	case FT_PACKET_R0KH_R1KH_PUSH:
790 		return hapd->oui_push;
791 	case FT_PACKET_R0KH_R1KH_SEQ_REQ:
792 		return hapd->oui_sreq;
793 	case FT_PACKET_R0KH_R1KH_SEQ_RESP:
794 		return hapd->oui_sresp;
795 #endif /* CONFIG_IEEE80211R_AP */
796 	default:
797 		return NULL;
798 	}
799 }
800 #endif /* CONFIG_ETH_P_OUI */
801 
802 
803 #ifdef CONFIG_IEEE80211R_AP
804 
805 struct oui_deliver_later_data {
806 	struct dl_list list;
807 	u8 src_addr[ETH_ALEN];
808 	u8 dst_addr[ETH_ALEN];
809 	size_t data_len;
810 	u8 oui_suffix;
811 	/* followed by data_len octets of data */
812 };
813 
814 static void hostapd_oui_deliver_later(void *eloop_ctx, void *timeout_ctx)
815 {
816 	struct hostapd_data *hapd = eloop_ctx;
817 	struct oui_deliver_later_data *data, *n;
818 	struct eth_p_oui_ctx *oui_ctx;
819 
820 	dl_list_for_each_safe(data, n, &hapd->l2_oui_queue,
821 			      struct oui_deliver_later_data, list) {
822 		oui_ctx = hostapd_wpa_get_oui(hapd, data->oui_suffix);
823 		wpa_printf(MSG_DEBUG, "RRB(%s): %s src=" MACSTR " dst=" MACSTR
824 			   " oui_suffix=%u data_len=%u data=%p",
825 			   hapd->conf->iface, __func__,
826 			   MAC2STR(data->src_addr), MAC2STR(data->dst_addr),
827 			   data->oui_suffix, (unsigned int) data->data_len,
828 			   data);
829 		if (hapd->wpa_auth && oui_ctx) {
830 			eth_p_oui_deliver(oui_ctx, data->src_addr,
831 					  data->dst_addr,
832 					  (const u8 *) (data + 1),
833 					  data->data_len);
834 		}
835 		dl_list_del(&data->list);
836 		os_free(data);
837 	}
838 }
839 
840 
841 struct wpa_auth_oui_iface_iter_data {
842 	struct hostapd_data *src_hapd;
843 	const u8 *dst_addr;
844 	const u8 *data;
845 	size_t data_len;
846 	u8 oui_suffix;
847 };
848 
849 static int hostapd_wpa_auth_oui_iter(struct hostapd_iface *iface, void *ctx)
850 {
851 	struct wpa_auth_oui_iface_iter_data *idata = ctx;
852 	struct oui_deliver_later_data *data;
853 	struct hostapd_data *hapd, *src_hapd = idata->src_hapd;
854 	size_t j;
855 
856 	for (j = 0; j < iface->num_bss; j++) {
857 		hapd = iface->bss[j];
858 		if (hapd == src_hapd)
859 			continue; /* don't deliver back to same interface */
860 		if (!wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) ||
861 		    hapd->conf->ssid.ssid_len !=
862 		    src_hapd->conf->ssid.ssid_len ||
863 		    os_memcmp(hapd->conf->ssid.ssid,
864 			      src_hapd->conf->ssid.ssid,
865 			      hapd->conf->ssid.ssid_len) != 0 ||
866 		    os_memcmp(hapd->conf->mobility_domain,
867 			      src_hapd->conf->mobility_domain,
868 			      MOBILITY_DOMAIN_ID_LEN) != 0)
869 			continue; /* no matching FT SSID/mobility domain */
870 		if (!is_multicast_ether_addr(idata->dst_addr) &&
871 		    !ether_addr_equal(hapd->own_addr, idata->dst_addr))
872 			continue; /* destination address does not match */
873 
874 		/* defer eth_p_oui_deliver until next eloop step as this is
875 		 * when it would be triggerd from reading from sock
876 		 * This avoids
877 		 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
878 		 * that is calling hapd0:recv handler from within
879 		 * hapd0:send directly.
880 		 */
881 		data = os_zalloc(sizeof(*data) + idata->data_len);
882 		if (!data)
883 			return 1;
884 		wpa_printf(MSG_DEBUG,
885 			   "RRB(%s): local delivery to %s dst=" MACSTR
886 			   " oui_suffix=%u data_len=%u data=%p",
887 			   src_hapd->conf->iface, hapd->conf->iface,
888 			   MAC2STR(idata->dst_addr), idata->oui_suffix,
889 			   (unsigned int) idata->data_len, data);
890 
891 		os_memcpy(data->src_addr, src_hapd->own_addr, ETH_ALEN);
892 		os_memcpy(data->dst_addr, idata->dst_addr, ETH_ALEN);
893 		os_memcpy(data + 1, idata->data, idata->data_len);
894 		data->data_len = idata->data_len;
895 		data->oui_suffix = idata->oui_suffix;
896 
897 		dl_list_add_tail(&hapd->l2_oui_queue, &data->list);
898 
899 		if (!eloop_is_timeout_registered(hostapd_oui_deliver_later,
900 						 hapd, NULL))
901 			eloop_register_timeout(0, 0,
902 					       hostapd_oui_deliver_later,
903 					       hapd, NULL);
904 
905 		/* If dst_addr is a multicast address, do not return any
906 		 * non-zero value here. Otherwise, the iteration of
907 		 * for_each_interface() will be stopped. */
908 		if (!is_multicast_ether_addr(idata->dst_addr))
909 			return 1;
910 	}
911 
912 	return 0;
913 }
914 
915 #endif /* CONFIG_IEEE80211R_AP */
916 
917 
918 static int hostapd_wpa_auth_send_oui(void *ctx, const u8 *dst, u8 oui_suffix,
919 				     const u8 *data, size_t data_len)
920 {
921 #ifdef CONFIG_ETH_P_OUI
922 	struct hostapd_data *hapd = ctx;
923 	struct eth_p_oui_ctx *oui_ctx;
924 
925 	wpa_printf(MSG_DEBUG, "RRB(%s): send to dst=" MACSTR
926 		   " oui_suffix=%u data_len=%u",
927 		   hapd->conf->iface, MAC2STR(dst), oui_suffix,
928 		   (unsigned int) data_len);
929 #ifdef CONFIG_IEEE80211R_AP
930 	if (hapd->iface->interfaces &&
931 	    hapd->iface->interfaces->for_each_interface) {
932 		struct wpa_auth_oui_iface_iter_data idata;
933 		int res;
934 
935 		idata.src_hapd = hapd;
936 		idata.dst_addr = dst;
937 		idata.data = data;
938 		idata.data_len = data_len;
939 		idata.oui_suffix = oui_suffix;
940 		res = hapd->iface->interfaces->for_each_interface(
941 			hapd->iface->interfaces, hostapd_wpa_auth_oui_iter,
942 			&idata);
943 		if (res == 1)
944 			return data_len;
945 	}
946 #endif /* CONFIG_IEEE80211R_AP */
947 
948 	oui_ctx = hostapd_wpa_get_oui(hapd, oui_suffix);
949 	if (!oui_ctx)
950 		return -1;
951 
952 	return eth_p_oui_send(oui_ctx, hapd->own_addr, dst, data, data_len);
953 #else /* CONFIG_ETH_P_OUI */
954 	return -1;
955 #endif /* CONFIG_ETH_P_OUI */
956 }
957 
958 
959 static int hostapd_channel_info(void *ctx, struct wpa_channel_info *ci)
960 {
961 	struct hostapd_data *hapd = ctx;
962 
963 	return hostapd_drv_channel_info(hapd, ci);
964 }
965 
966 
967 #ifdef CONFIG_PASN
968 
969 static void hostapd_store_ptksa(void *ctx, const u8 *addr,int cipher,
970 				u32 life_time, const struct wpa_ptk *ptk)
971 {
972 	struct hostapd_data *hapd = ctx;
973 
974 	ptksa_cache_add(hapd->ptksa, hapd->own_addr, addr, cipher, life_time,
975 			ptk, NULL, NULL, 0);
976 }
977 
978 
979 static void hostapd_clear_ptksa(void *ctx, const u8 *addr, int cipher)
980 {
981 	struct hostapd_data *hapd = ctx;
982 
983 	ptksa_cache_flush(hapd->ptksa, addr, cipher);
984 }
985 
986 #endif /* CONFIG_PASN */
987 
988 
989 static int hostapd_wpa_auth_update_vlan(void *ctx, const u8 *addr, int vlan_id)
990 {
991 #ifndef CONFIG_NO_VLAN
992 	struct hostapd_data *hapd = ctx;
993 	struct sta_info *sta;
994 
995 	sta = ap_get_sta(hapd, addr);
996 	if (!sta)
997 		return -1;
998 
999 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
1000 		struct vlan_description vlan_desc;
1001 
1002 		os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1003 		vlan_desc.notempty = 1;
1004 		vlan_desc.untagged = vlan_id;
1005 		if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
1006 			wpa_printf(MSG_INFO,
1007 				   "Invalid VLAN ID %d in wpa_psk_file",
1008 				   vlan_id);
1009 			return -1;
1010 		}
1011 
1012 		if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0) {
1013 			wpa_printf(MSG_INFO,
1014 				   "Failed to assign VLAN ID %d from wpa_psk_file to "
1015 				   MACSTR, vlan_id, MAC2STR(sta->addr));
1016 			return -1;
1017 		}
1018 	} else {
1019 		sta->vlan_id = vlan_id;
1020 	}
1021 
1022 	wpa_printf(MSG_INFO,
1023 		   "Assigned VLAN ID %d from wpa_psk_file to " MACSTR,
1024 		   vlan_id, MAC2STR(sta->addr));
1025 	if ((sta->flags & WLAN_STA_ASSOC) &&
1026 	    ap_sta_bind_vlan(hapd, sta) < 0)
1027 		return -1;
1028 #endif /* CONFIG_NO_VLAN */
1029 
1030 	return 0;
1031 }
1032 
1033 
1034 #ifdef CONFIG_OCV
1035 static int hostapd_get_sta_tx_params(void *ctx, const u8 *addr,
1036 				     int ap_max_chanwidth, int ap_seg1_idx,
1037 				     int *bandwidth, int *seg1_idx)
1038 {
1039 	struct hostapd_data *hapd = ctx;
1040 	struct sta_info *sta;
1041 
1042 	sta = ap_get_sta(hapd, addr);
1043 	if (!sta) {
1044 		hostapd_wpa_auth_logger(hapd, addr, LOGGER_INFO,
1045 					"Failed to get STA info to validate received OCI");
1046 		return -1;
1047 	}
1048 
1049 	return get_tx_parameters(sta, ap_max_chanwidth, ap_seg1_idx, bandwidth,
1050 				 seg1_idx);
1051 }
1052 #endif /* CONFIG_OCV */
1053 
1054 
1055 #ifdef CONFIG_IEEE80211R_AP
1056 
1057 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
1058 					   const u8 *data, size_t data_len)
1059 {
1060 	struct hostapd_data *hapd = ctx;
1061 	int res;
1062 	struct ieee80211_mgmt *m;
1063 	size_t mlen;
1064 	struct sta_info *sta;
1065 
1066 	sta = ap_get_sta(hapd, dst);
1067 	if (sta == NULL || sta->wpa_sm == NULL)
1068 		return -1;
1069 
1070 	m = os_zalloc(sizeof(*m) + data_len);
1071 	if (m == NULL)
1072 		return -1;
1073 	mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
1074 	m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1075 					WLAN_FC_STYPE_ACTION);
1076 	os_memcpy(m->da, dst, ETH_ALEN);
1077 	os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
1078 	os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
1079 	os_memcpy(&m->u, data, data_len);
1080 
1081 	res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0, NULL, 0, 0);
1082 	os_free(m);
1083 	return res;
1084 }
1085 
1086 
1087 static struct wpa_state_machine *
1088 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
1089 {
1090 	struct hostapd_data *hapd = ctx;
1091 	struct sta_info *sta;
1092 	int ret;
1093 
1094 	wpa_printf(MSG_DEBUG, "Add station entry for " MACSTR
1095 		   " based on WPA authenticator callback",
1096 		   MAC2STR(sta_addr));
1097 	ret = hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT);
1098 
1099 	/*
1100 	 * The expected return values from hostapd_add_sta_node() are
1101 	 * 0: successfully added STA entry
1102 	 * -EOPNOTSUPP: driver or driver wrapper does not support/need this
1103 	 *	operations
1104 	 * any other negative value: error in adding the STA entry */
1105 	if (ret < 0 && ret != -EOPNOTSUPP)
1106 		return NULL;
1107 
1108 	sta = ap_sta_add(hapd, sta_addr);
1109 	if (sta == NULL)
1110 		return NULL;
1111 	if (ret == 0)
1112 		sta->added_unassoc = 1;
1113 
1114 	sta->ft_over_ds = 1;
1115 	if (sta->wpa_sm) {
1116 		sta->auth_alg = WLAN_AUTH_FT;
1117 		return sta->wpa_sm;
1118 	}
1119 
1120 	sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
1121 	if (sta->wpa_sm == NULL) {
1122 		ap_free_sta(hapd, sta);
1123 		return NULL;
1124 	}
1125 	sta->auth_alg = WLAN_AUTH_FT;
1126 
1127 	return sta->wpa_sm;
1128 }
1129 
1130 
1131 static int hostapd_wpa_auth_add_sta_ft(void *ctx, const u8 *sta_addr)
1132 {
1133 	struct hostapd_data *hapd = ctx;
1134 	struct sta_info *sta;
1135 
1136 	sta = ap_get_sta(hapd, sta_addr);
1137 	if (!sta)
1138 		return -1;
1139 
1140 	if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
1141 	    (sta->flags & WLAN_STA_MFP) && ap_sta_is_authorized(sta) &&
1142 	    !(hapd->conf->mesh & MESH_ENABLED) && !(sta->added_unassoc)) {
1143 		/* We could not do this in handle_auth() since there was a
1144 		 * PMF-enabled association for the STA and the new
1145 		 * authentication attempt was not yet fully processed. Now that
1146 		 * we are ready to configure the TK to the driver,
1147 		 * authentication has succeeded and we can clean up the driver
1148 		 * STA entry to avoid issues with any maintained state from the
1149 		 * previous association. */
1150 		wpa_printf(MSG_DEBUG,
1151 			   "FT: Remove and re-add driver STA entry after successful FT authentication");
1152 		return ap_sta_re_add(hapd, sta);
1153 	}
1154 
1155 	return 0;
1156 }
1157 
1158 
1159 static int hostapd_wpa_auth_set_vlan(void *ctx, const u8 *sta_addr,
1160 				     struct vlan_description *vlan)
1161 {
1162 	struct hostapd_data *hapd = ctx;
1163 	struct sta_info *sta;
1164 
1165 	sta = ap_get_sta(hapd, sta_addr);
1166 	if (!sta || !sta->wpa_sm)
1167 		return -1;
1168 
1169 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
1170 		if (vlan->notempty &&
1171 		    !hostapd_vlan_valid(hapd->conf->vlan, vlan)) {
1172 			hostapd_logger(hapd, sta->addr,
1173 				       HOSTAPD_MODULE_IEEE80211,
1174 				       HOSTAPD_LEVEL_INFO,
1175 				       "Invalid VLAN %d%s received from FT",
1176 				       vlan->untagged, vlan->tagged[0] ?
1177 				       "+" : "");
1178 			return -1;
1179 		}
1180 
1181 		if (ap_sta_set_vlan(hapd, sta, vlan) < 0)
1182 			return -1;
1183 
1184 	} else {
1185 		if (vlan->notempty)
1186 			sta->vlan_id = vlan->untagged;
1187 	}
1188 	/* Configure wpa_group for GTK but ignore error due to driver not
1189 	 * knowing this STA. */
1190 	ap_sta_bind_vlan(hapd, sta);
1191 
1192 	if (sta->vlan_id)
1193 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1194 			       HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
1195 
1196 	return 0;
1197 }
1198 
1199 
1200 static int hostapd_wpa_auth_get_vlan(void *ctx, const u8 *sta_addr,
1201 				     struct vlan_description *vlan)
1202 {
1203 	struct hostapd_data *hapd = ctx;
1204 	struct sta_info *sta;
1205 
1206 	sta = ap_get_sta(hapd, sta_addr);
1207 	if (!sta)
1208 		return -1;
1209 
1210 	if (sta->vlan_desc) {
1211 		*vlan = *sta->vlan_desc;
1212 	} else if ((hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD) &&
1213 		   sta->vlan_id) {
1214 		vlan->notempty = 1;
1215 		vlan->untagged = sta->vlan_id;
1216 	} else {
1217 		os_memset(vlan, 0, sizeof(*vlan));
1218 	}
1219 
1220 	return 0;
1221 }
1222 
1223 
1224 static int
1225 hostapd_wpa_auth_set_identity(void *ctx, const u8 *sta_addr,
1226 			      const u8 *identity, size_t identity_len)
1227 {
1228 	struct hostapd_data *hapd = ctx;
1229 	struct sta_info *sta;
1230 
1231 	sta = ap_get_sta(hapd, sta_addr);
1232 	if (!sta)
1233 		return -1;
1234 
1235 	os_free(sta->identity);
1236 	sta->identity = NULL;
1237 
1238 	if (sta->eapol_sm) {
1239 		os_free(sta->eapol_sm->identity);
1240 		sta->eapol_sm->identity = NULL;
1241 		sta->eapol_sm->identity_len = 0;
1242 	}
1243 
1244 	if (!identity_len)
1245 		return 0;
1246 
1247 	/* sta->identity is NULL terminated */
1248 	sta->identity = os_zalloc(identity_len + 1);
1249 	if (!sta->identity)
1250 		return -1;
1251 	os_memcpy(sta->identity, identity, identity_len);
1252 
1253 	if (sta->eapol_sm) {
1254 		sta->eapol_sm->identity = os_zalloc(identity_len);
1255 		if (!sta->eapol_sm->identity)
1256 			return -1;
1257 		os_memcpy(sta->eapol_sm->identity, identity, identity_len);
1258 		sta->eapol_sm->identity_len = identity_len;
1259 	}
1260 
1261 	return 0;
1262 }
1263 
1264 
1265 static size_t
1266 hostapd_wpa_auth_get_identity(void *ctx, const u8 *sta_addr, const u8 **buf)
1267 {
1268 	struct hostapd_data *hapd = ctx;
1269 	struct sta_info *sta;
1270 	size_t len;
1271 	char *identity;
1272 
1273 	sta = ap_get_sta(hapd, sta_addr);
1274 	if (!sta)
1275 		return 0;
1276 
1277 	*buf = ieee802_1x_get_identity(sta->eapol_sm, &len);
1278 	if (*buf && len)
1279 		return len;
1280 
1281 	if (!sta->identity) {
1282 		*buf = NULL;
1283 		return 0;
1284 	}
1285 
1286 	identity = sta->identity;
1287 	len = os_strlen(identity);
1288 	*buf = (u8 *) identity;
1289 
1290 	return len;
1291 }
1292 
1293 
1294 static int
1295 hostapd_wpa_auth_set_radius_cui(void *ctx, const u8 *sta_addr,
1296 				const u8 *radius_cui, size_t radius_cui_len)
1297 {
1298 	struct hostapd_data *hapd = ctx;
1299 	struct sta_info *sta;
1300 
1301 	sta = ap_get_sta(hapd, sta_addr);
1302 	if (!sta)
1303 		return -1;
1304 
1305 	os_free(sta->radius_cui);
1306 	sta->radius_cui = NULL;
1307 
1308 	if (sta->eapol_sm) {
1309 		wpabuf_free(sta->eapol_sm->radius_cui);
1310 		sta->eapol_sm->radius_cui = NULL;
1311 	}
1312 
1313 	if (!radius_cui)
1314 		return 0;
1315 
1316 	/* sta->radius_cui is NULL terminated */
1317 	sta->radius_cui = os_zalloc(radius_cui_len + 1);
1318 	if (!sta->radius_cui)
1319 		return -1;
1320 	os_memcpy(sta->radius_cui, radius_cui, radius_cui_len);
1321 
1322 	if (sta->eapol_sm) {
1323 		sta->eapol_sm->radius_cui = wpabuf_alloc_copy(radius_cui,
1324 							      radius_cui_len);
1325 		if (!sta->eapol_sm->radius_cui)
1326 			return -1;
1327 	}
1328 
1329 	return 0;
1330 }
1331 
1332 
1333 static size_t
1334 hostapd_wpa_auth_get_radius_cui(void *ctx, const u8 *sta_addr, const u8 **buf)
1335 {
1336 	struct hostapd_data *hapd = ctx;
1337 	struct sta_info *sta;
1338 	struct wpabuf *b;
1339 	size_t len;
1340 	char *radius_cui;
1341 
1342 	sta = ap_get_sta(hapd, sta_addr);
1343 	if (!sta)
1344 		return 0;
1345 
1346 	b = ieee802_1x_get_radius_cui(sta->eapol_sm);
1347 	if (b) {
1348 		len = wpabuf_len(b);
1349 		*buf = wpabuf_head(b);
1350 		return len;
1351 	}
1352 
1353 	if (!sta->radius_cui) {
1354 		*buf = NULL;
1355 		return 0;
1356 	}
1357 
1358 	radius_cui = sta->radius_cui;
1359 	len = os_strlen(radius_cui);
1360 	*buf = (u8 *) radius_cui;
1361 
1362 	return len;
1363 }
1364 
1365 
1366 static void hostapd_wpa_auth_set_session_timeout(void *ctx, const u8 *sta_addr,
1367 						 int session_timeout)
1368 {
1369 	struct hostapd_data *hapd = ctx;
1370 	struct sta_info *sta;
1371 
1372 	sta = ap_get_sta(hapd, sta_addr);
1373 	if (!sta)
1374 		return;
1375 
1376 	if (session_timeout) {
1377 		os_get_reltime(&sta->session_timeout);
1378 		sta->session_timeout.sec += session_timeout;
1379 		sta->session_timeout_set = 1;
1380 		ap_sta_session_timeout(hapd, sta, session_timeout);
1381 	} else {
1382 		sta->session_timeout_set = 0;
1383 		ap_sta_no_session_timeout(hapd, sta);
1384 	}
1385 }
1386 
1387 
1388 static int hostapd_wpa_auth_get_session_timeout(void *ctx, const u8 *sta_addr)
1389 {
1390 	struct hostapd_data *hapd = ctx;
1391 	struct sta_info *sta;
1392 	struct os_reltime now, remaining;
1393 
1394 	sta = ap_get_sta(hapd, sta_addr);
1395 	if (!sta || !sta->session_timeout_set)
1396 		return 0;
1397 
1398 	os_get_reltime(&now);
1399 	if (os_reltime_before(&sta->session_timeout, &now)) {
1400 		/* already expired, return >0 as timeout was set */
1401 		return 1;
1402 	}
1403 
1404 	os_reltime_sub(&sta->session_timeout, &now, &remaining);
1405 
1406 	return (remaining.sec > 0) ? remaining.sec : 1;
1407 }
1408 
1409 
1410 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1411 				size_t len)
1412 {
1413 	struct hostapd_data *hapd = ctx;
1414 	struct l2_ethhdr *ethhdr;
1415 	if (len < sizeof(*ethhdr))
1416 		return;
1417 	ethhdr = (struct l2_ethhdr *) buf;
1418 	wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1419 		   MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
1420 	if (!is_multicast_ether_addr(ethhdr->h_dest) &&
1421 	    !ether_addr_equal(hapd->own_addr, ethhdr->h_dest))
1422 		return;
1423 	wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
1424 		      len - sizeof(*ethhdr));
1425 }
1426 
1427 
1428 static void hostapd_rrb_oui_receive(void *ctx, const u8 *src_addr,
1429 				    const u8 *dst_addr, u8 oui_suffix,
1430 				    const u8 *buf, size_t len)
1431 {
1432 	struct hostapd_data *hapd = ctx;
1433 
1434 	wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1435 		   MACSTR, MAC2STR(src_addr), MAC2STR(dst_addr));
1436 	if (!is_multicast_ether_addr(dst_addr) &&
1437 	    !ether_addr_equal(hapd->own_addr, dst_addr))
1438 		return;
1439 	wpa_ft_rrb_oui_rx(hapd->wpa_auth, src_addr, dst_addr, oui_suffix, buf,
1440 			  len);
1441 }
1442 
1443 
1444 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
1445 				      u8 *tspec_ie, size_t tspec_ielen)
1446 {
1447 	struct hostapd_data *hapd = ctx;
1448 	return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
1449 }
1450 
1451 
1452 
1453 static int hostapd_wpa_register_ft_oui(struct hostapd_data *hapd,
1454 				       const char *ft_iface)
1455 {
1456 	hapd->oui_pull = eth_p_oui_register(hapd, ft_iface,
1457 					    FT_PACKET_R0KH_R1KH_PULL,
1458 					    hostapd_rrb_oui_receive, hapd);
1459 	if (!hapd->oui_pull)
1460 		return -1;
1461 
1462 	hapd->oui_resp = eth_p_oui_register(hapd, ft_iface,
1463 					    FT_PACKET_R0KH_R1KH_RESP,
1464 					    hostapd_rrb_oui_receive, hapd);
1465 	if (!hapd->oui_resp)
1466 		return -1;
1467 
1468 	hapd->oui_push = eth_p_oui_register(hapd, ft_iface,
1469 					    FT_PACKET_R0KH_R1KH_PUSH,
1470 					    hostapd_rrb_oui_receive, hapd);
1471 	if (!hapd->oui_push)
1472 		return -1;
1473 
1474 	hapd->oui_sreq = eth_p_oui_register(hapd, ft_iface,
1475 					    FT_PACKET_R0KH_R1KH_SEQ_REQ,
1476 					    hostapd_rrb_oui_receive, hapd);
1477 	if (!hapd->oui_sreq)
1478 		return -1;
1479 
1480 	hapd->oui_sresp = eth_p_oui_register(hapd, ft_iface,
1481 					     FT_PACKET_R0KH_R1KH_SEQ_RESP,
1482 					     hostapd_rrb_oui_receive, hapd);
1483 	if (!hapd->oui_sresp)
1484 		return -1;
1485 
1486 	return 0;
1487 }
1488 
1489 
1490 static void hostapd_wpa_unregister_ft_oui(struct hostapd_data *hapd)
1491 {
1492 	eth_p_oui_unregister(hapd->oui_pull);
1493 	hapd->oui_pull = NULL;
1494 	eth_p_oui_unregister(hapd->oui_resp);
1495 	hapd->oui_resp = NULL;
1496 	eth_p_oui_unregister(hapd->oui_push);
1497 	hapd->oui_push = NULL;
1498 	eth_p_oui_unregister(hapd->oui_sreq);
1499 	hapd->oui_sreq = NULL;
1500 	eth_p_oui_unregister(hapd->oui_sresp);
1501 	hapd->oui_sresp = NULL;
1502 }
1503 #endif /* CONFIG_IEEE80211R_AP */
1504 
1505 
1506 #ifndef CONFIG_NO_RADIUS
1507 static void hostapd_request_radius_psk(void *ctx, const u8 *addr, int key_mgmt,
1508 				       const u8 *anonce,
1509 				       const u8 *eapol, size_t eapol_len)
1510 {
1511 	struct hostapd_data *hapd = ctx;
1512 
1513 	wpa_printf(MSG_DEBUG, "RADIUS PSK request for " MACSTR " key_mgmt=0x%x",
1514 		   MAC2STR(addr), key_mgmt);
1515 	wpa_hexdump(MSG_DEBUG, "ANonce", anonce, WPA_NONCE_LEN);
1516 	wpa_hexdump(MSG_DEBUG, "EAPOL", eapol, eapol_len);
1517 	hostapd_acl_req_radius_psk(hapd, addr, key_mgmt, anonce, eapol,
1518 				   eapol_len);
1519 }
1520 #endif /* CONFIG_NO_RADIUS */
1521 
1522 
1523 #ifdef CONFIG_PASN
1524 static int hostapd_set_ltf_keyseed(void *ctx, const u8 *peer_addr,
1525 				   const u8 *ltf_keyseed,
1526 				   size_t ltf_keyseed_len)
1527 {
1528 	struct hostapd_data *hapd = ctx;
1529 
1530 	return hostapd_drv_set_secure_ranging_ctx(hapd, hapd->own_addr,
1531 						  peer_addr, 0, 0, NULL,
1532 						  ltf_keyseed_len,
1533 						  ltf_keyseed, 0);
1534 }
1535 #endif /* CONFIG_PASN */
1536 
1537 
1538 #ifdef CONFIG_IEEE80211BE
1539 
1540 static int hostapd_wpa_auth_get_ml_key_info(void *ctx,
1541 					    struct wpa_auth_ml_key_info *info)
1542 {
1543 	struct hostapd_data *hapd = ctx;
1544 	unsigned int i;
1545 
1546 	wpa_printf(MSG_DEBUG, "WPA_AUTH: MLD: Get key info CB: n_mld_links=%u",
1547 		   info->n_mld_links);
1548 
1549 	if (!hapd->conf->mld_ap || !hapd->iface || !hapd->iface->interfaces)
1550 		return -1;
1551 
1552 	for (i = 0; i < info->n_mld_links; i++) {
1553 		struct hostapd_data *bss;
1554 		u8 link_id = info->links[i].link_id;
1555 		bool link_bss_found = false;
1556 
1557 		wpa_printf(MSG_DEBUG,
1558 			   "WPA_AUTH: MLD: Get link info CB: link_id=%u",
1559 			   link_id);
1560 
1561 		if (hapd->mld_link_id == link_id) {
1562 			wpa_auth_ml_get_key_info(hapd->wpa_auth,
1563 						 &info->links[i],
1564 						 info->mgmt_frame_prot,
1565 						 info->beacon_prot);
1566 			continue;
1567 		}
1568 
1569 		for_each_mld_link(bss, hapd) {
1570 			if (bss == hapd || bss->mld_link_id != link_id)
1571 				continue;
1572 
1573 			wpa_auth_ml_get_key_info(bss->wpa_auth,
1574 						 &info->links[i],
1575 						 info->mgmt_frame_prot,
1576 						 info->beacon_prot);
1577 			link_bss_found = true;
1578 			break;
1579 		}
1580 
1581 		if (!link_bss_found)
1582 			wpa_printf(MSG_DEBUG,
1583 				   "WPA_AUTH: MLD: link=%u not found", link_id);
1584 	}
1585 
1586 	return 0;
1587 }
1588 
1589 #endif /* CONFIG_IEEE80211BE */
1590 
1591 
1592 static int hostapd_wpa_auth_get_drv_flags(void *ctx,
1593 					  u64 *drv_flags, u64 *drv_flags2)
1594 {
1595 	struct hostapd_data *hapd = ctx;
1596 
1597 	if (drv_flags)
1598 		*drv_flags = hapd->iface->drv_flags;
1599 	if (drv_flags2)
1600 		*drv_flags2 = hapd->iface->drv_flags2;
1601 
1602 	return 0;
1603 }
1604 
1605 
1606 int hostapd_setup_wpa(struct hostapd_data *hapd)
1607 {
1608 	struct wpa_auth_config _conf;
1609 	static const struct wpa_auth_callbacks cb = {
1610 		.logger = hostapd_wpa_auth_logger,
1611 		.disconnect = hostapd_wpa_auth_disconnect,
1612 		.mic_failure_report = hostapd_wpa_auth_mic_failure_report,
1613 		.psk_failure_report = hostapd_wpa_auth_psk_failure_report,
1614 		.set_eapol = hostapd_wpa_auth_set_eapol,
1615 		.get_eapol = hostapd_wpa_auth_get_eapol,
1616 		.get_psk = hostapd_wpa_auth_get_psk,
1617 		.get_msk = hostapd_wpa_auth_get_msk,
1618 		.set_key = hostapd_wpa_auth_set_key,
1619 		.get_seqnum = hostapd_wpa_auth_get_seqnum,
1620 		.send_eapol = hostapd_wpa_auth_send_eapol,
1621 		.get_sta_count = hostapd_wpa_auth_get_sta_count,
1622 		.for_each_sta = hostapd_wpa_auth_for_each_sta,
1623 		.for_each_auth = hostapd_wpa_auth_for_each_auth,
1624 		.send_ether = hostapd_wpa_auth_send_ether,
1625 		.send_oui = hostapd_wpa_auth_send_oui,
1626 		.channel_info = hostapd_channel_info,
1627 		.update_vlan = hostapd_wpa_auth_update_vlan,
1628 #ifdef CONFIG_PASN
1629 		.store_ptksa = hostapd_store_ptksa,
1630 		.clear_ptksa = hostapd_clear_ptksa,
1631 #endif /* CONFIG_PASN */
1632 
1633 #ifdef CONFIG_OCV
1634 		.get_sta_tx_params = hostapd_get_sta_tx_params,
1635 #endif /* CONFIG_OCV */
1636 #ifdef CONFIG_IEEE80211R_AP
1637 		.send_ft_action = hostapd_wpa_auth_send_ft_action,
1638 		.add_sta = hostapd_wpa_auth_add_sta,
1639 		.add_sta_ft = hostapd_wpa_auth_add_sta_ft,
1640 		.add_tspec = hostapd_wpa_auth_add_tspec,
1641 		.set_vlan = hostapd_wpa_auth_set_vlan,
1642 		.get_vlan = hostapd_wpa_auth_get_vlan,
1643 		.set_identity = hostapd_wpa_auth_set_identity,
1644 		.get_identity = hostapd_wpa_auth_get_identity,
1645 		.set_radius_cui = hostapd_wpa_auth_set_radius_cui,
1646 		.get_radius_cui = hostapd_wpa_auth_get_radius_cui,
1647 		.set_session_timeout = hostapd_wpa_auth_set_session_timeout,
1648 		.get_session_timeout = hostapd_wpa_auth_get_session_timeout,
1649 #endif /* CONFIG_IEEE80211R_AP */
1650 #ifndef CONFIG_NO_RADIUS
1651 		.request_radius_psk = hostapd_request_radius_psk,
1652 #endif /* CONFIG_NO_RADIUS */
1653 #ifdef CONFIG_PASN
1654 		.set_ltf_keyseed = hostapd_set_ltf_keyseed,
1655 #endif /* CONFIG_PASN */
1656 #ifdef CONFIG_IEEE80211BE
1657 		.get_ml_key_info = hostapd_wpa_auth_get_ml_key_info,
1658 #endif /* CONFIG_IEEE80211BE */
1659 		.get_drv_flags = hostapd_wpa_auth_get_drv_flags,
1660 	};
1661 	const u8 *wpa_ie;
1662 	size_t wpa_ie_len;
1663 	struct hostapd_data *tx_bss;
1664 
1665 	hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
1666 	_conf.msg_ctx = hapd->msg_ctx;
1667 	tx_bss = hostapd_mbssid_get_tx_bss(hapd);
1668 	if (tx_bss != hapd)
1669 		_conf.tx_bss_auth = tx_bss->wpa_auth;
1670 	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
1671 		_conf.tx_status = 1;
1672 	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
1673 		_conf.ap_mlme = 1;
1674 
1675 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) &&
1676 	    (hapd->conf->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_NEVER ||
1677 	     (hapd->conf->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_LOCAL_OK &&
1678 	      !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_SAFE_PTK0_REKEYS)))) {
1679 		wpa_msg(hapd->msg_ctx, MSG_INFO,
1680 			"Disable PTK0 rekey support - replaced with disconnect");
1681 		_conf.wpa_deny_ptk0_rekey = 1;
1682 	}
1683 
1684 	if (_conf.extended_key_id &&
1685 	    (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EXTENDED_KEY_ID))
1686 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Extended Key ID supported");
1687 	else
1688 		_conf.extended_key_id = 0;
1689 
1690 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION))
1691 		_conf.beacon_prot = 0;
1692 
1693 #ifdef CONFIG_OCV
1694 	if (!(hapd->iface->drv_flags2 &
1695 	      (WPA_DRIVER_FLAGS2_AP_SME | WPA_DRIVER_FLAGS2_OCV)))
1696 		_conf.ocv = 0;
1697 #endif /* CONFIG_OCV */
1698 
1699 	_conf.secure_ltf =
1700 		!!(hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_AP);
1701 	_conf.secure_rtt =
1702 		!!(hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_AP);
1703 	_conf.prot_range_neg =
1704 		!!(hapd->iface->drv_flags2 &
1705 		   WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_AP);
1706 
1707 #ifdef CONFIG_IEEE80211BE
1708 	_conf.mld_addr = NULL;
1709 	_conf.link_id = -1;
1710 	_conf.first_link_auth = NULL;
1711 
1712 	if (hapd->conf->mld_ap) {
1713 		struct hostapd_data *lhapd;
1714 
1715 		_conf.mld_addr = hapd->mld->mld_addr;
1716 		_conf.link_id = hapd->mld_link_id;
1717 
1718 		for_each_mld_link(lhapd, hapd) {
1719 			if (lhapd == hapd)
1720 				continue;
1721 
1722 			if (lhapd->wpa_auth)
1723 				_conf.first_link_auth = lhapd->wpa_auth;
1724 		}
1725 	}
1726 #endif /* CONFIG_IEEE80211BE */
1727 
1728 	hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb, hapd);
1729 	if (hapd->wpa_auth == NULL) {
1730 		wpa_printf(MSG_ERROR, "WPA initialization failed.");
1731 		return -1;
1732 	}
1733 
1734 	if (hostapd_set_privacy(hapd, 1)) {
1735 		wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1736 			   "for interface %s", hapd->conf->iface);
1737 		return -1;
1738 	}
1739 
1740 	wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1741 	if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1742 		wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1743 			   "the kernel driver.");
1744 		return -1;
1745 	}
1746 
1747 	if (rsn_preauth_iface_init(hapd)) {
1748 		wpa_printf(MSG_ERROR, "Initialization of RSN "
1749 			   "pre-authentication failed.");
1750 		return -1;
1751 	}
1752 
1753 	if (!hapd->ptksa)
1754 		hapd->ptksa = ptksa_cache_init();
1755 	if (!hapd->ptksa) {
1756 		wpa_printf(MSG_ERROR, "Failed to allocate PTKSA cache");
1757 		return -1;
1758 	}
1759 
1760 #ifdef CONFIG_IEEE80211R_AP
1761 	if (!hostapd_drv_none(hapd) &&
1762 	    wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
1763 		const char *ft_iface;
1764 
1765 		ft_iface = hapd->conf->bridge[0] ? hapd->conf->bridge :
1766 			   hapd->conf->iface;
1767 		hapd->l2 = l2_packet_init(ft_iface, NULL, ETH_P_RRB,
1768 					  hostapd_rrb_receive, hapd, 1);
1769 		if (!hapd->l2) {
1770 			wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1771 				   "interface");
1772 			return -1;
1773 		}
1774 
1775 		if (hostapd_wpa_register_ft_oui(hapd, ft_iface)) {
1776 			wpa_printf(MSG_ERROR,
1777 				   "Failed to open ETH_P_OUI interface");
1778 			return -1;
1779 		}
1780 	}
1781 #endif /* CONFIG_IEEE80211R_AP */
1782 
1783 	return 0;
1784 
1785 }
1786 
1787 
1788 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
1789 {
1790 	struct wpa_auth_config wpa_auth_conf;
1791 	hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
1792 	wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
1793 }
1794 
1795 
1796 void hostapd_deinit_wpa(struct hostapd_data *hapd)
1797 {
1798 	ieee80211_tkip_countermeasures_deinit(hapd);
1799 	ptksa_cache_deinit(hapd->ptksa);
1800 	hapd->ptksa = NULL;
1801 
1802 	rsn_preauth_iface_deinit(hapd);
1803 	if (hapd->wpa_auth) {
1804 		wpa_deinit(hapd->wpa_auth);
1805 		hapd->wpa_auth = NULL;
1806 
1807 		if (hapd->drv_priv && hostapd_set_privacy(hapd, 0)) {
1808 			wpa_printf(MSG_DEBUG, "Could not disable "
1809 				   "PrivacyInvoked for interface %s",
1810 				   hapd->conf->iface);
1811 		}
1812 
1813 		if (hapd->drv_priv &&
1814 		    hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
1815 			wpa_printf(MSG_DEBUG, "Could not remove generic "
1816 				   "information element from interface %s",
1817 				   hapd->conf->iface);
1818 		}
1819 	}
1820 	ieee802_1x_deinit(hapd);
1821 
1822 #ifdef CONFIG_IEEE80211R_AP
1823 	eloop_cancel_timeout(hostapd_wpa_ft_rrb_rx_later, hapd, ELOOP_ALL_CTX);
1824 	hostapd_wpa_ft_rrb_rx_later(hapd, NULL); /* flush without delivering */
1825 	eloop_cancel_timeout(hostapd_oui_deliver_later, hapd, ELOOP_ALL_CTX);
1826 	hostapd_oui_deliver_later(hapd, NULL); /* flush without delivering */
1827 	l2_packet_deinit(hapd->l2);
1828 	hapd->l2 = NULL;
1829 	hostapd_wpa_unregister_ft_oui(hapd);
1830 #endif /* CONFIG_IEEE80211R_AP */
1831 
1832 #ifdef CONFIG_TESTING_OPTIONS
1833 	forced_memzero(hapd->last_gtk, WPA_GTK_MAX_LEN);
1834 	forced_memzero(hapd->last_igtk, WPA_IGTK_MAX_LEN);
1835 	forced_memzero(hapd->last_bigtk, WPA_BIGTK_MAX_LEN);
1836 #endif /* CONFIG_TESTING_OPTIONS */
1837 }
1838