blob: 3681df6a7c03ef881d2d6ccfd0f6eb99fdc992f1 [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 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001241 conf->ssid.short_ssid = ieee80211_crc32(conf->ssid.ssid,
1242 conf->ssid.ssid_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001245 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001246 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001247 conf->iface, MAC2STR(hapd->own_addr),
1248 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 }
1250
1251 if (hostapd_setup_wpa_psk(conf)) {
1252 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1253 return -1;
1254 }
1255
1256 /* Set SSID for the kernel driver (to be used in beacon and probe
1257 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001258 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 conf->ssid.ssid_len)) {
1260 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1261 return -1;
1262 }
1263
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001264 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 conf->radius->msg_dumps = 1;
1266#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001267
1268#ifdef CONFIG_SQLITE
1269 if (conf->radius_req_attr_sqlite) {
1270 if (sqlite3_open(conf->radius_req_attr_sqlite,
1271 &hapd->rad_attr_db)) {
1272 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1273 conf->radius_req_attr_sqlite);
1274 return -1;
1275 }
1276
1277 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1278 conf->radius_req_attr_sqlite);
1279 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1280 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1281 return -1;
1282 }
1283#endif /* CONFIG_SQLITE */
1284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 hapd->radius = radius_client_init(hapd, conf->radius);
1286 if (hapd->radius == NULL) {
1287 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1288 return -1;
1289 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001290
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001291 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001292 struct radius_das_conf das_conf;
1293 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001294 das_conf.port = conf->radius_das_port;
1295 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001296 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001297 conf->radius_das_shared_secret_len;
1298 das_conf.client_addr = &conf->radius_das_client_addr;
1299 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001300 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001301 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001302 das_conf.require_message_authenticator =
1303 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001304 das_conf.ctx = hapd;
1305 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001306 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001307 hapd->radius_das = radius_das_init(&das_conf);
1308 if (hapd->radius_das == NULL) {
1309 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1310 "failed.");
1311 return -1;
1312 }
1313 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314#endif /* CONFIG_NO_RADIUS */
1315
1316 if (hostapd_acl_init(hapd)) {
1317 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1318 return -1;
1319 }
1320 if (hostapd_init_wps(hapd, conf))
1321 return -1;
1322
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001323#ifdef CONFIG_DPP
1324 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1325 if (!hapd->gas)
1326 return -1;
1327 if (hostapd_dpp_init(hapd))
1328 return -1;
1329#endif /* CONFIG_DPP */
1330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 if (authsrv_init(hapd) < 0)
1332 return -1;
1333
1334 if (ieee802_1x_init(hapd)) {
1335 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1336 return -1;
1337 }
1338
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001339 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 return -1;
1341
1342 if (accounting_init(hapd)) {
1343 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1344 return -1;
1345 }
1346
Dmitry Shmidt04949592012-07-19 12:16:46 -07001347#ifdef CONFIG_INTERWORKING
1348 if (gas_serv_init(hapd)) {
1349 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1350 return -1;
1351 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001352
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001353 if (conf->qos_map_set_len &&
1354 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1355 conf->qos_map_set_len)) {
1356 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 return -1;
1358 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001359#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001361 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1362 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1363 return -1;
1364 }
1365
1366 if (conf->proxy_arp) {
1367 if (x_snoop_init(hapd)) {
1368 wpa_printf(MSG_ERROR,
1369 "Generic snooping infrastructure initialization failed");
1370 return -1;
1371 }
1372
1373 if (dhcp_snoop_init(hapd)) {
1374 wpa_printf(MSG_ERROR,
1375 "DHCP snooping initialization failed");
1376 return -1;
1377 }
1378
1379 if (ndisc_snoop_init(hapd)) {
1380 wpa_printf(MSG_ERROR,
1381 "Neighbor Discovery snooping initialization failed");
1382 return -1;
1383 }
1384 }
1385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1387 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1388 return -1;
1389 }
1390
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001391 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001392 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001394 if (flush_old_stations && !conf->start_disabled &&
1395 conf->broadcast_deauth) {
1396 u8 addr[ETH_ALEN];
1397
1398 /* Should any previously associated STA not have noticed that
1399 * the AP had stopped and restarted, send one more
1400 * deauthentication notification now that the AP is ready to
1401 * operate. */
1402 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1403 "Deauthenticate all stations at BSS start");
1404 os_memset(addr, 0xff, ETH_ALEN);
1405 hostapd_drv_sta_deauth(hapd, addr,
1406 WLAN_REASON_PREV_AUTH_NOT_VALID);
1407 }
1408
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001409 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1410 return -1;
1411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 if (hapd->driver && hapd->driver->set_operstate)
1413 hapd->driver->set_operstate(hapd->drv_priv, 1);
1414
1415 return 0;
1416}
1417
1418
1419static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1420{
1421 struct hostapd_data *hapd = iface->bss[0];
1422 int i;
1423 struct hostapd_tx_queue_params *p;
1424
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001425#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001426 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001427 return;
1428#endif /* CONFIG_MESH */
1429
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 for (i = 0; i < NUM_TX_QUEUES; i++) {
1431 p = &iface->conf->tx_queue[i];
1432
1433 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1434 p->cwmax, p->burst)) {
1435 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1436 "parameters for queue %d.", i);
1437 /* Continue anyway */
1438 }
1439 }
1440}
1441
1442
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001443static int hostapd_set_acl_list(struct hostapd_data *hapd,
1444 struct mac_acl_entry *mac_acl,
1445 int n_entries, u8 accept_acl)
1446{
1447 struct hostapd_acl_params *acl_params;
1448 int i, err;
1449
1450 acl_params = os_zalloc(sizeof(*acl_params) +
1451 (n_entries * sizeof(acl_params->mac_acl[0])));
1452 if (!acl_params)
1453 return -ENOMEM;
1454
1455 for (i = 0; i < n_entries; i++)
1456 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1457 ETH_ALEN);
1458
1459 acl_params->acl_policy = accept_acl;
1460 acl_params->num_mac_acl = n_entries;
1461
1462 err = hostapd_drv_set_acl(hapd, acl_params);
1463
1464 os_free(acl_params);
1465
1466 return err;
1467}
1468
1469
Sunil Ravia04bd252022-05-02 22:54:18 -07001470int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001471{
1472 struct hostapd_config *conf = hapd->iconf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001473 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001474 u8 accept_acl;
1475
1476 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001477 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001478
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001479 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001480 accept_acl = 1;
1481 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1482 conf->bss[0]->num_accept_mac,
1483 accept_acl);
1484 if (err) {
1485 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001486 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001487 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001488 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001489 accept_acl = 0;
1490 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1491 conf->bss[0]->num_deny_mac,
1492 accept_acl);
1493 if (err) {
1494 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001495 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001496 }
1497 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001498 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001499}
1500
1501
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001502static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1503{
1504 if (!hapd->iface->interfaces ||
1505 !hapd->iface->interfaces->ctrl_iface_init)
1506 return 0;
1507
1508 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1509 wpa_printf(MSG_ERROR,
1510 "Failed to setup control interface for %s",
1511 hapd->conf->iface);
1512 return -1;
1513 }
1514
1515 return 0;
1516}
1517
1518
1519static int start_ctrl_iface(struct hostapd_iface *iface)
1520{
1521 size_t i;
1522
1523 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1524 return 0;
1525
1526 for (i = 0; i < iface->num_bss; i++) {
1527 struct hostapd_data *hapd = iface->bss[i];
1528 if (iface->interfaces->ctrl_iface_init(hapd)) {
1529 wpa_printf(MSG_ERROR,
1530 "Failed to setup control interface for %s",
1531 hapd->conf->iface);
1532 return -1;
1533 }
1534 }
1535
1536 return 0;
1537}
1538
1539
1540static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1541{
1542 struct hostapd_iface *iface = eloop_ctx;
1543
1544 if (!iface->wait_channel_update) {
1545 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1546 return;
1547 }
1548
1549 /*
1550 * It is possible that the existing channel list is acceptable, so try
1551 * to proceed.
1552 */
1553 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1554 setup_interface2(iface);
1555}
1556
1557
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001558void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001559{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001560 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001561 return;
1562
1563 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1564 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1565 setup_interface2(iface);
1566}
1567
1568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569static int setup_interface(struct hostapd_iface *iface)
1570{
1571 struct hostapd_data *hapd = iface->bss[0];
1572 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001573
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001574 /*
1575 * It is possible that setup_interface() is called after the interface
1576 * was disabled etc., in which case driver_ap_teardown is possibly set
1577 * to 1. Clear it here so any other key/station deletion, which is not
1578 * part of a teardown flow, would also call the relevant driver
1579 * callbacks.
1580 */
1581 iface->driver_ap_teardown = 0;
1582
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001583 if (!iface->phy[0]) {
1584 const char *phy = hostapd_drv_get_radio_name(hapd);
1585 if (phy) {
1586 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1587 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1588 }
1589 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590
1591 /*
1592 * Make sure that all BSSes get configured with a pointer to the same
1593 * driver interface.
1594 */
1595 for (i = 1; i < iface->num_bss; i++) {
1596 iface->bss[i]->driver = hapd->driver;
1597 iface->bss[i]->drv_priv = hapd->drv_priv;
1598 }
1599
1600 if (hostapd_validate_bssid_configuration(iface))
1601 return -1;
1602
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001603 /*
1604 * Initialize control interfaces early to allow external monitoring of
1605 * channel setup operations that may take considerable amount of time
1606 * especially for DFS cases.
1607 */
1608 if (start_ctrl_iface(iface))
1609 return -1;
1610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001612 char country[4], previous_country[4];
1613
1614 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1615 if (hostapd_get_country(hapd, previous_country) < 0)
1616 previous_country[0] = '\0';
1617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618 os_memcpy(country, hapd->iconf->country, 3);
1619 country[3] = '\0';
1620 if (hostapd_set_country(hapd, country) < 0) {
1621 wpa_printf(MSG_ERROR, "Failed to set country code");
1622 return -1;
1623 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001624
1625 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1626 previous_country, country);
1627
1628 if (os_strncmp(previous_country, country, 2) != 0) {
1629 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1630 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001631 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001632 channel_list_update_timeout,
1633 iface, NULL);
1634 return 0;
1635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 }
1637
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001638 return setup_interface2(iface);
1639}
1640
1641
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001642static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1643{
1644 int freq, i, j;
1645
1646 if (!iface->conf->channel)
1647 return 0;
1648 if (iface->conf->op_class) {
1649 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1650 iface->conf->channel);
1651 if (freq < 0) {
1652 wpa_printf(MSG_INFO,
1653 "Could not convert op_class %u channel %u to operating frequency",
1654 iface->conf->op_class, iface->conf->channel);
1655 return -1;
1656 }
1657 iface->freq = freq;
1658 return 0;
1659 }
1660
1661 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1662 * op_class parameter. Select a matching channel from the configured
1663 * mode using the channel parameter for these cases.
1664 */
1665 for (j = 0; j < iface->num_hw_features; j++) {
1666 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1667
1668 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1669 iface->conf->hw_mode != mode->mode)
1670 continue;
1671 for (i = 0; i < mode->num_channels; i++) {
1672 struct hostapd_channel_data *chan = &mode->channels[i];
1673
1674 if (chan->chan == iface->conf->channel &&
1675 !is_6ghz_freq(chan->freq)) {
1676 iface->freq = chan->freq;
1677 return 0;
1678 }
1679 }
1680 }
1681
1682 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1683 return -1;
1684}
1685
1686
Hai Shaloma20dcd72022-02-04 13:43:00 -08001687static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
1688{
1689 int bw, seg0;
1690
1691 if (!is_6ghz_op_class(iface->conf->op_class))
1692 return;
1693
1694 seg0 = hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
1695 bw = center_idx_to_bw_6ghz(seg0);
1696 /* Assign the secondary channel if absent in config for
1697 * bandwidths > 20 MHz */
1698 if (bw > 20 && !iface->conf->secondary_channel) {
1699 if (((iface->conf->channel - 1) / 4) % 2)
1700 iface->conf->secondary_channel = -1;
1701 else
1702 iface->conf->secondary_channel = 1;
1703 }
1704}
1705
1706
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001707static int setup_interface2(struct hostapd_iface *iface)
1708{
1709 iface->wait_channel_update = 0;
1710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 if (hostapd_get_hw_features(iface)) {
1712 /* Not all drivers support this yet, so continue without hw
1713 * feature data. */
1714 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001715 int ret;
1716
1717 ret = configured_fixed_chan_to_freq(iface);
1718 if (ret < 0)
1719 goto fail;
1720
1721 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001722 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001723
1724 ch_width = op_class_to_ch_width(iface->conf->op_class);
1725 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001726 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001727 }
1728
1729 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 if (ret < 0) {
1731 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1732 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001733 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001735 if (ret == 1) {
1736 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1737 return 0;
1738 }
Hai Shalomc3565922019-10-28 11:58:20 -07001739 ret = hostapd_check_edmg_capab(iface);
1740 if (ret < 0)
1741 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08001742 ret = hostapd_check_he_6ghz_capab(iface);
1743 if (ret < 0)
1744 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 ret = hostapd_check_ht_capab(iface);
1746 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001747 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 if (ret == 1) {
1749 wpa_printf(MSG_DEBUG, "Interface initialization will "
1750 "be completed in a callback");
1751 return 0;
1752 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001753
1754 if (iface->conf->ieee80211h)
1755 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 }
1757 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001758
1759fail:
1760 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1761 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1762 if (iface->interfaces && iface->interfaces->terminate_on_error)
1763 eloop_terminate();
1764 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765}
1766
1767
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001768#ifdef CONFIG_FST
1769
1770static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1771{
1772 struct hostapd_data *hapd = ctx;
1773
1774 return hapd->own_addr;
1775}
1776
1777
1778static void fst_hostapd_get_channel_info_cb(void *ctx,
1779 enum hostapd_hw_mode *hw_mode,
1780 u8 *channel)
1781{
1782 struct hostapd_data *hapd = ctx;
1783
1784 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1785}
1786
1787
Sunil8cd6f4d2022-06-28 18:40:46 +00001788static int fst_hostapd_get_hw_modes_cb(void *ctx,
1789 struct hostapd_hw_modes **modes)
1790{
1791 struct hostapd_data *hapd = ctx;
1792
1793 *modes = hapd->iface->hw_features;
1794 return hapd->iface->num_hw_features;
1795}
1796
1797
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001798static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1799{
1800 struct hostapd_data *hapd = ctx;
1801
1802 if (hapd->iface->fst_ies != fst_ies) {
1803 hapd->iface->fst_ies = fst_ies;
1804 if (ieee802_11_set_beacon(hapd))
1805 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1806 }
1807}
1808
1809
1810static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1811 struct wpabuf *buf)
1812{
1813 struct hostapd_data *hapd = ctx;
1814
1815 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1816 wpabuf_head(buf), wpabuf_len(buf));
1817}
1818
1819
1820static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1821{
1822 struct hostapd_data *hapd = ctx;
1823 struct sta_info *sta = ap_get_sta(hapd, addr);
1824
1825 return sta ? sta->mb_ies : NULL;
1826}
1827
1828
1829static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1830 const u8 *buf, size_t size)
1831{
1832 struct hostapd_data *hapd = ctx;
1833 struct sta_info *sta = ap_get_sta(hapd, addr);
1834
1835 if (sta) {
1836 struct mb_ies_info info;
1837
1838 if (!mb_ies_info_by_ies(&info, buf, size)) {
1839 wpabuf_free(sta->mb_ies);
1840 sta->mb_ies = mb_ies_by_info(&info);
1841 }
1842 }
1843}
1844
1845
1846static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001847 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001848{
1849 struct sta_info *s = (struct sta_info *) *get_ctx;
1850
1851 if (mb_only) {
1852 for (; s && !s->mb_ies; s = s->next)
1853 ;
1854 }
1855
1856 if (s) {
1857 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1858
1859 return s->addr;
1860 }
1861
1862 *get_ctx = NULL;
1863 return NULL;
1864}
1865
1866
1867static const u8 * fst_hostapd_get_peer_first(void *ctx,
1868 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001869 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001870{
1871 struct hostapd_data *hapd = ctx;
1872
1873 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1874
1875 return fst_hostapd_get_sta(get_ctx, mb_only);
1876}
1877
1878
1879static const u8 * fst_hostapd_get_peer_next(void *ctx,
1880 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001881 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001882{
1883 return fst_hostapd_get_sta(get_ctx, mb_only);
1884}
1885
1886
1887void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1888 struct fst_wpa_obj *iface_obj)
1889{
Sunil8cd6f4d2022-06-28 18:40:46 +00001890 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001891 iface_obj->ctx = hapd;
1892 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1893 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00001894 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001895 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1896 iface_obj->send_action = fst_hostapd_send_action_cb;
1897 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1898 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1899 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1900 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1901}
1902
1903#endif /* CONFIG_FST */
1904
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001905#ifdef CONFIG_OWE
1906
1907static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1908{
1909 struct hostapd_data *hapd = ctx;
1910 size_t i;
1911
1912 for (i = 0; i < iface->num_bss; i++) {
1913 struct hostapd_data *bss = iface->bss[i];
1914
1915 if (os_strcmp(hapd->conf->owe_transition_ifname,
1916 bss->conf->iface) != 0)
1917 continue;
1918
1919 wpa_printf(MSG_DEBUG,
1920 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1921 MACSTR " SSID %s",
1922 hapd->conf->iface, bss->conf->iface,
1923 MAC2STR(bss->own_addr),
1924 wpa_ssid_txt(bss->conf->ssid.ssid,
1925 bss->conf->ssid.ssid_len));
1926 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1927 is_zero_ether_addr(bss->own_addr))
1928 continue;
1929
1930 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1931 ETH_ALEN);
1932 os_memcpy(hapd->conf->owe_transition_ssid,
1933 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1934 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1935 wpa_printf(MSG_DEBUG,
1936 "OWE: Copied transition mode information");
1937 return 1;
1938 }
1939
1940 return 0;
1941}
1942
1943
1944int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1945{
1946 if (hapd->conf->owe_transition_ssid_len > 0 &&
1947 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1948 return 0;
1949
1950 /* Find transition mode SSID/BSSID information from a BSS operated by
1951 * this hostapd instance. */
1952 if (!hapd->iface->interfaces ||
1953 !hapd->iface->interfaces->for_each_interface)
1954 return hostapd_owe_iface_iter(hapd->iface, hapd);
1955 else
1956 return hapd->iface->interfaces->for_each_interface(
1957 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1958}
1959
1960
1961static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1962{
1963 size_t i;
1964
1965 for (i = 0; i < iface->num_bss; i++) {
1966 struct hostapd_data *bss = iface->bss[i];
1967 int res;
1968
1969 if (!bss->conf->owe_transition_ifname[0])
1970 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07001971 if (bss->iface->state != HAPD_IFACE_ENABLED) {
1972 wpa_printf(MSG_DEBUG,
1973 "OWE: Interface %s state %s - defer beacon update",
1974 bss->conf->iface,
1975 hostapd_state_text(bss->iface->state));
1976 continue;
1977 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001978 res = hostapd_owe_trans_get_info(bss);
1979 if (res == 0)
1980 continue;
1981 wpa_printf(MSG_DEBUG,
1982 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1983 bss->conf->iface);
1984 ieee802_11_set_beacon(bss);
1985 }
1986
1987 return 0;
1988}
1989
1990#endif /* CONFIG_OWE */
1991
1992
1993static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1994{
1995#ifdef CONFIG_OWE
1996 /* Check whether the enabled BSS can complete OWE transition mode
1997 * configuration for any pending interface. */
1998 if (!iface->interfaces ||
1999 !iface->interfaces->for_each_interface)
2000 hostapd_owe_iface_iter2(iface, NULL);
2001 else
2002 iface->interfaces->for_each_interface(
2003 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2004#endif /* CONFIG_OWE */
2005}
2006
2007
Roshan Pius3a1667e2018-07-03 15:17:14 -07002008static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2009 void *timeout_ctx)
2010{
2011 struct hostapd_iface *iface = eloop_ctx;
2012 struct hostapd_data *hapd;
2013
2014 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2015 return;
2016 hapd = iface->bss[0];
2017 if (hapd->setup_complete_cb)
2018 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2019}
2020
2021
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002022static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2023 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024{
2025 struct hostapd_data *hapd = iface->bss[0];
2026 size_t j;
2027 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002028 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002029 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002031 if (err)
2032 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033
2034 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002035 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002036#ifdef NEED_AP_MLME
2037 int res;
2038#endif /* NEED_AP_MLME */
2039
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2041 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002042 hostapd_hw_mode_txt(iface->conf->hw_mode),
2043 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044
Dmitry Shmidt051af732013-10-22 13:52:46 -07002045#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002046 /* Handle DFS only if it is not offloaded to the driver */
2047 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2048 /* Check DFS */
2049 res = hostapd_handle_dfs(iface);
2050 if (res <= 0) {
2051 if (res < 0)
2052 goto fail;
2053 return res;
2054 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002055 } else {
2056 /* If DFS is offloaded to the driver */
2057 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2058 if (res_dfs_offload <= 0) {
2059 if (res_dfs_offload < 0)
2060 goto fail;
2061 } else {
2062 wpa_printf(MSG_DEBUG,
2063 "Proceed with AP/channel setup");
2064 /*
2065 * If this is a DFS channel, move to completing
2066 * AP setup.
2067 */
2068 if (res_dfs_offload == 1)
2069 goto dfs_offload;
2070 /* Otherwise fall through. */
2071 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002072 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002073#endif /* NEED_AP_MLME */
2074
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002075#ifdef CONFIG_MESH
2076 if (iface->mconf != NULL) {
2077 wpa_printf(MSG_DEBUG,
2078 "%s: Mesh configuration will be applied while joining the mesh network",
2079 iface->bss[0]->conf->iface);
2080 delay_apply_cfg = 1;
2081 }
2082#endif /* CONFIG_MESH */
2083
2084 if (!delay_apply_cfg &&
2085 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002087 hapd->iconf->enable_edmg,
2088 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002090 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002091 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002092 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002093 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002094 hostapd_get_oper_chwidth(hapd->iconf),
2095 hostapd_get_oper_centr_freq_seg0_idx(
2096 hapd->iconf),
2097 hostapd_get_oper_centr_freq_seg1_idx(
2098 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 wpa_printf(MSG_ERROR, "Could not set channel for "
2100 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002101 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 }
2103 }
2104
2105 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2108 "table.");
2109 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2110 HOSTAPD_LEVEL_WARNING,
2111 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002112 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 }
2114 }
2115
Hai Shalom021b0b52019-04-10 11:17:58 -07002116 if (hapd->iconf->rts_threshold >= -1 &&
2117 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2118 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2120 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002121 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 }
2123
Hai Shalom021b0b52019-04-10 11:17:58 -07002124 if (hapd->iconf->fragm_threshold >= -1 &&
2125 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2126 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2128 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002129 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130 }
2131
2132 prev_addr = hapd->own_addr;
2133
2134 for (j = 0; j < iface->num_bss; j++) {
2135 hapd = iface->bss[j];
2136 if (j)
2137 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002138 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002139 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002140 hapd = iface->bss[j];
2141 hostapd_bss_deinit_no_free(hapd);
2142 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002143 if (j == 0)
2144 break;
2145 j--;
2146 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002147 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002148 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002149 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 prev_addr = hapd->own_addr;
2151 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002152 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153
2154 hostapd_tx_queue_params(iface);
2155
2156 ap_list_init(iface);
2157
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002158 hostapd_set_acl(hapd);
2159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 if (hostapd_driver_commit(hapd) < 0) {
2161 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2162 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002163 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164 }
2165
Jouni Malinen87fd2792011-05-16 18:35:42 +03002166 /*
2167 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2168 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2169 * mode), the interface is up only after driver_commit, so initialize
2170 * WPS after driver_commit.
2171 */
2172 for (j = 0; j < iface->num_bss; j++) {
2173 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002174 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002175 }
2176
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002177 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2178 !res_dfs_offload) {
2179 /*
2180 * If freq is DFS, and DFS is offloaded to the driver, then wait
2181 * for CAC to complete.
2182 */
2183 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2184 return res_dfs_offload;
2185 }
2186
2187#ifdef NEED_AP_MLME
2188dfs_offload:
2189#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002190
2191#ifdef CONFIG_FST
2192 if (hapd->iconf->fst_cfg.group_id[0]) {
2193 struct fst_wpa_obj iface_obj;
2194
2195 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2196 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2197 &iface_obj, &hapd->iconf->fst_cfg);
2198 if (!iface->fst) {
2199 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2200 hapd->iconf->fst_cfg.group_id);
2201 goto fail;
2202 }
2203 }
2204#endif /* CONFIG_FST */
2205
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002206 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002207 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002208 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002209 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002210 if (hapd->setup_complete_cb)
2211 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2212
Hai Shalom60840252021-02-19 19:02:11 -08002213#ifdef CONFIG_MESH
2214 if (delay_apply_cfg && !iface->mconf) {
2215 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2216 goto fail;
2217 }
2218#endif /* CONFIG_MESH */
2219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2221 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002222 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2223 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002225 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002226 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002229
2230fail:
2231 wpa_printf(MSG_ERROR, "Interface initialization failed");
2232 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2233 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002234#ifdef CONFIG_FST
2235 if (iface->fst) {
2236 fst_detach(iface->fst);
2237 iface->fst = NULL;
2238 }
2239#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002240
2241 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002242 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002243 } else if (hapd->setup_complete_cb) {
2244 /*
2245 * Calling hapd->setup_complete_cb directly may cause iface
2246 * deinitialization which may be accessed later by the caller.
2247 */
2248 eloop_register_timeout(0, 0,
2249 hostapd_interface_setup_failure_handler,
2250 iface, NULL);
2251 }
2252
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002253 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002254}
2255
2256
2257/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002258 * hostapd_setup_interface_complete - Complete interface setup
2259 *
2260 * This function is called when previous steps in the interface setup has been
2261 * completed. This can also start operations, e.g., DFS, that will require
2262 * additional processing before interface is ready to be enabled. Such
2263 * operations will call this function from eloop callbacks when finished.
2264 */
2265int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2266{
2267 struct hapd_interfaces *interfaces = iface->interfaces;
2268 struct hostapd_data *hapd = iface->bss[0];
2269 unsigned int i;
2270 int not_ready_in_sync_ifaces = 0;
2271
2272 if (!iface->need_to_start_in_sync)
2273 return hostapd_setup_interface_complete_sync(iface, err);
2274
2275 if (err) {
2276 wpa_printf(MSG_ERROR, "Interface initialization failed");
2277 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2278 iface->need_to_start_in_sync = 0;
2279 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2280 if (interfaces && interfaces->terminate_on_error)
2281 eloop_terminate();
2282 return -1;
2283 }
2284
2285 if (iface->ready_to_start_in_sync) {
2286 /* Already in ready and waiting. should never happpen */
2287 return 0;
2288 }
2289
2290 for (i = 0; i < interfaces->count; i++) {
2291 if (interfaces->iface[i]->need_to_start_in_sync &&
2292 !interfaces->iface[i]->ready_to_start_in_sync)
2293 not_ready_in_sync_ifaces++;
2294 }
2295
2296 /*
2297 * Check if this is the last interface, if yes then start all the other
2298 * waiting interfaces. If not, add this interface to the waiting list.
2299 */
2300 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2301 /*
2302 * If this interface went through CAC, do not synchronize, just
2303 * start immediately.
2304 */
2305 iface->need_to_start_in_sync = 0;
2306 wpa_printf(MSG_INFO,
2307 "%s: Finished CAC - bypass sync and start interface",
2308 iface->bss[0]->conf->iface);
2309 return hostapd_setup_interface_complete_sync(iface, err);
2310 }
2311
2312 if (not_ready_in_sync_ifaces > 1) {
2313 /* need to wait as there are other interfaces still coming up */
2314 iface->ready_to_start_in_sync = 1;
2315 wpa_printf(MSG_INFO,
2316 "%s: Interface waiting to sync with other interfaces",
2317 iface->bss[0]->conf->iface);
2318 return 0;
2319 }
2320
2321 wpa_printf(MSG_INFO,
2322 "%s: Last interface to sync - starting all interfaces",
2323 iface->bss[0]->conf->iface);
2324 iface->need_to_start_in_sync = 0;
2325 hostapd_setup_interface_complete_sync(iface, err);
2326 for (i = 0; i < interfaces->count; i++) {
2327 if (interfaces->iface[i]->need_to_start_in_sync &&
2328 interfaces->iface[i]->ready_to_start_in_sync) {
2329 hostapd_setup_interface_complete_sync(
2330 interfaces->iface[i], 0);
2331 /* Only once the interfaces are sync started */
2332 interfaces->iface[i]->need_to_start_in_sync = 0;
2333 }
2334 }
2335
2336 return 0;
2337}
2338
2339
2340/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341 * hostapd_setup_interface - Setup of an interface
2342 * @iface: Pointer to interface data.
2343 * Returns: 0 on success, -1 on failure
2344 *
2345 * Initializes the driver interface, validates the configuration,
2346 * and sets driver parameters based on the configuration.
2347 * Flushes old stations, sets the channel, encryption,
2348 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002349 *
2350 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2351 * or DFS operations, this function returns 0 before such operations have been
2352 * completed. The pending operations are registered into eloop and will be
2353 * completed from eloop callbacks. Those callbacks end up calling
2354 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355 */
2356int hostapd_setup_interface(struct hostapd_iface *iface)
2357{
2358 int ret;
2359
Hai Shaloma20dcd72022-02-04 13:43:00 -08002360 if (!iface->conf)
2361 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362 ret = setup_interface(iface);
2363 if (ret) {
2364 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002365 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 return -1;
2367 }
2368
2369 return 0;
2370}
2371
2372
2373/**
2374 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2375 * @hapd_iface: Pointer to interface data
2376 * @conf: Pointer to per-interface configuration
2377 * @bss: Pointer to per-BSS configuration for this BSS
2378 * Returns: Pointer to allocated BSS data
2379 *
2380 * This function is used to allocate per-BSS data structure. This data will be
2381 * freed after hostapd_cleanup() is called for it during interface
2382 * deinitialization.
2383 */
2384struct hostapd_data *
2385hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2386 struct hostapd_config *conf,
2387 struct hostapd_bss_config *bss)
2388{
2389 struct hostapd_data *hapd;
2390
2391 hapd = os_zalloc(sizeof(*hapd));
2392 if (hapd == NULL)
2393 return NULL;
2394
2395 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2396 hapd->iconf = conf;
2397 hapd->conf = bss;
2398 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002399 if (conf)
2400 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002401 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002402 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002403 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002404 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002405#ifdef CONFIG_IEEE80211R_AP
2406 dl_list_init(&hapd->l2_queue);
2407 dl_list_init(&hapd->l2_oui_queue);
2408#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002409#ifdef CONFIG_SAE
2410 dl_list_init(&hapd->sae_commit_queue);
2411#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412
2413 return hapd;
2414}
2415
2416
Dmitry Shmidt54605472013-11-08 11:10:19 -08002417static void hostapd_bss_deinit(struct hostapd_data *hapd)
2418{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002419 if (!hapd)
2420 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002421 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002422 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002423 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002424 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002425#ifdef CONFIG_SQLITE
2426 if (hapd->rad_attr_db) {
2427 sqlite3_close(hapd->rad_attr_db);
2428 hapd->rad_attr_db = NULL;
2429 }
2430#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002431 hostapd_cleanup(hapd);
2432}
2433
2434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002435void hostapd_interface_deinit(struct hostapd_iface *iface)
2436{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002437 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438
Dmitry Shmidt54605472013-11-08 11:10:19 -08002439 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 if (iface == NULL)
2441 return;
2442
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002443 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2444
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002445 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2446 iface->wait_channel_update = 0;
2447
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002448#ifdef CONFIG_FST
2449 if (iface->fst) {
2450 fst_detach(iface->fst);
2451 iface->fst = NULL;
2452 }
2453#endif /* CONFIG_FST */
2454
Hai Shalom021b0b52019-04-10 11:17:58 -07002455 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002456 if (!iface->bss)
2457 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002458 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002459 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002460
Roshan Pius3a1667e2018-07-03 15:17:14 -07002461#ifdef NEED_AP_MLME
2462 hostapd_stop_setup_timers(iface);
2463 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2464#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465}
2466
2467
2468void hostapd_interface_free(struct hostapd_iface *iface)
2469{
2470 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002471 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2472 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002473 if (!iface->bss)
2474 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002475 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2476 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 hostapd_cleanup_iface(iface);
2480}
2481
2482
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002483struct hostapd_iface * hostapd_alloc_iface(void)
2484{
2485 struct hostapd_iface *hapd_iface;
2486
2487 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2488 if (!hapd_iface)
2489 return NULL;
2490
2491 dl_list_init(&hapd_iface->sta_seen);
2492
2493 return hapd_iface;
2494}
2495
2496
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002497/**
2498 * hostapd_init - Allocate and initialize per-interface data
2499 * @config_file: Path to the configuration file
2500 * Returns: Pointer to the allocated interface data or %NULL on failure
2501 *
2502 * This function is used to allocate main data structures for per-interface
2503 * data. The allocated data buffer will be freed by calling
2504 * hostapd_cleanup_iface().
2505 */
2506struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2507 const char *config_file)
2508{
2509 struct hostapd_iface *hapd_iface = NULL;
2510 struct hostapd_config *conf = NULL;
2511 struct hostapd_data *hapd;
2512 size_t i;
2513
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002514 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002515 if (hapd_iface == NULL)
2516 goto fail;
2517
2518 hapd_iface->config_fname = os_strdup(config_file);
2519 if (hapd_iface->config_fname == NULL)
2520 goto fail;
2521
2522 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2523 if (conf == NULL)
2524 goto fail;
2525 hapd_iface->conf = conf;
2526
2527 hapd_iface->num_bss = conf->num_bss;
2528 hapd_iface->bss = os_calloc(conf->num_bss,
2529 sizeof(struct hostapd_data *));
2530 if (hapd_iface->bss == NULL)
2531 goto fail;
2532
2533 for (i = 0; i < conf->num_bss; i++) {
2534 hapd = hapd_iface->bss[i] =
2535 hostapd_alloc_bss_data(hapd_iface, conf,
2536 conf->bss[i]);
2537 if (hapd == NULL)
2538 goto fail;
2539 hapd->msg_ctx = hapd;
2540 }
2541
2542 return hapd_iface;
2543
2544fail:
2545 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2546 config_file);
2547 if (conf)
2548 hostapd_config_free(conf);
2549 if (hapd_iface) {
2550 os_free(hapd_iface->config_fname);
2551 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002552 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2553 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002554 os_free(hapd_iface);
2555 }
2556 return NULL;
2557}
2558
2559
2560static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2561{
2562 size_t i, j;
2563
2564 for (i = 0; i < interfaces->count; i++) {
2565 struct hostapd_iface *iface = interfaces->iface[i];
2566 for (j = 0; j < iface->num_bss; j++) {
2567 struct hostapd_data *hapd = iface->bss[j];
2568 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2569 return 1;
2570 }
2571 }
2572
2573 return 0;
2574}
2575
2576
2577/**
2578 * hostapd_interface_init_bss - Read configuration file and init BSS data
2579 *
2580 * This function is used to parse configuration file for a BSS. This BSS is
2581 * added to an existing interface sharing the same radio (if any) or a new
2582 * interface is created if this is the first interface on a radio. This
2583 * allocate memory for the BSS. No actual driver operations are started.
2584 *
2585 * This is similar to hostapd_interface_init(), but for a case where the
2586 * configuration is used to add a single BSS instead of all BSSes for a radio.
2587 */
2588struct hostapd_iface *
2589hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2590 const char *config_fname, int debug)
2591{
2592 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2593 struct hostapd_data *hapd;
2594 int k;
2595 size_t i, bss_idx;
2596
2597 if (!phy || !*phy)
2598 return NULL;
2599
2600 for (i = 0; i < interfaces->count; i++) {
2601 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2602 iface = interfaces->iface[i];
2603 break;
2604 }
2605 }
2606
2607 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2608 config_fname, phy, iface ? "" : " --> new PHY");
2609 if (iface) {
2610 struct hostapd_config *conf;
2611 struct hostapd_bss_config **tmp_conf;
2612 struct hostapd_data **tmp_bss;
2613 struct hostapd_bss_config *bss;
2614 const char *ifname;
2615
2616 /* Add new BSS to existing iface */
2617 conf = interfaces->config_read_cb(config_fname);
2618 if (conf == NULL)
2619 return NULL;
2620 if (conf->num_bss > 1) {
2621 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2622 hostapd_config_free(conf);
2623 return NULL;
2624 }
2625
2626 ifname = conf->bss[0]->iface;
2627 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2628 wpa_printf(MSG_ERROR,
2629 "Interface name %s already in use", ifname);
2630 hostapd_config_free(conf);
2631 return NULL;
2632 }
2633
2634 tmp_conf = os_realloc_array(
2635 iface->conf->bss, iface->conf->num_bss + 1,
2636 sizeof(struct hostapd_bss_config *));
2637 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2638 sizeof(struct hostapd_data *));
2639 if (tmp_bss)
2640 iface->bss = tmp_bss;
2641 if (tmp_conf) {
2642 iface->conf->bss = tmp_conf;
2643 iface->conf->last_bss = tmp_conf[0];
2644 }
2645 if (tmp_bss == NULL || tmp_conf == NULL) {
2646 hostapd_config_free(conf);
2647 return NULL;
2648 }
2649 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2650 iface->conf->num_bss++;
2651
2652 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2653 if (hapd == NULL) {
2654 iface->conf->num_bss--;
2655 hostapd_config_free(conf);
2656 return NULL;
2657 }
2658 iface->conf->last_bss = bss;
2659 iface->bss[iface->num_bss] = hapd;
2660 hapd->msg_ctx = hapd;
2661
2662 bss_idx = iface->num_bss++;
2663 conf->num_bss--;
2664 conf->bss[0] = NULL;
2665 hostapd_config_free(conf);
2666 } else {
2667 /* Add a new iface with the first BSS */
2668 new_iface = iface = hostapd_init(interfaces, config_fname);
2669 if (!iface)
2670 return NULL;
2671 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2672 iface->interfaces = interfaces;
2673 bss_idx = 0;
2674 }
2675
2676 for (k = 0; k < debug; k++) {
2677 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2678 iface->bss[bss_idx]->conf->logger_stdout_level--;
2679 }
2680
2681 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2682 !hostapd_drv_none(iface->bss[bss_idx])) {
2683 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2684 config_fname);
2685 if (new_iface)
2686 hostapd_interface_deinit_free(new_iface);
2687 return NULL;
2688 }
2689
2690 return iface;
2691}
2692
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002693
2694void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2695{
2696 const struct wpa_driver_ops *driver;
2697 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002698
2699 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002700 if (iface == NULL)
2701 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002702 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2703 __func__, (unsigned int) iface->num_bss,
2704 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002705 driver = iface->bss[0]->driver;
2706 drv_priv = iface->bss[0]->drv_priv;
2707 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002708 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2709 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002710 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002711 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002712 iface->bss[0]->drv_priv = NULL;
2713 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002714 hostapd_interface_free(iface);
2715}
2716
2717
Dmitry Shmidt15907092014-03-25 10:42:57 -07002718static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2719 void *drv_priv,
2720 struct hostapd_iface *hapd_iface)
2721{
2722 size_t j;
2723
2724 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2725 __func__, driver, drv_priv);
2726 if (driver && driver->hapd_deinit && drv_priv) {
2727 driver->hapd_deinit(drv_priv);
2728 for (j = 0; j < hapd_iface->num_bss; j++) {
2729 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2730 __func__, (int) j,
2731 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002732 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002733 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002734 hapd_iface->extended_capa = NULL;
2735 hapd_iface->extended_capa_mask = NULL;
2736 hapd_iface->extended_capa_len = 0;
2737 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002738 }
2739 }
2740}
2741
2742
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002743int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2744{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002745 size_t j;
2746
Hai Shalom60840252021-02-19 19:02:11 -08002747 if (!hapd_iface)
2748 return -1;
2749
2750 if (hapd_iface->enable_iface_cb)
2751 return hapd_iface->enable_iface_cb(hapd_iface);
2752
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002753 if (hapd_iface->bss[0]->drv_priv != NULL) {
2754 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002755 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002756 return -1;
2757 }
2758
2759 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002760 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002761
Dmitry Shmidt71757432014-06-02 13:50:35 -07002762 for (j = 0; j < hapd_iface->num_bss; j++)
2763 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002764 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2765 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2766 return -1;
2767 }
2768
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002769 if (hapd_iface->interfaces == NULL ||
2770 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002771 hapd_iface->interfaces->driver_init(hapd_iface))
2772 return -1;
2773
2774 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002775 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2776 hapd_iface->bss[0]->drv_priv,
2777 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002778 return -1;
2779 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002780
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002781 return 0;
2782}
2783
2784
2785int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2786{
2787 size_t j;
2788
2789 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002790 hapd_iface->conf->bss[0]->iface);
2791 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002792 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002793 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002794 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2795 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002796 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002797 hostapd_clear_old(hapd_iface);
2798 for (j = 0; j < hapd_iface->num_bss; j++)
2799 hostapd_reload_bss(hapd_iface->bss[j]);
2800
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002801 return 0;
2802}
2803
2804
2805int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2806{
2807 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002808 const struct wpa_driver_ops *driver;
2809 void *drv_priv;
2810
2811 if (hapd_iface == NULL)
2812 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002813
Hai Shalom60840252021-02-19 19:02:11 -08002814 if (hapd_iface->disable_iface_cb)
2815 return hapd_iface->disable_iface_cb(hapd_iface);
2816
Dmitry Shmidt71757432014-06-02 13:50:35 -07002817 if (hapd_iface->bss[0]->drv_priv == NULL) {
2818 wpa_printf(MSG_INFO, "Interface %s already disabled",
2819 hapd_iface->conf->bss[0]->iface);
2820 return -1;
2821 }
2822
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002823 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002824 driver = hapd_iface->bss[0]->driver;
2825 drv_priv = hapd_iface->bss[0]->drv_priv;
2826
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002827 hapd_iface->driver_ap_teardown =
2828 !!(hapd_iface->drv_flags &
2829 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2830
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002831#ifdef NEED_AP_MLME
2832 for (j = 0; j < hapd_iface->num_bss; j++)
2833 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2834#endif /* NEED_AP_MLME */
2835
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002836 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002837 for (j = 0; j < hapd_iface->num_bss; j++) {
2838 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002839 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002840 hostapd_free_hapd_data(hapd);
2841 }
2842
Dmitry Shmidt15907092014-03-25 10:42:57 -07002843 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002844
2845 /* From hostapd_cleanup_iface: These were initialized in
2846 * hostapd_setup_interface and hostapd_setup_interface_complete
2847 */
2848 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002849
Dmitry Shmidt56052862013-10-04 10:23:25 -07002850 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2851 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002852 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002853 return 0;
2854}
2855
2856
2857static struct hostapd_iface *
2858hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2859{
2860 struct hostapd_iface **iface, *hapd_iface;
2861
2862 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2863 sizeof(struct hostapd_iface *));
2864 if (iface == NULL)
2865 return NULL;
2866 interfaces->iface = iface;
2867 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002868 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002869 if (hapd_iface == NULL) {
2870 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2871 "the interface", __func__);
2872 return NULL;
2873 }
2874 interfaces->count++;
2875 hapd_iface->interfaces = interfaces;
2876
2877 return hapd_iface;
2878}
2879
2880
2881static struct hostapd_config *
2882hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002883 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002884{
2885 struct hostapd_bss_config *bss;
2886 struct hostapd_config *conf;
2887
2888 /* Allocates memory for bss and conf */
2889 conf = hostapd_config_defaults();
2890 if (conf == NULL) {
2891 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2892 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002893 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002894 }
2895
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002896 if (driver) {
2897 int j;
2898
2899 for (j = 0; wpa_drivers[j]; j++) {
2900 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2901 conf->driver = wpa_drivers[j];
2902 goto skip;
2903 }
2904 }
2905
2906 wpa_printf(MSG_ERROR,
2907 "Invalid/unknown driver '%s' - registering the default driver",
2908 driver);
2909 }
2910
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002911 conf->driver = wpa_drivers[0];
2912 if (conf->driver == NULL) {
2913 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2914 hostapd_config_free(conf);
2915 return NULL;
2916 }
2917
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002918skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002919 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002920
2921 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2922 bss->ctrl_interface = os_strdup(ctrl_iface);
2923 if (bss->ctrl_interface == NULL) {
2924 hostapd_config_free(conf);
2925 return NULL;
2926 }
2927
2928 /* Reading configuration file skipped, will be done in SET!
2929 * From reading the configuration till the end has to be done in
2930 * SET
2931 */
2932 return conf;
2933}
2934
2935
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002936static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2937 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002938{
2939 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002940 struct hostapd_data *hapd;
2941
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002942 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002943 sizeof(struct hostapd_data *));
2944 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002945 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002946
2947 for (i = 0; i < conf->num_bss; i++) {
2948 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002949 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002950 if (hapd == NULL) {
2951 while (i > 0) {
2952 i--;
2953 os_free(hapd_iface->bss[i]);
2954 hapd_iface->bss[i] = NULL;
2955 }
2956 os_free(hapd_iface->bss);
2957 hapd_iface->bss = NULL;
2958 return -1;
2959 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002960 hapd->msg_ctx = hapd;
2961 }
2962
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002963 hapd_iface->conf = conf;
2964 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002965
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002966 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002967}
2968
2969
2970int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2971{
2972 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002973 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2974 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002975 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002976 size_t i, j;
2977 const char *conf_file = NULL, *phy_name = NULL;
2978
2979 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2980 char *pos;
2981 phy_name = buf + 11;
2982 pos = os_strchr(phy_name, ':');
2983 if (!pos)
2984 return -1;
2985 *pos++ = '\0';
2986 conf_file = pos;
2987 if (!os_strlen(conf_file))
2988 return -1;
2989
2990 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2991 conf_file, 0);
2992 if (!hapd_iface)
2993 return -1;
2994 for (j = 0; j < interfaces->count; j++) {
2995 if (interfaces->iface[j] == hapd_iface)
2996 break;
2997 }
2998 if (j == interfaces->count) {
2999 struct hostapd_iface **tmp;
3000 tmp = os_realloc_array(interfaces->iface,
3001 interfaces->count + 1,
3002 sizeof(struct hostapd_iface *));
3003 if (!tmp) {
3004 hostapd_interface_deinit_free(hapd_iface);
3005 return -1;
3006 }
3007 interfaces->iface = tmp;
3008 interfaces->iface[interfaces->count++] = hapd_iface;
3009 new_iface = hapd_iface;
3010 }
3011
3012 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003013 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003014 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003015
3016 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003017 hostapd_deinit_driver(
3018 hapd_iface->bss[0]->driver,
3019 hapd_iface->bss[0]->drv_priv,
3020 hapd_iface);
3021 goto fail;
3022 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003023 } else {
3024 /* Assign new BSS with bss[0]'s driver info */
3025 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3026 hapd->driver = hapd_iface->bss[0]->driver;
3027 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3028 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3029 ETH_ALEN);
3030
3031 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003032 (hapd_iface->state == HAPD_IFACE_ENABLED &&
3033 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003034 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003035 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003036 hapd_iface->conf->num_bss--;
3037 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003038 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3039 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003040 hostapd_config_free_bss(hapd->conf);
3041 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003042 os_free(hapd);
3043 return -1;
3044 }
3045 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003046 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003047 return 0;
3048 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003049
3050 ptr = os_strchr(buf, ' ');
3051 if (ptr == NULL)
3052 return -1;
3053 *ptr++ = '\0';
3054
Dmitry Shmidt56052862013-10-04 10:23:25 -07003055 if (os_strncmp(ptr, "config=", 7) == 0)
3056 conf_file = ptr + 7;
3057
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003058 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003059 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003060 buf)) {
3061 wpa_printf(MSG_INFO, "Cannot add interface - it "
3062 "already exists");
3063 return -1;
3064 }
3065 }
3066
3067 hapd_iface = hostapd_iface_alloc(interfaces);
3068 if (hapd_iface == NULL) {
3069 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3070 "for interface", __func__);
3071 goto fail;
3072 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003073 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003074
Dmitry Shmidt56052862013-10-04 10:23:25 -07003075 if (conf_file && interfaces->config_read_cb) {
3076 conf = interfaces->config_read_cb(conf_file);
3077 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003078 os_strlcpy(conf->bss[0]->iface, buf,
3079 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003080 } else {
3081 char *driver = os_strchr(ptr, ' ');
3082
3083 if (driver)
3084 *driver++ = '\0';
3085 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3086 }
3087
Dmitry Shmidt56052862013-10-04 10:23:25 -07003088 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003089 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3090 "for configuration", __func__);
3091 goto fail;
3092 }
3093
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003094 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003095 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3096 "for hostapd", __func__);
3097 goto fail;
3098 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003099 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003100
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003101 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003102 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003103
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003104 wpa_printf(MSG_INFO, "Add interface '%s'",
3105 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003106
3107 return 0;
3108
3109fail:
3110 if (conf)
3111 hostapd_config_free(conf);
3112 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003113 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003114 for (i = 0; i < hapd_iface->num_bss; i++) {
3115 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003116 if (!hapd)
3117 continue;
3118 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003119 hapd_iface->interfaces->ctrl_iface_deinit)
3120 hapd_iface->interfaces->
3121 ctrl_iface_deinit(hapd);
3122 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3123 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003124 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003125 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003126 os_free(hapd);
3127 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003128 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003129 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003130 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003131 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003132 if (new_iface) {
3133 interfaces->count--;
3134 interfaces->iface[interfaces->count] = NULL;
3135 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003136 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003137 }
3138 return -1;
3139}
3140
3141
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003142static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3143{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003144 size_t i;
3145
Dmitry Shmidt54605472013-11-08 11:10:19 -08003146 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003147
Dmitry Shmidt54605472013-11-08 11:10:19 -08003148 /* Remove hostapd_data only if it has already been initialized */
3149 if (idx < iface->num_bss) {
3150 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003151
Dmitry Shmidt54605472013-11-08 11:10:19 -08003152 hostapd_bss_deinit(hapd);
3153 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3154 __func__, hapd, hapd->conf->iface);
3155 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003156 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003157 os_free(hapd);
3158
3159 iface->num_bss--;
3160
3161 for (i = idx; i < iface->num_bss; i++)
3162 iface->bss[i] = iface->bss[i + 1];
3163 } else {
3164 hostapd_config_free_bss(iface->conf->bss[idx]);
3165 iface->conf->bss[idx] = NULL;
3166 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003167
3168 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003169 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003170 iface->conf->bss[i] = iface->conf->bss[i + 1];
3171
3172 return 0;
3173}
3174
3175
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003176int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3177{
3178 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003179 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003180
3181 for (i = 0; i < interfaces->count; i++) {
3182 hapd_iface = interfaces->iface[i];
3183 if (hapd_iface == NULL)
3184 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003185 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003186 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003187 hapd_iface->driver_ap_teardown =
3188 !!(hapd_iface->drv_flags &
3189 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3190
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003191 hostapd_interface_deinit_free(hapd_iface);
3192 k = i;
3193 while (k < (interfaces->count - 1)) {
3194 interfaces->iface[k] =
3195 interfaces->iface[k + 1];
3196 k++;
3197 }
3198 interfaces->count--;
3199 return 0;
3200 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003201
3202 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003203 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3204 hapd_iface->driver_ap_teardown =
3205 !(hapd_iface->drv_flags &
3206 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003207 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003208 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003209 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003210 }
3211 return -1;
3212}
3213
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215/**
3216 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3217 * @hapd: Pointer to BSS data
3218 * @sta: Pointer to the associated STA data
3219 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3220 *
3221 * This function will be called whenever a station associates with the AP. It
3222 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3223 * from drv_callbacks.c based on driver events for drivers that take care of
3224 * management frames (IEEE 802.11 authentication and association) internally.
3225 */
3226void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3227 int reassoc)
3228{
3229 if (hapd->tkip_countermeasures) {
3230 hostapd_drv_sta_deauth(hapd, sta->addr,
3231 WLAN_REASON_MICHAEL_MIC_FAILURE);
3232 return;
3233 }
3234
3235 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003236 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003237 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239#ifdef CONFIG_P2P
3240 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3241 sta->no_p2p_set = 1;
3242 hapd->num_sta_no_p2p++;
3243 if (hapd->num_sta_no_p2p == 1)
3244 hostapd_p2p_non_p2p_sta_connected(hapd);
3245 }
3246#endif /* CONFIG_P2P */
3247
Hai Shalom81f62d82019-07-22 12:10:00 -07003248 airtime_policy_new_sta(hapd, sta);
3249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003250 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3251 * IEEE 802.1X/WPA code will start accounting after the station has
3252 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003253 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3254 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003255 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258
3259 /* Start IEEE 802.1X authentication process for new stations */
3260 ieee802_1x_new_station(hapd, sta);
3261 if (reassoc) {
3262 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003263 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3264 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3265 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3267 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3268 } else
3269 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003270
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003271 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3272 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3273 wpa_printf(MSG_DEBUG,
3274 "%s: %s: canceled wired ap_handle_timer timeout for "
3275 MACSTR,
3276 hapd->conf->iface, __func__,
3277 MAC2STR(sta->addr));
3278 }
3279 } else if (!(hapd->iface->drv_flags &
3280 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003281 wpa_printf(MSG_DEBUG,
3282 "%s: %s: reschedule ap_handle_timer timeout for "
3283 MACSTR " (%d seconds - ap_max_inactivity)",
3284 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003285 hapd->conf->ap_max_inactivity);
3286 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3287 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3288 ap_handle_timer, hapd, sta);
3289 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003290
3291#ifdef CONFIG_MACSEC
3292 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3293 hapd->conf->mka_psk_set)
3294 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3295 else
3296 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3297#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003299
3300
3301const char * hostapd_state_text(enum hostapd_iface_state s)
3302{
3303 switch (s) {
3304 case HAPD_IFACE_UNINITIALIZED:
3305 return "UNINITIALIZED";
3306 case HAPD_IFACE_DISABLED:
3307 return "DISABLED";
3308 case HAPD_IFACE_COUNTRY_UPDATE:
3309 return "COUNTRY_UPDATE";
3310 case HAPD_IFACE_ACS:
3311 return "ACS";
3312 case HAPD_IFACE_HT_SCAN:
3313 return "HT_SCAN";
3314 case HAPD_IFACE_DFS:
3315 return "DFS";
3316 case HAPD_IFACE_ENABLED:
3317 return "ENABLED";
3318 }
3319
3320 return "UNKNOWN";
3321}
3322
3323
3324void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3325{
3326 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003327 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3328 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003329 iface->state = s;
3330}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003331
3332
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003333int hostapd_csa_in_progress(struct hostapd_iface *iface)
3334{
3335 unsigned int i;
3336
3337 for (i = 0; i < iface->num_bss; i++)
3338 if (iface->bss[i]->csa_in_progress)
3339 return 1;
3340 return 0;
3341}
3342
3343
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003344#ifdef NEED_AP_MLME
3345
3346static void free_beacon_data(struct beacon_data *beacon)
3347{
3348 os_free(beacon->head);
3349 beacon->head = NULL;
3350 os_free(beacon->tail);
3351 beacon->tail = NULL;
3352 os_free(beacon->probe_resp);
3353 beacon->probe_resp = NULL;
3354 os_free(beacon->beacon_ies);
3355 beacon->beacon_ies = NULL;
3356 os_free(beacon->proberesp_ies);
3357 beacon->proberesp_ies = NULL;
3358 os_free(beacon->assocresp_ies);
3359 beacon->assocresp_ies = NULL;
3360}
3361
3362
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003363static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003364 struct beacon_data *beacon)
3365{
3366 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3367 struct wpa_driver_ap_params params;
3368 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003369
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003370 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003371 ret = ieee802_11_build_ap_params(hapd, &params);
3372 if (ret < 0)
3373 return ret;
3374
3375 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3376 &proberesp_extra,
3377 &assocresp_extra);
3378 if (ret)
3379 goto free_ap_params;
3380
3381 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003382 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003383 if (!beacon->head)
3384 goto free_ap_extra_ies;
3385
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003386 beacon->head_len = params.head_len;
3387
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003388 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003389 if (!beacon->tail)
3390 goto free_beacon;
3391
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003392 beacon->tail_len = params.tail_len;
3393
3394 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003395 beacon->probe_resp = os_memdup(params.proberesp,
3396 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003397 if (!beacon->probe_resp)
3398 goto free_beacon;
3399
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003400 beacon->probe_resp_len = params.proberesp_len;
3401 }
3402
3403 /* copy the extra ies */
3404 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003405 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3406 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003407 if (!beacon->beacon_ies)
3408 goto free_beacon;
3409
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003410 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3411 }
3412
3413 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003414 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3415 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003416 if (!beacon->proberesp_ies)
3417 goto free_beacon;
3418
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003419 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3420 }
3421
3422 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003423 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3424 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003425 if (!beacon->assocresp_ies)
3426 goto free_beacon;
3427
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003428 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3429 }
3430
3431 ret = 0;
3432free_beacon:
3433 /* if the function fails, the caller should not free beacon data */
3434 if (ret)
3435 free_beacon_data(beacon);
3436
3437free_ap_extra_ies:
3438 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3439 assocresp_extra);
3440free_ap_params:
3441 ieee802_11_free_ap_params(&params);
3442 return ret;
3443}
3444
3445
3446/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003447 * TODO: This flow currently supports only changing channel and width within
3448 * the same hw_mode. Any other changes to MAC parameters or provided settings
3449 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003450 */
3451static int hostapd_change_config_freq(struct hostapd_data *hapd,
3452 struct hostapd_config *conf,
3453 struct hostapd_freq_params *params,
3454 struct hostapd_freq_params *old_params)
3455{
3456 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003457 u8 seg0, seg1;
3458 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003459
3460 if (!params->channel) {
3461 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003462 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003463 }
3464
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003465 channel = params->channel;
3466 if (!channel)
3467 return -1;
3468
Hai Shalom81f62d82019-07-22 12:10:00 -07003469 mode = hapd->iface->current_mode;
3470
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003471 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003472 if (old_params &&
3473 hostapd_set_freq_params(old_params, conf->hw_mode,
3474 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003475 conf->channel, conf->enable_edmg,
3476 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003477 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07003478 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003479 hostapd_get_oper_chwidth(conf),
3480 hostapd_get_oper_centr_freq_seg0_idx(conf),
3481 hostapd_get_oper_centr_freq_seg1_idx(conf),
3482 conf->vht_capab,
3483 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07003484 NULL,
3485 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Hai Shalom81f62d82019-07-22 12:10:00 -07003486 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003487 return -1;
3488
3489 switch (params->bandwidth) {
3490 case 0:
3491 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003492 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3493 break;
3494 case 40:
3495 case 80:
3496 case 160:
3497 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3498 break;
3499 default:
3500 return -1;
3501 }
3502
3503 switch (params->bandwidth) {
3504 case 0:
3505 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003506 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003507 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003508 break;
3509 case 80:
3510 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003511 hostapd_set_oper_chwidth(conf,
3512 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003513 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003514 hostapd_set_oper_chwidth(conf,
3515 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003516 break;
3517 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003518 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
3519 break;
3520 case 320:
3521 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003522 break;
3523 default:
3524 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003525 }
3526
3527 conf->channel = channel;
3528 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003529 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003530 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003531 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003532 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003533 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003534 &seg1);
3535 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3536 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003537
3538 /* TODO: maybe call here hostapd_config_check here? */
3539
3540 return 0;
3541}
3542
3543
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003544static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003545 struct csa_settings *settings)
3546{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003547 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003548 struct hostapd_freq_params old_freq;
3549 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003550 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003551
3552 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003553 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003554 return -1;
3555
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003556 switch (settings->freq_params.bandwidth) {
3557 case 80:
3558 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003559 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003560 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003561 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003562 break;
3563 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003564 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003565 break;
3566 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00003567 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003568 break;
3569 }
3570
3571 if (ieee80211_freq_to_channel_ext(
3572 settings->freq_params.freq,
3573 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003574 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003575 &hapd->iface->cs_oper_class,
3576 &chan) == NUM_HOSTAPD_MODES) {
3577 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07003578 "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 -08003579 settings->freq_params.freq,
3580 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003581 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07003582 settings->freq_params.he_enabled,
3583 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003584 return -1;
3585 }
3586
3587 settings->freq_params.channel = chan;
3588
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003589 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3590 &settings->freq_params,
3591 &old_freq);
3592 if (ret)
3593 return ret;
3594
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003595 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003596
3597 /* change back the configuration */
3598 hostapd_change_config_freq(iface->bss[0], iface->conf,
3599 &old_freq, NULL);
3600
3601 if (ret)
3602 return ret;
3603
3604 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003605 hapd->cs_freq_params = settings->freq_params;
3606 hapd->cs_count = settings->cs_count;
3607 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003608
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003609 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003610 if (ret) {
3611 free_beacon_data(&settings->beacon_after);
3612 return ret;
3613 }
3614
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003615 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3616 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3617 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3618 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003619
3620 return 0;
3621}
3622
3623
3624void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3625{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003626 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3627 hapd->cs_count = 0;
3628 hapd->cs_block_tx = 0;
3629 hapd->cs_c_off_beacon = 0;
3630 hapd->cs_c_off_proberesp = 0;
3631 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003632 hapd->cs_c_off_ecsa_beacon = 0;
3633 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003634}
3635
3636
Hai Shalom60840252021-02-19 19:02:11 -08003637void hostapd_chan_switch_config(struct hostapd_data *hapd,
3638 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003639{
Sunil Ravia04bd252022-05-02 22:54:18 -07003640 if (freq_params->eht_enabled)
3641 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
3642 else
3643 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
3644
Hai Shalom60840252021-02-19 19:02:11 -08003645 if (freq_params->he_enabled)
3646 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
3647 else
3648 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
3649
3650 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003651 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3652 else
3653 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3654
3655 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08003656 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07003657 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
3658 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08003659 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003660 hapd->iconf->ch_switch_vht_config);
3661}
3662
3663
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003664int hostapd_switch_channel(struct hostapd_data *hapd,
3665 struct csa_settings *settings)
3666{
3667 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003668
3669 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3670 wpa_printf(MSG_INFO, "CSA is not supported");
3671 return -1;
3672 }
3673
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003674 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003675 if (ret)
3676 return ret;
3677
3678 ret = hostapd_drv_switch_channel(hapd, settings);
3679 free_beacon_data(&settings->beacon_csa);
3680 free_beacon_data(&settings->beacon_after);
3681
3682 if (ret) {
3683 /* if we failed, clean cs parameters */
3684 hostapd_cleanup_cs_params(hapd);
3685 return ret;
3686 }
3687
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003688 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003689 return 0;
3690}
3691
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003692
3693void
3694hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3695 const struct hostapd_freq_params *freq_params)
3696{
Sunil8cd6f4d2022-06-28 18:40:46 +00003697 int seg0_idx = 0, seg1_idx = 0;
3698 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003699
3700 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3701
3702 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003703 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003704 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003705 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003706
3707 switch (freq_params->bandwidth) {
3708 case 0:
3709 case 20:
3710 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003711 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003712 break;
3713 case 80:
3714 if (freq_params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003715 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003716 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003717 bw = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003718 break;
3719 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003720 bw = CONF_OPER_CHWIDTH_160MHZ;
3721 break;
3722 case 320:
3723 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003724 break;
3725 default:
3726 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3727 freq_params->bandwidth);
3728 break;
3729 }
3730
3731 iface->freq = freq_params->freq;
3732 iface->conf->channel = freq_params->channel;
3733 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003734 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3735 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3736 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003737 iface->conf->ieee80211n = freq_params->ht_enabled;
3738 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003739 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07003740 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003741
3742 /*
3743 * cs_params must not be cleared earlier because the freq_params
3744 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003745 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003746 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003747 hostapd_disable_iface(iface);
3748 hostapd_enable_iface(iface);
3749}
3750
Sunil Ravia04bd252022-05-02 22:54:18 -07003751
3752#ifdef CONFIG_IEEE80211AX
3753
3754void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
3755{
3756 hapd->cca_count = 0;
3757 hapd->cca_color = 0;
3758 hapd->cca_c_off_beacon = 0;
3759 hapd->cca_c_off_proberesp = 0;
3760 hapd->cca_in_progress = false;
3761}
3762
3763
3764static int hostapd_fill_cca_settings(struct hostapd_data *hapd,
3765 struct cca_settings *settings)
3766{
3767 struct hostapd_iface *iface = hapd->iface;
3768 u8 old_color;
3769 int ret;
3770
3771 if (!iface || iface->conf->he_op.he_bss_color_disabled)
3772 return -1;
3773
3774 old_color = iface->conf->he_op.he_bss_color;
3775 iface->conf->he_op.he_bss_color = hapd->cca_color;
3776 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
3777 if (ret)
3778 return ret;
3779
3780 iface->conf->he_op.he_bss_color = old_color;
3781
3782 settings->cca_count = hapd->cca_count;
3783 settings->cca_color = hapd->cca_color,
3784 hapd->cca_in_progress = true;
3785
3786 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
3787 if (ret) {
3788 free_beacon_data(&settings->beacon_after);
3789 return ret;
3790 }
3791
3792 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
3793 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
3794
3795 return 0;
3796}
3797
3798
3799static void hostapd_switch_color_timeout_handler(void *eloop_data,
3800 void *user_ctx)
3801{
3802 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
3803 os_time_t delta_t;
3804 unsigned int b;
3805 int i, r;
3806
3807 /* CCA can be triggered once the handler constantly receives
3808 * color collision events to for at least
3809 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
3810 delta_t = hapd->last_color_collision.sec -
3811 hapd->first_color_collision.sec;
3812 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
3813 return;
3814
3815 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
3816 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00003817 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07003818 break;
3819
3820 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
3821 }
3822
3823 if (i == HE_OPERATION_BSS_COLOR_MAX) {
3824 /* There are no free colors so turn BSS coloring off */
3825 wpa_printf(MSG_INFO,
3826 "No free colors left, turning off BSS coloring");
3827 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
3828 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
3829 for (b = 0; b < hapd->iface->num_bss; b++)
3830 ieee802_11_set_beacon(hapd->iface->bss[b]);
3831 return;
3832 }
3833
3834 for (b = 0; b < hapd->iface->num_bss; b++) {
3835 struct hostapd_data *bss = hapd->iface->bss[b];
3836 struct cca_settings settings;
3837 int ret;
3838
3839 hostapd_cleanup_cca_params(bss);
3840 bss->cca_color = r;
3841 bss->cca_count = 10;
3842
3843 if (hostapd_fill_cca_settings(bss, &settings)) {
3844 hostapd_cleanup_cca_params(bss);
3845 continue;
3846 }
3847
3848 ret = hostapd_drv_switch_color(bss, &settings);
3849 if (ret)
3850 hostapd_cleanup_cca_params(bss);
3851
3852 free_beacon_data(&settings.beacon_cca);
3853 free_beacon_data(&settings.beacon_after);
3854 }
3855}
3856
3857
3858void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
3859{
3860 struct os_reltime now;
3861
3862 if (hapd->cca_in_progress)
3863 return;
3864
3865 if (os_get_reltime(&now))
3866 return;
3867
3868 hapd->color_collision_bitmap = bitmap;
3869 hapd->last_color_collision = now;
3870
3871 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
3872 hapd, NULL))
3873 return;
3874
3875 hapd->first_color_collision = now;
3876 /* 10 s window as margin for persistent color collision reporting */
3877 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
3878 hostapd_switch_color_timeout_handler,
3879 hapd, NULL);
3880}
3881
3882#endif /* CONFIG_IEEE80211AX */
3883
Dmitry Shmidte4663042016-04-04 10:07:49 -07003884#endif /* NEED_AP_MLME */
3885
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003886
3887struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3888 const char *ifname)
3889{
3890 size_t i, j;
3891
3892 for (i = 0; i < interfaces->count; i++) {
3893 struct hostapd_iface *iface = interfaces->iface[i];
3894
3895 for (j = 0; j < iface->num_bss; j++) {
3896 struct hostapd_data *hapd = iface->bss[j];
3897
3898 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3899 return hapd;
3900 }
3901 }
3902
3903 return NULL;
3904}
3905
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003906
3907void hostapd_periodic_iface(struct hostapd_iface *iface)
3908{
3909 size_t i;
3910
3911 ap_list_timer(iface);
3912
3913 for (i = 0; i < iface->num_bss; i++) {
3914 struct hostapd_data *hapd = iface->bss[i];
3915
3916 if (!hapd->started)
3917 continue;
3918
3919#ifndef CONFIG_NO_RADIUS
3920 hostapd_acl_expire(hapd);
3921#endif /* CONFIG_NO_RADIUS */
3922 }
3923}
Hai Shalom899fcc72020-10-19 14:38:18 -07003924
3925
3926#ifdef CONFIG_OCV
3927void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
3928{
3929 struct hostapd_data *hapd = eloop_ctx;
3930 struct sta_info *sta;
3931
3932 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
3933
3934 for (sta = hapd->sta_list; sta; sta = sta->next) {
3935 if (!sta->post_csa_sa_query)
3936 continue;
3937
3938 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
3939 " did not start SA Query after CSA - disconnect",
3940 MAC2STR(sta->addr));
3941 ap_sta_disconnect(hapd, sta, sta->addr,
3942 WLAN_REASON_PREV_AUTH_NOT_VALID);
3943 }
3944}
3945#endif /* CONFIG_OCV */