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