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