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