blob: 4b88641a2dde9241b709b85f7c4920687decae11 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Initialization and configuration
Hai Shaloma20dcd72022-02-04 13:43:00 -08003 * Copyright (c) 2002-2021, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
Hai Shalomc3565922019-10-28 11:58:20 -070010#ifdef CONFIG_SQLITE
11#include <sqlite3.h>
12#endif /* CONFIG_SQLITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013
14#include "utils/common.h"
15#include "utils/eloop.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070016#include "utils/crc32.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "common/ieee802_11_defs.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080018#include "common/wpa_ctrl.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080019#include "common/hw_features_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "radius/radius_client.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "radius/radius_das.h"
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070022#include "eap_server/tncs.h"
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080023#include "eapol_auth/eapol_auth_sm.h"
24#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080025#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "hostapd.h"
27#include "authsrv.h"
28#include "sta_info.h"
29#include "accounting.h"
30#include "ap_list.h"
31#include "beacon.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "ieee802_1x.h"
33#include "ieee802_11_auth.h"
34#include "vlan_init.h"
35#include "wpa_auth.h"
36#include "wps_hostapd.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070037#include "dpp_hostapd.h"
38#include "gas_query_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "hw_features.h"
40#include "wpa_auth_glue.h"
41#include "ap_drv_ops.h"
42#include "ap_config.h"
43#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070044#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070045#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070046#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080047#include "bss_load.h"
48#include "x_snoop.h"
49#include "dhcp_snoop.h"
50#include "ndisc_snoop.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070051#include "neighbor_db.h"
52#include "rrm.h"
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080053#include "fils_hlp.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070054#include "acs.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070055#include "hs20.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070056#include "airtime_policy.h"
57#include "wpa_auth_kay.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058
59
Dmitry Shmidt04949592012-07-19 12:16:46 -070060static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Hai Shalomfdcde762020-04-02 11:19:20 -070061#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070063static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -070064#endif /* CONFIG_WEP */
Dmitry Shmidtcce06662013-11-04 18:44:24 -080065static int setup_interface2(struct hostapd_iface *iface);
66static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Roshan Pius3a1667e2018-07-03 15:17:14 -070067static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
68 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070
Dmitry Shmidt04949592012-07-19 12:16:46 -070071int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
72 int (*cb)(struct hostapd_iface *iface,
73 void *ctx), void *ctx)
74{
75 size_t i;
76 int ret;
77
78 for (i = 0; i < interfaces->count; i++) {
Hai Shalom4fbc08f2020-05-18 12:37:00 -070079 if (!interfaces->iface[i])
80 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -070081 ret = cb(interfaces->iface[i], ctx);
82 if (ret)
83 return ret;
84 }
85
86 return 0;
87}
88
89
Hai Shalomce48b4a2018-09-05 11:41:35 -070090void hostapd_reconfig_encryption(struct hostapd_data *hapd)
91{
92 if (hapd->wpa_auth)
93 return;
94
95 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -070096#ifdef CONFIG_WEP
Hai Shalomce48b4a2018-09-05 11:41:35 -070097 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -070098#endif /* CONFIG_WEP */
Hai Shalomce48b4a2018-09-05 11:41:35 -070099}
100
101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102static void hostapd_reload_bss(struct hostapd_data *hapd)
103{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800104 struct hostapd_ssid *ssid;
105
Dmitry Shmidt29333592017-01-09 12:27:11 -0800106 if (!hapd->started)
107 return;
108
Roshan Pius3a1667e2018-07-03 15:17:14 -0700109 if (hapd->conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -0800110 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
111 hapd->iconf->ieee80211ax;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113#ifndef CONFIG_NO_RADIUS
114 radius_client_reconfig(hapd->radius, hapd->conf->radius);
115#endif /* CONFIG_NO_RADIUS */
116
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800117 ssid = &hapd->conf->ssid;
118 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
119 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
120 /*
121 * Force PSK to be derived again since SSID or passphrase may
122 * have changed.
123 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800124 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800125 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 if (hostapd_setup_wpa_psk(hapd->conf)) {
127 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
128 "after reloading configuration");
129 }
130
131 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
132 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
133 else
134 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
135
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800136 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800138 if (hapd->wpa_auth)
139 wpa_init_keys(hapd->wpa_auth);
140 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 const u8 *wpa_ie;
142 size_t wpa_ie_len;
143 hostapd_reconfig_wpa(hapd);
144 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
145 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
146 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
147 "the kernel driver.");
148 } else if (hapd->wpa_auth) {
149 wpa_deinit(hapd->wpa_auth);
150 hapd->wpa_auth = NULL;
151 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700152#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700154#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
156 }
157
158 ieee802_11_set_beacon(hapd);
159 hostapd_update_wps(hapd);
160
161 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700162 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 hapd->conf->ssid.ssid_len)) {
164 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
165 /* try to continue */
166 }
167 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
168}
169
170
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700171static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 size_t j;
174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700175 /*
176 * Deauthenticate all stations since the new configuration may not
177 * allow them to use the BSS anymore.
178 */
179 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700180 hostapd_flush_old_stations(iface->bss[j],
181 WLAN_REASON_PREV_AUTH_NOT_VALID);
Hai Shalomfdcde762020-04-02 11:19:20 -0700182#ifdef CONFIG_WEP
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700183 hostapd_broadcast_wep_clear(iface->bss[j]);
Hai Shalomfdcde762020-04-02 11:19:20 -0700184#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185
186#ifndef CONFIG_NO_RADIUS
187 /* TODO: update dynamic data based on changed configuration
188 * items (e.g., open/close sockets, etc.) */
189 radius_client_flush(iface->bss[j]->radius, 0);
190#endif /* CONFIG_NO_RADIUS */
191 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700192}
193
194
Hai Shalom74f70d42019-02-11 14:42:39 -0800195static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
196 struct hostapd_config *oldconf)
197{
198 size_t i;
199
200 if (newconf->num_bss != oldconf->num_bss)
201 return 1;
202
203 for (i = 0; i < newconf->num_bss; i++) {
204 if (os_strcmp(newconf->bss[i]->iface,
205 oldconf->bss[i]->iface) != 0)
206 return 1;
207 }
208
209 return 0;
210}
211
212
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700213int hostapd_reload_config(struct hostapd_iface *iface)
214{
Hai Shalom74f70d42019-02-11 14:42:39 -0800215 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700216 struct hostapd_data *hapd = iface->bss[0];
217 struct hostapd_config *newconf, *oldconf;
218 size_t j;
219
220 if (iface->config_fname == NULL) {
221 /* Only in-memory config in use - assume it has been updated */
222 hostapd_clear_old(iface);
223 for (j = 0; j < iface->num_bss; j++)
224 hostapd_reload_bss(iface->bss[j]);
225 return 0;
226 }
227
228 if (iface->interfaces == NULL ||
229 iface->interfaces->config_read_cb == NULL)
230 return -1;
231 newconf = iface->interfaces->config_read_cb(iface->config_fname);
232 if (newconf == NULL)
233 return -1;
234
235 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236
237 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800238 if (hostapd_iface_conf_changed(newconf, oldconf)) {
239 char *fname;
240 int res;
241
242 wpa_printf(MSG_DEBUG,
243 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
244 fname = os_strdup(iface->config_fname);
245 if (!fname) {
246 hostapd_config_free(newconf);
247 return -1;
248 }
249 hostapd_remove_iface(interfaces, hapd->conf->iface);
250 iface = hostapd_init(interfaces, fname);
251 os_free(fname);
252 hostapd_config_free(newconf);
253 if (!iface) {
254 wpa_printf(MSG_ERROR,
255 "Failed to initialize interface on config reload");
256 return -1;
257 }
258 iface->interfaces = interfaces;
259 interfaces->iface[interfaces->count] = iface;
260 interfaces->count++;
261 res = hostapd_enable_iface(iface);
262 if (res < 0)
263 wpa_printf(MSG_ERROR,
264 "Failed to enable interface on config reload");
265 return res;
266 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 iface->conf = newconf;
268
269 for (j = 0; j < iface->num_bss; j++) {
270 hapd = iface->bss[j];
271 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700272 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700273 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700274 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700275 hapd->iconf->ieee80211n = oldconf->ieee80211n;
276 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
277 hapd->iconf->ht_capab = oldconf->ht_capab;
278 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700279 hostapd_set_oper_chwidth(hapd->iconf,
280 hostapd_get_oper_chwidth(oldconf));
281 hostapd_set_oper_centr_freq_seg0_idx(
282 hapd->iconf,
283 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
284 hostapd_set_oper_centr_freq_seg1_idx(
285 hapd->iconf,
286 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800287 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 hostapd_reload_bss(hapd);
289 }
290
291 hostapd_config_free(oldconf);
292
293
294 return 0;
295}
296
297
Hai Shalomfdcde762020-04-02 11:19:20 -0700298#ifdef CONFIG_WEP
299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700301 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302{
303 int i;
304
Dmitry Shmidt29333592017-01-09 12:27:11 -0800305 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700306 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700308 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
309 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 wpa_printf(MSG_DEBUG, "Failed to clear default "
311 "encryption keys (ifname=%s keyidx=%d)",
312 ifname, i);
313 }
314 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 if (hapd->conf->ieee80211w) {
316 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
317 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700318 NULL, i, 0, 0, NULL,
319 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 wpa_printf(MSG_DEBUG, "Failed to clear "
321 "default mgmt encryption keys "
322 "(ifname=%s keyidx=%d)", ifname, i);
323 }
324 }
325 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326}
327
328
329static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
330{
331 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
332 return 0;
333}
334
335
336static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
337{
338 int errors = 0, idx;
339 struct hostapd_ssid *ssid = &hapd->conf->ssid;
340
341 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700342 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700344 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700346 ssid->wep.len[idx],
347 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
349 errors++;
350 }
351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 return errors;
353}
354
Hai Shalomfdcde762020-04-02 11:19:20 -0700355#endif /* CONFIG_WEP */
356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357
Hai Shalom60840252021-02-19 19:02:11 -0800358void hostapd_free_hapd_data(struct hostapd_data *hapd)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700359{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800360 os_free(hapd->probereq_cb);
361 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800362 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800363
364#ifdef CONFIG_P2P
365 wpabuf_free(hapd->p2p_beacon_ie);
366 hapd->p2p_beacon_ie = NULL;
367 wpabuf_free(hapd->p2p_probe_resp_ie);
368 hapd->p2p_probe_resp_ie = NULL;
369#endif /* CONFIG_P2P */
370
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800371 if (!hapd->started) {
372 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700373 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800374 return;
375 }
376 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700377 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800378
Dmitry Shmidt54605472013-11-08 11:10:19 -0800379 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 accounting_deinit(hapd);
381 hostapd_deinit_wpa(hapd);
382 vlan_deinit(hapd);
383 hostapd_acl_deinit(hapd);
384#ifndef CONFIG_NO_RADIUS
385 radius_client_deinit(hapd->radius);
386 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700387 radius_das_deinit(hapd->radius_das);
388 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389#endif /* CONFIG_NO_RADIUS */
390
391 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700392 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700393#ifdef CONFIG_DPP
394 hostapd_dpp_deinit(hapd);
395 gas_query_ap_deinit(hapd->gas);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800396 hapd->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700397#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398
399 authsrv_deinit(hapd);
400
Dmitry Shmidt71757432014-06-02 13:50:35 -0700401 if (hapd->interface_added) {
402 hapd->interface_added = 0;
403 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
404 wpa_printf(MSG_WARNING,
405 "Failed to remove BSS interface %s",
406 hapd->conf->iface);
407 hapd->interface_added = 1;
408 } else {
409 /*
410 * Since this was a dynamically added interface, the
411 * driver wrapper may have removed its internal instance
412 * and hapd->drv_priv is not valid anymore.
413 */
414 hapd->drv_priv = NULL;
415 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 }
417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800418 wpabuf_free(hapd->time_adv);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800419 hapd->time_adv = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700420
421#ifdef CONFIG_INTERWORKING
422 gas_serv_deinit(hapd);
423#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800424
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800425 bss_load_update_deinit(hapd);
426 ndisc_snoop_deinit(hapd);
427 dhcp_snoop_deinit(hapd);
428 x_snoop_deinit(hapd);
429
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800430#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700431 bin_clear_free(hapd->tmp_eap_user.identity,
432 hapd->tmp_eap_user.identity_len);
433 bin_clear_free(hapd->tmp_eap_user.password,
434 hapd->tmp_eap_user.password_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800435 os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800436#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800437
438#ifdef CONFIG_MESH
439 wpabuf_free(hapd->mesh_pending_auth);
440 hapd->mesh_pending_auth = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800441 /* handling setup failure is already done */
442 hapd->setup_complete_cb = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800443#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700444
445 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800446 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700447
Hai Shalom899fcc72020-10-19 14:38:18 -0700448#ifdef CONFIG_OCV
449 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
450#endif /* CONFIG_OCV */
451
Hai Shalom021b0b52019-04-10 11:17:58 -0700452#ifdef CONFIG_SAE
453 {
454 struct hostapd_sae_commit_queue *q;
455
456 while ((q = dl_list_first(&hapd->sae_commit_queue,
457 struct hostapd_sae_commit_queue,
458 list))) {
459 dl_list_del(&q->list);
460 os_free(q);
461 }
462 }
463 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
464#endif /* CONFIG_SAE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700465}
466
467
468/**
469 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
470 * @hapd: Pointer to BSS data
471 *
472 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800473 * Most of the modules that are initialized in hostapd_setup_bss() are
474 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700475 */
476static void hostapd_cleanup(struct hostapd_data *hapd)
477{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800478 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700479 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700480 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800481 hapd->iface->interfaces->ctrl_iface_deinit) {
482 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700483 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800484 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700485 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486}
487
488
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800489static void sta_track_deinit(struct hostapd_iface *iface)
490{
491 struct hostapd_sta_info *info;
492
493 if (!iface->num_sta_seen)
494 return;
495
496 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
497 list))) {
498 dl_list_del(&info->list);
499 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700500 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800501 }
502}
503
504
Hai Shalom60840252021-02-19 19:02:11 -0800505void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700506{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800507 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800508#ifdef NEED_AP_MLME
509 hostapd_stop_setup_timers(iface);
510#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700511 if (iface->current_mode)
512 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700513 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
514 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700515 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700516 os_free(iface->current_rates);
517 iface->current_rates = NULL;
518 os_free(iface->basic_rates);
519 iface->basic_rates = NULL;
520 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800521 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700522 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700523}
524
525
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526/**
527 * hostapd_cleanup_iface - Complete per-interface cleanup
528 * @iface: Pointer to interface data
529 *
530 * This function is called after per-BSS data structures are deinitialized
531 * with hostapd_cleanup().
532 */
533static void hostapd_cleanup_iface(struct hostapd_iface *iface)
534{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800535 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800536 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700537 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
538 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800539
Dmitry Shmidt04949592012-07-19 12:16:46 -0700540 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 hostapd_config_free(iface->conf);
542 iface->conf = NULL;
543
544 os_free(iface->config_fname);
545 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800546 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 os_free(iface);
548}
549
550
Hai Shalomfdcde762020-04-02 11:19:20 -0700551#ifdef CONFIG_WEP
552
Dmitry Shmidt04949592012-07-19 12:16:46 -0700553static void hostapd_clear_wep(struct hostapd_data *hapd)
554{
Hai Shalom021b0b52019-04-10 11:17:58 -0700555 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700556 hostapd_set_privacy(hapd, 0);
557 hostapd_broadcast_wep_clear(hapd);
558 }
559}
560
561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
563{
564 int i;
565
566 hostapd_broadcast_wep_set(hapd);
567
568 if (hapd->conf->ssid.wep.default_len) {
569 hostapd_set_privacy(hapd, 1);
570 return 0;
571 }
572
Jouni Malinen75ecf522011-06-27 15:19:46 -0700573 /*
574 * When IEEE 802.1X is not enabled, the driver may need to know how to
575 * set authentication algorithms for static WEP.
576 */
577 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 for (i = 0; i < 4; i++) {
580 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700581 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 i == hapd->conf->ssid.wep.idx, NULL, 0,
583 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700584 hapd->conf->ssid.wep.len[i],
585 i == hapd->conf->ssid.wep.idx ?
586 KEY_FLAG_GROUP_RX_TX_DEFAULT :
587 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 wpa_printf(MSG_WARNING, "Could not set WEP "
589 "encryption.");
590 return -1;
591 }
592 if (hapd->conf->ssid.wep.key[i] &&
593 i == hapd->conf->ssid.wep.idx)
594 hostapd_set_privacy(hapd, 1);
595 }
596
597 return 0;
598}
599
Hai Shalomfdcde762020-04-02 11:19:20 -0700600#endif /* CONFIG_WEP */
601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602
Dmitry Shmidt04949592012-07-19 12:16:46 -0700603static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604{
605 int ret = 0;
606 u8 addr[ETH_ALEN];
607
608 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
609 return 0;
610
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800611 if (!hapd->iface->driver_ap_teardown) {
612 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
613 "Flushing old station entries");
614
615 if (hostapd_flush(hapd)) {
616 wpa_msg(hapd->msg_ctx, MSG_WARNING,
617 "Could not connect to kernel driver");
618 ret = -1;
619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700621 if (hapd->conf && hapd->conf->broadcast_deauth) {
622 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
623 "Deauthenticate all stations");
624 os_memset(addr, 0xff, ETH_ALEN);
625 hostapd_drv_sta_deauth(hapd, addr, reason);
626 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 hostapd_free_stas(hapd);
628
629 return ret;
630}
631
632
Hai Shalom60840252021-02-19 19:02:11 -0800633void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
Dmitry Shmidt71757432014-06-02 13:50:35 -0700634{
635 hostapd_free_stas(hapd);
636 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700637#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700638 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700639#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700640}
641
642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643/**
644 * hostapd_validate_bssid_configuration - Validate BSSID configuration
645 * @iface: Pointer to interface data
646 * Returns: 0 on success, -1 on failure
647 *
648 * This function is used to validate that the configured BSSIDs are valid.
649 */
650static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
651{
652 u8 mask[ETH_ALEN] = { 0 };
653 struct hostapd_data *hapd = iface->bss[0];
654 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 int auto_addr = 0;
656
657 if (hostapd_drv_none(hapd))
658 return 0;
659
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800660 if (iface->conf->use_driver_iface_addr)
661 return 0;
662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
664
665 /* Determine the bits necessary to cover the number of BSSIDs. */
666 for (i--; i; i >>= 1)
667 bits++;
668
669 /* Determine the bits necessary to any configured BSSIDs,
670 if they are higher than the number of BSSIDs. */
671 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800672 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673 if (j)
674 auto_addr++;
675 continue;
676 }
677
678 for (i = 0; i < ETH_ALEN; i++) {
679 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800680 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 hapd->own_addr[i];
682 }
683 }
684
685 if (!auto_addr)
686 goto skip_mask_ext;
687
688 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
689 ;
690 j = 0;
691 if (i < ETH_ALEN) {
692 j = (5 - i) * 8;
693
694 while (mask[i] != 0) {
695 mask[i] >>= 1;
696 j++;
697 }
698 }
699
700 if (bits < j)
701 bits = j;
702
703 if (bits > 40) {
704 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
705 bits);
706 return -1;
707 }
708
709 os_memset(mask, 0xff, ETH_ALEN);
710 j = bits / 8;
711 for (i = 5; i > 5 - j; i--)
712 mask[i] = 0;
713 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700714 while (j) {
715 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700717 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718
719skip_mask_ext:
720 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
721 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 if (!auto_addr)
724 return 0;
725
726 for (i = 0; i < ETH_ALEN; i++) {
727 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
728 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
729 " for start address " MACSTR ".",
730 MAC2STR(mask), MAC2STR(hapd->own_addr));
731 wpa_printf(MSG_ERROR, "Start address must be the "
732 "first address in the block (i.e., addr "
733 "AND mask == addr).");
734 return -1;
735 }
736 }
737
738 return 0;
739}
740
741
742static int mac_in_conf(struct hostapd_config *conf, const void *a)
743{
744 size_t i;
745
746 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800747 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 return 1;
749 }
750 }
751
752 return 0;
753}
754
755
Dmitry Shmidt04949592012-07-19 12:16:46 -0700756#ifndef CONFIG_NO_RADIUS
757
758static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
759 struct radius_das_attrs *attr)
760{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800761 if (attr->nas_identifier &&
762 (!hapd->conf->nas_identifier ||
763 os_strlen(hapd->conf->nas_identifier) !=
764 attr->nas_identifier_len ||
765 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
766 attr->nas_identifier_len) != 0)) {
767 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
768 return 1;
769 }
770
771 if (attr->nas_ip_addr &&
772 (hapd->conf->own_ip_addr.af != AF_INET ||
773 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
774 0)) {
775 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
776 return 1;
777 }
778
779#ifdef CONFIG_IPV6
780 if (attr->nas_ipv6_addr &&
781 (hapd->conf->own_ip_addr.af != AF_INET6 ||
782 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
783 != 0)) {
784 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
785 return 1;
786 }
787#endif /* CONFIG_IPV6 */
788
Dmitry Shmidt04949592012-07-19 12:16:46 -0700789 return 0;
790}
791
792
793static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800794 struct radius_das_attrs *attr,
795 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700796{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800797 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700798 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800799 int num_attr = 0;
800 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700801
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800802 *multi = 0;
803
804 for (sta = hapd->sta_list; sta; sta = sta->next)
805 sta->radius_das_match = 1;
806
807 if (attr->sta_addr) {
808 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700809 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800810 if (!sta) {
811 wpa_printf(MSG_DEBUG,
812 "RADIUS DAS: No Calling-Station-Id match");
813 return NULL;
814 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700815
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800816 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800818 if (sta != selected)
819 sta->radius_das_match = 0;
820 }
821 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
822 }
823
824 if (attr->acct_session_id) {
825 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800826 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800827 wpa_printf(MSG_DEBUG,
828 "RADIUS DAS: Acct-Session-Id cannot match");
829 return NULL;
830 }
831 count = 0;
832
833 for (sta = hapd->sta_list; sta; sta = sta->next) {
834 if (!sta->radius_das_match)
835 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800836 os_snprintf(buf, sizeof(buf), "%016llX",
837 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800838 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800839 sta->radius_das_match = 0;
840 else
841 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700842 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800843
844 if (count == 0) {
845 wpa_printf(MSG_DEBUG,
846 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
847 return NULL;
848 }
849 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700850 }
851
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800852 if (attr->acct_multi_session_id) {
853 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800854 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800855 wpa_printf(MSG_DEBUG,
856 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
857 return NULL;
858 }
859 count = 0;
860
861 for (sta = hapd->sta_list; sta; sta = sta->next) {
862 if (!sta->radius_das_match)
863 continue;
864 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800865 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800866 sta->radius_das_match = 0;
867 continue;
868 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800869 os_snprintf(buf, sizeof(buf), "%016llX",
870 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800871 sta->eapol_sm->acct_multi_session_id);
872 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800873 0)
874 sta->radius_das_match = 0;
875 else
876 count++;
877 }
878
879 if (count == 0) {
880 wpa_printf(MSG_DEBUG,
881 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
882 return NULL;
883 }
884 wpa_printf(MSG_DEBUG,
885 "RADIUS DAS: Acct-Multi-Session-Id match");
886 }
887
888 if (attr->cui) {
889 num_attr++;
890 count = 0;
891
Dmitry Shmidt04949592012-07-19 12:16:46 -0700892 for (sta = hapd->sta_list; sta; sta = sta->next) {
893 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800894
895 if (!sta->radius_das_match)
896 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700897 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800898 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700899 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800900 attr->cui_len) != 0)
901 sta->radius_das_match = 0;
902 else
903 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700904 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800905
906 if (count == 0) {
907 wpa_printf(MSG_DEBUG,
908 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
909 return NULL;
910 }
911 wpa_printf(MSG_DEBUG,
912 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700913 }
914
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800915 if (attr->user_name) {
916 num_attr++;
917 count = 0;
918
Dmitry Shmidt04949592012-07-19 12:16:46 -0700919 for (sta = hapd->sta_list; sta; sta = sta->next) {
920 u8 *identity;
921 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800922
923 if (!sta->radius_das_match)
924 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700925 identity = ieee802_1x_get_identity(sta->eapol_sm,
926 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800927 if (!identity ||
928 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700929 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800930 != 0)
931 sta->radius_das_match = 0;
932 else
933 count++;
934 }
935
936 if (count == 0) {
937 wpa_printf(MSG_DEBUG,
938 "RADIUS DAS: No matches remaining after User-Name check");
939 return NULL;
940 }
941 wpa_printf(MSG_DEBUG,
942 "RADIUS DAS: User-Name match");
943 }
944
945 if (num_attr == 0) {
946 /*
947 * In theory, we could match all current associations, but it
948 * seems safer to just reject requests that do not include any
949 * session identification attributes.
950 */
951 wpa_printf(MSG_DEBUG,
952 "RADIUS DAS: No session identification attributes included");
953 return NULL;
954 }
955
956 selected = NULL;
957 for (sta = hapd->sta_list; sta; sta = sta->next) {
958 if (sta->radius_das_match) {
959 if (selected) {
960 *multi = 1;
961 return NULL;
962 }
963 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700964 }
965 }
966
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800967 return selected;
968}
969
970
971static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
972 struct radius_das_attrs *attr)
973{
974 if (!hapd->wpa_auth)
975 return -1;
976 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700977}
978
979
980static enum radius_das_res
981hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
982{
983 struct hostapd_data *hapd = ctx;
984 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800985 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700986
987 if (hostapd_das_nas_mismatch(hapd, attr))
988 return RADIUS_DAS_NAS_MISMATCH;
989
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800990 sta = hostapd_das_find_sta(hapd, attr, &multi);
991 if (sta == NULL) {
992 if (multi) {
993 wpa_printf(MSG_DEBUG,
994 "RADIUS DAS: Multiple sessions match - not supported");
995 return RADIUS_DAS_MULTI_SESSION_MATCH;
996 }
997 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
998 wpa_printf(MSG_DEBUG,
999 "RADIUS DAS: PMKSA cache entry matched");
1000 return RADIUS_DAS_SUCCESS;
1001 }
1002 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001003 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001004 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001006 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1007 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001008 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1009
Dmitry Shmidt04949592012-07-19 12:16:46 -07001010 hostapd_drv_sta_deauth(hapd, sta->addr,
1011 WLAN_REASON_PREV_AUTH_NOT_VALID);
1012 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1013
1014 return RADIUS_DAS_SUCCESS;
1015}
1016
Roshan Pius3a1667e2018-07-03 15:17:14 -07001017
1018#ifdef CONFIG_HS20
1019static enum radius_das_res
1020hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1021{
1022 struct hostapd_data *hapd = ctx;
1023 struct sta_info *sta;
1024 int multi;
1025
1026 if (hostapd_das_nas_mismatch(hapd, attr))
1027 return RADIUS_DAS_NAS_MISMATCH;
1028
1029 sta = hostapd_das_find_sta(hapd, attr, &multi);
1030 if (!sta) {
1031 if (multi) {
1032 wpa_printf(MSG_DEBUG,
1033 "RADIUS DAS: Multiple sessions match - not supported");
1034 return RADIUS_DAS_MULTI_SESSION_MATCH;
1035 }
1036 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1037 return RADIUS_DAS_SESSION_NOT_FOUND;
1038 }
1039
1040 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1041 " - CoA", MAC2STR(sta->addr));
1042
1043 if (attr->hs20_t_c_filtering) {
1044 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1045 wpa_printf(MSG_DEBUG,
1046 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1047 return RADIUS_DAS_COA_FAILED;
1048 }
1049
1050 hs20_t_c_filtering(hapd, sta, 0);
1051 }
1052
1053 return RADIUS_DAS_SUCCESS;
1054}
1055#else /* CONFIG_HS20 */
1056#define hostapd_das_coa NULL
1057#endif /* CONFIG_HS20 */
1058
Hai Shalomc3565922019-10-28 11:58:20 -07001059
1060#ifdef CONFIG_SQLITE
1061
1062static int db_table_exists(sqlite3 *db, const char *name)
1063{
1064 char cmd[128];
1065
1066 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1067 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1068}
1069
1070
1071static int db_table_create_radius_attributes(sqlite3 *db)
1072{
1073 char *err = NULL;
1074 const char *sql =
1075 "CREATE TABLE radius_attributes("
1076 " id INTEGER PRIMARY KEY,"
1077 " sta TEXT,"
1078 " reqtype TEXT,"
1079 " attr TEXT"
1080 ");"
1081 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1082
1083 wpa_printf(MSG_DEBUG,
1084 "Adding database table for RADIUS attribute information");
1085 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1086 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1087 sqlite3_free(err);
1088 return -1;
1089 }
1090
1091 return 0;
1092}
1093
1094#endif /* CONFIG_SQLITE */
1095
Dmitry Shmidt04949592012-07-19 12:16:46 -07001096#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097
1098
1099/**
1100 * hostapd_setup_bss - Per-BSS setup (initialization)
1101 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001102 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1103 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 *
1105 * This function is used to initialize all per-BSS data structures and
1106 * resources. This gets called in a loop for each BSS when an interface is
1107 * initialized. Most of the modules that are initialized here will be
1108 * deinitialized in hostapd_cleanup().
1109 */
1110static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1111{
1112 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001113 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 int ssid_len, set_ssid;
1115 char force_ifname[IFNAMSIZ];
1116 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001117 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118
Dmitry Shmidt54605472013-11-08 11:10:19 -08001119 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001120 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001121
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001122#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001123 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001124 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1125 return -1;
1126 }
1127#endif /* EAP_SERVER_TNC */
1128
Dmitry Shmidt54605472013-11-08 11:10:19 -08001129 if (hapd->started) {
1130 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001131 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001132 return -1;
1133 }
1134 hapd->started = 1;
1135
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001136 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001137 u8 *addr = hapd->own_addr;
1138
1139 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001141 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142
1143 if (hostapd_mac_comp(hapd->own_addr,
1144 hapd->iface->bss[0]->own_addr) ==
1145 0) {
1146 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1147 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001148 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 return -1;
1150 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001151 } else if (hapd->iconf->use_driver_iface_addr) {
1152 addr = NULL;
1153 } else {
1154 /* Allocate the next available BSSID. */
1155 do {
1156 inc_byte_array(hapd->own_addr, ETH_ALEN);
1157 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 }
1159
Dmitry Shmidt54605472013-11-08 11:10:19 -08001160 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001162 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001164 conf->bridge[0] ? conf->bridge : NULL,
1165 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1167 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001168 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 return -1;
1170 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001171
1172 if (!addr)
1173 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 }
1175
1176 if (conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -08001177 conf->wmm_enabled = hapd->iconf->ieee80211n |
1178 hapd->iconf->ieee80211ax;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001180#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001181 if (is_zero_ether_addr(conf->r1_key_holder))
1182 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001183#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001184
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001185#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001186 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001187 flush_old_stations = 0;
1188#endif /* CONFIG_MESH */
1189
1190 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001191 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 hostapd_set_privacy(hapd, 0);
1193
Hai Shalomfdcde762020-04-02 11:19:20 -07001194#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001195 if (!hostapd_drv_nl80211(hapd))
1196 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001197 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001199#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200
1201 /*
1202 * Fetch the SSID from the system and use it or,
1203 * if one was specified in the config file, verify they
1204 * match.
1205 */
1206 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1207 if (ssid_len < 0) {
1208 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1209 return -1;
1210 }
1211 if (conf->ssid.ssid_set) {
1212 /*
1213 * If SSID is specified in the config file and it differs
1214 * from what is being used then force installation of the
1215 * new SSID.
1216 */
1217 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1218 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1219 } else {
1220 /*
1221 * No SSID in the config file; just use the one we got
1222 * from the system.
1223 */
1224 set_ssid = 0;
1225 conf->ssid.ssid_len = ssid_len;
1226 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 }
1228
Hai Shalomfdcde762020-04-02 11:19:20 -07001229 /*
1230 * Short SSID calculation is identical to FCS and it is defined in
1231 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1232 */
1233 conf->ssid.short_ssid = crc32(conf->ssid.ssid, conf->ssid.ssid_len);
1234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001236 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001237 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001238 conf->iface, MAC2STR(hapd->own_addr),
1239 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 }
1241
1242 if (hostapd_setup_wpa_psk(conf)) {
1243 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1244 return -1;
1245 }
1246
1247 /* Set SSID for the kernel driver (to be used in beacon and probe
1248 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001249 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250 conf->ssid.ssid_len)) {
1251 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1252 return -1;
1253 }
1254
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001255 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 conf->radius->msg_dumps = 1;
1257#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001258
1259#ifdef CONFIG_SQLITE
1260 if (conf->radius_req_attr_sqlite) {
1261 if (sqlite3_open(conf->radius_req_attr_sqlite,
1262 &hapd->rad_attr_db)) {
1263 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1264 conf->radius_req_attr_sqlite);
1265 return -1;
1266 }
1267
1268 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1269 conf->radius_req_attr_sqlite);
1270 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1271 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1272 return -1;
1273 }
1274#endif /* CONFIG_SQLITE */
1275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 hapd->radius = radius_client_init(hapd, conf->radius);
1277 if (hapd->radius == NULL) {
1278 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1279 return -1;
1280 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001281
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001282 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001283 struct radius_das_conf das_conf;
1284 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001285 das_conf.port = conf->radius_das_port;
1286 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001287 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001288 conf->radius_das_shared_secret_len;
1289 das_conf.client_addr = &conf->radius_das_client_addr;
1290 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001291 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001292 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001293 das_conf.require_message_authenticator =
1294 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001295 das_conf.ctx = hapd;
1296 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001297 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001298 hapd->radius_das = radius_das_init(&das_conf);
1299 if (hapd->radius_das == NULL) {
1300 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1301 "failed.");
1302 return -1;
1303 }
1304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305#endif /* CONFIG_NO_RADIUS */
1306
1307 if (hostapd_acl_init(hapd)) {
1308 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1309 return -1;
1310 }
1311 if (hostapd_init_wps(hapd, conf))
1312 return -1;
1313
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001314#ifdef CONFIG_DPP
1315 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1316 if (!hapd->gas)
1317 return -1;
1318 if (hostapd_dpp_init(hapd))
1319 return -1;
1320#endif /* CONFIG_DPP */
1321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 if (authsrv_init(hapd) < 0)
1323 return -1;
1324
1325 if (ieee802_1x_init(hapd)) {
1326 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1327 return -1;
1328 }
1329
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001330 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 return -1;
1332
1333 if (accounting_init(hapd)) {
1334 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1335 return -1;
1336 }
1337
Dmitry Shmidt04949592012-07-19 12:16:46 -07001338#ifdef CONFIG_INTERWORKING
1339 if (gas_serv_init(hapd)) {
1340 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1341 return -1;
1342 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001343
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001344 if (conf->qos_map_set_len &&
1345 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1346 conf->qos_map_set_len)) {
1347 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 return -1;
1349 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001350#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001352 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1353 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1354 return -1;
1355 }
1356
1357 if (conf->proxy_arp) {
1358 if (x_snoop_init(hapd)) {
1359 wpa_printf(MSG_ERROR,
1360 "Generic snooping infrastructure initialization failed");
1361 return -1;
1362 }
1363
1364 if (dhcp_snoop_init(hapd)) {
1365 wpa_printf(MSG_ERROR,
1366 "DHCP snooping initialization failed");
1367 return -1;
1368 }
1369
1370 if (ndisc_snoop_init(hapd)) {
1371 wpa_printf(MSG_ERROR,
1372 "Neighbor Discovery snooping initialization failed");
1373 return -1;
1374 }
1375 }
1376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1378 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1379 return -1;
1380 }
1381
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001382 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001383 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001385 if (flush_old_stations && !conf->start_disabled &&
1386 conf->broadcast_deauth) {
1387 u8 addr[ETH_ALEN];
1388
1389 /* Should any previously associated STA not have noticed that
1390 * the AP had stopped and restarted, send one more
1391 * deauthentication notification now that the AP is ready to
1392 * operate. */
1393 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1394 "Deauthenticate all stations at BSS start");
1395 os_memset(addr, 0xff, ETH_ALEN);
1396 hostapd_drv_sta_deauth(hapd, addr,
1397 WLAN_REASON_PREV_AUTH_NOT_VALID);
1398 }
1399
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001400 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1401 return -1;
1402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 if (hapd->driver && hapd->driver->set_operstate)
1404 hapd->driver->set_operstate(hapd->drv_priv, 1);
1405
1406 return 0;
1407}
1408
1409
1410static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1411{
1412 struct hostapd_data *hapd = iface->bss[0];
1413 int i;
1414 struct hostapd_tx_queue_params *p;
1415
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001416#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001417 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001418 return;
1419#endif /* CONFIG_MESH */
1420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421 for (i = 0; i < NUM_TX_QUEUES; i++) {
1422 p = &iface->conf->tx_queue[i];
1423
1424 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1425 p->cwmax, p->burst)) {
1426 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1427 "parameters for queue %d.", i);
1428 /* Continue anyway */
1429 }
1430 }
1431}
1432
1433
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001434static int hostapd_set_acl_list(struct hostapd_data *hapd,
1435 struct mac_acl_entry *mac_acl,
1436 int n_entries, u8 accept_acl)
1437{
1438 struct hostapd_acl_params *acl_params;
1439 int i, err;
1440
1441 acl_params = os_zalloc(sizeof(*acl_params) +
1442 (n_entries * sizeof(acl_params->mac_acl[0])));
1443 if (!acl_params)
1444 return -ENOMEM;
1445
1446 for (i = 0; i < n_entries; i++)
1447 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1448 ETH_ALEN);
1449
1450 acl_params->acl_policy = accept_acl;
1451 acl_params->num_mac_acl = n_entries;
1452
1453 err = hostapd_drv_set_acl(hapd, acl_params);
1454
1455 os_free(acl_params);
1456
1457 return err;
1458}
1459
1460
1461static void hostapd_set_acl(struct hostapd_data *hapd)
1462{
1463 struct hostapd_config *conf = hapd->iconf;
1464 int err;
1465 u8 accept_acl;
1466
1467 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1468 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001469
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001470 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001471 accept_acl = 1;
1472 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1473 conf->bss[0]->num_accept_mac,
1474 accept_acl);
1475 if (err) {
1476 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1477 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001478 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001479 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001480 accept_acl = 0;
1481 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1482 conf->bss[0]->num_deny_mac,
1483 accept_acl);
1484 if (err) {
1485 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1486 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001487 }
1488 }
1489}
1490
1491
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001492static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1493{
1494 if (!hapd->iface->interfaces ||
1495 !hapd->iface->interfaces->ctrl_iface_init)
1496 return 0;
1497
1498 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1499 wpa_printf(MSG_ERROR,
1500 "Failed to setup control interface for %s",
1501 hapd->conf->iface);
1502 return -1;
1503 }
1504
1505 return 0;
1506}
1507
1508
1509static int start_ctrl_iface(struct hostapd_iface *iface)
1510{
1511 size_t i;
1512
1513 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1514 return 0;
1515
1516 for (i = 0; i < iface->num_bss; i++) {
1517 struct hostapd_data *hapd = iface->bss[i];
1518 if (iface->interfaces->ctrl_iface_init(hapd)) {
1519 wpa_printf(MSG_ERROR,
1520 "Failed to setup control interface for %s",
1521 hapd->conf->iface);
1522 return -1;
1523 }
1524 }
1525
1526 return 0;
1527}
1528
1529
1530static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1531{
1532 struct hostapd_iface *iface = eloop_ctx;
1533
1534 if (!iface->wait_channel_update) {
1535 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1536 return;
1537 }
1538
1539 /*
1540 * It is possible that the existing channel list is acceptable, so try
1541 * to proceed.
1542 */
1543 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1544 setup_interface2(iface);
1545}
1546
1547
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001548void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001549{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001550 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001551 return;
1552
1553 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1554 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1555 setup_interface2(iface);
1556}
1557
1558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559static int setup_interface(struct hostapd_iface *iface)
1560{
1561 struct hostapd_data *hapd = iface->bss[0];
1562 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001563
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001564 /*
1565 * It is possible that setup_interface() is called after the interface
1566 * was disabled etc., in which case driver_ap_teardown is possibly set
1567 * to 1. Clear it here so any other key/station deletion, which is not
1568 * part of a teardown flow, would also call the relevant driver
1569 * callbacks.
1570 */
1571 iface->driver_ap_teardown = 0;
1572
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001573 if (!iface->phy[0]) {
1574 const char *phy = hostapd_drv_get_radio_name(hapd);
1575 if (phy) {
1576 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1577 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1578 }
1579 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580
1581 /*
1582 * Make sure that all BSSes get configured with a pointer to the same
1583 * driver interface.
1584 */
1585 for (i = 1; i < iface->num_bss; i++) {
1586 iface->bss[i]->driver = hapd->driver;
1587 iface->bss[i]->drv_priv = hapd->drv_priv;
1588 }
1589
1590 if (hostapd_validate_bssid_configuration(iface))
1591 return -1;
1592
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001593 /*
1594 * Initialize control interfaces early to allow external monitoring of
1595 * channel setup operations that may take considerable amount of time
1596 * especially for DFS cases.
1597 */
1598 if (start_ctrl_iface(iface))
1599 return -1;
1600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001602 char country[4], previous_country[4];
1603
1604 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1605 if (hostapd_get_country(hapd, previous_country) < 0)
1606 previous_country[0] = '\0';
1607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 os_memcpy(country, hapd->iconf->country, 3);
1609 country[3] = '\0';
1610 if (hostapd_set_country(hapd, country) < 0) {
1611 wpa_printf(MSG_ERROR, "Failed to set country code");
1612 return -1;
1613 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001614
1615 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1616 previous_country, country);
1617
1618 if (os_strncmp(previous_country, country, 2) != 0) {
1619 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1620 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001621 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001622 channel_list_update_timeout,
1623 iface, NULL);
1624 return 0;
1625 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 }
1627
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001628 return setup_interface2(iface);
1629}
1630
1631
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001632static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1633{
1634 int freq, i, j;
1635
1636 if (!iface->conf->channel)
1637 return 0;
1638 if (iface->conf->op_class) {
1639 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1640 iface->conf->channel);
1641 if (freq < 0) {
1642 wpa_printf(MSG_INFO,
1643 "Could not convert op_class %u channel %u to operating frequency",
1644 iface->conf->op_class, iface->conf->channel);
1645 return -1;
1646 }
1647 iface->freq = freq;
1648 return 0;
1649 }
1650
1651 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1652 * op_class parameter. Select a matching channel from the configured
1653 * mode using the channel parameter for these cases.
1654 */
1655 for (j = 0; j < iface->num_hw_features; j++) {
1656 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1657
1658 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1659 iface->conf->hw_mode != mode->mode)
1660 continue;
1661 for (i = 0; i < mode->num_channels; i++) {
1662 struct hostapd_channel_data *chan = &mode->channels[i];
1663
1664 if (chan->chan == iface->conf->channel &&
1665 !is_6ghz_freq(chan->freq)) {
1666 iface->freq = chan->freq;
1667 return 0;
1668 }
1669 }
1670 }
1671
1672 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1673 return -1;
1674}
1675
1676
Hai Shaloma20dcd72022-02-04 13:43:00 -08001677static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
1678{
1679 int bw, seg0;
1680
1681 if (!is_6ghz_op_class(iface->conf->op_class))
1682 return;
1683
1684 seg0 = hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
1685 bw = center_idx_to_bw_6ghz(seg0);
1686 /* Assign the secondary channel if absent in config for
1687 * bandwidths > 20 MHz */
1688 if (bw > 20 && !iface->conf->secondary_channel) {
1689 if (((iface->conf->channel - 1) / 4) % 2)
1690 iface->conf->secondary_channel = -1;
1691 else
1692 iface->conf->secondary_channel = 1;
1693 }
1694}
1695
1696
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001697static int setup_interface2(struct hostapd_iface *iface)
1698{
1699 iface->wait_channel_update = 0;
1700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 if (hostapd_get_hw_features(iface)) {
1702 /* Not all drivers support this yet, so continue without hw
1703 * feature data. */
1704 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001705 int ret;
1706
1707 ret = configured_fixed_chan_to_freq(iface);
1708 if (ret < 0)
1709 goto fail;
1710
1711 if (iface->conf->op_class) {
1712 int ch_width;
1713
1714 ch_width = op_class_to_ch_width(iface->conf->op_class);
1715 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001716 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001717 }
1718
1719 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 if (ret < 0) {
1721 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1722 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001723 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001725 if (ret == 1) {
1726 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1727 return 0;
1728 }
Hai Shalomc3565922019-10-28 11:58:20 -07001729 ret = hostapd_check_edmg_capab(iface);
1730 if (ret < 0)
1731 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08001732 ret = hostapd_check_he_6ghz_capab(iface);
1733 if (ret < 0)
1734 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 ret = hostapd_check_ht_capab(iface);
1736 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001737 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 if (ret == 1) {
1739 wpa_printf(MSG_DEBUG, "Interface initialization will "
1740 "be completed in a callback");
1741 return 0;
1742 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001743
1744 if (iface->conf->ieee80211h)
1745 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 }
1747 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001748
1749fail:
1750 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1751 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1752 if (iface->interfaces && iface->interfaces->terminate_on_error)
1753 eloop_terminate();
1754 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755}
1756
1757
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001758#ifdef CONFIG_FST
1759
1760static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1761{
1762 struct hostapd_data *hapd = ctx;
1763
1764 return hapd->own_addr;
1765}
1766
1767
1768static void fst_hostapd_get_channel_info_cb(void *ctx,
1769 enum hostapd_hw_mode *hw_mode,
1770 u8 *channel)
1771{
1772 struct hostapd_data *hapd = ctx;
1773
1774 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1775}
1776
1777
1778static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1779{
1780 struct hostapd_data *hapd = ctx;
1781
1782 if (hapd->iface->fst_ies != fst_ies) {
1783 hapd->iface->fst_ies = fst_ies;
1784 if (ieee802_11_set_beacon(hapd))
1785 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1786 }
1787}
1788
1789
1790static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1791 struct wpabuf *buf)
1792{
1793 struct hostapd_data *hapd = ctx;
1794
1795 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1796 wpabuf_head(buf), wpabuf_len(buf));
1797}
1798
1799
1800static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1801{
1802 struct hostapd_data *hapd = ctx;
1803 struct sta_info *sta = ap_get_sta(hapd, addr);
1804
1805 return sta ? sta->mb_ies : NULL;
1806}
1807
1808
1809static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1810 const u8 *buf, size_t size)
1811{
1812 struct hostapd_data *hapd = ctx;
1813 struct sta_info *sta = ap_get_sta(hapd, addr);
1814
1815 if (sta) {
1816 struct mb_ies_info info;
1817
1818 if (!mb_ies_info_by_ies(&info, buf, size)) {
1819 wpabuf_free(sta->mb_ies);
1820 sta->mb_ies = mb_ies_by_info(&info);
1821 }
1822 }
1823}
1824
1825
1826static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001827 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001828{
1829 struct sta_info *s = (struct sta_info *) *get_ctx;
1830
1831 if (mb_only) {
1832 for (; s && !s->mb_ies; s = s->next)
1833 ;
1834 }
1835
1836 if (s) {
1837 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1838
1839 return s->addr;
1840 }
1841
1842 *get_ctx = NULL;
1843 return NULL;
1844}
1845
1846
1847static const u8 * fst_hostapd_get_peer_first(void *ctx,
1848 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001849 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001850{
1851 struct hostapd_data *hapd = ctx;
1852
1853 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1854
1855 return fst_hostapd_get_sta(get_ctx, mb_only);
1856}
1857
1858
1859static const u8 * fst_hostapd_get_peer_next(void *ctx,
1860 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001861 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001862{
1863 return fst_hostapd_get_sta(get_ctx, mb_only);
1864}
1865
1866
1867void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1868 struct fst_wpa_obj *iface_obj)
1869{
1870 iface_obj->ctx = hapd;
1871 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1872 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1873 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1874 iface_obj->send_action = fst_hostapd_send_action_cb;
1875 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1876 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1877 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1878 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1879}
1880
1881#endif /* CONFIG_FST */
1882
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001883#ifdef CONFIG_OWE
1884
1885static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1886{
1887 struct hostapd_data *hapd = ctx;
1888 size_t i;
1889
1890 for (i = 0; i < iface->num_bss; i++) {
1891 struct hostapd_data *bss = iface->bss[i];
1892
1893 if (os_strcmp(hapd->conf->owe_transition_ifname,
1894 bss->conf->iface) != 0)
1895 continue;
1896
1897 wpa_printf(MSG_DEBUG,
1898 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1899 MACSTR " SSID %s",
1900 hapd->conf->iface, bss->conf->iface,
1901 MAC2STR(bss->own_addr),
1902 wpa_ssid_txt(bss->conf->ssid.ssid,
1903 bss->conf->ssid.ssid_len));
1904 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1905 is_zero_ether_addr(bss->own_addr))
1906 continue;
1907
1908 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1909 ETH_ALEN);
1910 os_memcpy(hapd->conf->owe_transition_ssid,
1911 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1912 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1913 wpa_printf(MSG_DEBUG,
1914 "OWE: Copied transition mode information");
1915 return 1;
1916 }
1917
1918 return 0;
1919}
1920
1921
1922int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1923{
1924 if (hapd->conf->owe_transition_ssid_len > 0 &&
1925 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1926 return 0;
1927
1928 /* Find transition mode SSID/BSSID information from a BSS operated by
1929 * this hostapd instance. */
1930 if (!hapd->iface->interfaces ||
1931 !hapd->iface->interfaces->for_each_interface)
1932 return hostapd_owe_iface_iter(hapd->iface, hapd);
1933 else
1934 return hapd->iface->interfaces->for_each_interface(
1935 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1936}
1937
1938
1939static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1940{
1941 size_t i;
1942
1943 for (i = 0; i < iface->num_bss; i++) {
1944 struct hostapd_data *bss = iface->bss[i];
1945 int res;
1946
1947 if (!bss->conf->owe_transition_ifname[0])
1948 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07001949 if (bss->iface->state != HAPD_IFACE_ENABLED) {
1950 wpa_printf(MSG_DEBUG,
1951 "OWE: Interface %s state %s - defer beacon update",
1952 bss->conf->iface,
1953 hostapd_state_text(bss->iface->state));
1954 continue;
1955 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001956 res = hostapd_owe_trans_get_info(bss);
1957 if (res == 0)
1958 continue;
1959 wpa_printf(MSG_DEBUG,
1960 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1961 bss->conf->iface);
1962 ieee802_11_set_beacon(bss);
1963 }
1964
1965 return 0;
1966}
1967
1968#endif /* CONFIG_OWE */
1969
1970
1971static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1972{
1973#ifdef CONFIG_OWE
1974 /* Check whether the enabled BSS can complete OWE transition mode
1975 * configuration for any pending interface. */
1976 if (!iface->interfaces ||
1977 !iface->interfaces->for_each_interface)
1978 hostapd_owe_iface_iter2(iface, NULL);
1979 else
1980 iface->interfaces->for_each_interface(
1981 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1982#endif /* CONFIG_OWE */
1983}
1984
1985
Roshan Pius3a1667e2018-07-03 15:17:14 -07001986static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1987 void *timeout_ctx)
1988{
1989 struct hostapd_iface *iface = eloop_ctx;
1990 struct hostapd_data *hapd;
1991
1992 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1993 return;
1994 hapd = iface->bss[0];
1995 if (hapd->setup_complete_cb)
1996 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1997}
1998
1999
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002000static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2001 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002{
2003 struct hostapd_data *hapd = iface->bss[0];
2004 size_t j;
2005 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002006 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002007 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002009 if (err)
2010 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011
2012 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002013 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002014#ifdef NEED_AP_MLME
2015 int res;
2016#endif /* NEED_AP_MLME */
2017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2019 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002020 hostapd_hw_mode_txt(iface->conf->hw_mode),
2021 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022
Dmitry Shmidt051af732013-10-22 13:52:46 -07002023#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002024 /* Handle DFS only if it is not offloaded to the driver */
2025 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2026 /* Check DFS */
2027 res = hostapd_handle_dfs(iface);
2028 if (res <= 0) {
2029 if (res < 0)
2030 goto fail;
2031 return res;
2032 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002033 } else {
2034 /* If DFS is offloaded to the driver */
2035 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2036 if (res_dfs_offload <= 0) {
2037 if (res_dfs_offload < 0)
2038 goto fail;
2039 } else {
2040 wpa_printf(MSG_DEBUG,
2041 "Proceed with AP/channel setup");
2042 /*
2043 * If this is a DFS channel, move to completing
2044 * AP setup.
2045 */
2046 if (res_dfs_offload == 1)
2047 goto dfs_offload;
2048 /* Otherwise fall through. */
2049 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002050 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002051#endif /* NEED_AP_MLME */
2052
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002053#ifdef CONFIG_MESH
2054 if (iface->mconf != NULL) {
2055 wpa_printf(MSG_DEBUG,
2056 "%s: Mesh configuration will be applied while joining the mesh network",
2057 iface->bss[0]->conf->iface);
2058 delay_apply_cfg = 1;
2059 }
2060#endif /* CONFIG_MESH */
2061
2062 if (!delay_apply_cfg &&
2063 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002065 hapd->iconf->enable_edmg,
2066 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002068 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002069 hapd->iconf->ieee80211ax,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002070 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002071 hostapd_get_oper_chwidth(hapd->iconf),
2072 hostapd_get_oper_centr_freq_seg0_idx(
2073 hapd->iconf),
2074 hostapd_get_oper_centr_freq_seg1_idx(
2075 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 wpa_printf(MSG_ERROR, "Could not set channel for "
2077 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002078 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 }
2080 }
2081
2082 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002083 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2085 "table.");
2086 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2087 HOSTAPD_LEVEL_WARNING,
2088 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002089 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 }
2091 }
2092
Hai Shalom021b0b52019-04-10 11:17:58 -07002093 if (hapd->iconf->rts_threshold >= -1 &&
2094 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2095 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2097 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002098 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 }
2100
Hai Shalom021b0b52019-04-10 11:17:58 -07002101 if (hapd->iconf->fragm_threshold >= -1 &&
2102 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2103 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2105 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002106 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 }
2108
2109 prev_addr = hapd->own_addr;
2110
2111 for (j = 0; j < iface->num_bss; j++) {
2112 hapd = iface->bss[j];
2113 if (j)
2114 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002115 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002116 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002117 hapd = iface->bss[j];
2118 hostapd_bss_deinit_no_free(hapd);
2119 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002120 if (j == 0)
2121 break;
2122 j--;
2123 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002124 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002125 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002126 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127 prev_addr = hapd->own_addr;
2128 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002129 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130
2131 hostapd_tx_queue_params(iface);
2132
2133 ap_list_init(iface);
2134
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002135 hostapd_set_acl(hapd);
2136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 if (hostapd_driver_commit(hapd) < 0) {
2138 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2139 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002140 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141 }
2142
Jouni Malinen87fd2792011-05-16 18:35:42 +03002143 /*
2144 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2145 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2146 * mode), the interface is up only after driver_commit, so initialize
2147 * WPS after driver_commit.
2148 */
2149 for (j = 0; j < iface->num_bss; j++) {
2150 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002151 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002152 }
2153
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002154 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2155 !res_dfs_offload) {
2156 /*
2157 * If freq is DFS, and DFS is offloaded to the driver, then wait
2158 * for CAC to complete.
2159 */
2160 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2161 return res_dfs_offload;
2162 }
2163
2164#ifdef NEED_AP_MLME
2165dfs_offload:
2166#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002167
2168#ifdef CONFIG_FST
2169 if (hapd->iconf->fst_cfg.group_id[0]) {
2170 struct fst_wpa_obj iface_obj;
2171
2172 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2173 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2174 &iface_obj, &hapd->iconf->fst_cfg);
2175 if (!iface->fst) {
2176 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2177 hapd->iconf->fst_cfg.group_id);
2178 goto fail;
2179 }
2180 }
2181#endif /* CONFIG_FST */
2182
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002183 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002184 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002185 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002186 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 if (hapd->setup_complete_cb)
2188 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2189
Hai Shalom60840252021-02-19 19:02:11 -08002190#ifdef CONFIG_MESH
2191 if (delay_apply_cfg && !iface->mconf) {
2192 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2193 goto fail;
2194 }
2195#endif /* CONFIG_MESH */
2196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2198 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002199 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2200 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002202 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002203 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002206
2207fail:
2208 wpa_printf(MSG_ERROR, "Interface initialization failed");
2209 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2210 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002211#ifdef CONFIG_FST
2212 if (iface->fst) {
2213 fst_detach(iface->fst);
2214 iface->fst = NULL;
2215 }
2216#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002217
2218 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002219 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002220 } else if (hapd->setup_complete_cb) {
2221 /*
2222 * Calling hapd->setup_complete_cb directly may cause iface
2223 * deinitialization which may be accessed later by the caller.
2224 */
2225 eloop_register_timeout(0, 0,
2226 hostapd_interface_setup_failure_handler,
2227 iface, NULL);
2228 }
2229
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002230 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231}
2232
2233
2234/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002235 * hostapd_setup_interface_complete - Complete interface setup
2236 *
2237 * This function is called when previous steps in the interface setup has been
2238 * completed. This can also start operations, e.g., DFS, that will require
2239 * additional processing before interface is ready to be enabled. Such
2240 * operations will call this function from eloop callbacks when finished.
2241 */
2242int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2243{
2244 struct hapd_interfaces *interfaces = iface->interfaces;
2245 struct hostapd_data *hapd = iface->bss[0];
2246 unsigned int i;
2247 int not_ready_in_sync_ifaces = 0;
2248
2249 if (!iface->need_to_start_in_sync)
2250 return hostapd_setup_interface_complete_sync(iface, err);
2251
2252 if (err) {
2253 wpa_printf(MSG_ERROR, "Interface initialization failed");
2254 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2255 iface->need_to_start_in_sync = 0;
2256 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2257 if (interfaces && interfaces->terminate_on_error)
2258 eloop_terminate();
2259 return -1;
2260 }
2261
2262 if (iface->ready_to_start_in_sync) {
2263 /* Already in ready and waiting. should never happpen */
2264 return 0;
2265 }
2266
2267 for (i = 0; i < interfaces->count; i++) {
2268 if (interfaces->iface[i]->need_to_start_in_sync &&
2269 !interfaces->iface[i]->ready_to_start_in_sync)
2270 not_ready_in_sync_ifaces++;
2271 }
2272
2273 /*
2274 * Check if this is the last interface, if yes then start all the other
2275 * waiting interfaces. If not, add this interface to the waiting list.
2276 */
2277 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2278 /*
2279 * If this interface went through CAC, do not synchronize, just
2280 * start immediately.
2281 */
2282 iface->need_to_start_in_sync = 0;
2283 wpa_printf(MSG_INFO,
2284 "%s: Finished CAC - bypass sync and start interface",
2285 iface->bss[0]->conf->iface);
2286 return hostapd_setup_interface_complete_sync(iface, err);
2287 }
2288
2289 if (not_ready_in_sync_ifaces > 1) {
2290 /* need to wait as there are other interfaces still coming up */
2291 iface->ready_to_start_in_sync = 1;
2292 wpa_printf(MSG_INFO,
2293 "%s: Interface waiting to sync with other interfaces",
2294 iface->bss[0]->conf->iface);
2295 return 0;
2296 }
2297
2298 wpa_printf(MSG_INFO,
2299 "%s: Last interface to sync - starting all interfaces",
2300 iface->bss[0]->conf->iface);
2301 iface->need_to_start_in_sync = 0;
2302 hostapd_setup_interface_complete_sync(iface, err);
2303 for (i = 0; i < interfaces->count; i++) {
2304 if (interfaces->iface[i]->need_to_start_in_sync &&
2305 interfaces->iface[i]->ready_to_start_in_sync) {
2306 hostapd_setup_interface_complete_sync(
2307 interfaces->iface[i], 0);
2308 /* Only once the interfaces are sync started */
2309 interfaces->iface[i]->need_to_start_in_sync = 0;
2310 }
2311 }
2312
2313 return 0;
2314}
2315
2316
2317/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 * hostapd_setup_interface - Setup of an interface
2319 * @iface: Pointer to interface data.
2320 * Returns: 0 on success, -1 on failure
2321 *
2322 * Initializes the driver interface, validates the configuration,
2323 * and sets driver parameters based on the configuration.
2324 * Flushes old stations, sets the channel, encryption,
2325 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002326 *
2327 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2328 * or DFS operations, this function returns 0 before such operations have been
2329 * completed. The pending operations are registered into eloop and will be
2330 * completed from eloop callbacks. Those callbacks end up calling
2331 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002332 */
2333int hostapd_setup_interface(struct hostapd_iface *iface)
2334{
2335 int ret;
2336
Hai Shaloma20dcd72022-02-04 13:43:00 -08002337 if (!iface->conf)
2338 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339 ret = setup_interface(iface);
2340 if (ret) {
2341 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002342 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343 return -1;
2344 }
2345
2346 return 0;
2347}
2348
2349
2350/**
2351 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2352 * @hapd_iface: Pointer to interface data
2353 * @conf: Pointer to per-interface configuration
2354 * @bss: Pointer to per-BSS configuration for this BSS
2355 * Returns: Pointer to allocated BSS data
2356 *
2357 * This function is used to allocate per-BSS data structure. This data will be
2358 * freed after hostapd_cleanup() is called for it during interface
2359 * deinitialization.
2360 */
2361struct hostapd_data *
2362hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2363 struct hostapd_config *conf,
2364 struct hostapd_bss_config *bss)
2365{
2366 struct hostapd_data *hapd;
2367
2368 hapd = os_zalloc(sizeof(*hapd));
2369 if (hapd == NULL)
2370 return NULL;
2371
2372 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2373 hapd->iconf = conf;
2374 hapd->conf = bss;
2375 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002376 if (conf)
2377 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002378 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002379 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002380 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002381 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002382#ifdef CONFIG_IEEE80211R_AP
2383 dl_list_init(&hapd->l2_queue);
2384 dl_list_init(&hapd->l2_oui_queue);
2385#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002386#ifdef CONFIG_SAE
2387 dl_list_init(&hapd->sae_commit_queue);
2388#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389
2390 return hapd;
2391}
2392
2393
Dmitry Shmidt54605472013-11-08 11:10:19 -08002394static void hostapd_bss_deinit(struct hostapd_data *hapd)
2395{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002396 if (!hapd)
2397 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002398 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002399 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002400 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002401 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002402#ifdef CONFIG_SQLITE
2403 if (hapd->rad_attr_db) {
2404 sqlite3_close(hapd->rad_attr_db);
2405 hapd->rad_attr_db = NULL;
2406 }
2407#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002408 hostapd_cleanup(hapd);
2409}
2410
2411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412void hostapd_interface_deinit(struct hostapd_iface *iface)
2413{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002414 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415
Dmitry Shmidt54605472013-11-08 11:10:19 -08002416 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 if (iface == NULL)
2418 return;
2419
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002420 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2421
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002422 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2423 iface->wait_channel_update = 0;
2424
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002425#ifdef CONFIG_FST
2426 if (iface->fst) {
2427 fst_detach(iface->fst);
2428 iface->fst = NULL;
2429 }
2430#endif /* CONFIG_FST */
2431
Hai Shalom021b0b52019-04-10 11:17:58 -07002432 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002433 if (!iface->bss)
2434 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002435 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002436 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002437
Roshan Pius3a1667e2018-07-03 15:17:14 -07002438#ifdef NEED_AP_MLME
2439 hostapd_stop_setup_timers(iface);
2440 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2441#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442}
2443
2444
2445void hostapd_interface_free(struct hostapd_iface *iface)
2446{
2447 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002448 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2449 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002450 if (!iface->bss)
2451 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002452 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2453 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 hostapd_cleanup_iface(iface);
2457}
2458
2459
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002460struct hostapd_iface * hostapd_alloc_iface(void)
2461{
2462 struct hostapd_iface *hapd_iface;
2463
2464 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2465 if (!hapd_iface)
2466 return NULL;
2467
2468 dl_list_init(&hapd_iface->sta_seen);
2469
2470 return hapd_iface;
2471}
2472
2473
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002474/**
2475 * hostapd_init - Allocate and initialize per-interface data
2476 * @config_file: Path to the configuration file
2477 * Returns: Pointer to the allocated interface data or %NULL on failure
2478 *
2479 * This function is used to allocate main data structures for per-interface
2480 * data. The allocated data buffer will be freed by calling
2481 * hostapd_cleanup_iface().
2482 */
2483struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2484 const char *config_file)
2485{
2486 struct hostapd_iface *hapd_iface = NULL;
2487 struct hostapd_config *conf = NULL;
2488 struct hostapd_data *hapd;
2489 size_t i;
2490
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002491 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002492 if (hapd_iface == NULL)
2493 goto fail;
2494
2495 hapd_iface->config_fname = os_strdup(config_file);
2496 if (hapd_iface->config_fname == NULL)
2497 goto fail;
2498
2499 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2500 if (conf == NULL)
2501 goto fail;
2502 hapd_iface->conf = conf;
2503
2504 hapd_iface->num_bss = conf->num_bss;
2505 hapd_iface->bss = os_calloc(conf->num_bss,
2506 sizeof(struct hostapd_data *));
2507 if (hapd_iface->bss == NULL)
2508 goto fail;
2509
2510 for (i = 0; i < conf->num_bss; i++) {
2511 hapd = hapd_iface->bss[i] =
2512 hostapd_alloc_bss_data(hapd_iface, conf,
2513 conf->bss[i]);
2514 if (hapd == NULL)
2515 goto fail;
2516 hapd->msg_ctx = hapd;
2517 }
2518
2519 return hapd_iface;
2520
2521fail:
2522 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2523 config_file);
2524 if (conf)
2525 hostapd_config_free(conf);
2526 if (hapd_iface) {
2527 os_free(hapd_iface->config_fname);
2528 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002529 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2530 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002531 os_free(hapd_iface);
2532 }
2533 return NULL;
2534}
2535
2536
2537static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2538{
2539 size_t i, j;
2540
2541 for (i = 0; i < interfaces->count; i++) {
2542 struct hostapd_iface *iface = interfaces->iface[i];
2543 for (j = 0; j < iface->num_bss; j++) {
2544 struct hostapd_data *hapd = iface->bss[j];
2545 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2546 return 1;
2547 }
2548 }
2549
2550 return 0;
2551}
2552
2553
2554/**
2555 * hostapd_interface_init_bss - Read configuration file and init BSS data
2556 *
2557 * This function is used to parse configuration file for a BSS. This BSS is
2558 * added to an existing interface sharing the same radio (if any) or a new
2559 * interface is created if this is the first interface on a radio. This
2560 * allocate memory for the BSS. No actual driver operations are started.
2561 *
2562 * This is similar to hostapd_interface_init(), but for a case where the
2563 * configuration is used to add a single BSS instead of all BSSes for a radio.
2564 */
2565struct hostapd_iface *
2566hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2567 const char *config_fname, int debug)
2568{
2569 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2570 struct hostapd_data *hapd;
2571 int k;
2572 size_t i, bss_idx;
2573
2574 if (!phy || !*phy)
2575 return NULL;
2576
2577 for (i = 0; i < interfaces->count; i++) {
2578 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2579 iface = interfaces->iface[i];
2580 break;
2581 }
2582 }
2583
2584 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2585 config_fname, phy, iface ? "" : " --> new PHY");
2586 if (iface) {
2587 struct hostapd_config *conf;
2588 struct hostapd_bss_config **tmp_conf;
2589 struct hostapd_data **tmp_bss;
2590 struct hostapd_bss_config *bss;
2591 const char *ifname;
2592
2593 /* Add new BSS to existing iface */
2594 conf = interfaces->config_read_cb(config_fname);
2595 if (conf == NULL)
2596 return NULL;
2597 if (conf->num_bss > 1) {
2598 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2599 hostapd_config_free(conf);
2600 return NULL;
2601 }
2602
2603 ifname = conf->bss[0]->iface;
2604 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2605 wpa_printf(MSG_ERROR,
2606 "Interface name %s already in use", ifname);
2607 hostapd_config_free(conf);
2608 return NULL;
2609 }
2610
2611 tmp_conf = os_realloc_array(
2612 iface->conf->bss, iface->conf->num_bss + 1,
2613 sizeof(struct hostapd_bss_config *));
2614 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2615 sizeof(struct hostapd_data *));
2616 if (tmp_bss)
2617 iface->bss = tmp_bss;
2618 if (tmp_conf) {
2619 iface->conf->bss = tmp_conf;
2620 iface->conf->last_bss = tmp_conf[0];
2621 }
2622 if (tmp_bss == NULL || tmp_conf == NULL) {
2623 hostapd_config_free(conf);
2624 return NULL;
2625 }
2626 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2627 iface->conf->num_bss++;
2628
2629 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2630 if (hapd == NULL) {
2631 iface->conf->num_bss--;
2632 hostapd_config_free(conf);
2633 return NULL;
2634 }
2635 iface->conf->last_bss = bss;
2636 iface->bss[iface->num_bss] = hapd;
2637 hapd->msg_ctx = hapd;
2638
2639 bss_idx = iface->num_bss++;
2640 conf->num_bss--;
2641 conf->bss[0] = NULL;
2642 hostapd_config_free(conf);
2643 } else {
2644 /* Add a new iface with the first BSS */
2645 new_iface = iface = hostapd_init(interfaces, config_fname);
2646 if (!iface)
2647 return NULL;
2648 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2649 iface->interfaces = interfaces;
2650 bss_idx = 0;
2651 }
2652
2653 for (k = 0; k < debug; k++) {
2654 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2655 iface->bss[bss_idx]->conf->logger_stdout_level--;
2656 }
2657
2658 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2659 !hostapd_drv_none(iface->bss[bss_idx])) {
2660 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2661 config_fname);
2662 if (new_iface)
2663 hostapd_interface_deinit_free(new_iface);
2664 return NULL;
2665 }
2666
2667 return iface;
2668}
2669
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002670
2671void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2672{
2673 const struct wpa_driver_ops *driver;
2674 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002675
2676 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002677 if (iface == NULL)
2678 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002679 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2680 __func__, (unsigned int) iface->num_bss,
2681 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002682 driver = iface->bss[0]->driver;
2683 drv_priv = iface->bss[0]->drv_priv;
2684 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002685 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2686 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002687 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002688 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002689 iface->bss[0]->drv_priv = NULL;
2690 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002691 hostapd_interface_free(iface);
2692}
2693
2694
Dmitry Shmidt15907092014-03-25 10:42:57 -07002695static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2696 void *drv_priv,
2697 struct hostapd_iface *hapd_iface)
2698{
2699 size_t j;
2700
2701 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2702 __func__, driver, drv_priv);
2703 if (driver && driver->hapd_deinit && drv_priv) {
2704 driver->hapd_deinit(drv_priv);
2705 for (j = 0; j < hapd_iface->num_bss; j++) {
2706 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2707 __func__, (int) j,
2708 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002709 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002710 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002711 hapd_iface->extended_capa = NULL;
2712 hapd_iface->extended_capa_mask = NULL;
2713 hapd_iface->extended_capa_len = 0;
2714 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002715 }
2716 }
2717}
2718
2719
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002720int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2721{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002722 size_t j;
2723
Hai Shalom60840252021-02-19 19:02:11 -08002724 if (!hapd_iface)
2725 return -1;
2726
2727 if (hapd_iface->enable_iface_cb)
2728 return hapd_iface->enable_iface_cb(hapd_iface);
2729
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002730 if (hapd_iface->bss[0]->drv_priv != NULL) {
2731 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002732 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002733 return -1;
2734 }
2735
2736 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002737 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002738
Dmitry Shmidt71757432014-06-02 13:50:35 -07002739 for (j = 0; j < hapd_iface->num_bss; j++)
2740 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002741 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2742 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2743 return -1;
2744 }
2745
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002746 if (hapd_iface->interfaces == NULL ||
2747 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002748 hapd_iface->interfaces->driver_init(hapd_iface))
2749 return -1;
2750
2751 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002752 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2753 hapd_iface->bss[0]->drv_priv,
2754 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002755 return -1;
2756 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002757
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002758 return 0;
2759}
2760
2761
2762int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2763{
2764 size_t j;
2765
2766 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002767 hapd_iface->conf->bss[0]->iface);
2768 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002769 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002770 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002771 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2772 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002773 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002774 hostapd_clear_old(hapd_iface);
2775 for (j = 0; j < hapd_iface->num_bss; j++)
2776 hostapd_reload_bss(hapd_iface->bss[j]);
2777
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002778 return 0;
2779}
2780
2781
2782int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2783{
2784 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002785 const struct wpa_driver_ops *driver;
2786 void *drv_priv;
2787
2788 if (hapd_iface == NULL)
2789 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002790
Hai Shalom60840252021-02-19 19:02:11 -08002791 if (hapd_iface->disable_iface_cb)
2792 return hapd_iface->disable_iface_cb(hapd_iface);
2793
Dmitry Shmidt71757432014-06-02 13:50:35 -07002794 if (hapd_iface->bss[0]->drv_priv == NULL) {
2795 wpa_printf(MSG_INFO, "Interface %s already disabled",
2796 hapd_iface->conf->bss[0]->iface);
2797 return -1;
2798 }
2799
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002800 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002801 driver = hapd_iface->bss[0]->driver;
2802 drv_priv = hapd_iface->bss[0]->drv_priv;
2803
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002804 hapd_iface->driver_ap_teardown =
2805 !!(hapd_iface->drv_flags &
2806 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2807
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002808#ifdef NEED_AP_MLME
2809 for (j = 0; j < hapd_iface->num_bss; j++)
2810 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2811#endif /* NEED_AP_MLME */
2812
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002813 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002814 for (j = 0; j < hapd_iface->num_bss; j++) {
2815 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002816 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002817 hostapd_free_hapd_data(hapd);
2818 }
2819
Dmitry Shmidt15907092014-03-25 10:42:57 -07002820 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002821
2822 /* From hostapd_cleanup_iface: These were initialized in
2823 * hostapd_setup_interface and hostapd_setup_interface_complete
2824 */
2825 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002826
Dmitry Shmidt56052862013-10-04 10:23:25 -07002827 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2828 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002829 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002830 return 0;
2831}
2832
2833
2834static struct hostapd_iface *
2835hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2836{
2837 struct hostapd_iface **iface, *hapd_iface;
2838
2839 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2840 sizeof(struct hostapd_iface *));
2841 if (iface == NULL)
2842 return NULL;
2843 interfaces->iface = iface;
2844 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002845 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002846 if (hapd_iface == NULL) {
2847 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2848 "the interface", __func__);
2849 return NULL;
2850 }
2851 interfaces->count++;
2852 hapd_iface->interfaces = interfaces;
2853
2854 return hapd_iface;
2855}
2856
2857
2858static struct hostapd_config *
2859hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002860 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002861{
2862 struct hostapd_bss_config *bss;
2863 struct hostapd_config *conf;
2864
2865 /* Allocates memory for bss and conf */
2866 conf = hostapd_config_defaults();
2867 if (conf == NULL) {
2868 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2869 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002870 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002871 }
2872
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002873 if (driver) {
2874 int j;
2875
2876 for (j = 0; wpa_drivers[j]; j++) {
2877 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2878 conf->driver = wpa_drivers[j];
2879 goto skip;
2880 }
2881 }
2882
2883 wpa_printf(MSG_ERROR,
2884 "Invalid/unknown driver '%s' - registering the default driver",
2885 driver);
2886 }
2887
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002888 conf->driver = wpa_drivers[0];
2889 if (conf->driver == NULL) {
2890 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2891 hostapd_config_free(conf);
2892 return NULL;
2893 }
2894
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002895skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002896 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002897
2898 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2899 bss->ctrl_interface = os_strdup(ctrl_iface);
2900 if (bss->ctrl_interface == NULL) {
2901 hostapd_config_free(conf);
2902 return NULL;
2903 }
2904
2905 /* Reading configuration file skipped, will be done in SET!
2906 * From reading the configuration till the end has to be done in
2907 * SET
2908 */
2909 return conf;
2910}
2911
2912
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002913static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2914 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002915{
2916 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002917 struct hostapd_data *hapd;
2918
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002919 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002920 sizeof(struct hostapd_data *));
2921 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002922 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002923
2924 for (i = 0; i < conf->num_bss; i++) {
2925 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002926 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002927 if (hapd == NULL) {
2928 while (i > 0) {
2929 i--;
2930 os_free(hapd_iface->bss[i]);
2931 hapd_iface->bss[i] = NULL;
2932 }
2933 os_free(hapd_iface->bss);
2934 hapd_iface->bss = NULL;
2935 return -1;
2936 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002937 hapd->msg_ctx = hapd;
2938 }
2939
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002940 hapd_iface->conf = conf;
2941 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002942
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002943 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002944}
2945
2946
2947int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2948{
2949 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002950 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2951 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002952 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002953 size_t i, j;
2954 const char *conf_file = NULL, *phy_name = NULL;
2955
2956 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2957 char *pos;
2958 phy_name = buf + 11;
2959 pos = os_strchr(phy_name, ':');
2960 if (!pos)
2961 return -1;
2962 *pos++ = '\0';
2963 conf_file = pos;
2964 if (!os_strlen(conf_file))
2965 return -1;
2966
2967 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2968 conf_file, 0);
2969 if (!hapd_iface)
2970 return -1;
2971 for (j = 0; j < interfaces->count; j++) {
2972 if (interfaces->iface[j] == hapd_iface)
2973 break;
2974 }
2975 if (j == interfaces->count) {
2976 struct hostapd_iface **tmp;
2977 tmp = os_realloc_array(interfaces->iface,
2978 interfaces->count + 1,
2979 sizeof(struct hostapd_iface *));
2980 if (!tmp) {
2981 hostapd_interface_deinit_free(hapd_iface);
2982 return -1;
2983 }
2984 interfaces->iface = tmp;
2985 interfaces->iface[interfaces->count++] = hapd_iface;
2986 new_iface = hapd_iface;
2987 }
2988
2989 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002990 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002991 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002992
2993 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002994 hostapd_deinit_driver(
2995 hapd_iface->bss[0]->driver,
2996 hapd_iface->bss[0]->drv_priv,
2997 hapd_iface);
2998 goto fail;
2999 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003000 } else {
3001 /* Assign new BSS with bss[0]'s driver info */
3002 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3003 hapd->driver = hapd_iface->bss[0]->driver;
3004 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3005 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3006 ETH_ALEN);
3007
3008 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003009 (hapd_iface->state == HAPD_IFACE_ENABLED &&
3010 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003011 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003012 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003013 hapd_iface->conf->num_bss--;
3014 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003015 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3016 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003017 hostapd_config_free_bss(hapd->conf);
3018 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003019 os_free(hapd);
3020 return -1;
3021 }
3022 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003023 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003024 return 0;
3025 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003026
3027 ptr = os_strchr(buf, ' ');
3028 if (ptr == NULL)
3029 return -1;
3030 *ptr++ = '\0';
3031
Dmitry Shmidt56052862013-10-04 10:23:25 -07003032 if (os_strncmp(ptr, "config=", 7) == 0)
3033 conf_file = ptr + 7;
3034
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003035 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003036 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003037 buf)) {
3038 wpa_printf(MSG_INFO, "Cannot add interface - it "
3039 "already exists");
3040 return -1;
3041 }
3042 }
3043
3044 hapd_iface = hostapd_iface_alloc(interfaces);
3045 if (hapd_iface == NULL) {
3046 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3047 "for interface", __func__);
3048 goto fail;
3049 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003050 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003051
Dmitry Shmidt56052862013-10-04 10:23:25 -07003052 if (conf_file && interfaces->config_read_cb) {
3053 conf = interfaces->config_read_cb(conf_file);
3054 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003055 os_strlcpy(conf->bss[0]->iface, buf,
3056 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003057 } else {
3058 char *driver = os_strchr(ptr, ' ');
3059
3060 if (driver)
3061 *driver++ = '\0';
3062 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3063 }
3064
Dmitry Shmidt56052862013-10-04 10:23:25 -07003065 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003066 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3067 "for configuration", __func__);
3068 goto fail;
3069 }
3070
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003071 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003072 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3073 "for hostapd", __func__);
3074 goto fail;
3075 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003076 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003077
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003078 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003079 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003080
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003081 wpa_printf(MSG_INFO, "Add interface '%s'",
3082 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003083
3084 return 0;
3085
3086fail:
3087 if (conf)
3088 hostapd_config_free(conf);
3089 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003090 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003091 for (i = 0; i < hapd_iface->num_bss; i++) {
3092 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003093 if (!hapd)
3094 continue;
3095 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003096 hapd_iface->interfaces->ctrl_iface_deinit)
3097 hapd_iface->interfaces->
3098 ctrl_iface_deinit(hapd);
3099 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3100 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003101 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003102 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003103 os_free(hapd);
3104 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003105 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003106 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003107 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003108 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003109 if (new_iface) {
3110 interfaces->count--;
3111 interfaces->iface[interfaces->count] = NULL;
3112 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003113 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003114 }
3115 return -1;
3116}
3117
3118
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003119static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3120{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003121 size_t i;
3122
Dmitry Shmidt54605472013-11-08 11:10:19 -08003123 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003124
Dmitry Shmidt54605472013-11-08 11:10:19 -08003125 /* Remove hostapd_data only if it has already been initialized */
3126 if (idx < iface->num_bss) {
3127 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003128
Dmitry Shmidt54605472013-11-08 11:10:19 -08003129 hostapd_bss_deinit(hapd);
3130 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3131 __func__, hapd, hapd->conf->iface);
3132 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003133 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003134 os_free(hapd);
3135
3136 iface->num_bss--;
3137
3138 for (i = idx; i < iface->num_bss; i++)
3139 iface->bss[i] = iface->bss[i + 1];
3140 } else {
3141 hostapd_config_free_bss(iface->conf->bss[idx]);
3142 iface->conf->bss[idx] = NULL;
3143 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003144
3145 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003146 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003147 iface->conf->bss[i] = iface->conf->bss[i + 1];
3148
3149 return 0;
3150}
3151
3152
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003153int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3154{
3155 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003156 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003157
3158 for (i = 0; i < interfaces->count; i++) {
3159 hapd_iface = interfaces->iface[i];
3160 if (hapd_iface == NULL)
3161 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003162 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003163 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003164 hapd_iface->driver_ap_teardown =
3165 !!(hapd_iface->drv_flags &
3166 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3167
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003168 hostapd_interface_deinit_free(hapd_iface);
3169 k = i;
3170 while (k < (interfaces->count - 1)) {
3171 interfaces->iface[k] =
3172 interfaces->iface[k + 1];
3173 k++;
3174 }
3175 interfaces->count--;
3176 return 0;
3177 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003178
3179 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003180 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3181 hapd_iface->driver_ap_teardown =
3182 !(hapd_iface->drv_flags &
3183 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003184 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003185 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003186 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003187 }
3188 return -1;
3189}
3190
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192/**
3193 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3194 * @hapd: Pointer to BSS data
3195 * @sta: Pointer to the associated STA data
3196 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3197 *
3198 * This function will be called whenever a station associates with the AP. It
3199 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3200 * from drv_callbacks.c based on driver events for drivers that take care of
3201 * management frames (IEEE 802.11 authentication and association) internally.
3202 */
3203void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3204 int reassoc)
3205{
3206 if (hapd->tkip_countermeasures) {
3207 hostapd_drv_sta_deauth(hapd, sta->addr,
3208 WLAN_REASON_MICHAEL_MIC_FAILURE);
3209 return;
3210 }
3211
3212 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003213 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003214 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216#ifdef CONFIG_P2P
3217 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3218 sta->no_p2p_set = 1;
3219 hapd->num_sta_no_p2p++;
3220 if (hapd->num_sta_no_p2p == 1)
3221 hostapd_p2p_non_p2p_sta_connected(hapd);
3222 }
3223#endif /* CONFIG_P2P */
3224
Hai Shalom81f62d82019-07-22 12:10:00 -07003225 airtime_policy_new_sta(hapd, sta);
3226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3228 * IEEE 802.1X/WPA code will start accounting after the station has
3229 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003230 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3231 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003232 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003233 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003234 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235
3236 /* Start IEEE 802.1X authentication process for new stations */
3237 ieee802_1x_new_station(hapd, sta);
3238 if (reassoc) {
3239 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003240 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3241 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3242 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3244 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3245 } else
3246 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003247
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003248 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3249 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3250 wpa_printf(MSG_DEBUG,
3251 "%s: %s: canceled wired ap_handle_timer timeout for "
3252 MACSTR,
3253 hapd->conf->iface, __func__,
3254 MAC2STR(sta->addr));
3255 }
3256 } else if (!(hapd->iface->drv_flags &
3257 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003258 wpa_printf(MSG_DEBUG,
3259 "%s: %s: reschedule ap_handle_timer timeout for "
3260 MACSTR " (%d seconds - ap_max_inactivity)",
3261 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003262 hapd->conf->ap_max_inactivity);
3263 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3264 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3265 ap_handle_timer, hapd, sta);
3266 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003267
3268#ifdef CONFIG_MACSEC
3269 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3270 hapd->conf->mka_psk_set)
3271 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3272 else
3273 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3274#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003276
3277
3278const char * hostapd_state_text(enum hostapd_iface_state s)
3279{
3280 switch (s) {
3281 case HAPD_IFACE_UNINITIALIZED:
3282 return "UNINITIALIZED";
3283 case HAPD_IFACE_DISABLED:
3284 return "DISABLED";
3285 case HAPD_IFACE_COUNTRY_UPDATE:
3286 return "COUNTRY_UPDATE";
3287 case HAPD_IFACE_ACS:
3288 return "ACS";
3289 case HAPD_IFACE_HT_SCAN:
3290 return "HT_SCAN";
3291 case HAPD_IFACE_DFS:
3292 return "DFS";
3293 case HAPD_IFACE_ENABLED:
3294 return "ENABLED";
3295 }
3296
3297 return "UNKNOWN";
3298}
3299
3300
3301void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3302{
3303 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003304 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3305 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003306 iface->state = s;
3307}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003308
3309
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003310int hostapd_csa_in_progress(struct hostapd_iface *iface)
3311{
3312 unsigned int i;
3313
3314 for (i = 0; i < iface->num_bss; i++)
3315 if (iface->bss[i]->csa_in_progress)
3316 return 1;
3317 return 0;
3318}
3319
3320
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003321#ifdef NEED_AP_MLME
3322
3323static void free_beacon_data(struct beacon_data *beacon)
3324{
3325 os_free(beacon->head);
3326 beacon->head = NULL;
3327 os_free(beacon->tail);
3328 beacon->tail = NULL;
3329 os_free(beacon->probe_resp);
3330 beacon->probe_resp = NULL;
3331 os_free(beacon->beacon_ies);
3332 beacon->beacon_ies = NULL;
3333 os_free(beacon->proberesp_ies);
3334 beacon->proberesp_ies = NULL;
3335 os_free(beacon->assocresp_ies);
3336 beacon->assocresp_ies = NULL;
3337}
3338
3339
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003340static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003341 struct beacon_data *beacon)
3342{
3343 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3344 struct wpa_driver_ap_params params;
3345 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003346
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003347 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003348 ret = ieee802_11_build_ap_params(hapd, &params);
3349 if (ret < 0)
3350 return ret;
3351
3352 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3353 &proberesp_extra,
3354 &assocresp_extra);
3355 if (ret)
3356 goto free_ap_params;
3357
3358 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003359 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003360 if (!beacon->head)
3361 goto free_ap_extra_ies;
3362
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003363 beacon->head_len = params.head_len;
3364
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003365 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003366 if (!beacon->tail)
3367 goto free_beacon;
3368
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003369 beacon->tail_len = params.tail_len;
3370
3371 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003372 beacon->probe_resp = os_memdup(params.proberesp,
3373 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003374 if (!beacon->probe_resp)
3375 goto free_beacon;
3376
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003377 beacon->probe_resp_len = params.proberesp_len;
3378 }
3379
3380 /* copy the extra ies */
3381 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003382 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3383 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003384 if (!beacon->beacon_ies)
3385 goto free_beacon;
3386
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003387 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3388 }
3389
3390 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003391 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3392 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003393 if (!beacon->proberesp_ies)
3394 goto free_beacon;
3395
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003396 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3397 }
3398
3399 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003400 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3401 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003402 if (!beacon->assocresp_ies)
3403 goto free_beacon;
3404
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003405 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3406 }
3407
3408 ret = 0;
3409free_beacon:
3410 /* if the function fails, the caller should not free beacon data */
3411 if (ret)
3412 free_beacon_data(beacon);
3413
3414free_ap_extra_ies:
3415 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3416 assocresp_extra);
3417free_ap_params:
3418 ieee802_11_free_ap_params(&params);
3419 return ret;
3420}
3421
3422
3423/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003424 * TODO: This flow currently supports only changing channel and width within
3425 * the same hw_mode. Any other changes to MAC parameters or provided settings
3426 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003427 */
3428static int hostapd_change_config_freq(struct hostapd_data *hapd,
3429 struct hostapd_config *conf,
3430 struct hostapd_freq_params *params,
3431 struct hostapd_freq_params *old_params)
3432{
3433 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003434 u8 seg0, seg1;
3435 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003436
3437 if (!params->channel) {
3438 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003439 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003440 }
3441
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003442 channel = params->channel;
3443 if (!channel)
3444 return -1;
3445
Hai Shalom81f62d82019-07-22 12:10:00 -07003446 mode = hapd->iface->current_mode;
3447
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003448 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003449 if (old_params &&
3450 hostapd_set_freq_params(old_params, conf->hw_mode,
3451 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003452 conf->channel, conf->enable_edmg,
3453 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003454 conf->ieee80211ac, conf->ieee80211ax,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003455 conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003456 hostapd_get_oper_chwidth(conf),
3457 hostapd_get_oper_centr_freq_seg0_idx(conf),
3458 hostapd_get_oper_centr_freq_seg1_idx(conf),
3459 conf->vht_capab,
3460 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3461 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003462 return -1;
3463
3464 switch (params->bandwidth) {
3465 case 0:
3466 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003467 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3468 break;
3469 case 40:
3470 case 80:
3471 case 160:
3472 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3473 break;
3474 default:
3475 return -1;
3476 }
3477
3478 switch (params->bandwidth) {
3479 case 0:
3480 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003481 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003482 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003483 break;
3484 case 80:
3485 if (params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003486 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003487 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003488 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003489 break;
3490 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003491 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003492 break;
3493 default:
3494 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003495 }
3496
3497 conf->channel = channel;
3498 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003499 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003500 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003501 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003502 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003503 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003504 &seg1);
3505 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3506 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003507
3508 /* TODO: maybe call here hostapd_config_check here? */
3509
3510 return 0;
3511}
3512
3513
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003514static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003515 struct csa_settings *settings)
3516{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003517 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003518 struct hostapd_freq_params old_freq;
3519 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003520 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003521
3522 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003523 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003524 return -1;
3525
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003526 switch (settings->freq_params.bandwidth) {
3527 case 80:
3528 if (settings->freq_params.center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003529 bandwidth = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003530 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003531 bandwidth = CHANWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003532 break;
3533 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003534 bandwidth = CHANWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003535 break;
3536 default:
Hai Shalom81f62d82019-07-22 12:10:00 -07003537 bandwidth = CHANWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003538 break;
3539 }
3540
3541 if (ieee80211_freq_to_channel_ext(
3542 settings->freq_params.freq,
3543 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003544 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003545 &hapd->iface->cs_oper_class,
3546 &chan) == NUM_HOSTAPD_MODES) {
3547 wpa_printf(MSG_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07003548 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003549 settings->freq_params.freq,
3550 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003551 settings->freq_params.vht_enabled,
3552 settings->freq_params.he_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003553 return -1;
3554 }
3555
3556 settings->freq_params.channel = chan;
3557
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003558 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3559 &settings->freq_params,
3560 &old_freq);
3561 if (ret)
3562 return ret;
3563
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003564 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003565
3566 /* change back the configuration */
3567 hostapd_change_config_freq(iface->bss[0], iface->conf,
3568 &old_freq, NULL);
3569
3570 if (ret)
3571 return ret;
3572
3573 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003574 hapd->cs_freq_params = settings->freq_params;
3575 hapd->cs_count = settings->cs_count;
3576 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003577
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003578 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003579 if (ret) {
3580 free_beacon_data(&settings->beacon_after);
3581 return ret;
3582 }
3583
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003584 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3585 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3586 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3587 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003588
3589 return 0;
3590}
3591
3592
3593void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3594{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003595 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3596 hapd->cs_count = 0;
3597 hapd->cs_block_tx = 0;
3598 hapd->cs_c_off_beacon = 0;
3599 hapd->cs_c_off_proberesp = 0;
3600 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003601 hapd->cs_c_off_ecsa_beacon = 0;
3602 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003603}
3604
3605
Hai Shalom60840252021-02-19 19:02:11 -08003606void hostapd_chan_switch_config(struct hostapd_data *hapd,
3607 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003608{
Hai Shalom60840252021-02-19 19:02:11 -08003609 if (freq_params->he_enabled)
3610 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
3611 else
3612 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
3613
3614 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003615 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3616 else
3617 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3618
3619 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08003620 HOSTAPD_LEVEL_INFO,
3621 "CHAN_SWITCH HE config 0x%x VHT config 0x%x",
3622 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003623 hapd->iconf->ch_switch_vht_config);
3624}
3625
3626
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003627int hostapd_switch_channel(struct hostapd_data *hapd,
3628 struct csa_settings *settings)
3629{
3630 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003631
3632 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3633 wpa_printf(MSG_INFO, "CSA is not supported");
3634 return -1;
3635 }
3636
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003637 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003638 if (ret)
3639 return ret;
3640
3641 ret = hostapd_drv_switch_channel(hapd, settings);
3642 free_beacon_data(&settings->beacon_csa);
3643 free_beacon_data(&settings->beacon_after);
3644
3645 if (ret) {
3646 /* if we failed, clean cs parameters */
3647 hostapd_cleanup_cs_params(hapd);
3648 return ret;
3649 }
3650
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003651 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003652 return 0;
3653}
3654
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003655
3656void
3657hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3658 const struct hostapd_freq_params *freq_params)
3659{
Hai Shalom81f62d82019-07-22 12:10:00 -07003660 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003661
3662 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3663
3664 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003665 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003666 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003667 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003668
3669 switch (freq_params->bandwidth) {
3670 case 0:
3671 case 20:
3672 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003673 bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003674 break;
3675 case 80:
3676 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003677 bw = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003678 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003679 bw = CHANWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003680 break;
3681 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003682 bw = CHANWIDTH_160MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003683 break;
3684 default:
3685 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3686 freq_params->bandwidth);
3687 break;
3688 }
3689
3690 iface->freq = freq_params->freq;
3691 iface->conf->channel = freq_params->channel;
3692 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003693 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3694 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3695 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003696 iface->conf->ieee80211n = freq_params->ht_enabled;
3697 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003698 iface->conf->ieee80211ax = freq_params->he_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003699
3700 /*
3701 * cs_params must not be cleared earlier because the freq_params
3702 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003703 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003704 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003705 hostapd_disable_iface(iface);
3706 hostapd_enable_iface(iface);
3707}
3708
Dmitry Shmidte4663042016-04-04 10:07:49 -07003709#endif /* NEED_AP_MLME */
3710
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003711
3712struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3713 const char *ifname)
3714{
3715 size_t i, j;
3716
3717 for (i = 0; i < interfaces->count; i++) {
3718 struct hostapd_iface *iface = interfaces->iface[i];
3719
3720 for (j = 0; j < iface->num_bss; j++) {
3721 struct hostapd_data *hapd = iface->bss[j];
3722
3723 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3724 return hapd;
3725 }
3726 }
3727
3728 return NULL;
3729}
3730
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003731
3732void hostapd_periodic_iface(struct hostapd_iface *iface)
3733{
3734 size_t i;
3735
3736 ap_list_timer(iface);
3737
3738 for (i = 0; i < iface->num_bss; i++) {
3739 struct hostapd_data *hapd = iface->bss[i];
3740
3741 if (!hapd->started)
3742 continue;
3743
3744#ifndef CONFIG_NO_RADIUS
3745 hostapd_acl_expire(hapd);
3746#endif /* CONFIG_NO_RADIUS */
3747 }
3748}
Hai Shalom899fcc72020-10-19 14:38:18 -07003749
3750
3751#ifdef CONFIG_OCV
3752void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
3753{
3754 struct hostapd_data *hapd = eloop_ctx;
3755 struct sta_info *sta;
3756
3757 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
3758
3759 for (sta = hapd->sta_list; sta; sta = sta->next) {
3760 if (!sta->post_csa_sa_query)
3761 continue;
3762
3763 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
3764 " did not start SA Query after CSA - disconnect",
3765 MAC2STR(sta->addr));
3766 ap_sta_disconnect(hapd, sta, sta->addr,
3767 WLAN_REASON_PREV_AUTH_NOT_VALID);
3768 }
3769}
3770#endif /* CONFIG_OCV */