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