• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2021, 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 #ifdef CONFIG_SQLITE
11 #include <sqlite3.h>
12 #endif /* CONFIG_SQLITE */
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "utils/crc32.h"
17 #include "common/ieee802_11_defs.h"
18 #include "common/wpa_ctrl.h"
19 #include "common/hw_features_common.h"
20 #include "radius/radius_client.h"
21 #include "radius/radius_das.h"
22 #include "eap_server/tncs.h"
23 #include "eapol_auth/eapol_auth_sm.h"
24 #include "eapol_auth/eapol_auth_sm_i.h"
25 #include "fst/fst.h"
26 #include "hostapd.h"
27 #include "authsrv.h"
28 #include "sta_info.h"
29 #include "accounting.h"
30 #include "ap_list.h"
31 #include "beacon.h"
32 #include "ieee802_1x.h"
33 #include "ieee802_11_auth.h"
34 #include "vlan_init.h"
35 #include "wpa_auth.h"
36 #include "wps_hostapd.h"
37 #include "dpp_hostapd.h"
38 #include "gas_query_ap.h"
39 #include "hw_features.h"
40 #include "wpa_auth_glue.h"
41 #include "ap_drv_ops.h"
42 #include "ap_config.h"
43 #include "p2p_hostapd.h"
44 #include "gas_serv.h"
45 #include "dfs.h"
46 #include "ieee802_11.h"
47 #include "bss_load.h"
48 #include "x_snoop.h"
49 #include "dhcp_snoop.h"
50 #include "ndisc_snoop.h"
51 #include "neighbor_db.h"
52 #include "rrm.h"
53 #include "fils_hlp.h"
54 #include "acs.h"
55 #include "hs20.h"
56 #include "airtime_policy.h"
57 #include "wpa_auth_kay.h"
58 #include "wifi_api.h"
59 
60 
61 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
62 #ifdef CONFIG_WEP
63 #ifndef LOS_CONFIG_HOSTAPD_SECURITY
64 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
65 #endif
66 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
67 #endif /* CONFIG_WEP */
68 static int setup_interface2(struct hostapd_iface *iface);
69 #ifndef EXT_CODE_CROP
70 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
71 #endif /* EXT_CODE_CROP */
72 #ifdef CONFIG_P2P
73 static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
74 						    void *timeout_ctx);
75 #endif /* CONFIG_P2P */
76 
hostapd_for_each_interface(struct hapd_interfaces * interfaces,int (* cb)(struct hostapd_iface * iface,void * ctx),void * ctx)77 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
78 			       int (*cb)(struct hostapd_iface *iface,
79 					 void *ctx), void *ctx)
80 {
81 	size_t i;
82 	int ret;
83 
84 	for (i = 0; i < interfaces->count; i++) {
85 		if (!interfaces->iface[i])
86 			continue;
87 		ret = cb(interfaces->iface[i], ctx);
88 		if (ret)
89 			return ret;
90 	}
91 
92 	return 0;
93 }
94 
95 
hostapd_reconfig_encryption(struct hostapd_data * hapd)96 void hostapd_reconfig_encryption(struct hostapd_data *hapd)
97 {
98 	if (hapd->wpa_auth)
99 		return;
100 #ifndef LOS_CONFIG_HOSTAPD_SECURITY
101 	hostapd_set_privacy(hapd, 0);
102 #ifdef CONFIG_WEP
103 	hostapd_setup_encryption(hapd->conf->iface, hapd);
104 #endif /* CONFIG_WEP */
105 #endif
106 }
107 
108 
hostapd_reload_bss(struct hostapd_data * hapd)109 static void hostapd_reload_bss(struct hostapd_data *hapd)
110 {
111 	struct hostapd_ssid *ssid;
112 
113 	if (!hapd->started)
114 		return;
115 
116 #ifndef LOS_CONFIG_HOSTAPD_QOS
117 	if (hapd->conf->wmm_enabled < 0)
118 		hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
119 			hapd->iconf->ieee80211ax;
120 #endif /* LOS_CONFIG_HOSTAPD_QOS */
121 #ifndef CONFIG_NO_RADIUS
122 	radius_client_reconfig(hapd->radius, hapd->conf->radius);
123 #endif /* CONFIG_NO_RADIUS */
124 
125 	ssid = &hapd->conf->ssid;
126 	if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
127 	    ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
128 		/*
129 		 * Force PSK to be derived again since SSID or passphrase may
130 		 * have changed.
131 		 */
132 		hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
133 	}
134 	if (hostapd_setup_wpa_psk(hapd->conf)) {
135 		wpa_error_log0(MSG_ERROR, "Failed to re-configure WPA PSK "
136 			   "after reloading configuration");
137 	}
138 #ifndef LOS_CONFIG_HOSTAPD_SECURITY
139 	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
140 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
141 	else
142 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
143 #endif
144 #ifndef EXT_CODE_CROP
145 	if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
146 #else
147 	if ((hapd->conf->wpa) && hapd->wpa_auth == NULL) {
148 #endif /* EXT_CODE_CROP */
149 		hostapd_setup_wpa(hapd);
150 		if (hapd->wpa_auth)
151 			wpa_init_keys(hapd->wpa_auth);
152 	} else if (hapd->conf->wpa) {
153 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
154 		const u8 *wpa_ie;
155 		size_t wpa_ie_len;
156 		hostapd_reconfig_wpa(hapd);
157 		wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
158 		if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
159 			wpa_error_log0(MSG_ERROR, "Failed to configure WPA IE for "
160 				   "the kernel driver.");
161 #else
162 		hostapd_reconfig_wpa(hapd);
163 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
164 	} else if (hapd->wpa_auth) {
165 		wpa_deinit(hapd->wpa_auth);
166 		hapd->wpa_auth = NULL;
167 #ifndef LOS_CONFIG_HOSTAPD_SECURITY
168 		hostapd_set_privacy(hapd, 0);
169 #ifdef CONFIG_WEP
170 		hostapd_setup_encryption(hapd->conf->iface, hapd);
171 #endif /* CONFIG_WEP */
172 		hostapd_set_generic_elem(hapd, (u8 *) "", 0);
173 #endif
174 	}
175 
176 	ieee802_11_set_beacon(hapd);
177 #ifdef CONFIG_WPS_AP
178 	hostapd_update_wps(hapd);
179 #endif /* CONFIG_WPS_AP */
180 
181 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
182 	if (hapd->conf->ssid.ssid_set &&
183 	    hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
184 			     hapd->conf->ssid.ssid_len)) {
185 		wpa_error_log0(MSG_ERROR, "Could not set SSID for kernel driver");
186 		/* try to continue */
187 	}
188 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
189 	wpa_warning_buf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface, strlen(hapd->conf->iface));
190 }
191 
192 
193 static void hostapd_clear_old(struct hostapd_iface *iface)
194 {
195 	size_t j;
196 
197 	/*
198 	 * Deauthenticate all stations since the new configuration may not
199 	 * allow them to use the BSS anymore.
200 	 */
201 	for (j = 0; j < iface->num_bss; j++) {
202 		hostapd_flush_old_stations(iface->bss[j],
203 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
204 #ifdef CONFIG_WEP
205 		hostapd_broadcast_wep_clear(iface->bss[j]);
206 #endif /* CONFIG_WEP */
207 
208 #ifndef CONFIG_NO_RADIUS
209 		/* TODO: update dynamic data based on changed configuration
210 		 * items (e.g., open/close sockets, etc.) */
211 		radius_client_flush(iface->bss[j]->radius, 0);
212 #endif /* CONFIG_NO_RADIUS */
213 	}
214 }
215 
216 
217 static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
218 				      struct hostapd_config *oldconf)
219 {
220 	size_t i;
221 
222 	if (newconf->num_bss != oldconf->num_bss)
223 		return 1;
224 
225 	for (i = 0; i < newconf->num_bss; i++) {
226 		if (os_strcmp(newconf->bss[i]->iface,
227 			      oldconf->bss[i]->iface) != 0)
228 			return 1;
229 	}
230 
231 	return 0;
232 }
233 
234 
235 int hostapd_reload_config(struct hostapd_iface *iface)
236 {
237 	struct hapd_interfaces *interfaces = iface->interfaces;
238 	struct hostapd_data *hapd = iface->bss[0];
239 	struct hostapd_config *newconf, *oldconf;
240 	size_t j;
241 
242 	if (iface->config_fname == NULL) {
243 		/* Only in-memory config in use - assume it has been updated */
244 		hostapd_clear_old(iface);
245 		for (j = 0; j < iface->num_bss; j++)
246 			hostapd_reload_bss(iface->bss[j]);
247 		return 0;
248 	}
249 
250 	if (iface->interfaces == NULL ||
251 	    iface->interfaces->config_read_cb == NULL)
252 		return -1;
253 	newconf = iface->interfaces->config_read_cb(iface->config_fname);
254 	if (newconf == NULL)
255 		return -1;
256 
257 	hostapd_clear_old(iface);
258 
259 	oldconf = hapd->iconf;
260 	if (hostapd_iface_conf_changed(newconf, oldconf)) {
261 		char *fname;
262 		int res;
263 
264 		wpa_warning_log0(MSG_DEBUG,
265 			   "Configuration changes include interface/BSS modification - force full disable+enable sequence");
266 		fname = os_strdup(iface->config_fname);
267 		if (!fname) {
268 			hostapd_config_free(newconf);
269 			return -1;
270 		}
271 		hostapd_remove_iface(interfaces, hapd->conf->iface);
272 		iface = hostapd_init(interfaces, fname);
273 		os_free(fname);
274 		hostapd_config_free(newconf);
275 		if (!iface) {
276 			wpa_error_log0(MSG_ERROR,
277 				   "Failed to initialize interface on config reload");
278 			return -1;
279 		}
280 		iface->interfaces = interfaces;
281 		interfaces->iface[interfaces->count] = iface;
282 		interfaces->count++;
283 		res = hostapd_enable_iface(iface);
284 		if (res < 0)
285 			wpa_error_log0(MSG_ERROR,
286 				   "Failed to enable interface on config reload");
287 		return res;
288 	}
289 	iface->conf = newconf;
290 
291 	for (j = 0; j < iface->num_bss; j++) {
292 		hapd = iface->bss[j];
293 		hapd->iconf = newconf;
294 		hapd->iconf->channel = oldconf->channel;
295 
296 		hapd->iconf->ieee80211n = oldconf->ieee80211n;
297 		hapd->iconf->ht_capab = oldconf->ht_capab;
298 #ifndef EXT_CODE_CROP
299 		hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
300 		hapd->iconf->acs = oldconf->acs;
301 		hapd->iconf->secondary_channel = oldconf->secondary_channel;
302 		hapd->iconf->vht_capab = oldconf->vht_capab;
303 		hostapd_set_oper_chwidth(hapd->iconf,
304 					 hostapd_get_oper_chwidth(oldconf));
305 		hostapd_set_oper_centr_freq_seg0_idx(
306 			hapd->iconf,
307 			hostapd_get_oper_centr_freq_seg0_idx(oldconf));
308 		hostapd_set_oper_centr_freq_seg1_idx(
309 			hapd->iconf,
310 			hostapd_get_oper_centr_freq_seg1_idx(oldconf));
311 #endif /* EXT_CODE_CROP */
312 		hapd->conf = newconf->bss[j];
313 		hostapd_reload_bss(hapd);
314 	}
315 
316 	hostapd_config_free(oldconf);
317 
318 
319 	return 0;
320 }
321 
322 
323 #ifdef CONFIG_WEP
324 
325 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
326 					      const char *ifname)
327 {
328 	int i;
329 
330 	if (!ifname || !hapd->drv_priv)
331 		return;
332 	for (i = 0; i < NUM_WEP_KEYS; i++) {
333 		if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
334 					0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
335 			wpa_printf(MSG_DEBUG, "Failed to clear default "
336 				   "encryption keys (ifname=%s keyidx=%d)",
337 				   ifname, i);
338 		}
339 	}
340 	if (hapd->conf->ieee80211w) {
341 		for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
342 			if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
343 						NULL, i, 0, 0, NULL,
344 						0, NULL, 0, KEY_FLAG_GROUP)) {
345 				wpa_printf(MSG_DEBUG, "Failed to clear "
346 					   "default mgmt encryption keys "
347 					   "(ifname=%s keyidx=%d)", ifname, i);
348 			}
349 		}
350 	}
351 }
352 
353 
354 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
355 {
356 	hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
357 	return 0;
358 }
359 
360 #ifndef LOS_CONFIG_HOSTAPD_SECURITY
361 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
362 {
363 	int errors = 0, idx;
364 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
365 
366 	idx = ssid->wep.idx;
367 	if (ssid->wep.default_len && ssid->wep.key[idx] &&
368 	    hostapd_drv_set_key(hapd->conf->iface,
369 				hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
370 				1, NULL, 0, ssid->wep.key[idx],
371 				ssid->wep.len[idx],
372 				KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
373 		wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
374 		errors++;
375 	}
376 
377 	return errors;
378 }
379 #endif /* LOS_CONFIG_HOSTAPD_SECURITY */
380 #endif /* CONFIG_WEP */
381 
382 
383 void hostapd_free_hapd_data(struct hostapd_data *hapd)
384 {
385 	os_free(hapd->probereq_cb);
386 	hapd->probereq_cb = NULL;
387 	hapd->num_probereq_cb = 0;
388 
389 #ifdef CONFIG_P2P
390 	wpabuf_free(hapd->p2p_beacon_ie);
391 	hapd->p2p_beacon_ie = NULL;
392 	wpabuf_free(hapd->p2p_probe_resp_ie);
393 	hapd->p2p_probe_resp_ie = NULL;
394 #endif /* CONFIG_P2P */
395 
396 	if (!hapd->started) {
397 		wpa_error_buf(MSG_ERROR, "hostapd_free_hapd_data: Interface %s wasn't started",
398 			   hapd->conf->iface, strlen(hapd->conf->iface));
399 		return;
400 	}
401 	hapd->started = 0;
402 	hapd->beacon_set_done = 0;
403 
404 	wpa_warning_buf(MSG_DEBUG, "hostapd_free_hapd_data(%s)", hapd->conf->iface, strlen(hapd->conf->iface));
405 	accounting_deinit(hapd);
406 	hostapd_deinit_wpa(hapd);
407 	vlan_deinit(hapd);
408 #ifndef EXT_CODE_CROP
409 	hostapd_acl_deinit(hapd);
410 #endif /* EXT_CODE_CROP */
411 #ifndef CONFIG_NO_RADIUS
412 	radius_client_deinit(hapd->radius);
413 	hapd->radius = NULL;
414 	radius_das_deinit(hapd->radius_das);
415 	hapd->radius_das = NULL;
416 #endif /* CONFIG_NO_RADIUS */
417 
418 #ifdef CONFIG_WPS_AP
419 	hostapd_deinit_wps(hapd);
420 #endif /* CONFIG_WPS_AP */
421 #ifndef EXT_CODE_CROP
422 	ieee802_1x_dealloc_kay_sm_hapd(hapd);
423 #ifdef CONFIG_DPP
424 	hostapd_dpp_deinit(hapd);
425 	gas_query_ap_deinit(hapd->gas);
426 	hapd->gas = NULL;
427 #endif /* CONFIG_DPP */
428 
429 	authsrv_deinit(hapd);
430 
431 	if (hapd->interface_added) {
432 		hapd->interface_added = 0;
433 		if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
434 			wpa_printf(MSG_WARNING,
435 				   "Failed to remove BSS interface %s",
436 				   hapd->conf->iface);
437 			hapd->interface_added = 1;
438 		} else {
439 			/*
440 			 * Since this was a dynamically added interface, the
441 			 * driver wrapper may have removed its internal instance
442 			 * and hapd->drv_priv is not valid anymore.
443 			 */
444 			hapd->drv_priv = NULL;
445 		}
446 	}
447 
448 	wpabuf_free(hapd->time_adv);
449 	hapd->time_adv = NULL;
450 #endif /* EXT_CODE_CROP */
451 #ifdef CONFIG_INTERWORKING
452 	gas_serv_deinit(hapd);
453 #endif /* CONFIG_INTERWORKING */
454 #ifndef EXT_CODE_CROP
455 	bss_load_update_deinit(hapd);
456 	ndisc_snoop_deinit(hapd);
457 	dhcp_snoop_deinit(hapd);
458 	x_snoop_deinit(hapd);
459 #endif /* EXT_CODE_CROP */
460 #ifdef CONFIG_SQLITE
461 	bin_clear_free(hapd->tmp_eap_user.identity,
462 		       hapd->tmp_eap_user.identity_len);
463 	bin_clear_free(hapd->tmp_eap_user.password,
464 		       hapd->tmp_eap_user.password_len);
465 	os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
466 #endif /* CONFIG_SQLITE */
467 
468 #ifdef CONFIG_MESH
469 	wpabuf_free(hapd->mesh_pending_auth);
470 	hapd->mesh_pending_auth = NULL;
471 	/* handling setup failure is already done */
472 	hapd->setup_complete_cb = NULL;
473 #endif /* CONFIG_MESH */
474 
475 #ifndef LOS_CONFIG_HOSTAPD_RRM
476 	hostapd_clean_rrm(hapd);
477 #endif /* LOS_CONFIG_HOSTAPD_RRM */
478 #ifndef EXT_CODE_CROP
479 	fils_hlp_deinit(hapd);
480 #endif /* EXT_CODE_CROP */
481 #ifdef CONFIG_OCV
482 	eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
483 #endif /* CONFIG_OCV */
484 
485 #if (defined(LOS_CONFIG_HOSTAPD_MGMT) && defined(CONFIG_SAE))
486 #ifdef CONFIG_SAE
487 	{
488 		struct hostapd_sae_commit_queue *q;
489 
490 		while ((q = dl_list_first(&hapd->sae_commit_queue,
491 					  struct hostapd_sae_commit_queue,
492 					  list))) {
493 			dl_list_del(&q->list);
494 			os_free(q);
495 		}
496 	}
497 	eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
498 #endif /* CONFIG_SAE */
499 #endif /* LOS_CONFIG_HOSTAPD_MGMT && CONFIG_SAE */
500 }
501 
502 
503 /**
504  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
505  * @hapd: Pointer to BSS data
506  *
507  * This function is used to free all per-BSS data structures and resources.
508  * Most of the modules that are initialized in hostapd_setup_bss() are
509  * deinitialized here.
510  */
511 static void hostapd_cleanup(struct hostapd_data *hapd)
512 {
513 	wpa_warning_buf(MSG_DEBUG, "hostapd_cleanup((%s))", hapd->conf->iface, strlen(hapd->conf->iface));
514 	if (hapd->iface->interfaces &&
515 	    hapd->iface->interfaces->ctrl_iface_deinit) {
516 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
517 		hapd->iface->interfaces->ctrl_iface_deinit(hapd);
518 	}
519 	hostapd_free_hapd_data(hapd);
520 }
521 
522 #ifndef EXT_CODE_CROP
523 static void sta_track_deinit(struct hostapd_iface *iface)
524 {
525 	struct hostapd_sta_info *info;
526 
527 	if (!iface->num_sta_seen)
528 		return;
529 
530 	while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
531 				     list))) {
532 		dl_list_del(&info->list);
533 		iface->num_sta_seen--;
534 		sta_track_del(info);
535 	}
536 }
537 #endif /* EXT_CODE_CROP */
538 
539 void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
540 {
541 #ifndef LOS_HOSTAPD_CONFIG_CROP
542 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
543 #ifdef CONFIG_IEEE80211N
544 #ifdef NEED_AP_MLME
545 	hostapd_stop_setup_timers(iface);
546 #endif /* NEED_AP_MLME */
547 #endif /* CONFIG_IEEE80211N */
548 	if (iface->current_mode)
549 		acs_cleanup(iface);
550 #endif /* LOS_HOSTAPD_CONFIG_CROP */
551 	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
552 	iface->hw_features = NULL;
553 #ifndef EXT_CODE_CROP
554 	iface->current_mode = NULL;
555 #endif /* EXT_CODE_CROP */
556 	os_free(iface->current_rates);
557 	iface->current_rates = NULL;
558 	os_free(iface->basic_rates);
559 	iface->basic_rates = NULL;
560 #ifndef LOS_HOSTAPD_CONFIG_CROP
561 	ap_list_deinit(iface);
562 	sta_track_deinit(iface);
563 	airtime_policy_update_deinit(iface);
564 #endif /* LOS_HOSTAPD_CONFIG_CROP */
565 }
566 
567 
568 /**
569  * hostapd_cleanup_iface - Complete per-interface cleanup
570  * @iface: Pointer to interface data
571  *
572  * This function is called after per-BSS data structures are deinitialized
573  * with hostapd_cleanup().
574  */
575 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
576 {
577 	wpa_warning_log0(MSG_DEBUG, "hostapd_cleanup_iface");
578 #ifndef EXT_CODE_CROP
579 	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
580 #endif /* EXT_CODE_CROP */
581 #ifdef CONFIG_P2P
582 	eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
583 			     NULL);
584 #endif /* CONFIG_P2P */
585 
586 	hostapd_cleanup_iface_partial(iface);
587 	hostapd_config_free(iface->conf);
588 	iface->conf = NULL;
589 
590 	os_free(iface->config_fname);
591 	os_free(iface->bss);
592 	wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
593 	os_free(iface);
594 }
595 
596 
597 #ifdef CONFIG_WEP
598 
599 static void hostapd_clear_wep(struct hostapd_data *hapd)
600 {
601 #ifndef EXT_CODE_CROP
602 	if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
603 #else
604 	if (hapd->drv_priv && hapd->conf) {
605 #endif /* EXT_CODE_CROP */
606 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
607 		hostapd_set_privacy(hapd, 0);
608 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
609 		hostapd_broadcast_wep_clear(hapd);
610 	}
611 }
612 #ifndef LOS_CONFIG_HOSTAPD_SECURITY
613 
614 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
615 {
616 	int i;
617 
618 	hostapd_broadcast_wep_set(hapd);
619 
620 	if (hapd->conf->ssid.wep.default_len) {
621 		hostapd_set_privacy(hapd, 1);
622 		return 0;
623 	}
624 
625 	/*
626 	 * When IEEE 802.1X is not enabled, the driver may need to know how to
627 	 * set authentication algorithms for static WEP.
628 	 */
629 	hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
630 
631 	for (i = 0; i < 4; i++) {
632 		if (hapd->conf->ssid.wep.key[i] &&
633 		    hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
634 					i == hapd->conf->ssid.wep.idx, NULL, 0,
635 					hapd->conf->ssid.wep.key[i],
636 					hapd->conf->ssid.wep.len[i],
637 					i == hapd->conf->ssid.wep.idx ?
638 					KEY_FLAG_GROUP_RX_TX_DEFAULT :
639 					KEY_FLAG_GROUP_RX_TX)) {
640 			wpa_printf(MSG_WARNING, "Could not set WEP "
641 				   "encryption.");
642 			return -1;
643 		}
644 		if (hapd->conf->ssid.wep.key[i] &&
645 		    i == hapd->conf->ssid.wep.idx)
646 			hostapd_set_privacy(hapd, 1);
647 	}
648 
649 	return 0;
650 }
651 
652 #endif /* CONFIG_WEP */
653 #endif
654 
655 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
656 {
657 	int ret = 0;
658 	u8 addr[ETH_ALEN];
659 
660 	(void)reason;
661 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
662 	if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
663 #else
664 	if (hapd->drv_priv == NULL)
665 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
666 		return 0;
667 
668 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
669 	if (!hapd->iface->driver_ap_teardown) {
670 		wpa_warning_log0(MSG_DEBUG,
671 			"Flushing old station entries");
672 
673 		if (hostapd_flush(hapd)) {
674 			wpa_msg(hapd->msg_ctx, MSG_WARNING,
675 				"Could not connect to kernel driver");
676 			ret = -1;
677 		}
678 	}
679 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
680 #ifndef EXT_CODE_CROP
681 	if (hapd->conf && hapd->conf->broadcast_deauth) {
682 #else
683 	if (hapd->conf) {
684 #endif /* EXT_CODE_CROP */
685 		wpa_warning_log0(MSG_DEBUG, "Deauthenticate all stations");
686 		os_memset(addr, 0xff, ETH_ALEN);
687 #ifndef EXT_CODE_CROP
688 		hostapd_drv_sta_deauth(hapd, addr, reason);
689 #endif /* EXT_CODE_CROP */
690 	}
691 	hostapd_free_stas(hapd);
692 	return ret;
693 }
694 
695 
696 void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
697 {
698 	hostapd_free_stas(hapd);
699 	hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
700 #ifdef CONFIG_WEP
701 	hostapd_clear_wep(hapd);
702 #endif /* CONFIG_WEP */
703 }
704 
705 
706 /**
707  * hostapd_validate_bssid_configuration - Validate BSSID configuration
708  * @iface: Pointer to interface data
709  * Returns: 0 on success, -1 on failure
710  *
711  * This function is used to validate that the configured BSSIDs are valid.
712  */
713 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
714 {
715 	u8 mask[ETH_ALEN] = { 0 };
716 	struct hostapd_data *hapd = iface->bss[0];
717 	unsigned int i = iface->conf->num_bss, bits = 0, j;
718 	int auto_addr = 0;
719 
720 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
721 	if (hostapd_drv_none(hapd))
722 		return 0;
723 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
724 
725 #ifndef EXT_CODE_CROP
726 	if (iface->conf->use_driver_iface_addr)
727 		return 0;
728 #endif /* EXT_CODE_CROP */
729 
730 	/* Generate BSSID mask that is large enough to cover the BSSIDs. */
731 
732 	/* Determine the bits necessary to cover the number of BSSIDs. */
733 	for (i--; i; i >>= 1)
734 		bits++;
735 
736 	/* Determine the bits necessary to any configured BSSIDs,
737 	   if they are higher than the number of BSSIDs. */
738 	for (j = 0; j < iface->conf->num_bss; j++) {
739 		if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
740 			if (j)
741 				auto_addr++;
742 			continue;
743 		}
744 
745 		for (i = 0; i < ETH_ALEN; i++) {
746 			mask[i] |=
747 				iface->conf->bss[j]->bssid[i] ^
748 				hapd->own_addr[i];
749 		}
750 	}
751 
752 	if (!auto_addr)
753 		goto skip_mask_ext;
754 
755 	for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
756 		;
757 	j = 0;
758 	if (i < ETH_ALEN) {
759 		j = (5 - i) * 8;
760 
761 		while (mask[i] != 0) {
762 			mask[i] >>= 1;
763 			j++;
764 		}
765 	}
766 
767 	if (bits < j)
768 		bits = j;
769 
770 	if (bits > 40) {
771 		wpa_error_log1(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
772 			   bits);
773 		return -1;
774 	}
775 
776 	os_memset(mask, 0xff, ETH_ALEN);
777 	j = bits / 8;
778 	for (i = 5; i > 5 - j; i--)
779 		mask[i] = 0;
780 	j = bits % 8;
781 	while (j) {
782 		j--;
783 		mask[i] <<= 1;
784 	}
785 
786 skip_mask_ext:
787 	wpa_warning_log2(MSG_DEBUG, "BSS count %lu, BSSID mask " " (%d bits)",
788 		   (unsigned long) iface->conf->num_bss, bits);
789 	wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
790 			mask[0], mask[3], mask[4], mask[5]);
791 	if (!auto_addr)
792 		return 0;
793 
794 	for (i = 0; i < ETH_ALEN; i++) {
795 		if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
796 			wpa_warning_log4(MSG_ERROR, "Invalid BSSID mask " "%02x:xx:xx:%02x:%02x:%02x",
797 				   mask[0], mask[3], mask[4], mask[5]);
798 			wpa_warning_log4(MSG_ERROR, " for start address " "%02x:xx:xx:%02x:%02x:%02x" ".",
799 				   (hapd->own_addr)[0], (hapd->own_addr)[3], (hapd->own_addr)[4], (hapd->own_addr)[5]);
800 			wpa_error_log0(MSG_ERROR, "Start address must be the "
801 				   "first address in the block (i.e., addr "
802 				   "AND mask == addr).");
803 			return -1;
804 		}
805 	}
806 
807 	return 0;
808 }
809 
810 
811 #ifndef EXT_CODE_CROP
812 static int mac_in_conf(struct hostapd_config *conf, const void *a)
813 {
814 	size_t i;
815 
816 	for (i = 0; i < conf->num_bss; i++) {
817 		if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
818 			return 1;
819 		}
820 	}
821 
822 	return 0;
823 }
824 
825 
826 #ifndef CONFIG_NO_RADIUS
827 
828 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
829 				    struct radius_das_attrs *attr)
830 {
831 	if (attr->nas_identifier &&
832 	    (!hapd->conf->nas_identifier ||
833 	     os_strlen(hapd->conf->nas_identifier) !=
834 	     attr->nas_identifier_len ||
835 	     os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
836 		       attr->nas_identifier_len) != 0)) {
837 		wpa_warning_log0(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
838 		return 1;
839 	}
840 
841 	if (attr->nas_ip_addr &&
842 	    (hapd->conf->own_ip_addr.af != AF_INET ||
843 	     os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
844 	     0)) {
845 		wpa_warning_log0(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
846 		return 1;
847 	}
848 
849 #ifdef CONFIG_IPV6
850 	if (attr->nas_ipv6_addr &&
851 	    (hapd->conf->own_ip_addr.af != AF_INET6 ||
852 	     os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
853 	     != 0)) {
854 		wpa_warning_log0(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
855 		return 1;
856 	}
857 #endif /* CONFIG_IPV6 */
858 
859 	return 0;
860 }
861 
862 
863 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
864 					      struct radius_das_attrs *attr,
865 					      int *multi)
866 {
867 	struct sta_info *selected, *sta;
868 	char buf[128];
869 	int num_attr = 0;
870 	int count;
871 
872 	*multi = 0;
873 
874 	for (sta = hapd->sta_list; sta; sta = sta->next)
875 		sta->radius_das_match = 1;
876 
877 	if (attr->sta_addr) {
878 		num_attr++;
879 		sta = ap_get_sta(hapd, attr->sta_addr);
880 		if (!sta) {
881 			wpa_warning_log0(MSG_DEBUG,
882 				   "RADIUS DAS: No Calling-Station-Id match");
883 			return NULL;
884 		}
885 
886 		selected = sta;
887 		for (sta = hapd->sta_list; sta; sta = sta->next) {
888 			if (sta != selected)
889 				sta->radius_das_match = 0;
890 		}
891 		wpa_warning_log0(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
892 	}
893 
894 	if (attr->acct_session_id) {
895 		num_attr++;
896 		if (attr->acct_session_id_len != 16) {
897 			wpa_warning_log0(MSG_DEBUG,
898 				   "RADIUS DAS: Acct-Session-Id cannot match");
899 			return NULL;
900 		}
901 		count = 0;
902 
903 		for (sta = hapd->sta_list; sta; sta = sta->next) {
904 			if (!sta->radius_das_match)
905 				continue;
906 			os_snprintf(buf, sizeof(buf), "%016llX",
907 				    (unsigned long long) sta->acct_session_id);
908 			if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
909 				sta->radius_das_match = 0;
910 			else
911 				count++;
912 		}
913 
914 		if (count == 0) {
915 			wpa_warning_log0(MSG_DEBUG,
916 				   "RADIUS DAS: No matches remaining after Acct-Session-Id check");
917 			return NULL;
918 		}
919 		wpa_warning_log0(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
920 	}
921 
922 	if (attr->acct_multi_session_id) {
923 		num_attr++;
924 		if (attr->acct_multi_session_id_len != 16) {
925 			wpa_warning_log0(MSG_DEBUG,
926 				   "RADIUS DAS: Acct-Multi-Session-Id cannot match");
927 			return NULL;
928 		}
929 		count = 0;
930 
931 		for (sta = hapd->sta_list; sta; sta = sta->next) {
932 			if (!sta->radius_das_match)
933 				continue;
934 			if (!sta->eapol_sm ||
935 			    !sta->eapol_sm->acct_multi_session_id) {
936 				sta->radius_das_match = 0;
937 				continue;
938 			}
939 			os_snprintf(buf, sizeof(buf), "%016llX",
940 				    (unsigned long long)
941 				    sta->eapol_sm->acct_multi_session_id);
942 			if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
943 			    0)
944 				sta->radius_das_match = 0;
945 			else
946 				count++;
947 		}
948 
949 		if (count == 0) {
950 			wpa_warning_log0(MSG_DEBUG,
951 				   "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
952 			return NULL;
953 		}
954 		wpa_warning_log0(MSG_DEBUG,
955 			   "RADIUS DAS: Acct-Multi-Session-Id match");
956 	}
957 
958 	if (attr->cui) {
959 		num_attr++;
960 		count = 0;
961 
962 		for (sta = hapd->sta_list; sta; sta = sta->next) {
963 			struct wpabuf *cui;
964 
965 			if (!sta->radius_das_match)
966 				continue;
967 			cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
968 			if (!cui || wpabuf_len(cui) != attr->cui_len ||
969 			    os_memcmp(wpabuf_head(cui), attr->cui,
970 				      attr->cui_len) != 0)
971 				sta->radius_das_match = 0;
972 			else
973 				count++;
974 		}
975 
976 		if (count == 0) {
977 			wpa_warning_log0(MSG_DEBUG,
978 				   "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
979 			return NULL;
980 		}
981 		wpa_warning_log0(MSG_DEBUG,
982 			   "RADIUS DAS: Chargeable-User-Identity match");
983 	}
984 
985 	if (attr->user_name) {
986 		num_attr++;
987 		count = 0;
988 
989 		for (sta = hapd->sta_list; sta; sta = sta->next) {
990 			u8 *identity;
991 			size_t identity_len;
992 
993 			if (!sta->radius_das_match)
994 				continue;
995 			identity = ieee802_1x_get_identity(sta->eapol_sm,
996 							   &identity_len);
997 			if (!identity ||
998 			    identity_len != attr->user_name_len ||
999 			    os_memcmp(identity, attr->user_name, identity_len)
1000 			    != 0)
1001 				sta->radius_das_match = 0;
1002 			else
1003 				count++;
1004 		}
1005 
1006 		if (count == 0) {
1007 			wpa_warning_log0(MSG_DEBUG,
1008 				   "RADIUS DAS: No matches remaining after User-Name check");
1009 			return NULL;
1010 		}
1011 		wpa_warning_log0(MSG_DEBUG,
1012 			   "RADIUS DAS: User-Name match");
1013 	}
1014 
1015 	if (num_attr == 0) {
1016 		/*
1017 		 * In theory, we could match all current associations, but it
1018 		 * seems safer to just reject requests that do not include any
1019 		 * session identification attributes.
1020 		 */
1021 		wpa_warning_log0(MSG_DEBUG,
1022 			   "RADIUS DAS: No session identification attributes included");
1023 		return NULL;
1024 	}
1025 
1026 	selected = NULL;
1027 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1028 		if (sta->radius_das_match) {
1029 			if (selected) {
1030 				*multi = 1;
1031 				return NULL;
1032 			}
1033 			selected = sta;
1034 		}
1035 	}
1036 
1037 	return selected;
1038 }
1039 
1040 
1041 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
1042 					struct radius_das_attrs *attr)
1043 {
1044 	if (!hapd->wpa_auth)
1045 		return -1;
1046 	return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
1047 }
1048 
1049 
1050 static enum radius_das_res
1051 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
1052 {
1053 	struct hostapd_data *hapd = ctx;
1054 	struct sta_info *sta;
1055 	int multi;
1056 
1057 	if (hostapd_das_nas_mismatch(hapd, attr))
1058 		return RADIUS_DAS_NAS_MISMATCH;
1059 
1060 	sta = hostapd_das_find_sta(hapd, attr, &multi);
1061 	if (sta == NULL) {
1062 		if (multi) {
1063 			wpa_warning_log0(MSG_DEBUG,
1064 				   "RADIUS DAS: Multiple sessions match - not supported");
1065 			return RADIUS_DAS_MULTI_SESSION_MATCH;
1066 		}
1067 		if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1068 			wpa_warning_log0(MSG_DEBUG,
1069 				   "RADIUS DAS: PMKSA cache entry matched");
1070 			return RADIUS_DAS_SUCCESS;
1071 		}
1072 		wpa_warning_log0(MSG_DEBUG, "RADIUS DAS: No matching session found");
1073 		return RADIUS_DAS_SESSION_NOT_FOUND;
1074 	}
1075 
1076 	wpa_warning_log4(MSG_DEBUG, "RADIUS DAS: Found a matching session %02x:xx:xx:%02x:%02x:%02x"
1077 		   " - disconnecting", (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
1078 	wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1079 
1080 	hostapd_drv_sta_deauth(hapd, sta->addr,
1081 			       WLAN_REASON_PREV_AUTH_NOT_VALID);
1082 	ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1083 
1084 	return RADIUS_DAS_SUCCESS;
1085 }
1086 
1087 
1088 #ifdef CONFIG_HS20
1089 static enum radius_das_res
1090 hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1091 {
1092 	struct hostapd_data *hapd = ctx;
1093 	struct sta_info *sta;
1094 	int multi;
1095 
1096 	if (hostapd_das_nas_mismatch(hapd, attr))
1097 		return RADIUS_DAS_NAS_MISMATCH;
1098 
1099 	sta = hostapd_das_find_sta(hapd, attr, &multi);
1100 	if (!sta) {
1101 		if (multi) {
1102 			wpa_printf(MSG_DEBUG,
1103 				   "RADIUS DAS: Multiple sessions match - not supported");
1104 			return RADIUS_DAS_MULTI_SESSION_MATCH;
1105 		}
1106 		wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1107 		return RADIUS_DAS_SESSION_NOT_FOUND;
1108 	}
1109 
1110 	wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1111 		   " - CoA", MAC2STR(sta->addr));
1112 
1113 	if (attr->hs20_t_c_filtering) {
1114 		if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1115 			wpa_printf(MSG_DEBUG,
1116 				   "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1117 			return RADIUS_DAS_COA_FAILED;
1118 		}
1119 
1120 		hs20_t_c_filtering(hapd, sta, 0);
1121 	}
1122 
1123 	return RADIUS_DAS_SUCCESS;
1124 }
1125 #else /* CONFIG_HS20 */
1126 #define hostapd_das_coa NULL
1127 #endif /* CONFIG_HS20 */
1128 
1129 
1130 #ifdef CONFIG_SQLITE
1131 
1132 static int db_table_exists(sqlite3 *db, const char *name)
1133 {
1134 	char cmd[128];
1135 
1136 	os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1137 	return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1138 }
1139 
1140 
1141 static int db_table_create_radius_attributes(sqlite3 *db)
1142 {
1143 	char *err = NULL;
1144 	const char *sql =
1145 		"CREATE TABLE radius_attributes("
1146 		" id INTEGER PRIMARY KEY,"
1147 		" sta TEXT,"
1148 		" reqtype TEXT,"
1149 		" attr TEXT"
1150 		");"
1151 		"CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1152 
1153 	wpa_printf(MSG_DEBUG,
1154 		   "Adding database table for RADIUS attribute information");
1155 	if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1156 		wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1157 		sqlite3_free(err);
1158 		return -1;
1159 	}
1160 
1161 	return 0;
1162 }
1163 
1164 #endif /* CONFIG_SQLITE */
1165 
1166 #endif /* CONFIG_NO_RADIUS */
1167 #endif /* EXT_CODE_CROP */
1168 
1169 
1170 /**
1171  * hostapd_setup_bss - Per-BSS setup (initialization)
1172  * @hapd: Pointer to BSS data
1173  * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1174  *	but interface may exist
1175  *
1176  * This function is used to initialize all per-BSS data structures and
1177  * resources. This gets called in a loop for each BSS when an interface is
1178  * initialized. Most of the modules that are initialized here will be
1179  * deinitialized in hostapd_cleanup().
1180  */
1181 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1182 {
1183 	struct hostapd_bss_config *conf = hapd->conf;
1184 	u8 ssid[SSID_MAX_LEN + 1];
1185 	int ssid_len, set_ssid;
1186 	char force_ifname[IFNAMSIZ];
1187 	u8 if_addr[ETH_ALEN];
1188 	int flush_old_stations = 1;
1189 
1190 	wpa_warning_buf(MSG_DEBUG, "hostapd_setup_bss((%s)",
1191 		   conf->iface, strlen(conf->iface));
1192 
1193 #ifdef EAP_SERVER_TNC
1194 	if (conf->tnc && tncs_global_init() < 0) {
1195 		wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1196 		return -1;
1197 	}
1198 #endif /* EAP_SERVER_TNC */
1199 
1200 	if (hapd->started) {
1201 		wpa_error_buf(MSG_ERROR, "hostapd_setup_bss: Interface %s was already started",
1202 			   conf->iface, strlen(conf->iface));
1203 		return -1;
1204 	}
1205 	hapd->started = 1;
1206 
1207 #ifndef EXT_CODE_CROP
1208 	if (!first || first == -1) {
1209 		u8 *addr = hapd->own_addr;
1210 
1211 		if (!is_zero_ether_addr(conf->bssid)) {
1212 			/* Allocate the configured BSSID. */
1213 			os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
1214 
1215 			if (hostapd_mac_comp(hapd->own_addr,
1216 					     hapd->iface->bss[0]->own_addr) ==
1217 			    0) {
1218 				wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1219 					   "BSSID set to the MAC address of "
1220 					   "the radio", conf->iface);
1221 				return -1;
1222 			}
1223 		} else if (hapd->iconf->use_driver_iface_addr) {
1224 			addr = NULL;
1225 		} else {
1226 			/* Allocate the next available BSSID. */
1227 			do {
1228 				inc_byte_array(hapd->own_addr, ETH_ALEN);
1229 			} while (mac_in_conf(hapd->iconf, hapd->own_addr));
1230 		}
1231 
1232 		hapd->interface_added = 1;
1233 		if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
1234 				   conf->iface, addr, hapd,
1235 				   &hapd->drv_priv, force_ifname, if_addr,
1236 				   conf->bridge[0] ? conf->bridge : NULL,
1237 				   first == -1)) {
1238 			wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1239 				   MACSTR ")", MAC2STR(hapd->own_addr));
1240 			hapd->interface_added = 0;
1241 			return -1;
1242 		}
1243 
1244 		if (!addr)
1245 			os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
1246 	}
1247 #endif /* EXT_CODE_CROP */
1248 #ifndef LOS_CONFIG_HOSTAPD_QOS
1249 	if (conf->wmm_enabled < 0)
1250 		conf->wmm_enabled = hapd->iconf->ieee80211n |
1251 			hapd->iconf->ieee80211ax;
1252 #endif /* LOS_CONFIG_HOSTAPD_QOS */
1253 #ifdef CONFIG_IEEE80211R_AP
1254 	if (is_zero_ether_addr(conf->r1_key_holder))
1255 		os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
1256 #endif /* CONFIG_IEEE80211R_AP */
1257 
1258 #ifdef CONFIG_MESH
1259 	if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
1260 		flush_old_stations = 0;
1261 #endif /* CONFIG_MESH */
1262 
1263 	if (flush_old_stations)
1264 #ifndef EXT_CODE_CROP
1265 		hostapd_flush(hapd);
1266 #else
1267 		hostapd_flush_old_stations(hapd,
1268 			WLAN_REASON_PREV_AUTH_NOT_VALID);
1269 #endif /* EXT_CODE_CROP */
1270 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
1271 	hostapd_set_privacy(hapd, 0);
1272 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
1273 #ifdef CONFIG_WEP
1274 	if (!hostapd_drv_nl80211(hapd))
1275 		hostapd_broadcast_wep_clear(hapd);
1276 #ifndef LOS_CONFIG_HOSTAPD_SECURITY
1277 	if (hostapd_setup_encryption(conf->iface, hapd))
1278 		return -1;
1279 #endif
1280 #endif /* CONFIG_WEP */
1281 	/*
1282 	 * Fetch the SSID from the system and use it or,
1283 	 * if one was specified in the config file, verify they
1284 	 * match.
1285 	 */
1286 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
1287 	ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1288 	if (ssid_len < 0) {
1289 		wpa_error_log0(MSG_ERROR, "Could not read SSID from system");
1290 		return -1;
1291 	}
1292 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
1293 	/* ssid has been set in hostapd_config_read2() */
1294 #ifndef EXT_CODE_CROP
1295 	if (conf->ssid.ssid_set) {
1296 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
1297 		/*
1298 		 * If SSID is specified in the config file and it differs
1299 		 * from what is being used then force installation of the
1300 		 * new SSID.
1301 		 */
1302 		set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1303 			    os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1304 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
1305 	} else {
1306 		/*
1307 		 * No SSID in the config file; just use the one we got
1308 		 * from the system.
1309 		 */
1310 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
1311 		set_ssid = 0;
1312 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
1313 		conf->ssid.ssid_len = ssid_len;
1314 		os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1315 	}
1316 #endif /* EXT_CODE_CROP */
1317 	/*
1318 	 * Short SSID calculation is identical to FCS and it is defined in
1319 	 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1320 	 */
1321 #ifndef EXT_CODE_CROP
1322 	conf->ssid.short_ssid = crc32(conf->ssid.ssid, conf->ssid.ssid_len);
1323 #else
1324 	conf->ssid.short_ssid = crc32_wpa(conf->ssid.ssid, conf->ssid.ssid_len);
1325 #endif
1326 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
1327 	if (!hostapd_drv_none(hapd)) {
1328 		wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
1329 			   " and ssid \"%s\"",
1330 			   conf->iface, MAC2STR(hapd->own_addr),
1331 			   wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
1332 	}
1333 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
1334 	if (hostapd_setup_wpa_psk(conf)) {
1335 		wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1336 		return -1;
1337 	}
1338 
1339 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
1340 	/* Set SSID for the kernel driver (to be used in beacon and probe
1341 	 * response frames) */
1342 	if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1343 					 conf->ssid.ssid_len)) {
1344 		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1345 		return -1;
1346 	}
1347 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
1348 
1349 #ifndef CONFIG_NO_RADIUS
1350 	if (wpa_debug_level <= MSG_MSGDUMP)
1351 		conf->radius->msg_dumps = 1;
1352 #ifdef CONFIG_SQLITE
1353 	if (conf->radius_req_attr_sqlite) {
1354 		if (sqlite3_open(conf->radius_req_attr_sqlite,
1355 				 &hapd->rad_attr_db)) {
1356 			wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1357 				   conf->radius_req_attr_sqlite);
1358 			return -1;
1359 		}
1360 
1361 		wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1362 			   conf->radius_req_attr_sqlite);
1363 		if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1364 		    db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1365 			return -1;
1366 	}
1367 #endif /* CONFIG_SQLITE */
1368 
1369 	hapd->radius = radius_client_init(hapd, conf->radius);
1370 	if (hapd->radius == NULL) {
1371 		wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1372 		return -1;
1373 	}
1374 
1375 	if (conf->radius_das_port) {
1376 		struct radius_das_conf das_conf;
1377 		os_memset(&das_conf, 0, sizeof(das_conf));
1378 		das_conf.port = conf->radius_das_port;
1379 		das_conf.shared_secret = conf->radius_das_shared_secret;
1380 		das_conf.shared_secret_len =
1381 			conf->radius_das_shared_secret_len;
1382 		das_conf.client_addr = &conf->radius_das_client_addr;
1383 		das_conf.time_window = conf->radius_das_time_window;
1384 		das_conf.require_event_timestamp =
1385 			conf->radius_das_require_event_timestamp;
1386 		das_conf.require_message_authenticator =
1387 			conf->radius_das_require_message_authenticator;
1388 		das_conf.ctx = hapd;
1389 		das_conf.disconnect = hostapd_das_disconnect;
1390 		das_conf.coa = hostapd_das_coa;
1391 		hapd->radius_das = radius_das_init(&das_conf);
1392 		if (hapd->radius_das == NULL) {
1393 			wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1394 				   "failed.");
1395 			return -1;
1396 		}
1397 	}
1398 #endif /* CONFIG_NO_RADIUS */
1399 #ifndef EXT_CODE_CROP
1400 	if (hostapd_acl_init(hapd)) {
1401 		wpa_printf(MSG_ERROR, "ACL initialization failed.");
1402 		return -1;
1403 	}
1404 #endif /* EXT_CODE_CROP */
1405 #ifdef CONFIG_WPS_AP
1406 	if (hostapd_init_wps(hapd, conf))
1407 		return -1;
1408 #endif /* CONFIG_WPS_AP */
1409 #ifndef EXT_CODE_CROP
1410 #ifdef CONFIG_DPP
1411 	hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1412 	if (!hapd->gas)
1413 		return -1;
1414 	if (hostapd_dpp_init(hapd))
1415 		return -1;
1416 #endif /* CONFIG_DPP */
1417 
1418 	if (authsrv_init(hapd) < 0)
1419 		return -1;
1420 #endif /* EXT_CODE_CROP */
1421 	if (ieee802_1x_init(hapd)) {
1422 		wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1423 		return -1;
1424 	}
1425 #ifndef EXT_CODE_CROP
1426 	if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1427 #else
1428 	if ((conf->wpa) && hostapd_setup_wpa(hapd))
1429 #endif /* EXT_CODE_CROP */
1430 		return -1;
1431 
1432 	if (accounting_init(hapd)) {
1433 		wpa_error_log0(MSG_ERROR, "Accounting initialization failed.");
1434 		return -1;
1435 	}
1436 #ifndef EXT_CODE_CROP
1437 #ifdef CONFIG_INTERWORKING
1438 	if (gas_serv_init(hapd)) {
1439 		wpa_printf(MSG_ERROR, "GAS server initialization failed");
1440 		return -1;
1441 	}
1442 
1443 	if (conf->qos_map_set_len &&
1444 	    hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1445 				    conf->qos_map_set_len)) {
1446 		wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1447 		return -1;
1448 	}
1449 #endif /* CONFIG_INTERWORKING */
1450 
1451 	if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1452 		wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1453 		return -1;
1454 	}
1455 
1456 	if (conf->proxy_arp) {
1457 		if (x_snoop_init(hapd)) {
1458 			wpa_printf(MSG_ERROR,
1459 				   "Generic snooping infrastructure initialization failed");
1460 			return -1;
1461 		}
1462 
1463 		if (dhcp_snoop_init(hapd)) {
1464 			wpa_printf(MSG_ERROR,
1465 				   "DHCP snooping initialization failed");
1466 			return -1;
1467 		}
1468 
1469 		if (ndisc_snoop_init(hapd)) {
1470 			wpa_printf(MSG_ERROR,
1471 				   "Neighbor Discovery snooping initialization failed");
1472 			return -1;
1473 		}
1474 	}
1475 #endif /* EXT_CODE_CROP */
1476 #ifndef LOS_CONFIG_NO_VLAN
1477 	if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1478 		wpa_error_log0(MSG_ERROR, "VLAN initialization failed.");
1479 		return -1;
1480 	}
1481 #endif
1482 #ifndef EXT_CODE_CROP
1483 	if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1484 #else
1485 	if (ieee802_11_set_beacon(hapd) < 0)
1486 #endif /* EXT_CODE_CROP */
1487 		return -1;
1488 #ifndef EXT_CODE_CROP
1489 	if (flush_old_stations && !conf->start_disabled &&
1490 	    conf->broadcast_deauth) {
1491 		u8 addr[ETH_ALEN];
1492 
1493 		/* Should any previously associated STA not have noticed that
1494 		 * the AP had stopped and restarted, send one more
1495 		 * deauthentication notification now that the AP is ready to
1496 		 * operate. */
1497 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1498 			"Deauthenticate all stations at BSS start");
1499 		os_memset(addr, 0xff, ETH_ALEN);
1500 		hostapd_drv_sta_deauth(hapd, addr,
1501 				       WLAN_REASON_PREV_AUTH_NOT_VALID);
1502 	}
1503 #endif /* EXT_CODE_CROP */
1504 	if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1505 		return -1;
1506 
1507 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
1508 	if (hapd->driver && hapd->driver->set_operstate)
1509 		hapd->driver->set_operstate(hapd->drv_priv, 1);
1510 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
1511 	return 0;
1512 }
1513 
1514 #ifndef LOS_CONFIG_HOSTAPD_QOS
1515 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1516 {
1517 	struct hostapd_data *hapd = iface->bss[0];
1518 	int i;
1519 	struct hostapd_tx_queue_params *p;
1520 
1521 #ifdef CONFIG_MESH
1522 	if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
1523 		return;
1524 #endif /* CONFIG_MESH */
1525 
1526 	for (i = 0; i < NUM_TX_QUEUES; i++) {
1527 		p = &iface->conf->tx_queue[i];
1528 
1529 		if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1530 						p->cwmax, p->burst)) {
1531 			wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1532 				   "parameters for queue %d.", i);
1533 			/* Continue anyway */
1534 		}
1535 	}
1536 }
1537 #endif
1538 #ifndef EXT_CODE_CROP
1539 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1540 				struct mac_acl_entry *mac_acl,
1541 				int n_entries, u8 accept_acl)
1542 {
1543 	struct hostapd_acl_params *acl_params;
1544 	int i, err;
1545 
1546 	acl_params = os_zalloc(sizeof(*acl_params) +
1547 			       (n_entries * sizeof(acl_params->mac_acl[0])));
1548 	if (!acl_params)
1549 		return -ENOMEM;
1550 
1551 	for (i = 0; i < n_entries; i++)
1552 		os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1553 			  ETH_ALEN);
1554 
1555 	acl_params->acl_policy = accept_acl;
1556 	acl_params->num_mac_acl = n_entries;
1557 
1558 	err = hostapd_drv_set_acl(hapd, acl_params);
1559 
1560 	os_free(acl_params);
1561 
1562 	return err;
1563 }
1564 
1565 
1566 static void hostapd_set_acl(struct hostapd_data *hapd)
1567 {
1568 	struct hostapd_config *conf = hapd->iconf;
1569 	int err;
1570 	u8 accept_acl;
1571 
1572 	if (hapd->iface->drv_max_acl_mac_addrs == 0)
1573 		return;
1574 
1575 	if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1576 		accept_acl = 1;
1577 		err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1578 					   conf->bss[0]->num_accept_mac,
1579 					   accept_acl);
1580 		if (err) {
1581 			wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1582 			return;
1583 		}
1584 	} else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1585 		accept_acl = 0;
1586 		err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1587 					   conf->bss[0]->num_deny_mac,
1588 					   accept_acl);
1589 		if (err) {
1590 			wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1591 			return;
1592 		}
1593 	}
1594 }
1595 
1596 
1597 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1598 {
1599 	if (!hapd->iface->interfaces ||
1600 	    !hapd->iface->interfaces->ctrl_iface_init)
1601 		return 0;
1602 
1603 	if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1604 		wpa_printf(MSG_ERROR,
1605 			   "Failed to setup control interface for %s",
1606 			   hapd->conf->iface);
1607 		return -1;
1608 	}
1609 
1610 	return 0;
1611 }
1612 #endif /* EXT_CODE_CROP */
1613 
1614 static int start_ctrl_iface(struct hostapd_iface *iface)
1615 {
1616 	size_t i;
1617 
1618 	if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1619 		return 0;
1620 
1621 	for (i = 0; i < iface->num_bss; i++) {
1622 		struct hostapd_data *hapd = iface->bss[i];
1623 		if (iface->interfaces->ctrl_iface_init(hapd)) {
1624 			wpa_error_log1(MSG_ERROR,
1625 				   "Failed to setup control interface for %s",
1626 				   hapd->conf->iface);
1627 			return -1;
1628 		}
1629 	}
1630 
1631 	return 0;
1632 }
1633 
1634 #ifndef EXT_CODE_CROP
1635 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1636 {
1637 	struct hostapd_iface *iface = eloop_ctx;
1638 
1639 	if (!iface->wait_channel_update) {
1640 		wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1641 		return;
1642 	}
1643 
1644 	/*
1645 	 * It is possible that the existing channel list is acceptable, so try
1646 	 * to proceed.
1647 	 */
1648 	wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1649 	setup_interface2(iface);
1650 }
1651 
1652 
1653 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1654 {
1655 	if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1656 		return;
1657 
1658 	wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1659 	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1660 	setup_interface2(iface);
1661 }
1662 #endif /* EXT_CODE_CROP */
1663 
1664 static int setup_interface(struct hostapd_iface *iface)
1665 {
1666 	struct hostapd_data *hapd = iface->bss[0];
1667 	size_t i;
1668 
1669 	/*
1670 	 * It is possible that setup_interface() is called after the interface
1671 	 * was disabled etc., in which case driver_ap_teardown is possibly set
1672 	 * to 1. Clear it here so any other key/station deletion, which is not
1673 	 * part of a teardown flow, would also call the relevant driver
1674 	 * callbacks.
1675 	 */
1676 	iface->driver_ap_teardown = 0;
1677 
1678 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
1679 	if (!iface->phy[0]) {
1680 		const char *phy = hostapd_drv_get_radio_name(hapd);
1681 		if (phy) {
1682 			wpa_printf(MSG_DEBUG, "phy: %s", phy);
1683 			os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1684 		}
1685 	}
1686 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
1687 
1688 	/*
1689 	 * Make sure that all BSSes get configured with a pointer to the same
1690 	 * driver interface.
1691 	 */
1692 	for (i = 1; i < iface->num_bss; i++) {
1693 		iface->bss[i]->driver = hapd->driver;
1694 		iface->bss[i]->drv_priv = hapd->drv_priv;
1695 	}
1696 
1697 	if (hostapd_validate_bssid_configuration(iface))
1698 		return -1;
1699 
1700 	/*
1701 	 * Initialize control interfaces early to allow external monitoring of
1702 	 * channel setup operations that may take considerable amount of time
1703 	 * especially for DFS cases.
1704 	 */
1705 	if (start_ctrl_iface(iface))
1706 		return -1;
1707 
1708 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
1709 	if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1710 		char country[4], previous_country[4];
1711 
1712 		hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1713 		if (hostapd_get_country(hapd, previous_country) < 0)
1714 			previous_country[0] = '\0';
1715 
1716 		os_memcpy(country, hapd->iconf->country, 3);
1717 		country[3] = '\0';
1718 		if (hostapd_set_country(hapd, country) < 0) {
1719 			wpa_printf(MSG_ERROR, "Failed to set country code");
1720 			return -1;
1721 		}
1722 
1723 		wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1724 			   previous_country, country);
1725 
1726 		if (os_strncmp(previous_country, country, 2) != 0) {
1727 			wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1728 			iface->wait_channel_update = 1;
1729 			eloop_register_timeout(5, 0,
1730 					       channel_list_update_timeout,
1731 					       iface, NULL);
1732 			return 0;
1733 		}
1734 	}
1735 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
1736 
1737 	return setup_interface2(iface);
1738 }
1739 
1740 
1741 static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1742 {
1743 	int freq, i, j;
1744 
1745 	if (!iface->conf->channel)
1746 		return 0;
1747 	if (iface->conf->op_class) {
1748 		freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1749 					      iface->conf->channel);
1750 		if (freq < 0) {
1751 			wpa_printf(MSG_INFO,
1752 				   "Could not convert op_class %u channel %u to operating frequency",
1753 				   iface->conf->op_class, iface->conf->channel);
1754 			return -1;
1755 		}
1756 		iface->freq = freq;
1757 		return 0;
1758 	}
1759 
1760 	/* Old configurations using only 2.4/5/60 GHz bands may not specify the
1761 	 * op_class parameter. Select a matching channel from the configured
1762 	 * mode using the channel parameter for these cases.
1763 	 */
1764 	for (j = 0; j < iface->num_hw_features; j++) {
1765 		struct hostapd_hw_modes *mode = &iface->hw_features[j];
1766 
1767 		if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1768 		    iface->conf->hw_mode != mode->mode)
1769 			continue;
1770 		for (i = 0; i < mode->num_channels; i++) {
1771 			struct hostapd_channel_data *chan = &mode->channels[i];
1772 
1773 			if (chan->chan == iface->conf->channel &&
1774 			    !is_6ghz_freq(chan->freq)) {
1775 				iface->freq = chan->freq;
1776 				return 0;
1777 			}
1778 		}
1779 	}
1780 
1781 	wpa_printf(MSG_INFO, "Could not determine operating frequency");
1782 	return -1;
1783 }
1784 
1785 #ifndef EXT_CODE_CROP
1786 static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
1787 {
1788 	int bw, seg0;
1789 
1790 	if (!is_6ghz_op_class(iface->conf->op_class))
1791 		return;
1792 
1793 	seg0 = hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
1794 	bw = center_idx_to_bw_6ghz(seg0);
1795 	/* Assign the secondary channel if absent in config for
1796 	 * bandwidths > 20 MHz */
1797 	if (bw > 20 && !iface->conf->secondary_channel) {
1798 		if (((iface->conf->channel - 1) / 4) % 2)
1799 			iface->conf->secondary_channel = -1;
1800 		else
1801 			iface->conf->secondary_channel = 1;
1802 	}
1803 }
1804 #endif // EXT_CODE_CROP
1805 
1806 static int setup_interface2(struct hostapd_iface *iface)
1807 {
1808 #ifndef EXT_CODE_CROP
1809 	iface->wait_channel_update = 0;
1810 #endif /* EXT_CODE_CROP */
1811 
1812 	if (hostapd_get_hw_features(iface)) {
1813 		/* Not all drivers support this yet, so continue without hw
1814 		 * feature data. */
1815 	} else {
1816 		int ret;
1817 
1818 		ret = configured_fixed_chan_to_freq(iface);
1819 		if (ret < 0)
1820 			goto fail;
1821 
1822 		if (iface->conf->op_class) {
1823 			int ch_width;
1824 
1825 			ch_width = op_class_to_ch_width(iface->conf->op_class);
1826 			hostapd_set_oper_chwidth(iface->conf, ch_width);
1827 #ifndef EXT_CODE_CROP
1828 			hostapd_set_6ghz_sec_chan(iface);
1829 #endif // #ifndef EXT_CODE_CROP
1830 		}
1831 
1832 		ret = hostapd_select_hw_mode(iface);
1833 		if (ret < 0) {
1834 			wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1835 				   "channel. (%d)", ret);
1836 			goto fail;
1837 		}
1838 		if (ret == 1) {
1839 			wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1840 			return 0;
1841 		}
1842 		ret = hostapd_check_edmg_capab(iface);
1843 		if (ret < 0)
1844 			goto fail;
1845 		ret = hostapd_check_he_6ghz_capab(iface);
1846 		if (ret < 0)
1847 			goto fail;
1848 		ret = hostapd_check_ht_capab(iface);
1849 		if (ret < 0)
1850 			goto fail;
1851 		if (ret == 1) {
1852 			wpa_printf(MSG_DEBUG, "Interface initialization will "
1853 				   "be completed in a callback");
1854 			return 0;
1855 		}
1856 
1857 #ifndef EXT_CODE_CROP
1858 		if (iface->conf->ieee80211h)
1859 			wpa_printf(MSG_DEBUG, "DFS support is enabled");
1860 #endif /* EXT_CODE_CROP */
1861 	}
1862 	return hostapd_setup_interface_complete(iface, 0);
1863 
1864 fail:
1865 	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1866 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1867 #ifdef LOS_WPA_PATCH
1868 	wpa_comm_event_report(EXT_WIFI_EVT_AP_DISABLE, iface->bss[0]->msg_ctx, NULL);
1869 #endif /* LOS_WPA_PATCH */
1870 	if (iface->interfaces && iface->interfaces->terminate_on_error) {
1871 #ifndef LOS_WPA_PATCH
1872 		eloop_terminate();
1873 #else
1874 		wpa_error_log1(MSG_ERROR, "eloop_terminate : %d", ELOOP_TASK_HOSTAPD);
1875 		eloop_terminate(ELOOP_TASK_HOSTAPD);
1876 #endif /* LOS_WPA_PATCH */
1877 	}
1878 	return -1;
1879 }
1880 
1881 
1882 #ifdef CONFIG_FST
1883 
1884 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1885 {
1886 	struct hostapd_data *hapd = ctx;
1887 
1888 	return hapd->own_addr;
1889 }
1890 
1891 
1892 static void fst_hostapd_get_channel_info_cb(void *ctx,
1893 					    enum hostapd_hw_mode *hw_mode,
1894 					    u8 *channel)
1895 {
1896 	struct hostapd_data *hapd = ctx;
1897 
1898 	*hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1899 }
1900 
1901 
1902 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1903 {
1904 	struct hostapd_data *hapd = ctx;
1905 
1906 	if (hapd->iface->fst_ies != fst_ies) {
1907 		hapd->iface->fst_ies = fst_ies;
1908 		if (ieee802_11_set_beacon(hapd))
1909 			wpa_warning_log0(MSG_WARNING, "FST: Cannot set beacon");
1910 	}
1911 }
1912 
1913 
1914 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1915 				      struct wpabuf *buf)
1916 {
1917 	struct hostapd_data *hapd = ctx;
1918 
1919 	return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1920 				       wpabuf_head(buf), wpabuf_len(buf));
1921 }
1922 
1923 
1924 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1925 {
1926 	struct hostapd_data *hapd = ctx;
1927 	struct sta_info *sta = ap_get_sta(hapd, addr);
1928 
1929 	return sta ? sta->mb_ies : NULL;
1930 }
1931 
1932 
1933 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1934 					const u8 *buf, size_t size)
1935 {
1936 	struct hostapd_data *hapd = ctx;
1937 	struct sta_info *sta = ap_get_sta(hapd, addr);
1938 
1939 	if (sta) {
1940 		struct mb_ies_info info;
1941 
1942 		if (!mb_ies_info_by_ies(&info, buf, size)) {
1943 			wpabuf_free(sta->mb_ies);
1944 			sta->mb_ies = mb_ies_by_info(&info);
1945 		}
1946 	}
1947 }
1948 
1949 
1950 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1951 				      bool mb_only)
1952 {
1953 	struct sta_info *s = (struct sta_info *) *get_ctx;
1954 
1955 	if (mb_only) {
1956 		for (; s && !s->mb_ies; s = s->next)
1957 			;
1958 	}
1959 
1960 	if (s) {
1961 		*get_ctx = (struct fst_get_peer_ctx *) s->next;
1962 
1963 		return s->addr;
1964 	}
1965 
1966 	*get_ctx = NULL;
1967 	return NULL;
1968 }
1969 
1970 
1971 static const u8 * fst_hostapd_get_peer_first(void *ctx,
1972 					     struct fst_get_peer_ctx **get_ctx,
1973 					     bool mb_only)
1974 {
1975 	struct hostapd_data *hapd = ctx;
1976 
1977 	*get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1978 
1979 	return fst_hostapd_get_sta(get_ctx, mb_only);
1980 }
1981 
1982 
1983 static const u8 * fst_hostapd_get_peer_next(void *ctx,
1984 					    struct fst_get_peer_ctx **get_ctx,
1985 					    bool mb_only)
1986 {
1987 	return fst_hostapd_get_sta(get_ctx, mb_only);
1988 }
1989 
1990 
1991 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1992 				struct fst_wpa_obj *iface_obj)
1993 {
1994 	iface_obj->ctx = hapd;
1995 	iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1996 	iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1997 	iface_obj->set_ies = fst_hostapd_set_ies_cb;
1998 	iface_obj->send_action = fst_hostapd_send_action_cb;
1999 	iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
2000 	iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
2001 	iface_obj->get_peer_first = fst_hostapd_get_peer_first;
2002 	iface_obj->get_peer_next = fst_hostapd_get_peer_next;
2003 }
2004 
2005 #endif /* CONFIG_FST */
2006 
2007 #ifdef CONFIG_OWE
2008 
2009 static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
2010 {
2011 	struct hostapd_data *hapd = ctx;
2012 	size_t i;
2013 
2014 	for (i = 0; i < iface->num_bss; i++) {
2015 		struct hostapd_data *bss = iface->bss[i];
2016 
2017 		if (os_strcmp(hapd->conf->owe_transition_ifname,
2018 			      bss->conf->iface) != 0)
2019 			continue;
2020 
2021 		wpa_printf(MSG_DEBUG,
2022 			   "OWE: ifname=%s found transition mode ifname=%s BSSID "
2023 			   MACSTR " SSID %s",
2024 			   hapd->conf->iface, bss->conf->iface,
2025 			   MAC2STR(bss->own_addr),
2026 			   wpa_ssid_txt(bss->conf->ssid.ssid,
2027 					bss->conf->ssid.ssid_len));
2028 		if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2029 		    is_zero_ether_addr(bss->own_addr))
2030 			continue;
2031 
2032 		os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2033 			  ETH_ALEN);
2034 		os_memcpy(hapd->conf->owe_transition_ssid,
2035 			  bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2036 		hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2037 		wpa_printf(MSG_DEBUG,
2038 			   "OWE: Copied transition mode information");
2039 		return 1;
2040 	}
2041 
2042 	return 0;
2043 }
2044 
2045 
2046 int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2047 {
2048 	if (hapd->conf->owe_transition_ssid_len > 0 &&
2049 	    !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2050 		return 0;
2051 
2052 	/* Find transition mode SSID/BSSID information from a BSS operated by
2053 	 * this hostapd instance. */
2054 	if (!hapd->iface->interfaces ||
2055 	    !hapd->iface->interfaces->for_each_interface)
2056 		return hostapd_owe_iface_iter(hapd->iface, hapd);
2057 	else
2058 		return hapd->iface->interfaces->for_each_interface(
2059 			hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2060 }
2061 
2062 
2063 static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2064 {
2065 	size_t i;
2066 
2067 	for (i = 0; i < iface->num_bss; i++) {
2068 		struct hostapd_data *bss = iface->bss[i];
2069 		int res;
2070 
2071 		if (!bss->conf->owe_transition_ifname[0])
2072 			continue;
2073 		if (bss->iface->state != HAPD_IFACE_ENABLED) {
2074 			wpa_printf(MSG_DEBUG,
2075 				   "OWE: Interface %s state %s - defer beacon update",
2076 				   bss->conf->iface,
2077 				   hostapd_state_text(bss->iface->state));
2078 			continue;
2079 		}
2080 		res = hostapd_owe_trans_get_info(bss);
2081 		if (res == 0)
2082 			continue;
2083 		wpa_printf(MSG_DEBUG,
2084 			   "OWE: Matching transition mode interface enabled - update beacon data for %s",
2085 			   bss->conf->iface);
2086 		ieee802_11_set_beacon(bss);
2087 	}
2088 
2089 	return 0;
2090 }
2091 
2092 #endif /* CONFIG_OWE */
2093 
2094 
2095 static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2096 {
2097 #ifdef CONFIG_OWE
2098 	/* Check whether the enabled BSS can complete OWE transition mode
2099 	 * configuration for any pending interface. */
2100 	if (!iface->interfaces ||
2101 	    !iface->interfaces->for_each_interface)
2102 		hostapd_owe_iface_iter2(iface, NULL);
2103 	else
2104 		iface->interfaces->for_each_interface(
2105 			iface->interfaces, hostapd_owe_iface_iter2, NULL);
2106 #endif /* CONFIG_OWE */
2107 }
2108 
2109 #ifdef CONFIG_P2P
2110 static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2111 						    void *timeout_ctx)
2112 {
2113 	struct hostapd_iface *iface = eloop_ctx;
2114 	struct hostapd_data *hapd;
2115 
2116 	if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2117 		return;
2118 	hapd = iface->bss[0];
2119 	if (hapd->setup_complete_cb)
2120 		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2121 }
2122 #endif /* CONFIG_P2P */
2123 
2124 static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2125 						 int err)
2126 {
2127 	struct hostapd_data *hapd = iface->bss[0];
2128 	size_t j;
2129 	u8 *prev_addr;
2130 	int delay_apply_cfg = 0;
2131 	int res_dfs_offload = 0;
2132 
2133 	if (err)
2134 		goto fail;
2135 
2136 	wpa_warning_log0(MSG_DEBUG, "Completing interface initialization");
2137 	if (iface->freq) {
2138 #ifdef NEED_AP_MLME
2139 		int res;
2140 #endif /* NEED_AP_MLME */
2141 
2142 		wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
2143 			   "Frequency: %d MHz",
2144 			   hostapd_hw_mode_txt(iface->conf->hw_mode),
2145 			   iface->conf->channel, iface->freq);
2146 
2147 #ifndef EXT_CODE_CROP
2148 #ifdef NEED_AP_MLME
2149 		/* Handle DFS only if it is not offloaded to the driver */
2150 		if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2151 			/* Check DFS */
2152 			res = hostapd_handle_dfs(iface);
2153 			if (res <= 0) {
2154 				if (res < 0)
2155 					goto fail;
2156 				return res;
2157 			}
2158 		} else {
2159 			/* If DFS is offloaded to the driver */
2160 			res_dfs_offload = hostapd_handle_dfs_offload(iface);
2161 			if (res_dfs_offload <= 0) {
2162 				if (res_dfs_offload < 0)
2163 					goto fail;
2164 			} else {
2165 				wpa_printf(MSG_DEBUG,
2166 					   "Proceed with AP/channel setup");
2167 				/*
2168 				 * If this is a DFS channel, move to completing
2169 				 * AP setup.
2170 				 */
2171 				if (res_dfs_offload == 1)
2172 					goto dfs_offload;
2173 				/* Otherwise fall through. */
2174 			}
2175 		}
2176 #endif /* NEED_AP_MLME */
2177 
2178 #ifdef CONFIG_MESH
2179 		if (iface->mconf != NULL) {
2180 			wpa_printf(MSG_DEBUG,
2181 				   "%s: Mesh configuration will be applied while joining the mesh network",
2182 				   iface->bss[0]->conf->iface);
2183 			delay_apply_cfg = 1;
2184 		}
2185 #endif /* CONFIG_MESH */
2186 
2187 		if (!delay_apply_cfg &&
2188 		    hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
2189 				     hapd->iconf->channel,
2190 				     hapd->iconf->enable_edmg,
2191 				     hapd->iconf->edmg_channel,
2192 				     hapd->iconf->ieee80211n,
2193 				     hapd->iconf->ieee80211ac,
2194 				     hapd->iconf->ieee80211ax,
2195 				     hapd->iconf->secondary_channel,
2196 				     hostapd_get_oper_chwidth(hapd->iconf),
2197 				     hostapd_get_oper_centr_freq_seg0_idx(
2198 					     hapd->iconf),
2199 				     hostapd_get_oper_centr_freq_seg1_idx(
2200 					     hapd->iconf))) {
2201 			wpa_printf(MSG_ERROR, "Could not set channel for "
2202 				   "kernel driver");
2203 			goto fail;
2204 		}
2205 #endif /* EXT_CODE_CROP */
2206 	}
2207 
2208 	if (iface->current_mode) {
2209 		if (hostapd_prepare_rates(iface, iface->current_mode)) {
2210 			wpa_error_log0(MSG_ERROR, "Failed to prepare rates "
2211 				   "table.");
2212 #ifndef EXT_CODE_CROP
2213 			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2214 				       HOSTAPD_LEVEL_WARNING,
2215 				       "Failed to prepare rates table.");
2216 #endif /* EXT_CODE_CROP */
2217 			goto fail;
2218 		}
2219 	}
2220 
2221 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
2222 	if (hapd->iconf->rts_threshold >= -1 &&
2223 	    hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2224 	    hapd->iconf->rts_threshold >= -1) {
2225 		wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2226 			   "kernel driver");
2227 		goto fail;
2228 	}
2229 
2230 	if (hapd->iconf->fragm_threshold >= -1 &&
2231 	    hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2232 	    hapd->iconf->fragm_threshold != -1) {
2233 		wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2234 			   "for kernel driver");
2235 		goto fail;
2236 	}
2237 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
2238 
2239 	prev_addr = hapd->own_addr;
2240 
2241 	for (j = 0; j < iface->num_bss; j++) {
2242 		hapd = iface->bss[j];
2243 		if (j)
2244 			os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
2245 		if (hostapd_setup_bss(hapd, j == 0)) {
2246 			for (;;) {
2247 				hapd = iface->bss[j];
2248 				hostapd_bss_deinit_no_free(hapd);
2249 				hostapd_free_hapd_data(hapd);
2250 				if (j == 0)
2251 					break;
2252 				j--;
2253 			}
2254 			goto fail;
2255 		}
2256 		if (is_zero_ether_addr(hapd->conf->bssid))
2257 			prev_addr = hapd->own_addr;
2258 	}
2259 	hapd = iface->bss[0];
2260 #ifndef LOS_CONFIG_HOSTAPD_QOS
2261 	hostapd_tx_queue_params(iface);
2262 #endif
2263 #ifndef LOS_HOSTAPD_CONFIG_CROP
2264 	ap_list_init(iface);
2265 #endif /* LOS_HOSTAPD_CONFIG_CROP */
2266 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
2267 	hostapd_set_acl(hapd);
2268 
2269 	if (hostapd_driver_commit(hapd) < 0) {
2270 		wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2271 			   "configuration", __func__);
2272 		goto fail;
2273 	}
2274 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
2275 
2276 #ifdef CONFIG_WPS_AP
2277 	/*
2278 	 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2279 	 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2280 	 * mode), the interface is up only after driver_commit, so initialize
2281 	 * WPS after driver_commit.
2282 	 */
2283 	for (j = 0; j < iface->num_bss; j++) {
2284 		if (hostapd_init_wps_complete(iface->bss[j]))
2285 			goto fail;
2286 	}
2287 #endif /* CONFIG_WPS_AP */
2288 #ifndef EXT_CODE_CROP
2289 	if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2290 	    !res_dfs_offload) {
2291 		/*
2292 		 * If freq is DFS, and DFS is offloaded to the driver, then wait
2293 		 * for CAC to complete.
2294 		 */
2295 		wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2296 		return res_dfs_offload;
2297 	}
2298 
2299 #ifdef NEED_AP_MLME
2300 dfs_offload:
2301 #endif /* NEED_AP_MLME */
2302 #endif /* EXT_CODE_CROP */
2303 #ifdef CONFIG_FST
2304 	if (hapd->iconf->fst_cfg.group_id[0]) {
2305 		struct fst_wpa_obj iface_obj;
2306 
2307 		fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2308 		iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2309 					&iface_obj, &hapd->iconf->fst_cfg);
2310 		if (!iface->fst) {
2311 			wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2312 				   hapd->iconf->fst_cfg.group_id);
2313 			goto fail;
2314 		}
2315 	}
2316 #endif /* CONFIG_FST */
2317 
2318 	hostapd_set_state(iface, HAPD_IFACE_ENABLED);
2319 #ifndef EXT_CODE_CROP
2320 	hostapd_owe_update_trans(iface);
2321 #endif /* EXT_CODE_CROP */
2322 #ifndef LOS_HOSTAPD_CONFIG_CROP
2323 	airtime_policy_update_init(iface);
2324 #endif /* LOS_HOSTAPD_CONFIG_CROP */
2325 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
2326 #ifdef LOS_WPA_PATCH
2327 	wpa_comm_event_report(EXT_WIFI_EVT_AP_START, iface->bss[0]->msg_ctx, NULL);
2328 #endif /* LOS_WPA_PATCH */
2329 #ifdef CONFIG_P2P
2330 	if (hapd->setup_complete_cb)
2331 		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2332 #endif /* CONFIG_P2P */
2333 #ifdef CONFIG_MESH
2334 	if (delay_apply_cfg && !iface->mconf) {
2335 		wpa_printf(MSG_ERROR, "Error while completing mesh init");
2336 		goto fail;
2337 	}
2338 #endif /* CONFIG_MESH */
2339 
2340 	wpa_warning_buf(MSG_DEBUG, "%s: Setup of interface done.",
2341 		   iface->bss[0]->conf->iface, strlen(iface->bss[0]->conf->iface));
2342 	if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2343 		iface->interfaces->terminate_on_error--;
2344 
2345 #ifndef LOS_CONFIG_HOSTAPD_RRM
2346 	for (j = 0; j < iface->num_bss; j++)
2347 		hostapd_neighbor_set_own_report(iface->bss[j]);
2348 #endif /* LOS_CONFIG_HOSTAPD_RRM */
2349 	return 0;
2350 
2351 fail:
2352 	wpa_error_log0(MSG_ERROR, "Interface initialization failed");
2353 	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2354 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2355 #ifdef LOS_WPA_PATCH
2356 	wpa_comm_event_report(EXT_WIFI_EVT_AP_DISABLE, hapd->msg_ctx, NULL);
2357 #endif /* LOS_WPA_PATCH */
2358 
2359 #ifdef CONFIG_FST
2360 	if (iface->fst) {
2361 		fst_detach(iface->fst);
2362 		iface->fst = NULL;
2363 	}
2364 #endif /* CONFIG_FST */
2365 
2366 	if (iface->interfaces && iface->interfaces->terminate_on_error) {
2367 #ifndef LOS_WPA_PATCH
2368 		eloop_terminate();
2369 #else
2370 		wpa_error_log1(MSG_ERROR, "eloop_terminate : %d", ELOOP_TASK_HOSTAPD);
2371 		eloop_terminate(ELOOP_TASK_HOSTAPD);
2372 #endif /* LOS_WPA_PATCH */
2373 #ifdef CONFIG_P2P
2374 	} else if (hapd->setup_complete_cb) {
2375 		/*
2376 		 * Calling hapd->setup_complete_cb directly may cause iface
2377 		 * deinitialization which may be accessed later by the caller.
2378 		 */
2379 		eloop_register_timeout(0, 0,
2380 				       hostapd_interface_setup_failure_handler,
2381 				       iface, NULL);
2382 #endif /* CONFIG_P2P */
2383 	}
2384 
2385 	return -1;
2386 }
2387 
2388 
2389 /**
2390  * hostapd_setup_interface_complete - Complete interface setup
2391  *
2392  * This function is called when previous steps in the interface setup has been
2393  * completed. This can also start operations, e.g., DFS, that will require
2394  * additional processing before interface is ready to be enabled. Such
2395  * operations will call this function from eloop callbacks when finished.
2396  */
2397 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2398 {
2399 #ifndef EXT_CODE_CROP
2400 	struct hapd_interfaces *interfaces = iface->interfaces;
2401 	struct hostapd_data *hapd = iface->bss[0];
2402 	unsigned int i;
2403 	int not_ready_in_sync_ifaces = 0;
2404 
2405 	if (!iface->need_to_start_in_sync)
2406 #endif /* EXT_CODE_CROP */
2407 		return hostapd_setup_interface_complete_sync(iface, err);
2408 
2409 #ifndef EXT_CODE_CROP
2410 	if (err) {
2411 		wpa_error_log0(MSG_ERROR, "Interface initialization failed");
2412 		hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2413 		iface->need_to_start_in_sync = 0;
2414 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2415 #ifdef LOS_WPA_PATCH
2416 		wpa_comm_event_report(EXT_WIFI_EVT_AP_DISABLE, hapd->msg_ctx, NULL);
2417 #endif /* LOS_WPA_PATCH */
2418 
2419 		if (interfaces && interfaces->terminate_on_error)
2420 #ifndef LOS_WPA_PATCH
2421 			eloop_terminate();
2422 #else
2423 			eloop_terminate(ELOOP_TASK_HOSTAPD);
2424 #endif /* LOS_WPA_PATCH */
2425 		return -1;
2426 	}
2427 
2428 	if (iface->ready_to_start_in_sync) {
2429 		/* Already in ready and waiting. should never happpen */
2430 		return 0;
2431 	}
2432 
2433 	for (i = 0; i < interfaces->count; i++) {
2434 		if (interfaces->iface[i]->need_to_start_in_sync &&
2435 		    !interfaces->iface[i]->ready_to_start_in_sync)
2436 			not_ready_in_sync_ifaces++;
2437 	}
2438 
2439 	/*
2440 	 * Check if this is the last interface, if yes then start all the other
2441 	 * waiting interfaces. If not, add this interface to the waiting list.
2442 	 */
2443 	if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2444 		/*
2445 		 * If this interface went through CAC, do not synchronize, just
2446 		 * start immediately.
2447 		 */
2448 		iface->need_to_start_in_sync = 0;
2449 		wpa_printf(MSG_INFO,
2450 			   "%s: Finished CAC - bypass sync and start interface",
2451 			   iface->bss[0]->conf->iface);
2452 		return hostapd_setup_interface_complete_sync(iface, err);
2453 	}
2454 
2455 	if (not_ready_in_sync_ifaces > 1) {
2456 		/* need to wait as there are other interfaces still coming up */
2457 		iface->ready_to_start_in_sync = 1;
2458 		wpa_printf(MSG_INFO,
2459 			   "%s: Interface waiting to sync with other interfaces",
2460 			   iface->bss[0]->conf->iface);
2461 		return 0;
2462 	}
2463 
2464 	wpa_printf(MSG_INFO,
2465 		   "%s: Last interface to sync - starting all interfaces",
2466 		   iface->bss[0]->conf->iface);
2467 	iface->need_to_start_in_sync = 0;
2468 	hostapd_setup_interface_complete_sync(iface, err);
2469 	for (i = 0; i < interfaces->count; i++) {
2470 		if (interfaces->iface[i]->need_to_start_in_sync &&
2471 		    interfaces->iface[i]->ready_to_start_in_sync) {
2472 			hostapd_setup_interface_complete_sync(
2473 				interfaces->iface[i], 0);
2474 			/* Only once the interfaces are sync started */
2475 			interfaces->iface[i]->need_to_start_in_sync = 0;
2476 		}
2477 	}
2478 
2479 	return 0;
2480 #endif /* EXT_CODE_CROP */
2481 }
2482 
2483 
2484 /**
2485  * hostapd_setup_interface - Setup of an interface
2486  * @iface: Pointer to interface data.
2487  * Returns: 0 on success, -1 on failure
2488  *
2489  * Initializes the driver interface, validates the configuration,
2490  * and sets driver parameters based on the configuration.
2491  * Flushes old stations, sets the channel, encryption,
2492  * beacons, and WDS links based on the configuration.
2493  *
2494  * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2495  * or DFS operations, this function returns 0 before such operations have been
2496  * completed. The pending operations are registered into eloop and will be
2497  * completed from eloop callbacks. Those callbacks end up calling
2498  * hostapd_setup_interface_complete() once setup has been completed.
2499  */
2500 int hostapd_setup_interface(struct hostapd_iface *iface)
2501 {
2502 	int ret;
2503 
2504 	if (!iface->conf)
2505 		return -1;
2506 	ret = setup_interface(iface);
2507 	if (ret) {
2508 		wpa_error_buf(MSG_ERROR, "%s: Unable to setup interface.",
2509 			   iface->bss[0]->conf->iface, strlen(iface->bss[0]->conf->iface));
2510 		return -1;
2511 	}
2512 
2513 	return 0;
2514 }
2515 
2516 
2517 /**
2518  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2519  * @hapd_iface: Pointer to interface data
2520  * @conf: Pointer to per-interface configuration
2521  * @bss: Pointer to per-BSS configuration for this BSS
2522  * Returns: Pointer to allocated BSS data
2523  *
2524  * This function is used to allocate per-BSS data structure. This data will be
2525  * freed after hostapd_cleanup() is called for it during interface
2526  * deinitialization.
2527  */
2528 struct hostapd_data *
2529 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2530 		       struct hostapd_config *conf,
2531 		       struct hostapd_bss_config *bss)
2532 {
2533 	struct hostapd_data *hapd;
2534 
2535 	hapd = os_zalloc(sizeof(*hapd));
2536 	if (hapd == NULL)
2537 		return NULL;
2538 
2539 #ifdef CONFIG_P2P
2540 	hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2541 #endif /* CONFIG_P2P */
2542 	hapd->iconf = conf;
2543 	hapd->conf = bss;
2544 	hapd->iface = hapd_iface;
2545 	if (conf)
2546 		hapd->driver = conf->driver;
2547 #ifndef EXT_CODE_CROP
2548 	hapd->ctrl_sock = -1;
2549 	dl_list_init(&hapd->ctrl_dst);
2550 	hapd->dhcp_sock = -1;
2551 #endif /* EXT_CODE_CROP */
2552 #ifndef LOS_CONFIG_HOSTAPD_RRM
2553 	dl_list_init(&hapd->nr_db);
2554 #endif /* LOS_CONFIG_HOSTAPD_RRM */
2555 #ifdef CONFIG_IEEE80211R_AP
2556 	dl_list_init(&hapd->l2_queue);
2557 	dl_list_init(&hapd->l2_oui_queue);
2558 #endif /* CONFIG_IEEE80211R_AP */
2559 #ifdef CONFIG_SAE
2560 	dl_list_init(&hapd->sae_commit_queue);
2561 #endif /* CONFIG_SAE */
2562 
2563 	return hapd;
2564 }
2565 
2566 
2567 static void hostapd_bss_deinit(struct hostapd_data *hapd)
2568 {
2569 	if (!hapd)
2570 		return;
2571 	wpa_warning_buf(MSG_DEBUG, "hostapd_bss_deinit: deinit bss %s",
2572 		   hapd->conf->iface, strlen(hapd->conf->iface));
2573 	hostapd_bss_deinit_no_free(hapd);
2574 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2575 #ifdef LOS_WPA_PATCH
2576 	wpa_comm_event_report(EXT_WIFI_EVT_AP_DISABLE, hapd->msg_ctx, NULL);
2577 #endif /* LOS_WPA_PATCH */
2578 
2579 #ifdef CONFIG_SQLITE
2580 	if (hapd->rad_attr_db) {
2581 		sqlite3_close(hapd->rad_attr_db);
2582 		hapd->rad_attr_db = NULL;
2583 	}
2584 #endif /* CONFIG_SQLITE */
2585 	hostapd_cleanup(hapd);
2586 }
2587 
2588 
2589 void hostapd_interface_deinit(struct hostapd_iface *iface)
2590 {
2591 	int j;
2592 
2593 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2594 	if (iface == NULL)
2595 		return;
2596 
2597 	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2598 
2599 #ifndef EXT_CODE_CROP
2600 	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2601 	iface->wait_channel_update = 0;
2602 #endif /* EXT_CODE_CROP */
2603 
2604 #ifdef CONFIG_FST
2605 	if (iface->fst) {
2606 		fst_detach(iface->fst);
2607 		iface->fst = NULL;
2608 	}
2609 #endif /* CONFIG_FST */
2610 
2611 	for (j = (int) iface->num_bss - 1; j >= 0; j--) {
2612 		if (!iface->bss)
2613 			break;
2614 		hostapd_bss_deinit(iface->bss[j]);
2615 	}
2616 
2617 #ifndef LOS_HOSTAPD_CONFIG_CROP
2618 #ifdef NEED_AP_MLME
2619 	hostapd_stop_setup_timers(iface);
2620 	eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2621 #endif /* NEED_AP_MLME */
2622 #endif /* LOS_HOSTAPD_CONFIG_CROP */
2623 }
2624 
2625 
2626 void hostapd_interface_free(struct hostapd_iface *iface)
2627 {
2628 	size_t j;
2629 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2630 	for (j = 0; j < iface->num_bss; j++) {
2631 		if (!iface->bss)
2632 			break;
2633 		wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2634 			   __func__, iface->bss[j]);
2635 		os_free(iface->bss[j]);
2636 	}
2637 	hostapd_cleanup_iface(iface);
2638 }
2639 
2640 
2641 struct hostapd_iface * hostapd_alloc_iface(void)
2642 {
2643 	struct hostapd_iface *hapd_iface;
2644 
2645 	hapd_iface = os_zalloc(sizeof(*hapd_iface));
2646 	if (!hapd_iface)
2647 		return NULL;
2648 
2649 #ifndef EXT_CODE_CROP
2650 	dl_list_init(&hapd_iface->sta_seen);
2651 #endif /* EXT_CODE_CROP */
2652 
2653 	return hapd_iface;
2654 }
2655 
2656 
2657 /**
2658  * hostapd_init - Allocate and initialize per-interface data
2659  * @config_file: Path to the configuration file
2660  * Returns: Pointer to the allocated interface data or %NULL on failure
2661  *
2662  * This function is used to allocate main data structures for per-interface
2663  * data. The allocated data buffer will be freed by calling
2664  * hostapd_cleanup_iface().
2665  */
2666 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2667 				    const char *config_file)
2668 {
2669 	struct hostapd_iface *hapd_iface = NULL;
2670 	struct hostapd_config *conf = NULL;
2671 	struct hostapd_data *hapd;
2672 	size_t i;
2673 
2674 	hapd_iface = hostapd_alloc_iface();
2675 	if (hapd_iface == NULL)
2676 		goto fail;
2677 
2678 	hapd_iface->config_fname = os_strdup(config_file);
2679 	if (hapd_iface->config_fname == NULL)
2680 		goto fail;
2681 
2682 	conf = interfaces->config_read_cb(hapd_iface->config_fname);
2683 	if (conf == NULL)
2684 		goto fail;
2685 	hapd_iface->conf = conf;
2686 
2687 	hapd_iface->num_bss = conf->num_bss;
2688 	hapd_iface->bss = os_calloc(conf->num_bss,
2689 				    sizeof(struct hostapd_data *));
2690 	if (hapd_iface->bss == NULL)
2691 		goto fail;
2692 
2693 	for (i = 0; i < conf->num_bss; i++) {
2694 		hapd = hapd_iface->bss[i] =
2695 			hostapd_alloc_bss_data(hapd_iface, conf,
2696 					       conf->bss[i]);
2697 		if (hapd == NULL)
2698 			goto fail;
2699 		hapd->msg_ctx = hapd;
2700 	}
2701 
2702 	return hapd_iface;
2703 
2704 fail:
2705 	wpa_error_buf(MSG_ERROR, "Failed to set up interface with %s",
2706 		   config_file, strlen(config_file));
2707 	if (conf)
2708 		hostapd_config_free(conf);
2709 	if (hapd_iface) {
2710 		os_free(hapd_iface->config_fname);
2711 		os_free(hapd_iface->bss);
2712 		wpa_printf(MSG_DEBUG, "%s: free iface %p",
2713 			   __func__, hapd_iface);
2714 		os_free(hapd_iface);
2715 	}
2716 	return NULL;
2717 }
2718 
2719 #ifndef EXT_CODE_CROP
2720 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2721 {
2722 	size_t i, j;
2723 
2724 	for (i = 0; i < interfaces->count; i++) {
2725 		struct hostapd_iface *iface = interfaces->iface[i];
2726 		for (j = 0; j < iface->num_bss; j++) {
2727 			struct hostapd_data *hapd = iface->bss[j];
2728 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
2729 				return 1;
2730 		}
2731 	}
2732 
2733 	return 0;
2734 }
2735 
2736 
2737 /**
2738  * hostapd_interface_init_bss - Read configuration file and init BSS data
2739  *
2740  * This function is used to parse configuration file for a BSS. This BSS is
2741  * added to an existing interface sharing the same radio (if any) or a new
2742  * interface is created if this is the first interface on a radio. This
2743  * allocate memory for the BSS. No actual driver operations are started.
2744  *
2745  * This is similar to hostapd_interface_init(), but for a case where the
2746  * configuration is used to add a single BSS instead of all BSSes for a radio.
2747  */
2748 struct hostapd_iface *
2749 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2750 			   const char *config_fname, int debug)
2751 {
2752 	struct hostapd_iface *new_iface = NULL, *iface = NULL;
2753 	struct hostapd_data *hapd;
2754 	int k;
2755 	size_t i, bss_idx;
2756 
2757 	if (!phy || !*phy)
2758 		return NULL;
2759 
2760 	for (i = 0; i < interfaces->count; i++) {
2761 		if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2762 			iface = interfaces->iface[i];
2763 			break;
2764 		}
2765 	}
2766 
2767 	wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2768 		   config_fname, phy, iface ? "" : " --> new PHY");
2769 	if (iface) {
2770 		struct hostapd_config *conf;
2771 		struct hostapd_bss_config **tmp_conf;
2772 		struct hostapd_data **tmp_bss;
2773 		struct hostapd_bss_config *bss;
2774 		const char *ifname;
2775 
2776 		/* Add new BSS to existing iface */
2777 		conf = interfaces->config_read_cb(config_fname);
2778 		if (conf == NULL)
2779 			return NULL;
2780 		if (conf->num_bss > 1) {
2781 			wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2782 			hostapd_config_free(conf);
2783 			return NULL;
2784 		}
2785 
2786 		ifname = conf->bss[0]->iface;
2787 		if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2788 			wpa_printf(MSG_ERROR,
2789 				   "Interface name %s already in use", ifname);
2790 			hostapd_config_free(conf);
2791 			return NULL;
2792 		}
2793 
2794 		tmp_conf = os_realloc_array(
2795 			iface->conf->bss, iface->conf->num_bss + 1,
2796 			sizeof(struct hostapd_bss_config *));
2797 		tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2798 					   sizeof(struct hostapd_data *));
2799 		if (tmp_bss)
2800 			iface->bss = tmp_bss;
2801 		if (tmp_conf) {
2802 			iface->conf->bss = tmp_conf;
2803 			iface->conf->last_bss = tmp_conf[0];
2804 		}
2805 		if (tmp_bss == NULL || tmp_conf == NULL) {
2806 			hostapd_config_free(conf);
2807 			return NULL;
2808 		}
2809 		bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2810 		iface->conf->num_bss++;
2811 
2812 		hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2813 		if (hapd == NULL) {
2814 			iface->conf->num_bss--;
2815 			hostapd_config_free(conf);
2816 			return NULL;
2817 		}
2818 		iface->conf->last_bss = bss;
2819 		iface->bss[iface->num_bss] = hapd;
2820 		hapd->msg_ctx = hapd;
2821 
2822 		bss_idx = iface->num_bss++;
2823 		conf->num_bss--;
2824 		conf->bss[0] = NULL;
2825 		hostapd_config_free(conf);
2826 	} else {
2827 		/* Add a new iface with the first BSS */
2828 		new_iface = iface = hostapd_init(interfaces, config_fname);
2829 		if (!iface)
2830 			return NULL;
2831 		os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2832 		iface->interfaces = interfaces;
2833 		bss_idx = 0;
2834 	}
2835 
2836 	for (k = 0; k < debug; k++) {
2837 		if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2838 			iface->bss[bss_idx]->conf->logger_stdout_level--;
2839 	}
2840 
2841 	if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2842 	    !hostapd_drv_none(iface->bss[bss_idx])) {
2843 		wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2844 			   config_fname);
2845 		if (new_iface)
2846 			hostapd_interface_deinit_free(new_iface);
2847 		return NULL;
2848 	}
2849 
2850 	return iface;
2851 }
2852 #endif /* EXT_CODE_CROP */
2853 
2854 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2855 {
2856 	const struct wpa_driver_ops *driver;
2857 	void *drv_priv;
2858 
2859 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2860 	if (iface == NULL)
2861 		return;
2862 	wpa_warning_log2(MSG_DEBUG, "hostapd_interface_deinit_free: num_bss=%u conf->num_bss=%u",
2863 		   (unsigned int) iface->num_bss,
2864 		   (unsigned int) iface->conf->num_bss);
2865 	driver = iface->bss[0]->driver;
2866 	drv_priv = iface->bss[0]->drv_priv;
2867 	hostapd_interface_deinit(iface);
2868 	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2869 		   __func__, driver, drv_priv);
2870 	if (driver && driver->hapd_deinit && drv_priv) {
2871 		driver->hapd_deinit(drv_priv);
2872 		iface->bss[0]->drv_priv = NULL;
2873 	}
2874 	hostapd_interface_free(iface);
2875 }
2876 
2877 
2878 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2879 				  void *drv_priv,
2880 				  struct hostapd_iface *hapd_iface)
2881 {
2882 	size_t j;
2883 
2884 	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2885 		   __func__, driver, drv_priv);
2886 	if (driver && driver->hapd_deinit && drv_priv) {
2887 		driver->hapd_deinit(drv_priv);
2888 		for (j = 0; j < hapd_iface->num_bss; j++) {
2889 			wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2890 				   __func__, (int) j,
2891 				   hapd_iface->bss[j]->drv_priv);
2892 			if (hapd_iface->bss[j]->drv_priv == drv_priv) {
2893 				hapd_iface->bss[j]->drv_priv = NULL;
2894 #ifndef EXT_CODE_CROP
2895 				hapd_iface->extended_capa = NULL;
2896 				hapd_iface->extended_capa_mask = NULL;
2897 				hapd_iface->extended_capa_len = 0;
2898 #endif /* EXT_CODE_CROP */
2899 			}
2900 		}
2901 	}
2902 }
2903 
2904 
2905 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2906 {
2907 	size_t j;
2908 
2909 	if (!hapd_iface)
2910 		return -1;
2911 
2912 	if (hapd_iface->enable_iface_cb)
2913 		return hapd_iface->enable_iface_cb(hapd_iface);
2914 
2915 	if (hapd_iface->bss[0]->drv_priv != NULL) {
2916 		wpa_error_buf(MSG_ERROR, "Interface %s already enabled",
2917 			   hapd_iface->conf->bss[0]->iface, strlen(hapd_iface->conf->bss[0]->iface));
2918 		return -1;
2919 	}
2920 
2921 	wpa_warning_buf(MSG_DEBUG, "Enable interface %s",
2922 		   hapd_iface->conf->bss[0]->iface, strlen(hapd_iface->conf->bss[0]->iface));
2923 
2924 	for (j = 0; j < hapd_iface->num_bss; j++)
2925 		hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2926 	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2927 		wpa_warning_log0(MSG_INFO, "Invalid configuration - cannot enable");
2928 		return -1;
2929 	}
2930 
2931 	if (hapd_iface->interfaces == NULL ||
2932 	    hapd_iface->interfaces->driver_init == NULL ||
2933 	    hapd_iface->interfaces->driver_init(hapd_iface))
2934 		return -1;
2935 
2936 	if (hostapd_setup_interface(hapd_iface)) {
2937 		hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2938 				      hapd_iface->bss[0]->drv_priv,
2939 				      hapd_iface);
2940 		return -1;
2941 	}
2942 
2943 	return 0;
2944 }
2945 #ifndef EXT_CODE_CROP
2946 
2947 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2948 {
2949 	size_t j;
2950 
2951 	wpa_printf(MSG_DEBUG, "Reload interface %s",
2952 		   hapd_iface->conf->bss[0]->iface);
2953 	for (j = 0; j < hapd_iface->num_bss; j++)
2954 		hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2955 	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2956 		wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2957 		return -1;
2958 	}
2959 	hostapd_clear_old(hapd_iface);
2960 	for (j = 0; j < hapd_iface->num_bss; j++)
2961 		hostapd_reload_bss(hapd_iface->bss[j]);
2962 
2963 	return 0;
2964 }
2965 #endif
2966 
2967 #if !defined(EXT_CODE_CROP) || defined(CONFIG_ACS)
2968 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2969 {
2970 	size_t j;
2971 	const struct wpa_driver_ops *driver;
2972 	void *drv_priv;
2973 
2974 	if (hapd_iface == NULL)
2975 		return -1;
2976 
2977 	if (hapd_iface->disable_iface_cb)
2978 		return hapd_iface->disable_iface_cb(hapd_iface);
2979 
2980 	if (hapd_iface->bss[0]->drv_priv == NULL) {
2981 		wpa_warning_buf(MSG_INFO, "Interface %s already disabled",
2982 			   hapd_iface->conf->bss[0]->iface, strlen(hapd_iface->conf->bss[0]->iface));
2983 		return -1;
2984 	}
2985 
2986 	wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2987 #ifdef LOS_WPA_PATCH
2988 	wpa_comm_event_report(EXT_WIFI_EVT_AP_DISABLE, hapd_iface->bss[0]->msg_ctx, NULL);
2989 #endif /* LOS_WPA_PATCH */
2990 
2991 	driver = hapd_iface->bss[0]->driver;
2992 	drv_priv = hapd_iface->bss[0]->drv_priv;
2993 
2994 	hapd_iface->driver_ap_teardown =
2995 		!!(hapd_iface->drv_flags &
2996 		   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2997 
2998 #ifdef NEED_AP_MLME
2999 	for (j = 0; j < hapd_iface->num_bss; j++)
3000 		hostapd_cleanup_cs_params(hapd_iface->bss[j]);
3001 #endif /* NEED_AP_MLME */
3002 
3003 	/* same as hostapd_interface_deinit without deinitializing ctrl-iface */
3004 	for (j = 0; j < hapd_iface->num_bss; j++) {
3005 		struct hostapd_data *hapd = hapd_iface->bss[j];
3006 		hostapd_bss_deinit_no_free(hapd);
3007 		hostapd_free_hapd_data(hapd);
3008 	}
3009 
3010 	hostapd_deinit_driver(driver, drv_priv, hapd_iface);
3011 
3012 	/* From hostapd_cleanup_iface: These were initialized in
3013 	 * hostapd_setup_interface and hostapd_setup_interface_complete
3014 	 */
3015 	hostapd_cleanup_iface_partial(hapd_iface);
3016 
3017 	wpa_warning_buf(MSG_DEBUG, "Interface %s disabled",
3018 		   hapd_iface->bss[0]->conf->iface, strlen(hapd_iface->bss[0]->conf->iface));
3019 	hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
3020 	return 0;
3021 }
3022 #endif
3023 
3024 #ifndef EXT_CODE_CROP
3025 static struct hostapd_iface *
3026 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
3027 {
3028 	struct hostapd_iface **iface, *hapd_iface;
3029 
3030 	iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
3031 				 sizeof(struct hostapd_iface *));
3032 	if (iface == NULL)
3033 		return NULL;
3034 	interfaces->iface = iface;
3035 	hapd_iface = interfaces->iface[interfaces->count] =
3036 		hostapd_alloc_iface();
3037 	if (hapd_iface == NULL) {
3038 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3039 			   "the interface", __func__);
3040 		return NULL;
3041 	}
3042 	interfaces->count++;
3043 	hapd_iface->interfaces = interfaces;
3044 
3045 	return hapd_iface;
3046 }
3047 
3048 
3049 static struct hostapd_config *
3050 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
3051 		     const char *ctrl_iface, const char *driver)
3052 {
3053 	struct hostapd_bss_config *bss;
3054 	struct hostapd_config *conf;
3055 
3056 	/* Allocates memory for bss and conf */
3057 	conf = hostapd_config_defaults();
3058 	if (conf == NULL) {
3059 		 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3060 				"configuration", __func__);
3061 		 return NULL;
3062 	}
3063 
3064 	if (driver) {
3065 		int j;
3066 
3067 		for (j = 0; wpa_drivers[j]; j++) {
3068 			if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3069 				conf->driver = wpa_drivers[j];
3070 				goto skip;
3071 			}
3072 		}
3073 
3074 		wpa_printf(MSG_ERROR,
3075 			   "Invalid/unknown driver '%s' - registering the default driver",
3076 			   driver);
3077 	}
3078 
3079 	conf->driver = wpa_drivers[0];
3080 	if (conf->driver == NULL) {
3081 		wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3082 		hostapd_config_free(conf);
3083 		return NULL;
3084 	}
3085 
3086 skip:
3087 	bss = conf->last_bss = conf->bss[0];
3088 
3089 	os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3090 	bss->ctrl_interface = os_strdup(ctrl_iface);
3091 	if (bss->ctrl_interface == NULL) {
3092 		hostapd_config_free(conf);
3093 		return NULL;
3094 	}
3095 
3096 	/* Reading configuration file skipped, will be done in SET!
3097 	 * From reading the configuration till the end has to be done in
3098 	 * SET
3099 	 */
3100 	return conf;
3101 }
3102 
3103 
3104 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3105 			      struct hostapd_config *conf)
3106 {
3107 	size_t i;
3108 	struct hostapd_data *hapd;
3109 
3110 	hapd_iface->bss = os_calloc(conf->num_bss,
3111 				    sizeof(struct hostapd_data *));
3112 	if (hapd_iface->bss == NULL)
3113 		return -1;
3114 
3115 	for (i = 0; i < conf->num_bss; i++) {
3116 		hapd = hapd_iface->bss[i] =
3117 			hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
3118 		if (hapd == NULL) {
3119 			while (i > 0) {
3120 				i--;
3121 				os_free(hapd_iface->bss[i]);
3122 				hapd_iface->bss[i] = NULL;
3123 			}
3124 			os_free(hapd_iface->bss);
3125 			hapd_iface->bss = NULL;
3126 			return -1;
3127 		}
3128 		hapd->msg_ctx = hapd;
3129 	}
3130 
3131 	hapd_iface->conf = conf;
3132 	hapd_iface->num_bss = conf->num_bss;
3133 
3134 	return 0;
3135 }
3136 
3137 
3138 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3139 {
3140 	struct hostapd_config *conf = NULL;
3141 	struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3142 	struct hostapd_data *hapd;
3143 	char *ptr;
3144 	size_t i, j;
3145 	const char *conf_file = NULL, *phy_name = NULL;
3146 
3147 	if (os_strncmp(buf, "bss_config=", 11) == 0) {
3148 		char *pos;
3149 		phy_name = buf + 11;
3150 		pos = os_strchr(phy_name, ':');
3151 		if (!pos)
3152 			return -1;
3153 		*pos++ = '\0';
3154 		conf_file = pos;
3155 		if (!os_strlen(conf_file))
3156 			return -1;
3157 
3158 		hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3159 							conf_file, 0);
3160 		if (!hapd_iface)
3161 			return -1;
3162 		for (j = 0; j < interfaces->count; j++) {
3163 			if (interfaces->iface[j] == hapd_iface)
3164 				break;
3165 		}
3166 		if (j == interfaces->count) {
3167 			struct hostapd_iface **tmp;
3168 			tmp = os_realloc_array(interfaces->iface,
3169 					       interfaces->count + 1,
3170 					       sizeof(struct hostapd_iface *));
3171 			if (!tmp) {
3172 				hostapd_interface_deinit_free(hapd_iface);
3173 				return -1;
3174 			}
3175 			interfaces->iface = tmp;
3176 			interfaces->iface[interfaces->count++] = hapd_iface;
3177 			new_iface = hapd_iface;
3178 		}
3179 
3180 		if (new_iface) {
3181 			if (interfaces->driver_init(hapd_iface))
3182 				goto fail;
3183 
3184 			if (hostapd_setup_interface(hapd_iface)) {
3185 				hostapd_deinit_driver(
3186 					hapd_iface->bss[0]->driver,
3187 					hapd_iface->bss[0]->drv_priv,
3188 					hapd_iface);
3189 				goto fail;
3190 			}
3191 		} else {
3192 			/* Assign new BSS with bss[0]'s driver info */
3193 			hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3194 			hapd->driver = hapd_iface->bss[0]->driver;
3195 			hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3196 			os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3197 				  ETH_ALEN);
3198 
3199 			if (start_ctrl_iface_bss(hapd) < 0 ||
3200 			    (hapd_iface->state == HAPD_IFACE_ENABLED &&
3201 			     hostapd_setup_bss(hapd, -1))) {
3202 				hostapd_cleanup(hapd);
3203 				hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
3204 				hapd_iface->conf->num_bss--;
3205 				hapd_iface->num_bss--;
3206 				wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3207 					   __func__, hapd, hapd->conf->iface);
3208 				hostapd_config_free_bss(hapd->conf);
3209 				hapd->conf = NULL;
3210 				os_free(hapd);
3211 				return -1;
3212 			}
3213 		}
3214 		hostapd_owe_update_trans(hapd_iface);
3215 		return 0;
3216 	}
3217 
3218 	ptr = os_strchr(buf, ' ');
3219 	if (ptr == NULL)
3220 		return -1;
3221 	*ptr++ = '\0';
3222 
3223 	if (os_strncmp(ptr, "config=", 7) == 0)
3224 		conf_file = ptr + 7;
3225 
3226 	for (i = 0; i < interfaces->count; i++) {
3227 		if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
3228 			       buf)) {
3229 			wpa_printf(MSG_INFO, "Cannot add interface - it "
3230 				   "already exists");
3231 			return -1;
3232 		}
3233 	}
3234 
3235 	hapd_iface = hostapd_iface_alloc(interfaces);
3236 	if (hapd_iface == NULL) {
3237 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3238 			   "for interface", __func__);
3239 		goto fail;
3240 	}
3241 	new_iface = hapd_iface;
3242 
3243 	if (conf_file && interfaces->config_read_cb) {
3244 		conf = interfaces->config_read_cb(conf_file);
3245 		if (conf && conf->bss)
3246 			os_strlcpy(conf->bss[0]->iface, buf,
3247 				   sizeof(conf->bss[0]->iface));
3248 	} else {
3249 		char *driver = os_strchr(ptr, ' ');
3250 
3251 		if (driver)
3252 			*driver++ = '\0';
3253 		conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3254 	}
3255 
3256 	if (conf == NULL || conf->bss == NULL) {
3257 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3258 			   "for configuration", __func__);
3259 		goto fail;
3260 	}
3261 
3262 	if (hostapd_data_alloc(hapd_iface, conf) < 0) {
3263 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3264 			   "for hostapd", __func__);
3265 		goto fail;
3266 	}
3267 	conf = NULL;
3268 
3269 	if (start_ctrl_iface(hapd_iface) < 0)
3270 		goto fail;
3271 
3272 	wpa_printf(MSG_INFO, "Add interface '%s'",
3273 		   hapd_iface->conf->bss[0]->iface);
3274 
3275 	return 0;
3276 
3277 fail:
3278 	if (conf)
3279 		hostapd_config_free(conf);
3280 	if (hapd_iface) {
3281 		if (hapd_iface->bss) {
3282 			for (i = 0; i < hapd_iface->num_bss; i++) {
3283 				hapd = hapd_iface->bss[i];
3284 				if (!hapd)
3285 					continue;
3286 				if (hapd_iface->interfaces &&
3287 				    hapd_iface->interfaces->ctrl_iface_deinit)
3288 					hapd_iface->interfaces->
3289 						ctrl_iface_deinit(hapd);
3290 				wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3291 					   __func__, hapd_iface->bss[i],
3292 					   hapd->conf->iface);
3293 				hostapd_cleanup(hapd);
3294 				os_free(hapd);
3295 				hapd_iface->bss[i] = NULL;
3296 			}
3297 			os_free(hapd_iface->bss);
3298 			hapd_iface->bss = NULL;
3299 		}
3300 		if (new_iface) {
3301 			interfaces->count--;
3302 			interfaces->iface[interfaces->count] = NULL;
3303 		}
3304 		hostapd_cleanup_iface(hapd_iface);
3305 	}
3306 	return -1;
3307 }
3308 #endif /* EXT_CODE_CROP */
3309 
3310 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3311 {
3312 	size_t i;
3313 
3314 	wpa_warning_buf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface, strlen(iface->conf->bss[idx]->iface));
3315 
3316 	/* Remove hostapd_data only if it has already been initialized */
3317 	if (idx < iface->num_bss) {
3318 		struct hostapd_data *hapd = iface->bss[idx];
3319 
3320 		hostapd_bss_deinit(hapd);
3321 		wpa_warning_buf(MSG_DEBUG, "hostapd_remove_bss: free hapd (%s)",
3322 			   hapd->conf->iface, strlen(hapd->conf->iface));
3323 
3324 		hostapd_config_free_bss(hapd->conf);
3325 		hapd->conf = NULL;
3326 		os_free(hapd);
3327 
3328 		iface->num_bss--;
3329 
3330 		for (i = idx; i < iface->num_bss; i++)
3331 			iface->bss[i] = iface->bss[i + 1];
3332 	} else {
3333 		hostapd_config_free_bss(iface->conf->bss[idx]);
3334 		iface->conf->bss[idx] = NULL;
3335 	}
3336 
3337 	iface->conf->num_bss--;
3338 	for (i = idx; i < iface->conf->num_bss; i++)
3339 		iface->conf->bss[i] = iface->conf->bss[i + 1];
3340 
3341 	return 0;
3342 }
3343 
3344 
3345 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3346 {
3347 	struct hostapd_iface *hapd_iface;
3348 	size_t i, j, k = 0;
3349 
3350 	for (i = 0; i < interfaces->count; i++) {
3351 		hapd_iface = interfaces->iface[i];
3352 		if (hapd_iface == NULL)
3353 			return -1;
3354 		if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
3355 			wpa_warning_buf(MSG_INFO, "Remove interface '%s'", buf, strlen(buf));
3356 #ifndef EXT_CODE_CROP
3357 			hapd_iface->driver_ap_teardown =
3358 				!!(hapd_iface->drv_flags &
3359 				   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3360 #endif /* EXT_CODE_CROP */
3361 
3362 			hostapd_interface_deinit_free(hapd_iface);
3363 			k = i;
3364 			while (k < (interfaces->count - 1)) {
3365 				interfaces->iface[k] =
3366 					interfaces->iface[k + 1];
3367 				k++;
3368 			}
3369 			interfaces->count--;
3370 			return 0;
3371 		}
3372 
3373 		for (j = 0; j < hapd_iface->conf->num_bss; j++) {
3374 			if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3375 #ifndef EXT_CODE_CROP
3376 				hapd_iface->driver_ap_teardown =
3377 					!(hapd_iface->drv_flags &
3378 					  WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3379 #endif /* EXT_CODE_CROP */
3380 				return hostapd_remove_bss(hapd_iface, j);
3381 			}
3382 		}
3383 	}
3384 	return -1;
3385 }
3386 
3387 
3388 /**
3389  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3390  * @hapd: Pointer to BSS data
3391  * @sta: Pointer to the associated STA data
3392  * @reassoc: 1 to indicate this was a re-association; 0 = first association
3393  *
3394  * This function will be called whenever a station associates with the AP. It
3395  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3396  * from drv_callbacks.c based on driver events for drivers that take care of
3397  * management frames (IEEE 802.11 authentication and association) internally.
3398  */
3399 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3400 			   int reassoc)
3401 {
3402 #ifndef EXT_CODE_CROP
3403 	if (hapd->tkip_countermeasures) {
3404 		hostapd_drv_sta_deauth(hapd, sta->addr,
3405 				       WLAN_REASON_MICHAEL_MIC_FAILURE);
3406 		return;
3407 	}
3408 #endif /* EXT_CODE_CROP */
3409 	hostapd_prune_associations(hapd, sta->addr);
3410 	ap_sta_clear_disconnect_timeouts(hapd, sta);
3411 	sta->post_csa_sa_query = 0;
3412 
3413 #ifdef CONFIG_P2P
3414 	if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3415 		sta->no_p2p_set = 1;
3416 		hapd->num_sta_no_p2p++;
3417 		if (hapd->num_sta_no_p2p == 1)
3418 			hostapd_p2p_non_p2p_sta_connected(hapd);
3419 	}
3420 #endif /* CONFIG_P2P */
3421 
3422 #ifndef LOS_HOSTAPD_CONFIG_CROP
3423 	airtime_policy_new_sta(hapd, sta);
3424 #endif /* LOS_HOSTAPD_CONFIG_CROP */
3425 
3426 	/* Start accounting here, if IEEE 802.1X and WPA are not used.
3427 	 * IEEE 802.1X/WPA code will start accounting after the station has
3428 	 * been authorized. */
3429 #ifndef LOS_CONFIG_HOSTAPD_SECURITY
3430 	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3431 #else
3432 	if (!hapd->conf->wpa) {
3433 #endif
3434 		ap_sta_set_authorized(hapd, sta, 1);
3435 		os_get_reltime(&sta->connected_time);
3436 		accounting_sta_start(hapd, sta);
3437 	}
3438 
3439 	/* Start IEEE 802.1X authentication process for new stations */
3440 	ieee802_1x_new_station(hapd, sta);
3441 	if (reassoc) {
3442 		if (sta->auth_alg != WLAN_AUTH_FT &&
3443 #ifndef EXT_CODE_CROP
3444 		    sta->auth_alg != WLAN_AUTH_FILS_SK &&
3445 		    sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3446 		    sta->auth_alg != WLAN_AUTH_FILS_PK &&
3447 #endif /* EXT_CODE_CROP */
3448 		    !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3449 			wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3450 	} else
3451 		wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
3452 
3453 #ifndef EXT_CODE_CROP
3454 	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3455 		if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3456 			wpa_warning_buf(MSG_DEBUG,
3457 				   "%s: hostapd_new_assoc_sta: canceled wired ap_handle_timer timeout for ",
3458 				   hapd->conf->iface, strlen(hapd->conf->iface));
3459 			wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
3460 				   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
3461 		}
3462 	}
3463 #endif /* EXT_CODE_CROP */
3464 	if (!(hapd->iface->drv_flags &
3465 		     WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
3466 		wpa_warning_buf(MSG_DEBUG,
3467 			   "%s: hostapd_new_assoc_sta: reschedule ap_handle_timer timeout for ",
3468 			   hapd->conf->iface, strlen(hapd->conf->iface));
3469 		wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
3470 				(sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
3471 		wpa_warning_log1(MSG_DEBUG, " (%d seconds - ap_max_inactivity)", hapd->conf->ap_max_inactivity);
3472 		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3473 		eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3474 				       ap_handle_timer, hapd, sta);
3475 	}
3476 
3477 #ifdef CONFIG_MACSEC
3478 	if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3479 	    hapd->conf->mka_psk_set)
3480 		ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3481 	else
3482 		ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3483 #endif /* CONFIG_MACSEC */
3484 }
3485 
3486 
3487 const char * hostapd_state_text(enum hostapd_iface_state s)
3488 {
3489 	switch (s) {
3490 	case HAPD_IFACE_UNINITIALIZED:
3491 		return "UNINITIALIZED";
3492 	case HAPD_IFACE_DISABLED:
3493 		return "DISABLED";
3494 	case HAPD_IFACE_COUNTRY_UPDATE:
3495 		return "COUNTRY_UPDATE";
3496 	case HAPD_IFACE_ACS:
3497 		return "ACS";
3498 	case HAPD_IFACE_HT_SCAN:
3499 		return "HT_SCAN";
3500 	case HAPD_IFACE_DFS:
3501 		return "DFS";
3502 	case HAPD_IFACE_ENABLED:
3503 		return "ENABLED";
3504 	}
3505 
3506 	return "UNKNOWN";
3507 }
3508 
3509 
3510 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3511 {
3512 	wpa_warning_buf(MSG_INFO, "%s",
3513 		   iface->conf ? iface->conf->bss[0]->iface : "N/A", iface->conf ? strlen(iface->conf->bss[0]->iface) : strlen("N/A"));
3514 	wpa_warning_two_buf(MSG_INFO, "interface state %s->%s",
3515 		   hostapd_state_text(iface->state), strlen(hostapd_state_text(iface->state)),
3516 		   hostapd_state_text(s), strlen(hostapd_state_text(s)));
3517 	iface->state = s;
3518 }
3519 
3520 #ifndef EXT_CODE_CROP
3521 int hostapd_csa_in_progress(struct hostapd_iface *iface)
3522 {
3523 	unsigned int i;
3524 
3525 	for (i = 0; i < iface->num_bss; i++)
3526 		if (iface->bss[i]->csa_in_progress)
3527 			return 1;
3528 	return 0;
3529 }
3530 #endif /* EXT_CODE_CROP */
3531 
3532 #ifdef NEED_AP_MLME
3533 
3534 #ifndef EXT_CODE_CROP
3535 static void free_beacon_data(struct beacon_data *beacon)
3536 {
3537 	os_free(beacon->head);
3538 	beacon->head = NULL;
3539 	os_free(beacon->tail);
3540 	beacon->tail = NULL;
3541 	os_free(beacon->probe_resp);
3542 	beacon->probe_resp = NULL;
3543 	os_free(beacon->beacon_ies);
3544 	beacon->beacon_ies = NULL;
3545 	os_free(beacon->proberesp_ies);
3546 	beacon->proberesp_ies = NULL;
3547 	os_free(beacon->assocresp_ies);
3548 	beacon->assocresp_ies = NULL;
3549 }
3550 
3551 
3552 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
3553 				     struct beacon_data *beacon)
3554 {
3555 	struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3556 	struct wpa_driver_ap_params params;
3557 	int ret;
3558 
3559 	os_memset(beacon, 0, sizeof(*beacon));
3560 	ret = ieee802_11_build_ap_params(hapd, &params);
3561 	if (ret < 0)
3562 		return ret;
3563 
3564 	ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3565 					 &proberesp_extra,
3566 					 &assocresp_extra);
3567 	if (ret)
3568 		goto free_ap_params;
3569 
3570 	ret = -1;
3571 	beacon->head = os_memdup(params.head, params.head_len);
3572 	if (!beacon->head)
3573 		goto free_ap_extra_ies;
3574 
3575 	beacon->head_len = params.head_len;
3576 
3577 	beacon->tail = os_memdup(params.tail, params.tail_len);
3578 	if (!beacon->tail)
3579 		goto free_beacon;
3580 
3581 	beacon->tail_len = params.tail_len;
3582 
3583 	if (params.proberesp != NULL) {
3584 		beacon->probe_resp = os_memdup(params.proberesp,
3585 					       params.proberesp_len);
3586 		if (!beacon->probe_resp)
3587 			goto free_beacon;
3588 
3589 		beacon->probe_resp_len = params.proberesp_len;
3590 	}
3591 
3592 	/* copy the extra ies */
3593 	if (beacon_extra) {
3594 		beacon->beacon_ies = os_memdup(beacon_extra->buf,
3595 					       wpabuf_len(beacon_extra));
3596 		if (!beacon->beacon_ies)
3597 			goto free_beacon;
3598 
3599 		beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3600 	}
3601 
3602 	if (proberesp_extra) {
3603 		beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3604 						  wpabuf_len(proberesp_extra));
3605 		if (!beacon->proberesp_ies)
3606 			goto free_beacon;
3607 
3608 		beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3609 	}
3610 
3611 	if (assocresp_extra) {
3612 		beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3613 						  wpabuf_len(assocresp_extra));
3614 		if (!beacon->assocresp_ies)
3615 			goto free_beacon;
3616 
3617 		beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3618 	}
3619 
3620 	ret = 0;
3621 free_beacon:
3622 	/* if the function fails, the caller should not free beacon data */
3623 	if (ret)
3624 		free_beacon_data(beacon);
3625 
3626 free_ap_extra_ies:
3627 	hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3628 				  assocresp_extra);
3629 free_ap_params:
3630 	ieee802_11_free_ap_params(&params);
3631 	return ret;
3632 }
3633 
3634 
3635 /*
3636  * TODO: This flow currently supports only changing channel and width within
3637  * the same hw_mode. Any other changes to MAC parameters or provided settings
3638  * are not supported.
3639  */
3640 static int hostapd_change_config_freq(struct hostapd_data *hapd,
3641 				      struct hostapd_config *conf,
3642 				      struct hostapd_freq_params *params,
3643 				      struct hostapd_freq_params *old_params)
3644 {
3645 	int channel;
3646 	u8 seg0, seg1;
3647 	struct hostapd_hw_modes *mode;
3648 
3649 	if (!params->channel) {
3650 		/* check if the new channel is supported by hw */
3651 		params->channel = hostapd_hw_get_channel(hapd, params->freq);
3652 	}
3653 
3654 	channel = params->channel;
3655 	if (!channel)
3656 		return -1;
3657 
3658 	mode = hapd->iface->current_mode;
3659 
3660 	/* if a pointer to old_params is provided we save previous state */
3661 	if (old_params &&
3662 	    hostapd_set_freq_params(old_params, conf->hw_mode,
3663 				    hostapd_hw_get_freq(hapd, conf->channel),
3664 				    conf->channel, conf->enable_edmg,
3665 				    conf->edmg_channel, conf->ieee80211n,
3666 				    conf->ieee80211ac, conf->ieee80211ax,
3667 				    conf->secondary_channel,
3668 				    hostapd_get_oper_chwidth(conf),
3669 				    hostapd_get_oper_centr_freq_seg0_idx(conf),
3670 				    hostapd_get_oper_centr_freq_seg1_idx(conf),
3671 				    conf->vht_capab,
3672 				    mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3673 				    NULL))
3674 		return -1;
3675 
3676 	switch (params->bandwidth) {
3677 	case 0:
3678 	case 20:
3679 		conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3680 		break;
3681 	case 40:
3682 	case 80:
3683 	case 160:
3684 		conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3685 		break;
3686 	default:
3687 		return -1;
3688 	}
3689 
3690 	switch (params->bandwidth) {
3691 	case 0:
3692 	case 20:
3693 	case 40:
3694 		hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
3695 		break;
3696 	case 80:
3697 		if (params->center_freq2)
3698 			hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
3699 		else
3700 			hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
3701 		break;
3702 	case 160:
3703 		hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
3704 		break;
3705 	default:
3706 		return -1;
3707 	}
3708 
3709 	conf->channel = channel;
3710 	conf->ieee80211n = params->ht_enabled;
3711 	conf->ieee80211ac = params->vht_enabled;
3712 	conf->secondary_channel = params->sec_channel_offset;
3713 	ieee80211_freq_to_chan(params->center_freq1,
3714 			       &seg0);
3715 	ieee80211_freq_to_chan(params->center_freq2,
3716 			       &seg1);
3717 	hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3718 	hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
3719 
3720 	/* TODO: maybe call here hostapd_config_check here? */
3721 
3722 	return 0;
3723 }
3724 
3725 
3726 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
3727 				     struct csa_settings *settings)
3728 {
3729 	struct hostapd_iface *iface = hapd->iface;
3730 	struct hostapd_freq_params old_freq;
3731 	int ret;
3732 	u8 chan, bandwidth;
3733 
3734 	os_memset(&old_freq, 0, sizeof(old_freq));
3735 	if (!iface || !iface->freq || hapd->csa_in_progress)
3736 		return -1;
3737 
3738 	switch (settings->freq_params.bandwidth) {
3739 	case 80:
3740 		if (settings->freq_params.center_freq2)
3741 			bandwidth = CHANWIDTH_80P80MHZ;
3742 		else
3743 			bandwidth = CHANWIDTH_80MHZ;
3744 		break;
3745 	case 160:
3746 		bandwidth = CHANWIDTH_160MHZ;
3747 		break;
3748 	default:
3749 		bandwidth = CHANWIDTH_USE_HT;
3750 		break;
3751 	}
3752 
3753 	if (ieee80211_freq_to_channel_ext(
3754 		    settings->freq_params.freq,
3755 		    settings->freq_params.sec_channel_offset,
3756 		    bandwidth,
3757 		    &hapd->iface->cs_oper_class,
3758 		    &chan) == NUM_HOSTAPD_MODES) {
3759 		wpa_printf(MSG_DEBUG,
3760 			   "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
3761 			   settings->freq_params.freq,
3762 			   settings->freq_params.sec_channel_offset,
3763 			   settings->freq_params.vht_enabled,
3764 			   settings->freq_params.he_enabled);
3765 		return -1;
3766 	}
3767 
3768 	settings->freq_params.channel = chan;
3769 
3770 	ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3771 					 &settings->freq_params,
3772 					 &old_freq);
3773 	if (ret)
3774 		return ret;
3775 
3776 	ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
3777 
3778 	/* change back the configuration */
3779 	hostapd_change_config_freq(iface->bss[0], iface->conf,
3780 				   &old_freq, NULL);
3781 
3782 	if (ret)
3783 		return ret;
3784 
3785 	/* set channel switch parameters for csa ie */
3786 	hapd->cs_freq_params = settings->freq_params;
3787 	hapd->cs_count = settings->cs_count;
3788 	hapd->cs_block_tx = settings->block_tx;
3789 
3790 	ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
3791 	if (ret) {
3792 		free_beacon_data(&settings->beacon_after);
3793 		return ret;
3794 	}
3795 
3796 	settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3797 	settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3798 	settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3799 	settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
3800 
3801 	return 0;
3802 }
3803 #endif /* EXT_CODE_CROP */
3804 
3805 #if !defined(EXT_CODE_CROP) || defined(CONFIG_ACS)
3806 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3807 {
3808 	os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3809 	hapd->cs_count = 0;
3810 	hapd->cs_block_tx = 0;
3811 	hapd->cs_c_off_beacon = 0;
3812 	hapd->cs_c_off_proberesp = 0;
3813 	hapd->csa_in_progress = 0;
3814 	hapd->cs_c_off_ecsa_beacon = 0;
3815 	hapd->cs_c_off_ecsa_proberesp = 0;
3816 }
3817 #endif
3818 
3819 #ifndef EXT_CODE_CROP
3820 void hostapd_chan_switch_config(struct hostapd_data *hapd,
3821 				struct hostapd_freq_params *freq_params)
3822 {
3823 	if (freq_params->he_enabled)
3824 		hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
3825 	else
3826 		hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
3827 
3828 	if (freq_params->vht_enabled)
3829 		hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3830 	else
3831 		hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3832 
3833 	hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3834 		       HOSTAPD_LEVEL_INFO,
3835 		       "CHAN_SWITCH HE config 0x%x VHT config 0x%x",
3836 		       hapd->iconf->ch_switch_he_config,
3837 		       hapd->iconf->ch_switch_vht_config);
3838 }
3839 
3840 
3841 int hostapd_switch_channel(struct hostapd_data *hapd,
3842 			   struct csa_settings *settings)
3843 {
3844 	int ret;
3845 
3846 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3847 		wpa_printf(MSG_INFO, "CSA is not supported");
3848 		return -1;
3849 	}
3850 
3851 	ret = hostapd_fill_csa_settings(hapd, settings);
3852 	if (ret)
3853 		return ret;
3854 
3855 	ret = hostapd_drv_switch_channel(hapd, settings);
3856 	free_beacon_data(&settings->beacon_csa);
3857 	free_beacon_data(&settings->beacon_after);
3858 
3859 	if (ret) {
3860 		/* if we failed, clean cs parameters */
3861 		hostapd_cleanup_cs_params(hapd);
3862 		return ret;
3863 	}
3864 
3865 	hapd->csa_in_progress = 1;
3866 	return 0;
3867 }
3868 
3869 
3870 void
3871 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3872 				const struct hostapd_freq_params *freq_params)
3873 {
3874 	int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
3875 
3876 	wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3877 
3878 	if (freq_params->center_freq1)
3879 		seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3880 	if (freq_params->center_freq2)
3881 		seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3882 
3883 	switch (freq_params->bandwidth) {
3884 	case 0:
3885 	case 20:
3886 	case 40:
3887 		bw = CHANWIDTH_USE_HT;
3888 		break;
3889 	case 80:
3890 		if (freq_params->center_freq2)
3891 			bw = CHANWIDTH_80P80MHZ;
3892 		else
3893 			bw = CHANWIDTH_80MHZ;
3894 		break;
3895 	case 160:
3896 		bw = CHANWIDTH_160MHZ;
3897 		break;
3898 	default:
3899 		wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3900 			   freq_params->bandwidth);
3901 		break;
3902 	}
3903 
3904 	iface->freq = freq_params->freq;
3905 	iface->conf->channel = freq_params->channel;
3906 	iface->conf->secondary_channel = freq_params->sec_channel_offset;
3907 	hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3908 	hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3909 	hostapd_set_oper_chwidth(iface->conf, bw);
3910 	iface->conf->ieee80211n = freq_params->ht_enabled;
3911 	iface->conf->ieee80211ac = freq_params->vht_enabled;
3912 	iface->conf->ieee80211ax = freq_params->he_enabled;
3913 
3914 	/*
3915 	 * cs_params must not be cleared earlier because the freq_params
3916 	 * argument may actually point to one of these.
3917 	 * These params will be cleared during interface disable below.
3918 	 */
3919 	hostapd_disable_iface(iface);
3920 	hostapd_enable_iface(iface);
3921 }
3922 
3923 #endif /* NEED_AP_MLME */
3924 #endif /* EXT_CODE_CROP */
3925 
3926 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3927 					const char *ifname)
3928 {
3929 	size_t i, j;
3930 
3931 	for (i = 0; i < interfaces->count; i++) {
3932 		struct hostapd_iface *iface = interfaces->iface[i];
3933 
3934 		for (j = 0; j < iface->num_bss; j++) {
3935 			struct hostapd_data *hapd = iface->bss[j];
3936 
3937 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
3938 				return hapd;
3939 		}
3940 	}
3941 
3942 	return NULL;
3943 }
3944 
3945 #ifndef EXT_CODE_CROP
3946 void hostapd_periodic_iface(struct hostapd_iface *iface)
3947 {
3948 	size_t i;
3949 
3950 #ifndef LOS_HOSTAPD_CONFIG_CROP
3951 	ap_list_timer(iface);
3952 #endif /* LOS_HOSTAPD_CONFIG_CROP */
3953 
3954 	for (i = 0; i < iface->num_bss; i++) {
3955 		struct hostapd_data *hapd = iface->bss[i];
3956 
3957 		if (!hapd->started)
3958 			continue;
3959 
3960 #ifndef CONFIG_NO_RADIUS
3961 		hostapd_acl_expire(hapd);
3962 #endif /* CONFIG_NO_RADIUS */
3963 	}
3964 }
3965 #endif /* EXT_CODE_CROP */
3966 
3967 #ifdef CONFIG_OCV
3968 void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
3969 {
3970 	struct hostapd_data *hapd = eloop_ctx;
3971 	struct sta_info *sta;
3972 
3973 	wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
3974 
3975 	for (sta = hapd->sta_list; sta; sta = sta->next) {
3976 		if (!sta->post_csa_sa_query)
3977 			continue;
3978 
3979 		wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
3980 			   " did not start SA Query after CSA - disconnect",
3981 			   MAC2STR(sta->addr));
3982 		ap_sta_disconnect(hapd, sta, sta->addr,
3983 				  WLAN_REASON_PREV_AUTH_NOT_VALID);
3984 	}
3985 }
3986 #endif /* CONFIG_OCV */
3987