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