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