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