xref: /freebsd/contrib/wpa/src/ap/hostapd.c (revision 84ed8638)
1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * hostapd / Initialization and configuration
3c1d255d3SCy Schubert  * Copyright (c) 2002-2021, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
7e28a4053SRui Paulo  */
8e28a4053SRui Paulo 
9e28a4053SRui Paulo #include "utils/includes.h"
10206b73d0SCy Schubert #ifdef CONFIG_SQLITE
11206b73d0SCy Schubert #include <sqlite3.h>
12206b73d0SCy Schubert #endif /* CONFIG_SQLITE */
13e28a4053SRui Paulo 
14e28a4053SRui Paulo #include "utils/common.h"
15e28a4053SRui Paulo #include "utils/eloop.h"
16c1d255d3SCy Schubert #include "utils/crc32.h"
17e28a4053SRui Paulo #include "common/ieee802_11_defs.h"
185b9c547cSRui Paulo #include "common/wpa_ctrl.h"
19780fb4a2SCy Schubert #include "common/hw_features_common.h"
20e28a4053SRui Paulo #include "radius/radius_client.h"
21f05cddf9SRui Paulo #include "radius/radius_das.h"
225b9c547cSRui Paulo #include "eap_server/tncs.h"
235b9c547cSRui Paulo #include "eapol_auth/eapol_auth_sm.h"
245b9c547cSRui Paulo #include "eapol_auth/eapol_auth_sm_i.h"
25325151a3SRui Paulo #include "fst/fst.h"
26e28a4053SRui Paulo #include "hostapd.h"
27e28a4053SRui Paulo #include "authsrv.h"
28e28a4053SRui Paulo #include "sta_info.h"
29e28a4053SRui Paulo #include "accounting.h"
30e28a4053SRui Paulo #include "ap_list.h"
31e28a4053SRui Paulo #include "beacon.h"
32e28a4053SRui Paulo #include "ieee802_1x.h"
33e28a4053SRui Paulo #include "ieee802_11_auth.h"
34e28a4053SRui Paulo #include "vlan_init.h"
35e28a4053SRui Paulo #include "wpa_auth.h"
36e28a4053SRui Paulo #include "wps_hostapd.h"
3785732ac8SCy Schubert #include "dpp_hostapd.h"
3885732ac8SCy Schubert #include "gas_query_ap.h"
39e28a4053SRui Paulo #include "hw_features.h"
40e28a4053SRui Paulo #include "wpa_auth_glue.h"
41e28a4053SRui Paulo #include "ap_drv_ops.h"
42e28a4053SRui Paulo #include "ap_config.h"
43f05cddf9SRui Paulo #include "p2p_hostapd.h"
44f05cddf9SRui Paulo #include "gas_serv.h"
455b9c547cSRui Paulo #include "dfs.h"
465b9c547cSRui Paulo #include "ieee802_11.h"
475b9c547cSRui Paulo #include "bss_load.h"
485b9c547cSRui Paulo #include "x_snoop.h"
495b9c547cSRui Paulo #include "dhcp_snoop.h"
505b9c547cSRui Paulo #include "ndisc_snoop.h"
51780fb4a2SCy Schubert #include "neighbor_db.h"
52780fb4a2SCy Schubert #include "rrm.h"
5385732ac8SCy Schubert #include "fils_hlp.h"
5485732ac8SCy Schubert #include "acs.h"
5585732ac8SCy Schubert #include "hs20.h"
56206b73d0SCy Schubert #include "airtime_policy.h"
57206b73d0SCy Schubert #include "wpa_auth_kay.h"
58e28a4053SRui Paulo 
59e28a4053SRui Paulo 
60f05cddf9SRui Paulo static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
61c1d255d3SCy Schubert #ifdef CONFIG_WEP
62e28a4053SRui Paulo static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
63f05cddf9SRui Paulo static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
64c1d255d3SCy Schubert #endif /* CONFIG_WEP */
655b9c547cSRui Paulo static int setup_interface2(struct hostapd_iface *iface);
665b9c547cSRui Paulo static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
6785732ac8SCy Schubert static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
6885732ac8SCy Schubert 						    void *timeout_ctx);
69e28a4053SRui Paulo 
70e28a4053SRui Paulo 
hostapd_for_each_interface(struct hapd_interfaces * interfaces,int (* cb)(struct hostapd_iface * iface,void * ctx),void * ctx)71f05cddf9SRui Paulo int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
72f05cddf9SRui Paulo 			       int (*cb)(struct hostapd_iface *iface,
73f05cddf9SRui Paulo 					 void *ctx), void *ctx)
74e28a4053SRui Paulo {
75f05cddf9SRui Paulo 	size_t i;
76f05cddf9SRui Paulo 	int ret;
77e28a4053SRui Paulo 
78f05cddf9SRui Paulo 	for (i = 0; i < interfaces->count; i++) {
79c1d255d3SCy Schubert 		if (!interfaces->iface[i])
80c1d255d3SCy Schubert 			continue;
81f05cddf9SRui Paulo 		ret = cb(interfaces->iface[i], ctx);
82f05cddf9SRui Paulo 		if (ret)
83f05cddf9SRui Paulo 			return ret;
84f05cddf9SRui Paulo 	}
85e28a4053SRui Paulo 
86f05cddf9SRui Paulo 	return 0;
87f05cddf9SRui Paulo }
88e28a4053SRui Paulo 
89f05cddf9SRui Paulo 
hostapd_reconfig_encryption(struct hostapd_data * hapd)9085732ac8SCy Schubert void hostapd_reconfig_encryption(struct hostapd_data *hapd)
9185732ac8SCy Schubert {
9285732ac8SCy Schubert 	if (hapd->wpa_auth)
9385732ac8SCy Schubert 		return;
9485732ac8SCy Schubert 
9585732ac8SCy Schubert 	hostapd_set_privacy(hapd, 0);
96c1d255d3SCy Schubert #ifdef CONFIG_WEP
9785732ac8SCy Schubert 	hostapd_setup_encryption(hapd->conf->iface, hapd);
98c1d255d3SCy Schubert #endif /* CONFIG_WEP */
9985732ac8SCy Schubert }
10085732ac8SCy Schubert 
10185732ac8SCy Schubert 
hostapd_reload_bss(struct hostapd_data * hapd)102f05cddf9SRui Paulo static void hostapd_reload_bss(struct hostapd_data *hapd)
103f05cddf9SRui Paulo {
1045b9c547cSRui Paulo 	struct hostapd_ssid *ssid;
1055b9c547cSRui Paulo 
10685732ac8SCy Schubert 	if (!hapd->started)
10785732ac8SCy Schubert 		return;
10885732ac8SCy Schubert 
10985732ac8SCy Schubert 	if (hapd->conf->wmm_enabled < 0)
110c1d255d3SCy Schubert 		hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
111c1d255d3SCy Schubert 			hapd->iconf->ieee80211ax;
11285732ac8SCy Schubert 
113e28a4053SRui Paulo #ifndef CONFIG_NO_RADIUS
114f05cddf9SRui Paulo 	radius_client_reconfig(hapd->radius, hapd->conf->radius);
115e28a4053SRui Paulo #endif /* CONFIG_NO_RADIUS */
116e28a4053SRui Paulo 
1175b9c547cSRui Paulo 	ssid = &hapd->conf->ssid;
1185b9c547cSRui Paulo 	if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
1195b9c547cSRui Paulo 	    ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
1205b9c547cSRui Paulo 		/*
1215b9c547cSRui Paulo 		 * Force PSK to be derived again since SSID or passphrase may
1225b9c547cSRui Paulo 		 * have changed.
1235b9c547cSRui Paulo 		 */
1245b9c547cSRui Paulo 		hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
1255b9c547cSRui Paulo 	}
126e28a4053SRui Paulo 	if (hostapd_setup_wpa_psk(hapd->conf)) {
127e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
128e28a4053SRui Paulo 			   "after reloading configuration");
129e28a4053SRui Paulo 	}
130e28a4053SRui Paulo 
131e28a4053SRui Paulo 	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
132f05cddf9SRui Paulo 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
133e28a4053SRui Paulo 	else
134f05cddf9SRui Paulo 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
135e28a4053SRui Paulo 
1365b9c547cSRui Paulo 	if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
137e28a4053SRui Paulo 		hostapd_setup_wpa(hapd);
138f05cddf9SRui Paulo 		if (hapd->wpa_auth)
139f05cddf9SRui Paulo 			wpa_init_keys(hapd->wpa_auth);
140f05cddf9SRui Paulo 	} else if (hapd->conf->wpa) {
141e28a4053SRui Paulo 		const u8 *wpa_ie;
142e28a4053SRui Paulo 		size_t wpa_ie_len;
143e28a4053SRui Paulo 		hostapd_reconfig_wpa(hapd);
144e28a4053SRui Paulo 		wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
145e28a4053SRui Paulo 		if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
146e28a4053SRui Paulo 			wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
147e28a4053SRui Paulo 				   "the kernel driver.");
148e28a4053SRui Paulo 	} else if (hapd->wpa_auth) {
149e28a4053SRui Paulo 		wpa_deinit(hapd->wpa_auth);
150e28a4053SRui Paulo 		hapd->wpa_auth = NULL;
151e28a4053SRui Paulo 		hostapd_set_privacy(hapd, 0);
152c1d255d3SCy Schubert #ifdef CONFIG_WEP
153e28a4053SRui Paulo 		hostapd_setup_encryption(hapd->conf->iface, hapd);
154c1d255d3SCy Schubert #endif /* CONFIG_WEP */
155e28a4053SRui Paulo 		hostapd_set_generic_elem(hapd, (u8 *) "", 0);
156e28a4053SRui Paulo 	}
157e28a4053SRui Paulo 
158e28a4053SRui Paulo 	ieee802_11_set_beacon(hapd);
159e28a4053SRui Paulo 	hostapd_update_wps(hapd);
160e28a4053SRui Paulo 
161e28a4053SRui Paulo 	if (hapd->conf->ssid.ssid_set &&
162f05cddf9SRui Paulo 	    hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
163e28a4053SRui Paulo 			     hapd->conf->ssid.ssid_len)) {
164e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
165e28a4053SRui Paulo 		/* try to continue */
166e28a4053SRui Paulo 	}
167f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
168f05cddf9SRui Paulo }
169f05cddf9SRui Paulo 
170f05cddf9SRui Paulo 
hostapd_clear_old(struct hostapd_iface * iface)1715b9c547cSRui Paulo static void hostapd_clear_old(struct hostapd_iface *iface)
172f05cddf9SRui Paulo {
173f05cddf9SRui Paulo 	size_t j;
174f05cddf9SRui Paulo 
175f05cddf9SRui Paulo 	/*
176f05cddf9SRui Paulo 	 * Deauthenticate all stations since the new configuration may not
177f05cddf9SRui Paulo 	 * allow them to use the BSS anymore.
178f05cddf9SRui Paulo 	 */
179f05cddf9SRui Paulo 	for (j = 0; j < iface->num_bss; j++) {
180f05cddf9SRui Paulo 		hostapd_flush_old_stations(iface->bss[j],
181f05cddf9SRui Paulo 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
182c1d255d3SCy Schubert #ifdef CONFIG_WEP
183f05cddf9SRui Paulo 		hostapd_broadcast_wep_clear(iface->bss[j]);
184c1d255d3SCy Schubert #endif /* CONFIG_WEP */
185f05cddf9SRui Paulo 
186f05cddf9SRui Paulo #ifndef CONFIG_NO_RADIUS
187f05cddf9SRui Paulo 		/* TODO: update dynamic data based on changed configuration
188f05cddf9SRui Paulo 		 * items (e.g., open/close sockets, etc.) */
189f05cddf9SRui Paulo 		radius_client_flush(iface->bss[j]->radius, 0);
190f05cddf9SRui Paulo #endif /* CONFIG_NO_RADIUS */
191f05cddf9SRui Paulo 	}
1925b9c547cSRui Paulo }
1935b9c547cSRui Paulo 
1945b9c547cSRui Paulo 
hostapd_iface_conf_changed(struct hostapd_config * newconf,struct hostapd_config * oldconf)19585732ac8SCy Schubert static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
19685732ac8SCy Schubert 				      struct hostapd_config *oldconf)
19785732ac8SCy Schubert {
19885732ac8SCy Schubert 	size_t i;
19985732ac8SCy Schubert 
20085732ac8SCy Schubert 	if (newconf->num_bss != oldconf->num_bss)
20185732ac8SCy Schubert 		return 1;
20285732ac8SCy Schubert 
20385732ac8SCy Schubert 	for (i = 0; i < newconf->num_bss; i++) {
20485732ac8SCy Schubert 		if (os_strcmp(newconf->bss[i]->iface,
20585732ac8SCy Schubert 			      oldconf->bss[i]->iface) != 0)
20685732ac8SCy Schubert 			return 1;
20785732ac8SCy Schubert 	}
20885732ac8SCy Schubert 
20985732ac8SCy Schubert 	return 0;
21085732ac8SCy Schubert }
21185732ac8SCy Schubert 
21285732ac8SCy Schubert 
hostapd_reload_config(struct hostapd_iface * iface)2135b9c547cSRui Paulo int hostapd_reload_config(struct hostapd_iface *iface)
2145b9c547cSRui Paulo {
21585732ac8SCy Schubert 	struct hapd_interfaces *interfaces = iface->interfaces;
2165b9c547cSRui Paulo 	struct hostapd_data *hapd = iface->bss[0];
2175b9c547cSRui Paulo 	struct hostapd_config *newconf, *oldconf;
2185b9c547cSRui Paulo 	size_t j;
2195b9c547cSRui Paulo 
2205b9c547cSRui Paulo 	if (iface->config_fname == NULL) {
2215b9c547cSRui Paulo 		/* Only in-memory config in use - assume it has been updated */
2225b9c547cSRui Paulo 		hostapd_clear_old(iface);
2235b9c547cSRui Paulo 		for (j = 0; j < iface->num_bss; j++)
2245b9c547cSRui Paulo 			hostapd_reload_bss(iface->bss[j]);
2255b9c547cSRui Paulo 		return 0;
2265b9c547cSRui Paulo 	}
2275b9c547cSRui Paulo 
2285b9c547cSRui Paulo 	if (iface->interfaces == NULL ||
2295b9c547cSRui Paulo 	    iface->interfaces->config_read_cb == NULL)
2305b9c547cSRui Paulo 		return -1;
2315b9c547cSRui Paulo 	newconf = iface->interfaces->config_read_cb(iface->config_fname);
2325b9c547cSRui Paulo 	if (newconf == NULL)
2335b9c547cSRui Paulo 		return -1;
2345b9c547cSRui Paulo 
2355b9c547cSRui Paulo 	hostapd_clear_old(iface);
236f05cddf9SRui Paulo 
237f05cddf9SRui Paulo 	oldconf = hapd->iconf;
23885732ac8SCy Schubert 	if (hostapd_iface_conf_changed(newconf, oldconf)) {
23985732ac8SCy Schubert 		char *fname;
24085732ac8SCy Schubert 		int res;
24185732ac8SCy Schubert 
24285732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
24385732ac8SCy Schubert 			   "Configuration changes include interface/BSS modification - force full disable+enable sequence");
24485732ac8SCy Schubert 		fname = os_strdup(iface->config_fname);
24585732ac8SCy Schubert 		if (!fname) {
24685732ac8SCy Schubert 			hostapd_config_free(newconf);
24785732ac8SCy Schubert 			return -1;
24885732ac8SCy Schubert 		}
24985732ac8SCy Schubert 		hostapd_remove_iface(interfaces, hapd->conf->iface);
25085732ac8SCy Schubert 		iface = hostapd_init(interfaces, fname);
25185732ac8SCy Schubert 		os_free(fname);
25285732ac8SCy Schubert 		hostapd_config_free(newconf);
25385732ac8SCy Schubert 		if (!iface) {
25485732ac8SCy Schubert 			wpa_printf(MSG_ERROR,
25585732ac8SCy Schubert 				   "Failed to initialize interface on config reload");
25685732ac8SCy Schubert 			return -1;
25785732ac8SCy Schubert 		}
25885732ac8SCy Schubert 		iface->interfaces = interfaces;
25985732ac8SCy Schubert 		interfaces->iface[interfaces->count] = iface;
26085732ac8SCy Schubert 		interfaces->count++;
26185732ac8SCy Schubert 		res = hostapd_enable_iface(iface);
26285732ac8SCy Schubert 		if (res < 0)
26385732ac8SCy Schubert 			wpa_printf(MSG_ERROR,
26485732ac8SCy Schubert 				   "Failed to enable interface on config reload");
26585732ac8SCy Schubert 		return res;
26685732ac8SCy Schubert 	}
267f05cddf9SRui Paulo 	iface->conf = newconf;
268f05cddf9SRui Paulo 
269f05cddf9SRui Paulo 	for (j = 0; j < iface->num_bss; j++) {
270f05cddf9SRui Paulo 		hapd = iface->bss[j];
271f05cddf9SRui Paulo 		hapd->iconf = newconf;
2725b9c547cSRui Paulo 		hapd->iconf->channel = oldconf->channel;
273325151a3SRui Paulo 		hapd->iconf->acs = oldconf->acs;
2745b9c547cSRui Paulo 		hapd->iconf->secondary_channel = oldconf->secondary_channel;
2755b9c547cSRui Paulo 		hapd->iconf->ieee80211n = oldconf->ieee80211n;
2765b9c547cSRui Paulo 		hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
2775b9c547cSRui Paulo 		hapd->iconf->ht_capab = oldconf->ht_capab;
2785b9c547cSRui Paulo 		hapd->iconf->vht_capab = oldconf->vht_capab;
279206b73d0SCy Schubert 		hostapd_set_oper_chwidth(hapd->iconf,
280206b73d0SCy Schubert 					 hostapd_get_oper_chwidth(oldconf));
281206b73d0SCy Schubert 		hostapd_set_oper_centr_freq_seg0_idx(
282206b73d0SCy Schubert 			hapd->iconf,
283206b73d0SCy Schubert 			hostapd_get_oper_centr_freq_seg0_idx(oldconf));
284206b73d0SCy Schubert 		hostapd_set_oper_centr_freq_seg1_idx(
285206b73d0SCy Schubert 			hapd->iconf,
286206b73d0SCy Schubert 			hostapd_get_oper_centr_freq_seg1_idx(oldconf));
2875b9c547cSRui Paulo 		hapd->conf = newconf->bss[j];
288f05cddf9SRui Paulo 		hostapd_reload_bss(hapd);
289f05cddf9SRui Paulo 	}
290e28a4053SRui Paulo 
291e28a4053SRui Paulo 	hostapd_config_free(oldconf);
292e28a4053SRui Paulo 
293e28a4053SRui Paulo 
294e28a4053SRui Paulo 	return 0;
295e28a4053SRui Paulo }
296e28a4053SRui Paulo 
297e28a4053SRui Paulo 
298c1d255d3SCy Schubert #ifdef CONFIG_WEP
299c1d255d3SCy Schubert 
hostapd_broadcast_key_clear_iface(struct hostapd_data * hapd,const char * ifname)300e28a4053SRui Paulo static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
301780fb4a2SCy Schubert 					      const char *ifname)
302e28a4053SRui Paulo {
303e28a4053SRui Paulo 	int i;
304e28a4053SRui Paulo 
30585732ac8SCy Schubert 	if (!ifname || !hapd->drv_priv)
306780fb4a2SCy Schubert 		return;
307e28a4053SRui Paulo 	for (i = 0; i < NUM_WEP_KEYS; i++) {
308c1d255d3SCy Schubert 		if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
309c1d255d3SCy Schubert 					0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
310e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "Failed to clear default "
311e28a4053SRui Paulo 				   "encryption keys (ifname=%s keyidx=%d)",
312e28a4053SRui Paulo 				   ifname, i);
313e28a4053SRui Paulo 		}
314e28a4053SRui Paulo 	}
315e28a4053SRui Paulo 	if (hapd->conf->ieee80211w) {
316e28a4053SRui Paulo 		for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
317f05cddf9SRui Paulo 			if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
318c1d255d3SCy Schubert 						NULL, i, 0, 0, NULL,
319c1d255d3SCy Schubert 						0, NULL, 0, KEY_FLAG_GROUP)) {
320e28a4053SRui Paulo 				wpa_printf(MSG_DEBUG, "Failed to clear "
321e28a4053SRui Paulo 					   "default mgmt encryption keys "
322e28a4053SRui Paulo 					   "(ifname=%s keyidx=%d)", ifname, i);
323e28a4053SRui Paulo 			}
324e28a4053SRui Paulo 		}
325e28a4053SRui Paulo 	}
326e28a4053SRui Paulo }
327e28a4053SRui Paulo 
328e28a4053SRui Paulo 
hostapd_broadcast_wep_clear(struct hostapd_data * hapd)329e28a4053SRui Paulo static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
330e28a4053SRui Paulo {
331e28a4053SRui Paulo 	hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
332e28a4053SRui Paulo 	return 0;
333e28a4053SRui Paulo }
334e28a4053SRui Paulo 
335e28a4053SRui Paulo 
hostapd_broadcast_wep_set(struct hostapd_data * hapd)336e28a4053SRui Paulo static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
337e28a4053SRui Paulo {
338e28a4053SRui Paulo 	int errors = 0, idx;
339e28a4053SRui Paulo 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
340e28a4053SRui Paulo 
341e28a4053SRui Paulo 	idx = ssid->wep.idx;
342c1d255d3SCy Schubert 	if (ssid->wep.default_len && ssid->wep.key[idx] &&
343f05cddf9SRui Paulo 	    hostapd_drv_set_key(hapd->conf->iface,
344c1d255d3SCy Schubert 				hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
345f05cddf9SRui Paulo 				1, NULL, 0, ssid->wep.key[idx],
346c1d255d3SCy Schubert 				ssid->wep.len[idx],
347c1d255d3SCy Schubert 				KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
348e28a4053SRui Paulo 		wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
349e28a4053SRui Paulo 		errors++;
350e28a4053SRui Paulo 	}
351e28a4053SRui Paulo 
352e28a4053SRui Paulo 	return errors;
353e28a4053SRui Paulo }
354e28a4053SRui Paulo 
355c1d255d3SCy Schubert #endif /* CONFIG_WEP */
356e28a4053SRui Paulo 
357c1d255d3SCy Schubert 
hostapd_free_hapd_data(struct hostapd_data * hapd)358c1d255d3SCy Schubert void hostapd_free_hapd_data(struct hostapd_data *hapd)
359f05cddf9SRui Paulo {
3605b9c547cSRui Paulo 	os_free(hapd->probereq_cb);
3615b9c547cSRui Paulo 	hapd->probereq_cb = NULL;
362325151a3SRui Paulo 	hapd->num_probereq_cb = 0;
3635b9c547cSRui Paulo 
3645b9c547cSRui Paulo #ifdef CONFIG_P2P
3655b9c547cSRui Paulo 	wpabuf_free(hapd->p2p_beacon_ie);
3665b9c547cSRui Paulo 	hapd->p2p_beacon_ie = NULL;
3675b9c547cSRui Paulo 	wpabuf_free(hapd->p2p_probe_resp_ie);
3685b9c547cSRui Paulo 	hapd->p2p_probe_resp_ie = NULL;
3695b9c547cSRui Paulo #endif /* CONFIG_P2P */
3705b9c547cSRui Paulo 
3715b9c547cSRui Paulo 	if (!hapd->started) {
3725b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
3734bc52338SCy Schubert 			   __func__, hapd->conf ? hapd->conf->iface : "N/A");
3745b9c547cSRui Paulo 		return;
3755b9c547cSRui Paulo 	}
3765b9c547cSRui Paulo 	hapd->started = 0;
3774bc52338SCy Schubert 	hapd->beacon_set_done = 0;
3785b9c547cSRui Paulo 
3795b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
380e28a4053SRui Paulo 	accounting_deinit(hapd);
381e28a4053SRui Paulo 	hostapd_deinit_wpa(hapd);
382e28a4053SRui Paulo 	vlan_deinit(hapd);
383e28a4053SRui Paulo 	hostapd_acl_deinit(hapd);
384e28a4053SRui Paulo #ifndef CONFIG_NO_RADIUS
385e28a4053SRui Paulo 	radius_client_deinit(hapd->radius);
386e28a4053SRui Paulo 	hapd->radius = NULL;
387f05cddf9SRui Paulo 	radius_das_deinit(hapd->radius_das);
388f05cddf9SRui Paulo 	hapd->radius_das = NULL;
389e28a4053SRui Paulo #endif /* CONFIG_NO_RADIUS */
390e28a4053SRui Paulo 
391e28a4053SRui Paulo 	hostapd_deinit_wps(hapd);
392206b73d0SCy Schubert 	ieee802_1x_dealloc_kay_sm_hapd(hapd);
39385732ac8SCy Schubert #ifdef CONFIG_DPP
39485732ac8SCy Schubert 	hostapd_dpp_deinit(hapd);
39585732ac8SCy Schubert 	gas_query_ap_deinit(hapd->gas);
396c1d255d3SCy Schubert 	hapd->gas = NULL;
39785732ac8SCy Schubert #endif /* CONFIG_DPP */
398e28a4053SRui Paulo 
399e28a4053SRui Paulo 	authsrv_deinit(hapd);
400e28a4053SRui Paulo 
4015b9c547cSRui Paulo 	if (hapd->interface_added) {
4025b9c547cSRui Paulo 		hapd->interface_added = 0;
4035b9c547cSRui Paulo 		if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
4045b9c547cSRui Paulo 			wpa_printf(MSG_WARNING,
4055b9c547cSRui Paulo 				   "Failed to remove BSS interface %s",
406e28a4053SRui Paulo 				   hapd->conf->iface);
4075b9c547cSRui Paulo 			hapd->interface_added = 1;
4085b9c547cSRui Paulo 		} else {
4095b9c547cSRui Paulo 			/*
4105b9c547cSRui Paulo 			 * Since this was a dynamically added interface, the
4115b9c547cSRui Paulo 			 * driver wrapper may have removed its internal instance
4125b9c547cSRui Paulo 			 * and hapd->drv_priv is not valid anymore.
4135b9c547cSRui Paulo 			 */
4145b9c547cSRui Paulo 			hapd->drv_priv = NULL;
415e28a4053SRui Paulo 		}
4165b9c547cSRui Paulo 	}
417f05cddf9SRui Paulo 
418f05cddf9SRui Paulo 	wpabuf_free(hapd->time_adv);
419c1d255d3SCy Schubert 	hapd->time_adv = NULL;
420f05cddf9SRui Paulo 
421f05cddf9SRui Paulo #ifdef CONFIG_INTERWORKING
422f05cddf9SRui Paulo 	gas_serv_deinit(hapd);
423f05cddf9SRui Paulo #endif /* CONFIG_INTERWORKING */
424f05cddf9SRui Paulo 
4255b9c547cSRui Paulo 	bss_load_update_deinit(hapd);
4265b9c547cSRui Paulo 	ndisc_snoop_deinit(hapd);
4275b9c547cSRui Paulo 	dhcp_snoop_deinit(hapd);
4285b9c547cSRui Paulo 	x_snoop_deinit(hapd);
4295b9c547cSRui Paulo 
430f05cddf9SRui Paulo #ifdef CONFIG_SQLITE
4315b9c547cSRui Paulo 	bin_clear_free(hapd->tmp_eap_user.identity,
4325b9c547cSRui Paulo 		       hapd->tmp_eap_user.identity_len);
4335b9c547cSRui Paulo 	bin_clear_free(hapd->tmp_eap_user.password,
4345b9c547cSRui Paulo 		       hapd->tmp_eap_user.password_len);
435c1d255d3SCy Schubert 	os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
436f05cddf9SRui Paulo #endif /* CONFIG_SQLITE */
4375b9c547cSRui Paulo 
4385b9c547cSRui Paulo #ifdef CONFIG_MESH
4395b9c547cSRui Paulo 	wpabuf_free(hapd->mesh_pending_auth);
4405b9c547cSRui Paulo 	hapd->mesh_pending_auth = NULL;
441c1d255d3SCy Schubert 	/* handling setup failure is already done */
442c1d255d3SCy Schubert 	hapd->setup_complete_cb = NULL;
4435b9c547cSRui Paulo #endif /* CONFIG_MESH */
444780fb4a2SCy Schubert 
445780fb4a2SCy Schubert 	hostapd_clean_rrm(hapd);
44685732ac8SCy Schubert 	fils_hlp_deinit(hapd);
4474bc52338SCy Schubert 
448c1d255d3SCy Schubert #ifdef CONFIG_OCV
449c1d255d3SCy Schubert 	eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
450c1d255d3SCy Schubert #endif /* CONFIG_OCV */
451c1d255d3SCy Schubert 
4524bc52338SCy Schubert #ifdef CONFIG_SAE
4534bc52338SCy Schubert 	{
4544bc52338SCy Schubert 		struct hostapd_sae_commit_queue *q;
4554bc52338SCy Schubert 
4564bc52338SCy Schubert 		while ((q = dl_list_first(&hapd->sae_commit_queue,
4574bc52338SCy Schubert 					  struct hostapd_sae_commit_queue,
4584bc52338SCy Schubert 					  list))) {
4594bc52338SCy Schubert 			dl_list_del(&q->list);
4604bc52338SCy Schubert 			os_free(q);
4614bc52338SCy Schubert 		}
4624bc52338SCy Schubert 	}
4634bc52338SCy Schubert 	eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
4644bc52338SCy Schubert #endif /* CONFIG_SAE */
465f05cddf9SRui Paulo }
466f05cddf9SRui Paulo 
467f05cddf9SRui Paulo 
468f05cddf9SRui Paulo /**
469f05cddf9SRui Paulo  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
470f05cddf9SRui Paulo  * @hapd: Pointer to BSS data
471f05cddf9SRui Paulo  *
472f05cddf9SRui Paulo  * This function is used to free all per-BSS data structures and resources.
4735b9c547cSRui Paulo  * Most of the modules that are initialized in hostapd_setup_bss() are
4745b9c547cSRui Paulo  * deinitialized here.
475f05cddf9SRui Paulo  */
hostapd_cleanup(struct hostapd_data * hapd)476f05cddf9SRui Paulo static void hostapd_cleanup(struct hostapd_data *hapd)
477f05cddf9SRui Paulo {
4785b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
4794bc52338SCy Schubert 		   hapd->conf ? hapd->conf->iface : "N/A");
480f05cddf9SRui Paulo 	if (hapd->iface->interfaces &&
48185732ac8SCy Schubert 	    hapd->iface->interfaces->ctrl_iface_deinit) {
48285732ac8SCy Schubert 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
483f05cddf9SRui Paulo 		hapd->iface->interfaces->ctrl_iface_deinit(hapd);
48485732ac8SCy Schubert 	}
485f05cddf9SRui Paulo 	hostapd_free_hapd_data(hapd);
486e28a4053SRui Paulo }
487e28a4053SRui Paulo 
488e28a4053SRui Paulo 
sta_track_deinit(struct hostapd_iface * iface)489325151a3SRui Paulo static void sta_track_deinit(struct hostapd_iface *iface)
490325151a3SRui Paulo {
491325151a3SRui Paulo 	struct hostapd_sta_info *info;
492325151a3SRui Paulo 
493325151a3SRui Paulo 	if (!iface->num_sta_seen)
494325151a3SRui Paulo 		return;
495325151a3SRui Paulo 
496325151a3SRui Paulo 	while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
497325151a3SRui Paulo 				     list))) {
498325151a3SRui Paulo 		dl_list_del(&info->list);
499325151a3SRui Paulo 		iface->num_sta_seen--;
500780fb4a2SCy Schubert 		sta_track_del(info);
501325151a3SRui Paulo 	}
502325151a3SRui Paulo }
503325151a3SRui Paulo 
504325151a3SRui Paulo 
hostapd_cleanup_iface_partial(struct hostapd_iface * iface)505c1d255d3SCy Schubert void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
506f05cddf9SRui Paulo {
5075b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
5085b9c547cSRui Paulo #ifdef NEED_AP_MLME
5095b9c547cSRui Paulo 	hostapd_stop_setup_timers(iface);
5105b9c547cSRui Paulo #endif /* NEED_AP_MLME */
51185732ac8SCy Schubert 	if (iface->current_mode)
51285732ac8SCy Schubert 		acs_cleanup(iface);
513f05cddf9SRui Paulo 	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
514f05cddf9SRui Paulo 	iface->hw_features = NULL;
51585732ac8SCy Schubert 	iface->current_mode = NULL;
516f05cddf9SRui Paulo 	os_free(iface->current_rates);
517f05cddf9SRui Paulo 	iface->current_rates = NULL;
518f05cddf9SRui Paulo 	os_free(iface->basic_rates);
519f05cddf9SRui Paulo 	iface->basic_rates = NULL;
520f05cddf9SRui Paulo 	ap_list_deinit(iface);
521325151a3SRui Paulo 	sta_track_deinit(iface);
522206b73d0SCy Schubert 	airtime_policy_update_deinit(iface);
523f05cddf9SRui Paulo }
524f05cddf9SRui Paulo 
525f05cddf9SRui Paulo 
526e28a4053SRui Paulo /**
527e28a4053SRui Paulo  * hostapd_cleanup_iface - Complete per-interface cleanup
528e28a4053SRui Paulo  * @iface: Pointer to interface data
529e28a4053SRui Paulo  *
530e28a4053SRui Paulo  * This function is called after per-BSS data structures are deinitialized
531e28a4053SRui Paulo  * with hostapd_cleanup().
532e28a4053SRui Paulo  */
hostapd_cleanup_iface(struct hostapd_iface * iface)533e28a4053SRui Paulo static void hostapd_cleanup_iface(struct hostapd_iface *iface)
534e28a4053SRui Paulo {
5355b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
5365b9c547cSRui Paulo 	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
53785732ac8SCy Schubert 	eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
53885732ac8SCy Schubert 			     NULL);
5395b9c547cSRui Paulo 
540f05cddf9SRui Paulo 	hostapd_cleanup_iface_partial(iface);
541e28a4053SRui Paulo 	hostapd_config_free(iface->conf);
542e28a4053SRui Paulo 	iface->conf = NULL;
543e28a4053SRui Paulo 
544e28a4053SRui Paulo 	os_free(iface->config_fname);
545e28a4053SRui Paulo 	os_free(iface->bss);
5465b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
547e28a4053SRui Paulo 	os_free(iface);
548e28a4053SRui Paulo }
549e28a4053SRui Paulo 
550e28a4053SRui Paulo 
551c1d255d3SCy Schubert #ifdef CONFIG_WEP
552c1d255d3SCy Schubert 
hostapd_clear_wep(struct hostapd_data * hapd)553f05cddf9SRui Paulo static void hostapd_clear_wep(struct hostapd_data *hapd)
554f05cddf9SRui Paulo {
5554bc52338SCy Schubert 	if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
556f05cddf9SRui Paulo 		hostapd_set_privacy(hapd, 0);
557f05cddf9SRui Paulo 		hostapd_broadcast_wep_clear(hapd);
558f05cddf9SRui Paulo 	}
559f05cddf9SRui Paulo }
560f05cddf9SRui Paulo 
561f05cddf9SRui Paulo 
hostapd_setup_encryption(char * iface,struct hostapd_data * hapd)562e28a4053SRui Paulo static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
563e28a4053SRui Paulo {
564e28a4053SRui Paulo 	int i;
565e28a4053SRui Paulo 
566e28a4053SRui Paulo 	hostapd_broadcast_wep_set(hapd);
567e28a4053SRui Paulo 
568e28a4053SRui Paulo 	if (hapd->conf->ssid.wep.default_len) {
569e28a4053SRui Paulo 		hostapd_set_privacy(hapd, 1);
570e28a4053SRui Paulo 		return 0;
571e28a4053SRui Paulo 	}
572e28a4053SRui Paulo 
573f05cddf9SRui Paulo 	/*
574f05cddf9SRui Paulo 	 * When IEEE 802.1X is not enabled, the driver may need to know how to
575f05cddf9SRui Paulo 	 * set authentication algorithms for static WEP.
576f05cddf9SRui Paulo 	 */
577f05cddf9SRui Paulo 	hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
578f05cddf9SRui Paulo 
579e28a4053SRui Paulo 	for (i = 0; i < 4; i++) {
580e28a4053SRui Paulo 		if (hapd->conf->ssid.wep.key[i] &&
581c1d255d3SCy Schubert 		    hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
582e28a4053SRui Paulo 					i == hapd->conf->ssid.wep.idx, NULL, 0,
583e28a4053SRui Paulo 					hapd->conf->ssid.wep.key[i],
584c1d255d3SCy Schubert 					hapd->conf->ssid.wep.len[i],
585c1d255d3SCy Schubert 					i == hapd->conf->ssid.wep.idx ?
586c1d255d3SCy Schubert 					KEY_FLAG_GROUP_RX_TX_DEFAULT :
587c1d255d3SCy Schubert 					KEY_FLAG_GROUP_RX_TX)) {
588e28a4053SRui Paulo 			wpa_printf(MSG_WARNING, "Could not set WEP "
589e28a4053SRui Paulo 				   "encryption.");
590e28a4053SRui Paulo 			return -1;
591e28a4053SRui Paulo 		}
592e28a4053SRui Paulo 		if (hapd->conf->ssid.wep.key[i] &&
593e28a4053SRui Paulo 		    i == hapd->conf->ssid.wep.idx)
594e28a4053SRui Paulo 			hostapd_set_privacy(hapd, 1);
595e28a4053SRui Paulo 	}
596e28a4053SRui Paulo 
597e28a4053SRui Paulo 	return 0;
598e28a4053SRui Paulo }
599e28a4053SRui Paulo 
600c1d255d3SCy Schubert #endif /* CONFIG_WEP */
601c1d255d3SCy Schubert 
602e28a4053SRui Paulo 
hostapd_flush_old_stations(struct hostapd_data * hapd,u16 reason)603f05cddf9SRui Paulo static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
604e28a4053SRui Paulo {
605e28a4053SRui Paulo 	int ret = 0;
606f05cddf9SRui Paulo 	u8 addr[ETH_ALEN];
607e28a4053SRui Paulo 
608e28a4053SRui Paulo 	if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
609e28a4053SRui Paulo 		return 0;
610e28a4053SRui Paulo 
6115b9c547cSRui Paulo 	if (!hapd->iface->driver_ap_teardown) {
6125b9c547cSRui Paulo 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
6135b9c547cSRui Paulo 			"Flushing old station entries");
6145b9c547cSRui Paulo 
615e28a4053SRui Paulo 		if (hostapd_flush(hapd)) {
6165b9c547cSRui Paulo 			wpa_msg(hapd->msg_ctx, MSG_WARNING,
6175b9c547cSRui Paulo 				"Could not connect to kernel driver");
618e28a4053SRui Paulo 			ret = -1;
619e28a4053SRui Paulo 		}
6205b9c547cSRui Paulo 	}
62185732ac8SCy Schubert 	if (hapd->conf && hapd->conf->broadcast_deauth) {
62285732ac8SCy Schubert 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
62385732ac8SCy Schubert 			"Deauthenticate all stations");
624e28a4053SRui Paulo 		os_memset(addr, 0xff, ETH_ALEN);
625f05cddf9SRui Paulo 		hostapd_drv_sta_deauth(hapd, addr, reason);
62685732ac8SCy Schubert 	}
627f05cddf9SRui Paulo 	hostapd_free_stas(hapd);
628e28a4053SRui Paulo 
629e28a4053SRui Paulo 	return ret;
630e28a4053SRui Paulo }
631e28a4053SRui Paulo 
632e28a4053SRui Paulo 
hostapd_bss_deinit_no_free(struct hostapd_data * hapd)633c1d255d3SCy Schubert void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
6345b9c547cSRui Paulo {
6355b9c547cSRui Paulo 	hostapd_free_stas(hapd);
6365b9c547cSRui Paulo 	hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
637c1d255d3SCy Schubert #ifdef CONFIG_WEP
6385b9c547cSRui Paulo 	hostapd_clear_wep(hapd);
639c1d255d3SCy Schubert #endif /* CONFIG_WEP */
6405b9c547cSRui Paulo }
6415b9c547cSRui Paulo 
6425b9c547cSRui Paulo 
643e28a4053SRui Paulo /**
644e28a4053SRui Paulo  * hostapd_validate_bssid_configuration - Validate BSSID configuration
645e28a4053SRui Paulo  * @iface: Pointer to interface data
646e28a4053SRui Paulo  * Returns: 0 on success, -1 on failure
647e28a4053SRui Paulo  *
648e28a4053SRui Paulo  * This function is used to validate that the configured BSSIDs are valid.
649e28a4053SRui Paulo  */
hostapd_validate_bssid_configuration(struct hostapd_iface * iface)650e28a4053SRui Paulo static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
651e28a4053SRui Paulo {
652e28a4053SRui Paulo 	u8 mask[ETH_ALEN] = { 0 };
653e28a4053SRui Paulo 	struct hostapd_data *hapd = iface->bss[0];
654e28a4053SRui Paulo 	unsigned int i = iface->conf->num_bss, bits = 0, j;
655e28a4053SRui Paulo 	int auto_addr = 0;
656e28a4053SRui Paulo 
657e28a4053SRui Paulo 	if (hostapd_drv_none(hapd))
658e28a4053SRui Paulo 		return 0;
659e28a4053SRui Paulo 
660780fb4a2SCy Schubert 	if (iface->conf->use_driver_iface_addr)
661780fb4a2SCy Schubert 		return 0;
662780fb4a2SCy Schubert 
663e28a4053SRui Paulo 	/* Generate BSSID mask that is large enough to cover the BSSIDs. */
664e28a4053SRui Paulo 
665e28a4053SRui Paulo 	/* Determine the bits necessary to cover the number of BSSIDs. */
666e28a4053SRui Paulo 	for (i--; i; i >>= 1)
667e28a4053SRui Paulo 		bits++;
668e28a4053SRui Paulo 
669e28a4053SRui Paulo 	/* Determine the bits necessary to any configured BSSIDs,
670e28a4053SRui Paulo 	   if they are higher than the number of BSSIDs. */
671e28a4053SRui Paulo 	for (j = 0; j < iface->conf->num_bss; j++) {
672780fb4a2SCy Schubert 		if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
673e28a4053SRui Paulo 			if (j)
674e28a4053SRui Paulo 				auto_addr++;
675e28a4053SRui Paulo 			continue;
676e28a4053SRui Paulo 		}
677e28a4053SRui Paulo 
678e28a4053SRui Paulo 		for (i = 0; i < ETH_ALEN; i++) {
679e28a4053SRui Paulo 			mask[i] |=
6805b9c547cSRui Paulo 				iface->conf->bss[j]->bssid[i] ^
681e28a4053SRui Paulo 				hapd->own_addr[i];
682e28a4053SRui Paulo 		}
683e28a4053SRui Paulo 	}
684e28a4053SRui Paulo 
685e28a4053SRui Paulo 	if (!auto_addr)
686e28a4053SRui Paulo 		goto skip_mask_ext;
687e28a4053SRui Paulo 
688e28a4053SRui Paulo 	for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
689e28a4053SRui Paulo 		;
690e28a4053SRui Paulo 	j = 0;
691e28a4053SRui Paulo 	if (i < ETH_ALEN) {
692e28a4053SRui Paulo 		j = (5 - i) * 8;
693e28a4053SRui Paulo 
694e28a4053SRui Paulo 		while (mask[i] != 0) {
695e28a4053SRui Paulo 			mask[i] >>= 1;
696e28a4053SRui Paulo 			j++;
697e28a4053SRui Paulo 		}
698e28a4053SRui Paulo 	}
699e28a4053SRui Paulo 
700e28a4053SRui Paulo 	if (bits < j)
701e28a4053SRui Paulo 		bits = j;
702e28a4053SRui Paulo 
703e28a4053SRui Paulo 	if (bits > 40) {
704e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
705e28a4053SRui Paulo 			   bits);
706e28a4053SRui Paulo 		return -1;
707e28a4053SRui Paulo 	}
708e28a4053SRui Paulo 
709e28a4053SRui Paulo 	os_memset(mask, 0xff, ETH_ALEN);
710e28a4053SRui Paulo 	j = bits / 8;
711e28a4053SRui Paulo 	for (i = 5; i > 5 - j; i--)
712e28a4053SRui Paulo 		mask[i] = 0;
713e28a4053SRui Paulo 	j = bits % 8;
7144bc52338SCy Schubert 	while (j) {
7154bc52338SCy Schubert 		j--;
716e28a4053SRui Paulo 		mask[i] <<= 1;
7174bc52338SCy Schubert 	}
718e28a4053SRui Paulo 
719e28a4053SRui Paulo skip_mask_ext:
720e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
721e28a4053SRui Paulo 		   (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
722e28a4053SRui Paulo 
723e28a4053SRui Paulo 	if (!auto_addr)
724e28a4053SRui Paulo 		return 0;
725e28a4053SRui Paulo 
726e28a4053SRui Paulo 	for (i = 0; i < ETH_ALEN; i++) {
727e28a4053SRui Paulo 		if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
728e28a4053SRui Paulo 			wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
729e28a4053SRui Paulo 				   " for start address " MACSTR ".",
730e28a4053SRui Paulo 				   MAC2STR(mask), MAC2STR(hapd->own_addr));
731e28a4053SRui Paulo 			wpa_printf(MSG_ERROR, "Start address must be the "
732e28a4053SRui Paulo 				   "first address in the block (i.e., addr "
733e28a4053SRui Paulo 				   "AND mask == addr).");
734e28a4053SRui Paulo 			return -1;
735e28a4053SRui Paulo 		}
736e28a4053SRui Paulo 	}
737e28a4053SRui Paulo 
738e28a4053SRui Paulo 	return 0;
739e28a4053SRui Paulo }
740e28a4053SRui Paulo 
741e28a4053SRui Paulo 
mac_in_conf(struct hostapd_config * conf,const void * a)742e28a4053SRui Paulo static int mac_in_conf(struct hostapd_config *conf, const void *a)
743e28a4053SRui Paulo {
744e28a4053SRui Paulo 	size_t i;
745e28a4053SRui Paulo 
746e28a4053SRui Paulo 	for (i = 0; i < conf->num_bss; i++) {
7475b9c547cSRui Paulo 		if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
748e28a4053SRui Paulo 			return 1;
749e28a4053SRui Paulo 		}
750e28a4053SRui Paulo 	}
751e28a4053SRui Paulo 
752e28a4053SRui Paulo 	return 0;
753e28a4053SRui Paulo }
754e28a4053SRui Paulo 
755e28a4053SRui Paulo 
756f05cddf9SRui Paulo #ifndef CONFIG_NO_RADIUS
757f05cddf9SRui Paulo 
hostapd_das_nas_mismatch(struct hostapd_data * hapd,struct radius_das_attrs * attr)758f05cddf9SRui Paulo static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
759f05cddf9SRui Paulo 				    struct radius_das_attrs *attr)
760f05cddf9SRui Paulo {
7615b9c547cSRui Paulo 	if (attr->nas_identifier &&
7625b9c547cSRui Paulo 	    (!hapd->conf->nas_identifier ||
7635b9c547cSRui Paulo 	     os_strlen(hapd->conf->nas_identifier) !=
7645b9c547cSRui Paulo 	     attr->nas_identifier_len ||
7655b9c547cSRui Paulo 	     os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
7665b9c547cSRui Paulo 		       attr->nas_identifier_len) != 0)) {
7675b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
7685b9c547cSRui Paulo 		return 1;
7695b9c547cSRui Paulo 	}
7705b9c547cSRui Paulo 
7715b9c547cSRui Paulo 	if (attr->nas_ip_addr &&
7725b9c547cSRui Paulo 	    (hapd->conf->own_ip_addr.af != AF_INET ||
7735b9c547cSRui Paulo 	     os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
7745b9c547cSRui Paulo 	     0)) {
7755b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
7765b9c547cSRui Paulo 		return 1;
7775b9c547cSRui Paulo 	}
7785b9c547cSRui Paulo 
7795b9c547cSRui Paulo #ifdef CONFIG_IPV6
7805b9c547cSRui Paulo 	if (attr->nas_ipv6_addr &&
7815b9c547cSRui Paulo 	    (hapd->conf->own_ip_addr.af != AF_INET6 ||
7825b9c547cSRui Paulo 	     os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
7835b9c547cSRui Paulo 	     != 0)) {
7845b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
7855b9c547cSRui Paulo 		return 1;
7865b9c547cSRui Paulo 	}
7875b9c547cSRui Paulo #endif /* CONFIG_IPV6 */
7885b9c547cSRui Paulo 
789f05cddf9SRui Paulo 	return 0;
790f05cddf9SRui Paulo }
791f05cddf9SRui Paulo 
792f05cddf9SRui Paulo 
hostapd_das_find_sta(struct hostapd_data * hapd,struct radius_das_attrs * attr,int * multi)793f05cddf9SRui Paulo static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
7945b9c547cSRui Paulo 					      struct radius_das_attrs *attr,
7955b9c547cSRui Paulo 					      int *multi)
796f05cddf9SRui Paulo {
7975b9c547cSRui Paulo 	struct sta_info *selected, *sta;
798f05cddf9SRui Paulo 	char buf[128];
7995b9c547cSRui Paulo 	int num_attr = 0;
8005b9c547cSRui Paulo 	int count;
801f05cddf9SRui Paulo 
8025b9c547cSRui Paulo 	*multi = 0;
8035b9c547cSRui Paulo 
8045b9c547cSRui Paulo 	for (sta = hapd->sta_list; sta; sta = sta->next)
8055b9c547cSRui Paulo 		sta->radius_das_match = 1;
8065b9c547cSRui Paulo 
8075b9c547cSRui Paulo 	if (attr->sta_addr) {
8085b9c547cSRui Paulo 		num_attr++;
809f05cddf9SRui Paulo 		sta = ap_get_sta(hapd, attr->sta_addr);
8105b9c547cSRui Paulo 		if (!sta) {
8115b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
8125b9c547cSRui Paulo 				   "RADIUS DAS: No Calling-Station-Id match");
8135b9c547cSRui Paulo 			return NULL;
8145b9c547cSRui Paulo 		}
815f05cddf9SRui Paulo 
8165b9c547cSRui Paulo 		selected = sta;
817f05cddf9SRui Paulo 		for (sta = hapd->sta_list; sta; sta = sta->next) {
8185b9c547cSRui Paulo 			if (sta != selected)
8195b9c547cSRui Paulo 				sta->radius_das_match = 0;
8205b9c547cSRui Paulo 		}
8215b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
8225b9c547cSRui Paulo 	}
8235b9c547cSRui Paulo 
8245b9c547cSRui Paulo 	if (attr->acct_session_id) {
8255b9c547cSRui Paulo 		num_attr++;
826780fb4a2SCy Schubert 		if (attr->acct_session_id_len != 16) {
8275b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
8285b9c547cSRui Paulo 				   "RADIUS DAS: Acct-Session-Id cannot match");
8295b9c547cSRui Paulo 			return NULL;
8305b9c547cSRui Paulo 		}
8315b9c547cSRui Paulo 		count = 0;
8325b9c547cSRui Paulo 
8335b9c547cSRui Paulo 		for (sta = hapd->sta_list; sta; sta = sta->next) {
8345b9c547cSRui Paulo 			if (!sta->radius_das_match)
8355b9c547cSRui Paulo 				continue;
836780fb4a2SCy Schubert 			os_snprintf(buf, sizeof(buf), "%016llX",
837780fb4a2SCy Schubert 				    (unsigned long long) sta->acct_session_id);
838780fb4a2SCy Schubert 			if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
8395b9c547cSRui Paulo 				sta->radius_das_match = 0;
8405b9c547cSRui Paulo 			else
8415b9c547cSRui Paulo 				count++;
842f05cddf9SRui Paulo 		}
843f05cddf9SRui Paulo 
8445b9c547cSRui Paulo 		if (count == 0) {
8455b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
8465b9c547cSRui Paulo 				   "RADIUS DAS: No matches remaining after Acct-Session-Id check");
8475b9c547cSRui Paulo 			return NULL;
8485b9c547cSRui Paulo 		}
8495b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
8505b9c547cSRui Paulo 	}
8515b9c547cSRui Paulo 
8525b9c547cSRui Paulo 	if (attr->acct_multi_session_id) {
8535b9c547cSRui Paulo 		num_attr++;
854780fb4a2SCy Schubert 		if (attr->acct_multi_session_id_len != 16) {
8555b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
8565b9c547cSRui Paulo 				   "RADIUS DAS: Acct-Multi-Session-Id cannot match");
8575b9c547cSRui Paulo 			return NULL;
8585b9c547cSRui Paulo 		}
8595b9c547cSRui Paulo 		count = 0;
8605b9c547cSRui Paulo 
8615b9c547cSRui Paulo 		for (sta = hapd->sta_list; sta; sta = sta->next) {
8625b9c547cSRui Paulo 			if (!sta->radius_das_match)
8635b9c547cSRui Paulo 				continue;
8645b9c547cSRui Paulo 			if (!sta->eapol_sm ||
865780fb4a2SCy Schubert 			    !sta->eapol_sm->acct_multi_session_id) {
8665b9c547cSRui Paulo 				sta->radius_das_match = 0;
8675b9c547cSRui Paulo 				continue;
8685b9c547cSRui Paulo 			}
869780fb4a2SCy Schubert 			os_snprintf(buf, sizeof(buf), "%016llX",
870780fb4a2SCy Schubert 				    (unsigned long long)
871780fb4a2SCy Schubert 				    sta->eapol_sm->acct_multi_session_id);
872780fb4a2SCy Schubert 			if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
8735b9c547cSRui Paulo 			    0)
8745b9c547cSRui Paulo 				sta->radius_das_match = 0;
8755b9c547cSRui Paulo 			else
8765b9c547cSRui Paulo 				count++;
8775b9c547cSRui Paulo 		}
8785b9c547cSRui Paulo 
8795b9c547cSRui Paulo 		if (count == 0) {
8805b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
8815b9c547cSRui Paulo 				   "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
8825b9c547cSRui Paulo 			return NULL;
8835b9c547cSRui Paulo 		}
8845b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
8855b9c547cSRui Paulo 			   "RADIUS DAS: Acct-Multi-Session-Id match");
8865b9c547cSRui Paulo 	}
8875b9c547cSRui Paulo 
8885b9c547cSRui Paulo 	if (attr->cui) {
8895b9c547cSRui Paulo 		num_attr++;
8905b9c547cSRui Paulo 		count = 0;
8915b9c547cSRui Paulo 
892f05cddf9SRui Paulo 		for (sta = hapd->sta_list; sta; sta = sta->next) {
893f05cddf9SRui Paulo 			struct wpabuf *cui;
8945b9c547cSRui Paulo 
8955b9c547cSRui Paulo 			if (!sta->radius_das_match)
8965b9c547cSRui Paulo 				continue;
897f05cddf9SRui Paulo 			cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
8985b9c547cSRui Paulo 			if (!cui || wpabuf_len(cui) != attr->cui_len ||
899f05cddf9SRui Paulo 			    os_memcmp(wpabuf_head(cui), attr->cui,
9005b9c547cSRui Paulo 				      attr->cui_len) != 0)
9015b9c547cSRui Paulo 				sta->radius_das_match = 0;
9025b9c547cSRui Paulo 			else
9035b9c547cSRui Paulo 				count++;
904f05cddf9SRui Paulo 		}
905f05cddf9SRui Paulo 
9065b9c547cSRui Paulo 		if (count == 0) {
9075b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
9085b9c547cSRui Paulo 				   "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
9095b9c547cSRui Paulo 			return NULL;
9105b9c547cSRui Paulo 		}
9115b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
9125b9c547cSRui Paulo 			   "RADIUS DAS: Chargeable-User-Identity match");
9135b9c547cSRui Paulo 	}
9145b9c547cSRui Paulo 
9155b9c547cSRui Paulo 	if (attr->user_name) {
9165b9c547cSRui Paulo 		num_attr++;
9175b9c547cSRui Paulo 		count = 0;
9185b9c547cSRui Paulo 
919f05cddf9SRui Paulo 		for (sta = hapd->sta_list; sta; sta = sta->next) {
920f05cddf9SRui Paulo 			u8 *identity;
921f05cddf9SRui Paulo 			size_t identity_len;
9225b9c547cSRui Paulo 
9235b9c547cSRui Paulo 			if (!sta->radius_das_match)
9245b9c547cSRui Paulo 				continue;
925f05cddf9SRui Paulo 			identity = ieee802_1x_get_identity(sta->eapol_sm,
926f05cddf9SRui Paulo 							   &identity_len);
9275b9c547cSRui Paulo 			if (!identity ||
9285b9c547cSRui Paulo 			    identity_len != attr->user_name_len ||
929f05cddf9SRui Paulo 			    os_memcmp(identity, attr->user_name, identity_len)
9305b9c547cSRui Paulo 			    != 0)
9315b9c547cSRui Paulo 				sta->radius_das_match = 0;
9325b9c547cSRui Paulo 			else
9335b9c547cSRui Paulo 				count++;
9345b9c547cSRui Paulo 		}
9355b9c547cSRui Paulo 
9365b9c547cSRui Paulo 		if (count == 0) {
9375b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
9385b9c547cSRui Paulo 				   "RADIUS DAS: No matches remaining after User-Name check");
9395b9c547cSRui Paulo 			return NULL;
9405b9c547cSRui Paulo 		}
9415b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
9425b9c547cSRui Paulo 			   "RADIUS DAS: User-Name match");
9435b9c547cSRui Paulo 	}
9445b9c547cSRui Paulo 
9455b9c547cSRui Paulo 	if (num_attr == 0) {
9465b9c547cSRui Paulo 		/*
9475b9c547cSRui Paulo 		 * In theory, we could match all current associations, but it
9485b9c547cSRui Paulo 		 * seems safer to just reject requests that do not include any
9495b9c547cSRui Paulo 		 * session identification attributes.
9505b9c547cSRui Paulo 		 */
9515b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
9525b9c547cSRui Paulo 			   "RADIUS DAS: No session identification attributes included");
9535b9c547cSRui Paulo 		return NULL;
9545b9c547cSRui Paulo 	}
9555b9c547cSRui Paulo 
9565b9c547cSRui Paulo 	selected = NULL;
9575b9c547cSRui Paulo 	for (sta = hapd->sta_list; sta; sta = sta->next) {
9585b9c547cSRui Paulo 		if (sta->radius_das_match) {
9595b9c547cSRui Paulo 			if (selected) {
9605b9c547cSRui Paulo 				*multi = 1;
9615b9c547cSRui Paulo 				return NULL;
9625b9c547cSRui Paulo 			}
9635b9c547cSRui Paulo 			selected = sta;
964f05cddf9SRui Paulo 		}
965f05cddf9SRui Paulo 	}
966f05cddf9SRui Paulo 
9675b9c547cSRui Paulo 	return selected;
9685b9c547cSRui Paulo }
9695b9c547cSRui Paulo 
9705b9c547cSRui Paulo 
hostapd_das_disconnect_pmksa(struct hostapd_data * hapd,struct radius_das_attrs * attr)9715b9c547cSRui Paulo static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
9725b9c547cSRui Paulo 					struct radius_das_attrs *attr)
9735b9c547cSRui Paulo {
9745b9c547cSRui Paulo 	if (!hapd->wpa_auth)
9755b9c547cSRui Paulo 		return -1;
9765b9c547cSRui Paulo 	return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
977f05cddf9SRui Paulo }
978f05cddf9SRui Paulo 
979f05cddf9SRui Paulo 
980f05cddf9SRui Paulo static enum radius_das_res
hostapd_das_disconnect(void * ctx,struct radius_das_attrs * attr)981f05cddf9SRui Paulo hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
982f05cddf9SRui Paulo {
983f05cddf9SRui Paulo 	struct hostapd_data *hapd = ctx;
984f05cddf9SRui Paulo 	struct sta_info *sta;
9855b9c547cSRui Paulo 	int multi;
986f05cddf9SRui Paulo 
987f05cddf9SRui Paulo 	if (hostapd_das_nas_mismatch(hapd, attr))
988f05cddf9SRui Paulo 		return RADIUS_DAS_NAS_MISMATCH;
989f05cddf9SRui Paulo 
9905b9c547cSRui Paulo 	sta = hostapd_das_find_sta(hapd, attr, &multi);
9915b9c547cSRui Paulo 	if (sta == NULL) {
9925b9c547cSRui Paulo 		if (multi) {
9935b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
9945b9c547cSRui Paulo 				   "RADIUS DAS: Multiple sessions match - not supported");
9955b9c547cSRui Paulo 			return RADIUS_DAS_MULTI_SESSION_MATCH;
9965b9c547cSRui Paulo 		}
9975b9c547cSRui Paulo 		if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
9985b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
9995b9c547cSRui Paulo 				   "RADIUS DAS: PMKSA cache entry matched");
10005b9c547cSRui Paulo 			return RADIUS_DAS_SUCCESS;
10015b9c547cSRui Paulo 		}
10025b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1003f05cddf9SRui Paulo 		return RADIUS_DAS_SESSION_NOT_FOUND;
10045b9c547cSRui Paulo 	}
10055b9c547cSRui Paulo 
10065b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
10075b9c547cSRui Paulo 		   " - disconnecting", MAC2STR(sta->addr));
10085b9c547cSRui Paulo 	wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1009f05cddf9SRui Paulo 
1010f05cddf9SRui Paulo 	hostapd_drv_sta_deauth(hapd, sta->addr,
1011f05cddf9SRui Paulo 			       WLAN_REASON_PREV_AUTH_NOT_VALID);
1012f05cddf9SRui Paulo 	ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1013f05cddf9SRui Paulo 
1014f05cddf9SRui Paulo 	return RADIUS_DAS_SUCCESS;
1015f05cddf9SRui Paulo }
1016f05cddf9SRui Paulo 
101785732ac8SCy Schubert 
101885732ac8SCy Schubert #ifdef CONFIG_HS20
101985732ac8SCy Schubert static enum radius_das_res
hostapd_das_coa(void * ctx,struct radius_das_attrs * attr)102085732ac8SCy Schubert hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
102185732ac8SCy Schubert {
102285732ac8SCy Schubert 	struct hostapd_data *hapd = ctx;
102385732ac8SCy Schubert 	struct sta_info *sta;
102485732ac8SCy Schubert 	int multi;
102585732ac8SCy Schubert 
102685732ac8SCy Schubert 	if (hostapd_das_nas_mismatch(hapd, attr))
102785732ac8SCy Schubert 		return RADIUS_DAS_NAS_MISMATCH;
102885732ac8SCy Schubert 
102985732ac8SCy Schubert 	sta = hostapd_das_find_sta(hapd, attr, &multi);
103085732ac8SCy Schubert 	if (!sta) {
103185732ac8SCy Schubert 		if (multi) {
103285732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
103385732ac8SCy Schubert 				   "RADIUS DAS: Multiple sessions match - not supported");
103485732ac8SCy Schubert 			return RADIUS_DAS_MULTI_SESSION_MATCH;
103585732ac8SCy Schubert 		}
103685732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
103785732ac8SCy Schubert 		return RADIUS_DAS_SESSION_NOT_FOUND;
103885732ac8SCy Schubert 	}
103985732ac8SCy Schubert 
104085732ac8SCy Schubert 	wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
104185732ac8SCy Schubert 		   " - CoA", MAC2STR(sta->addr));
104285732ac8SCy Schubert 
104385732ac8SCy Schubert 	if (attr->hs20_t_c_filtering) {
104485732ac8SCy Schubert 		if (attr->hs20_t_c_filtering[0] & BIT(0)) {
104585732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
104685732ac8SCy Schubert 				   "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
104785732ac8SCy Schubert 			return RADIUS_DAS_COA_FAILED;
104885732ac8SCy Schubert 		}
104985732ac8SCy Schubert 
105085732ac8SCy Schubert 		hs20_t_c_filtering(hapd, sta, 0);
105185732ac8SCy Schubert 	}
105285732ac8SCy Schubert 
105385732ac8SCy Schubert 	return RADIUS_DAS_SUCCESS;
105485732ac8SCy Schubert }
105585732ac8SCy Schubert #else /* CONFIG_HS20 */
105685732ac8SCy Schubert #define hostapd_das_coa NULL
105785732ac8SCy Schubert #endif /* CONFIG_HS20 */
105885732ac8SCy Schubert 
1059206b73d0SCy Schubert 
1060206b73d0SCy Schubert #ifdef CONFIG_SQLITE
1061206b73d0SCy Schubert 
db_table_exists(sqlite3 * db,const char * name)1062206b73d0SCy Schubert static int db_table_exists(sqlite3 *db, const char *name)
1063206b73d0SCy Schubert {
1064206b73d0SCy Schubert 	char cmd[128];
1065206b73d0SCy Schubert 
1066206b73d0SCy Schubert 	os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1067206b73d0SCy Schubert 	return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1068206b73d0SCy Schubert }
1069206b73d0SCy Schubert 
1070206b73d0SCy Schubert 
db_table_create_radius_attributes(sqlite3 * db)1071206b73d0SCy Schubert static int db_table_create_radius_attributes(sqlite3 *db)
1072206b73d0SCy Schubert {
1073206b73d0SCy Schubert 	char *err = NULL;
1074206b73d0SCy Schubert 	const char *sql =
1075206b73d0SCy Schubert 		"CREATE TABLE radius_attributes("
1076206b73d0SCy Schubert 		" id INTEGER PRIMARY KEY,"
1077206b73d0SCy Schubert 		" sta TEXT,"
1078206b73d0SCy Schubert 		" reqtype TEXT,"
1079206b73d0SCy Schubert 		" attr TEXT"
1080206b73d0SCy Schubert 		");"
1081206b73d0SCy Schubert 		"CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1082206b73d0SCy Schubert 
1083206b73d0SCy Schubert 	wpa_printf(MSG_DEBUG,
1084206b73d0SCy Schubert 		   "Adding database table for RADIUS attribute information");
1085206b73d0SCy Schubert 	if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1086206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1087206b73d0SCy Schubert 		sqlite3_free(err);
1088206b73d0SCy Schubert 		return -1;
1089206b73d0SCy Schubert 	}
1090206b73d0SCy Schubert 
1091206b73d0SCy Schubert 	return 0;
1092206b73d0SCy Schubert }
1093206b73d0SCy Schubert 
1094206b73d0SCy Schubert #endif /* CONFIG_SQLITE */
1095206b73d0SCy Schubert 
1096f05cddf9SRui Paulo #endif /* CONFIG_NO_RADIUS */
1097e28a4053SRui Paulo 
1098e28a4053SRui Paulo 
1099e28a4053SRui Paulo /**
1100e28a4053SRui Paulo  * hostapd_setup_bss - Per-BSS setup (initialization)
1101e28a4053SRui Paulo  * @hapd: Pointer to BSS data
11025b9c547cSRui Paulo  * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
11035b9c547cSRui Paulo  *	but interface may exist
1104e28a4053SRui Paulo  *
1105e28a4053SRui Paulo  * This function is used to initialize all per-BSS data structures and
1106e28a4053SRui Paulo  * resources. This gets called in a loop for each BSS when an interface is
1107e28a4053SRui Paulo  * initialized. Most of the modules that are initialized here will be
1108e28a4053SRui Paulo  * deinitialized in hostapd_cleanup().
1109e28a4053SRui Paulo  */
hostapd_setup_bss(struct hostapd_data * hapd,int first)1110e28a4053SRui Paulo static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1111e28a4053SRui Paulo {
1112e28a4053SRui Paulo 	struct hostapd_bss_config *conf = hapd->conf;
1113325151a3SRui Paulo 	u8 ssid[SSID_MAX_LEN + 1];
1114e28a4053SRui Paulo 	int ssid_len, set_ssid;
1115e28a4053SRui Paulo 	char force_ifname[IFNAMSIZ];
1116e28a4053SRui Paulo 	u8 if_addr[ETH_ALEN];
11175b9c547cSRui Paulo 	int flush_old_stations = 1;
1118e28a4053SRui Paulo 
11195b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
11205b9c547cSRui Paulo 		   __func__, hapd, conf->iface, first);
11215b9c547cSRui Paulo 
11225b9c547cSRui Paulo #ifdef EAP_SERVER_TNC
11235b9c547cSRui Paulo 	if (conf->tnc && tncs_global_init() < 0) {
11245b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
11255b9c547cSRui Paulo 		return -1;
11265b9c547cSRui Paulo 	}
11275b9c547cSRui Paulo #endif /* EAP_SERVER_TNC */
11285b9c547cSRui Paulo 
11295b9c547cSRui Paulo 	if (hapd->started) {
11305b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
11315b9c547cSRui Paulo 			   __func__, conf->iface);
11325b9c547cSRui Paulo 		return -1;
11335b9c547cSRui Paulo 	}
11345b9c547cSRui Paulo 	hapd->started = 1;
11355b9c547cSRui Paulo 
11365b9c547cSRui Paulo 	if (!first || first == -1) {
1137780fb4a2SCy Schubert 		u8 *addr = hapd->own_addr;
1138780fb4a2SCy Schubert 
1139780fb4a2SCy Schubert 		if (!is_zero_ether_addr(conf->bssid)) {
1140e28a4053SRui Paulo 			/* Allocate the configured BSSID. */
11415b9c547cSRui Paulo 			os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
1142e28a4053SRui Paulo 
1143e28a4053SRui Paulo 			if (hostapd_mac_comp(hapd->own_addr,
1144e28a4053SRui Paulo 					     hapd->iface->bss[0]->own_addr) ==
1145e28a4053SRui Paulo 			    0) {
1146e28a4053SRui Paulo 				wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1147e28a4053SRui Paulo 					   "BSSID set to the MAC address of "
11485b9c547cSRui Paulo 					   "the radio", conf->iface);
1149e28a4053SRui Paulo 				return -1;
1150e28a4053SRui Paulo 			}
1151780fb4a2SCy Schubert 		} else if (hapd->iconf->use_driver_iface_addr) {
1152780fb4a2SCy Schubert 			addr = NULL;
1153780fb4a2SCy Schubert 		} else {
1154780fb4a2SCy Schubert 			/* Allocate the next available BSSID. */
1155780fb4a2SCy Schubert 			do {
1156780fb4a2SCy Schubert 				inc_byte_array(hapd->own_addr, ETH_ALEN);
1157780fb4a2SCy Schubert 			} while (mac_in_conf(hapd->iconf, hapd->own_addr));
1158e28a4053SRui Paulo 		}
1159e28a4053SRui Paulo 
1160e28a4053SRui Paulo 		hapd->interface_added = 1;
1161e28a4053SRui Paulo 		if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
1162780fb4a2SCy Schubert 				   conf->iface, addr, hapd,
1163f05cddf9SRui Paulo 				   &hapd->drv_priv, force_ifname, if_addr,
11645b9c547cSRui Paulo 				   conf->bridge[0] ? conf->bridge : NULL,
11655b9c547cSRui Paulo 				   first == -1)) {
1166e28a4053SRui Paulo 			wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1167e28a4053SRui Paulo 				   MACSTR ")", MAC2STR(hapd->own_addr));
11685b9c547cSRui Paulo 			hapd->interface_added = 0;
1169e28a4053SRui Paulo 			return -1;
1170e28a4053SRui Paulo 		}
1171780fb4a2SCy Schubert 
1172780fb4a2SCy Schubert 		if (!addr)
1173780fb4a2SCy Schubert 			os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
1174e28a4053SRui Paulo 	}
1175e28a4053SRui Paulo 
1176f05cddf9SRui Paulo 	if (conf->wmm_enabled < 0)
1177c1d255d3SCy Schubert 		conf->wmm_enabled = hapd->iconf->ieee80211n |
1178c1d255d3SCy Schubert 			hapd->iconf->ieee80211ax;
1179f05cddf9SRui Paulo 
118085732ac8SCy Schubert #ifdef CONFIG_IEEE80211R_AP
1181780fb4a2SCy Schubert 	if (is_zero_ether_addr(conf->r1_key_holder))
1182780fb4a2SCy Schubert 		os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
118385732ac8SCy Schubert #endif /* CONFIG_IEEE80211R_AP */
1184780fb4a2SCy Schubert 
11855b9c547cSRui Paulo #ifdef CONFIG_MESH
118685732ac8SCy Schubert 	if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
11875b9c547cSRui Paulo 		flush_old_stations = 0;
11885b9c547cSRui Paulo #endif /* CONFIG_MESH */
11895b9c547cSRui Paulo 
11905b9c547cSRui Paulo 	if (flush_old_stations)
1191c1d255d3SCy Schubert 		hostapd_flush(hapd);
1192e28a4053SRui Paulo 	hostapd_set_privacy(hapd, 0);
1193e28a4053SRui Paulo 
1194c1d255d3SCy Schubert #ifdef CONFIG_WEP
1195c1d255d3SCy Schubert 	if (!hostapd_drv_nl80211(hapd))
1196e28a4053SRui Paulo 		hostapd_broadcast_wep_clear(hapd);
11975b9c547cSRui Paulo 	if (hostapd_setup_encryption(conf->iface, hapd))
1198e28a4053SRui Paulo 		return -1;
1199c1d255d3SCy Schubert #endif /* CONFIG_WEP */
1200e28a4053SRui Paulo 
1201e28a4053SRui Paulo 	/*
1202e28a4053SRui Paulo 	 * Fetch the SSID from the system and use it or,
1203e28a4053SRui Paulo 	 * if one was specified in the config file, verify they
1204e28a4053SRui Paulo 	 * match.
1205e28a4053SRui Paulo 	 */
1206e28a4053SRui Paulo 	ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1207e28a4053SRui Paulo 	if (ssid_len < 0) {
1208e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "Could not read SSID from system");
1209e28a4053SRui Paulo 		return -1;
1210e28a4053SRui Paulo 	}
1211e28a4053SRui Paulo 	if (conf->ssid.ssid_set) {
1212e28a4053SRui Paulo 		/*
1213e28a4053SRui Paulo 		 * If SSID is specified in the config file and it differs
1214e28a4053SRui Paulo 		 * from what is being used then force installation of the
1215e28a4053SRui Paulo 		 * new SSID.
1216e28a4053SRui Paulo 		 */
1217e28a4053SRui Paulo 		set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1218e28a4053SRui Paulo 			    os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1219e28a4053SRui Paulo 	} else {
1220e28a4053SRui Paulo 		/*
1221e28a4053SRui Paulo 		 * No SSID in the config file; just use the one we got
1222e28a4053SRui Paulo 		 * from the system.
1223e28a4053SRui Paulo 		 */
1224e28a4053SRui Paulo 		set_ssid = 0;
1225e28a4053SRui Paulo 		conf->ssid.ssid_len = ssid_len;
1226e28a4053SRui Paulo 		os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1227e28a4053SRui Paulo 	}
1228e28a4053SRui Paulo 
1229c1d255d3SCy Schubert 	/*
1230c1d255d3SCy Schubert 	 * Short SSID calculation is identical to FCS and it is defined in
1231c1d255d3SCy Schubert 	 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1232c1d255d3SCy Schubert 	 */
1233c1d255d3SCy Schubert 	conf->ssid.short_ssid = crc32(conf->ssid.ssid, conf->ssid.ssid_len);
1234c1d255d3SCy Schubert 
1235e28a4053SRui Paulo 	if (!hostapd_drv_none(hapd)) {
1236c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
1237f05cddf9SRui Paulo 			   " and ssid \"%s\"",
12385b9c547cSRui Paulo 			   conf->iface, MAC2STR(hapd->own_addr),
12395b9c547cSRui Paulo 			   wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
1240e28a4053SRui Paulo 	}
1241e28a4053SRui Paulo 
1242e28a4053SRui Paulo 	if (hostapd_setup_wpa_psk(conf)) {
1243e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1244e28a4053SRui Paulo 		return -1;
1245e28a4053SRui Paulo 	}
1246e28a4053SRui Paulo 
1247e28a4053SRui Paulo 	/* Set SSID for the kernel driver (to be used in beacon and probe
1248e28a4053SRui Paulo 	 * response frames) */
1249f05cddf9SRui Paulo 	if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1250e28a4053SRui Paulo 					 conf->ssid.ssid_len)) {
1251e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1252e28a4053SRui Paulo 		return -1;
1253e28a4053SRui Paulo 	}
1254e28a4053SRui Paulo 
12555b9c547cSRui Paulo 	if (wpa_debug_level <= MSG_MSGDUMP)
1256e28a4053SRui Paulo 		conf->radius->msg_dumps = 1;
1257e28a4053SRui Paulo #ifndef CONFIG_NO_RADIUS
1258206b73d0SCy Schubert 
1259206b73d0SCy Schubert #ifdef CONFIG_SQLITE
1260206b73d0SCy Schubert 	if (conf->radius_req_attr_sqlite) {
1261206b73d0SCy Schubert 		if (sqlite3_open(conf->radius_req_attr_sqlite,
1262206b73d0SCy Schubert 				 &hapd->rad_attr_db)) {
1263206b73d0SCy Schubert 			wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1264206b73d0SCy Schubert 				   conf->radius_req_attr_sqlite);
1265206b73d0SCy Schubert 			return -1;
1266206b73d0SCy Schubert 		}
1267206b73d0SCy Schubert 
1268206b73d0SCy Schubert 		wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1269206b73d0SCy Schubert 			   conf->radius_req_attr_sqlite);
1270206b73d0SCy Schubert 		if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1271206b73d0SCy Schubert 		    db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1272206b73d0SCy Schubert 			return -1;
1273206b73d0SCy Schubert 	}
1274206b73d0SCy Schubert #endif /* CONFIG_SQLITE */
1275206b73d0SCy Schubert 
1276e28a4053SRui Paulo 	hapd->radius = radius_client_init(hapd, conf->radius);
1277e28a4053SRui Paulo 	if (hapd->radius == NULL) {
1278e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1279e28a4053SRui Paulo 		return -1;
1280e28a4053SRui Paulo 	}
1281f05cddf9SRui Paulo 
12825b9c547cSRui Paulo 	if (conf->radius_das_port) {
1283f05cddf9SRui Paulo 		struct radius_das_conf das_conf;
1284f05cddf9SRui Paulo 		os_memset(&das_conf, 0, sizeof(das_conf));
12855b9c547cSRui Paulo 		das_conf.port = conf->radius_das_port;
12865b9c547cSRui Paulo 		das_conf.shared_secret = conf->radius_das_shared_secret;
1287f05cddf9SRui Paulo 		das_conf.shared_secret_len =
12885b9c547cSRui Paulo 			conf->radius_das_shared_secret_len;
12895b9c547cSRui Paulo 		das_conf.client_addr = &conf->radius_das_client_addr;
12905b9c547cSRui Paulo 		das_conf.time_window = conf->radius_das_time_window;
1291f05cddf9SRui Paulo 		das_conf.require_event_timestamp =
12925b9c547cSRui Paulo 			conf->radius_das_require_event_timestamp;
1293780fb4a2SCy Schubert 		das_conf.require_message_authenticator =
1294780fb4a2SCy Schubert 			conf->radius_das_require_message_authenticator;
1295f05cddf9SRui Paulo 		das_conf.ctx = hapd;
1296f05cddf9SRui Paulo 		das_conf.disconnect = hostapd_das_disconnect;
129785732ac8SCy Schubert 		das_conf.coa = hostapd_das_coa;
1298f05cddf9SRui Paulo 		hapd->radius_das = radius_das_init(&das_conf);
1299f05cddf9SRui Paulo 		if (hapd->radius_das == NULL) {
1300f05cddf9SRui Paulo 			wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1301f05cddf9SRui Paulo 				   "failed.");
1302f05cddf9SRui Paulo 			return -1;
1303f05cddf9SRui Paulo 		}
1304f05cddf9SRui Paulo 	}
1305e28a4053SRui Paulo #endif /* CONFIG_NO_RADIUS */
1306e28a4053SRui Paulo 
1307e28a4053SRui Paulo 	if (hostapd_acl_init(hapd)) {
1308e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "ACL initialization failed.");
1309e28a4053SRui Paulo 		return -1;
1310e28a4053SRui Paulo 	}
1311e28a4053SRui Paulo 	if (hostapd_init_wps(hapd, conf))
1312e28a4053SRui Paulo 		return -1;
1313e28a4053SRui Paulo 
131485732ac8SCy Schubert #ifdef CONFIG_DPP
131585732ac8SCy Schubert 	hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
131685732ac8SCy Schubert 	if (!hapd->gas)
131785732ac8SCy Schubert 		return -1;
131885732ac8SCy Schubert 	if (hostapd_dpp_init(hapd))
131985732ac8SCy Schubert 		return -1;
132085732ac8SCy Schubert #endif /* CONFIG_DPP */
132185732ac8SCy Schubert 
1322e28a4053SRui Paulo 	if (authsrv_init(hapd) < 0)
1323e28a4053SRui Paulo 		return -1;
1324e28a4053SRui Paulo 
1325e28a4053SRui Paulo 	if (ieee802_1x_init(hapd)) {
1326e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1327e28a4053SRui Paulo 		return -1;
1328e28a4053SRui Paulo 	}
1329e28a4053SRui Paulo 
13305b9c547cSRui Paulo 	if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1331e28a4053SRui Paulo 		return -1;
1332e28a4053SRui Paulo 
1333e28a4053SRui Paulo 	if (accounting_init(hapd)) {
1334e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1335e28a4053SRui Paulo 		return -1;
1336e28a4053SRui Paulo 	}
1337e28a4053SRui Paulo 
1338f05cddf9SRui Paulo #ifdef CONFIG_INTERWORKING
1339f05cddf9SRui Paulo 	if (gas_serv_init(hapd)) {
1340f05cddf9SRui Paulo 		wpa_printf(MSG_ERROR, "GAS server initialization failed");
1341f05cddf9SRui Paulo 		return -1;
1342f05cddf9SRui Paulo 	}
13435b9c547cSRui Paulo 
13445b9c547cSRui Paulo 	if (conf->qos_map_set_len &&
13455b9c547cSRui Paulo 	    hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
13465b9c547cSRui Paulo 				    conf->qos_map_set_len)) {
13475b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
13485b9c547cSRui Paulo 		return -1;
13495b9c547cSRui Paulo 	}
1350f05cddf9SRui Paulo #endif /* CONFIG_INTERWORKING */
1351f05cddf9SRui Paulo 
13525b9c547cSRui Paulo 	if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
13535b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1354e28a4053SRui Paulo 		return -1;
1355e28a4053SRui Paulo 	}
1356e28a4053SRui Paulo 
13575b9c547cSRui Paulo 	if (conf->proxy_arp) {
13585b9c547cSRui Paulo 		if (x_snoop_init(hapd)) {
13595b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
13605b9c547cSRui Paulo 				   "Generic snooping infrastructure initialization failed");
13615b9c547cSRui Paulo 			return -1;
13625b9c547cSRui Paulo 		}
13635b9c547cSRui Paulo 
13645b9c547cSRui Paulo 		if (dhcp_snoop_init(hapd)) {
13655b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
13665b9c547cSRui Paulo 				   "DHCP snooping initialization failed");
13675b9c547cSRui Paulo 			return -1;
13685b9c547cSRui Paulo 		}
13695b9c547cSRui Paulo 
13705b9c547cSRui Paulo 		if (ndisc_snoop_init(hapd)) {
13715b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
13725b9c547cSRui Paulo 				   "Neighbor Discovery snooping initialization failed");
13735b9c547cSRui Paulo 			return -1;
13745b9c547cSRui Paulo 		}
13755b9c547cSRui Paulo 	}
13765b9c547cSRui Paulo 
1377e28a4053SRui Paulo 	if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1378e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1379e28a4053SRui Paulo 		return -1;
1380e28a4053SRui Paulo 	}
1381e28a4053SRui Paulo 
13825b9c547cSRui Paulo 	if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
13835b9c547cSRui Paulo 		return -1;
1384e28a4053SRui Paulo 
1385c1d255d3SCy Schubert 	if (flush_old_stations && !conf->start_disabled &&
1386c1d255d3SCy Schubert 	    conf->broadcast_deauth) {
1387c1d255d3SCy Schubert 		u8 addr[ETH_ALEN];
1388c1d255d3SCy Schubert 
1389c1d255d3SCy Schubert 		/* Should any previously associated STA not have noticed that
1390c1d255d3SCy Schubert 		 * the AP had stopped and restarted, send one more
1391c1d255d3SCy Schubert 		 * deauthentication notification now that the AP is ready to
1392c1d255d3SCy Schubert 		 * operate. */
1393c1d255d3SCy Schubert 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1394c1d255d3SCy Schubert 			"Deauthenticate all stations at BSS start");
1395c1d255d3SCy Schubert 		os_memset(addr, 0xff, ETH_ALEN);
1396c1d255d3SCy Schubert 		hostapd_drv_sta_deauth(hapd, addr,
1397c1d255d3SCy Schubert 				       WLAN_REASON_PREV_AUTH_NOT_VALID);
1398c1d255d3SCy Schubert 	}
1399c1d255d3SCy Schubert 
1400f05cddf9SRui Paulo 	if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1401f05cddf9SRui Paulo 		return -1;
1402f05cddf9SRui Paulo 
1403f05cddf9SRui Paulo 	if (hapd->driver && hapd->driver->set_operstate)
1404f05cddf9SRui Paulo 		hapd->driver->set_operstate(hapd->drv_priv, 1);
1405f05cddf9SRui Paulo 
1406e28a4053SRui Paulo 	return 0;
1407e28a4053SRui Paulo }
1408e28a4053SRui Paulo 
1409e28a4053SRui Paulo 
hostapd_tx_queue_params(struct hostapd_iface * iface)1410e28a4053SRui Paulo static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1411e28a4053SRui Paulo {
1412e28a4053SRui Paulo 	struct hostapd_data *hapd = iface->bss[0];
1413e28a4053SRui Paulo 	int i;
1414e28a4053SRui Paulo 	struct hostapd_tx_queue_params *p;
1415e28a4053SRui Paulo 
14165b9c547cSRui Paulo #ifdef CONFIG_MESH
141785732ac8SCy Schubert 	if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
14185b9c547cSRui Paulo 		return;
14195b9c547cSRui Paulo #endif /* CONFIG_MESH */
14205b9c547cSRui Paulo 
1421e28a4053SRui Paulo 	for (i = 0; i < NUM_TX_QUEUES; i++) {
1422e28a4053SRui Paulo 		p = &iface->conf->tx_queue[i];
1423e28a4053SRui Paulo 
1424e28a4053SRui Paulo 		if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1425e28a4053SRui Paulo 						p->cwmax, p->burst)) {
1426e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1427e28a4053SRui Paulo 				   "parameters for queue %d.", i);
1428e28a4053SRui Paulo 			/* Continue anyway */
1429e28a4053SRui Paulo 		}
1430e28a4053SRui Paulo 	}
1431e28a4053SRui Paulo }
1432e28a4053SRui Paulo 
1433e28a4053SRui Paulo 
hostapd_set_acl_list(struct hostapd_data * hapd,struct mac_acl_entry * mac_acl,int n_entries,u8 accept_acl)14345b9c547cSRui Paulo static int hostapd_set_acl_list(struct hostapd_data *hapd,
14355b9c547cSRui Paulo 				struct mac_acl_entry *mac_acl,
14365b9c547cSRui Paulo 				int n_entries, u8 accept_acl)
14375b9c547cSRui Paulo {
14385b9c547cSRui Paulo 	struct hostapd_acl_params *acl_params;
14395b9c547cSRui Paulo 	int i, err;
14405b9c547cSRui Paulo 
14415b9c547cSRui Paulo 	acl_params = os_zalloc(sizeof(*acl_params) +
14425b9c547cSRui Paulo 			       (n_entries * sizeof(acl_params->mac_acl[0])));
14435b9c547cSRui Paulo 	if (!acl_params)
14445b9c547cSRui Paulo 		return -ENOMEM;
14455b9c547cSRui Paulo 
14465b9c547cSRui Paulo 	for (i = 0; i < n_entries; i++)
14475b9c547cSRui Paulo 		os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
14485b9c547cSRui Paulo 			  ETH_ALEN);
14495b9c547cSRui Paulo 
14505b9c547cSRui Paulo 	acl_params->acl_policy = accept_acl;
14515b9c547cSRui Paulo 	acl_params->num_mac_acl = n_entries;
14525b9c547cSRui Paulo 
14535b9c547cSRui Paulo 	err = hostapd_drv_set_acl(hapd, acl_params);
14545b9c547cSRui Paulo 
14555b9c547cSRui Paulo 	os_free(acl_params);
14565b9c547cSRui Paulo 
14575b9c547cSRui Paulo 	return err;
14585b9c547cSRui Paulo }
14595b9c547cSRui Paulo 
14605b9c547cSRui Paulo 
hostapd_set_acl(struct hostapd_data * hapd)14615b9c547cSRui Paulo static void hostapd_set_acl(struct hostapd_data *hapd)
14625b9c547cSRui Paulo {
14635b9c547cSRui Paulo 	struct hostapd_config *conf = hapd->iconf;
14645b9c547cSRui Paulo 	int err;
14655b9c547cSRui Paulo 	u8 accept_acl;
14665b9c547cSRui Paulo 
14675b9c547cSRui Paulo 	if (hapd->iface->drv_max_acl_mac_addrs == 0)
14685b9c547cSRui Paulo 		return;
14695b9c547cSRui Paulo 
14705b9c547cSRui Paulo 	if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
14715b9c547cSRui Paulo 		accept_acl = 1;
14725b9c547cSRui Paulo 		err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
14735b9c547cSRui Paulo 					   conf->bss[0]->num_accept_mac,
14745b9c547cSRui Paulo 					   accept_acl);
14755b9c547cSRui Paulo 		if (err) {
14765b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Failed to set accept acl");
14775b9c547cSRui Paulo 			return;
14785b9c547cSRui Paulo 		}
14795b9c547cSRui Paulo 	} else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
14805b9c547cSRui Paulo 		accept_acl = 0;
14815b9c547cSRui Paulo 		err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
14825b9c547cSRui Paulo 					   conf->bss[0]->num_deny_mac,
14835b9c547cSRui Paulo 					   accept_acl);
14845b9c547cSRui Paulo 		if (err) {
14855b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Failed to set deny acl");
14865b9c547cSRui Paulo 			return;
14875b9c547cSRui Paulo 		}
14885b9c547cSRui Paulo 	}
14895b9c547cSRui Paulo }
14905b9c547cSRui Paulo 
14915b9c547cSRui Paulo 
start_ctrl_iface_bss(struct hostapd_data * hapd)14925b9c547cSRui Paulo static int start_ctrl_iface_bss(struct hostapd_data *hapd)
14935b9c547cSRui Paulo {
14945b9c547cSRui Paulo 	if (!hapd->iface->interfaces ||
14955b9c547cSRui Paulo 	    !hapd->iface->interfaces->ctrl_iface_init)
14965b9c547cSRui Paulo 		return 0;
14975b9c547cSRui Paulo 
14985b9c547cSRui Paulo 	if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
14995b9c547cSRui Paulo 		wpa_printf(MSG_ERROR,
15005b9c547cSRui Paulo 			   "Failed to setup control interface for %s",
15015b9c547cSRui Paulo 			   hapd->conf->iface);
15025b9c547cSRui Paulo 		return -1;
15035b9c547cSRui Paulo 	}
15045b9c547cSRui Paulo 
15055b9c547cSRui Paulo 	return 0;
15065b9c547cSRui Paulo }
15075b9c547cSRui Paulo 
15085b9c547cSRui Paulo 
start_ctrl_iface(struct hostapd_iface * iface)15095b9c547cSRui Paulo static int start_ctrl_iface(struct hostapd_iface *iface)
15105b9c547cSRui Paulo {
15115b9c547cSRui Paulo 	size_t i;
15125b9c547cSRui Paulo 
15135b9c547cSRui Paulo 	if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
15145b9c547cSRui Paulo 		return 0;
15155b9c547cSRui Paulo 
15165b9c547cSRui Paulo 	for (i = 0; i < iface->num_bss; i++) {
15175b9c547cSRui Paulo 		struct hostapd_data *hapd = iface->bss[i];
15185b9c547cSRui Paulo 		if (iface->interfaces->ctrl_iface_init(hapd)) {
15195b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
15205b9c547cSRui Paulo 				   "Failed to setup control interface for %s",
15215b9c547cSRui Paulo 				   hapd->conf->iface);
15225b9c547cSRui Paulo 			return -1;
15235b9c547cSRui Paulo 		}
15245b9c547cSRui Paulo 	}
15255b9c547cSRui Paulo 
15265b9c547cSRui Paulo 	return 0;
15275b9c547cSRui Paulo }
15285b9c547cSRui Paulo 
15295b9c547cSRui Paulo 
channel_list_update_timeout(void * eloop_ctx,void * timeout_ctx)15305b9c547cSRui Paulo static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
15315b9c547cSRui Paulo {
15325b9c547cSRui Paulo 	struct hostapd_iface *iface = eloop_ctx;
15335b9c547cSRui Paulo 
15345b9c547cSRui Paulo 	if (!iface->wait_channel_update) {
15355b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
15365b9c547cSRui Paulo 		return;
15375b9c547cSRui Paulo 	}
15385b9c547cSRui Paulo 
15395b9c547cSRui Paulo 	/*
15405b9c547cSRui Paulo 	 * It is possible that the existing channel list is acceptable, so try
15415b9c547cSRui Paulo 	 * to proceed.
15425b9c547cSRui Paulo 	 */
15435b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
15445b9c547cSRui Paulo 	setup_interface2(iface);
15455b9c547cSRui Paulo }
15465b9c547cSRui Paulo 
15475b9c547cSRui Paulo 
hostapd_channel_list_updated(struct hostapd_iface * iface,int initiator)15485b9c547cSRui Paulo void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
15495b9c547cSRui Paulo {
15505b9c547cSRui Paulo 	if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
15515b9c547cSRui Paulo 		return;
15525b9c547cSRui Paulo 
15535b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
15545b9c547cSRui Paulo 	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
15555b9c547cSRui Paulo 	setup_interface2(iface);
15565b9c547cSRui Paulo }
15575b9c547cSRui Paulo 
15585b9c547cSRui Paulo 
setup_interface(struct hostapd_iface * iface)1559e28a4053SRui Paulo static int setup_interface(struct hostapd_iface *iface)
1560e28a4053SRui Paulo {
1561e28a4053SRui Paulo 	struct hostapd_data *hapd = iface->bss[0];
1562e28a4053SRui Paulo 	size_t i;
15635b9c547cSRui Paulo 
15645b9c547cSRui Paulo 	/*
15655b9c547cSRui Paulo 	 * It is possible that setup_interface() is called after the interface
15665b9c547cSRui Paulo 	 * was disabled etc., in which case driver_ap_teardown is possibly set
15675b9c547cSRui Paulo 	 * to 1. Clear it here so any other key/station deletion, which is not
15685b9c547cSRui Paulo 	 * part of a teardown flow, would also call the relevant driver
15695b9c547cSRui Paulo 	 * callbacks.
15705b9c547cSRui Paulo 	 */
15715b9c547cSRui Paulo 	iface->driver_ap_teardown = 0;
15725b9c547cSRui Paulo 
15735b9c547cSRui Paulo 	if (!iface->phy[0]) {
15745b9c547cSRui Paulo 		const char *phy = hostapd_drv_get_radio_name(hapd);
15755b9c547cSRui Paulo 		if (phy) {
15765b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "phy: %s", phy);
15775b9c547cSRui Paulo 			os_strlcpy(iface->phy, phy, sizeof(iface->phy));
15785b9c547cSRui Paulo 		}
15795b9c547cSRui Paulo 	}
1580e28a4053SRui Paulo 
1581e28a4053SRui Paulo 	/*
1582e28a4053SRui Paulo 	 * Make sure that all BSSes get configured with a pointer to the same
1583e28a4053SRui Paulo 	 * driver interface.
1584e28a4053SRui Paulo 	 */
1585e28a4053SRui Paulo 	for (i = 1; i < iface->num_bss; i++) {
1586e28a4053SRui Paulo 		iface->bss[i]->driver = hapd->driver;
1587e28a4053SRui Paulo 		iface->bss[i]->drv_priv = hapd->drv_priv;
1588e28a4053SRui Paulo 	}
1589e28a4053SRui Paulo 
1590e28a4053SRui Paulo 	if (hostapd_validate_bssid_configuration(iface))
1591e28a4053SRui Paulo 		return -1;
1592e28a4053SRui Paulo 
15935b9c547cSRui Paulo 	/*
15945b9c547cSRui Paulo 	 * Initialize control interfaces early to allow external monitoring of
15955b9c547cSRui Paulo 	 * channel setup operations that may take considerable amount of time
15965b9c547cSRui Paulo 	 * especially for DFS cases.
15975b9c547cSRui Paulo 	 */
15985b9c547cSRui Paulo 	if (start_ctrl_iface(iface))
15995b9c547cSRui Paulo 		return -1;
16005b9c547cSRui Paulo 
1601e28a4053SRui Paulo 	if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
16025b9c547cSRui Paulo 		char country[4], previous_country[4];
16035b9c547cSRui Paulo 
16045b9c547cSRui Paulo 		hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
16055b9c547cSRui Paulo 		if (hostapd_get_country(hapd, previous_country) < 0)
16065b9c547cSRui Paulo 			previous_country[0] = '\0';
16075b9c547cSRui Paulo 
1608e28a4053SRui Paulo 		os_memcpy(country, hapd->iconf->country, 3);
1609e28a4053SRui Paulo 		country[3] = '\0';
1610e28a4053SRui Paulo 		if (hostapd_set_country(hapd, country) < 0) {
1611e28a4053SRui Paulo 			wpa_printf(MSG_ERROR, "Failed to set country code");
1612e28a4053SRui Paulo 			return -1;
1613e28a4053SRui Paulo 		}
16145b9c547cSRui Paulo 
16155b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
16165b9c547cSRui Paulo 			   previous_country, country);
16175b9c547cSRui Paulo 
16185b9c547cSRui Paulo 		if (os_strncmp(previous_country, country, 2) != 0) {
16195b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
16205b9c547cSRui Paulo 			iface->wait_channel_update = 1;
16215b9c547cSRui Paulo 			eloop_register_timeout(5, 0,
16225b9c547cSRui Paulo 					       channel_list_update_timeout,
16235b9c547cSRui Paulo 					       iface, NULL);
16245b9c547cSRui Paulo 			return 0;
1625e28a4053SRui Paulo 		}
16265b9c547cSRui Paulo 	}
16275b9c547cSRui Paulo 
16285b9c547cSRui Paulo 	return setup_interface2(iface);
16295b9c547cSRui Paulo }
16305b9c547cSRui Paulo 
16315b9c547cSRui Paulo 
configured_fixed_chan_to_freq(struct hostapd_iface * iface)1632c1d255d3SCy Schubert static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1633c1d255d3SCy Schubert {
1634c1d255d3SCy Schubert 	int freq, i, j;
1635c1d255d3SCy Schubert 
1636c1d255d3SCy Schubert 	if (!iface->conf->channel)
1637c1d255d3SCy Schubert 		return 0;
1638c1d255d3SCy Schubert 	if (iface->conf->op_class) {
1639c1d255d3SCy Schubert 		freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1640c1d255d3SCy Schubert 					      iface->conf->channel);
1641c1d255d3SCy Schubert 		if (freq < 0) {
1642c1d255d3SCy Schubert 			wpa_printf(MSG_INFO,
1643c1d255d3SCy Schubert 				   "Could not convert op_class %u channel %u to operating frequency",
1644c1d255d3SCy Schubert 				   iface->conf->op_class, iface->conf->channel);
1645c1d255d3SCy Schubert 			return -1;
1646c1d255d3SCy Schubert 		}
1647c1d255d3SCy Schubert 		iface->freq = freq;
1648c1d255d3SCy Schubert 		return 0;
1649c1d255d3SCy Schubert 	}
1650c1d255d3SCy Schubert 
1651c1d255d3SCy Schubert 	/* Old configurations using only 2.4/5/60 GHz bands may not specify the
1652c1d255d3SCy Schubert 	 * op_class parameter. Select a matching channel from the configured
1653c1d255d3SCy Schubert 	 * mode using the channel parameter for these cases.
1654c1d255d3SCy Schubert 	 */
1655c1d255d3SCy Schubert 	for (j = 0; j < iface->num_hw_features; j++) {
1656c1d255d3SCy Schubert 		struct hostapd_hw_modes *mode = &iface->hw_features[j];
1657c1d255d3SCy Schubert 
1658c1d255d3SCy Schubert 		if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1659c1d255d3SCy Schubert 		    iface->conf->hw_mode != mode->mode)
1660c1d255d3SCy Schubert 			continue;
1661c1d255d3SCy Schubert 		for (i = 0; i < mode->num_channels; i++) {
1662c1d255d3SCy Schubert 			struct hostapd_channel_data *chan = &mode->channels[i];
1663c1d255d3SCy Schubert 
1664c1d255d3SCy Schubert 			if (chan->chan == iface->conf->channel &&
1665c1d255d3SCy Schubert 			    !is_6ghz_freq(chan->freq)) {
1666c1d255d3SCy Schubert 				iface->freq = chan->freq;
1667c1d255d3SCy Schubert 				return 0;
1668c1d255d3SCy Schubert 			}
1669c1d255d3SCy Schubert 		}
1670c1d255d3SCy Schubert 	}
1671c1d255d3SCy Schubert 
1672c1d255d3SCy Schubert 	wpa_printf(MSG_INFO, "Could not determine operating frequency");
1673c1d255d3SCy Schubert 	return -1;
1674c1d255d3SCy Schubert }
1675c1d255d3SCy Schubert 
1676c1d255d3SCy Schubert 
hostapd_set_6ghz_sec_chan(struct hostapd_iface * iface)1677c1d255d3SCy Schubert static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
1678c1d255d3SCy Schubert {
1679c1d255d3SCy Schubert 	int bw, seg0;
1680c1d255d3SCy Schubert 
1681c1d255d3SCy Schubert 	if (!is_6ghz_op_class(iface->conf->op_class))
1682c1d255d3SCy Schubert 		return;
1683c1d255d3SCy Schubert 
1684c1d255d3SCy Schubert 	seg0 = hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
1685c1d255d3SCy Schubert 	bw = center_idx_to_bw_6ghz(seg0);
1686c1d255d3SCy Schubert 	/* Assign the secondary channel if absent in config for
1687c1d255d3SCy Schubert 	 * bandwidths > 20 MHz */
1688c1d255d3SCy Schubert 	if (bw > 20 && !iface->conf->secondary_channel) {
1689c1d255d3SCy Schubert 		if (((iface->conf->channel - 1) / 4) % 2)
1690c1d255d3SCy Schubert 			iface->conf->secondary_channel = -1;
1691c1d255d3SCy Schubert 		else
1692c1d255d3SCy Schubert 			iface->conf->secondary_channel = 1;
1693c1d255d3SCy Schubert 	}
1694c1d255d3SCy Schubert }
1695c1d255d3SCy Schubert 
1696c1d255d3SCy Schubert 
setup_interface2(struct hostapd_iface * iface)16975b9c547cSRui Paulo static int setup_interface2(struct hostapd_iface *iface)
16985b9c547cSRui Paulo {
16995b9c547cSRui Paulo 	iface->wait_channel_update = 0;
1700e28a4053SRui Paulo 
170184ed8638SCy Schubert #ifdef __FreeBSD
170284ed8638SCy Schubert 	/* XXX hostapd_get_hw_features() is an inline that always returns -1
170384ed8638SCy Schubert 	 * because MLME will not build under FreeBSD due to its use of
170484ed8638SCy Schubert 	 * Linux definitions. Normally FreeBSD would uncondionally execute the
170584ed8638SCy Schubert 	 * "Not all drivers support..." block. Instead we #ifdef out the entire
170684ed8638SCy Schubert 	 * block of code instead of maintaining the fallacy that
170784ed8638SCy Schubert 	 * hostapd_get_hw_features() returns anything meaninful.
170884ed8638SCy Schubert 	 *
170984ed8638SCy Schubert 	 * Ideally WANT_AP_MLME should be taught about FreeBSD data structures
171084ed8638SCy Schubert 	 * and defintions. Instead we do this to enable channel selection in
171184ed8638SCy Schubert 	 * hostapd.conf.
171284ed8638SCy Schubert 	 */
171384ed8638SCy Schubert 	iface->freq = iface->conf->channel;
171484ed8638SCy Schubert #else
1715e28a4053SRui Paulo 	if (hostapd_get_hw_features(iface)) {
1716e28a4053SRui Paulo 		/* Not all drivers support this yet, so continue without hw
1717e28a4053SRui Paulo 		 * feature data. */
1718e28a4053SRui Paulo 	} else {
1719c1d255d3SCy Schubert 		int ret;
1720c1d255d3SCy Schubert 
1721c1d255d3SCy Schubert 		ret = configured_fixed_chan_to_freq(iface);
1722c1d255d3SCy Schubert 		if (ret < 0)
1723c1d255d3SCy Schubert 			goto fail;
1724c1d255d3SCy Schubert 
1725c1d255d3SCy Schubert 		if (iface->conf->op_class) {
1726c1d255d3SCy Schubert 			int ch_width;
1727c1d255d3SCy Schubert 
1728c1d255d3SCy Schubert 			ch_width = op_class_to_ch_width(iface->conf->op_class);
1729c1d255d3SCy Schubert 			hostapd_set_oper_chwidth(iface->conf, ch_width);
1730c1d255d3SCy Schubert 			hostapd_set_6ghz_sec_chan(iface);
1731c1d255d3SCy Schubert 		}
1732c1d255d3SCy Schubert 
1733c1d255d3SCy Schubert 		ret = hostapd_select_hw_mode(iface);
1734e28a4053SRui Paulo 		if (ret < 0) {
1735e28a4053SRui Paulo 			wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1736e28a4053SRui Paulo 				   "channel. (%d)", ret);
17375b9c547cSRui Paulo 			goto fail;
17385b9c547cSRui Paulo 		}
17395b9c547cSRui Paulo 		if (ret == 1) {
17405b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
17415b9c547cSRui Paulo 			return 0;
1742e28a4053SRui Paulo 		}
1743c1d255d3SCy Schubert 		ret = hostapd_check_edmg_capab(iface);
1744c1d255d3SCy Schubert 		if (ret < 0)
1745c1d255d3SCy Schubert 			goto fail;
1746c1d255d3SCy Schubert 		ret = hostapd_check_he_6ghz_capab(iface);
1747c1d255d3SCy Schubert 		if (ret < 0)
1748c1d255d3SCy Schubert 			goto fail;
1749e28a4053SRui Paulo 		ret = hostapd_check_ht_capab(iface);
1750e28a4053SRui Paulo 		if (ret < 0)
17515b9c547cSRui Paulo 			goto fail;
1752e28a4053SRui Paulo 		if (ret == 1) {
1753e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "Interface initialization will "
1754e28a4053SRui Paulo 				   "be completed in a callback");
1755e28a4053SRui Paulo 			return 0;
1756e28a4053SRui Paulo 		}
17575b9c547cSRui Paulo 
17585b9c547cSRui Paulo 		if (iface->conf->ieee80211h)
17595b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "DFS support is enabled");
1760e28a4053SRui Paulo 	}
176184ed8638SCy Schubert #endif
1762e28a4053SRui Paulo 	return hostapd_setup_interface_complete(iface, 0);
17635b9c547cSRui Paulo 
17645b9c547cSRui Paulo fail:
17655b9c547cSRui Paulo 	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
17665b9c547cSRui Paulo 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
17675b9c547cSRui Paulo 	if (iface->interfaces && iface->interfaces->terminate_on_error)
17685b9c547cSRui Paulo 		eloop_terminate();
17695b9c547cSRui Paulo 	return -1;
1770e28a4053SRui Paulo }
1771e28a4053SRui Paulo 
1772e28a4053SRui Paulo 
1773325151a3SRui Paulo #ifdef CONFIG_FST
1774325151a3SRui Paulo 
fst_hostapd_get_bssid_cb(void * ctx)1775325151a3SRui Paulo static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1776325151a3SRui Paulo {
1777325151a3SRui Paulo 	struct hostapd_data *hapd = ctx;
1778325151a3SRui Paulo 
1779325151a3SRui Paulo 	return hapd->own_addr;
1780325151a3SRui Paulo }
1781325151a3SRui Paulo 
1782325151a3SRui Paulo 
fst_hostapd_get_channel_info_cb(void * ctx,enum hostapd_hw_mode * hw_mode,u8 * channel)1783325151a3SRui Paulo static void fst_hostapd_get_channel_info_cb(void *ctx,
1784325151a3SRui Paulo 					    enum hostapd_hw_mode *hw_mode,
1785325151a3SRui Paulo 					    u8 *channel)
1786325151a3SRui Paulo {
1787325151a3SRui Paulo 	struct hostapd_data *hapd = ctx;
1788325151a3SRui Paulo 
1789325151a3SRui Paulo 	*hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1790325151a3SRui Paulo }
1791325151a3SRui Paulo 
1792325151a3SRui Paulo 
fst_hostapd_set_ies_cb(void * ctx,const struct wpabuf * fst_ies)1793325151a3SRui Paulo static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1794325151a3SRui Paulo {
1795325151a3SRui Paulo 	struct hostapd_data *hapd = ctx;
1796325151a3SRui Paulo 
1797325151a3SRui Paulo 	if (hapd->iface->fst_ies != fst_ies) {
1798325151a3SRui Paulo 		hapd->iface->fst_ies = fst_ies;
1799325151a3SRui Paulo 		if (ieee802_11_set_beacon(hapd))
1800325151a3SRui Paulo 			wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1801325151a3SRui Paulo 	}
1802325151a3SRui Paulo }
1803325151a3SRui Paulo 
1804325151a3SRui Paulo 
fst_hostapd_send_action_cb(void * ctx,const u8 * da,struct wpabuf * buf)1805325151a3SRui Paulo static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1806325151a3SRui Paulo 				      struct wpabuf *buf)
1807325151a3SRui Paulo {
1808325151a3SRui Paulo 	struct hostapd_data *hapd = ctx;
1809325151a3SRui Paulo 
1810325151a3SRui Paulo 	return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1811325151a3SRui Paulo 				       wpabuf_head(buf), wpabuf_len(buf));
1812325151a3SRui Paulo }
1813325151a3SRui Paulo 
1814325151a3SRui Paulo 
fst_hostapd_get_mb_ie_cb(void * ctx,const u8 * addr)1815325151a3SRui Paulo static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1816325151a3SRui Paulo {
1817325151a3SRui Paulo 	struct hostapd_data *hapd = ctx;
1818325151a3SRui Paulo 	struct sta_info *sta = ap_get_sta(hapd, addr);
1819325151a3SRui Paulo 
1820325151a3SRui Paulo 	return sta ? sta->mb_ies : NULL;
1821325151a3SRui Paulo }
1822325151a3SRui Paulo 
1823325151a3SRui Paulo 
fst_hostapd_update_mb_ie_cb(void * ctx,const u8 * addr,const u8 * buf,size_t size)1824325151a3SRui Paulo static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1825325151a3SRui Paulo 					const u8 *buf, size_t size)
1826325151a3SRui Paulo {
1827325151a3SRui Paulo 	struct hostapd_data *hapd = ctx;
1828325151a3SRui Paulo 	struct sta_info *sta = ap_get_sta(hapd, addr);
1829325151a3SRui Paulo 
1830325151a3SRui Paulo 	if (sta) {
1831325151a3SRui Paulo 		struct mb_ies_info info;
1832325151a3SRui Paulo 
1833325151a3SRui Paulo 		if (!mb_ies_info_by_ies(&info, buf, size)) {
1834325151a3SRui Paulo 			wpabuf_free(sta->mb_ies);
1835325151a3SRui Paulo 			sta->mb_ies = mb_ies_by_info(&info);
1836325151a3SRui Paulo 		}
1837325151a3SRui Paulo 	}
1838325151a3SRui Paulo }
1839325151a3SRui Paulo 
1840325151a3SRui Paulo 
fst_hostapd_get_sta(struct fst_get_peer_ctx ** get_ctx,bool mb_only)1841325151a3SRui Paulo static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1842c1d255d3SCy Schubert 				      bool mb_only)
1843325151a3SRui Paulo {
1844325151a3SRui Paulo 	struct sta_info *s = (struct sta_info *) *get_ctx;
1845325151a3SRui Paulo 
1846325151a3SRui Paulo 	if (mb_only) {
1847325151a3SRui Paulo 		for (; s && !s->mb_ies; s = s->next)
1848325151a3SRui Paulo 			;
1849325151a3SRui Paulo 	}
1850325151a3SRui Paulo 
1851325151a3SRui Paulo 	if (s) {
1852325151a3SRui Paulo 		*get_ctx = (struct fst_get_peer_ctx *) s->next;
1853325151a3SRui Paulo 
1854325151a3SRui Paulo 		return s->addr;
1855325151a3SRui Paulo 	}
1856325151a3SRui Paulo 
1857325151a3SRui Paulo 	*get_ctx = NULL;
1858325151a3SRui Paulo 	return NULL;
1859325151a3SRui Paulo }
1860325151a3SRui Paulo 
1861325151a3SRui Paulo 
fst_hostapd_get_peer_first(void * ctx,struct fst_get_peer_ctx ** get_ctx,bool mb_only)1862325151a3SRui Paulo static const u8 * fst_hostapd_get_peer_first(void *ctx,
1863325151a3SRui Paulo 					     struct fst_get_peer_ctx **get_ctx,
1864c1d255d3SCy Schubert 					     bool mb_only)
1865325151a3SRui Paulo {
1866325151a3SRui Paulo 	struct hostapd_data *hapd = ctx;
1867325151a3SRui Paulo 
1868325151a3SRui Paulo 	*get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1869325151a3SRui Paulo 
1870325151a3SRui Paulo 	return fst_hostapd_get_sta(get_ctx, mb_only);
1871325151a3SRui Paulo }
1872325151a3SRui Paulo 
1873325151a3SRui Paulo 
fst_hostapd_get_peer_next(void * ctx,struct fst_get_peer_ctx ** get_ctx,bool mb_only)1874325151a3SRui Paulo static const u8 * fst_hostapd_get_peer_next(void *ctx,
1875325151a3SRui Paulo 					    struct fst_get_peer_ctx **get_ctx,
1876c1d255d3SCy Schubert 					    bool mb_only)
1877325151a3SRui Paulo {
1878325151a3SRui Paulo 	return fst_hostapd_get_sta(get_ctx, mb_only);
1879325151a3SRui Paulo }
1880325151a3SRui Paulo 
1881325151a3SRui Paulo 
fst_hostapd_fill_iface_obj(struct hostapd_data * hapd,struct fst_wpa_obj * iface_obj)1882325151a3SRui Paulo void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1883325151a3SRui Paulo 				struct fst_wpa_obj *iface_obj)
1884325151a3SRui Paulo {
1885325151a3SRui Paulo 	iface_obj->ctx = hapd;
1886325151a3SRui Paulo 	iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1887325151a3SRui Paulo 	iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1888325151a3SRui Paulo 	iface_obj->set_ies = fst_hostapd_set_ies_cb;
1889325151a3SRui Paulo 	iface_obj->send_action = fst_hostapd_send_action_cb;
1890325151a3SRui Paulo 	iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1891325151a3SRui Paulo 	iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1892325151a3SRui Paulo 	iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1893325151a3SRui Paulo 	iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1894325151a3SRui Paulo }
1895325151a3SRui Paulo 
1896325151a3SRui Paulo #endif /* CONFIG_FST */
1897325151a3SRui Paulo 
189885732ac8SCy Schubert #ifdef CONFIG_OWE
189985732ac8SCy Schubert 
hostapd_owe_iface_iter(struct hostapd_iface * iface,void * ctx)190085732ac8SCy Schubert static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
190185732ac8SCy Schubert {
190285732ac8SCy Schubert 	struct hostapd_data *hapd = ctx;
190385732ac8SCy Schubert 	size_t i;
190485732ac8SCy Schubert 
190585732ac8SCy Schubert 	for (i = 0; i < iface->num_bss; i++) {
190685732ac8SCy Schubert 		struct hostapd_data *bss = iface->bss[i];
190785732ac8SCy Schubert 
190885732ac8SCy Schubert 		if (os_strcmp(hapd->conf->owe_transition_ifname,
190985732ac8SCy Schubert 			      bss->conf->iface) != 0)
191085732ac8SCy Schubert 			continue;
191185732ac8SCy Schubert 
191285732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
191385732ac8SCy Schubert 			   "OWE: ifname=%s found transition mode ifname=%s BSSID "
191485732ac8SCy Schubert 			   MACSTR " SSID %s",
191585732ac8SCy Schubert 			   hapd->conf->iface, bss->conf->iface,
191685732ac8SCy Schubert 			   MAC2STR(bss->own_addr),
191785732ac8SCy Schubert 			   wpa_ssid_txt(bss->conf->ssid.ssid,
191885732ac8SCy Schubert 					bss->conf->ssid.ssid_len));
191985732ac8SCy Schubert 		if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
192085732ac8SCy Schubert 		    is_zero_ether_addr(bss->own_addr))
192185732ac8SCy Schubert 			continue;
192285732ac8SCy Schubert 
192385732ac8SCy Schubert 		os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
192485732ac8SCy Schubert 			  ETH_ALEN);
192585732ac8SCy Schubert 		os_memcpy(hapd->conf->owe_transition_ssid,
192685732ac8SCy Schubert 			  bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
192785732ac8SCy Schubert 		hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
192885732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
192985732ac8SCy Schubert 			   "OWE: Copied transition mode information");
193085732ac8SCy Schubert 		return 1;
193185732ac8SCy Schubert 	}
193285732ac8SCy Schubert 
193385732ac8SCy Schubert 	return 0;
193485732ac8SCy Schubert }
193585732ac8SCy Schubert 
193685732ac8SCy Schubert 
hostapd_owe_trans_get_info(struct hostapd_data * hapd)193785732ac8SCy Schubert int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
193885732ac8SCy Schubert {
193985732ac8SCy Schubert 	if (hapd->conf->owe_transition_ssid_len > 0 &&
194085732ac8SCy Schubert 	    !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
194185732ac8SCy Schubert 		return 0;
194285732ac8SCy Schubert 
194385732ac8SCy Schubert 	/* Find transition mode SSID/BSSID information from a BSS operated by
194485732ac8SCy Schubert 	 * this hostapd instance. */
194585732ac8SCy Schubert 	if (!hapd->iface->interfaces ||
194685732ac8SCy Schubert 	    !hapd->iface->interfaces->for_each_interface)
194785732ac8SCy Schubert 		return hostapd_owe_iface_iter(hapd->iface, hapd);
194885732ac8SCy Schubert 	else
194985732ac8SCy Schubert 		return hapd->iface->interfaces->for_each_interface(
195085732ac8SCy Schubert 			hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
195185732ac8SCy Schubert }
195285732ac8SCy Schubert 
195385732ac8SCy Schubert 
hostapd_owe_iface_iter2(struct hostapd_iface * iface,void * ctx)195485732ac8SCy Schubert static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
195585732ac8SCy Schubert {
195685732ac8SCy Schubert 	size_t i;
195785732ac8SCy Schubert 
195885732ac8SCy Schubert 	for (i = 0; i < iface->num_bss; i++) {
195985732ac8SCy Schubert 		struct hostapd_data *bss = iface->bss[i];
196085732ac8SCy Schubert 		int res;
196185732ac8SCy Schubert 
196285732ac8SCy Schubert 		if (!bss->conf->owe_transition_ifname[0])
196385732ac8SCy Schubert 			continue;
1964c1d255d3SCy Schubert 		if (bss->iface->state != HAPD_IFACE_ENABLED) {
1965c1d255d3SCy Schubert 			wpa_printf(MSG_DEBUG,
1966c1d255d3SCy Schubert 				   "OWE: Interface %s state %s - defer beacon update",
1967c1d255d3SCy Schubert 				   bss->conf->iface,
1968c1d255d3SCy Schubert 				   hostapd_state_text(bss->iface->state));
1969c1d255d3SCy Schubert 			continue;
1970c1d255d3SCy Schubert 		}
197185732ac8SCy Schubert 		res = hostapd_owe_trans_get_info(bss);
197285732ac8SCy Schubert 		if (res == 0)
197385732ac8SCy Schubert 			continue;
197485732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
197585732ac8SCy Schubert 			   "OWE: Matching transition mode interface enabled - update beacon data for %s",
197685732ac8SCy Schubert 			   bss->conf->iface);
197785732ac8SCy Schubert 		ieee802_11_set_beacon(bss);
197885732ac8SCy Schubert 	}
197985732ac8SCy Schubert 
198085732ac8SCy Schubert 	return 0;
198185732ac8SCy Schubert }
198285732ac8SCy Schubert 
198385732ac8SCy Schubert #endif /* CONFIG_OWE */
198485732ac8SCy Schubert 
198585732ac8SCy Schubert 
hostapd_owe_update_trans(struct hostapd_iface * iface)198685732ac8SCy Schubert static void hostapd_owe_update_trans(struct hostapd_iface *iface)
198785732ac8SCy Schubert {
198885732ac8SCy Schubert #ifdef CONFIG_OWE
198985732ac8SCy Schubert 	/* Check whether the enabled BSS can complete OWE transition mode
199085732ac8SCy Schubert 	 * configuration for any pending interface. */
199185732ac8SCy Schubert 	if (!iface->interfaces ||
199285732ac8SCy Schubert 	    !iface->interfaces->for_each_interface)
199385732ac8SCy Schubert 		hostapd_owe_iface_iter2(iface, NULL);
199485732ac8SCy Schubert 	else
199585732ac8SCy Schubert 		iface->interfaces->for_each_interface(
199685732ac8SCy Schubert 			iface->interfaces, hostapd_owe_iface_iter2, NULL);
199785732ac8SCy Schubert #endif /* CONFIG_OWE */
199885732ac8SCy Schubert }
199985732ac8SCy Schubert 
200085732ac8SCy Schubert 
hostapd_interface_setup_failure_handler(void * eloop_ctx,void * timeout_ctx)200185732ac8SCy Schubert static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
200285732ac8SCy Schubert 						    void *timeout_ctx)
200385732ac8SCy Schubert {
200485732ac8SCy Schubert 	struct hostapd_iface *iface = eloop_ctx;
200585732ac8SCy Schubert 	struct hostapd_data *hapd;
200685732ac8SCy Schubert 
200785732ac8SCy Schubert 	if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
200885732ac8SCy Schubert 		return;
200985732ac8SCy Schubert 	hapd = iface->bss[0];
201085732ac8SCy Schubert 	if (hapd->setup_complete_cb)
201185732ac8SCy Schubert 		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
201285732ac8SCy Schubert }
201385732ac8SCy Schubert 
201485732ac8SCy Schubert 
hostapd_setup_interface_complete_sync(struct hostapd_iface * iface,int err)2015780fb4a2SCy Schubert static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2016780fb4a2SCy Schubert 						 int err)
2017e28a4053SRui Paulo {
2018e28a4053SRui Paulo 	struct hostapd_data *hapd = iface->bss[0];
2019e28a4053SRui Paulo 	size_t j;
2020e28a4053SRui Paulo 	u8 *prev_addr;
20215b9c547cSRui Paulo 	int delay_apply_cfg = 0;
20225b9c547cSRui Paulo 	int res_dfs_offload = 0;
2023e28a4053SRui Paulo 
20245b9c547cSRui Paulo 	if (err)
20255b9c547cSRui Paulo 		goto fail;
2026e28a4053SRui Paulo 
2027e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "Completing interface initialization");
2028c1d255d3SCy Schubert 	if (iface->freq) {
20295b9c547cSRui Paulo #ifdef NEED_AP_MLME
20305b9c547cSRui Paulo 		int res;
20315b9c547cSRui Paulo #endif /* NEED_AP_MLME */
20325b9c547cSRui Paulo 
2033e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
2034e28a4053SRui Paulo 			   "Frequency: %d MHz",
20355b9c547cSRui Paulo 			   hostapd_hw_mode_txt(iface->conf->hw_mode),
20365b9c547cSRui Paulo 			   iface->conf->channel, iface->freq);
2037e28a4053SRui Paulo 
20385b9c547cSRui Paulo #ifdef NEED_AP_MLME
20395b9c547cSRui Paulo 		/* Handle DFS only if it is not offloaded to the driver */
20405b9c547cSRui Paulo 		if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
20415b9c547cSRui Paulo 			/* Check DFS */
20425b9c547cSRui Paulo 			res = hostapd_handle_dfs(iface);
20435b9c547cSRui Paulo 			if (res <= 0) {
20445b9c547cSRui Paulo 				if (res < 0)
20455b9c547cSRui Paulo 					goto fail;
20465b9c547cSRui Paulo 				return res;
20475b9c547cSRui Paulo 			}
20485b9c547cSRui Paulo 		} else {
20495b9c547cSRui Paulo 			/* If DFS is offloaded to the driver */
20505b9c547cSRui Paulo 			res_dfs_offload = hostapd_handle_dfs_offload(iface);
20515b9c547cSRui Paulo 			if (res_dfs_offload <= 0) {
20525b9c547cSRui Paulo 				if (res_dfs_offload < 0)
20535b9c547cSRui Paulo 					goto fail;
20545b9c547cSRui Paulo 			} else {
20555b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG,
20565b9c547cSRui Paulo 					   "Proceed with AP/channel setup");
20575b9c547cSRui Paulo 				/*
20585b9c547cSRui Paulo 				 * If this is a DFS channel, move to completing
20595b9c547cSRui Paulo 				 * AP setup.
20605b9c547cSRui Paulo 				 */
20615b9c547cSRui Paulo 				if (res_dfs_offload == 1)
20625b9c547cSRui Paulo 					goto dfs_offload;
20635b9c547cSRui Paulo 				/* Otherwise fall through. */
20645b9c547cSRui Paulo 			}
20655b9c547cSRui Paulo 		}
20665b9c547cSRui Paulo #endif /* NEED_AP_MLME */
20675b9c547cSRui Paulo 
20685b9c547cSRui Paulo #ifdef CONFIG_MESH
20695b9c547cSRui Paulo 		if (iface->mconf != NULL) {
20705b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
20715b9c547cSRui Paulo 				   "%s: Mesh configuration will be applied while joining the mesh network",
20725b9c547cSRui Paulo 				   iface->bss[0]->conf->iface);
20735b9c547cSRui Paulo 			delay_apply_cfg = 1;
20745b9c547cSRui Paulo 		}
20755b9c547cSRui Paulo #endif /* CONFIG_MESH */
20765b9c547cSRui Paulo 
20775b9c547cSRui Paulo 		if (!delay_apply_cfg &&
20785b9c547cSRui Paulo 		    hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
2079e28a4053SRui Paulo 				     hapd->iconf->channel,
2080c1d255d3SCy Schubert 				     hapd->iconf->enable_edmg,
2081c1d255d3SCy Schubert 				     hapd->iconf->edmg_channel,
2082e28a4053SRui Paulo 				     hapd->iconf->ieee80211n,
20835b9c547cSRui Paulo 				     hapd->iconf->ieee80211ac,
2084206b73d0SCy Schubert 				     hapd->iconf->ieee80211ax,
20855b9c547cSRui Paulo 				     hapd->iconf->secondary_channel,
2086206b73d0SCy Schubert 				     hostapd_get_oper_chwidth(hapd->iconf),
2087206b73d0SCy Schubert 				     hostapd_get_oper_centr_freq_seg0_idx(
2088206b73d0SCy Schubert 					     hapd->iconf),
2089206b73d0SCy Schubert 				     hostapd_get_oper_centr_freq_seg1_idx(
2090206b73d0SCy Schubert 					     hapd->iconf))) {
2091e28a4053SRui Paulo 			wpa_printf(MSG_ERROR, "Could not set channel for "
2092e28a4053SRui Paulo 				   "kernel driver");
20935b9c547cSRui Paulo 			goto fail;
2094e28a4053SRui Paulo 		}
2095e28a4053SRui Paulo 	}
2096e28a4053SRui Paulo 
2097f05cddf9SRui Paulo 	if (iface->current_mode) {
2098f05cddf9SRui Paulo 		if (hostapd_prepare_rates(iface, iface->current_mode)) {
2099f05cddf9SRui Paulo 			wpa_printf(MSG_ERROR, "Failed to prepare rates "
2100f05cddf9SRui Paulo 				   "table.");
2101f05cddf9SRui Paulo 			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2102f05cddf9SRui Paulo 				       HOSTAPD_LEVEL_WARNING,
2103f05cddf9SRui Paulo 				       "Failed to prepare rates table.");
21045b9c547cSRui Paulo 			goto fail;
2105f05cddf9SRui Paulo 		}
2106f05cddf9SRui Paulo 	}
2107f05cddf9SRui Paulo 
21084bc52338SCy Schubert 	if (hapd->iconf->rts_threshold >= -1 &&
21094bc52338SCy Schubert 	    hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
21104bc52338SCy Schubert 	    hapd->iconf->rts_threshold >= -1) {
2111e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2112e28a4053SRui Paulo 			   "kernel driver");
21135b9c547cSRui Paulo 		goto fail;
2114e28a4053SRui Paulo 	}
2115e28a4053SRui Paulo 
21164bc52338SCy Schubert 	if (hapd->iconf->fragm_threshold >= -1 &&
21174bc52338SCy Schubert 	    hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
21184bc52338SCy Schubert 	    hapd->iconf->fragm_threshold != -1) {
2119e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2120e28a4053SRui Paulo 			   "for kernel driver");
21215b9c547cSRui Paulo 		goto fail;
2122e28a4053SRui Paulo 	}
2123e28a4053SRui Paulo 
2124e28a4053SRui Paulo 	prev_addr = hapd->own_addr;
2125e28a4053SRui Paulo 
2126e28a4053SRui Paulo 	for (j = 0; j < iface->num_bss; j++) {
2127e28a4053SRui Paulo 		hapd = iface->bss[j];
2128e28a4053SRui Paulo 		if (j)
2129e28a4053SRui Paulo 			os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
21305b9c547cSRui Paulo 		if (hostapd_setup_bss(hapd, j == 0)) {
21314bc52338SCy Schubert 			for (;;) {
21325b9c547cSRui Paulo 				hapd = iface->bss[j];
21335b9c547cSRui Paulo 				hostapd_bss_deinit_no_free(hapd);
21345b9c547cSRui Paulo 				hostapd_free_hapd_data(hapd);
21354bc52338SCy Schubert 				if (j == 0)
21364bc52338SCy Schubert 					break;
21374bc52338SCy Schubert 				j--;
21384bc52338SCy Schubert 			}
21395b9c547cSRui Paulo 			goto fail;
21405b9c547cSRui Paulo 		}
2141780fb4a2SCy Schubert 		if (is_zero_ether_addr(hapd->conf->bssid))
2142e28a4053SRui Paulo 			prev_addr = hapd->own_addr;
2143e28a4053SRui Paulo 	}
21445b9c547cSRui Paulo 	hapd = iface->bss[0];
2145e28a4053SRui Paulo 
2146e28a4053SRui Paulo 	hostapd_tx_queue_params(iface);
2147e28a4053SRui Paulo 
2148e28a4053SRui Paulo 	ap_list_init(iface);
2149e28a4053SRui Paulo 
21505b9c547cSRui Paulo 	hostapd_set_acl(hapd);
21515b9c547cSRui Paulo 
2152e28a4053SRui Paulo 	if (hostapd_driver_commit(hapd) < 0) {
2153e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2154e28a4053SRui Paulo 			   "configuration", __func__);
21555b9c547cSRui Paulo 		goto fail;
2156e28a4053SRui Paulo 	}
2157e28a4053SRui Paulo 
2158f05cddf9SRui Paulo 	/*
2159f05cddf9SRui Paulo 	 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2160f05cddf9SRui Paulo 	 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2161f05cddf9SRui Paulo 	 * mode), the interface is up only after driver_commit, so initialize
2162f05cddf9SRui Paulo 	 * WPS after driver_commit.
2163f05cddf9SRui Paulo 	 */
2164f05cddf9SRui Paulo 	for (j = 0; j < iface->num_bss; j++) {
2165f05cddf9SRui Paulo 		if (hostapd_init_wps_complete(iface->bss[j]))
21665b9c547cSRui Paulo 			goto fail;
2167f05cddf9SRui Paulo 	}
2168f05cddf9SRui Paulo 
21695b9c547cSRui Paulo 	if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
21705b9c547cSRui Paulo 	    !res_dfs_offload) {
21715b9c547cSRui Paulo 		/*
21725b9c547cSRui Paulo 		 * If freq is DFS, and DFS is offloaded to the driver, then wait
21735b9c547cSRui Paulo 		 * for CAC to complete.
21745b9c547cSRui Paulo 		 */
21755b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
21765b9c547cSRui Paulo 		return res_dfs_offload;
21775b9c547cSRui Paulo 	}
21785b9c547cSRui Paulo 
21795b9c547cSRui Paulo #ifdef NEED_AP_MLME
21805b9c547cSRui Paulo dfs_offload:
21815b9c547cSRui Paulo #endif /* NEED_AP_MLME */
2182325151a3SRui Paulo 
2183325151a3SRui Paulo #ifdef CONFIG_FST
2184325151a3SRui Paulo 	if (hapd->iconf->fst_cfg.group_id[0]) {
2185325151a3SRui Paulo 		struct fst_wpa_obj iface_obj;
2186325151a3SRui Paulo 
2187325151a3SRui Paulo 		fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2188325151a3SRui Paulo 		iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2189325151a3SRui Paulo 					&iface_obj, &hapd->iconf->fst_cfg);
2190325151a3SRui Paulo 		if (!iface->fst) {
2191325151a3SRui Paulo 			wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2192325151a3SRui Paulo 				   hapd->iconf->fst_cfg.group_id);
2193325151a3SRui Paulo 			goto fail;
2194325151a3SRui Paulo 		}
2195325151a3SRui Paulo 	}
2196325151a3SRui Paulo #endif /* CONFIG_FST */
2197325151a3SRui Paulo 
21985b9c547cSRui Paulo 	hostapd_set_state(iface, HAPD_IFACE_ENABLED);
219985732ac8SCy Schubert 	hostapd_owe_update_trans(iface);
2200206b73d0SCy Schubert 	airtime_policy_update_init(iface);
22015b9c547cSRui Paulo 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
2202f05cddf9SRui Paulo 	if (hapd->setup_complete_cb)
2203f05cddf9SRui Paulo 		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2204f05cddf9SRui Paulo 
2205c1d255d3SCy Schubert #ifdef CONFIG_MESH
2206c1d255d3SCy Schubert 	if (delay_apply_cfg && !iface->mconf) {
2207c1d255d3SCy Schubert 		wpa_printf(MSG_ERROR, "Error while completing mesh init");
2208c1d255d3SCy Schubert 		goto fail;
2209c1d255d3SCy Schubert 	}
2210c1d255d3SCy Schubert #endif /* CONFIG_MESH */
2211c1d255d3SCy Schubert 
2212e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2213e28a4053SRui Paulo 		   iface->bss[0]->conf->iface);
22145b9c547cSRui Paulo 	if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
22155b9c547cSRui Paulo 		iface->interfaces->terminate_on_error--;
2216e28a4053SRui Paulo 
2217780fb4a2SCy Schubert 	for (j = 0; j < iface->num_bss; j++)
22184bc52338SCy Schubert 		hostapd_neighbor_set_own_report(iface->bss[j]);
2219780fb4a2SCy Schubert 
2220e28a4053SRui Paulo 	return 0;
22215b9c547cSRui Paulo 
22225b9c547cSRui Paulo fail:
22235b9c547cSRui Paulo 	wpa_printf(MSG_ERROR, "Interface initialization failed");
22245b9c547cSRui Paulo 	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
22255b9c547cSRui Paulo 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2226325151a3SRui Paulo #ifdef CONFIG_FST
2227325151a3SRui Paulo 	if (iface->fst) {
2228325151a3SRui Paulo 		fst_detach(iface->fst);
2229325151a3SRui Paulo 		iface->fst = NULL;
2230325151a3SRui Paulo 	}
2231325151a3SRui Paulo #endif /* CONFIG_FST */
223285732ac8SCy Schubert 
223385732ac8SCy Schubert 	if (iface->interfaces && iface->interfaces->terminate_on_error) {
22345b9c547cSRui Paulo 		eloop_terminate();
223585732ac8SCy Schubert 	} else if (hapd->setup_complete_cb) {
223685732ac8SCy Schubert 		/*
223785732ac8SCy Schubert 		 * Calling hapd->setup_complete_cb directly may cause iface
223885732ac8SCy Schubert 		 * deinitialization which may be accessed later by the caller.
223985732ac8SCy Schubert 		 */
224085732ac8SCy Schubert 		eloop_register_timeout(0, 0,
224185732ac8SCy Schubert 				       hostapd_interface_setup_failure_handler,
224285732ac8SCy Schubert 				       iface, NULL);
224385732ac8SCy Schubert 	}
224485732ac8SCy Schubert 
22455b9c547cSRui Paulo 	return -1;
2246e28a4053SRui Paulo }
2247e28a4053SRui Paulo 
2248e28a4053SRui Paulo 
2249e28a4053SRui Paulo /**
2250780fb4a2SCy Schubert  * hostapd_setup_interface_complete - Complete interface setup
2251780fb4a2SCy Schubert  *
2252780fb4a2SCy Schubert  * This function is called when previous steps in the interface setup has been
2253780fb4a2SCy Schubert  * completed. This can also start operations, e.g., DFS, that will require
2254780fb4a2SCy Schubert  * additional processing before interface is ready to be enabled. Such
2255780fb4a2SCy Schubert  * operations will call this function from eloop callbacks when finished.
2256780fb4a2SCy Schubert  */
hostapd_setup_interface_complete(struct hostapd_iface * iface,int err)2257780fb4a2SCy Schubert int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2258780fb4a2SCy Schubert {
2259780fb4a2SCy Schubert 	struct hapd_interfaces *interfaces = iface->interfaces;
2260780fb4a2SCy Schubert 	struct hostapd_data *hapd = iface->bss[0];
2261780fb4a2SCy Schubert 	unsigned int i;
2262780fb4a2SCy Schubert 	int not_ready_in_sync_ifaces = 0;
2263780fb4a2SCy Schubert 
2264780fb4a2SCy Schubert 	if (!iface->need_to_start_in_sync)
2265780fb4a2SCy Schubert 		return hostapd_setup_interface_complete_sync(iface, err);
2266780fb4a2SCy Schubert 
2267780fb4a2SCy Schubert 	if (err) {
2268780fb4a2SCy Schubert 		wpa_printf(MSG_ERROR, "Interface initialization failed");
2269780fb4a2SCy Schubert 		hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2270780fb4a2SCy Schubert 		iface->need_to_start_in_sync = 0;
2271780fb4a2SCy Schubert 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2272780fb4a2SCy Schubert 		if (interfaces && interfaces->terminate_on_error)
2273780fb4a2SCy Schubert 			eloop_terminate();
2274780fb4a2SCy Schubert 		return -1;
2275780fb4a2SCy Schubert 	}
2276780fb4a2SCy Schubert 
2277780fb4a2SCy Schubert 	if (iface->ready_to_start_in_sync) {
2278780fb4a2SCy Schubert 		/* Already in ready and waiting. should never happpen */
2279780fb4a2SCy Schubert 		return 0;
2280780fb4a2SCy Schubert 	}
2281780fb4a2SCy Schubert 
2282780fb4a2SCy Schubert 	for (i = 0; i < interfaces->count; i++) {
2283780fb4a2SCy Schubert 		if (interfaces->iface[i]->need_to_start_in_sync &&
2284780fb4a2SCy Schubert 		    !interfaces->iface[i]->ready_to_start_in_sync)
2285780fb4a2SCy Schubert 			not_ready_in_sync_ifaces++;
2286780fb4a2SCy Schubert 	}
2287780fb4a2SCy Schubert 
2288780fb4a2SCy Schubert 	/*
2289780fb4a2SCy Schubert 	 * Check if this is the last interface, if yes then start all the other
2290780fb4a2SCy Schubert 	 * waiting interfaces. If not, add this interface to the waiting list.
2291780fb4a2SCy Schubert 	 */
2292780fb4a2SCy Schubert 	if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2293780fb4a2SCy Schubert 		/*
2294780fb4a2SCy Schubert 		 * If this interface went through CAC, do not synchronize, just
2295780fb4a2SCy Schubert 		 * start immediately.
2296780fb4a2SCy Schubert 		 */
2297780fb4a2SCy Schubert 		iface->need_to_start_in_sync = 0;
2298780fb4a2SCy Schubert 		wpa_printf(MSG_INFO,
2299780fb4a2SCy Schubert 			   "%s: Finished CAC - bypass sync and start interface",
2300780fb4a2SCy Schubert 			   iface->bss[0]->conf->iface);
2301780fb4a2SCy Schubert 		return hostapd_setup_interface_complete_sync(iface, err);
2302780fb4a2SCy Schubert 	}
2303780fb4a2SCy Schubert 
2304780fb4a2SCy Schubert 	if (not_ready_in_sync_ifaces > 1) {
2305780fb4a2SCy Schubert 		/* need to wait as there are other interfaces still coming up */
2306780fb4a2SCy Schubert 		iface->ready_to_start_in_sync = 1;
2307780fb4a2SCy Schubert 		wpa_printf(MSG_INFO,
2308780fb4a2SCy Schubert 			   "%s: Interface waiting to sync with other interfaces",
2309780fb4a2SCy Schubert 			   iface->bss[0]->conf->iface);
2310780fb4a2SCy Schubert 		return 0;
2311780fb4a2SCy Schubert 	}
2312780fb4a2SCy Schubert 
2313780fb4a2SCy Schubert 	wpa_printf(MSG_INFO,
2314780fb4a2SCy Schubert 		   "%s: Last interface to sync - starting all interfaces",
2315780fb4a2SCy Schubert 		   iface->bss[0]->conf->iface);
2316780fb4a2SCy Schubert 	iface->need_to_start_in_sync = 0;
2317780fb4a2SCy Schubert 	hostapd_setup_interface_complete_sync(iface, err);
2318780fb4a2SCy Schubert 	for (i = 0; i < interfaces->count; i++) {
2319780fb4a2SCy Schubert 		if (interfaces->iface[i]->need_to_start_in_sync &&
2320780fb4a2SCy Schubert 		    interfaces->iface[i]->ready_to_start_in_sync) {
2321780fb4a2SCy Schubert 			hostapd_setup_interface_complete_sync(
2322780fb4a2SCy Schubert 				interfaces->iface[i], 0);
2323780fb4a2SCy Schubert 			/* Only once the interfaces are sync started */
2324780fb4a2SCy Schubert 			interfaces->iface[i]->need_to_start_in_sync = 0;
2325780fb4a2SCy Schubert 		}
2326780fb4a2SCy Schubert 	}
2327780fb4a2SCy Schubert 
2328780fb4a2SCy Schubert 	return 0;
2329780fb4a2SCy Schubert }
2330780fb4a2SCy Schubert 
2331780fb4a2SCy Schubert 
2332780fb4a2SCy Schubert /**
2333e28a4053SRui Paulo  * hostapd_setup_interface - Setup of an interface
2334e28a4053SRui Paulo  * @iface: Pointer to interface data.
2335e28a4053SRui Paulo  * Returns: 0 on success, -1 on failure
2336e28a4053SRui Paulo  *
2337e28a4053SRui Paulo  * Initializes the driver interface, validates the configuration,
2338e28a4053SRui Paulo  * and sets driver parameters based on the configuration.
2339e28a4053SRui Paulo  * Flushes old stations, sets the channel, encryption,
2340e28a4053SRui Paulo  * beacons, and WDS links based on the configuration.
23415b9c547cSRui Paulo  *
23425b9c547cSRui Paulo  * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
23435b9c547cSRui Paulo  * or DFS operations, this function returns 0 before such operations have been
23445b9c547cSRui Paulo  * completed. The pending operations are registered into eloop and will be
23455b9c547cSRui Paulo  * completed from eloop callbacks. Those callbacks end up calling
23465b9c547cSRui Paulo  * hostapd_setup_interface_complete() once setup has been completed.
2347e28a4053SRui Paulo  */
hostapd_setup_interface(struct hostapd_iface * iface)2348e28a4053SRui Paulo int hostapd_setup_interface(struct hostapd_iface *iface)
2349e28a4053SRui Paulo {
2350e28a4053SRui Paulo 	int ret;
2351e28a4053SRui Paulo 
2352c1d255d3SCy Schubert 	if (!iface->conf)
2353c1d255d3SCy Schubert 		return -1;
2354e28a4053SRui Paulo 	ret = setup_interface(iface);
2355e28a4053SRui Paulo 	if (ret) {
2356e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2357c1d255d3SCy Schubert 			   iface->conf->bss[0]->iface);
2358e28a4053SRui Paulo 		return -1;
2359e28a4053SRui Paulo 	}
2360e28a4053SRui Paulo 
2361e28a4053SRui Paulo 	return 0;
2362e28a4053SRui Paulo }
2363e28a4053SRui Paulo 
2364e28a4053SRui Paulo 
2365e28a4053SRui Paulo /**
2366e28a4053SRui Paulo  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2367e28a4053SRui Paulo  * @hapd_iface: Pointer to interface data
2368e28a4053SRui Paulo  * @conf: Pointer to per-interface configuration
2369e28a4053SRui Paulo  * @bss: Pointer to per-BSS configuration for this BSS
2370e28a4053SRui Paulo  * Returns: Pointer to allocated BSS data
2371e28a4053SRui Paulo  *
2372e28a4053SRui Paulo  * This function is used to allocate per-BSS data structure. This data will be
2373e28a4053SRui Paulo  * freed after hostapd_cleanup() is called for it during interface
2374e28a4053SRui Paulo  * deinitialization.
2375e28a4053SRui Paulo  */
2376e28a4053SRui Paulo struct hostapd_data *
hostapd_alloc_bss_data(struct hostapd_iface * hapd_iface,struct hostapd_config * conf,struct hostapd_bss_config * bss)2377e28a4053SRui Paulo hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2378e28a4053SRui Paulo 		       struct hostapd_config *conf,
2379e28a4053SRui Paulo 		       struct hostapd_bss_config *bss)
2380e28a4053SRui Paulo {
2381e28a4053SRui Paulo 	struct hostapd_data *hapd;
2382e28a4053SRui Paulo 
2383e28a4053SRui Paulo 	hapd = os_zalloc(sizeof(*hapd));
2384e28a4053SRui Paulo 	if (hapd == NULL)
2385e28a4053SRui Paulo 		return NULL;
2386e28a4053SRui Paulo 
2387e28a4053SRui Paulo 	hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2388e28a4053SRui Paulo 	hapd->iconf = conf;
2389e28a4053SRui Paulo 	hapd->conf = bss;
2390e28a4053SRui Paulo 	hapd->iface = hapd_iface;
239185732ac8SCy Schubert 	if (conf)
239285732ac8SCy Schubert 		hapd->driver = conf->driver;
2393f05cddf9SRui Paulo 	hapd->ctrl_sock = -1;
2394780fb4a2SCy Schubert 	dl_list_init(&hapd->ctrl_dst);
2395780fb4a2SCy Schubert 	dl_list_init(&hapd->nr_db);
239685732ac8SCy Schubert 	hapd->dhcp_sock = -1;
239785732ac8SCy Schubert #ifdef CONFIG_IEEE80211R_AP
239885732ac8SCy Schubert 	dl_list_init(&hapd->l2_queue);
239985732ac8SCy Schubert 	dl_list_init(&hapd->l2_oui_queue);
240085732ac8SCy Schubert #endif /* CONFIG_IEEE80211R_AP */
24014bc52338SCy Schubert #ifdef CONFIG_SAE
24024bc52338SCy Schubert 	dl_list_init(&hapd->sae_commit_queue);
24034bc52338SCy Schubert #endif /* CONFIG_SAE */
2404e28a4053SRui Paulo 
2405e28a4053SRui Paulo 	return hapd;
2406e28a4053SRui Paulo }
2407e28a4053SRui Paulo 
2408e28a4053SRui Paulo 
hostapd_bss_deinit(struct hostapd_data * hapd)24095b9c547cSRui Paulo static void hostapd_bss_deinit(struct hostapd_data *hapd)
24105b9c547cSRui Paulo {
2411780fb4a2SCy Schubert 	if (!hapd)
2412780fb4a2SCy Schubert 		return;
24135b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
24144bc52338SCy Schubert 		   hapd->conf ? hapd->conf->iface : "N/A");
24155b9c547cSRui Paulo 	hostapd_bss_deinit_no_free(hapd);
24165b9c547cSRui Paulo 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2417206b73d0SCy Schubert #ifdef CONFIG_SQLITE
2418206b73d0SCy Schubert 	if (hapd->rad_attr_db) {
2419206b73d0SCy Schubert 		sqlite3_close(hapd->rad_attr_db);
2420206b73d0SCy Schubert 		hapd->rad_attr_db = NULL;
2421206b73d0SCy Schubert 	}
2422206b73d0SCy Schubert #endif /* CONFIG_SQLITE */
24235b9c547cSRui Paulo 	hostapd_cleanup(hapd);
24245b9c547cSRui Paulo }
24255b9c547cSRui Paulo 
24265b9c547cSRui Paulo 
hostapd_interface_deinit(struct hostapd_iface * iface)2427e28a4053SRui Paulo void hostapd_interface_deinit(struct hostapd_iface *iface)
2428e28a4053SRui Paulo {
24295b9c547cSRui Paulo 	int j;
2430e28a4053SRui Paulo 
24315b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2432e28a4053SRui Paulo 	if (iface == NULL)
2433e28a4053SRui Paulo 		return;
2434e28a4053SRui Paulo 
24355b9c547cSRui Paulo 	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
24365b9c547cSRui Paulo 
24375b9c547cSRui Paulo 	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
24385b9c547cSRui Paulo 	iface->wait_channel_update = 0;
24395b9c547cSRui Paulo 
2440325151a3SRui Paulo #ifdef CONFIG_FST
2441325151a3SRui Paulo 	if (iface->fst) {
2442325151a3SRui Paulo 		fst_detach(iface->fst);
2443325151a3SRui Paulo 		iface->fst = NULL;
2444325151a3SRui Paulo 	}
2445325151a3SRui Paulo #endif /* CONFIG_FST */
2446325151a3SRui Paulo 
24474bc52338SCy Schubert 	for (j = (int) iface->num_bss - 1; j >= 0; j--) {
2448780fb4a2SCy Schubert 		if (!iface->bss)
2449780fb4a2SCy Schubert 			break;
24505b9c547cSRui Paulo 		hostapd_bss_deinit(iface->bss[j]);
2451e28a4053SRui Paulo 	}
245285732ac8SCy Schubert 
245385732ac8SCy Schubert #ifdef NEED_AP_MLME
245485732ac8SCy Schubert 	hostapd_stop_setup_timers(iface);
245585732ac8SCy Schubert 	eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
245685732ac8SCy Schubert #endif /* NEED_AP_MLME */
2457780fb4a2SCy Schubert }
2458e28a4053SRui Paulo 
2459e28a4053SRui Paulo 
hostapd_interface_free(struct hostapd_iface * iface)2460e28a4053SRui Paulo void hostapd_interface_free(struct hostapd_iface *iface)
2461e28a4053SRui Paulo {
2462e28a4053SRui Paulo 	size_t j;
24635b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
24645b9c547cSRui Paulo 	for (j = 0; j < iface->num_bss; j++) {
2465780fb4a2SCy Schubert 		if (!iface->bss)
2466780fb4a2SCy Schubert 			break;
24675b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "%s: free hapd %p",
24685b9c547cSRui Paulo 			   __func__, iface->bss[j]);
2469e28a4053SRui Paulo 		os_free(iface->bss[j]);
24705b9c547cSRui Paulo 	}
2471e28a4053SRui Paulo 	hostapd_cleanup_iface(iface);
2472e28a4053SRui Paulo }
2473e28a4053SRui Paulo 
2474e28a4053SRui Paulo 
hostapd_alloc_iface(void)2475780fb4a2SCy Schubert struct hostapd_iface * hostapd_alloc_iface(void)
2476780fb4a2SCy Schubert {
2477780fb4a2SCy Schubert 	struct hostapd_iface *hapd_iface;
2478780fb4a2SCy Schubert 
2479780fb4a2SCy Schubert 	hapd_iface = os_zalloc(sizeof(*hapd_iface));
2480780fb4a2SCy Schubert 	if (!hapd_iface)
2481780fb4a2SCy Schubert 		return NULL;
2482780fb4a2SCy Schubert 
2483780fb4a2SCy Schubert 	dl_list_init(&hapd_iface->sta_seen);
2484780fb4a2SCy Schubert 
2485780fb4a2SCy Schubert 	return hapd_iface;
2486780fb4a2SCy Schubert }
2487780fb4a2SCy Schubert 
2488780fb4a2SCy Schubert 
24895b9c547cSRui Paulo /**
24905b9c547cSRui Paulo  * hostapd_init - Allocate and initialize per-interface data
24915b9c547cSRui Paulo  * @config_file: Path to the configuration file
24925b9c547cSRui Paulo  * Returns: Pointer to the allocated interface data or %NULL on failure
24935b9c547cSRui Paulo  *
24945b9c547cSRui Paulo  * This function is used to allocate main data structures for per-interface
24955b9c547cSRui Paulo  * data. The allocated data buffer will be freed by calling
24965b9c547cSRui Paulo  * hostapd_cleanup_iface().
24975b9c547cSRui Paulo  */
hostapd_init(struct hapd_interfaces * interfaces,const char * config_file)24985b9c547cSRui Paulo struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
24995b9c547cSRui Paulo 				    const char *config_file)
25005b9c547cSRui Paulo {
25015b9c547cSRui Paulo 	struct hostapd_iface *hapd_iface = NULL;
25025b9c547cSRui Paulo 	struct hostapd_config *conf = NULL;
25035b9c547cSRui Paulo 	struct hostapd_data *hapd;
25045b9c547cSRui Paulo 	size_t i;
25055b9c547cSRui Paulo 
2506780fb4a2SCy Schubert 	hapd_iface = hostapd_alloc_iface();
25075b9c547cSRui Paulo 	if (hapd_iface == NULL)
25085b9c547cSRui Paulo 		goto fail;
25095b9c547cSRui Paulo 
25105b9c547cSRui Paulo 	hapd_iface->config_fname = os_strdup(config_file);
25115b9c547cSRui Paulo 	if (hapd_iface->config_fname == NULL)
25125b9c547cSRui Paulo 		goto fail;
25135b9c547cSRui Paulo 
25145b9c547cSRui Paulo 	conf = interfaces->config_read_cb(hapd_iface->config_fname);
25155b9c547cSRui Paulo 	if (conf == NULL)
25165b9c547cSRui Paulo 		goto fail;
25175b9c547cSRui Paulo 	hapd_iface->conf = conf;
25185b9c547cSRui Paulo 
25195b9c547cSRui Paulo 	hapd_iface->num_bss = conf->num_bss;
25205b9c547cSRui Paulo 	hapd_iface->bss = os_calloc(conf->num_bss,
25215b9c547cSRui Paulo 				    sizeof(struct hostapd_data *));
25225b9c547cSRui Paulo 	if (hapd_iface->bss == NULL)
25235b9c547cSRui Paulo 		goto fail;
25245b9c547cSRui Paulo 
25255b9c547cSRui Paulo 	for (i = 0; i < conf->num_bss; i++) {
25265b9c547cSRui Paulo 		hapd = hapd_iface->bss[i] =
25275b9c547cSRui Paulo 			hostapd_alloc_bss_data(hapd_iface, conf,
25285b9c547cSRui Paulo 					       conf->bss[i]);
25295b9c547cSRui Paulo 		if (hapd == NULL)
25305b9c547cSRui Paulo 			goto fail;
25315b9c547cSRui Paulo 		hapd->msg_ctx = hapd;
25325b9c547cSRui Paulo 	}
25335b9c547cSRui Paulo 
25345b9c547cSRui Paulo 	return hapd_iface;
25355b9c547cSRui Paulo 
25365b9c547cSRui Paulo fail:
25375b9c547cSRui Paulo 	wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
25385b9c547cSRui Paulo 		   config_file);
25395b9c547cSRui Paulo 	if (conf)
25405b9c547cSRui Paulo 		hostapd_config_free(conf);
25415b9c547cSRui Paulo 	if (hapd_iface) {
25425b9c547cSRui Paulo 		os_free(hapd_iface->config_fname);
25435b9c547cSRui Paulo 		os_free(hapd_iface->bss);
25445b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "%s: free iface %p",
25455b9c547cSRui Paulo 			   __func__, hapd_iface);
25465b9c547cSRui Paulo 		os_free(hapd_iface);
25475b9c547cSRui Paulo 	}
25485b9c547cSRui Paulo 	return NULL;
25495b9c547cSRui Paulo }
25505b9c547cSRui Paulo 
25515b9c547cSRui Paulo 
ifname_in_use(struct hapd_interfaces * interfaces,const char * ifname)25525b9c547cSRui Paulo static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
25535b9c547cSRui Paulo {
25545b9c547cSRui Paulo 	size_t i, j;
25555b9c547cSRui Paulo 
25565b9c547cSRui Paulo 	for (i = 0; i < interfaces->count; i++) {
25575b9c547cSRui Paulo 		struct hostapd_iface *iface = interfaces->iface[i];
25585b9c547cSRui Paulo 		for (j = 0; j < iface->num_bss; j++) {
25595b9c547cSRui Paulo 			struct hostapd_data *hapd = iface->bss[j];
25605b9c547cSRui Paulo 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
25615b9c547cSRui Paulo 				return 1;
25625b9c547cSRui Paulo 		}
25635b9c547cSRui Paulo 	}
25645b9c547cSRui Paulo 
25655b9c547cSRui Paulo 	return 0;
25665b9c547cSRui Paulo }
25675b9c547cSRui Paulo 
25685b9c547cSRui Paulo 
25695b9c547cSRui Paulo /**
25705b9c547cSRui Paulo  * hostapd_interface_init_bss - Read configuration file and init BSS data
25715b9c547cSRui Paulo  *
25725b9c547cSRui Paulo  * This function is used to parse configuration file for a BSS. This BSS is
25735b9c547cSRui Paulo  * added to an existing interface sharing the same radio (if any) or a new
25745b9c547cSRui Paulo  * interface is created if this is the first interface on a radio. This
25755b9c547cSRui Paulo  * allocate memory for the BSS. No actual driver operations are started.
25765b9c547cSRui Paulo  *
25775b9c547cSRui Paulo  * This is similar to hostapd_interface_init(), but for a case where the
25785b9c547cSRui Paulo  * configuration is used to add a single BSS instead of all BSSes for a radio.
25795b9c547cSRui Paulo  */
25805b9c547cSRui Paulo struct hostapd_iface *
hostapd_interface_init_bss(struct hapd_interfaces * interfaces,const char * phy,const char * config_fname,int debug)25815b9c547cSRui Paulo hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
25825b9c547cSRui Paulo 			   const char *config_fname, int debug)
25835b9c547cSRui Paulo {
25845b9c547cSRui Paulo 	struct hostapd_iface *new_iface = NULL, *iface = NULL;
25855b9c547cSRui Paulo 	struct hostapd_data *hapd;
25865b9c547cSRui Paulo 	int k;
25875b9c547cSRui Paulo 	size_t i, bss_idx;
25885b9c547cSRui Paulo 
25895b9c547cSRui Paulo 	if (!phy || !*phy)
25905b9c547cSRui Paulo 		return NULL;
25915b9c547cSRui Paulo 
25925b9c547cSRui Paulo 	for (i = 0; i < interfaces->count; i++) {
25935b9c547cSRui Paulo 		if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
25945b9c547cSRui Paulo 			iface = interfaces->iface[i];
25955b9c547cSRui Paulo 			break;
25965b9c547cSRui Paulo 		}
25975b9c547cSRui Paulo 	}
25985b9c547cSRui Paulo 
25995b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
26005b9c547cSRui Paulo 		   config_fname, phy, iface ? "" : " --> new PHY");
26015b9c547cSRui Paulo 	if (iface) {
26025b9c547cSRui Paulo 		struct hostapd_config *conf;
26035b9c547cSRui Paulo 		struct hostapd_bss_config **tmp_conf;
26045b9c547cSRui Paulo 		struct hostapd_data **tmp_bss;
26055b9c547cSRui Paulo 		struct hostapd_bss_config *bss;
26065b9c547cSRui Paulo 		const char *ifname;
26075b9c547cSRui Paulo 
26085b9c547cSRui Paulo 		/* Add new BSS to existing iface */
26095b9c547cSRui Paulo 		conf = interfaces->config_read_cb(config_fname);
26105b9c547cSRui Paulo 		if (conf == NULL)
26115b9c547cSRui Paulo 			return NULL;
26125b9c547cSRui Paulo 		if (conf->num_bss > 1) {
26135b9c547cSRui Paulo 			wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
26145b9c547cSRui Paulo 			hostapd_config_free(conf);
26155b9c547cSRui Paulo 			return NULL;
26165b9c547cSRui Paulo 		}
26175b9c547cSRui Paulo 
26185b9c547cSRui Paulo 		ifname = conf->bss[0]->iface;
26195b9c547cSRui Paulo 		if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
26205b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
26215b9c547cSRui Paulo 				   "Interface name %s already in use", ifname);
26225b9c547cSRui Paulo 			hostapd_config_free(conf);
26235b9c547cSRui Paulo 			return NULL;
26245b9c547cSRui Paulo 		}
26255b9c547cSRui Paulo 
26265b9c547cSRui Paulo 		tmp_conf = os_realloc_array(
26275b9c547cSRui Paulo 			iface->conf->bss, iface->conf->num_bss + 1,
26285b9c547cSRui Paulo 			sizeof(struct hostapd_bss_config *));
26295b9c547cSRui Paulo 		tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
26305b9c547cSRui Paulo 					   sizeof(struct hostapd_data *));
26315b9c547cSRui Paulo 		if (tmp_bss)
26325b9c547cSRui Paulo 			iface->bss = tmp_bss;
26335b9c547cSRui Paulo 		if (tmp_conf) {
26345b9c547cSRui Paulo 			iface->conf->bss = tmp_conf;
26355b9c547cSRui Paulo 			iface->conf->last_bss = tmp_conf[0];
26365b9c547cSRui Paulo 		}
26375b9c547cSRui Paulo 		if (tmp_bss == NULL || tmp_conf == NULL) {
26385b9c547cSRui Paulo 			hostapd_config_free(conf);
26395b9c547cSRui Paulo 			return NULL;
26405b9c547cSRui Paulo 		}
26415b9c547cSRui Paulo 		bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
26425b9c547cSRui Paulo 		iface->conf->num_bss++;
26435b9c547cSRui Paulo 
26445b9c547cSRui Paulo 		hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
26455b9c547cSRui Paulo 		if (hapd == NULL) {
26465b9c547cSRui Paulo 			iface->conf->num_bss--;
26475b9c547cSRui Paulo 			hostapd_config_free(conf);
26485b9c547cSRui Paulo 			return NULL;
26495b9c547cSRui Paulo 		}
26505b9c547cSRui Paulo 		iface->conf->last_bss = bss;
26515b9c547cSRui Paulo 		iface->bss[iface->num_bss] = hapd;
26525b9c547cSRui Paulo 		hapd->msg_ctx = hapd;
26535b9c547cSRui Paulo 
26545b9c547cSRui Paulo 		bss_idx = iface->num_bss++;
26555b9c547cSRui Paulo 		conf->num_bss--;
26565b9c547cSRui Paulo 		conf->bss[0] = NULL;
26575b9c547cSRui Paulo 		hostapd_config_free(conf);
26585b9c547cSRui Paulo 	} else {
26595b9c547cSRui Paulo 		/* Add a new iface with the first BSS */
26605b9c547cSRui Paulo 		new_iface = iface = hostapd_init(interfaces, config_fname);
26615b9c547cSRui Paulo 		if (!iface)
26625b9c547cSRui Paulo 			return NULL;
26635b9c547cSRui Paulo 		os_strlcpy(iface->phy, phy, sizeof(iface->phy));
26645b9c547cSRui Paulo 		iface->interfaces = interfaces;
26655b9c547cSRui Paulo 		bss_idx = 0;
26665b9c547cSRui Paulo 	}
26675b9c547cSRui Paulo 
26685b9c547cSRui Paulo 	for (k = 0; k < debug; k++) {
26695b9c547cSRui Paulo 		if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
26705b9c547cSRui Paulo 			iface->bss[bss_idx]->conf->logger_stdout_level--;
26715b9c547cSRui Paulo 	}
26725b9c547cSRui Paulo 
26735b9c547cSRui Paulo 	if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
26745b9c547cSRui Paulo 	    !hostapd_drv_none(iface->bss[bss_idx])) {
26755b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "Interface name not specified in %s",
26765b9c547cSRui Paulo 			   config_fname);
26775b9c547cSRui Paulo 		if (new_iface)
26785b9c547cSRui Paulo 			hostapd_interface_deinit_free(new_iface);
26795b9c547cSRui Paulo 		return NULL;
26805b9c547cSRui Paulo 	}
26815b9c547cSRui Paulo 
26825b9c547cSRui Paulo 	return iface;
26835b9c547cSRui Paulo }
26845b9c547cSRui Paulo 
2685f05cddf9SRui Paulo 
hostapd_interface_deinit_free(struct hostapd_iface * iface)2686f05cddf9SRui Paulo void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2687f05cddf9SRui Paulo {
2688f05cddf9SRui Paulo 	const struct wpa_driver_ops *driver;
2689f05cddf9SRui Paulo 	void *drv_priv;
26905b9c547cSRui Paulo 
26915b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2692f05cddf9SRui Paulo 	if (iface == NULL)
2693f05cddf9SRui Paulo 		return;
26945b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
26955b9c547cSRui Paulo 		   __func__, (unsigned int) iface->num_bss,
26965b9c547cSRui Paulo 		   (unsigned int) iface->conf->num_bss);
2697f05cddf9SRui Paulo 	driver = iface->bss[0]->driver;
2698f05cddf9SRui Paulo 	drv_priv = iface->bss[0]->drv_priv;
2699f05cddf9SRui Paulo 	hostapd_interface_deinit(iface);
27005b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
27015b9c547cSRui Paulo 		   __func__, driver, drv_priv);
27025b9c547cSRui Paulo 	if (driver && driver->hapd_deinit && drv_priv) {
2703f05cddf9SRui Paulo 		driver->hapd_deinit(drv_priv);
27045b9c547cSRui Paulo 		iface->bss[0]->drv_priv = NULL;
27055b9c547cSRui Paulo 	}
2706f05cddf9SRui Paulo 	hostapd_interface_free(iface);
2707f05cddf9SRui Paulo }
2708f05cddf9SRui Paulo 
2709f05cddf9SRui Paulo 
hostapd_deinit_driver(const struct wpa_driver_ops * driver,void * drv_priv,struct hostapd_iface * hapd_iface)27105b9c547cSRui Paulo static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
27115b9c547cSRui Paulo 				  void *drv_priv,
27125b9c547cSRui Paulo 				  struct hostapd_iface *hapd_iface)
27135b9c547cSRui Paulo {
27145b9c547cSRui Paulo 	size_t j;
27155b9c547cSRui Paulo 
27165b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
27175b9c547cSRui Paulo 		   __func__, driver, drv_priv);
27185b9c547cSRui Paulo 	if (driver && driver->hapd_deinit && drv_priv) {
27195b9c547cSRui Paulo 		driver->hapd_deinit(drv_priv);
27205b9c547cSRui Paulo 		for (j = 0; j < hapd_iface->num_bss; j++) {
27215b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
27225b9c547cSRui Paulo 				   __func__, (int) j,
27235b9c547cSRui Paulo 				   hapd_iface->bss[j]->drv_priv);
2724206b73d0SCy Schubert 			if (hapd_iface->bss[j]->drv_priv == drv_priv) {
27255b9c547cSRui Paulo 				hapd_iface->bss[j]->drv_priv = NULL;
2726206b73d0SCy Schubert 				hapd_iface->extended_capa = NULL;
2727206b73d0SCy Schubert 				hapd_iface->extended_capa_mask = NULL;
2728206b73d0SCy Schubert 				hapd_iface->extended_capa_len = 0;
2729206b73d0SCy Schubert 			}
27305b9c547cSRui Paulo 		}
27315b9c547cSRui Paulo 	}
27325b9c547cSRui Paulo }
27335b9c547cSRui Paulo 
27345b9c547cSRui Paulo 
hostapd_enable_iface(struct hostapd_iface * hapd_iface)2735f05cddf9SRui Paulo int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2736f05cddf9SRui Paulo {
27375b9c547cSRui Paulo 	size_t j;
27385b9c547cSRui Paulo 
2739c1d255d3SCy Schubert 	if (!hapd_iface)
2740c1d255d3SCy Schubert 		return -1;
2741c1d255d3SCy Schubert 
2742c1d255d3SCy Schubert 	if (hapd_iface->enable_iface_cb)
2743c1d255d3SCy Schubert 		return hapd_iface->enable_iface_cb(hapd_iface);
2744c1d255d3SCy Schubert 
2745f05cddf9SRui Paulo 	if (hapd_iface->bss[0]->drv_priv != NULL) {
2746f05cddf9SRui Paulo 		wpa_printf(MSG_ERROR, "Interface %s already enabled",
27475b9c547cSRui Paulo 			   hapd_iface->conf->bss[0]->iface);
2748f05cddf9SRui Paulo 		return -1;
2749f05cddf9SRui Paulo 	}
2750f05cddf9SRui Paulo 
2751f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "Enable interface %s",
27525b9c547cSRui Paulo 		   hapd_iface->conf->bss[0]->iface);
27535b9c547cSRui Paulo 
27545b9c547cSRui Paulo 	for (j = 0; j < hapd_iface->num_bss; j++)
27555b9c547cSRui Paulo 		hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
27565b9c547cSRui Paulo 	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
27575b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
27585b9c547cSRui Paulo 		return -1;
27595b9c547cSRui Paulo 	}
2760f05cddf9SRui Paulo 
2761f05cddf9SRui Paulo 	if (hapd_iface->interfaces == NULL ||
2762f05cddf9SRui Paulo 	    hapd_iface->interfaces->driver_init == NULL ||
27635b9c547cSRui Paulo 	    hapd_iface->interfaces->driver_init(hapd_iface))
27645b9c547cSRui Paulo 		return -1;
27655b9c547cSRui Paulo 
27665b9c547cSRui Paulo 	if (hostapd_setup_interface(hapd_iface)) {
27675b9c547cSRui Paulo 		hostapd_deinit_driver(hapd_iface->bss[0]->driver,
27685b9c547cSRui Paulo 				      hapd_iface->bss[0]->drv_priv,
27695b9c547cSRui Paulo 				      hapd_iface);
2770f05cddf9SRui Paulo 		return -1;
2771f05cddf9SRui Paulo 	}
27725b9c547cSRui Paulo 
2773f05cddf9SRui Paulo 	return 0;
2774f05cddf9SRui Paulo }
2775f05cddf9SRui Paulo 
2776f05cddf9SRui Paulo 
hostapd_reload_iface(struct hostapd_iface * hapd_iface)2777f05cddf9SRui Paulo int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2778f05cddf9SRui Paulo {
2779f05cddf9SRui Paulo 	size_t j;
2780f05cddf9SRui Paulo 
2781f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "Reload interface %s",
27825b9c547cSRui Paulo 		   hapd_iface->conf->bss[0]->iface);
27835b9c547cSRui Paulo 	for (j = 0; j < hapd_iface->num_bss; j++)
27845b9c547cSRui Paulo 		hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
27855b9c547cSRui Paulo 	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
27865b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "Updated configuration is invalid");
27875b9c547cSRui Paulo 		return -1;
2788f05cddf9SRui Paulo 	}
27895b9c547cSRui Paulo 	hostapd_clear_old(hapd_iface);
27905b9c547cSRui Paulo 	for (j = 0; j < hapd_iface->num_bss; j++)
27915b9c547cSRui Paulo 		hostapd_reload_bss(hapd_iface->bss[j]);
27925b9c547cSRui Paulo 
2793f05cddf9SRui Paulo 	return 0;
2794f05cddf9SRui Paulo }
2795f05cddf9SRui Paulo 
2796f05cddf9SRui Paulo 
hostapd_disable_iface(struct hostapd_iface * hapd_iface)2797f05cddf9SRui Paulo int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2798f05cddf9SRui Paulo {
2799f05cddf9SRui Paulo 	size_t j;
2800f05cddf9SRui Paulo 	const struct wpa_driver_ops *driver;
2801f05cddf9SRui Paulo 	void *drv_priv;
2802f05cddf9SRui Paulo 
2803f05cddf9SRui Paulo 	if (hapd_iface == NULL)
2804f05cddf9SRui Paulo 		return -1;
28055b9c547cSRui Paulo 
2806c1d255d3SCy Schubert 	if (hapd_iface->disable_iface_cb)
2807c1d255d3SCy Schubert 		return hapd_iface->disable_iface_cb(hapd_iface);
2808c1d255d3SCy Schubert 
28095b9c547cSRui Paulo 	if (hapd_iface->bss[0]->drv_priv == NULL) {
28105b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "Interface %s already disabled",
28115b9c547cSRui Paulo 			   hapd_iface->conf->bss[0]->iface);
28125b9c547cSRui Paulo 		return -1;
28135b9c547cSRui Paulo 	}
28145b9c547cSRui Paulo 
28155b9c547cSRui Paulo 	wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2816f05cddf9SRui Paulo 	driver = hapd_iface->bss[0]->driver;
2817f05cddf9SRui Paulo 	drv_priv = hapd_iface->bss[0]->drv_priv;
2818f05cddf9SRui Paulo 
28195b9c547cSRui Paulo 	hapd_iface->driver_ap_teardown =
28205b9c547cSRui Paulo 		!!(hapd_iface->drv_flags &
28215b9c547cSRui Paulo 		   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
28225b9c547cSRui Paulo 
282385732ac8SCy Schubert #ifdef NEED_AP_MLME
282485732ac8SCy Schubert 	for (j = 0; j < hapd_iface->num_bss; j++)
282585732ac8SCy Schubert 		hostapd_cleanup_cs_params(hapd_iface->bss[j]);
282685732ac8SCy Schubert #endif /* NEED_AP_MLME */
282785732ac8SCy Schubert 
28285b9c547cSRui Paulo 	/* same as hostapd_interface_deinit without deinitializing ctrl-iface */
2829f05cddf9SRui Paulo 	for (j = 0; j < hapd_iface->num_bss; j++) {
2830f05cddf9SRui Paulo 		struct hostapd_data *hapd = hapd_iface->bss[j];
28315b9c547cSRui Paulo 		hostapd_bss_deinit_no_free(hapd);
2832f05cddf9SRui Paulo 		hostapd_free_hapd_data(hapd);
2833f05cddf9SRui Paulo 	}
2834f05cddf9SRui Paulo 
28355b9c547cSRui Paulo 	hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2836f05cddf9SRui Paulo 
2837f05cddf9SRui Paulo 	/* From hostapd_cleanup_iface: These were initialized in
2838f05cddf9SRui Paulo 	 * hostapd_setup_interface and hostapd_setup_interface_complete
2839f05cddf9SRui Paulo 	 */
2840f05cddf9SRui Paulo 	hostapd_cleanup_iface_partial(hapd_iface);
2841f05cddf9SRui Paulo 
28425b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interface %s disabled",
28435b9c547cSRui Paulo 		   hapd_iface->bss[0]->conf->iface);
28445b9c547cSRui Paulo 	hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2845f05cddf9SRui Paulo 	return 0;
2846f05cddf9SRui Paulo }
2847f05cddf9SRui Paulo 
2848f05cddf9SRui Paulo 
2849f05cddf9SRui Paulo static struct hostapd_iface *
hostapd_iface_alloc(struct hapd_interfaces * interfaces)2850f05cddf9SRui Paulo hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2851f05cddf9SRui Paulo {
2852f05cddf9SRui Paulo 	struct hostapd_iface **iface, *hapd_iface;
2853f05cddf9SRui Paulo 
2854f05cddf9SRui Paulo 	iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2855f05cddf9SRui Paulo 				 sizeof(struct hostapd_iface *));
2856f05cddf9SRui Paulo 	if (iface == NULL)
2857f05cddf9SRui Paulo 		return NULL;
2858f05cddf9SRui Paulo 	interfaces->iface = iface;
2859f05cddf9SRui Paulo 	hapd_iface = interfaces->iface[interfaces->count] =
2860780fb4a2SCy Schubert 		hostapd_alloc_iface();
2861f05cddf9SRui Paulo 	if (hapd_iface == NULL) {
2862f05cddf9SRui Paulo 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2863f05cddf9SRui Paulo 			   "the interface", __func__);
2864f05cddf9SRui Paulo 		return NULL;
2865f05cddf9SRui Paulo 	}
2866f05cddf9SRui Paulo 	interfaces->count++;
2867f05cddf9SRui Paulo 	hapd_iface->interfaces = interfaces;
2868f05cddf9SRui Paulo 
2869f05cddf9SRui Paulo 	return hapd_iface;
2870f05cddf9SRui Paulo }
2871f05cddf9SRui Paulo 
2872f05cddf9SRui Paulo 
2873f05cddf9SRui Paulo static struct hostapd_config *
hostapd_config_alloc(struct hapd_interfaces * interfaces,const char * ifname,const char * ctrl_iface,const char * driver)2874f05cddf9SRui Paulo hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2875325151a3SRui Paulo 		     const char *ctrl_iface, const char *driver)
2876f05cddf9SRui Paulo {
2877f05cddf9SRui Paulo 	struct hostapd_bss_config *bss;
2878f05cddf9SRui Paulo 	struct hostapd_config *conf;
2879f05cddf9SRui Paulo 
2880f05cddf9SRui Paulo 	/* Allocates memory for bss and conf */
2881f05cddf9SRui Paulo 	conf = hostapd_config_defaults();
2882f05cddf9SRui Paulo 	if (conf == NULL) {
2883f05cddf9SRui Paulo 		 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2884f05cddf9SRui Paulo 				"configuration", __func__);
2885f05cddf9SRui Paulo 		 return NULL;
2886f05cddf9SRui Paulo 	}
2887f05cddf9SRui Paulo 
2888325151a3SRui Paulo 	if (driver) {
2889325151a3SRui Paulo 		int j;
2890325151a3SRui Paulo 
2891325151a3SRui Paulo 		for (j = 0; wpa_drivers[j]; j++) {
2892325151a3SRui Paulo 			if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2893325151a3SRui Paulo 				conf->driver = wpa_drivers[j];
2894325151a3SRui Paulo 				goto skip;
2895325151a3SRui Paulo 			}
2896325151a3SRui Paulo 		}
2897325151a3SRui Paulo 
2898325151a3SRui Paulo 		wpa_printf(MSG_ERROR,
2899325151a3SRui Paulo 			   "Invalid/unknown driver '%s' - registering the default driver",
2900325151a3SRui Paulo 			   driver);
2901325151a3SRui Paulo 	}
2902325151a3SRui Paulo 
2903f05cddf9SRui Paulo 	conf->driver = wpa_drivers[0];
2904f05cddf9SRui Paulo 	if (conf->driver == NULL) {
2905f05cddf9SRui Paulo 		wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2906f05cddf9SRui Paulo 		hostapd_config_free(conf);
2907f05cddf9SRui Paulo 		return NULL;
2908f05cddf9SRui Paulo 	}
2909f05cddf9SRui Paulo 
2910325151a3SRui Paulo skip:
29115b9c547cSRui Paulo 	bss = conf->last_bss = conf->bss[0];
2912f05cddf9SRui Paulo 
2913f05cddf9SRui Paulo 	os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2914f05cddf9SRui Paulo 	bss->ctrl_interface = os_strdup(ctrl_iface);
2915f05cddf9SRui Paulo 	if (bss->ctrl_interface == NULL) {
2916f05cddf9SRui Paulo 		hostapd_config_free(conf);
2917f05cddf9SRui Paulo 		return NULL;
2918f05cddf9SRui Paulo 	}
2919f05cddf9SRui Paulo 
2920f05cddf9SRui Paulo 	/* Reading configuration file skipped, will be done in SET!
2921f05cddf9SRui Paulo 	 * From reading the configuration till the end has to be done in
2922f05cddf9SRui Paulo 	 * SET
2923f05cddf9SRui Paulo 	 */
2924f05cddf9SRui Paulo 	return conf;
2925f05cddf9SRui Paulo }
2926f05cddf9SRui Paulo 
2927f05cddf9SRui Paulo 
hostapd_data_alloc(struct hostapd_iface * hapd_iface,struct hostapd_config * conf)29285b9c547cSRui Paulo static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
29295b9c547cSRui Paulo 			      struct hostapd_config *conf)
2930f05cddf9SRui Paulo {
2931f05cddf9SRui Paulo 	size_t i;
2932f05cddf9SRui Paulo 	struct hostapd_data *hapd;
2933f05cddf9SRui Paulo 
29345b9c547cSRui Paulo 	hapd_iface->bss = os_calloc(conf->num_bss,
29355b9c547cSRui Paulo 				    sizeof(struct hostapd_data *));
29365b9c547cSRui Paulo 	if (hapd_iface->bss == NULL)
29375b9c547cSRui Paulo 		return -1;
29385b9c547cSRui Paulo 
29395b9c547cSRui Paulo 	for (i = 0; i < conf->num_bss; i++) {
29405b9c547cSRui Paulo 		hapd = hapd_iface->bss[i] =
29415b9c547cSRui Paulo 			hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
29425b9c547cSRui Paulo 		if (hapd == NULL) {
29435b9c547cSRui Paulo 			while (i > 0) {
29445b9c547cSRui Paulo 				i--;
29455b9c547cSRui Paulo 				os_free(hapd_iface->bss[i]);
29465b9c547cSRui Paulo 				hapd_iface->bss[i] = NULL;
29475b9c547cSRui Paulo 			}
29485b9c547cSRui Paulo 			os_free(hapd_iface->bss);
29495b9c547cSRui Paulo 			hapd_iface->bss = NULL;
29505b9c547cSRui Paulo 			return -1;
29515b9c547cSRui Paulo 		}
29525b9c547cSRui Paulo 		hapd->msg_ctx = hapd;
29535b9c547cSRui Paulo 	}
29545b9c547cSRui Paulo 
2955f05cddf9SRui Paulo 	hapd_iface->conf = conf;
2956f05cddf9SRui Paulo 	hapd_iface->num_bss = conf->num_bss;
2957f05cddf9SRui Paulo 
29585b9c547cSRui Paulo 	return 0;
2959f05cddf9SRui Paulo }
2960f05cddf9SRui Paulo 
2961f05cddf9SRui Paulo 
hostapd_add_iface(struct hapd_interfaces * interfaces,char * buf)2962f05cddf9SRui Paulo int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2963f05cddf9SRui Paulo {
2964f05cddf9SRui Paulo 	struct hostapd_config *conf = NULL;
29655b9c547cSRui Paulo 	struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
29665b9c547cSRui Paulo 	struct hostapd_data *hapd;
2967f05cddf9SRui Paulo 	char *ptr;
29685b9c547cSRui Paulo 	size_t i, j;
29695b9c547cSRui Paulo 	const char *conf_file = NULL, *phy_name = NULL;
29705b9c547cSRui Paulo 
29715b9c547cSRui Paulo 	if (os_strncmp(buf, "bss_config=", 11) == 0) {
29725b9c547cSRui Paulo 		char *pos;
29735b9c547cSRui Paulo 		phy_name = buf + 11;
29745b9c547cSRui Paulo 		pos = os_strchr(phy_name, ':');
29755b9c547cSRui Paulo 		if (!pos)
29765b9c547cSRui Paulo 			return -1;
29775b9c547cSRui Paulo 		*pos++ = '\0';
29785b9c547cSRui Paulo 		conf_file = pos;
29795b9c547cSRui Paulo 		if (!os_strlen(conf_file))
29805b9c547cSRui Paulo 			return -1;
29815b9c547cSRui Paulo 
29825b9c547cSRui Paulo 		hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
29835b9c547cSRui Paulo 							conf_file, 0);
29845b9c547cSRui Paulo 		if (!hapd_iface)
29855b9c547cSRui Paulo 			return -1;
29865b9c547cSRui Paulo 		for (j = 0; j < interfaces->count; j++) {
29875b9c547cSRui Paulo 			if (interfaces->iface[j] == hapd_iface)
29885b9c547cSRui Paulo 				break;
29895b9c547cSRui Paulo 		}
29905b9c547cSRui Paulo 		if (j == interfaces->count) {
29915b9c547cSRui Paulo 			struct hostapd_iface **tmp;
29925b9c547cSRui Paulo 			tmp = os_realloc_array(interfaces->iface,
29935b9c547cSRui Paulo 					       interfaces->count + 1,
29945b9c547cSRui Paulo 					       sizeof(struct hostapd_iface *));
29955b9c547cSRui Paulo 			if (!tmp) {
29965b9c547cSRui Paulo 				hostapd_interface_deinit_free(hapd_iface);
29975b9c547cSRui Paulo 				return -1;
29985b9c547cSRui Paulo 			}
29995b9c547cSRui Paulo 			interfaces->iface = tmp;
30005b9c547cSRui Paulo 			interfaces->iface[interfaces->count++] = hapd_iface;
30015b9c547cSRui Paulo 			new_iface = hapd_iface;
30025b9c547cSRui Paulo 		}
30035b9c547cSRui Paulo 
30045b9c547cSRui Paulo 		if (new_iface) {
30055b9c547cSRui Paulo 			if (interfaces->driver_init(hapd_iface))
30065b9c547cSRui Paulo 				goto fail;
30075b9c547cSRui Paulo 
30085b9c547cSRui Paulo 			if (hostapd_setup_interface(hapd_iface)) {
30095b9c547cSRui Paulo 				hostapd_deinit_driver(
30105b9c547cSRui Paulo 					hapd_iface->bss[0]->driver,
30115b9c547cSRui Paulo 					hapd_iface->bss[0]->drv_priv,
30125b9c547cSRui Paulo 					hapd_iface);
30135b9c547cSRui Paulo 				goto fail;
30145b9c547cSRui Paulo 			}
30155b9c547cSRui Paulo 		} else {
30165b9c547cSRui Paulo 			/* Assign new BSS with bss[0]'s driver info */
30175b9c547cSRui Paulo 			hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
30185b9c547cSRui Paulo 			hapd->driver = hapd_iface->bss[0]->driver;
30195b9c547cSRui Paulo 			hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
30205b9c547cSRui Paulo 			os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
30215b9c547cSRui Paulo 				  ETH_ALEN);
30225b9c547cSRui Paulo 
30235b9c547cSRui Paulo 			if (start_ctrl_iface_bss(hapd) < 0 ||
30245b9c547cSRui Paulo 			    (hapd_iface->state == HAPD_IFACE_ENABLED &&
30255b9c547cSRui Paulo 			     hostapd_setup_bss(hapd, -1))) {
30265b9c547cSRui Paulo 				hostapd_cleanup(hapd);
30275b9c547cSRui Paulo 				hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
30285b9c547cSRui Paulo 				hapd_iface->conf->num_bss--;
30295b9c547cSRui Paulo 				hapd_iface->num_bss--;
30305b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
30315b9c547cSRui Paulo 					   __func__, hapd, hapd->conf->iface);
30325b9c547cSRui Paulo 				hostapd_config_free_bss(hapd->conf);
30335b9c547cSRui Paulo 				hapd->conf = NULL;
30345b9c547cSRui Paulo 				os_free(hapd);
30355b9c547cSRui Paulo 				return -1;
30365b9c547cSRui Paulo 			}
30375b9c547cSRui Paulo 		}
303885732ac8SCy Schubert 		hostapd_owe_update_trans(hapd_iface);
30395b9c547cSRui Paulo 		return 0;
30405b9c547cSRui Paulo 	}
3041f05cddf9SRui Paulo 
3042f05cddf9SRui Paulo 	ptr = os_strchr(buf, ' ');
3043f05cddf9SRui Paulo 	if (ptr == NULL)
3044f05cddf9SRui Paulo 		return -1;
3045f05cddf9SRui Paulo 	*ptr++ = '\0';
3046f05cddf9SRui Paulo 
30475b9c547cSRui Paulo 	if (os_strncmp(ptr, "config=", 7) == 0)
30485b9c547cSRui Paulo 		conf_file = ptr + 7;
30495b9c547cSRui Paulo 
3050f05cddf9SRui Paulo 	for (i = 0; i < interfaces->count; i++) {
30515b9c547cSRui Paulo 		if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
3052f05cddf9SRui Paulo 			       buf)) {
3053f05cddf9SRui Paulo 			wpa_printf(MSG_INFO, "Cannot add interface - it "
3054f05cddf9SRui Paulo 				   "already exists");
3055f05cddf9SRui Paulo 			return -1;
3056f05cddf9SRui Paulo 		}
3057f05cddf9SRui Paulo 	}
3058f05cddf9SRui Paulo 
3059f05cddf9SRui Paulo 	hapd_iface = hostapd_iface_alloc(interfaces);
3060f05cddf9SRui Paulo 	if (hapd_iface == NULL) {
3061f05cddf9SRui Paulo 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3062f05cddf9SRui Paulo 			   "for interface", __func__);
3063f05cddf9SRui Paulo 		goto fail;
3064f05cddf9SRui Paulo 	}
30655b9c547cSRui Paulo 	new_iface = hapd_iface;
3066f05cddf9SRui Paulo 
30675b9c547cSRui Paulo 	if (conf_file && interfaces->config_read_cb) {
30685b9c547cSRui Paulo 		conf = interfaces->config_read_cb(conf_file);
30695b9c547cSRui Paulo 		if (conf && conf->bss)
30705b9c547cSRui Paulo 			os_strlcpy(conf->bss[0]->iface, buf,
30715b9c547cSRui Paulo 				   sizeof(conf->bss[0]->iface));
3072325151a3SRui Paulo 	} else {
3073325151a3SRui Paulo 		char *driver = os_strchr(ptr, ' ');
3074325151a3SRui Paulo 
3075325151a3SRui Paulo 		if (driver)
3076325151a3SRui Paulo 			*driver++ = '\0';
3077325151a3SRui Paulo 		conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3078325151a3SRui Paulo 	}
3079325151a3SRui Paulo 
30805b9c547cSRui Paulo 	if (conf == NULL || conf->bss == NULL) {
3081f05cddf9SRui Paulo 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3082f05cddf9SRui Paulo 			   "for configuration", __func__);
3083f05cddf9SRui Paulo 		goto fail;
3084f05cddf9SRui Paulo 	}
3085f05cddf9SRui Paulo 
30865b9c547cSRui Paulo 	if (hostapd_data_alloc(hapd_iface, conf) < 0) {
3087f05cddf9SRui Paulo 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3088f05cddf9SRui Paulo 			   "for hostapd", __func__);
3089f05cddf9SRui Paulo 		goto fail;
3090f05cddf9SRui Paulo 	}
30915b9c547cSRui Paulo 	conf = NULL;
3092f05cddf9SRui Paulo 
30935b9c547cSRui Paulo 	if (start_ctrl_iface(hapd_iface) < 0)
3094f05cddf9SRui Paulo 		goto fail;
30955b9c547cSRui Paulo 
30965b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "Add interface '%s'",
30975b9c547cSRui Paulo 		   hapd_iface->conf->bss[0]->iface);
3098f05cddf9SRui Paulo 
3099f05cddf9SRui Paulo 	return 0;
3100f05cddf9SRui Paulo 
3101f05cddf9SRui Paulo fail:
3102f05cddf9SRui Paulo 	if (conf)
3103f05cddf9SRui Paulo 		hostapd_config_free(conf);
3104f05cddf9SRui Paulo 	if (hapd_iface) {
31055b9c547cSRui Paulo 		if (hapd_iface->bss) {
31065b9c547cSRui Paulo 			for (i = 0; i < hapd_iface->num_bss; i++) {
31075b9c547cSRui Paulo 				hapd = hapd_iface->bss[i];
31085b9c547cSRui Paulo 				if (!hapd)
31095b9c547cSRui Paulo 					continue;
31105b9c547cSRui Paulo 				if (hapd_iface->interfaces &&
31115b9c547cSRui Paulo 				    hapd_iface->interfaces->ctrl_iface_deinit)
31125b9c547cSRui Paulo 					hapd_iface->interfaces->
31135b9c547cSRui Paulo 						ctrl_iface_deinit(hapd);
31145b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
31155b9c547cSRui Paulo 					   __func__, hapd_iface->bss[i],
31165b9c547cSRui Paulo 					   hapd->conf->iface);
31175b9c547cSRui Paulo 				hostapd_cleanup(hapd);
31185b9c547cSRui Paulo 				os_free(hapd);
31195b9c547cSRui Paulo 				hapd_iface->bss[i] = NULL;
31205b9c547cSRui Paulo 			}
31215b9c547cSRui Paulo 			os_free(hapd_iface->bss);
31225b9c547cSRui Paulo 			hapd_iface->bss = NULL;
31235b9c547cSRui Paulo 		}
31245b9c547cSRui Paulo 		if (new_iface) {
31255b9c547cSRui Paulo 			interfaces->count--;
31265b9c547cSRui Paulo 			interfaces->iface[interfaces->count] = NULL;
31275b9c547cSRui Paulo 		}
31285b9c547cSRui Paulo 		hostapd_cleanup_iface(hapd_iface);
3129f05cddf9SRui Paulo 	}
3130f05cddf9SRui Paulo 	return -1;
3131f05cddf9SRui Paulo }
3132f05cddf9SRui Paulo 
3133f05cddf9SRui Paulo 
hostapd_remove_bss(struct hostapd_iface * iface,unsigned int idx)31345b9c547cSRui Paulo static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
31355b9c547cSRui Paulo {
31365b9c547cSRui Paulo 	size_t i;
31375b9c547cSRui Paulo 
31385b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
31395b9c547cSRui Paulo 
31405b9c547cSRui Paulo 	/* Remove hostapd_data only if it has already been initialized */
31415b9c547cSRui Paulo 	if (idx < iface->num_bss) {
31425b9c547cSRui Paulo 		struct hostapd_data *hapd = iface->bss[idx];
31435b9c547cSRui Paulo 
31445b9c547cSRui Paulo 		hostapd_bss_deinit(hapd);
31455b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
31465b9c547cSRui Paulo 			   __func__, hapd, hapd->conf->iface);
31475b9c547cSRui Paulo 		hostapd_config_free_bss(hapd->conf);
31485b9c547cSRui Paulo 		hapd->conf = NULL;
31495b9c547cSRui Paulo 		os_free(hapd);
31505b9c547cSRui Paulo 
31515b9c547cSRui Paulo 		iface->num_bss--;
31525b9c547cSRui Paulo 
31535b9c547cSRui Paulo 		for (i = idx; i < iface->num_bss; i++)
31545b9c547cSRui Paulo 			iface->bss[i] = iface->bss[i + 1];
31555b9c547cSRui Paulo 	} else {
31565b9c547cSRui Paulo 		hostapd_config_free_bss(iface->conf->bss[idx]);
31575b9c547cSRui Paulo 		iface->conf->bss[idx] = NULL;
31585b9c547cSRui Paulo 	}
31595b9c547cSRui Paulo 
31605b9c547cSRui Paulo 	iface->conf->num_bss--;
31615b9c547cSRui Paulo 	for (i = idx; i < iface->conf->num_bss; i++)
31625b9c547cSRui Paulo 		iface->conf->bss[i] = iface->conf->bss[i + 1];
31635b9c547cSRui Paulo 
31645b9c547cSRui Paulo 	return 0;
31655b9c547cSRui Paulo }
31665b9c547cSRui Paulo 
31675b9c547cSRui Paulo 
hostapd_remove_iface(struct hapd_interfaces * interfaces,char * buf)3168f05cddf9SRui Paulo int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3169f05cddf9SRui Paulo {
3170f05cddf9SRui Paulo 	struct hostapd_iface *hapd_iface;
31715b9c547cSRui Paulo 	size_t i, j, k = 0;
3172f05cddf9SRui Paulo 
3173f05cddf9SRui Paulo 	for (i = 0; i < interfaces->count; i++) {
3174f05cddf9SRui Paulo 		hapd_iface = interfaces->iface[i];
3175f05cddf9SRui Paulo 		if (hapd_iface == NULL)
3176f05cddf9SRui Paulo 			return -1;
31775b9c547cSRui Paulo 		if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
3178f05cddf9SRui Paulo 			wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
31795b9c547cSRui Paulo 			hapd_iface->driver_ap_teardown =
31805b9c547cSRui Paulo 				!!(hapd_iface->drv_flags &
31815b9c547cSRui Paulo 				   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
31825b9c547cSRui Paulo 
3183f05cddf9SRui Paulo 			hostapd_interface_deinit_free(hapd_iface);
3184f05cddf9SRui Paulo 			k = i;
3185f05cddf9SRui Paulo 			while (k < (interfaces->count - 1)) {
3186f05cddf9SRui Paulo 				interfaces->iface[k] =
3187f05cddf9SRui Paulo 					interfaces->iface[k + 1];
3188f05cddf9SRui Paulo 				k++;
3189f05cddf9SRui Paulo 			}
3190f05cddf9SRui Paulo 			interfaces->count--;
3191f05cddf9SRui Paulo 			return 0;
3192f05cddf9SRui Paulo 		}
31935b9c547cSRui Paulo 
31945b9c547cSRui Paulo 		for (j = 0; j < hapd_iface->conf->num_bss; j++) {
31955b9c547cSRui Paulo 			if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
31965b9c547cSRui Paulo 				hapd_iface->driver_ap_teardown =
31975b9c547cSRui Paulo 					!(hapd_iface->drv_flags &
31985b9c547cSRui Paulo 					  WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
31995b9c547cSRui Paulo 				return hostapd_remove_bss(hapd_iface, j);
32005b9c547cSRui Paulo 			}
32015b9c547cSRui Paulo 		}
3202f05cddf9SRui Paulo 	}
3203f05cddf9SRui Paulo 	return -1;
3204f05cddf9SRui Paulo }
3205f05cddf9SRui Paulo 
3206f05cddf9SRui Paulo 
3207e28a4053SRui Paulo /**
3208e28a4053SRui Paulo  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3209e28a4053SRui Paulo  * @hapd: Pointer to BSS data
3210e28a4053SRui Paulo  * @sta: Pointer to the associated STA data
3211e28a4053SRui Paulo  * @reassoc: 1 to indicate this was a re-association; 0 = first association
3212e28a4053SRui Paulo  *
3213e28a4053SRui Paulo  * This function will be called whenever a station associates with the AP. It
3214e28a4053SRui Paulo  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3215e28a4053SRui Paulo  * from drv_callbacks.c based on driver events for drivers that take care of
3216e28a4053SRui Paulo  * management frames (IEEE 802.11 authentication and association) internally.
3217e28a4053SRui Paulo  */
hostapd_new_assoc_sta(struct hostapd_data * hapd,struct sta_info * sta,int reassoc)3218e28a4053SRui Paulo void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3219e28a4053SRui Paulo 			   int reassoc)
3220e28a4053SRui Paulo {
3221e28a4053SRui Paulo 	if (hapd->tkip_countermeasures) {
3222f05cddf9SRui Paulo 		hostapd_drv_sta_deauth(hapd, sta->addr,
3223e28a4053SRui Paulo 				       WLAN_REASON_MICHAEL_MIC_FAILURE);
3224e28a4053SRui Paulo 		return;
3225e28a4053SRui Paulo 	}
3226e28a4053SRui Paulo 
3227e28a4053SRui Paulo 	hostapd_prune_associations(hapd, sta->addr);
3228780fb4a2SCy Schubert 	ap_sta_clear_disconnect_timeouts(hapd, sta);
3229c1d255d3SCy Schubert 	sta->post_csa_sa_query = 0;
3230e28a4053SRui Paulo 
3231f05cddf9SRui Paulo #ifdef CONFIG_P2P
3232f05cddf9SRui Paulo 	if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3233f05cddf9SRui Paulo 		sta->no_p2p_set = 1;
3234f05cddf9SRui Paulo 		hapd->num_sta_no_p2p++;
3235f05cddf9SRui Paulo 		if (hapd->num_sta_no_p2p == 1)
3236f05cddf9SRui Paulo 			hostapd_p2p_non_p2p_sta_connected(hapd);
3237f05cddf9SRui Paulo 	}
3238f05cddf9SRui Paulo #endif /* CONFIG_P2P */
3239f05cddf9SRui Paulo 
3240206b73d0SCy Schubert 	airtime_policy_new_sta(hapd, sta);
3241206b73d0SCy Schubert 
3242e28a4053SRui Paulo 	/* Start accounting here, if IEEE 802.1X and WPA are not used.
3243e28a4053SRui Paulo 	 * IEEE 802.1X/WPA code will start accounting after the station has
3244e28a4053SRui Paulo 	 * been authorized. */
32455b9c547cSRui Paulo 	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
32465b9c547cSRui Paulo 		ap_sta_set_authorized(hapd, sta, 1);
32475b9c547cSRui Paulo 		os_get_reltime(&sta->connected_time);
3248e28a4053SRui Paulo 		accounting_sta_start(hapd, sta);
3249f05cddf9SRui Paulo 	}
3250e28a4053SRui Paulo 
3251e28a4053SRui Paulo 	/* Start IEEE 802.1X authentication process for new stations */
3252e28a4053SRui Paulo 	ieee802_1x_new_station(hapd, sta);
3253e28a4053SRui Paulo 	if (reassoc) {
3254e28a4053SRui Paulo 		if (sta->auth_alg != WLAN_AUTH_FT &&
325585732ac8SCy Schubert 		    sta->auth_alg != WLAN_AUTH_FILS_SK &&
325685732ac8SCy Schubert 		    sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
325785732ac8SCy Schubert 		    sta->auth_alg != WLAN_AUTH_FILS_PK &&
3258e28a4053SRui Paulo 		    !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3259e28a4053SRui Paulo 			wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3260e28a4053SRui Paulo 	} else
3261e28a4053SRui Paulo 		wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
3262f05cddf9SRui Paulo 
326385732ac8SCy Schubert 	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
326485732ac8SCy Schubert 		if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
326585732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
326685732ac8SCy Schubert 				   "%s: %s: canceled wired ap_handle_timer timeout for "
326785732ac8SCy Schubert 				   MACSTR,
326885732ac8SCy Schubert 				   hapd->conf->iface, __func__,
326985732ac8SCy Schubert 				   MAC2STR(sta->addr));
327085732ac8SCy Schubert 		}
327185732ac8SCy Schubert 	} else if (!(hapd->iface->drv_flags &
327285732ac8SCy Schubert 		     WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
3273780fb4a2SCy Schubert 		wpa_printf(MSG_DEBUG,
3274780fb4a2SCy Schubert 			   "%s: %s: reschedule ap_handle_timer timeout for "
3275780fb4a2SCy Schubert 			   MACSTR " (%d seconds - ap_max_inactivity)",
3276780fb4a2SCy Schubert 			   hapd->conf->iface, __func__, MAC2STR(sta->addr),
3277f05cddf9SRui Paulo 			   hapd->conf->ap_max_inactivity);
3278f05cddf9SRui Paulo 		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3279f05cddf9SRui Paulo 		eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3280f05cddf9SRui Paulo 				       ap_handle_timer, hapd, sta);
3281e28a4053SRui Paulo 	}
3282206b73d0SCy Schubert 
3283206b73d0SCy Schubert #ifdef CONFIG_MACSEC
3284206b73d0SCy Schubert 	if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3285206b73d0SCy Schubert 	    hapd->conf->mka_psk_set)
3286206b73d0SCy Schubert 		ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3287206b73d0SCy Schubert 	else
3288206b73d0SCy Schubert 		ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3289206b73d0SCy Schubert #endif /* CONFIG_MACSEC */
32905b9c547cSRui Paulo }
32915b9c547cSRui Paulo 
32925b9c547cSRui Paulo 
hostapd_state_text(enum hostapd_iface_state s)32935b9c547cSRui Paulo const char * hostapd_state_text(enum hostapd_iface_state s)
32945b9c547cSRui Paulo {
32955b9c547cSRui Paulo 	switch (s) {
32965b9c547cSRui Paulo 	case HAPD_IFACE_UNINITIALIZED:
32975b9c547cSRui Paulo 		return "UNINITIALIZED";
32985b9c547cSRui Paulo 	case HAPD_IFACE_DISABLED:
32995b9c547cSRui Paulo 		return "DISABLED";
33005b9c547cSRui Paulo 	case HAPD_IFACE_COUNTRY_UPDATE:
33015b9c547cSRui Paulo 		return "COUNTRY_UPDATE";
33025b9c547cSRui Paulo 	case HAPD_IFACE_ACS:
33035b9c547cSRui Paulo 		return "ACS";
33045b9c547cSRui Paulo 	case HAPD_IFACE_HT_SCAN:
33055b9c547cSRui Paulo 		return "HT_SCAN";
33065b9c547cSRui Paulo 	case HAPD_IFACE_DFS:
33075b9c547cSRui Paulo 		return "DFS";
33085b9c547cSRui Paulo 	case HAPD_IFACE_ENABLED:
33095b9c547cSRui Paulo 		return "ENABLED";
33105b9c547cSRui Paulo 	}
33115b9c547cSRui Paulo 
33125b9c547cSRui Paulo 	return "UNKNOWN";
33135b9c547cSRui Paulo }
33145b9c547cSRui Paulo 
33155b9c547cSRui Paulo 
hostapd_set_state(struct hostapd_iface * iface,enum hostapd_iface_state s)33165b9c547cSRui Paulo void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
33175b9c547cSRui Paulo {
33185b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "%s: interface state %s->%s",
3319780fb4a2SCy Schubert 		   iface->conf ? iface->conf->bss[0]->iface : "N/A",
3320780fb4a2SCy Schubert 		   hostapd_state_text(iface->state), hostapd_state_text(s));
33215b9c547cSRui Paulo 	iface->state = s;
33225b9c547cSRui Paulo }
33235b9c547cSRui Paulo 
33245b9c547cSRui Paulo 
hostapd_csa_in_progress(struct hostapd_iface * iface)3325780fb4a2SCy Schubert int hostapd_csa_in_progress(struct hostapd_iface *iface)
3326780fb4a2SCy Schubert {
3327780fb4a2SCy Schubert 	unsigned int i;
3328780fb4a2SCy Schubert 
3329780fb4a2SCy Schubert 	for (i = 0; i < iface->num_bss; i++)
3330780fb4a2SCy Schubert 		if (iface->bss[i]->csa_in_progress)
3331780fb4a2SCy Schubert 			return 1;
3332780fb4a2SCy Schubert 	return 0;
3333780fb4a2SCy Schubert }
3334780fb4a2SCy Schubert 
3335780fb4a2SCy Schubert 
33365b9c547cSRui Paulo #ifdef NEED_AP_MLME
33375b9c547cSRui Paulo 
free_beacon_data(struct beacon_data * beacon)33385b9c547cSRui Paulo static void free_beacon_data(struct beacon_data *beacon)
33395b9c547cSRui Paulo {
33405b9c547cSRui Paulo 	os_free(beacon->head);
33415b9c547cSRui Paulo 	beacon->head = NULL;
33425b9c547cSRui Paulo 	os_free(beacon->tail);
33435b9c547cSRui Paulo 	beacon->tail = NULL;
33445b9c547cSRui Paulo 	os_free(beacon->probe_resp);
33455b9c547cSRui Paulo 	beacon->probe_resp = NULL;
33465b9c547cSRui Paulo 	os_free(beacon->beacon_ies);
33475b9c547cSRui Paulo 	beacon->beacon_ies = NULL;
33485b9c547cSRui Paulo 	os_free(beacon->proberesp_ies);
33495b9c547cSRui Paulo 	beacon->proberesp_ies = NULL;
33505b9c547cSRui Paulo 	os_free(beacon->assocresp_ies);
33515b9c547cSRui Paulo 	beacon->assocresp_ies = NULL;
33525b9c547cSRui Paulo }
33535b9c547cSRui Paulo 
33545b9c547cSRui Paulo 
hostapd_build_beacon_data(struct hostapd_data * hapd,struct beacon_data * beacon)33555b9c547cSRui Paulo static int hostapd_build_beacon_data(struct hostapd_data *hapd,
33565b9c547cSRui Paulo 				     struct beacon_data *beacon)
33575b9c547cSRui Paulo {
33585b9c547cSRui Paulo 	struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
33595b9c547cSRui Paulo 	struct wpa_driver_ap_params params;
33605b9c547cSRui Paulo 	int ret;
33615b9c547cSRui Paulo 
33625b9c547cSRui Paulo 	os_memset(beacon, 0, sizeof(*beacon));
33635b9c547cSRui Paulo 	ret = ieee802_11_build_ap_params(hapd, &params);
33645b9c547cSRui Paulo 	if (ret < 0)
33655b9c547cSRui Paulo 		return ret;
33665b9c547cSRui Paulo 
33675b9c547cSRui Paulo 	ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
33685b9c547cSRui Paulo 					 &proberesp_extra,
33695b9c547cSRui Paulo 					 &assocresp_extra);
33705b9c547cSRui Paulo 	if (ret)
33715b9c547cSRui Paulo 		goto free_ap_params;
33725b9c547cSRui Paulo 
33735b9c547cSRui Paulo 	ret = -1;
337485732ac8SCy Schubert 	beacon->head = os_memdup(params.head, params.head_len);
33755b9c547cSRui Paulo 	if (!beacon->head)
33765b9c547cSRui Paulo 		goto free_ap_extra_ies;
33775b9c547cSRui Paulo 
33785b9c547cSRui Paulo 	beacon->head_len = params.head_len;
33795b9c547cSRui Paulo 
338085732ac8SCy Schubert 	beacon->tail = os_memdup(params.tail, params.tail_len);
33815b9c547cSRui Paulo 	if (!beacon->tail)
33825b9c547cSRui Paulo 		goto free_beacon;
33835b9c547cSRui Paulo 
33845b9c547cSRui Paulo 	beacon->tail_len = params.tail_len;
33855b9c547cSRui Paulo 
33865b9c547cSRui Paulo 	if (params.proberesp != NULL) {
338785732ac8SCy Schubert 		beacon->probe_resp = os_memdup(params.proberesp,
338885732ac8SCy Schubert 					       params.proberesp_len);
33895b9c547cSRui Paulo 		if (!beacon->probe_resp)
33905b9c547cSRui Paulo 			goto free_beacon;
33915b9c547cSRui Paulo 
33925b9c547cSRui Paulo 		beacon->probe_resp_len = params.proberesp_len;
33935b9c547cSRui Paulo 	}
33945b9c547cSRui Paulo 
33955b9c547cSRui Paulo 	/* copy the extra ies */
33965b9c547cSRui Paulo 	if (beacon_extra) {
339785732ac8SCy Schubert 		beacon->beacon_ies = os_memdup(beacon_extra->buf,
339885732ac8SCy Schubert 					       wpabuf_len(beacon_extra));
33995b9c547cSRui Paulo 		if (!beacon->beacon_ies)
34005b9c547cSRui Paulo 			goto free_beacon;
34015b9c547cSRui Paulo 
34025b9c547cSRui Paulo 		beacon->beacon_ies_len = wpabuf_len(beacon_extra);
34035b9c547cSRui Paulo 	}
34045b9c547cSRui Paulo 
34055b9c547cSRui Paulo 	if (proberesp_extra) {
340685732ac8SCy Schubert 		beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
340785732ac8SCy Schubert 						  wpabuf_len(proberesp_extra));
34085b9c547cSRui Paulo 		if (!beacon->proberesp_ies)
34095b9c547cSRui Paulo 			goto free_beacon;
34105b9c547cSRui Paulo 
34115b9c547cSRui Paulo 		beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
34125b9c547cSRui Paulo 	}
34135b9c547cSRui Paulo 
34145b9c547cSRui Paulo 	if (assocresp_extra) {
341585732ac8SCy Schubert 		beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
341685732ac8SCy Schubert 						  wpabuf_len(assocresp_extra));
34175b9c547cSRui Paulo 		if (!beacon->assocresp_ies)
34185b9c547cSRui Paulo 			goto free_beacon;
34195b9c547cSRui Paulo 
34205b9c547cSRui Paulo 		beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
34215b9c547cSRui Paulo 	}
34225b9c547cSRui Paulo 
34235b9c547cSRui Paulo 	ret = 0;
34245b9c547cSRui Paulo free_beacon:
34255b9c547cSRui Paulo 	/* if the function fails, the caller should not free beacon data */
34265b9c547cSRui Paulo 	if (ret)
34275b9c547cSRui Paulo 		free_beacon_data(beacon);
34285b9c547cSRui Paulo 
34295b9c547cSRui Paulo free_ap_extra_ies:
34305b9c547cSRui Paulo 	hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
34315b9c547cSRui Paulo 				  assocresp_extra);
34325b9c547cSRui Paulo free_ap_params:
34335b9c547cSRui Paulo 	ieee802_11_free_ap_params(&params);
34345b9c547cSRui Paulo 	return ret;
34355b9c547cSRui Paulo }
34365b9c547cSRui Paulo 
34375b9c547cSRui Paulo 
34385b9c547cSRui Paulo /*
3439780fb4a2SCy Schubert  * TODO: This flow currently supports only changing channel and width within
3440780fb4a2SCy Schubert  * the same hw_mode. Any other changes to MAC parameters or provided settings
3441780fb4a2SCy Schubert  * are not supported.
34425b9c547cSRui Paulo  */
hostapd_change_config_freq(struct hostapd_data * hapd,struct hostapd_config * conf,struct hostapd_freq_params * params,struct hostapd_freq_params * old_params)34435b9c547cSRui Paulo static int hostapd_change_config_freq(struct hostapd_data *hapd,
34445b9c547cSRui Paulo 				      struct hostapd_config *conf,
34455b9c547cSRui Paulo 				      struct hostapd_freq_params *params,
34465b9c547cSRui Paulo 				      struct hostapd_freq_params *old_params)
34475b9c547cSRui Paulo {
34485b9c547cSRui Paulo 	int channel;
3449206b73d0SCy Schubert 	u8 seg0, seg1;
3450206b73d0SCy Schubert 	struct hostapd_hw_modes *mode;
34515b9c547cSRui Paulo 
34525b9c547cSRui Paulo 	if (!params->channel) {
34535b9c547cSRui Paulo 		/* check if the new channel is supported by hw */
34545b9c547cSRui Paulo 		params->channel = hostapd_hw_get_channel(hapd, params->freq);
34555b9c547cSRui Paulo 	}
34565b9c547cSRui Paulo 
34575b9c547cSRui Paulo 	channel = params->channel;
34585b9c547cSRui Paulo 	if (!channel)
34595b9c547cSRui Paulo 		return -1;
34605b9c547cSRui Paulo 
3461206b73d0SCy Schubert 	mode = hapd->iface->current_mode;
3462206b73d0SCy Schubert 
34635b9c547cSRui Paulo 	/* if a pointer to old_params is provided we save previous state */
3464780fb4a2SCy Schubert 	if (old_params &&
3465780fb4a2SCy Schubert 	    hostapd_set_freq_params(old_params, conf->hw_mode,
3466780fb4a2SCy Schubert 				    hostapd_hw_get_freq(hapd, conf->channel),
3467c1d255d3SCy Schubert 				    conf->channel, conf->enable_edmg,
3468c1d255d3SCy Schubert 				    conf->edmg_channel, conf->ieee80211n,
3469206b73d0SCy Schubert 				    conf->ieee80211ac, conf->ieee80211ax,
3470780fb4a2SCy Schubert 				    conf->secondary_channel,
3471206b73d0SCy Schubert 				    hostapd_get_oper_chwidth(conf),
3472206b73d0SCy Schubert 				    hostapd_get_oper_centr_freq_seg0_idx(conf),
3473206b73d0SCy Schubert 				    hostapd_get_oper_centr_freq_seg1_idx(conf),
3474206b73d0SCy Schubert 				    conf->vht_capab,
3475206b73d0SCy Schubert 				    mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3476206b73d0SCy Schubert 				    NULL))
3477780fb4a2SCy Schubert 		return -1;
3478780fb4a2SCy Schubert 
3479780fb4a2SCy Schubert 	switch (params->bandwidth) {
3480780fb4a2SCy Schubert 	case 0:
3481780fb4a2SCy Schubert 	case 20:
348232a95656SCy Schubert 		conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
348332a95656SCy Schubert 		break;
348432a95656SCy Schubert 	case 40:
348532a95656SCy Schubert 	case 80:
348632a95656SCy Schubert 	case 160:
348732a95656SCy Schubert 		conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
348832a95656SCy Schubert 		break;
348932a95656SCy Schubert 	default:
349032a95656SCy Schubert 		return -1;
349132a95656SCy Schubert 	}
349232a95656SCy Schubert 
349332a95656SCy Schubert 	switch (params->bandwidth) {
349432a95656SCy Schubert 	case 0:
349532a95656SCy Schubert 	case 20:
3496780fb4a2SCy Schubert 	case 40:
3497206b73d0SCy Schubert 		hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
3498780fb4a2SCy Schubert 		break;
3499780fb4a2SCy Schubert 	case 80:
3500780fb4a2SCy Schubert 		if (params->center_freq2)
3501206b73d0SCy Schubert 			hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
3502780fb4a2SCy Schubert 		else
3503206b73d0SCy Schubert 			hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
3504780fb4a2SCy Schubert 		break;
3505780fb4a2SCy Schubert 	case 160:
3506206b73d0SCy Schubert 		hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
3507780fb4a2SCy Schubert 		break;
3508780fb4a2SCy Schubert 	default:
3509780fb4a2SCy Schubert 		return -1;
35105b9c547cSRui Paulo 	}
35115b9c547cSRui Paulo 
35125b9c547cSRui Paulo 	conf->channel = channel;
35135b9c547cSRui Paulo 	conf->ieee80211n = params->ht_enabled;
351432a95656SCy Schubert 	conf->ieee80211ac = params->vht_enabled;
35155b9c547cSRui Paulo 	conf->secondary_channel = params->sec_channel_offset;
3516780fb4a2SCy Schubert 	ieee80211_freq_to_chan(params->center_freq1,
3517206b73d0SCy Schubert 			       &seg0);
3518780fb4a2SCy Schubert 	ieee80211_freq_to_chan(params->center_freq2,
3519206b73d0SCy Schubert 			       &seg1);
3520206b73d0SCy Schubert 	hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3521206b73d0SCy Schubert 	hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
35225b9c547cSRui Paulo 
35235b9c547cSRui Paulo 	/* TODO: maybe call here hostapd_config_check here? */
35245b9c547cSRui Paulo 
35255b9c547cSRui Paulo 	return 0;
35265b9c547cSRui Paulo }
35275b9c547cSRui Paulo 
35285b9c547cSRui Paulo 
hostapd_fill_csa_settings(struct hostapd_data * hapd,struct csa_settings * settings)35295b9c547cSRui Paulo static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
35305b9c547cSRui Paulo 				     struct csa_settings *settings)
35315b9c547cSRui Paulo {
35325b9c547cSRui Paulo 	struct hostapd_iface *iface = hapd->iface;
35335b9c547cSRui Paulo 	struct hostapd_freq_params old_freq;
35345b9c547cSRui Paulo 	int ret;
3535206b73d0SCy Schubert 	u8 chan, bandwidth;
35365b9c547cSRui Paulo 
35375b9c547cSRui Paulo 	os_memset(&old_freq, 0, sizeof(old_freq));
35385b9c547cSRui Paulo 	if (!iface || !iface->freq || hapd->csa_in_progress)
35395b9c547cSRui Paulo 		return -1;
35405b9c547cSRui Paulo 
3541780fb4a2SCy Schubert 	switch (settings->freq_params.bandwidth) {
3542780fb4a2SCy Schubert 	case 80:
3543780fb4a2SCy Schubert 		if (settings->freq_params.center_freq2)
3544206b73d0SCy Schubert 			bandwidth = CHANWIDTH_80P80MHZ;
3545780fb4a2SCy Schubert 		else
3546206b73d0SCy Schubert 			bandwidth = CHANWIDTH_80MHZ;
3547780fb4a2SCy Schubert 		break;
3548780fb4a2SCy Schubert 	case 160:
3549206b73d0SCy Schubert 		bandwidth = CHANWIDTH_160MHZ;
3550780fb4a2SCy Schubert 		break;
3551780fb4a2SCy Schubert 	default:
3552206b73d0SCy Schubert 		bandwidth = CHANWIDTH_USE_HT;
3553780fb4a2SCy Schubert 		break;
3554780fb4a2SCy Schubert 	}
3555780fb4a2SCy Schubert 
3556780fb4a2SCy Schubert 	if (ieee80211_freq_to_channel_ext(
3557780fb4a2SCy Schubert 		    settings->freq_params.freq,
3558780fb4a2SCy Schubert 		    settings->freq_params.sec_channel_offset,
3559206b73d0SCy Schubert 		    bandwidth,
3560780fb4a2SCy Schubert 		    &hapd->iface->cs_oper_class,
3561780fb4a2SCy Schubert 		    &chan) == NUM_HOSTAPD_MODES) {
3562780fb4a2SCy Schubert 		wpa_printf(MSG_DEBUG,
3563206b73d0SCy Schubert 			   "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
3564780fb4a2SCy Schubert 			   settings->freq_params.freq,
3565780fb4a2SCy Schubert 			   settings->freq_params.sec_channel_offset,
3566206b73d0SCy Schubert 			   settings->freq_params.vht_enabled,
3567206b73d0SCy Schubert 			   settings->freq_params.he_enabled);
3568780fb4a2SCy Schubert 		return -1;
3569780fb4a2SCy Schubert 	}
3570780fb4a2SCy Schubert 
3571780fb4a2SCy Schubert 	settings->freq_params.channel = chan;
3572780fb4a2SCy Schubert 
35735b9c547cSRui Paulo 	ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
35745b9c547cSRui Paulo 					 &settings->freq_params,
35755b9c547cSRui Paulo 					 &old_freq);
35765b9c547cSRui Paulo 	if (ret)
35775b9c547cSRui Paulo 		return ret;
35785b9c547cSRui Paulo 
35795b9c547cSRui Paulo 	ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
35805b9c547cSRui Paulo 
35815b9c547cSRui Paulo 	/* change back the configuration */
35825b9c547cSRui Paulo 	hostapd_change_config_freq(iface->bss[0], iface->conf,
35835b9c547cSRui Paulo 				   &old_freq, NULL);
35845b9c547cSRui Paulo 
35855b9c547cSRui Paulo 	if (ret)
35865b9c547cSRui Paulo 		return ret;
35875b9c547cSRui Paulo 
35885b9c547cSRui Paulo 	/* set channel switch parameters for csa ie */
35895b9c547cSRui Paulo 	hapd->cs_freq_params = settings->freq_params;
35905b9c547cSRui Paulo 	hapd->cs_count = settings->cs_count;
35915b9c547cSRui Paulo 	hapd->cs_block_tx = settings->block_tx;
35925b9c547cSRui Paulo 
35935b9c547cSRui Paulo 	ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
35945b9c547cSRui Paulo 	if (ret) {
35955b9c547cSRui Paulo 		free_beacon_data(&settings->beacon_after);
35965b9c547cSRui Paulo 		return ret;
35975b9c547cSRui Paulo 	}
35985b9c547cSRui Paulo 
3599780fb4a2SCy Schubert 	settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3600780fb4a2SCy Schubert 	settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3601780fb4a2SCy Schubert 	settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3602780fb4a2SCy Schubert 	settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
36035b9c547cSRui Paulo 
36045b9c547cSRui Paulo 	return 0;
36055b9c547cSRui Paulo }
36065b9c547cSRui Paulo 
36075b9c547cSRui Paulo 
hostapd_cleanup_cs_params(struct hostapd_data * hapd)36085b9c547cSRui Paulo void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
36095b9c547cSRui Paulo {
36105b9c547cSRui Paulo 	os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
36115b9c547cSRui Paulo 	hapd->cs_count = 0;
36125b9c547cSRui Paulo 	hapd->cs_block_tx = 0;
36135b9c547cSRui Paulo 	hapd->cs_c_off_beacon = 0;
36145b9c547cSRui Paulo 	hapd->cs_c_off_proberesp = 0;
36155b9c547cSRui Paulo 	hapd->csa_in_progress = 0;
3616780fb4a2SCy Schubert 	hapd->cs_c_off_ecsa_beacon = 0;
3617780fb4a2SCy Schubert 	hapd->cs_c_off_ecsa_proberesp = 0;
36185b9c547cSRui Paulo }
36195b9c547cSRui Paulo 
36205b9c547cSRui Paulo 
hostapd_chan_switch_config(struct hostapd_data * hapd,struct hostapd_freq_params * freq_params)3621c1d255d3SCy Schubert void hostapd_chan_switch_config(struct hostapd_data *hapd,
3622c1d255d3SCy Schubert 				struct hostapd_freq_params *freq_params)
362385732ac8SCy Schubert {
3624c1d255d3SCy Schubert 	if (freq_params->he_enabled)
3625c1d255d3SCy Schubert 		hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
3626c1d255d3SCy Schubert 	else
3627c1d255d3SCy Schubert 		hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
3628c1d255d3SCy Schubert 
3629c1d255d3SCy Schubert 	if (freq_params->vht_enabled)
363085732ac8SCy Schubert 		hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
363185732ac8SCy Schubert 	else
363285732ac8SCy Schubert 		hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
363385732ac8SCy Schubert 
363485732ac8SCy Schubert 	hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3635c1d255d3SCy Schubert 		       HOSTAPD_LEVEL_INFO,
3636c1d255d3SCy Schubert 		       "CHAN_SWITCH HE config 0x%x VHT config 0x%x",
3637c1d255d3SCy Schubert 		       hapd->iconf->ch_switch_he_config,
363885732ac8SCy Schubert 		       hapd->iconf->ch_switch_vht_config);
363985732ac8SCy Schubert }
364085732ac8SCy Schubert 
364185732ac8SCy Schubert 
hostapd_switch_channel(struct hostapd_data * hapd,struct csa_settings * settings)36425b9c547cSRui Paulo int hostapd_switch_channel(struct hostapd_data *hapd,
36435b9c547cSRui Paulo 			   struct csa_settings *settings)
36445b9c547cSRui Paulo {
36455b9c547cSRui Paulo 	int ret;
36465b9c547cSRui Paulo 
36475b9c547cSRui Paulo 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
36485b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "CSA is not supported");
36495b9c547cSRui Paulo 		return -1;
36505b9c547cSRui Paulo 	}
36515b9c547cSRui Paulo 
36525b9c547cSRui Paulo 	ret = hostapd_fill_csa_settings(hapd, settings);
36535b9c547cSRui Paulo 	if (ret)
36545b9c547cSRui Paulo 		return ret;
36555b9c547cSRui Paulo 
36565b9c547cSRui Paulo 	ret = hostapd_drv_switch_channel(hapd, settings);
36575b9c547cSRui Paulo 	free_beacon_data(&settings->beacon_csa);
36585b9c547cSRui Paulo 	free_beacon_data(&settings->beacon_after);
36595b9c547cSRui Paulo 
36605b9c547cSRui Paulo 	if (ret) {
36615b9c547cSRui Paulo 		/* if we failed, clean cs parameters */
36625b9c547cSRui Paulo 		hostapd_cleanup_cs_params(hapd);
36635b9c547cSRui Paulo 		return ret;
36645b9c547cSRui Paulo 	}
36655b9c547cSRui Paulo 
36665b9c547cSRui Paulo 	hapd->csa_in_progress = 1;
36675b9c547cSRui Paulo 	return 0;
36685b9c547cSRui Paulo }
36695b9c547cSRui Paulo 
36705b9c547cSRui Paulo 
36715b9c547cSRui Paulo void
hostapd_switch_channel_fallback(struct hostapd_iface * iface,const struct hostapd_freq_params * freq_params)36725b9c547cSRui Paulo hostapd_switch_channel_fallback(struct hostapd_iface *iface,
36735b9c547cSRui Paulo 				const struct hostapd_freq_params *freq_params)
36745b9c547cSRui Paulo {
3675206b73d0SCy Schubert 	int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
36765b9c547cSRui Paulo 
36775b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
36785b9c547cSRui Paulo 
36795b9c547cSRui Paulo 	if (freq_params->center_freq1)
3680206b73d0SCy Schubert 		seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
36815b9c547cSRui Paulo 	if (freq_params->center_freq2)
3682206b73d0SCy Schubert 		seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
36835b9c547cSRui Paulo 
36845b9c547cSRui Paulo 	switch (freq_params->bandwidth) {
36855b9c547cSRui Paulo 	case 0:
36865b9c547cSRui Paulo 	case 20:
36875b9c547cSRui Paulo 	case 40:
3688206b73d0SCy Schubert 		bw = CHANWIDTH_USE_HT;
36895b9c547cSRui Paulo 		break;
36905b9c547cSRui Paulo 	case 80:
36915b9c547cSRui Paulo 		if (freq_params->center_freq2)
3692206b73d0SCy Schubert 			bw = CHANWIDTH_80P80MHZ;
36935b9c547cSRui Paulo 		else
3694206b73d0SCy Schubert 			bw = CHANWIDTH_80MHZ;
36955b9c547cSRui Paulo 		break;
36965b9c547cSRui Paulo 	case 160:
3697206b73d0SCy Schubert 		bw = CHANWIDTH_160MHZ;
36985b9c547cSRui Paulo 		break;
36995b9c547cSRui Paulo 	default:
37005b9c547cSRui Paulo 		wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
37015b9c547cSRui Paulo 			   freq_params->bandwidth);
37025b9c547cSRui Paulo 		break;
37035b9c547cSRui Paulo 	}
37045b9c547cSRui Paulo 
37055b9c547cSRui Paulo 	iface->freq = freq_params->freq;
37065b9c547cSRui Paulo 	iface->conf->channel = freq_params->channel;
37075b9c547cSRui Paulo 	iface->conf->secondary_channel = freq_params->sec_channel_offset;
3708206b73d0SCy Schubert 	hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3709206b73d0SCy Schubert 	hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3710206b73d0SCy Schubert 	hostapd_set_oper_chwidth(iface->conf, bw);
37115b9c547cSRui Paulo 	iface->conf->ieee80211n = freq_params->ht_enabled;
37125b9c547cSRui Paulo 	iface->conf->ieee80211ac = freq_params->vht_enabled;
3713206b73d0SCy Schubert 	iface->conf->ieee80211ax = freq_params->he_enabled;
37145b9c547cSRui Paulo 
37155b9c547cSRui Paulo 	/*
37165b9c547cSRui Paulo 	 * cs_params must not be cleared earlier because the freq_params
37175b9c547cSRui Paulo 	 * argument may actually point to one of these.
371885732ac8SCy Schubert 	 * These params will be cleared during interface disable below.
37195b9c547cSRui Paulo 	 */
37205b9c547cSRui Paulo 	hostapd_disable_iface(iface);
37215b9c547cSRui Paulo 	hostapd_enable_iface(iface);
37225b9c547cSRui Paulo }
37235b9c547cSRui Paulo 
3724780fb4a2SCy Schubert #endif /* NEED_AP_MLME */
3725780fb4a2SCy Schubert 
3726325151a3SRui Paulo 
hostapd_get_iface(struct hapd_interfaces * interfaces,const char * ifname)3727325151a3SRui Paulo struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3728325151a3SRui Paulo 					const char *ifname)
3729325151a3SRui Paulo {
3730325151a3SRui Paulo 	size_t i, j;
3731325151a3SRui Paulo 
3732325151a3SRui Paulo 	for (i = 0; i < interfaces->count; i++) {
3733325151a3SRui Paulo 		struct hostapd_iface *iface = interfaces->iface[i];
3734325151a3SRui Paulo 
3735325151a3SRui Paulo 		for (j = 0; j < iface->num_bss; j++) {
3736325151a3SRui Paulo 			struct hostapd_data *hapd = iface->bss[j];
3737325151a3SRui Paulo 
3738325151a3SRui Paulo 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
3739325151a3SRui Paulo 				return hapd;
3740325151a3SRui Paulo 		}
3741325151a3SRui Paulo 	}
3742325151a3SRui Paulo 
3743325151a3SRui Paulo 	return NULL;
3744325151a3SRui Paulo }
3745325151a3SRui Paulo 
3746325151a3SRui Paulo 
hostapd_periodic_iface(struct hostapd_iface * iface)3747325151a3SRui Paulo void hostapd_periodic_iface(struct hostapd_iface *iface)
3748325151a3SRui Paulo {
3749325151a3SRui Paulo 	size_t i;
3750325151a3SRui Paulo 
3751325151a3SRui Paulo 	ap_list_timer(iface);
3752325151a3SRui Paulo 
3753325151a3SRui Paulo 	for (i = 0; i < iface->num_bss; i++) {
3754325151a3SRui Paulo 		struct hostapd_data *hapd = iface->bss[i];
3755325151a3SRui Paulo 
3756325151a3SRui Paulo 		if (!hapd->started)
3757325151a3SRui Paulo 			continue;
3758325151a3SRui Paulo 
3759325151a3SRui Paulo #ifndef CONFIG_NO_RADIUS
3760325151a3SRui Paulo 		hostapd_acl_expire(hapd);
3761325151a3SRui Paulo #endif /* CONFIG_NO_RADIUS */
3762325151a3SRui Paulo 	}
3763325151a3SRui Paulo }
3764c1d255d3SCy Schubert 
3765c1d255d3SCy Schubert 
3766c1d255d3SCy Schubert #ifdef CONFIG_OCV
hostapd_ocv_check_csa_sa_query(void * eloop_ctx,void * timeout_ctx)3767c1d255d3SCy Schubert void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
3768c1d255d3SCy Schubert {
3769c1d255d3SCy Schubert 	struct hostapd_data *hapd = eloop_ctx;
3770c1d255d3SCy Schubert 	struct sta_info *sta;
3771c1d255d3SCy Schubert 
3772c1d255d3SCy Schubert 	wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
3773c1d255d3SCy Schubert 
3774c1d255d3SCy Schubert 	for (sta = hapd->sta_list; sta; sta = sta->next) {
3775c1d255d3SCy Schubert 		if (!sta->post_csa_sa_query)
3776c1d255d3SCy Schubert 			continue;
3777c1d255d3SCy Schubert 
3778c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
3779c1d255d3SCy Schubert 			   " did not start SA Query after CSA - disconnect",
3780c1d255d3SCy Schubert 			   MAC2STR(sta->addr));
3781c1d255d3SCy Schubert 		ap_sta_disconnect(hapd, sta, sta->addr,
3782c1d255d3SCy Schubert 				  WLAN_REASON_PREV_AUTH_NOT_VALID);
3783c1d255d3SCy Schubert 	}
3784c1d255d3SCy Schubert }
3785c1d255d3SCy Schubert #endif /* CONFIG_OCV */
3786