blob: 05faa0f143d58b51cf01f4e1f50116783d805306 [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);
Sunil Ravia04bd252022-05-02 22:54:18 -070069#ifdef CONFIG_IEEE80211AX
70static void hostapd_switch_color_timeout_handler(void *eloop_data,
71 void *user_ctx);
72#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074
Dmitry Shmidt04949592012-07-19 12:16:46 -070075int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
76 int (*cb)(struct hostapd_iface *iface,
77 void *ctx), void *ctx)
78{
79 size_t i;
80 int ret;
81
82 for (i = 0; i < interfaces->count; i++) {
Hai Shalom4fbc08f2020-05-18 12:37:00 -070083 if (!interfaces->iface[i])
84 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -070085 ret = cb(interfaces->iface[i], ctx);
86 if (ret)
87 return ret;
88 }
89
90 return 0;
91}
92
93
Hai Shalomce48b4a2018-09-05 11:41:35 -070094void hostapd_reconfig_encryption(struct hostapd_data *hapd)
95{
96 if (hapd->wpa_auth)
97 return;
98
99 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700100#ifdef CONFIG_WEP
Hai Shalomce48b4a2018-09-05 11:41:35 -0700101 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700102#endif /* CONFIG_WEP */
Hai Shalomce48b4a2018-09-05 11:41:35 -0700103}
104
105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106static void hostapd_reload_bss(struct hostapd_data *hapd)
107{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800108 struct hostapd_ssid *ssid;
109
Dmitry Shmidt29333592017-01-09 12:27:11 -0800110 if (!hapd->started)
111 return;
112
Roshan Pius3a1667e2018-07-03 15:17:14 -0700113 if (hapd->conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -0800114 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
115 hapd->iconf->ieee80211ax;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117#ifndef CONFIG_NO_RADIUS
118 radius_client_reconfig(hapd->radius, hapd->conf->radius);
119#endif /* CONFIG_NO_RADIUS */
120
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800121 ssid = &hapd->conf->ssid;
122 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
123 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
124 /*
125 * Force PSK to be derived again since SSID or passphrase may
126 * have changed.
127 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800128 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 if (hostapd_setup_wpa_psk(hapd->conf)) {
131 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
132 "after reloading configuration");
133 }
134
135 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
136 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
137 else
138 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
139
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800140 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800142 if (hapd->wpa_auth)
143 wpa_init_keys(hapd->wpa_auth);
144 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145 const u8 *wpa_ie;
146 size_t wpa_ie_len;
147 hostapd_reconfig_wpa(hapd);
148 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
149 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
150 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
151 "the kernel driver.");
152 } else if (hapd->wpa_auth) {
153 wpa_deinit(hapd->wpa_auth);
154 hapd->wpa_auth = NULL;
155 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700156#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700158#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
160 }
161
162 ieee802_11_set_beacon(hapd);
163 hostapd_update_wps(hapd);
164
165 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700166 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167 hapd->conf->ssid.ssid_len)) {
168 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
169 /* try to continue */
170 }
171 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
172}
173
174
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700175static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 size_t j;
178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179 /*
180 * Deauthenticate all stations since the new configuration may not
181 * allow them to use the BSS anymore.
182 */
183 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700184 hostapd_flush_old_stations(iface->bss[j],
185 WLAN_REASON_PREV_AUTH_NOT_VALID);
Hai Shalomfdcde762020-04-02 11:19:20 -0700186#ifdef CONFIG_WEP
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700187 hostapd_broadcast_wep_clear(iface->bss[j]);
Hai Shalomfdcde762020-04-02 11:19:20 -0700188#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189
190#ifndef CONFIG_NO_RADIUS
191 /* TODO: update dynamic data based on changed configuration
192 * items (e.g., open/close sockets, etc.) */
193 radius_client_flush(iface->bss[j]->radius, 0);
194#endif /* CONFIG_NO_RADIUS */
195 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700196}
197
198
Hai Shalom74f70d42019-02-11 14:42:39 -0800199static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
200 struct hostapd_config *oldconf)
201{
202 size_t i;
203
204 if (newconf->num_bss != oldconf->num_bss)
205 return 1;
206
207 for (i = 0; i < newconf->num_bss; i++) {
208 if (os_strcmp(newconf->bss[i]->iface,
209 oldconf->bss[i]->iface) != 0)
210 return 1;
211 }
212
213 return 0;
214}
215
216
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700217int hostapd_reload_config(struct hostapd_iface *iface)
218{
Hai Shalom74f70d42019-02-11 14:42:39 -0800219 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700220 struct hostapd_data *hapd = iface->bss[0];
221 struct hostapd_config *newconf, *oldconf;
222 size_t j;
223
224 if (iface->config_fname == NULL) {
225 /* Only in-memory config in use - assume it has been updated */
226 hostapd_clear_old(iface);
227 for (j = 0; j < iface->num_bss; j++)
228 hostapd_reload_bss(iface->bss[j]);
229 return 0;
230 }
231
232 if (iface->interfaces == NULL ||
233 iface->interfaces->config_read_cb == NULL)
234 return -1;
235 newconf = iface->interfaces->config_read_cb(iface->config_fname);
236 if (newconf == NULL)
237 return -1;
238
239 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240
241 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800242 if (hostapd_iface_conf_changed(newconf, oldconf)) {
243 char *fname;
244 int res;
245
246 wpa_printf(MSG_DEBUG,
247 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
248 fname = os_strdup(iface->config_fname);
249 if (!fname) {
250 hostapd_config_free(newconf);
251 return -1;
252 }
253 hostapd_remove_iface(interfaces, hapd->conf->iface);
254 iface = hostapd_init(interfaces, fname);
255 os_free(fname);
256 hostapd_config_free(newconf);
257 if (!iface) {
258 wpa_printf(MSG_ERROR,
259 "Failed to initialize interface on config reload");
260 return -1;
261 }
262 iface->interfaces = interfaces;
263 interfaces->iface[interfaces->count] = iface;
264 interfaces->count++;
265 res = hostapd_enable_iface(iface);
266 if (res < 0)
267 wpa_printf(MSG_ERROR,
268 "Failed to enable interface on config reload");
269 return res;
270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 iface->conf = newconf;
272
273 for (j = 0; j < iface->num_bss; j++) {
274 hapd = iface->bss[j];
275 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700276 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700277 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700278 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700279 hapd->iconf->ieee80211n = oldconf->ieee80211n;
280 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
281 hapd->iconf->ht_capab = oldconf->ht_capab;
282 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700283 hostapd_set_oper_chwidth(hapd->iconf,
284 hostapd_get_oper_chwidth(oldconf));
285 hostapd_set_oper_centr_freq_seg0_idx(
286 hapd->iconf,
287 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
288 hostapd_set_oper_centr_freq_seg1_idx(
289 hapd->iconf,
290 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800291 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 hostapd_reload_bss(hapd);
293 }
294
295 hostapd_config_free(oldconf);
296
297
298 return 0;
299}
300
301
Hai Shalomfdcde762020-04-02 11:19:20 -0700302#ifdef CONFIG_WEP
303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700305 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306{
307 int i;
308
Dmitry Shmidt29333592017-01-09 12:27:11 -0800309 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700310 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700312 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
313 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 wpa_printf(MSG_DEBUG, "Failed to clear default "
315 "encryption keys (ifname=%s keyidx=%d)",
316 ifname, i);
317 }
318 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 if (hapd->conf->ieee80211w) {
320 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
321 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700322 NULL, i, 0, 0, NULL,
323 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 wpa_printf(MSG_DEBUG, "Failed to clear "
325 "default mgmt encryption keys "
326 "(ifname=%s keyidx=%d)", ifname, i);
327 }
328 }
329 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330}
331
332
333static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
334{
335 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
336 return 0;
337}
338
339
340static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
341{
342 int errors = 0, idx;
343 struct hostapd_ssid *ssid = &hapd->conf->ssid;
344
345 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700346 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700348 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700350 ssid->wep.len[idx],
351 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
353 errors++;
354 }
355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 return errors;
357}
358
Hai Shalomfdcde762020-04-02 11:19:20 -0700359#endif /* CONFIG_WEP */
360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361
Hai Shalom60840252021-02-19 19:02:11 -0800362void hostapd_free_hapd_data(struct hostapd_data *hapd)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700363{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800364 os_free(hapd->probereq_cb);
365 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800366 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800367
368#ifdef CONFIG_P2P
369 wpabuf_free(hapd->p2p_beacon_ie);
370 hapd->p2p_beacon_ie = NULL;
371 wpabuf_free(hapd->p2p_probe_resp_ie);
372 hapd->p2p_probe_resp_ie = NULL;
373#endif /* CONFIG_P2P */
374
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800375 if (!hapd->started) {
376 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700377 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800378 return;
379 }
380 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700381 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800382
Dmitry Shmidt54605472013-11-08 11:10:19 -0800383 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 accounting_deinit(hapd);
385 hostapd_deinit_wpa(hapd);
386 vlan_deinit(hapd);
387 hostapd_acl_deinit(hapd);
388#ifndef CONFIG_NO_RADIUS
389 radius_client_deinit(hapd->radius);
390 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700391 radius_das_deinit(hapd->radius_das);
392 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393#endif /* CONFIG_NO_RADIUS */
394
395 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700396 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700397#ifdef CONFIG_DPP
398 hostapd_dpp_deinit(hapd);
399 gas_query_ap_deinit(hapd->gas);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800400 hapd->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700401#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402
403 authsrv_deinit(hapd);
404
Dmitry Shmidt71757432014-06-02 13:50:35 -0700405 if (hapd->interface_added) {
406 hapd->interface_added = 0;
407 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
408 wpa_printf(MSG_WARNING,
409 "Failed to remove BSS interface %s",
410 hapd->conf->iface);
411 hapd->interface_added = 1;
412 } else {
413 /*
414 * Since this was a dynamically added interface, the
415 * driver wrapper may have removed its internal instance
416 * and hapd->drv_priv is not valid anymore.
417 */
418 hapd->drv_priv = NULL;
419 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 }
421
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800422 wpabuf_free(hapd->time_adv);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800423 hapd->time_adv = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700424
425#ifdef CONFIG_INTERWORKING
426 gas_serv_deinit(hapd);
427#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800428
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800429 bss_load_update_deinit(hapd);
430 ndisc_snoop_deinit(hapd);
431 dhcp_snoop_deinit(hapd);
432 x_snoop_deinit(hapd);
433
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800434#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700435 bin_clear_free(hapd->tmp_eap_user.identity,
436 hapd->tmp_eap_user.identity_len);
437 bin_clear_free(hapd->tmp_eap_user.password,
438 hapd->tmp_eap_user.password_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800439 os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800440#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800441
442#ifdef CONFIG_MESH
443 wpabuf_free(hapd->mesh_pending_auth);
444 hapd->mesh_pending_auth = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800445 /* handling setup failure is already done */
446 hapd->setup_complete_cb = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800447#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700448
449 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800450 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700451
Hai Shalom899fcc72020-10-19 14:38:18 -0700452#ifdef CONFIG_OCV
453 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
454#endif /* CONFIG_OCV */
455
Hai Shalom021b0b52019-04-10 11:17:58 -0700456#ifdef CONFIG_SAE
457 {
458 struct hostapd_sae_commit_queue *q;
459
460 while ((q = dl_list_first(&hapd->sae_commit_queue,
461 struct hostapd_sae_commit_queue,
462 list))) {
463 dl_list_del(&q->list);
464 os_free(q);
465 }
466 }
467 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
468#endif /* CONFIG_SAE */
Sunil Ravia04bd252022-05-02 22:54:18 -0700469
470#ifdef CONFIG_IEEE80211AX
471 eloop_cancel_timeout(hostapd_switch_color_timeout_handler, hapd, NULL);
472#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700473}
474
475
476/**
477 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
478 * @hapd: Pointer to BSS data
479 *
480 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800481 * Most of the modules that are initialized in hostapd_setup_bss() are
482 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700483 */
484static void hostapd_cleanup(struct hostapd_data *hapd)
485{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800486 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700487 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700488 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800489 hapd->iface->interfaces->ctrl_iface_deinit) {
490 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700491 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800492 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494}
495
496
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800497static void sta_track_deinit(struct hostapd_iface *iface)
498{
499 struct hostapd_sta_info *info;
500
501 if (!iface->num_sta_seen)
502 return;
503
504 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
505 list))) {
506 dl_list_del(&info->list);
507 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700508 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800509 }
510}
511
512
Hai Shalom60840252021-02-19 19:02:11 -0800513void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700514{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800515 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800516#ifdef NEED_AP_MLME
517 hostapd_stop_setup_timers(iface);
518#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700519 if (iface->current_mode)
520 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700521 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
522 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700523 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700524 os_free(iface->current_rates);
525 iface->current_rates = NULL;
526 os_free(iface->basic_rates);
527 iface->basic_rates = NULL;
528 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800529 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700530 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700531}
532
533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534/**
535 * hostapd_cleanup_iface - Complete per-interface cleanup
536 * @iface: Pointer to interface data
537 *
538 * This function is called after per-BSS data structures are deinitialized
539 * with hostapd_cleanup().
540 */
541static void hostapd_cleanup_iface(struct hostapd_iface *iface)
542{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800543 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800544 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700545 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
546 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800547
Dmitry Shmidt04949592012-07-19 12:16:46 -0700548 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 hostapd_config_free(iface->conf);
550 iface->conf = NULL;
551
552 os_free(iface->config_fname);
553 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800554 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 os_free(iface);
556}
557
558
Hai Shalomfdcde762020-04-02 11:19:20 -0700559#ifdef CONFIG_WEP
560
Dmitry Shmidt04949592012-07-19 12:16:46 -0700561static void hostapd_clear_wep(struct hostapd_data *hapd)
562{
Hai Shalom021b0b52019-04-10 11:17:58 -0700563 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700564 hostapd_set_privacy(hapd, 0);
565 hostapd_broadcast_wep_clear(hapd);
566 }
567}
568
569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
571{
572 int i;
573
574 hostapd_broadcast_wep_set(hapd);
575
576 if (hapd->conf->ssid.wep.default_len) {
577 hostapd_set_privacy(hapd, 1);
578 return 0;
579 }
580
Jouni Malinen75ecf522011-06-27 15:19:46 -0700581 /*
582 * When IEEE 802.1X is not enabled, the driver may need to know how to
583 * set authentication algorithms for static WEP.
584 */
585 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 for (i = 0; i < 4; i++) {
588 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700589 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 i == hapd->conf->ssid.wep.idx, NULL, 0,
591 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700592 hapd->conf->ssid.wep.len[i],
593 i == hapd->conf->ssid.wep.idx ?
594 KEY_FLAG_GROUP_RX_TX_DEFAULT :
595 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 wpa_printf(MSG_WARNING, "Could not set WEP "
597 "encryption.");
598 return -1;
599 }
600 if (hapd->conf->ssid.wep.key[i] &&
601 i == hapd->conf->ssid.wep.idx)
602 hostapd_set_privacy(hapd, 1);
603 }
604
605 return 0;
606}
607
Hai Shalomfdcde762020-04-02 11:19:20 -0700608#endif /* CONFIG_WEP */
609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610
Dmitry Shmidt04949592012-07-19 12:16:46 -0700611static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612{
613 int ret = 0;
614 u8 addr[ETH_ALEN];
615
616 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
617 return 0;
618
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800619 if (!hapd->iface->driver_ap_teardown) {
620 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
621 "Flushing old station entries");
622
623 if (hostapd_flush(hapd)) {
624 wpa_msg(hapd->msg_ctx, MSG_WARNING,
625 "Could not connect to kernel driver");
626 ret = -1;
627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700629 if (hapd->conf && hapd->conf->broadcast_deauth) {
630 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
631 "Deauthenticate all stations");
632 os_memset(addr, 0xff, ETH_ALEN);
633 hostapd_drv_sta_deauth(hapd, addr, reason);
634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 hostapd_free_stas(hapd);
636
637 return ret;
638}
639
640
Hai Shalom60840252021-02-19 19:02:11 -0800641void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
Dmitry Shmidt71757432014-06-02 13:50:35 -0700642{
643 hostapd_free_stas(hapd);
644 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700645#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700646 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700647#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700648}
649
650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651/**
652 * hostapd_validate_bssid_configuration - Validate BSSID configuration
653 * @iface: Pointer to interface data
654 * Returns: 0 on success, -1 on failure
655 *
656 * This function is used to validate that the configured BSSIDs are valid.
657 */
658static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
659{
660 u8 mask[ETH_ALEN] = { 0 };
661 struct hostapd_data *hapd = iface->bss[0];
662 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 int auto_addr = 0;
664
665 if (hostapd_drv_none(hapd))
666 return 0;
667
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800668 if (iface->conf->use_driver_iface_addr)
669 return 0;
670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
672
673 /* Determine the bits necessary to cover the number of BSSIDs. */
674 for (i--; i; i >>= 1)
675 bits++;
676
677 /* Determine the bits necessary to any configured BSSIDs,
678 if they are higher than the number of BSSIDs. */
679 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800680 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 if (j)
682 auto_addr++;
683 continue;
684 }
685
686 for (i = 0; i < ETH_ALEN; i++) {
687 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800688 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 hapd->own_addr[i];
690 }
691 }
692
693 if (!auto_addr)
694 goto skip_mask_ext;
695
696 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
697 ;
698 j = 0;
699 if (i < ETH_ALEN) {
700 j = (5 - i) * 8;
701
702 while (mask[i] != 0) {
703 mask[i] >>= 1;
704 j++;
705 }
706 }
707
708 if (bits < j)
709 bits = j;
710
711 if (bits > 40) {
712 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
713 bits);
714 return -1;
715 }
716
717 os_memset(mask, 0xff, ETH_ALEN);
718 j = bits / 8;
719 for (i = 5; i > 5 - j; i--)
720 mask[i] = 0;
721 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700722 while (j) {
723 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700725 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726
727skip_mask_ext:
728 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
729 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 if (!auto_addr)
732 return 0;
733
734 for (i = 0; i < ETH_ALEN; i++) {
735 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
736 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
737 " for start address " MACSTR ".",
738 MAC2STR(mask), MAC2STR(hapd->own_addr));
739 wpa_printf(MSG_ERROR, "Start address must be the "
740 "first address in the block (i.e., addr "
741 "AND mask == addr).");
742 return -1;
743 }
744 }
745
746 return 0;
747}
748
749
750static int mac_in_conf(struct hostapd_config *conf, const void *a)
751{
752 size_t i;
753
754 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800755 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 return 1;
757 }
758 }
759
760 return 0;
761}
762
763
Dmitry Shmidt04949592012-07-19 12:16:46 -0700764#ifndef CONFIG_NO_RADIUS
765
766static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
767 struct radius_das_attrs *attr)
768{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800769 if (attr->nas_identifier &&
770 (!hapd->conf->nas_identifier ||
771 os_strlen(hapd->conf->nas_identifier) !=
772 attr->nas_identifier_len ||
773 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
774 attr->nas_identifier_len) != 0)) {
775 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
776 return 1;
777 }
778
779 if (attr->nas_ip_addr &&
780 (hapd->conf->own_ip_addr.af != AF_INET ||
781 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
782 0)) {
783 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
784 return 1;
785 }
786
787#ifdef CONFIG_IPV6
788 if (attr->nas_ipv6_addr &&
789 (hapd->conf->own_ip_addr.af != AF_INET6 ||
790 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
791 != 0)) {
792 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
793 return 1;
794 }
795#endif /* CONFIG_IPV6 */
796
Dmitry Shmidt04949592012-07-19 12:16:46 -0700797 return 0;
798}
799
800
801static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800802 struct radius_das_attrs *attr,
803 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700804{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800805 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700806 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800807 int num_attr = 0;
808 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700809
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800810 *multi = 0;
811
812 for (sta = hapd->sta_list; sta; sta = sta->next)
813 sta->radius_das_match = 1;
814
815 if (attr->sta_addr) {
816 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800818 if (!sta) {
819 wpa_printf(MSG_DEBUG,
820 "RADIUS DAS: No Calling-Station-Id match");
821 return NULL;
822 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700823
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800824 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700825 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800826 if (sta != selected)
827 sta->radius_das_match = 0;
828 }
829 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
830 }
831
832 if (attr->acct_session_id) {
833 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800834 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800835 wpa_printf(MSG_DEBUG,
836 "RADIUS DAS: Acct-Session-Id cannot match");
837 return NULL;
838 }
839 count = 0;
840
841 for (sta = hapd->sta_list; sta; sta = sta->next) {
842 if (!sta->radius_das_match)
843 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800844 os_snprintf(buf, sizeof(buf), "%016llX",
845 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800846 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800847 sta->radius_das_match = 0;
848 else
849 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700850 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800851
852 if (count == 0) {
853 wpa_printf(MSG_DEBUG,
854 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
855 return NULL;
856 }
857 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700858 }
859
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800860 if (attr->acct_multi_session_id) {
861 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800862 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800863 wpa_printf(MSG_DEBUG,
864 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
865 return NULL;
866 }
867 count = 0;
868
869 for (sta = hapd->sta_list; sta; sta = sta->next) {
870 if (!sta->radius_das_match)
871 continue;
872 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800873 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800874 sta->radius_das_match = 0;
875 continue;
876 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800877 os_snprintf(buf, sizeof(buf), "%016llX",
878 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800879 sta->eapol_sm->acct_multi_session_id);
880 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800881 0)
882 sta->radius_das_match = 0;
883 else
884 count++;
885 }
886
887 if (count == 0) {
888 wpa_printf(MSG_DEBUG,
889 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
890 return NULL;
891 }
892 wpa_printf(MSG_DEBUG,
893 "RADIUS DAS: Acct-Multi-Session-Id match");
894 }
895
896 if (attr->cui) {
897 num_attr++;
898 count = 0;
899
Dmitry Shmidt04949592012-07-19 12:16:46 -0700900 for (sta = hapd->sta_list; sta; sta = sta->next) {
901 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800902
903 if (!sta->radius_das_match)
904 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700905 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800906 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700907 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800908 attr->cui_len) != 0)
909 sta->radius_das_match = 0;
910 else
911 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700912 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800913
914 if (count == 0) {
915 wpa_printf(MSG_DEBUG,
916 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
917 return NULL;
918 }
919 wpa_printf(MSG_DEBUG,
920 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700921 }
922
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800923 if (attr->user_name) {
924 num_attr++;
925 count = 0;
926
Dmitry Shmidt04949592012-07-19 12:16:46 -0700927 for (sta = hapd->sta_list; sta; sta = sta->next) {
928 u8 *identity;
929 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800930
931 if (!sta->radius_das_match)
932 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700933 identity = ieee802_1x_get_identity(sta->eapol_sm,
934 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800935 if (!identity ||
936 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700937 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800938 != 0)
939 sta->radius_das_match = 0;
940 else
941 count++;
942 }
943
944 if (count == 0) {
945 wpa_printf(MSG_DEBUG,
946 "RADIUS DAS: No matches remaining after User-Name check");
947 return NULL;
948 }
949 wpa_printf(MSG_DEBUG,
950 "RADIUS DAS: User-Name match");
951 }
952
953 if (num_attr == 0) {
954 /*
955 * In theory, we could match all current associations, but it
956 * seems safer to just reject requests that do not include any
957 * session identification attributes.
958 */
959 wpa_printf(MSG_DEBUG,
960 "RADIUS DAS: No session identification attributes included");
961 return NULL;
962 }
963
964 selected = NULL;
965 for (sta = hapd->sta_list; sta; sta = sta->next) {
966 if (sta->radius_das_match) {
967 if (selected) {
968 *multi = 1;
969 return NULL;
970 }
971 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700972 }
973 }
974
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800975 return selected;
976}
977
978
979static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
980 struct radius_das_attrs *attr)
981{
982 if (!hapd->wpa_auth)
983 return -1;
984 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700985}
986
987
988static enum radius_das_res
989hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
990{
991 struct hostapd_data *hapd = ctx;
992 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800993 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700994
995 if (hostapd_das_nas_mismatch(hapd, attr))
996 return RADIUS_DAS_NAS_MISMATCH;
997
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800998 sta = hostapd_das_find_sta(hapd, attr, &multi);
999 if (sta == NULL) {
1000 if (multi) {
1001 wpa_printf(MSG_DEBUG,
1002 "RADIUS DAS: Multiple sessions match - not supported");
1003 return RADIUS_DAS_MULTI_SESSION_MATCH;
1004 }
1005 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1006 wpa_printf(MSG_DEBUG,
1007 "RADIUS DAS: PMKSA cache entry matched");
1008 return RADIUS_DAS_SUCCESS;
1009 }
1010 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001012 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001013
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001014 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1015 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001016 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1017
Dmitry Shmidt04949592012-07-19 12:16:46 -07001018 hostapd_drv_sta_deauth(hapd, sta->addr,
1019 WLAN_REASON_PREV_AUTH_NOT_VALID);
1020 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1021
1022 return RADIUS_DAS_SUCCESS;
1023}
1024
Roshan Pius3a1667e2018-07-03 15:17:14 -07001025
1026#ifdef CONFIG_HS20
1027static enum radius_das_res
1028hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1029{
1030 struct hostapd_data *hapd = ctx;
1031 struct sta_info *sta;
1032 int multi;
1033
1034 if (hostapd_das_nas_mismatch(hapd, attr))
1035 return RADIUS_DAS_NAS_MISMATCH;
1036
1037 sta = hostapd_das_find_sta(hapd, attr, &multi);
1038 if (!sta) {
1039 if (multi) {
1040 wpa_printf(MSG_DEBUG,
1041 "RADIUS DAS: Multiple sessions match - not supported");
1042 return RADIUS_DAS_MULTI_SESSION_MATCH;
1043 }
1044 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1045 return RADIUS_DAS_SESSION_NOT_FOUND;
1046 }
1047
1048 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1049 " - CoA", MAC2STR(sta->addr));
1050
1051 if (attr->hs20_t_c_filtering) {
1052 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1053 wpa_printf(MSG_DEBUG,
1054 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1055 return RADIUS_DAS_COA_FAILED;
1056 }
1057
1058 hs20_t_c_filtering(hapd, sta, 0);
1059 }
1060
1061 return RADIUS_DAS_SUCCESS;
1062}
1063#else /* CONFIG_HS20 */
1064#define hostapd_das_coa NULL
1065#endif /* CONFIG_HS20 */
1066
Hai Shalomc3565922019-10-28 11:58:20 -07001067
1068#ifdef CONFIG_SQLITE
1069
1070static int db_table_exists(sqlite3 *db, const char *name)
1071{
1072 char cmd[128];
1073
1074 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1075 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1076}
1077
1078
1079static int db_table_create_radius_attributes(sqlite3 *db)
1080{
1081 char *err = NULL;
1082 const char *sql =
1083 "CREATE TABLE radius_attributes("
1084 " id INTEGER PRIMARY KEY,"
1085 " sta TEXT,"
1086 " reqtype TEXT,"
1087 " attr TEXT"
1088 ");"
1089 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1090
1091 wpa_printf(MSG_DEBUG,
1092 "Adding database table for RADIUS attribute information");
1093 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1094 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1095 sqlite3_free(err);
1096 return -1;
1097 }
1098
1099 return 0;
1100}
1101
1102#endif /* CONFIG_SQLITE */
1103
Dmitry Shmidt04949592012-07-19 12:16:46 -07001104#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105
1106
1107/**
1108 * hostapd_setup_bss - Per-BSS setup (initialization)
1109 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001110 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1111 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 *
1113 * This function is used to initialize all per-BSS data structures and
1114 * resources. This gets called in a loop for each BSS when an interface is
1115 * initialized. Most of the modules that are initialized here will be
1116 * deinitialized in hostapd_cleanup().
1117 */
1118static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1119{
1120 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001121 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 int ssid_len, set_ssid;
1123 char force_ifname[IFNAMSIZ];
1124 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001125 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126
Dmitry Shmidt54605472013-11-08 11:10:19 -08001127 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001128 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001129
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001130#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001131 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001132 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1133 return -1;
1134 }
1135#endif /* EAP_SERVER_TNC */
1136
Dmitry Shmidt54605472013-11-08 11:10:19 -08001137 if (hapd->started) {
1138 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001139 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001140 return -1;
1141 }
1142 hapd->started = 1;
1143
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001144 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001145 u8 *addr = hapd->own_addr;
1146
1147 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001149 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150
1151 if (hostapd_mac_comp(hapd->own_addr,
1152 hapd->iface->bss[0]->own_addr) ==
1153 0) {
1154 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1155 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001156 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 return -1;
1158 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001159 } else if (hapd->iconf->use_driver_iface_addr) {
1160 addr = NULL;
1161 } else {
1162 /* Allocate the next available BSSID. */
1163 do {
1164 inc_byte_array(hapd->own_addr, ETH_ALEN);
1165 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 }
1167
Dmitry Shmidt54605472013-11-08 11:10:19 -08001168 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001170 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001172 conf->bridge[0] ? conf->bridge : NULL,
1173 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1175 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001176 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 return -1;
1178 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001179
1180 if (!addr)
1181 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 }
1183
1184 if (conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -08001185 conf->wmm_enabled = hapd->iconf->ieee80211n |
1186 hapd->iconf->ieee80211ax;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001188#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001189 if (is_zero_ether_addr(conf->r1_key_holder))
1190 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001191#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001192
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001193#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001194 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001195 flush_old_stations = 0;
1196#endif /* CONFIG_MESH */
1197
1198 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001199 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 hostapd_set_privacy(hapd, 0);
1201
Hai Shalomfdcde762020-04-02 11:19:20 -07001202#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001203 if (!hostapd_drv_nl80211(hapd))
1204 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001205 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001207#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208
1209 /*
1210 * Fetch the SSID from the system and use it or,
1211 * if one was specified in the config file, verify they
1212 * match.
1213 */
1214 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1215 if (ssid_len < 0) {
1216 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1217 return -1;
1218 }
1219 if (conf->ssid.ssid_set) {
1220 /*
1221 * If SSID is specified in the config file and it differs
1222 * from what is being used then force installation of the
1223 * new SSID.
1224 */
1225 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1226 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1227 } else {
1228 /*
1229 * No SSID in the config file; just use the one we got
1230 * from the system.
1231 */
1232 set_ssid = 0;
1233 conf->ssid.ssid_len = ssid_len;
1234 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 }
1236
Hai Shalomfdcde762020-04-02 11:19:20 -07001237 /*
1238 * Short SSID calculation is identical to FCS and it is defined in
1239 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1240 */
1241 conf->ssid.short_ssid = crc32(conf->ssid.ssid, conf->ssid.ssid_len);
1242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001244 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001245 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001246 conf->iface, MAC2STR(hapd->own_addr),
1247 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 }
1249
1250 if (hostapd_setup_wpa_psk(conf)) {
1251 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1252 return -1;
1253 }
1254
1255 /* Set SSID for the kernel driver (to be used in beacon and probe
1256 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001257 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 conf->ssid.ssid_len)) {
1259 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1260 return -1;
1261 }
1262
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001263 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 conf->radius->msg_dumps = 1;
1265#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001266
1267#ifdef CONFIG_SQLITE
1268 if (conf->radius_req_attr_sqlite) {
1269 if (sqlite3_open(conf->radius_req_attr_sqlite,
1270 &hapd->rad_attr_db)) {
1271 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1272 conf->radius_req_attr_sqlite);
1273 return -1;
1274 }
1275
1276 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1277 conf->radius_req_attr_sqlite);
1278 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1279 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1280 return -1;
1281 }
1282#endif /* CONFIG_SQLITE */
1283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284 hapd->radius = radius_client_init(hapd, conf->radius);
1285 if (hapd->radius == NULL) {
1286 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1287 return -1;
1288 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001289
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001290 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001291 struct radius_das_conf das_conf;
1292 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001293 das_conf.port = conf->radius_das_port;
1294 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001295 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001296 conf->radius_das_shared_secret_len;
1297 das_conf.client_addr = &conf->radius_das_client_addr;
1298 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001299 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001300 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001301 das_conf.require_message_authenticator =
1302 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001303 das_conf.ctx = hapd;
1304 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001305 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001306 hapd->radius_das = radius_das_init(&das_conf);
1307 if (hapd->radius_das == NULL) {
1308 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1309 "failed.");
1310 return -1;
1311 }
1312 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313#endif /* CONFIG_NO_RADIUS */
1314
1315 if (hostapd_acl_init(hapd)) {
1316 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1317 return -1;
1318 }
1319 if (hostapd_init_wps(hapd, conf))
1320 return -1;
1321
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001322#ifdef CONFIG_DPP
1323 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1324 if (!hapd->gas)
1325 return -1;
1326 if (hostapd_dpp_init(hapd))
1327 return -1;
1328#endif /* CONFIG_DPP */
1329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330 if (authsrv_init(hapd) < 0)
1331 return -1;
1332
1333 if (ieee802_1x_init(hapd)) {
1334 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1335 return -1;
1336 }
1337
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001338 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 return -1;
1340
1341 if (accounting_init(hapd)) {
1342 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1343 return -1;
1344 }
1345
Dmitry Shmidt04949592012-07-19 12:16:46 -07001346#ifdef CONFIG_INTERWORKING
1347 if (gas_serv_init(hapd)) {
1348 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1349 return -1;
1350 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001351
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001352 if (conf->qos_map_set_len &&
1353 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1354 conf->qos_map_set_len)) {
1355 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 return -1;
1357 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001358#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001360 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1361 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1362 return -1;
1363 }
1364
1365 if (conf->proxy_arp) {
1366 if (x_snoop_init(hapd)) {
1367 wpa_printf(MSG_ERROR,
1368 "Generic snooping infrastructure initialization failed");
1369 return -1;
1370 }
1371
1372 if (dhcp_snoop_init(hapd)) {
1373 wpa_printf(MSG_ERROR,
1374 "DHCP snooping initialization failed");
1375 return -1;
1376 }
1377
1378 if (ndisc_snoop_init(hapd)) {
1379 wpa_printf(MSG_ERROR,
1380 "Neighbor Discovery snooping initialization failed");
1381 return -1;
1382 }
1383 }
1384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1386 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1387 return -1;
1388 }
1389
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001390 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001391 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001393 if (flush_old_stations && !conf->start_disabled &&
1394 conf->broadcast_deauth) {
1395 u8 addr[ETH_ALEN];
1396
1397 /* Should any previously associated STA not have noticed that
1398 * the AP had stopped and restarted, send one more
1399 * deauthentication notification now that the AP is ready to
1400 * operate. */
1401 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1402 "Deauthenticate all stations at BSS start");
1403 os_memset(addr, 0xff, ETH_ALEN);
1404 hostapd_drv_sta_deauth(hapd, addr,
1405 WLAN_REASON_PREV_AUTH_NOT_VALID);
1406 }
1407
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001408 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1409 return -1;
1410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 if (hapd->driver && hapd->driver->set_operstate)
1412 hapd->driver->set_operstate(hapd->drv_priv, 1);
1413
1414 return 0;
1415}
1416
1417
1418static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1419{
1420 struct hostapd_data *hapd = iface->bss[0];
1421 int i;
1422 struct hostapd_tx_queue_params *p;
1423
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001424#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001425 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001426 return;
1427#endif /* CONFIG_MESH */
1428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 for (i = 0; i < NUM_TX_QUEUES; i++) {
1430 p = &iface->conf->tx_queue[i];
1431
1432 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1433 p->cwmax, p->burst)) {
1434 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1435 "parameters for queue %d.", i);
1436 /* Continue anyway */
1437 }
1438 }
1439}
1440
1441
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001442static int hostapd_set_acl_list(struct hostapd_data *hapd,
1443 struct mac_acl_entry *mac_acl,
1444 int n_entries, u8 accept_acl)
1445{
1446 struct hostapd_acl_params *acl_params;
1447 int i, err;
1448
1449 acl_params = os_zalloc(sizeof(*acl_params) +
1450 (n_entries * sizeof(acl_params->mac_acl[0])));
1451 if (!acl_params)
1452 return -ENOMEM;
1453
1454 for (i = 0; i < n_entries; i++)
1455 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1456 ETH_ALEN);
1457
1458 acl_params->acl_policy = accept_acl;
1459 acl_params->num_mac_acl = n_entries;
1460
1461 err = hostapd_drv_set_acl(hapd, acl_params);
1462
1463 os_free(acl_params);
1464
1465 return err;
1466}
1467
1468
Sunil Ravia04bd252022-05-02 22:54:18 -07001469int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001470{
1471 struct hostapd_config *conf = hapd->iconf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001472 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001473 u8 accept_acl;
1474
1475 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001476 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001477
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001478 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001479 accept_acl = 1;
1480 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1481 conf->bss[0]->num_accept_mac,
1482 accept_acl);
1483 if (err) {
1484 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001485 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001486 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001487 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001488 accept_acl = 0;
1489 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1490 conf->bss[0]->num_deny_mac,
1491 accept_acl);
1492 if (err) {
1493 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001494 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001495 }
1496 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001497 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001498}
1499
1500
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001501static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1502{
1503 if (!hapd->iface->interfaces ||
1504 !hapd->iface->interfaces->ctrl_iface_init)
1505 return 0;
1506
1507 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1508 wpa_printf(MSG_ERROR,
1509 "Failed to setup control interface for %s",
1510 hapd->conf->iface);
1511 return -1;
1512 }
1513
1514 return 0;
1515}
1516
1517
1518static int start_ctrl_iface(struct hostapd_iface *iface)
1519{
1520 size_t i;
1521
1522 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1523 return 0;
1524
1525 for (i = 0; i < iface->num_bss; i++) {
1526 struct hostapd_data *hapd = iface->bss[i];
1527 if (iface->interfaces->ctrl_iface_init(hapd)) {
1528 wpa_printf(MSG_ERROR,
1529 "Failed to setup control interface for %s",
1530 hapd->conf->iface);
1531 return -1;
1532 }
1533 }
1534
1535 return 0;
1536}
1537
1538
1539static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1540{
1541 struct hostapd_iface *iface = eloop_ctx;
1542
1543 if (!iface->wait_channel_update) {
1544 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1545 return;
1546 }
1547
1548 /*
1549 * It is possible that the existing channel list is acceptable, so try
1550 * to proceed.
1551 */
1552 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1553 setup_interface2(iface);
1554}
1555
1556
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001557void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001558{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001559 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001560 return;
1561
1562 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1563 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1564 setup_interface2(iface);
1565}
1566
1567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568static int setup_interface(struct hostapd_iface *iface)
1569{
1570 struct hostapd_data *hapd = iface->bss[0];
1571 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001572
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001573 /*
1574 * It is possible that setup_interface() is called after the interface
1575 * was disabled etc., in which case driver_ap_teardown is possibly set
1576 * to 1. Clear it here so any other key/station deletion, which is not
1577 * part of a teardown flow, would also call the relevant driver
1578 * callbacks.
1579 */
1580 iface->driver_ap_teardown = 0;
1581
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001582 if (!iface->phy[0]) {
1583 const char *phy = hostapd_drv_get_radio_name(hapd);
1584 if (phy) {
1585 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1586 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1587 }
1588 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589
1590 /*
1591 * Make sure that all BSSes get configured with a pointer to the same
1592 * driver interface.
1593 */
1594 for (i = 1; i < iface->num_bss; i++) {
1595 iface->bss[i]->driver = hapd->driver;
1596 iface->bss[i]->drv_priv = hapd->drv_priv;
1597 }
1598
1599 if (hostapd_validate_bssid_configuration(iface))
1600 return -1;
1601
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001602 /*
1603 * Initialize control interfaces early to allow external monitoring of
1604 * channel setup operations that may take considerable amount of time
1605 * especially for DFS cases.
1606 */
1607 if (start_ctrl_iface(iface))
1608 return -1;
1609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001611 char country[4], previous_country[4];
1612
1613 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1614 if (hostapd_get_country(hapd, previous_country) < 0)
1615 previous_country[0] = '\0';
1616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617 os_memcpy(country, hapd->iconf->country, 3);
1618 country[3] = '\0';
1619 if (hostapd_set_country(hapd, country) < 0) {
1620 wpa_printf(MSG_ERROR, "Failed to set country code");
1621 return -1;
1622 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001623
1624 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1625 previous_country, country);
1626
1627 if (os_strncmp(previous_country, country, 2) != 0) {
1628 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1629 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001630 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001631 channel_list_update_timeout,
1632 iface, NULL);
1633 return 0;
1634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 }
1636
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001637 return setup_interface2(iface);
1638}
1639
1640
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001641static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1642{
1643 int freq, i, j;
1644
1645 if (!iface->conf->channel)
1646 return 0;
1647 if (iface->conf->op_class) {
1648 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1649 iface->conf->channel);
1650 if (freq < 0) {
1651 wpa_printf(MSG_INFO,
1652 "Could not convert op_class %u channel %u to operating frequency",
1653 iface->conf->op_class, iface->conf->channel);
1654 return -1;
1655 }
1656 iface->freq = freq;
1657 return 0;
1658 }
1659
1660 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1661 * op_class parameter. Select a matching channel from the configured
1662 * mode using the channel parameter for these cases.
1663 */
1664 for (j = 0; j < iface->num_hw_features; j++) {
1665 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1666
1667 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1668 iface->conf->hw_mode != mode->mode)
1669 continue;
1670 for (i = 0; i < mode->num_channels; i++) {
1671 struct hostapd_channel_data *chan = &mode->channels[i];
1672
1673 if (chan->chan == iface->conf->channel &&
1674 !is_6ghz_freq(chan->freq)) {
1675 iface->freq = chan->freq;
1676 return 0;
1677 }
1678 }
1679 }
1680
1681 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1682 return -1;
1683}
1684
1685
Hai Shaloma20dcd72022-02-04 13:43:00 -08001686static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
1687{
1688 int bw, seg0;
1689
1690 if (!is_6ghz_op_class(iface->conf->op_class))
1691 return;
1692
1693 seg0 = hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
1694 bw = center_idx_to_bw_6ghz(seg0);
1695 /* Assign the secondary channel if absent in config for
1696 * bandwidths > 20 MHz */
1697 if (bw > 20 && !iface->conf->secondary_channel) {
1698 if (((iface->conf->channel - 1) / 4) % 2)
1699 iface->conf->secondary_channel = -1;
1700 else
1701 iface->conf->secondary_channel = 1;
1702 }
1703}
1704
1705
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001706static int setup_interface2(struct hostapd_iface *iface)
1707{
1708 iface->wait_channel_update = 0;
1709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710 if (hostapd_get_hw_features(iface)) {
1711 /* Not all drivers support this yet, so continue without hw
1712 * feature data. */
1713 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001714 int ret;
1715
1716 ret = configured_fixed_chan_to_freq(iface);
1717 if (ret < 0)
1718 goto fail;
1719
1720 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001721 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001722
1723 ch_width = op_class_to_ch_width(iface->conf->op_class);
1724 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001725 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001726 }
1727
1728 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 if (ret < 0) {
1730 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1731 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001732 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001734 if (ret == 1) {
1735 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1736 return 0;
1737 }
Hai Shalomc3565922019-10-28 11:58:20 -07001738 ret = hostapd_check_edmg_capab(iface);
1739 if (ret < 0)
1740 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08001741 ret = hostapd_check_he_6ghz_capab(iface);
1742 if (ret < 0)
1743 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001744 ret = hostapd_check_ht_capab(iface);
1745 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001746 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 if (ret == 1) {
1748 wpa_printf(MSG_DEBUG, "Interface initialization will "
1749 "be completed in a callback");
1750 return 0;
1751 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001752
1753 if (iface->conf->ieee80211h)
1754 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755 }
1756 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001757
1758fail:
1759 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1760 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1761 if (iface->interfaces && iface->interfaces->terminate_on_error)
1762 eloop_terminate();
1763 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764}
1765
1766
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001767#ifdef CONFIG_FST
1768
1769static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1770{
1771 struct hostapd_data *hapd = ctx;
1772
1773 return hapd->own_addr;
1774}
1775
1776
1777static void fst_hostapd_get_channel_info_cb(void *ctx,
1778 enum hostapd_hw_mode *hw_mode,
1779 u8 *channel)
1780{
1781 struct hostapd_data *hapd = ctx;
1782
1783 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1784}
1785
1786
Sunil8cd6f4d2022-06-28 18:40:46 +00001787static int fst_hostapd_get_hw_modes_cb(void *ctx,
1788 struct hostapd_hw_modes **modes)
1789{
1790 struct hostapd_data *hapd = ctx;
1791
1792 *modes = hapd->iface->hw_features;
1793 return hapd->iface->num_hw_features;
1794}
1795
1796
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001797static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1798{
1799 struct hostapd_data *hapd = ctx;
1800
1801 if (hapd->iface->fst_ies != fst_ies) {
1802 hapd->iface->fst_ies = fst_ies;
1803 if (ieee802_11_set_beacon(hapd))
1804 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1805 }
1806}
1807
1808
1809static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1810 struct wpabuf *buf)
1811{
1812 struct hostapd_data *hapd = ctx;
1813
1814 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1815 wpabuf_head(buf), wpabuf_len(buf));
1816}
1817
1818
1819static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1820{
1821 struct hostapd_data *hapd = ctx;
1822 struct sta_info *sta = ap_get_sta(hapd, addr);
1823
1824 return sta ? sta->mb_ies : NULL;
1825}
1826
1827
1828static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1829 const u8 *buf, size_t size)
1830{
1831 struct hostapd_data *hapd = ctx;
1832 struct sta_info *sta = ap_get_sta(hapd, addr);
1833
1834 if (sta) {
1835 struct mb_ies_info info;
1836
1837 if (!mb_ies_info_by_ies(&info, buf, size)) {
1838 wpabuf_free(sta->mb_ies);
1839 sta->mb_ies = mb_ies_by_info(&info);
1840 }
1841 }
1842}
1843
1844
1845static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001846 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001847{
1848 struct sta_info *s = (struct sta_info *) *get_ctx;
1849
1850 if (mb_only) {
1851 for (; s && !s->mb_ies; s = s->next)
1852 ;
1853 }
1854
1855 if (s) {
1856 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1857
1858 return s->addr;
1859 }
1860
1861 *get_ctx = NULL;
1862 return NULL;
1863}
1864
1865
1866static const u8 * fst_hostapd_get_peer_first(void *ctx,
1867 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001868 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001869{
1870 struct hostapd_data *hapd = ctx;
1871
1872 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1873
1874 return fst_hostapd_get_sta(get_ctx, mb_only);
1875}
1876
1877
1878static const u8 * fst_hostapd_get_peer_next(void *ctx,
1879 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001880 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001881{
1882 return fst_hostapd_get_sta(get_ctx, mb_only);
1883}
1884
1885
1886void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1887 struct fst_wpa_obj *iface_obj)
1888{
Sunil8cd6f4d2022-06-28 18:40:46 +00001889 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001890 iface_obj->ctx = hapd;
1891 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1892 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00001893 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001894 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1895 iface_obj->send_action = fst_hostapd_send_action_cb;
1896 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1897 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1898 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1899 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1900}
1901
1902#endif /* CONFIG_FST */
1903
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001904#ifdef CONFIG_OWE
1905
1906static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1907{
1908 struct hostapd_data *hapd = ctx;
1909 size_t i;
1910
1911 for (i = 0; i < iface->num_bss; i++) {
1912 struct hostapd_data *bss = iface->bss[i];
1913
1914 if (os_strcmp(hapd->conf->owe_transition_ifname,
1915 bss->conf->iface) != 0)
1916 continue;
1917
1918 wpa_printf(MSG_DEBUG,
1919 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1920 MACSTR " SSID %s",
1921 hapd->conf->iface, bss->conf->iface,
1922 MAC2STR(bss->own_addr),
1923 wpa_ssid_txt(bss->conf->ssid.ssid,
1924 bss->conf->ssid.ssid_len));
1925 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1926 is_zero_ether_addr(bss->own_addr))
1927 continue;
1928
1929 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1930 ETH_ALEN);
1931 os_memcpy(hapd->conf->owe_transition_ssid,
1932 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1933 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1934 wpa_printf(MSG_DEBUG,
1935 "OWE: Copied transition mode information");
1936 return 1;
1937 }
1938
1939 return 0;
1940}
1941
1942
1943int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1944{
1945 if (hapd->conf->owe_transition_ssid_len > 0 &&
1946 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1947 return 0;
1948
1949 /* Find transition mode SSID/BSSID information from a BSS operated by
1950 * this hostapd instance. */
1951 if (!hapd->iface->interfaces ||
1952 !hapd->iface->interfaces->for_each_interface)
1953 return hostapd_owe_iface_iter(hapd->iface, hapd);
1954 else
1955 return hapd->iface->interfaces->for_each_interface(
1956 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1957}
1958
1959
1960static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1961{
1962 size_t i;
1963
1964 for (i = 0; i < iface->num_bss; i++) {
1965 struct hostapd_data *bss = iface->bss[i];
1966 int res;
1967
1968 if (!bss->conf->owe_transition_ifname[0])
1969 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07001970 if (bss->iface->state != HAPD_IFACE_ENABLED) {
1971 wpa_printf(MSG_DEBUG,
1972 "OWE: Interface %s state %s - defer beacon update",
1973 bss->conf->iface,
1974 hostapd_state_text(bss->iface->state));
1975 continue;
1976 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001977 res = hostapd_owe_trans_get_info(bss);
1978 if (res == 0)
1979 continue;
1980 wpa_printf(MSG_DEBUG,
1981 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1982 bss->conf->iface);
1983 ieee802_11_set_beacon(bss);
1984 }
1985
1986 return 0;
1987}
1988
1989#endif /* CONFIG_OWE */
1990
1991
1992static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1993{
1994#ifdef CONFIG_OWE
1995 /* Check whether the enabled BSS can complete OWE transition mode
1996 * configuration for any pending interface. */
1997 if (!iface->interfaces ||
1998 !iface->interfaces->for_each_interface)
1999 hostapd_owe_iface_iter2(iface, NULL);
2000 else
2001 iface->interfaces->for_each_interface(
2002 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2003#endif /* CONFIG_OWE */
2004}
2005
2006
Roshan Pius3a1667e2018-07-03 15:17:14 -07002007static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2008 void *timeout_ctx)
2009{
2010 struct hostapd_iface *iface = eloop_ctx;
2011 struct hostapd_data *hapd;
2012
2013 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2014 return;
2015 hapd = iface->bss[0];
2016 if (hapd->setup_complete_cb)
2017 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2018}
2019
2020
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002021static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2022 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023{
2024 struct hostapd_data *hapd = iface->bss[0];
2025 size_t j;
2026 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002027 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002028 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002030 if (err)
2031 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032
2033 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002034 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002035#ifdef NEED_AP_MLME
2036 int res;
2037#endif /* NEED_AP_MLME */
2038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2040 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002041 hostapd_hw_mode_txt(iface->conf->hw_mode),
2042 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043
Dmitry Shmidt051af732013-10-22 13:52:46 -07002044#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002045 /* Handle DFS only if it is not offloaded to the driver */
2046 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2047 /* Check DFS */
2048 res = hostapd_handle_dfs(iface);
2049 if (res <= 0) {
2050 if (res < 0)
2051 goto fail;
2052 return res;
2053 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002054 } else {
2055 /* If DFS is offloaded to the driver */
2056 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2057 if (res_dfs_offload <= 0) {
2058 if (res_dfs_offload < 0)
2059 goto fail;
2060 } else {
2061 wpa_printf(MSG_DEBUG,
2062 "Proceed with AP/channel setup");
2063 /*
2064 * If this is a DFS channel, move to completing
2065 * AP setup.
2066 */
2067 if (res_dfs_offload == 1)
2068 goto dfs_offload;
2069 /* Otherwise fall through. */
2070 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002071 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002072#endif /* NEED_AP_MLME */
2073
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002074#ifdef CONFIG_MESH
2075 if (iface->mconf != NULL) {
2076 wpa_printf(MSG_DEBUG,
2077 "%s: Mesh configuration will be applied while joining the mesh network",
2078 iface->bss[0]->conf->iface);
2079 delay_apply_cfg = 1;
2080 }
2081#endif /* CONFIG_MESH */
2082
2083 if (!delay_apply_cfg &&
2084 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002086 hapd->iconf->enable_edmg,
2087 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002089 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002090 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002091 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002092 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002093 hostapd_get_oper_chwidth(hapd->iconf),
2094 hostapd_get_oper_centr_freq_seg0_idx(
2095 hapd->iconf),
2096 hostapd_get_oper_centr_freq_seg1_idx(
2097 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098 wpa_printf(MSG_ERROR, "Could not set channel for "
2099 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002100 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 }
2102 }
2103
2104 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002105 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2107 "table.");
2108 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2109 HOSTAPD_LEVEL_WARNING,
2110 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002111 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 }
2113 }
2114
Hai Shalom021b0b52019-04-10 11:17:58 -07002115 if (hapd->iconf->rts_threshold >= -1 &&
2116 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2117 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2119 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002120 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121 }
2122
Hai Shalom021b0b52019-04-10 11:17:58 -07002123 if (hapd->iconf->fragm_threshold >= -1 &&
2124 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2125 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2127 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002128 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 }
2130
2131 prev_addr = hapd->own_addr;
2132
2133 for (j = 0; j < iface->num_bss; j++) {
2134 hapd = iface->bss[j];
2135 if (j)
2136 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002137 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002138 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002139 hapd = iface->bss[j];
2140 hostapd_bss_deinit_no_free(hapd);
2141 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002142 if (j == 0)
2143 break;
2144 j--;
2145 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002146 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002147 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002148 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 prev_addr = hapd->own_addr;
2150 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002151 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152
2153 hostapd_tx_queue_params(iface);
2154
2155 ap_list_init(iface);
2156
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002157 hostapd_set_acl(hapd);
2158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 if (hostapd_driver_commit(hapd) < 0) {
2160 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2161 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002162 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163 }
2164
Jouni Malinen87fd2792011-05-16 18:35:42 +03002165 /*
2166 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2167 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2168 * mode), the interface is up only after driver_commit, so initialize
2169 * WPS after driver_commit.
2170 */
2171 for (j = 0; j < iface->num_bss; j++) {
2172 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002173 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002174 }
2175
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002176 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2177 !res_dfs_offload) {
2178 /*
2179 * If freq is DFS, and DFS is offloaded to the driver, then wait
2180 * for CAC to complete.
2181 */
2182 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2183 return res_dfs_offload;
2184 }
2185
2186#ifdef NEED_AP_MLME
2187dfs_offload:
2188#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002189
2190#ifdef CONFIG_FST
2191 if (hapd->iconf->fst_cfg.group_id[0]) {
2192 struct fst_wpa_obj iface_obj;
2193
2194 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2195 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2196 &iface_obj, &hapd->iconf->fst_cfg);
2197 if (!iface->fst) {
2198 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2199 hapd->iconf->fst_cfg.group_id);
2200 goto fail;
2201 }
2202 }
2203#endif /* CONFIG_FST */
2204
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002205 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002206 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002207 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002208 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209 if (hapd->setup_complete_cb)
2210 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2211
Hai Shalom60840252021-02-19 19:02:11 -08002212#ifdef CONFIG_MESH
2213 if (delay_apply_cfg && !iface->mconf) {
2214 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2215 goto fail;
2216 }
2217#endif /* CONFIG_MESH */
2218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2220 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002221 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2222 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002224 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002225 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002228
2229fail:
2230 wpa_printf(MSG_ERROR, "Interface initialization failed");
2231 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2232 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002233#ifdef CONFIG_FST
2234 if (iface->fst) {
2235 fst_detach(iface->fst);
2236 iface->fst = NULL;
2237 }
2238#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002239
2240 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002241 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002242 } else if (hapd->setup_complete_cb) {
2243 /*
2244 * Calling hapd->setup_complete_cb directly may cause iface
2245 * deinitialization which may be accessed later by the caller.
2246 */
2247 eloop_register_timeout(0, 0,
2248 hostapd_interface_setup_failure_handler,
2249 iface, NULL);
2250 }
2251
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002252 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253}
2254
2255
2256/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002257 * hostapd_setup_interface_complete - Complete interface setup
2258 *
2259 * This function is called when previous steps in the interface setup has been
2260 * completed. This can also start operations, e.g., DFS, that will require
2261 * additional processing before interface is ready to be enabled. Such
2262 * operations will call this function from eloop callbacks when finished.
2263 */
2264int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2265{
2266 struct hapd_interfaces *interfaces = iface->interfaces;
2267 struct hostapd_data *hapd = iface->bss[0];
2268 unsigned int i;
2269 int not_ready_in_sync_ifaces = 0;
2270
2271 if (!iface->need_to_start_in_sync)
2272 return hostapd_setup_interface_complete_sync(iface, err);
2273
2274 if (err) {
2275 wpa_printf(MSG_ERROR, "Interface initialization failed");
2276 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2277 iface->need_to_start_in_sync = 0;
2278 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2279 if (interfaces && interfaces->terminate_on_error)
2280 eloop_terminate();
2281 return -1;
2282 }
2283
2284 if (iface->ready_to_start_in_sync) {
2285 /* Already in ready and waiting. should never happpen */
2286 return 0;
2287 }
2288
2289 for (i = 0; i < interfaces->count; i++) {
2290 if (interfaces->iface[i]->need_to_start_in_sync &&
2291 !interfaces->iface[i]->ready_to_start_in_sync)
2292 not_ready_in_sync_ifaces++;
2293 }
2294
2295 /*
2296 * Check if this is the last interface, if yes then start all the other
2297 * waiting interfaces. If not, add this interface to the waiting list.
2298 */
2299 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2300 /*
2301 * If this interface went through CAC, do not synchronize, just
2302 * start immediately.
2303 */
2304 iface->need_to_start_in_sync = 0;
2305 wpa_printf(MSG_INFO,
2306 "%s: Finished CAC - bypass sync and start interface",
2307 iface->bss[0]->conf->iface);
2308 return hostapd_setup_interface_complete_sync(iface, err);
2309 }
2310
2311 if (not_ready_in_sync_ifaces > 1) {
2312 /* need to wait as there are other interfaces still coming up */
2313 iface->ready_to_start_in_sync = 1;
2314 wpa_printf(MSG_INFO,
2315 "%s: Interface waiting to sync with other interfaces",
2316 iface->bss[0]->conf->iface);
2317 return 0;
2318 }
2319
2320 wpa_printf(MSG_INFO,
2321 "%s: Last interface to sync - starting all interfaces",
2322 iface->bss[0]->conf->iface);
2323 iface->need_to_start_in_sync = 0;
2324 hostapd_setup_interface_complete_sync(iface, err);
2325 for (i = 0; i < interfaces->count; i++) {
2326 if (interfaces->iface[i]->need_to_start_in_sync &&
2327 interfaces->iface[i]->ready_to_start_in_sync) {
2328 hostapd_setup_interface_complete_sync(
2329 interfaces->iface[i], 0);
2330 /* Only once the interfaces are sync started */
2331 interfaces->iface[i]->need_to_start_in_sync = 0;
2332 }
2333 }
2334
2335 return 0;
2336}
2337
2338
2339/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 * hostapd_setup_interface - Setup of an interface
2341 * @iface: Pointer to interface data.
2342 * Returns: 0 on success, -1 on failure
2343 *
2344 * Initializes the driver interface, validates the configuration,
2345 * and sets driver parameters based on the configuration.
2346 * Flushes old stations, sets the channel, encryption,
2347 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002348 *
2349 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2350 * or DFS operations, this function returns 0 before such operations have been
2351 * completed. The pending operations are registered into eloop and will be
2352 * completed from eloop callbacks. Those callbacks end up calling
2353 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002354 */
2355int hostapd_setup_interface(struct hostapd_iface *iface)
2356{
2357 int ret;
2358
Hai Shaloma20dcd72022-02-04 13:43:00 -08002359 if (!iface->conf)
2360 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 ret = setup_interface(iface);
2362 if (ret) {
2363 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002364 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365 return -1;
2366 }
2367
2368 return 0;
2369}
2370
2371
2372/**
2373 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2374 * @hapd_iface: Pointer to interface data
2375 * @conf: Pointer to per-interface configuration
2376 * @bss: Pointer to per-BSS configuration for this BSS
2377 * Returns: Pointer to allocated BSS data
2378 *
2379 * This function is used to allocate per-BSS data structure. This data will be
2380 * freed after hostapd_cleanup() is called for it during interface
2381 * deinitialization.
2382 */
2383struct hostapd_data *
2384hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2385 struct hostapd_config *conf,
2386 struct hostapd_bss_config *bss)
2387{
2388 struct hostapd_data *hapd;
2389
2390 hapd = os_zalloc(sizeof(*hapd));
2391 if (hapd == NULL)
2392 return NULL;
2393
2394 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2395 hapd->iconf = conf;
2396 hapd->conf = bss;
2397 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002398 if (conf)
2399 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002400 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002401 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002402 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002403 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002404#ifdef CONFIG_IEEE80211R_AP
2405 dl_list_init(&hapd->l2_queue);
2406 dl_list_init(&hapd->l2_oui_queue);
2407#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002408#ifdef CONFIG_SAE
2409 dl_list_init(&hapd->sae_commit_queue);
2410#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411
2412 return hapd;
2413}
2414
2415
Dmitry Shmidt54605472013-11-08 11:10:19 -08002416static void hostapd_bss_deinit(struct hostapd_data *hapd)
2417{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002418 if (!hapd)
2419 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002420 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002421 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002422 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002423 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002424#ifdef CONFIG_SQLITE
2425 if (hapd->rad_attr_db) {
2426 sqlite3_close(hapd->rad_attr_db);
2427 hapd->rad_attr_db = NULL;
2428 }
2429#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002430 hostapd_cleanup(hapd);
2431}
2432
2433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434void hostapd_interface_deinit(struct hostapd_iface *iface)
2435{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002436 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437
Dmitry Shmidt54605472013-11-08 11:10:19 -08002438 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 if (iface == NULL)
2440 return;
2441
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002442 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2443
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002444 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2445 iface->wait_channel_update = 0;
2446
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002447#ifdef CONFIG_FST
2448 if (iface->fst) {
2449 fst_detach(iface->fst);
2450 iface->fst = NULL;
2451 }
2452#endif /* CONFIG_FST */
2453
Hai Shalom021b0b52019-04-10 11:17:58 -07002454 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002455 if (!iface->bss)
2456 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002457 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002458 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002459
Roshan Pius3a1667e2018-07-03 15:17:14 -07002460#ifdef NEED_AP_MLME
2461 hostapd_stop_setup_timers(iface);
2462 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2463#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464}
2465
2466
2467void hostapd_interface_free(struct hostapd_iface *iface)
2468{
2469 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002470 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2471 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002472 if (!iface->bss)
2473 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002474 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2475 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002477 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002478 hostapd_cleanup_iface(iface);
2479}
2480
2481
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002482struct hostapd_iface * hostapd_alloc_iface(void)
2483{
2484 struct hostapd_iface *hapd_iface;
2485
2486 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2487 if (!hapd_iface)
2488 return NULL;
2489
2490 dl_list_init(&hapd_iface->sta_seen);
2491
2492 return hapd_iface;
2493}
2494
2495
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002496/**
2497 * hostapd_init - Allocate and initialize per-interface data
2498 * @config_file: Path to the configuration file
2499 * Returns: Pointer to the allocated interface data or %NULL on failure
2500 *
2501 * This function is used to allocate main data structures for per-interface
2502 * data. The allocated data buffer will be freed by calling
2503 * hostapd_cleanup_iface().
2504 */
2505struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2506 const char *config_file)
2507{
2508 struct hostapd_iface *hapd_iface = NULL;
2509 struct hostapd_config *conf = NULL;
2510 struct hostapd_data *hapd;
2511 size_t i;
2512
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002513 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002514 if (hapd_iface == NULL)
2515 goto fail;
2516
2517 hapd_iface->config_fname = os_strdup(config_file);
2518 if (hapd_iface->config_fname == NULL)
2519 goto fail;
2520
2521 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2522 if (conf == NULL)
2523 goto fail;
2524 hapd_iface->conf = conf;
2525
2526 hapd_iface->num_bss = conf->num_bss;
2527 hapd_iface->bss = os_calloc(conf->num_bss,
2528 sizeof(struct hostapd_data *));
2529 if (hapd_iface->bss == NULL)
2530 goto fail;
2531
2532 for (i = 0; i < conf->num_bss; i++) {
2533 hapd = hapd_iface->bss[i] =
2534 hostapd_alloc_bss_data(hapd_iface, conf,
2535 conf->bss[i]);
2536 if (hapd == NULL)
2537 goto fail;
2538 hapd->msg_ctx = hapd;
2539 }
2540
2541 return hapd_iface;
2542
2543fail:
2544 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2545 config_file);
2546 if (conf)
2547 hostapd_config_free(conf);
2548 if (hapd_iface) {
2549 os_free(hapd_iface->config_fname);
2550 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002551 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2552 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002553 os_free(hapd_iface);
2554 }
2555 return NULL;
2556}
2557
2558
2559static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2560{
2561 size_t i, j;
2562
2563 for (i = 0; i < interfaces->count; i++) {
2564 struct hostapd_iface *iface = interfaces->iface[i];
2565 for (j = 0; j < iface->num_bss; j++) {
2566 struct hostapd_data *hapd = iface->bss[j];
2567 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2568 return 1;
2569 }
2570 }
2571
2572 return 0;
2573}
2574
2575
2576/**
2577 * hostapd_interface_init_bss - Read configuration file and init BSS data
2578 *
2579 * This function is used to parse configuration file for a BSS. This BSS is
2580 * added to an existing interface sharing the same radio (if any) or a new
2581 * interface is created if this is the first interface on a radio. This
2582 * allocate memory for the BSS. No actual driver operations are started.
2583 *
2584 * This is similar to hostapd_interface_init(), but for a case where the
2585 * configuration is used to add a single BSS instead of all BSSes for a radio.
2586 */
2587struct hostapd_iface *
2588hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2589 const char *config_fname, int debug)
2590{
2591 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2592 struct hostapd_data *hapd;
2593 int k;
2594 size_t i, bss_idx;
2595
2596 if (!phy || !*phy)
2597 return NULL;
2598
2599 for (i = 0; i < interfaces->count; i++) {
2600 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2601 iface = interfaces->iface[i];
2602 break;
2603 }
2604 }
2605
2606 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2607 config_fname, phy, iface ? "" : " --> new PHY");
2608 if (iface) {
2609 struct hostapd_config *conf;
2610 struct hostapd_bss_config **tmp_conf;
2611 struct hostapd_data **tmp_bss;
2612 struct hostapd_bss_config *bss;
2613 const char *ifname;
2614
2615 /* Add new BSS to existing iface */
2616 conf = interfaces->config_read_cb(config_fname);
2617 if (conf == NULL)
2618 return NULL;
2619 if (conf->num_bss > 1) {
2620 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2621 hostapd_config_free(conf);
2622 return NULL;
2623 }
2624
2625 ifname = conf->bss[0]->iface;
2626 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2627 wpa_printf(MSG_ERROR,
2628 "Interface name %s already in use", ifname);
2629 hostapd_config_free(conf);
2630 return NULL;
2631 }
2632
2633 tmp_conf = os_realloc_array(
2634 iface->conf->bss, iface->conf->num_bss + 1,
2635 sizeof(struct hostapd_bss_config *));
2636 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2637 sizeof(struct hostapd_data *));
2638 if (tmp_bss)
2639 iface->bss = tmp_bss;
2640 if (tmp_conf) {
2641 iface->conf->bss = tmp_conf;
2642 iface->conf->last_bss = tmp_conf[0];
2643 }
2644 if (tmp_bss == NULL || tmp_conf == NULL) {
2645 hostapd_config_free(conf);
2646 return NULL;
2647 }
2648 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2649 iface->conf->num_bss++;
2650
2651 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2652 if (hapd == NULL) {
2653 iface->conf->num_bss--;
2654 hostapd_config_free(conf);
2655 return NULL;
2656 }
2657 iface->conf->last_bss = bss;
2658 iface->bss[iface->num_bss] = hapd;
2659 hapd->msg_ctx = hapd;
2660
2661 bss_idx = iface->num_bss++;
2662 conf->num_bss--;
2663 conf->bss[0] = NULL;
2664 hostapd_config_free(conf);
2665 } else {
2666 /* Add a new iface with the first BSS */
2667 new_iface = iface = hostapd_init(interfaces, config_fname);
2668 if (!iface)
2669 return NULL;
2670 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2671 iface->interfaces = interfaces;
2672 bss_idx = 0;
2673 }
2674
2675 for (k = 0; k < debug; k++) {
2676 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2677 iface->bss[bss_idx]->conf->logger_stdout_level--;
2678 }
2679
2680 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2681 !hostapd_drv_none(iface->bss[bss_idx])) {
2682 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2683 config_fname);
2684 if (new_iface)
2685 hostapd_interface_deinit_free(new_iface);
2686 return NULL;
2687 }
2688
2689 return iface;
2690}
2691
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002692
2693void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2694{
2695 const struct wpa_driver_ops *driver;
2696 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002697
2698 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002699 if (iface == NULL)
2700 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002701 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2702 __func__, (unsigned int) iface->num_bss,
2703 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002704 driver = iface->bss[0]->driver;
2705 drv_priv = iface->bss[0]->drv_priv;
2706 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002707 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2708 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002709 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002710 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002711 iface->bss[0]->drv_priv = NULL;
2712 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002713 hostapd_interface_free(iface);
2714}
2715
2716
Dmitry Shmidt15907092014-03-25 10:42:57 -07002717static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2718 void *drv_priv,
2719 struct hostapd_iface *hapd_iface)
2720{
2721 size_t j;
2722
2723 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2724 __func__, driver, drv_priv);
2725 if (driver && driver->hapd_deinit && drv_priv) {
2726 driver->hapd_deinit(drv_priv);
2727 for (j = 0; j < hapd_iface->num_bss; j++) {
2728 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2729 __func__, (int) j,
2730 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002731 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002732 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002733 hapd_iface->extended_capa = NULL;
2734 hapd_iface->extended_capa_mask = NULL;
2735 hapd_iface->extended_capa_len = 0;
2736 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002737 }
2738 }
2739}
2740
2741
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002742int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2743{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002744 size_t j;
2745
Hai Shalom60840252021-02-19 19:02:11 -08002746 if (!hapd_iface)
2747 return -1;
2748
2749 if (hapd_iface->enable_iface_cb)
2750 return hapd_iface->enable_iface_cb(hapd_iface);
2751
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002752 if (hapd_iface->bss[0]->drv_priv != NULL) {
2753 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002754 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002755 return -1;
2756 }
2757
2758 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002759 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002760
Dmitry Shmidt71757432014-06-02 13:50:35 -07002761 for (j = 0; j < hapd_iface->num_bss; j++)
2762 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002763 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2764 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2765 return -1;
2766 }
2767
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002768 if (hapd_iface->interfaces == NULL ||
2769 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002770 hapd_iface->interfaces->driver_init(hapd_iface))
2771 return -1;
2772
2773 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002774 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2775 hapd_iface->bss[0]->drv_priv,
2776 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002777 return -1;
2778 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002779
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002780 return 0;
2781}
2782
2783
2784int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2785{
2786 size_t j;
2787
2788 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002789 hapd_iface->conf->bss[0]->iface);
2790 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002791 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002792 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002793 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2794 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002795 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002796 hostapd_clear_old(hapd_iface);
2797 for (j = 0; j < hapd_iface->num_bss; j++)
2798 hostapd_reload_bss(hapd_iface->bss[j]);
2799
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002800 return 0;
2801}
2802
2803
2804int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2805{
2806 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002807 const struct wpa_driver_ops *driver;
2808 void *drv_priv;
2809
2810 if (hapd_iface == NULL)
2811 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002812
Hai Shalom60840252021-02-19 19:02:11 -08002813 if (hapd_iface->disable_iface_cb)
2814 return hapd_iface->disable_iface_cb(hapd_iface);
2815
Dmitry Shmidt71757432014-06-02 13:50:35 -07002816 if (hapd_iface->bss[0]->drv_priv == NULL) {
2817 wpa_printf(MSG_INFO, "Interface %s already disabled",
2818 hapd_iface->conf->bss[0]->iface);
2819 return -1;
2820 }
2821
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002822 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002823 driver = hapd_iface->bss[0]->driver;
2824 drv_priv = hapd_iface->bss[0]->drv_priv;
2825
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002826 hapd_iface->driver_ap_teardown =
2827 !!(hapd_iface->drv_flags &
2828 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2829
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002830#ifdef NEED_AP_MLME
2831 for (j = 0; j < hapd_iface->num_bss; j++)
2832 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2833#endif /* NEED_AP_MLME */
2834
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002835 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002836 for (j = 0; j < hapd_iface->num_bss; j++) {
2837 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002838 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002839 hostapd_free_hapd_data(hapd);
2840 }
2841
Dmitry Shmidt15907092014-03-25 10:42:57 -07002842 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002843
2844 /* From hostapd_cleanup_iface: These were initialized in
2845 * hostapd_setup_interface and hostapd_setup_interface_complete
2846 */
2847 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002848
Dmitry Shmidt56052862013-10-04 10:23:25 -07002849 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2850 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002851 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002852 return 0;
2853}
2854
2855
2856static struct hostapd_iface *
2857hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2858{
2859 struct hostapd_iface **iface, *hapd_iface;
2860
2861 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2862 sizeof(struct hostapd_iface *));
2863 if (iface == NULL)
2864 return NULL;
2865 interfaces->iface = iface;
2866 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002867 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002868 if (hapd_iface == NULL) {
2869 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2870 "the interface", __func__);
2871 return NULL;
2872 }
2873 interfaces->count++;
2874 hapd_iface->interfaces = interfaces;
2875
2876 return hapd_iface;
2877}
2878
2879
2880static struct hostapd_config *
2881hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002882 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002883{
2884 struct hostapd_bss_config *bss;
2885 struct hostapd_config *conf;
2886
2887 /* Allocates memory for bss and conf */
2888 conf = hostapd_config_defaults();
2889 if (conf == NULL) {
2890 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2891 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002892 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002893 }
2894
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002895 if (driver) {
2896 int j;
2897
2898 for (j = 0; wpa_drivers[j]; j++) {
2899 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2900 conf->driver = wpa_drivers[j];
2901 goto skip;
2902 }
2903 }
2904
2905 wpa_printf(MSG_ERROR,
2906 "Invalid/unknown driver '%s' - registering the default driver",
2907 driver);
2908 }
2909
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002910 conf->driver = wpa_drivers[0];
2911 if (conf->driver == NULL) {
2912 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2913 hostapd_config_free(conf);
2914 return NULL;
2915 }
2916
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002917skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002918 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002919
2920 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2921 bss->ctrl_interface = os_strdup(ctrl_iface);
2922 if (bss->ctrl_interface == NULL) {
2923 hostapd_config_free(conf);
2924 return NULL;
2925 }
2926
2927 /* Reading configuration file skipped, will be done in SET!
2928 * From reading the configuration till the end has to be done in
2929 * SET
2930 */
2931 return conf;
2932}
2933
2934
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002935static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2936 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002937{
2938 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002939 struct hostapd_data *hapd;
2940
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002941 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002942 sizeof(struct hostapd_data *));
2943 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002944 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002945
2946 for (i = 0; i < conf->num_bss; i++) {
2947 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002948 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002949 if (hapd == NULL) {
2950 while (i > 0) {
2951 i--;
2952 os_free(hapd_iface->bss[i]);
2953 hapd_iface->bss[i] = NULL;
2954 }
2955 os_free(hapd_iface->bss);
2956 hapd_iface->bss = NULL;
2957 return -1;
2958 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002959 hapd->msg_ctx = hapd;
2960 }
2961
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002962 hapd_iface->conf = conf;
2963 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002964
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002965 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002966}
2967
2968
2969int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2970{
2971 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002972 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2973 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002974 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002975 size_t i, j;
2976 const char *conf_file = NULL, *phy_name = NULL;
2977
2978 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2979 char *pos;
2980 phy_name = buf + 11;
2981 pos = os_strchr(phy_name, ':');
2982 if (!pos)
2983 return -1;
2984 *pos++ = '\0';
2985 conf_file = pos;
2986 if (!os_strlen(conf_file))
2987 return -1;
2988
2989 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2990 conf_file, 0);
2991 if (!hapd_iface)
2992 return -1;
2993 for (j = 0; j < interfaces->count; j++) {
2994 if (interfaces->iface[j] == hapd_iface)
2995 break;
2996 }
2997 if (j == interfaces->count) {
2998 struct hostapd_iface **tmp;
2999 tmp = os_realloc_array(interfaces->iface,
3000 interfaces->count + 1,
3001 sizeof(struct hostapd_iface *));
3002 if (!tmp) {
3003 hostapd_interface_deinit_free(hapd_iface);
3004 return -1;
3005 }
3006 interfaces->iface = tmp;
3007 interfaces->iface[interfaces->count++] = hapd_iface;
3008 new_iface = hapd_iface;
3009 }
3010
3011 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003012 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003013 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003014
3015 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003016 hostapd_deinit_driver(
3017 hapd_iface->bss[0]->driver,
3018 hapd_iface->bss[0]->drv_priv,
3019 hapd_iface);
3020 goto fail;
3021 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003022 } else {
3023 /* Assign new BSS with bss[0]'s driver info */
3024 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3025 hapd->driver = hapd_iface->bss[0]->driver;
3026 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3027 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3028 ETH_ALEN);
3029
3030 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003031 (hapd_iface->state == HAPD_IFACE_ENABLED &&
3032 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003033 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003034 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003035 hapd_iface->conf->num_bss--;
3036 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003037 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3038 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003039 hostapd_config_free_bss(hapd->conf);
3040 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003041 os_free(hapd);
3042 return -1;
3043 }
3044 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003045 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003046 return 0;
3047 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003048
3049 ptr = os_strchr(buf, ' ');
3050 if (ptr == NULL)
3051 return -1;
3052 *ptr++ = '\0';
3053
Dmitry Shmidt56052862013-10-04 10:23:25 -07003054 if (os_strncmp(ptr, "config=", 7) == 0)
3055 conf_file = ptr + 7;
3056
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003057 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003058 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003059 buf)) {
3060 wpa_printf(MSG_INFO, "Cannot add interface - it "
3061 "already exists");
3062 return -1;
3063 }
3064 }
3065
3066 hapd_iface = hostapd_iface_alloc(interfaces);
3067 if (hapd_iface == NULL) {
3068 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3069 "for interface", __func__);
3070 goto fail;
3071 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003072 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003073
Dmitry Shmidt56052862013-10-04 10:23:25 -07003074 if (conf_file && interfaces->config_read_cb) {
3075 conf = interfaces->config_read_cb(conf_file);
3076 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003077 os_strlcpy(conf->bss[0]->iface, buf,
3078 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003079 } else {
3080 char *driver = os_strchr(ptr, ' ');
3081
3082 if (driver)
3083 *driver++ = '\0';
3084 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3085 }
3086
Dmitry Shmidt56052862013-10-04 10:23:25 -07003087 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003088 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3089 "for configuration", __func__);
3090 goto fail;
3091 }
3092
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003093 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003094 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3095 "for hostapd", __func__);
3096 goto fail;
3097 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003098 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003099
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003100 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003101 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003102
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003103 wpa_printf(MSG_INFO, "Add interface '%s'",
3104 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003105
3106 return 0;
3107
3108fail:
3109 if (conf)
3110 hostapd_config_free(conf);
3111 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003112 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003113 for (i = 0; i < hapd_iface->num_bss; i++) {
3114 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003115 if (!hapd)
3116 continue;
3117 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003118 hapd_iface->interfaces->ctrl_iface_deinit)
3119 hapd_iface->interfaces->
3120 ctrl_iface_deinit(hapd);
3121 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3122 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003123 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003124 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003125 os_free(hapd);
3126 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003127 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003128 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003129 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003130 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003131 if (new_iface) {
3132 interfaces->count--;
3133 interfaces->iface[interfaces->count] = NULL;
3134 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003135 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003136 }
3137 return -1;
3138}
3139
3140
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003141static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3142{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003143 size_t i;
3144
Dmitry Shmidt54605472013-11-08 11:10:19 -08003145 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003146
Dmitry Shmidt54605472013-11-08 11:10:19 -08003147 /* Remove hostapd_data only if it has already been initialized */
3148 if (idx < iface->num_bss) {
3149 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003150
Dmitry Shmidt54605472013-11-08 11:10:19 -08003151 hostapd_bss_deinit(hapd);
3152 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3153 __func__, hapd, hapd->conf->iface);
3154 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003155 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003156 os_free(hapd);
3157
3158 iface->num_bss--;
3159
3160 for (i = idx; i < iface->num_bss; i++)
3161 iface->bss[i] = iface->bss[i + 1];
3162 } else {
3163 hostapd_config_free_bss(iface->conf->bss[idx]);
3164 iface->conf->bss[idx] = NULL;
3165 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003166
3167 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003168 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003169 iface->conf->bss[i] = iface->conf->bss[i + 1];
3170
3171 return 0;
3172}
3173
3174
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003175int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3176{
3177 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003178 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003179
3180 for (i = 0; i < interfaces->count; i++) {
3181 hapd_iface = interfaces->iface[i];
3182 if (hapd_iface == NULL)
3183 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003184 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003185 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003186 hapd_iface->driver_ap_teardown =
3187 !!(hapd_iface->drv_flags &
3188 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3189
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003190 hostapd_interface_deinit_free(hapd_iface);
3191 k = i;
3192 while (k < (interfaces->count - 1)) {
3193 interfaces->iface[k] =
3194 interfaces->iface[k + 1];
3195 k++;
3196 }
3197 interfaces->count--;
3198 return 0;
3199 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003200
3201 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003202 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3203 hapd_iface->driver_ap_teardown =
3204 !(hapd_iface->drv_flags &
3205 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003206 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003207 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003208 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003209 }
3210 return -1;
3211}
3212
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214/**
3215 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3216 * @hapd: Pointer to BSS data
3217 * @sta: Pointer to the associated STA data
3218 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3219 *
3220 * This function will be called whenever a station associates with the AP. It
3221 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3222 * from drv_callbacks.c based on driver events for drivers that take care of
3223 * management frames (IEEE 802.11 authentication and association) internally.
3224 */
3225void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3226 int reassoc)
3227{
3228 if (hapd->tkip_countermeasures) {
3229 hostapd_drv_sta_deauth(hapd, sta->addr,
3230 WLAN_REASON_MICHAEL_MIC_FAILURE);
3231 return;
3232 }
3233
3234 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003235 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003236 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238#ifdef CONFIG_P2P
3239 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3240 sta->no_p2p_set = 1;
3241 hapd->num_sta_no_p2p++;
3242 if (hapd->num_sta_no_p2p == 1)
3243 hostapd_p2p_non_p2p_sta_connected(hapd);
3244 }
3245#endif /* CONFIG_P2P */
3246
Hai Shalom81f62d82019-07-22 12:10:00 -07003247 airtime_policy_new_sta(hapd, sta);
3248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3250 * IEEE 802.1X/WPA code will start accounting after the station has
3251 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003252 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3253 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003254 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003256 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257
3258 /* Start IEEE 802.1X authentication process for new stations */
3259 ieee802_1x_new_station(hapd, sta);
3260 if (reassoc) {
3261 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003262 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3263 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3264 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003265 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3266 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3267 } else
3268 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003269
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003270 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3271 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3272 wpa_printf(MSG_DEBUG,
3273 "%s: %s: canceled wired ap_handle_timer timeout for "
3274 MACSTR,
3275 hapd->conf->iface, __func__,
3276 MAC2STR(sta->addr));
3277 }
3278 } else if (!(hapd->iface->drv_flags &
3279 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003280 wpa_printf(MSG_DEBUG,
3281 "%s: %s: reschedule ap_handle_timer timeout for "
3282 MACSTR " (%d seconds - ap_max_inactivity)",
3283 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003284 hapd->conf->ap_max_inactivity);
3285 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3286 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3287 ap_handle_timer, hapd, sta);
3288 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003289
3290#ifdef CONFIG_MACSEC
3291 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3292 hapd->conf->mka_psk_set)
3293 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3294 else
3295 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3296#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003298
3299
3300const char * hostapd_state_text(enum hostapd_iface_state s)
3301{
3302 switch (s) {
3303 case HAPD_IFACE_UNINITIALIZED:
3304 return "UNINITIALIZED";
3305 case HAPD_IFACE_DISABLED:
3306 return "DISABLED";
3307 case HAPD_IFACE_COUNTRY_UPDATE:
3308 return "COUNTRY_UPDATE";
3309 case HAPD_IFACE_ACS:
3310 return "ACS";
3311 case HAPD_IFACE_HT_SCAN:
3312 return "HT_SCAN";
3313 case HAPD_IFACE_DFS:
3314 return "DFS";
3315 case HAPD_IFACE_ENABLED:
3316 return "ENABLED";
3317 }
3318
3319 return "UNKNOWN";
3320}
3321
3322
3323void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3324{
3325 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003326 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3327 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003328 iface->state = s;
3329}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003330
3331
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003332int hostapd_csa_in_progress(struct hostapd_iface *iface)
3333{
3334 unsigned int i;
3335
3336 for (i = 0; i < iface->num_bss; i++)
3337 if (iface->bss[i]->csa_in_progress)
3338 return 1;
3339 return 0;
3340}
3341
3342
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003343#ifdef NEED_AP_MLME
3344
3345static void free_beacon_data(struct beacon_data *beacon)
3346{
3347 os_free(beacon->head);
3348 beacon->head = NULL;
3349 os_free(beacon->tail);
3350 beacon->tail = NULL;
3351 os_free(beacon->probe_resp);
3352 beacon->probe_resp = NULL;
3353 os_free(beacon->beacon_ies);
3354 beacon->beacon_ies = NULL;
3355 os_free(beacon->proberesp_ies);
3356 beacon->proberesp_ies = NULL;
3357 os_free(beacon->assocresp_ies);
3358 beacon->assocresp_ies = NULL;
3359}
3360
3361
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003362static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003363 struct beacon_data *beacon)
3364{
3365 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3366 struct wpa_driver_ap_params params;
3367 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003368
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003369 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003370 ret = ieee802_11_build_ap_params(hapd, &params);
3371 if (ret < 0)
3372 return ret;
3373
3374 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3375 &proberesp_extra,
3376 &assocresp_extra);
3377 if (ret)
3378 goto free_ap_params;
3379
3380 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003381 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003382 if (!beacon->head)
3383 goto free_ap_extra_ies;
3384
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003385 beacon->head_len = params.head_len;
3386
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003387 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003388 if (!beacon->tail)
3389 goto free_beacon;
3390
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003391 beacon->tail_len = params.tail_len;
3392
3393 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003394 beacon->probe_resp = os_memdup(params.proberesp,
3395 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003396 if (!beacon->probe_resp)
3397 goto free_beacon;
3398
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003399 beacon->probe_resp_len = params.proberesp_len;
3400 }
3401
3402 /* copy the extra ies */
3403 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003404 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3405 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003406 if (!beacon->beacon_ies)
3407 goto free_beacon;
3408
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003409 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3410 }
3411
3412 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003413 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3414 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003415 if (!beacon->proberesp_ies)
3416 goto free_beacon;
3417
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003418 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3419 }
3420
3421 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003422 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3423 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003424 if (!beacon->assocresp_ies)
3425 goto free_beacon;
3426
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003427 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3428 }
3429
3430 ret = 0;
3431free_beacon:
3432 /* if the function fails, the caller should not free beacon data */
3433 if (ret)
3434 free_beacon_data(beacon);
3435
3436free_ap_extra_ies:
3437 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3438 assocresp_extra);
3439free_ap_params:
3440 ieee802_11_free_ap_params(&params);
3441 return ret;
3442}
3443
3444
3445/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003446 * TODO: This flow currently supports only changing channel and width within
3447 * the same hw_mode. Any other changes to MAC parameters or provided settings
3448 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003449 */
3450static int hostapd_change_config_freq(struct hostapd_data *hapd,
3451 struct hostapd_config *conf,
3452 struct hostapd_freq_params *params,
3453 struct hostapd_freq_params *old_params)
3454{
3455 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003456 u8 seg0, seg1;
3457 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003458
3459 if (!params->channel) {
3460 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003461 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003462 }
3463
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003464 channel = params->channel;
3465 if (!channel)
3466 return -1;
3467
Hai Shalom81f62d82019-07-22 12:10:00 -07003468 mode = hapd->iface->current_mode;
3469
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003470 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003471 if (old_params &&
3472 hostapd_set_freq_params(old_params, conf->hw_mode,
3473 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003474 conf->channel, conf->enable_edmg,
3475 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003476 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07003477 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003478 hostapd_get_oper_chwidth(conf),
3479 hostapd_get_oper_centr_freq_seg0_idx(conf),
3480 hostapd_get_oper_centr_freq_seg1_idx(conf),
3481 conf->vht_capab,
3482 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07003483 NULL,
3484 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Hai Shalom81f62d82019-07-22 12:10:00 -07003485 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003486 return -1;
3487
3488 switch (params->bandwidth) {
3489 case 0:
3490 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003491 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3492 break;
3493 case 40:
3494 case 80:
3495 case 160:
3496 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3497 break;
3498 default:
3499 return -1;
3500 }
3501
3502 switch (params->bandwidth) {
3503 case 0:
3504 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003505 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003506 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003507 break;
3508 case 80:
3509 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003510 hostapd_set_oper_chwidth(conf,
3511 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003512 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003513 hostapd_set_oper_chwidth(conf,
3514 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003515 break;
3516 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003517 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
3518 break;
3519 case 320:
3520 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003521 break;
3522 default:
3523 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003524 }
3525
3526 conf->channel = channel;
3527 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003528 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003529 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003530 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003531 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003532 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003533 &seg1);
3534 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3535 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003536
3537 /* TODO: maybe call here hostapd_config_check here? */
3538
3539 return 0;
3540}
3541
3542
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003543static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003544 struct csa_settings *settings)
3545{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003546 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003547 struct hostapd_freq_params old_freq;
3548 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003549 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003550
3551 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003552 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003553 return -1;
3554
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003555 switch (settings->freq_params.bandwidth) {
3556 case 80:
3557 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003558 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003559 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003560 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003561 break;
3562 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003563 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003564 break;
3565 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00003566 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003567 break;
3568 }
3569
3570 if (ieee80211_freq_to_channel_ext(
3571 settings->freq_params.freq,
3572 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003573 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003574 &hapd->iface->cs_oper_class,
3575 &chan) == NUM_HOSTAPD_MODES) {
3576 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07003577 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d, eht_enabled=%d)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003578 settings->freq_params.freq,
3579 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003580 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07003581 settings->freq_params.he_enabled,
3582 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003583 return -1;
3584 }
3585
3586 settings->freq_params.channel = chan;
3587
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003588 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3589 &settings->freq_params,
3590 &old_freq);
3591 if (ret)
3592 return ret;
3593
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003594 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003595
3596 /* change back the configuration */
3597 hostapd_change_config_freq(iface->bss[0], iface->conf,
3598 &old_freq, NULL);
3599
3600 if (ret)
3601 return ret;
3602
3603 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003604 hapd->cs_freq_params = settings->freq_params;
3605 hapd->cs_count = settings->cs_count;
3606 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003607
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003608 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003609 if (ret) {
3610 free_beacon_data(&settings->beacon_after);
3611 return ret;
3612 }
3613
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003614 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3615 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3616 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3617 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003618
3619 return 0;
3620}
3621
3622
3623void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3624{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003625 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3626 hapd->cs_count = 0;
3627 hapd->cs_block_tx = 0;
3628 hapd->cs_c_off_beacon = 0;
3629 hapd->cs_c_off_proberesp = 0;
3630 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003631 hapd->cs_c_off_ecsa_beacon = 0;
3632 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003633}
3634
3635
Hai Shalom60840252021-02-19 19:02:11 -08003636void hostapd_chan_switch_config(struct hostapd_data *hapd,
3637 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003638{
Sunil Ravia04bd252022-05-02 22:54:18 -07003639 if (freq_params->eht_enabled)
3640 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
3641 else
3642 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
3643
Hai Shalom60840252021-02-19 19:02:11 -08003644 if (freq_params->he_enabled)
3645 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
3646 else
3647 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
3648
3649 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003650 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3651 else
3652 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3653
3654 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08003655 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07003656 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
3657 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08003658 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003659 hapd->iconf->ch_switch_vht_config);
3660}
3661
3662
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003663int hostapd_switch_channel(struct hostapd_data *hapd,
3664 struct csa_settings *settings)
3665{
3666 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003667
3668 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3669 wpa_printf(MSG_INFO, "CSA is not supported");
3670 return -1;
3671 }
3672
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003673 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003674 if (ret)
3675 return ret;
3676
3677 ret = hostapd_drv_switch_channel(hapd, settings);
3678 free_beacon_data(&settings->beacon_csa);
3679 free_beacon_data(&settings->beacon_after);
3680
3681 if (ret) {
3682 /* if we failed, clean cs parameters */
3683 hostapd_cleanup_cs_params(hapd);
3684 return ret;
3685 }
3686
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003687 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003688 return 0;
3689}
3690
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003691
3692void
3693hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3694 const struct hostapd_freq_params *freq_params)
3695{
Sunil8cd6f4d2022-06-28 18:40:46 +00003696 int seg0_idx = 0, seg1_idx = 0;
3697 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003698
3699 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3700
3701 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003702 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003703 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003704 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003705
3706 switch (freq_params->bandwidth) {
3707 case 0:
3708 case 20:
3709 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003710 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003711 break;
3712 case 80:
3713 if (freq_params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003714 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003715 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003716 bw = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003717 break;
3718 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003719 bw = CONF_OPER_CHWIDTH_160MHZ;
3720 break;
3721 case 320:
3722 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003723 break;
3724 default:
3725 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3726 freq_params->bandwidth);
3727 break;
3728 }
3729
3730 iface->freq = freq_params->freq;
3731 iface->conf->channel = freq_params->channel;
3732 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003733 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3734 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3735 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003736 iface->conf->ieee80211n = freq_params->ht_enabled;
3737 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003738 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07003739 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003740
3741 /*
3742 * cs_params must not be cleared earlier because the freq_params
3743 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003744 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003745 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003746 hostapd_disable_iface(iface);
3747 hostapd_enable_iface(iface);
3748}
3749
Sunil Ravia04bd252022-05-02 22:54:18 -07003750
3751#ifdef CONFIG_IEEE80211AX
3752
3753void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
3754{
3755 hapd->cca_count = 0;
3756 hapd->cca_color = 0;
3757 hapd->cca_c_off_beacon = 0;
3758 hapd->cca_c_off_proberesp = 0;
3759 hapd->cca_in_progress = false;
3760}
3761
3762
3763static int hostapd_fill_cca_settings(struct hostapd_data *hapd,
3764 struct cca_settings *settings)
3765{
3766 struct hostapd_iface *iface = hapd->iface;
3767 u8 old_color;
3768 int ret;
3769
3770 if (!iface || iface->conf->he_op.he_bss_color_disabled)
3771 return -1;
3772
3773 old_color = iface->conf->he_op.he_bss_color;
3774 iface->conf->he_op.he_bss_color = hapd->cca_color;
3775 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
3776 if (ret)
3777 return ret;
3778
3779 iface->conf->he_op.he_bss_color = old_color;
3780
3781 settings->cca_count = hapd->cca_count;
3782 settings->cca_color = hapd->cca_color,
3783 hapd->cca_in_progress = true;
3784
3785 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
3786 if (ret) {
3787 free_beacon_data(&settings->beacon_after);
3788 return ret;
3789 }
3790
3791 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
3792 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
3793
3794 return 0;
3795}
3796
3797
3798static void hostapd_switch_color_timeout_handler(void *eloop_data,
3799 void *user_ctx)
3800{
3801 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
3802 os_time_t delta_t;
3803 unsigned int b;
3804 int i, r;
3805
3806 /* CCA can be triggered once the handler constantly receives
3807 * color collision events to for at least
3808 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
3809 delta_t = hapd->last_color_collision.sec -
3810 hapd->first_color_collision.sec;
3811 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
3812 return;
3813
3814 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
3815 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00003816 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07003817 break;
3818
3819 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
3820 }
3821
3822 if (i == HE_OPERATION_BSS_COLOR_MAX) {
3823 /* There are no free colors so turn BSS coloring off */
3824 wpa_printf(MSG_INFO,
3825 "No free colors left, turning off BSS coloring");
3826 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
3827 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
3828 for (b = 0; b < hapd->iface->num_bss; b++)
3829 ieee802_11_set_beacon(hapd->iface->bss[b]);
3830 return;
3831 }
3832
3833 for (b = 0; b < hapd->iface->num_bss; b++) {
3834 struct hostapd_data *bss = hapd->iface->bss[b];
3835 struct cca_settings settings;
3836 int ret;
3837
3838 hostapd_cleanup_cca_params(bss);
3839 bss->cca_color = r;
3840 bss->cca_count = 10;
3841
3842 if (hostapd_fill_cca_settings(bss, &settings)) {
3843 hostapd_cleanup_cca_params(bss);
3844 continue;
3845 }
3846
3847 ret = hostapd_drv_switch_color(bss, &settings);
3848 if (ret)
3849 hostapd_cleanup_cca_params(bss);
3850
3851 free_beacon_data(&settings.beacon_cca);
3852 free_beacon_data(&settings.beacon_after);
3853 }
3854}
3855
3856
3857void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
3858{
3859 struct os_reltime now;
3860
3861 if (hapd->cca_in_progress)
3862 return;
3863
3864 if (os_get_reltime(&now))
3865 return;
3866
3867 hapd->color_collision_bitmap = bitmap;
3868 hapd->last_color_collision = now;
3869
3870 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
3871 hapd, NULL))
3872 return;
3873
3874 hapd->first_color_collision = now;
3875 /* 10 s window as margin for persistent color collision reporting */
3876 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
3877 hostapd_switch_color_timeout_handler,
3878 hapd, NULL);
3879}
3880
3881#endif /* CONFIG_IEEE80211AX */
3882
Dmitry Shmidte4663042016-04-04 10:07:49 -07003883#endif /* NEED_AP_MLME */
3884
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003885
3886struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3887 const char *ifname)
3888{
3889 size_t i, j;
3890
3891 for (i = 0; i < interfaces->count; i++) {
3892 struct hostapd_iface *iface = interfaces->iface[i];
3893
3894 for (j = 0; j < iface->num_bss; j++) {
3895 struct hostapd_data *hapd = iface->bss[j];
3896
3897 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3898 return hapd;
3899 }
3900 }
3901
3902 return NULL;
3903}
3904
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003905
3906void hostapd_periodic_iface(struct hostapd_iface *iface)
3907{
3908 size_t i;
3909
3910 ap_list_timer(iface);
3911
3912 for (i = 0; i < iface->num_bss; i++) {
3913 struct hostapd_data *hapd = iface->bss[i];
3914
3915 if (!hapd->started)
3916 continue;
3917
3918#ifndef CONFIG_NO_RADIUS
3919 hostapd_acl_expire(hapd);
3920#endif /* CONFIG_NO_RADIUS */
3921 }
3922}
Hai Shalom899fcc72020-10-19 14:38:18 -07003923
3924
3925#ifdef CONFIG_OCV
3926void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
3927{
3928 struct hostapd_data *hapd = eloop_ctx;
3929 struct sta_info *sta;
3930
3931 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
3932
3933 for (sta = hapd->sta_list; sta; sta = sta->next) {
3934 if (!sta->post_csa_sa_query)
3935 continue;
3936
3937 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
3938 " did not start SA Query after CSA - disconnect",
3939 MAC2STR(sta->addr));
3940 ap_sta_disconnect(hapd, sta, sta->addr,
3941 WLAN_REASON_PREV_AUTH_NOT_VALID);
3942 }
3943}
3944#endif /* CONFIG_OCV */