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