blob: ddbcabc49328e57dac55d0d65c2b4d37829bf339 [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"
Sunil Ravib0ac25f2024-07-12 01:42:03 +000038#include "nan_usd_ap.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070039#include "gas_query_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040#include "hw_features.h"
41#include "wpa_auth_glue.h"
42#include "ap_drv_ops.h"
43#include "ap_config.h"
44#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070045#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070046#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070047#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080048#include "bss_load.h"
49#include "x_snoop.h"
50#include "dhcp_snoop.h"
51#include "ndisc_snoop.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070052#include "neighbor_db.h"
53#include "rrm.h"
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080054#include "fils_hlp.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070055#include "acs.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070056#include "hs20.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070057#include "airtime_policy.h"
58#include "wpa_auth_kay.h"
Sunil Ravi2a14cf12023-11-21 00:54:38 +000059#include "hw_features.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060
61
Dmitry Shmidt04949592012-07-19 12:16:46 -070062static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Hai Shalomfdcde762020-04-02 11:19:20 -070063#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070065static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -070066#endif /* CONFIG_WEP */
Dmitry Shmidtcce06662013-11-04 18:44:24 -080067static int setup_interface2(struct hostapd_iface *iface);
68static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Roshan Pius3a1667e2018-07-03 15:17:14 -070069static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
70 void *timeout_ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -070071#ifdef CONFIG_IEEE80211AX
72static void hostapd_switch_color_timeout_handler(void *eloop_data,
73 void *user_ctx);
74#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076
Dmitry Shmidt04949592012-07-19 12:16:46 -070077int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
78 int (*cb)(struct hostapd_iface *iface,
79 void *ctx), void *ctx)
80{
81 size_t i;
82 int ret;
83
84 for (i = 0; i < interfaces->count; i++) {
Hai Shalom4fbc08f2020-05-18 12:37:00 -070085 if (!interfaces->iface[i])
86 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -070087 ret = cb(interfaces->iface[i], ctx);
88 if (ret)
89 return ret;
90 }
91
92 return 0;
93}
94
95
Sunil Ravi77d572f2023-01-17 23:58:31 +000096struct hostapd_data * hostapd_mbssid_get_tx_bss(struct hostapd_data *hapd)
97{
98 if (hapd->iconf->mbssid)
99 return hapd->iface->bss[0];
100
101 return hapd;
102}
103
104
105int hostapd_mbssid_get_bss_index(struct hostapd_data *hapd)
106{
107 if (hapd->iconf->mbssid) {
108 size_t i;
109
110 for (i = 1; i < hapd->iface->num_bss; i++)
111 if (hapd->iface->bss[i] == hapd)
112 return i;
113 }
114
115 return 0;
116}
117
118
Hai Shalomce48b4a2018-09-05 11:41:35 -0700119void hostapd_reconfig_encryption(struct hostapd_data *hapd)
120{
121 if (hapd->wpa_auth)
122 return;
123
124 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700125#ifdef CONFIG_WEP
Hai Shalomce48b4a2018-09-05 11:41:35 -0700126 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700127#endif /* CONFIG_WEP */
Hai Shalomce48b4a2018-09-05 11:41:35 -0700128}
129
130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131static void hostapd_reload_bss(struct hostapd_data *hapd)
132{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800133 struct hostapd_ssid *ssid;
134
Dmitry Shmidt29333592017-01-09 12:27:11 -0800135 if (!hapd->started)
136 return;
137
Roshan Pius3a1667e2018-07-03 15:17:14 -0700138 if (hapd->conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -0800139 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
140 hapd->iconf->ieee80211ax;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142#ifndef CONFIG_NO_RADIUS
143 radius_client_reconfig(hapd->radius, hapd->conf->radius);
144#endif /* CONFIG_NO_RADIUS */
145
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800146 ssid = &hapd->conf->ssid;
147 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
148 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
149 /*
150 * Force PSK to be derived again since SSID or passphrase may
151 * have changed.
152 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800153 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800154 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155 if (hostapd_setup_wpa_psk(hapd->conf)) {
156 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
157 "after reloading configuration");
158 }
159
160 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
161 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
162 else
163 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
164
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800165 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167 if (hapd->wpa_auth)
168 wpa_init_keys(hapd->wpa_auth);
169 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170 const u8 *wpa_ie;
171 size_t wpa_ie_len;
172 hostapd_reconfig_wpa(hapd);
173 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
174 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
175 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
176 "the kernel driver.");
177 } else if (hapd->wpa_auth) {
178 wpa_deinit(hapd->wpa_auth);
179 hapd->wpa_auth = NULL;
180 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700181#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700183#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
185 }
186
187 ieee802_11_set_beacon(hapd);
188 hostapd_update_wps(hapd);
189
190 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700191 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 hapd->conf->ssid.ssid_len)) {
193 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
194 /* try to continue */
195 }
196 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
197}
198
199
Sunil Ravi77d572f2023-01-17 23:58:31 +0000200static void hostapd_clear_old_bss(struct hostapd_data *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201{
Sunil Ravi77d572f2023-01-17 23:58:31 +0000202 wpa_printf(MSG_DEBUG, "BSS %s changed - clear old state",
203 bss->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 /*
206 * Deauthenticate all stations since the new configuration may not
207 * allow them to use the BSS anymore.
208 */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000209 hostapd_flush_old_stations(bss, WLAN_REASON_PREV_AUTH_NOT_VALID);
Hai Shalomfdcde762020-04-02 11:19:20 -0700210#ifdef CONFIG_WEP
Sunil Ravi77d572f2023-01-17 23:58:31 +0000211 hostapd_broadcast_wep_clear(bss);
Hai Shalomfdcde762020-04-02 11:19:20 -0700212#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213
214#ifndef CONFIG_NO_RADIUS
Sunil Ravi77d572f2023-01-17 23:58:31 +0000215 /* TODO: update dynamic data based on changed configuration
216 * items (e.g., open/close sockets, etc.) */
217 radius_client_flush(bss->radius, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218#endif /* CONFIG_NO_RADIUS */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000219}
220
221
222static void hostapd_clear_old(struct hostapd_iface *iface)
223{
224 size_t j;
225
226 for (j = 0; j < iface->num_bss; j++)
227 hostapd_clear_old_bss(iface->bss[j]);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700228}
229
230
Hai Shalom74f70d42019-02-11 14:42:39 -0800231static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
232 struct hostapd_config *oldconf)
233{
234 size_t i;
235
236 if (newconf->num_bss != oldconf->num_bss)
237 return 1;
238
239 for (i = 0; i < newconf->num_bss; i++) {
240 if (os_strcmp(newconf->bss[i]->iface,
241 oldconf->bss[i]->iface) != 0)
242 return 1;
243 }
244
245 return 0;
246}
247
248
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700249int hostapd_reload_config(struct hostapd_iface *iface)
250{
Hai Shalom74f70d42019-02-11 14:42:39 -0800251 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700252 struct hostapd_data *hapd = iface->bss[0];
253 struct hostapd_config *newconf, *oldconf;
254 size_t j;
255
256 if (iface->config_fname == NULL) {
257 /* Only in-memory config in use - assume it has been updated */
258 hostapd_clear_old(iface);
259 for (j = 0; j < iface->num_bss; j++)
260 hostapd_reload_bss(iface->bss[j]);
261 return 0;
262 }
263
264 if (iface->interfaces == NULL ||
265 iface->interfaces->config_read_cb == NULL)
266 return -1;
267 newconf = iface->interfaces->config_read_cb(iface->config_fname);
268 if (newconf == NULL)
269 return -1;
270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800272 if (hostapd_iface_conf_changed(newconf, oldconf)) {
273 char *fname;
274 int res;
275
Sunil Ravi77d572f2023-01-17 23:58:31 +0000276 hostapd_clear_old(iface);
277
Hai Shalom74f70d42019-02-11 14:42:39 -0800278 wpa_printf(MSG_DEBUG,
279 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
280 fname = os_strdup(iface->config_fname);
281 if (!fname) {
282 hostapd_config_free(newconf);
283 return -1;
284 }
285 hostapd_remove_iface(interfaces, hapd->conf->iface);
286 iface = hostapd_init(interfaces, fname);
287 os_free(fname);
288 hostapd_config_free(newconf);
289 if (!iface) {
290 wpa_printf(MSG_ERROR,
291 "Failed to initialize interface on config reload");
292 return -1;
293 }
294 iface->interfaces = interfaces;
295 interfaces->iface[interfaces->count] = iface;
296 interfaces->count++;
297 res = hostapd_enable_iface(iface);
298 if (res < 0)
299 wpa_printf(MSG_ERROR,
300 "Failed to enable interface on config reload");
301 return res;
302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 iface->conf = newconf;
304
305 for (j = 0; j < iface->num_bss; j++) {
306 hapd = iface->bss[j];
Sunil Ravi77d572f2023-01-17 23:58:31 +0000307 if (!hapd->conf->config_id || !newconf->bss[j]->config_id ||
308 os_strcmp(hapd->conf->config_id,
309 newconf->bss[j]->config_id) != 0)
310 hostapd_clear_old_bss(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700312 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700313 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700314 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700315 hapd->iconf->ieee80211n = oldconf->ieee80211n;
316 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
317 hapd->iconf->ht_capab = oldconf->ht_capab;
318 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700319 hostapd_set_oper_chwidth(hapd->iconf,
320 hostapd_get_oper_chwidth(oldconf));
321 hostapd_set_oper_centr_freq_seg0_idx(
322 hapd->iconf,
323 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
324 hostapd_set_oper_centr_freq_seg1_idx(
325 hapd->iconf,
326 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800327 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 hostapd_reload_bss(hapd);
329 }
330
331 hostapd_config_free(oldconf);
332
333
334 return 0;
335}
336
337
Hai Shalomfdcde762020-04-02 11:19:20 -0700338#ifdef CONFIG_WEP
339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700341 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342{
343 int i;
344
Dmitry Shmidt29333592017-01-09 12:27:11 -0800345 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700346 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700348 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
349 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 wpa_printf(MSG_DEBUG, "Failed to clear default "
351 "encryption keys (ifname=%s keyidx=%d)",
352 ifname, i);
353 }
354 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 if (hapd->conf->ieee80211w) {
356 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
357 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700358 NULL, i, 0, 0, NULL,
359 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 wpa_printf(MSG_DEBUG, "Failed to clear "
361 "default mgmt encryption keys "
362 "(ifname=%s keyidx=%d)", ifname, i);
363 }
364 }
365 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366}
367
368
369static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
370{
371 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
372 return 0;
373}
374
375
376static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
377{
378 int errors = 0, idx;
379 struct hostapd_ssid *ssid = &hapd->conf->ssid;
380
381 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700382 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700384 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700386 ssid->wep.len[idx],
387 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
389 errors++;
390 }
391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 return errors;
393}
394
Hai Shalomfdcde762020-04-02 11:19:20 -0700395#endif /* CONFIG_WEP */
396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397
Sunil Ravi88611412024-06-28 17:34:56 +0000398static void hostapd_clear_drv_priv(struct hostapd_data *hapd)
399{
400 unsigned int i;
401
402 for (i = 0; i < hapd->iface->interfaces->count; i++) {
403 struct hostapd_iface *iface = hapd->iface->interfaces->iface[i];
404
405 if (hapd->iface == iface || !iface)
406 continue;
407
408 if (iface->bss && iface->bss[0] &&
409 iface->bss[0]->mld_first_bss == hapd)
410 iface->bss[0]->drv_priv = NULL;
411 }
412
413 hapd->drv_priv = NULL;
414}
415
416
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000417#ifdef CONFIG_IEEE80211BE
418#ifdef CONFIG_TESTING_OPTIONS
419
420#define TU_TO_USEC(_val) ((_val) * 1024)
421
422static void hostapd_link_remove_timeout_handler(void *eloop_data,
423 void *user_ctx)
424{
425 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
426
427 if (hapd->eht_mld_link_removal_count == 0)
428 return;
429 hapd->eht_mld_link_removal_count--;
430
431 wpa_printf(MSG_DEBUG, "MLD: Remove link_id=%u in %u beacons",
432 hapd->mld_link_id,
433 hapd->eht_mld_link_removal_count);
434
435 ieee802_11_set_beacon(hapd);
436
437 if (!hapd->eht_mld_link_removal_count) {
438 hostapd_disable_iface(hapd->iface);
439 return;
440 }
441
442 eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
443 hostapd_link_remove_timeout_handler,
444 hapd, NULL);
445}
446
447
448int hostapd_link_remove(struct hostapd_data *hapd, u32 count)
449{
450 if (!hapd->conf->mld_ap)
451 return -1;
452
453 wpa_printf(MSG_DEBUG,
454 "MLD: Remove link_id=%u in %u beacons",
455 hapd->mld_link_id, count);
456
457 hapd->eht_mld_link_removal_count = count;
458 hapd->eht_mld_bss_param_change++;
459
460 eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
461 hostapd_link_remove_timeout_handler,
462 hapd, NULL);
463
464 ieee802_11_set_beacon(hapd);
465 return 0;
466}
467
468#endif /* CONFIG_TESTING_OPTIONS */
469#endif /* CONFIG_IEEE80211BE */
470
471
Hai Shalom60840252021-02-19 19:02:11 -0800472void hostapd_free_hapd_data(struct hostapd_data *hapd)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700473{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800474 os_free(hapd->probereq_cb);
475 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800476 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800477
478#ifdef CONFIG_P2P
479 wpabuf_free(hapd->p2p_beacon_ie);
480 hapd->p2p_beacon_ie = NULL;
481 wpabuf_free(hapd->p2p_probe_resp_ie);
482 hapd->p2p_probe_resp_ie = NULL;
483#endif /* CONFIG_P2P */
484
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800485 if (!hapd->started) {
486 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700487 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800488 return;
489 }
490 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700491 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800492
Dmitry Shmidt54605472013-11-08 11:10:19 -0800493 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494 accounting_deinit(hapd);
495 hostapd_deinit_wpa(hapd);
496 vlan_deinit(hapd);
497 hostapd_acl_deinit(hapd);
498#ifndef CONFIG_NO_RADIUS
Sunil Ravi88611412024-06-28 17:34:56 +0000499 if (!hapd->mld_first_bss) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000500 struct hapd_interfaces *ifaces = hapd->iface->interfaces;
501 size_t i;
502
503 for (i = 0; i < ifaces->count; i++) {
504 struct hostapd_iface *iface = ifaces->iface[i];
505 size_t j;
506
507 for (j = 0; iface && j < iface->num_bss; j++) {
508 struct hostapd_data *h = iface->bss[j];
509
510 if (hapd == h)
511 continue;
512 if (h->radius == hapd->radius)
513 h->radius = NULL;
514 if (h->radius_das == hapd->radius_das)
515 h->radius_das = NULL;
516 }
517 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000518 radius_client_deinit(hapd->radius);
519 radius_das_deinit(hapd->radius_das);
520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700522 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523#endif /* CONFIG_NO_RADIUS */
524
525 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700526 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700527#ifdef CONFIG_DPP
528 hostapd_dpp_deinit(hapd);
529 gas_query_ap_deinit(hapd->gas);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800530 hapd->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700531#endif /* CONFIG_DPP */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000532#ifdef CONFIG_NAN_USD
533 hostapd_nan_usd_deinit(hapd);
534#endif /* CONFIG_NAN_USD */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535
536 authsrv_deinit(hapd);
537
Dmitry Shmidt71757432014-06-02 13:50:35 -0700538 if (hapd->interface_added) {
539 hapd->interface_added = 0;
540 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
541 wpa_printf(MSG_WARNING,
542 "Failed to remove BSS interface %s",
543 hapd->conf->iface);
544 hapd->interface_added = 1;
545 } else {
546 /*
547 * Since this was a dynamically added interface, the
548 * driver wrapper may have removed its internal instance
549 * and hapd->drv_priv is not valid anymore.
550 */
Sunil Ravi88611412024-06-28 17:34:56 +0000551 hostapd_clear_drv_priv(hapd);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 }
554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800555 wpabuf_free(hapd->time_adv);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800556 hapd->time_adv = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700557
558#ifdef CONFIG_INTERWORKING
559 gas_serv_deinit(hapd);
560#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800561
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800562 bss_load_update_deinit(hapd);
563 ndisc_snoop_deinit(hapd);
564 dhcp_snoop_deinit(hapd);
565 x_snoop_deinit(hapd);
566
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800567#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700568 bin_clear_free(hapd->tmp_eap_user.identity,
569 hapd->tmp_eap_user.identity_len);
570 bin_clear_free(hapd->tmp_eap_user.password,
571 hapd->tmp_eap_user.password_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800572 os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800573#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800574
575#ifdef CONFIG_MESH
576 wpabuf_free(hapd->mesh_pending_auth);
577 hapd->mesh_pending_auth = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800578 /* handling setup failure is already done */
579 hapd->setup_complete_cb = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800580#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700581
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000582#ifndef CONFIG_NO_RRM
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700583 hostapd_clean_rrm(hapd);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000584#endif /* CONFIG_NO_RRM */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800585 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700586
Hai Shalom899fcc72020-10-19 14:38:18 -0700587#ifdef CONFIG_OCV
588 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
589#endif /* CONFIG_OCV */
590
Hai Shalom021b0b52019-04-10 11:17:58 -0700591#ifdef CONFIG_SAE
592 {
593 struct hostapd_sae_commit_queue *q;
594
595 while ((q = dl_list_first(&hapd->sae_commit_queue,
596 struct hostapd_sae_commit_queue,
597 list))) {
598 dl_list_del(&q->list);
599 os_free(q);
600 }
601 }
602 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
603#endif /* CONFIG_SAE */
Sunil Ravia04bd252022-05-02 22:54:18 -0700604
605#ifdef CONFIG_IEEE80211AX
606 eloop_cancel_timeout(hostapd_switch_color_timeout_handler, hapd, NULL);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000607#ifdef CONFIG_TESTING_OPTIONS
608#ifdef CONFIG_IEEE80211BE
609 eloop_cancel_timeout(hostapd_link_remove_timeout_handler, hapd, NULL);
610#endif /* CONFIG_IEEE80211BE */
611#endif /* CONFIG_TESTING_OPTIONS */
612
Sunil Ravia04bd252022-05-02 22:54:18 -0700613#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700614}
615
616
617/**
618 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
619 * @hapd: Pointer to BSS data
620 *
621 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800622 * Most of the modules that are initialized in hostapd_setup_bss() are
623 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700624 */
625static void hostapd_cleanup(struct hostapd_data *hapd)
626{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800627 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700628 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700629 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800630 hapd->iface->interfaces->ctrl_iface_deinit) {
631 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700632 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800633 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700634 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635}
636
637
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800638static void sta_track_deinit(struct hostapd_iface *iface)
639{
640 struct hostapd_sta_info *info;
641
642 if (!iface->num_sta_seen)
643 return;
644
645 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
646 list))) {
647 dl_list_del(&info->list);
648 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700649 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800650 }
651}
652
653
Hai Shalom60840252021-02-19 19:02:11 -0800654void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700655{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800656 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800657#ifdef NEED_AP_MLME
658 hostapd_stop_setup_timers(iface);
659#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700660 if (iface->current_mode)
661 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700662 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
663 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700664 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700665 os_free(iface->current_rates);
666 iface->current_rates = NULL;
667 os_free(iface->basic_rates);
668 iface->basic_rates = NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000669 iface->cac_started = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700670 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800671 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700672 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700673}
674
675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676/**
677 * hostapd_cleanup_iface - Complete per-interface cleanup
678 * @iface: Pointer to interface data
679 *
680 * This function is called after per-BSS data structures are deinitialized
681 * with hostapd_cleanup().
682 */
683static void hostapd_cleanup_iface(struct hostapd_iface *iface)
684{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800685 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Sunil Ravi88611412024-06-28 17:34:56 +0000686 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700687 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
688 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800689
Dmitry Shmidt04949592012-07-19 12:16:46 -0700690 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 hostapd_config_free(iface->conf);
692 iface->conf = NULL;
693
694 os_free(iface->config_fname);
695 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800696 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 os_free(iface);
698}
699
700
Hai Shalomfdcde762020-04-02 11:19:20 -0700701#ifdef CONFIG_WEP
702
Dmitry Shmidt04949592012-07-19 12:16:46 -0700703static void hostapd_clear_wep(struct hostapd_data *hapd)
704{
Hai Shalom021b0b52019-04-10 11:17:58 -0700705 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706 hostapd_set_privacy(hapd, 0);
707 hostapd_broadcast_wep_clear(hapd);
708 }
709}
710
711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
713{
714 int i;
715
716 hostapd_broadcast_wep_set(hapd);
717
718 if (hapd->conf->ssid.wep.default_len) {
719 hostapd_set_privacy(hapd, 1);
720 return 0;
721 }
722
Jouni Malinen75ecf522011-06-27 15:19:46 -0700723 /*
724 * When IEEE 802.1X is not enabled, the driver may need to know how to
725 * set authentication algorithms for static WEP.
726 */
727 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 for (i = 0; i < 4; i++) {
730 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700731 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 i == hapd->conf->ssid.wep.idx, NULL, 0,
733 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700734 hapd->conf->ssid.wep.len[i],
735 i == hapd->conf->ssid.wep.idx ?
736 KEY_FLAG_GROUP_RX_TX_DEFAULT :
737 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 wpa_printf(MSG_WARNING, "Could not set WEP "
739 "encryption.");
740 return -1;
741 }
742 if (hapd->conf->ssid.wep.key[i] &&
743 i == hapd->conf->ssid.wep.idx)
744 hostapd_set_privacy(hapd, 1);
745 }
746
747 return 0;
748}
749
Hai Shalomfdcde762020-04-02 11:19:20 -0700750#endif /* CONFIG_WEP */
751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752
Dmitry Shmidt04949592012-07-19 12:16:46 -0700753static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754{
755 int ret = 0;
756 u8 addr[ETH_ALEN];
757
758 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
759 return 0;
760
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800761 if (!hapd->iface->driver_ap_teardown) {
762 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
763 "Flushing old station entries");
764
765 if (hostapd_flush(hapd)) {
766 wpa_msg(hapd->msg_ctx, MSG_WARNING,
767 "Could not connect to kernel driver");
768 ret = -1;
769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700771 if (hapd->conf && hapd->conf->broadcast_deauth) {
772 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
773 "Deauthenticate all stations");
774 os_memset(addr, 0xff, ETH_ALEN);
775 hostapd_drv_sta_deauth(hapd, addr, reason);
776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777 hostapd_free_stas(hapd);
778
779 return ret;
780}
781
782
Hai Shalom60840252021-02-19 19:02:11 -0800783void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
Dmitry Shmidt71757432014-06-02 13:50:35 -0700784{
785 hostapd_free_stas(hapd);
786 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700787#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700788 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700789#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700790}
791
792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793/**
794 * hostapd_validate_bssid_configuration - Validate BSSID configuration
795 * @iface: Pointer to interface data
796 * Returns: 0 on success, -1 on failure
797 *
798 * This function is used to validate that the configured BSSIDs are valid.
799 */
800static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
801{
802 u8 mask[ETH_ALEN] = { 0 };
803 struct hostapd_data *hapd = iface->bss[0];
804 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 int auto_addr = 0;
806
807 if (hostapd_drv_none(hapd))
808 return 0;
809
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800810 if (iface->conf->use_driver_iface_addr)
811 return 0;
812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
814
815 /* Determine the bits necessary to cover the number of BSSIDs. */
816 for (i--; i; i >>= 1)
817 bits++;
818
819 /* Determine the bits necessary to any configured BSSIDs,
820 if they are higher than the number of BSSIDs. */
821 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800822 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823 if (j)
824 auto_addr++;
825 continue;
826 }
827
828 for (i = 0; i < ETH_ALEN; i++) {
829 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800830 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 hapd->own_addr[i];
832 }
833 }
834
835 if (!auto_addr)
836 goto skip_mask_ext;
837
838 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
839 ;
840 j = 0;
841 if (i < ETH_ALEN) {
842 j = (5 - i) * 8;
843
844 while (mask[i] != 0) {
845 mask[i] >>= 1;
846 j++;
847 }
848 }
849
850 if (bits < j)
851 bits = j;
852
853 if (bits > 40) {
854 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
855 bits);
856 return -1;
857 }
858
859 os_memset(mask, 0xff, ETH_ALEN);
860 j = bits / 8;
861 for (i = 5; i > 5 - j; i--)
862 mask[i] = 0;
863 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700864 while (j) {
865 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700867 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868
869skip_mask_ext:
870 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
871 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 if (!auto_addr)
874 return 0;
875
876 for (i = 0; i < ETH_ALEN; i++) {
877 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
878 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
879 " for start address " MACSTR ".",
880 MAC2STR(mask), MAC2STR(hapd->own_addr));
881 wpa_printf(MSG_ERROR, "Start address must be the "
882 "first address in the block (i.e., addr "
883 "AND mask == addr).");
884 return -1;
885 }
886 }
887
888 return 0;
889}
890
891
892static int mac_in_conf(struct hostapd_config *conf, const void *a)
893{
894 size_t i;
895
896 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800897 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 return 1;
899 }
900 }
901
902 return 0;
903}
904
905
Dmitry Shmidt04949592012-07-19 12:16:46 -0700906#ifndef CONFIG_NO_RADIUS
907
908static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
909 struct radius_das_attrs *attr)
910{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800911 if (attr->nas_identifier &&
912 (!hapd->conf->nas_identifier ||
913 os_strlen(hapd->conf->nas_identifier) !=
914 attr->nas_identifier_len ||
915 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
916 attr->nas_identifier_len) != 0)) {
917 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
918 return 1;
919 }
920
921 if (attr->nas_ip_addr &&
922 (hapd->conf->own_ip_addr.af != AF_INET ||
923 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
924 0)) {
925 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
926 return 1;
927 }
928
929#ifdef CONFIG_IPV6
930 if (attr->nas_ipv6_addr &&
931 (hapd->conf->own_ip_addr.af != AF_INET6 ||
932 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
933 != 0)) {
934 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
935 return 1;
936 }
937#endif /* CONFIG_IPV6 */
938
Dmitry Shmidt04949592012-07-19 12:16:46 -0700939 return 0;
940}
941
942
943static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800944 struct radius_das_attrs *attr,
945 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700946{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800947 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700948 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800949 int num_attr = 0;
950 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700951
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800952 *multi = 0;
953
954 for (sta = hapd->sta_list; sta; sta = sta->next)
955 sta->radius_das_match = 1;
956
957 if (attr->sta_addr) {
958 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700959 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800960 if (!sta) {
961 wpa_printf(MSG_DEBUG,
962 "RADIUS DAS: No Calling-Station-Id match");
963 return NULL;
964 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700965
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800966 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800968 if (sta != selected)
969 sta->radius_das_match = 0;
970 }
971 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
972 }
973
974 if (attr->acct_session_id) {
975 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800976 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800977 wpa_printf(MSG_DEBUG,
978 "RADIUS DAS: Acct-Session-Id cannot match");
979 return NULL;
980 }
981 count = 0;
982
983 for (sta = hapd->sta_list; sta; sta = sta->next) {
984 if (!sta->radius_das_match)
985 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800986 os_snprintf(buf, sizeof(buf), "%016llX",
987 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800988 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800989 sta->radius_das_match = 0;
990 else
991 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700992 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800993
994 if (count == 0) {
995 wpa_printf(MSG_DEBUG,
996 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
997 return NULL;
998 }
999 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001000 }
1001
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001002 if (attr->acct_multi_session_id) {
1003 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001004 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001005 wpa_printf(MSG_DEBUG,
1006 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
1007 return NULL;
1008 }
1009 count = 0;
1010
1011 for (sta = hapd->sta_list; sta; sta = sta->next) {
1012 if (!sta->radius_das_match)
1013 continue;
1014 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001015 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001016 sta->radius_das_match = 0;
1017 continue;
1018 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001019 os_snprintf(buf, sizeof(buf), "%016llX",
1020 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001021 sta->eapol_sm->acct_multi_session_id);
1022 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001023 0)
1024 sta->radius_das_match = 0;
1025 else
1026 count++;
1027 }
1028
1029 if (count == 0) {
1030 wpa_printf(MSG_DEBUG,
1031 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
1032 return NULL;
1033 }
1034 wpa_printf(MSG_DEBUG,
1035 "RADIUS DAS: Acct-Multi-Session-Id match");
1036 }
1037
1038 if (attr->cui) {
1039 num_attr++;
1040 count = 0;
1041
Dmitry Shmidt04949592012-07-19 12:16:46 -07001042 for (sta = hapd->sta_list; sta; sta = sta->next) {
1043 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001044
1045 if (!sta->radius_das_match)
1046 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001047 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001048 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001049 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001050 attr->cui_len) != 0)
1051 sta->radius_das_match = 0;
1052 else
1053 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001054 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001055
1056 if (count == 0) {
1057 wpa_printf(MSG_DEBUG,
1058 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
1059 return NULL;
1060 }
1061 wpa_printf(MSG_DEBUG,
1062 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001063 }
1064
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001065 if (attr->user_name) {
1066 num_attr++;
1067 count = 0;
1068
Dmitry Shmidt04949592012-07-19 12:16:46 -07001069 for (sta = hapd->sta_list; sta; sta = sta->next) {
1070 u8 *identity;
1071 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001072
1073 if (!sta->radius_das_match)
1074 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001075 identity = ieee802_1x_get_identity(sta->eapol_sm,
1076 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001077 if (!identity ||
1078 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001079 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001080 != 0)
1081 sta->radius_das_match = 0;
1082 else
1083 count++;
1084 }
1085
1086 if (count == 0) {
1087 wpa_printf(MSG_DEBUG,
1088 "RADIUS DAS: No matches remaining after User-Name check");
1089 return NULL;
1090 }
1091 wpa_printf(MSG_DEBUG,
1092 "RADIUS DAS: User-Name match");
1093 }
1094
1095 if (num_attr == 0) {
1096 /*
1097 * In theory, we could match all current associations, but it
1098 * seems safer to just reject requests that do not include any
1099 * session identification attributes.
1100 */
1101 wpa_printf(MSG_DEBUG,
1102 "RADIUS DAS: No session identification attributes included");
1103 return NULL;
1104 }
1105
1106 selected = NULL;
1107 for (sta = hapd->sta_list; sta; sta = sta->next) {
1108 if (sta->radius_das_match) {
1109 if (selected) {
1110 *multi = 1;
1111 return NULL;
1112 }
1113 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001114 }
1115 }
1116
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001117 return selected;
1118}
1119
1120
1121static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
1122 struct radius_das_attrs *attr)
1123{
1124 if (!hapd->wpa_auth)
1125 return -1;
1126 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001127}
1128
1129
1130static enum radius_das_res
1131hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
1132{
1133 struct hostapd_data *hapd = ctx;
1134 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001135 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001136
1137 if (hostapd_das_nas_mismatch(hapd, attr))
1138 return RADIUS_DAS_NAS_MISMATCH;
1139
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001140 sta = hostapd_das_find_sta(hapd, attr, &multi);
1141 if (sta == NULL) {
1142 if (multi) {
1143 wpa_printf(MSG_DEBUG,
1144 "RADIUS DAS: Multiple sessions match - not supported");
1145 return RADIUS_DAS_MULTI_SESSION_MATCH;
1146 }
1147 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1148 wpa_printf(MSG_DEBUG,
1149 "RADIUS DAS: PMKSA cache entry matched");
1150 return RADIUS_DAS_SUCCESS;
1151 }
1152 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001153 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001154 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001155
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001156 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1157 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001158 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1159
Dmitry Shmidt04949592012-07-19 12:16:46 -07001160 hostapd_drv_sta_deauth(hapd, sta->addr,
1161 WLAN_REASON_PREV_AUTH_NOT_VALID);
1162 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1163
1164 return RADIUS_DAS_SUCCESS;
1165}
1166
Roshan Pius3a1667e2018-07-03 15:17:14 -07001167
1168#ifdef CONFIG_HS20
1169static enum radius_das_res
1170hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1171{
1172 struct hostapd_data *hapd = ctx;
1173 struct sta_info *sta;
1174 int multi;
1175
1176 if (hostapd_das_nas_mismatch(hapd, attr))
1177 return RADIUS_DAS_NAS_MISMATCH;
1178
1179 sta = hostapd_das_find_sta(hapd, attr, &multi);
1180 if (!sta) {
1181 if (multi) {
1182 wpa_printf(MSG_DEBUG,
1183 "RADIUS DAS: Multiple sessions match - not supported");
1184 return RADIUS_DAS_MULTI_SESSION_MATCH;
1185 }
1186 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1187 return RADIUS_DAS_SESSION_NOT_FOUND;
1188 }
1189
1190 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1191 " - CoA", MAC2STR(sta->addr));
1192
1193 if (attr->hs20_t_c_filtering) {
1194 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1195 wpa_printf(MSG_DEBUG,
1196 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1197 return RADIUS_DAS_COA_FAILED;
1198 }
1199
1200 hs20_t_c_filtering(hapd, sta, 0);
1201 }
1202
1203 return RADIUS_DAS_SUCCESS;
1204}
1205#else /* CONFIG_HS20 */
1206#define hostapd_das_coa NULL
1207#endif /* CONFIG_HS20 */
1208
Hai Shalomc3565922019-10-28 11:58:20 -07001209
1210#ifdef CONFIG_SQLITE
1211
1212static int db_table_exists(sqlite3 *db, const char *name)
1213{
1214 char cmd[128];
1215
1216 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1217 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1218}
1219
1220
1221static int db_table_create_radius_attributes(sqlite3 *db)
1222{
1223 char *err = NULL;
1224 const char *sql =
1225 "CREATE TABLE radius_attributes("
1226 " id INTEGER PRIMARY KEY,"
1227 " sta TEXT,"
1228 " reqtype TEXT,"
1229 " attr TEXT"
1230 ");"
1231 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1232
1233 wpa_printf(MSG_DEBUG,
1234 "Adding database table for RADIUS attribute information");
1235 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1236 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1237 sqlite3_free(err);
1238 return -1;
1239 }
1240
1241 return 0;
1242}
1243
1244#endif /* CONFIG_SQLITE */
1245
Dmitry Shmidt04949592012-07-19 12:16:46 -07001246#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247
1248
Sunil Ravi77d572f2023-01-17 23:58:31 +00001249static int hostapd_start_beacon(struct hostapd_data *hapd,
1250 bool flush_old_stations)
1251{
1252 struct hostapd_bss_config *conf = hapd->conf;
1253
1254 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1255 return -1;
1256
1257 if (flush_old_stations && !conf->start_disabled &&
1258 conf->broadcast_deauth) {
1259 u8 addr[ETH_ALEN];
1260
1261 /* Should any previously associated STA not have noticed that
1262 * the AP had stopped and restarted, send one more
1263 * deauthentication notification now that the AP is ready to
1264 * operate. */
1265 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1266 "Deauthenticate all stations at BSS start");
1267 os_memset(addr, 0xff, ETH_ALEN);
1268 hostapd_drv_sta_deauth(hapd, addr,
1269 WLAN_REASON_PREV_AUTH_NOT_VALID);
1270 }
1271
1272 if (hapd->driver && hapd->driver->set_operstate)
1273 hapd->driver->set_operstate(hapd->drv_priv, 1);
1274
1275 return 0;
1276}
1277
1278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279/**
1280 * hostapd_setup_bss - Per-BSS setup (initialization)
1281 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001282 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1283 * but interface may exist
Sunil Ravi77d572f2023-01-17 23:58:31 +00001284 * @start_beacon: Whether Beacon frame template should be configured and
1285 * transmission of Beaconf rames started at this time. This is used when
1286 * MBSSID element is enabled where the information regarding all BSSes
1287 * should be retrieved before configuring the Beacon frame template. The
1288 * calling functions are responsible for configuring the Beacon frame
1289 * explicitly if this is set to false.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 *
1291 * This function is used to initialize all per-BSS data structures and
1292 * resources. This gets called in a loop for each BSS when an interface is
1293 * initialized. Most of the modules that are initialized here will be
1294 * deinitialized in hostapd_cleanup().
1295 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001296static int hostapd_setup_bss(struct hostapd_data *hapd, int first,
1297 bool start_beacon)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298{
1299 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001300 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 int ssid_len, set_ssid;
1302 char force_ifname[IFNAMSIZ];
1303 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001304 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305
Sunil Ravi88611412024-06-28 17:34:56 +00001306 if (hapd->mld_first_bss)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001307 wpa_printf(MSG_DEBUG,
1308 "MLD: %s: Setting non-first BSS", __func__);
1309
Dmitry Shmidt54605472013-11-08 11:10:19 -08001310 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001311 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001312
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001313#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001314 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001315 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1316 return -1;
1317 }
1318#endif /* EAP_SERVER_TNC */
1319
Dmitry Shmidt54605472013-11-08 11:10:19 -08001320 if (hapd->started) {
1321 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001322 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001323 return -1;
1324 }
1325 hapd->started = 1;
1326
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001327 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001328 u8 *addr = hapd->own_addr;
1329
1330 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001332 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333
1334 if (hostapd_mac_comp(hapd->own_addr,
1335 hapd->iface->bss[0]->own_addr) ==
1336 0) {
1337 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1338 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001339 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 return -1;
1341 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001342 } else if (hapd->iconf->use_driver_iface_addr) {
1343 addr = NULL;
1344 } else {
1345 /* Allocate the next available BSSID. */
1346 do {
1347 inc_byte_array(hapd->own_addr, ETH_ALEN);
1348 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 }
1350
Dmitry Shmidt54605472013-11-08 11:10:19 -08001351 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001353 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001355 conf->bridge[0] ? conf->bridge : NULL,
1356 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1358 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001359 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 return -1;
1361 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001362
1363 if (!addr)
1364 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 }
1366
1367 if (conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -08001368 conf->wmm_enabled = hapd->iconf->ieee80211n |
1369 hapd->iconf->ieee80211ax;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001371#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001372 if (is_zero_ether_addr(conf->r1_key_holder))
1373 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001374#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001375
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001376#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001377 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001378 flush_old_stations = 0;
1379#endif /* CONFIG_MESH */
1380
1381 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001382 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383 hostapd_set_privacy(hapd, 0);
1384
Hai Shalomfdcde762020-04-02 11:19:20 -07001385#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001386 if (!hostapd_drv_nl80211(hapd))
1387 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001388 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001390#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391
1392 /*
1393 * Fetch the SSID from the system and use it or,
1394 * if one was specified in the config file, verify they
1395 * match.
1396 */
1397 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1398 if (ssid_len < 0) {
1399 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1400 return -1;
1401 }
1402 if (conf->ssid.ssid_set) {
1403 /*
1404 * If SSID is specified in the config file and it differs
1405 * from what is being used then force installation of the
1406 * new SSID.
1407 */
1408 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1409 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1410 } else {
1411 /*
1412 * No SSID in the config file; just use the one we got
1413 * from the system.
1414 */
1415 set_ssid = 0;
1416 conf->ssid.ssid_len = ssid_len;
1417 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 }
1419
Hai Shalomfdcde762020-04-02 11:19:20 -07001420 /*
1421 * Short SSID calculation is identical to FCS and it is defined in
1422 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1423 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001424 conf->ssid.short_ssid = ieee80211_crc32(conf->ssid.ssid,
1425 conf->ssid.ssid_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001428 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001429 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001430 conf->iface, MAC2STR(hapd->own_addr),
1431 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 }
1433
1434 if (hostapd_setup_wpa_psk(conf)) {
1435 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1436 return -1;
1437 }
1438
1439 /* Set SSID for the kernel driver (to be used in beacon and probe
1440 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001441 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 conf->ssid.ssid_len)) {
1443 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1444 return -1;
1445 }
1446
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001447 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 conf->radius->msg_dumps = 1;
1449#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001450
1451#ifdef CONFIG_SQLITE
1452 if (conf->radius_req_attr_sqlite) {
1453 if (sqlite3_open(conf->radius_req_attr_sqlite,
1454 &hapd->rad_attr_db)) {
1455 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1456 conf->radius_req_attr_sqlite);
1457 return -1;
1458 }
1459
1460 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1461 conf->radius_req_attr_sqlite);
1462 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1463 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1464 return -1;
1465 }
1466#endif /* CONFIG_SQLITE */
1467
Sunil Ravi88611412024-06-28 17:34:56 +00001468 if (!hapd->mld_first_bss) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001469 hapd->radius = radius_client_init(hapd, conf->radius);
1470 if (!hapd->radius) {
1471 wpa_printf(MSG_ERROR,
1472 "RADIUS client initialization failed.");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001473 return -1;
1474 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001475
1476 if (conf->radius_das_port) {
1477 struct radius_das_conf das_conf;
1478
1479 os_memset(&das_conf, 0, sizeof(das_conf));
1480 das_conf.port = conf->radius_das_port;
1481 das_conf.shared_secret = conf->radius_das_shared_secret;
1482 das_conf.shared_secret_len =
1483 conf->radius_das_shared_secret_len;
1484 das_conf.client_addr = &conf->radius_das_client_addr;
1485 das_conf.time_window = conf->radius_das_time_window;
1486 das_conf.require_event_timestamp =
1487 conf->radius_das_require_event_timestamp;
1488 das_conf.require_message_authenticator =
1489 conf->radius_das_require_message_authenticator;
1490 das_conf.ctx = hapd;
1491 das_conf.disconnect = hostapd_das_disconnect;
1492 das_conf.coa = hostapd_das_coa;
1493 hapd->radius_das = radius_das_init(&das_conf);
1494 if (!hapd->radius_das) {
1495 wpa_printf(MSG_ERROR,
1496 "RADIUS DAS initialization failed.");
1497 return -1;
1498 }
1499 }
1500 } else {
1501 wpa_printf(MSG_DEBUG,
1502 "MLD: Using RADIUS client of the first BSS");
Sunil Ravi88611412024-06-28 17:34:56 +00001503 hapd->radius = hapd->mld_first_bss->radius;
1504 hapd->radius_das = hapd->mld_first_bss->radius_das;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001505 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506#endif /* CONFIG_NO_RADIUS */
1507
1508 if (hostapd_acl_init(hapd)) {
1509 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1510 return -1;
1511 }
1512 if (hostapd_init_wps(hapd, conf))
1513 return -1;
1514
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001515#ifdef CONFIG_DPP
1516 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1517 if (!hapd->gas)
1518 return -1;
1519 if (hostapd_dpp_init(hapd))
1520 return -1;
1521#endif /* CONFIG_DPP */
1522
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001523#ifdef CONFIG_NAN_USD
1524 if (hostapd_nan_usd_init(hapd) < 0)
1525 return -1;
1526#endif /* CONFIG_NAN_USD */
1527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 if (authsrv_init(hapd) < 0)
1529 return -1;
1530
1531 if (ieee802_1x_init(hapd)) {
1532 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1533 return -1;
1534 }
1535
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001536 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 return -1;
1538
1539 if (accounting_init(hapd)) {
1540 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1541 return -1;
1542 }
1543
Dmitry Shmidt04949592012-07-19 12:16:46 -07001544#ifdef CONFIG_INTERWORKING
1545 if (gas_serv_init(hapd)) {
1546 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1547 return -1;
1548 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001549
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001550 if (conf->qos_map_set_len &&
1551 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1552 conf->qos_map_set_len)) {
1553 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 return -1;
1555 }
Sunil Ravi88611412024-06-28 17:34:56 +00001556#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001557
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001558 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1559 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1560 return -1;
1561 }
1562
Sunil Ravi036cec52023-03-29 11:35:17 -07001563 if (conf->bridge[0]) {
1564 /* Set explicitly configured bridge parameters that might have
1565 * been lost if the interface has been removed out of the
1566 * bridge. */
1567
1568 /* multicast to unicast on bridge ports */
1569 if (conf->bridge_multicast_to_unicast)
1570 hostapd_drv_br_port_set_attr(
1571 hapd, DRV_BR_PORT_ATTR_MCAST2UCAST, 1);
1572
1573 /* hairpin mode */
1574 if (conf->bridge_hairpin)
1575 hostapd_drv_br_port_set_attr(
1576 hapd, DRV_BR_PORT_ATTR_HAIRPIN_MODE, 1);
1577 }
1578
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001579 if (conf->proxy_arp) {
1580 if (x_snoop_init(hapd)) {
1581 wpa_printf(MSG_ERROR,
1582 "Generic snooping infrastructure initialization failed");
1583 return -1;
1584 }
1585
1586 if (dhcp_snoop_init(hapd)) {
1587 wpa_printf(MSG_ERROR,
1588 "DHCP snooping initialization failed");
1589 return -1;
1590 }
1591
1592 if (ndisc_snoop_init(hapd)) {
1593 wpa_printf(MSG_ERROR,
1594 "Neighbor Discovery snooping initialization failed");
1595 return -1;
1596 }
1597 }
1598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1600 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1601 return -1;
1602 }
1603
Sunil Ravi640215c2023-06-28 23:08:09 +00001604 if (start_beacon && hostapd_start_beacon(hapd, flush_old_stations) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001605 return -1;
1606
Sunil Ravi640215c2023-06-28 23:08:09 +00001607 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1608 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609
1610 return 0;
1611}
1612
1613
1614static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1615{
1616 struct hostapd_data *hapd = iface->bss[0];
1617 int i;
1618 struct hostapd_tx_queue_params *p;
1619
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001620#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001621 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001622 return;
1623#endif /* CONFIG_MESH */
1624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 for (i = 0; i < NUM_TX_QUEUES; i++) {
1626 p = &iface->conf->tx_queue[i];
1627
1628 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1629 p->cwmax, p->burst)) {
1630 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1631 "parameters for queue %d.", i);
1632 /* Continue anyway */
1633 }
1634 }
1635}
1636
1637
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001638static int hostapd_set_acl_list(struct hostapd_data *hapd,
1639 struct mac_acl_entry *mac_acl,
1640 int n_entries, u8 accept_acl)
1641{
1642 struct hostapd_acl_params *acl_params;
1643 int i, err;
1644
1645 acl_params = os_zalloc(sizeof(*acl_params) +
1646 (n_entries * sizeof(acl_params->mac_acl[0])));
1647 if (!acl_params)
1648 return -ENOMEM;
1649
1650 for (i = 0; i < n_entries; i++)
1651 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1652 ETH_ALEN);
1653
1654 acl_params->acl_policy = accept_acl;
1655 acl_params->num_mac_acl = n_entries;
1656
1657 err = hostapd_drv_set_acl(hapd, acl_params);
1658
1659 os_free(acl_params);
1660
1661 return err;
1662}
1663
1664
Sunil Ravia04bd252022-05-02 22:54:18 -07001665int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001666{
1667 struct hostapd_config *conf = hapd->iconf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001668 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001669 u8 accept_acl;
1670
1671 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001672 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001673
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001674 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001675 accept_acl = 1;
1676 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1677 conf->bss[0]->num_accept_mac,
1678 accept_acl);
1679 if (err) {
1680 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001681 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001682 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001683 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001684 accept_acl = 0;
1685 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1686 conf->bss[0]->num_deny_mac,
1687 accept_acl);
1688 if (err) {
1689 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001690 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001691 }
1692 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001693 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001694}
1695
1696
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001697static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1698{
1699 if (!hapd->iface->interfaces ||
1700 !hapd->iface->interfaces->ctrl_iface_init)
1701 return 0;
1702
1703 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1704 wpa_printf(MSG_ERROR,
1705 "Failed to setup control interface for %s",
1706 hapd->conf->iface);
1707 return -1;
1708 }
1709
1710 return 0;
1711}
1712
1713
1714static int start_ctrl_iface(struct hostapd_iface *iface)
1715{
1716 size_t i;
1717
1718 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1719 return 0;
1720
1721 for (i = 0; i < iface->num_bss; i++) {
1722 struct hostapd_data *hapd = iface->bss[i];
1723 if (iface->interfaces->ctrl_iface_init(hapd)) {
1724 wpa_printf(MSG_ERROR,
1725 "Failed to setup control interface for %s",
1726 hapd->conf->iface);
1727 return -1;
1728 }
1729 }
1730
1731 return 0;
1732}
1733
1734
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001735/* When NO_IR flag is set and AP is stopped, clean up BSS parameters without
1736 * deinitializing the driver and the control interfaces. A subsequent
1737 * REG_CHANGE event can bring the AP back up.
1738 */
1739static void hostapd_no_ir_cleanup(struct hostapd_data *bss)
1740{
1741 hostapd_bss_deinit_no_free(bss);
1742 hostapd_free_hapd_data(bss);
1743 hostapd_cleanup_iface_partial(bss->iface);
1744}
1745
1746
1747static int hostapd_no_ir_channel_list_updated(struct hostapd_iface *iface,
1748 void *ctx)
1749{
1750 bool all_no_ir, is_6ghz;
1751 int i, j;
1752 struct hostapd_hw_modes *mode = NULL;
1753
1754 if (hostapd_get_hw_features(iface))
1755 return 0;
1756
1757 all_no_ir = true;
1758 is_6ghz = false;
1759
1760 for (i = 0; i < iface->num_hw_features; i++) {
1761 mode = &iface->hw_features[i];
1762
1763 if (mode->mode == iface->conf->hw_mode) {
1764 if (iface->freq > 0 &&
1765 !hw_mode_get_channel(mode, iface->freq, NULL)) {
1766 mode = NULL;
1767 continue;
1768 }
1769
1770 for (j = 0; j < mode->num_channels; j++) {
1771 if (!(mode->channels[j].flag &
1772 HOSTAPD_CHAN_NO_IR))
1773 all_no_ir = false;
1774
1775 if (is_6ghz_freq(mode->channels[j].freq))
1776 is_6ghz = true;
1777 }
1778 break;
1779 }
1780 }
1781
1782 if (!mode || !is_6ghz)
1783 return 0;
1784 iface->current_mode = mode;
1785
1786 if (iface->state == HAPD_IFACE_ENABLED) {
1787 if (!all_no_ir) {
1788 struct hostapd_channel_data *chan;
1789
1790 chan = hw_get_channel_freq(iface->current_mode->mode,
1791 iface->freq, NULL,
1792 iface->hw_features,
1793 iface->num_hw_features);
1794
1795 if (!chan) {
1796 wpa_printf(MSG_ERROR,
1797 "NO_IR: Could not derive chan from freq");
1798 return 0;
1799 }
1800
1801 if (!(chan->flag & HOSTAPD_CHAN_NO_IR))
1802 return 0;
1803 wpa_printf(MSG_DEBUG,
1804 "NO_IR: The current channel has NO_IR flag now, stop AP.");
1805 } else {
1806 wpa_printf(MSG_DEBUG,
1807 "NO_IR: All chan in new chanlist are NO_IR, stop AP.");
1808 }
1809
1810 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
1811 iface->is_no_ir = true;
1812 hostapd_drv_stop_ap(iface->bss[0]);
1813 hostapd_no_ir_cleanup(iface->bss[0]);
1814 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
1815 } else if (iface->state == HAPD_IFACE_NO_IR) {
1816 if (all_no_ir) {
1817 wpa_printf(MSG_DEBUG,
1818 "NO_IR: AP in NO_IR and all chan in the new chanlist are NO_IR. Ignore");
1819 return 0;
1820 }
1821
1822 if (!iface->conf->acs) {
1823 struct hostapd_channel_data *chan;
1824
1825 chan = hw_get_channel_freq(iface->current_mode->mode,
1826 iface->freq, NULL,
1827 iface->hw_features,
1828 iface->num_hw_features);
1829 if (!chan) {
1830 wpa_printf(MSG_ERROR,
1831 "NO_IR: Could not derive chan from freq");
1832 return 0;
1833 }
1834
1835 /* If the last operating channel is NO_IR, trigger ACS.
1836 */
1837 if (chan->flag & HOSTAPD_CHAN_NO_IR) {
1838 iface->freq = 0;
1839 iface->conf->channel = 0;
1840 if (acs_init(iface) != HOSTAPD_CHAN_ACS)
1841 wpa_printf(MSG_ERROR,
1842 "NO_IR: Could not start ACS");
1843 return 0;
1844 }
1845 }
1846
1847 setup_interface2(iface);
1848 }
1849
1850 return 0;
1851}
1852
1853
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001854static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1855{
1856 struct hostapd_iface *iface = eloop_ctx;
1857
1858 if (!iface->wait_channel_update) {
1859 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1860 return;
1861 }
1862
1863 /*
1864 * It is possible that the existing channel list is acceptable, so try
1865 * to proceed.
1866 */
1867 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1868 setup_interface2(iface);
1869}
1870
1871
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001872void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001873{
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001874 if (initiator == REGDOM_SET_BY_DRIVER) {
1875 hostapd_for_each_interface(iface->interfaces,
1876 hostapd_no_ir_channel_list_updated,
1877 NULL);
1878 return;
1879 }
1880
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001881 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001882 return;
1883
1884 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1885 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1886 setup_interface2(iface);
1887}
1888
1889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890static int setup_interface(struct hostapd_iface *iface)
1891{
1892 struct hostapd_data *hapd = iface->bss[0];
1893 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001894
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001895 /*
1896 * It is possible that setup_interface() is called after the interface
1897 * was disabled etc., in which case driver_ap_teardown is possibly set
1898 * to 1. Clear it here so any other key/station deletion, which is not
1899 * part of a teardown flow, would also call the relevant driver
1900 * callbacks.
1901 */
1902 iface->driver_ap_teardown = 0;
1903
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001904 if (!iface->phy[0]) {
1905 const char *phy = hostapd_drv_get_radio_name(hapd);
1906 if (phy) {
1907 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1908 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1909 }
1910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911
1912 /*
1913 * Make sure that all BSSes get configured with a pointer to the same
1914 * driver interface.
1915 */
1916 for (i = 1; i < iface->num_bss; i++) {
1917 iface->bss[i]->driver = hapd->driver;
1918 iface->bss[i]->drv_priv = hapd->drv_priv;
1919 }
1920
1921 if (hostapd_validate_bssid_configuration(iface))
1922 return -1;
1923
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001924 /*
1925 * Initialize control interfaces early to allow external monitoring of
1926 * channel setup operations that may take considerable amount of time
1927 * especially for DFS cases.
1928 */
1929 if (start_ctrl_iface(iface))
1930 return -1;
1931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001933 char country[4], previous_country[4];
1934
1935 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1936 if (hostapd_get_country(hapd, previous_country) < 0)
1937 previous_country[0] = '\0';
1938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001939 os_memcpy(country, hapd->iconf->country, 3);
1940 country[3] = '\0';
1941 if (hostapd_set_country(hapd, country) < 0) {
1942 wpa_printf(MSG_ERROR, "Failed to set country code");
1943 return -1;
1944 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001945
1946 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1947 previous_country, country);
1948
1949 if (os_strncmp(previous_country, country, 2) != 0) {
1950 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1951 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001952 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001953 channel_list_update_timeout,
1954 iface, NULL);
1955 return 0;
1956 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 }
1958
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001959 return setup_interface2(iface);
1960}
1961
1962
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001963static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1964{
1965 int freq, i, j;
1966
1967 if (!iface->conf->channel)
1968 return 0;
1969 if (iface->conf->op_class) {
1970 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1971 iface->conf->channel);
1972 if (freq < 0) {
1973 wpa_printf(MSG_INFO,
1974 "Could not convert op_class %u channel %u to operating frequency",
1975 iface->conf->op_class, iface->conf->channel);
1976 return -1;
1977 }
1978 iface->freq = freq;
1979 return 0;
1980 }
1981
1982 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1983 * op_class parameter. Select a matching channel from the configured
1984 * mode using the channel parameter for these cases.
1985 */
1986 for (j = 0; j < iface->num_hw_features; j++) {
1987 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1988
1989 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1990 iface->conf->hw_mode != mode->mode)
1991 continue;
1992 for (i = 0; i < mode->num_channels; i++) {
1993 struct hostapd_channel_data *chan = &mode->channels[i];
1994
1995 if (chan->chan == iface->conf->channel &&
1996 !is_6ghz_freq(chan->freq)) {
1997 iface->freq = chan->freq;
1998 return 0;
1999 }
2000 }
2001 }
2002
2003 wpa_printf(MSG_INFO, "Could not determine operating frequency");
2004 return -1;
2005}
2006
2007
Hai Shaloma20dcd72022-02-04 13:43:00 -08002008static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
2009{
Sunil Ravi640215c2023-06-28 23:08:09 +00002010 int bw;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002011
2012 if (!is_6ghz_op_class(iface->conf->op_class))
2013 return;
2014
Sunil Ravi640215c2023-06-28 23:08:09 +00002015 bw = op_class_to_bandwidth(iface->conf->op_class);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002016 /* Assign the secondary channel if absent in config for
2017 * bandwidths > 20 MHz */
Sunil Ravi640215c2023-06-28 23:08:09 +00002018 if (bw >= 40 && !iface->conf->secondary_channel) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002019 if (((iface->conf->channel - 1) / 4) % 2)
2020 iface->conf->secondary_channel = -1;
2021 else
2022 iface->conf->secondary_channel = 1;
2023 }
2024}
2025
2026
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002027static int setup_interface2(struct hostapd_iface *iface)
2028{
2029 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002030 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 if (hostapd_get_hw_features(iface)) {
2033 /* Not all drivers support this yet, so continue without hw
2034 * feature data. */
2035 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002036 int ret;
2037
Sunil Ravi88611412024-06-28 17:34:56 +00002038 if (iface->conf->acs) {
Sunil Ravi036cec52023-03-29 11:35:17 -07002039 iface->freq = 0;
2040 iface->conf->channel = 0;
2041 }
2042
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002043 ret = configured_fixed_chan_to_freq(iface);
2044 if (ret < 0)
2045 goto fail;
2046
2047 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002048 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002049
2050 ch_width = op_class_to_ch_width(iface->conf->op_class);
2051 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002052 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002053 }
2054
2055 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 if (ret < 0) {
2057 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
2058 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002059 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002061 if (ret == 1) {
2062 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
2063 return 0;
2064 }
Hai Shalomc3565922019-10-28 11:58:20 -07002065 ret = hostapd_check_edmg_capab(iface);
2066 if (ret < 0)
2067 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08002068 ret = hostapd_check_he_6ghz_capab(iface);
2069 if (ret < 0)
2070 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 ret = hostapd_check_ht_capab(iface);
2072 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002073 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 if (ret == 1) {
2075 wpa_printf(MSG_DEBUG, "Interface initialization will "
2076 "be completed in a callback");
2077 return 0;
2078 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002079
2080 if (iface->conf->ieee80211h)
2081 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 }
2083 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002084
2085fail:
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002086 if (iface->is_no_ir) {
2087 /* If AP is in NO_IR state, it can be reenabled by the driver
2088 * regulatory update and EVENT_CHANNEL_LIST_CHANGED. */
2089 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2090 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2091 return 0;
2092 }
2093
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002094 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2095 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2096 if (iface->interfaces && iface->interfaces->terminate_on_error)
2097 eloop_terminate();
2098 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099}
2100
2101
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002102#ifdef CONFIG_FST
2103
2104static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
2105{
2106 struct hostapd_data *hapd = ctx;
2107
2108 return hapd->own_addr;
2109}
2110
2111
2112static void fst_hostapd_get_channel_info_cb(void *ctx,
2113 enum hostapd_hw_mode *hw_mode,
2114 u8 *channel)
2115{
2116 struct hostapd_data *hapd = ctx;
2117
2118 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
2119}
2120
2121
Sunil8cd6f4d2022-06-28 18:40:46 +00002122static int fst_hostapd_get_hw_modes_cb(void *ctx,
2123 struct hostapd_hw_modes **modes)
2124{
2125 struct hostapd_data *hapd = ctx;
2126
2127 *modes = hapd->iface->hw_features;
2128 return hapd->iface->num_hw_features;
2129}
2130
2131
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002132static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
2133{
2134 struct hostapd_data *hapd = ctx;
2135
2136 if (hapd->iface->fst_ies != fst_ies) {
2137 hapd->iface->fst_ies = fst_ies;
2138 if (ieee802_11_set_beacon(hapd))
2139 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
2140 }
2141}
2142
2143
2144static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
2145 struct wpabuf *buf)
2146{
2147 struct hostapd_data *hapd = ctx;
2148
2149 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
2150 wpabuf_head(buf), wpabuf_len(buf));
2151}
2152
2153
2154static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
2155{
2156 struct hostapd_data *hapd = ctx;
2157 struct sta_info *sta = ap_get_sta(hapd, addr);
2158
2159 return sta ? sta->mb_ies : NULL;
2160}
2161
2162
2163static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
2164 const u8 *buf, size_t size)
2165{
2166 struct hostapd_data *hapd = ctx;
2167 struct sta_info *sta = ap_get_sta(hapd, addr);
2168
2169 if (sta) {
2170 struct mb_ies_info info;
2171
2172 if (!mb_ies_info_by_ies(&info, buf, size)) {
2173 wpabuf_free(sta->mb_ies);
2174 sta->mb_ies = mb_ies_by_info(&info);
2175 }
2176 }
2177}
2178
2179
2180static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002181 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002182{
2183 struct sta_info *s = (struct sta_info *) *get_ctx;
2184
2185 if (mb_only) {
2186 for (; s && !s->mb_ies; s = s->next)
2187 ;
2188 }
2189
2190 if (s) {
2191 *get_ctx = (struct fst_get_peer_ctx *) s->next;
2192
2193 return s->addr;
2194 }
2195
2196 *get_ctx = NULL;
2197 return NULL;
2198}
2199
2200
2201static const u8 * fst_hostapd_get_peer_first(void *ctx,
2202 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002203 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002204{
2205 struct hostapd_data *hapd = ctx;
2206
2207 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
2208
2209 return fst_hostapd_get_sta(get_ctx, mb_only);
2210}
2211
2212
2213static const u8 * fst_hostapd_get_peer_next(void *ctx,
2214 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002215 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002216{
2217 return fst_hostapd_get_sta(get_ctx, mb_only);
2218}
2219
2220
2221void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
2222 struct fst_wpa_obj *iface_obj)
2223{
Sunil8cd6f4d2022-06-28 18:40:46 +00002224 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002225 iface_obj->ctx = hapd;
2226 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
2227 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00002228 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002229 iface_obj->set_ies = fst_hostapd_set_ies_cb;
2230 iface_obj->send_action = fst_hostapd_send_action_cb;
2231 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
2232 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
2233 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
2234 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
2235}
2236
2237#endif /* CONFIG_FST */
2238
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002239#ifdef CONFIG_OWE
2240
2241static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
2242{
2243 struct hostapd_data *hapd = ctx;
2244 size_t i;
2245
2246 for (i = 0; i < iface->num_bss; i++) {
2247 struct hostapd_data *bss = iface->bss[i];
2248
2249 if (os_strcmp(hapd->conf->owe_transition_ifname,
2250 bss->conf->iface) != 0)
2251 continue;
2252
2253 wpa_printf(MSG_DEBUG,
2254 "OWE: ifname=%s found transition mode ifname=%s BSSID "
2255 MACSTR " SSID %s",
2256 hapd->conf->iface, bss->conf->iface,
2257 MAC2STR(bss->own_addr),
2258 wpa_ssid_txt(bss->conf->ssid.ssid,
2259 bss->conf->ssid.ssid_len));
2260 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2261 is_zero_ether_addr(bss->own_addr))
2262 continue;
2263
2264 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2265 ETH_ALEN);
2266 os_memcpy(hapd->conf->owe_transition_ssid,
2267 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2268 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2269 wpa_printf(MSG_DEBUG,
2270 "OWE: Copied transition mode information");
2271 return 1;
2272 }
2273
2274 return 0;
2275}
2276
2277
2278int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2279{
2280 if (hapd->conf->owe_transition_ssid_len > 0 &&
2281 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2282 return 0;
2283
2284 /* Find transition mode SSID/BSSID information from a BSS operated by
2285 * this hostapd instance. */
2286 if (!hapd->iface->interfaces ||
2287 !hapd->iface->interfaces->for_each_interface)
2288 return hostapd_owe_iface_iter(hapd->iface, hapd);
2289 else
2290 return hapd->iface->interfaces->for_each_interface(
2291 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2292}
2293
2294
2295static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2296{
2297 size_t i;
2298
2299 for (i = 0; i < iface->num_bss; i++) {
2300 struct hostapd_data *bss = iface->bss[i];
2301 int res;
2302
2303 if (!bss->conf->owe_transition_ifname[0])
2304 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07002305 if (bss->iface->state != HAPD_IFACE_ENABLED) {
2306 wpa_printf(MSG_DEBUG,
2307 "OWE: Interface %s state %s - defer beacon update",
2308 bss->conf->iface,
2309 hostapd_state_text(bss->iface->state));
2310 continue;
2311 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002312 res = hostapd_owe_trans_get_info(bss);
2313 if (res == 0)
2314 continue;
2315 wpa_printf(MSG_DEBUG,
2316 "OWE: Matching transition mode interface enabled - update beacon data for %s",
2317 bss->conf->iface);
2318 ieee802_11_set_beacon(bss);
2319 }
2320
2321 return 0;
2322}
2323
2324#endif /* CONFIG_OWE */
2325
2326
2327static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2328{
2329#ifdef CONFIG_OWE
2330 /* Check whether the enabled BSS can complete OWE transition mode
2331 * configuration for any pending interface. */
2332 if (!iface->interfaces ||
2333 !iface->interfaces->for_each_interface)
2334 hostapd_owe_iface_iter2(iface, NULL);
2335 else
2336 iface->interfaces->for_each_interface(
2337 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2338#endif /* CONFIG_OWE */
2339}
2340
2341
Roshan Pius3a1667e2018-07-03 15:17:14 -07002342static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2343 void *timeout_ctx)
2344{
2345 struct hostapd_iface *iface = eloop_ctx;
2346 struct hostapd_data *hapd;
2347
2348 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2349 return;
2350 hapd = iface->bss[0];
2351 if (hapd->setup_complete_cb)
2352 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2353}
2354
2355
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002356static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2357 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358{
2359 struct hostapd_data *hapd = iface->bss[0];
2360 size_t j;
2361 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002362 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002363 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002365 if (err)
2366 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367
2368 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002369 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002370#ifdef NEED_AP_MLME
2371 int res;
2372#endif /* NEED_AP_MLME */
2373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2375 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002376 hostapd_hw_mode_txt(iface->conf->hw_mode),
2377 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378
Dmitry Shmidt051af732013-10-22 13:52:46 -07002379#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002380 /* Handle DFS only if it is not offloaded to the driver */
2381 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2382 /* Check DFS */
2383 res = hostapd_handle_dfs(iface);
2384 if (res <= 0) {
2385 if (res < 0)
2386 goto fail;
2387 return res;
2388 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002389 } else {
2390 /* If DFS is offloaded to the driver */
2391 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2392 if (res_dfs_offload <= 0) {
2393 if (res_dfs_offload < 0)
2394 goto fail;
2395 } else {
2396 wpa_printf(MSG_DEBUG,
2397 "Proceed with AP/channel setup");
2398 /*
2399 * If this is a DFS channel, move to completing
2400 * AP setup.
2401 */
2402 if (res_dfs_offload == 1)
2403 goto dfs_offload;
2404 /* Otherwise fall through. */
2405 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002406 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002407#endif /* NEED_AP_MLME */
2408
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002409#ifdef CONFIG_MESH
2410 if (iface->mconf != NULL) {
2411 wpa_printf(MSG_DEBUG,
2412 "%s: Mesh configuration will be applied while joining the mesh network",
2413 iface->bss[0]->conf->iface);
2414 delay_apply_cfg = 1;
2415 }
2416#endif /* CONFIG_MESH */
2417
2418 if (!delay_apply_cfg &&
2419 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002421 hapd->iconf->enable_edmg,
2422 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002424 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002425 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002426 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002427 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002428 hostapd_get_oper_chwidth(hapd->iconf),
2429 hostapd_get_oper_centr_freq_seg0_idx(
2430 hapd->iconf),
2431 hostapd_get_oper_centr_freq_seg1_idx(
2432 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433 wpa_printf(MSG_ERROR, "Could not set channel for "
2434 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002435 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002436 }
2437 }
2438
2439 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002440 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2442 "table.");
2443 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2444 HOSTAPD_LEVEL_WARNING,
2445 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002446 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 }
2448 }
2449
Hai Shalom021b0b52019-04-10 11:17:58 -07002450 if (hapd->iconf->rts_threshold >= -1 &&
2451 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2452 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2454 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002455 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 }
2457
Hai Shalom021b0b52019-04-10 11:17:58 -07002458 if (hapd->iconf->fragm_threshold >= -1 &&
2459 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2460 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002461 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2462 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002463 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 }
2465
2466 prev_addr = hapd->own_addr;
2467
2468 for (j = 0; j < iface->num_bss; j++) {
2469 hapd = iface->bss[j];
2470 if (j)
2471 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002472 if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002473 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002474 hapd = iface->bss[j];
2475 hostapd_bss_deinit_no_free(hapd);
2476 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002477 if (j == 0)
2478 break;
2479 j--;
2480 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002481 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002482 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002483 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484 prev_addr = hapd->own_addr;
2485 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002486
2487 if (hapd->iconf->mbssid) {
2488 for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2489 hapd = iface->bss[j];
2490 if (hostapd_start_beacon(hapd, true)) {
2491 for (;;) {
2492 hapd = iface->bss[j];
2493 hostapd_bss_deinit_no_free(hapd);
2494 hostapd_free_hapd_data(hapd);
2495 if (j == 0)
2496 break;
2497 j--;
2498 }
2499 goto fail;
2500 }
2501 }
2502 }
2503
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002504 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505
2506 hostapd_tx_queue_params(iface);
2507
2508 ap_list_init(iface);
2509
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002510 hostapd_set_acl(hapd);
2511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 if (hostapd_driver_commit(hapd) < 0) {
2513 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2514 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002515 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516 }
2517
Jouni Malinen87fd2792011-05-16 18:35:42 +03002518 /*
2519 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2520 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2521 * mode), the interface is up only after driver_commit, so initialize
2522 * WPS after driver_commit.
2523 */
2524 for (j = 0; j < iface->num_bss; j++) {
2525 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002526 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002527 }
2528
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002529 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2530 !res_dfs_offload) {
2531 /*
2532 * If freq is DFS, and DFS is offloaded to the driver, then wait
2533 * for CAC to complete.
2534 */
2535 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2536 return res_dfs_offload;
2537 }
2538
2539#ifdef NEED_AP_MLME
2540dfs_offload:
2541#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002542
2543#ifdef CONFIG_FST
2544 if (hapd->iconf->fst_cfg.group_id[0]) {
2545 struct fst_wpa_obj iface_obj;
2546
2547 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2548 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2549 &iface_obj, &hapd->iconf->fst_cfg);
2550 if (!iface->fst) {
2551 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2552 hapd->iconf->fst_cfg.group_id);
2553 goto fail;
2554 }
2555 }
2556#endif /* CONFIG_FST */
2557
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002558 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002559 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002560 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002561 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562 if (hapd->setup_complete_cb)
2563 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2564
Hai Shalom60840252021-02-19 19:02:11 -08002565#ifdef CONFIG_MESH
2566 if (delay_apply_cfg && !iface->mconf) {
2567 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2568 goto fail;
2569 }
2570#endif /* CONFIG_MESH */
2571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2573 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002574 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2575 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002577 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002578 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002579
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002580 if (iface->interfaces && iface->interfaces->count > 1)
2581 ieee802_11_set_beacons(iface);
2582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002584
2585fail:
2586 wpa_printf(MSG_ERROR, "Interface initialization failed");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002587
2588 if (iface->is_no_ir) {
2589 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2590 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2591 return 0;
2592 }
2593
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002594 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2595 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002596#ifdef CONFIG_FST
2597 if (iface->fst) {
2598 fst_detach(iface->fst);
2599 iface->fst = NULL;
2600 }
2601#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002602
2603 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002604 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002605 } else if (hapd->setup_complete_cb) {
2606 /*
2607 * Calling hapd->setup_complete_cb directly may cause iface
2608 * deinitialization which may be accessed later by the caller.
2609 */
2610 eloop_register_timeout(0, 0,
2611 hostapd_interface_setup_failure_handler,
2612 iface, NULL);
2613 }
2614
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002615 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616}
2617
2618
2619/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002620 * hostapd_setup_interface_complete - Complete interface setup
2621 *
2622 * This function is called when previous steps in the interface setup has been
2623 * completed. This can also start operations, e.g., DFS, that will require
2624 * additional processing before interface is ready to be enabled. Such
2625 * operations will call this function from eloop callbacks when finished.
2626 */
2627int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2628{
2629 struct hapd_interfaces *interfaces = iface->interfaces;
2630 struct hostapd_data *hapd = iface->bss[0];
2631 unsigned int i;
2632 int not_ready_in_sync_ifaces = 0;
2633
2634 if (!iface->need_to_start_in_sync)
2635 return hostapd_setup_interface_complete_sync(iface, err);
2636
2637 if (err) {
2638 wpa_printf(MSG_ERROR, "Interface initialization failed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002639 iface->need_to_start_in_sync = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002640
2641 if (iface->is_no_ir) {
2642 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2643 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2644 return 0;
2645 }
2646
2647 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002648 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2649 if (interfaces && interfaces->terminate_on_error)
2650 eloop_terminate();
2651 return -1;
2652 }
2653
2654 if (iface->ready_to_start_in_sync) {
2655 /* Already in ready and waiting. should never happpen */
2656 return 0;
2657 }
2658
2659 for (i = 0; i < interfaces->count; i++) {
2660 if (interfaces->iface[i]->need_to_start_in_sync &&
2661 !interfaces->iface[i]->ready_to_start_in_sync)
2662 not_ready_in_sync_ifaces++;
2663 }
2664
2665 /*
2666 * Check if this is the last interface, if yes then start all the other
2667 * waiting interfaces. If not, add this interface to the waiting list.
2668 */
2669 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2670 /*
2671 * If this interface went through CAC, do not synchronize, just
2672 * start immediately.
2673 */
2674 iface->need_to_start_in_sync = 0;
2675 wpa_printf(MSG_INFO,
2676 "%s: Finished CAC - bypass sync and start interface",
2677 iface->bss[0]->conf->iface);
2678 return hostapd_setup_interface_complete_sync(iface, err);
2679 }
2680
2681 if (not_ready_in_sync_ifaces > 1) {
2682 /* need to wait as there are other interfaces still coming up */
2683 iface->ready_to_start_in_sync = 1;
2684 wpa_printf(MSG_INFO,
2685 "%s: Interface waiting to sync with other interfaces",
2686 iface->bss[0]->conf->iface);
2687 return 0;
2688 }
2689
2690 wpa_printf(MSG_INFO,
2691 "%s: Last interface to sync - starting all interfaces",
2692 iface->bss[0]->conf->iface);
2693 iface->need_to_start_in_sync = 0;
2694 hostapd_setup_interface_complete_sync(iface, err);
2695 for (i = 0; i < interfaces->count; i++) {
2696 if (interfaces->iface[i]->need_to_start_in_sync &&
2697 interfaces->iface[i]->ready_to_start_in_sync) {
2698 hostapd_setup_interface_complete_sync(
2699 interfaces->iface[i], 0);
2700 /* Only once the interfaces are sync started */
2701 interfaces->iface[i]->need_to_start_in_sync = 0;
2702 }
2703 }
2704
2705 return 0;
2706}
2707
2708
2709/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710 * hostapd_setup_interface - Setup of an interface
2711 * @iface: Pointer to interface data.
2712 * Returns: 0 on success, -1 on failure
2713 *
2714 * Initializes the driver interface, validates the configuration,
2715 * and sets driver parameters based on the configuration.
2716 * Flushes old stations, sets the channel, encryption,
2717 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002718 *
2719 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2720 * or DFS operations, this function returns 0 before such operations have been
2721 * completed. The pending operations are registered into eloop and will be
2722 * completed from eloop callbacks. Those callbacks end up calling
2723 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002724 */
2725int hostapd_setup_interface(struct hostapd_iface *iface)
2726{
2727 int ret;
2728
Hai Shaloma20dcd72022-02-04 13:43:00 -08002729 if (!iface->conf)
2730 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 ret = setup_interface(iface);
2732 if (ret) {
2733 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002734 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735 return -1;
2736 }
2737
2738 return 0;
2739}
2740
2741
2742/**
2743 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2744 * @hapd_iface: Pointer to interface data
2745 * @conf: Pointer to per-interface configuration
2746 * @bss: Pointer to per-BSS configuration for this BSS
2747 * Returns: Pointer to allocated BSS data
2748 *
2749 * This function is used to allocate per-BSS data structure. This data will be
2750 * freed after hostapd_cleanup() is called for it during interface
2751 * deinitialization.
2752 */
2753struct hostapd_data *
2754hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2755 struct hostapd_config *conf,
2756 struct hostapd_bss_config *bss)
2757{
2758 struct hostapd_data *hapd;
2759
2760 hapd = os_zalloc(sizeof(*hapd));
2761 if (hapd == NULL)
2762 return NULL;
2763
2764 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2765 hapd->iconf = conf;
2766 hapd->conf = bss;
2767 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002768 if (conf)
2769 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002770 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002771 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002772 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002773 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002774#ifdef CONFIG_IEEE80211R_AP
2775 dl_list_init(&hapd->l2_queue);
2776 dl_list_init(&hapd->l2_oui_queue);
2777#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002778#ifdef CONFIG_SAE
2779 dl_list_init(&hapd->sae_commit_queue);
2780#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781
2782 return hapd;
2783}
2784
2785
Dmitry Shmidt54605472013-11-08 11:10:19 -08002786static void hostapd_bss_deinit(struct hostapd_data *hapd)
2787{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002788 if (!hapd)
2789 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002790 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002791 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002792 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002793 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002794#ifdef CONFIG_SQLITE
2795 if (hapd->rad_attr_db) {
2796 sqlite3_close(hapd->rad_attr_db);
2797 hapd->rad_attr_db = NULL;
2798 }
2799#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002800 hostapd_cleanup(hapd);
2801}
2802
2803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804void hostapd_interface_deinit(struct hostapd_iface *iface)
2805{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002806 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807
Dmitry Shmidt54605472013-11-08 11:10:19 -08002808 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 if (iface == NULL)
2810 return;
2811
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002812 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2813
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002814 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2815 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002816 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002817
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002818#ifdef CONFIG_FST
2819 if (iface->fst) {
2820 fst_detach(iface->fst);
2821 iface->fst = NULL;
2822 }
2823#endif /* CONFIG_FST */
2824
Hai Shalom021b0b52019-04-10 11:17:58 -07002825 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002826 if (!iface->bss)
2827 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002828 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002829 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002830
Roshan Pius3a1667e2018-07-03 15:17:14 -07002831#ifdef NEED_AP_MLME
2832 hostapd_stop_setup_timers(iface);
2833 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2834#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835}
2836
2837
2838void hostapd_interface_free(struct hostapd_iface *iface)
2839{
2840 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002841 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2842 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002843 if (!iface->bss)
2844 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002845 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2846 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002847 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002848 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849 hostapd_cleanup_iface(iface);
2850}
2851
2852
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002853struct hostapd_iface * hostapd_alloc_iface(void)
2854{
2855 struct hostapd_iface *hapd_iface;
2856
2857 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2858 if (!hapd_iface)
2859 return NULL;
2860
2861 dl_list_init(&hapd_iface->sta_seen);
2862
2863 return hapd_iface;
2864}
2865
2866
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002867/**
2868 * hostapd_init - Allocate and initialize per-interface data
2869 * @config_file: Path to the configuration file
2870 * Returns: Pointer to the allocated interface data or %NULL on failure
2871 *
2872 * This function is used to allocate main data structures for per-interface
2873 * data. The allocated data buffer will be freed by calling
2874 * hostapd_cleanup_iface().
2875 */
2876struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2877 const char *config_file)
2878{
2879 struct hostapd_iface *hapd_iface = NULL;
2880 struct hostapd_config *conf = NULL;
2881 struct hostapd_data *hapd;
2882 size_t i;
2883
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002884 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002885 if (hapd_iface == NULL)
2886 goto fail;
2887
2888 hapd_iface->config_fname = os_strdup(config_file);
2889 if (hapd_iface->config_fname == NULL)
2890 goto fail;
2891
2892 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2893 if (conf == NULL)
2894 goto fail;
2895 hapd_iface->conf = conf;
2896
2897 hapd_iface->num_bss = conf->num_bss;
2898 hapd_iface->bss = os_calloc(conf->num_bss,
2899 sizeof(struct hostapd_data *));
2900 if (hapd_iface->bss == NULL)
2901 goto fail;
2902
2903 for (i = 0; i < conf->num_bss; i++) {
2904 hapd = hapd_iface->bss[i] =
2905 hostapd_alloc_bss_data(hapd_iface, conf,
2906 conf->bss[i]);
2907 if (hapd == NULL)
2908 goto fail;
2909 hapd->msg_ctx = hapd;
2910 }
2911
2912 return hapd_iface;
2913
2914fail:
2915 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2916 config_file);
2917 if (conf)
2918 hostapd_config_free(conf);
2919 if (hapd_iface) {
2920 os_free(hapd_iface->config_fname);
2921 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002922 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2923 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002924 os_free(hapd_iface);
2925 }
2926 return NULL;
2927}
2928
2929
2930static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2931{
2932 size_t i, j;
2933
2934 for (i = 0; i < interfaces->count; i++) {
2935 struct hostapd_iface *iface = interfaces->iface[i];
2936 for (j = 0; j < iface->num_bss; j++) {
2937 struct hostapd_data *hapd = iface->bss[j];
2938 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2939 return 1;
2940 }
2941 }
2942
2943 return 0;
2944}
2945
2946
2947/**
2948 * hostapd_interface_init_bss - Read configuration file and init BSS data
2949 *
2950 * This function is used to parse configuration file for a BSS. This BSS is
2951 * added to an existing interface sharing the same radio (if any) or a new
2952 * interface is created if this is the first interface on a radio. This
2953 * allocate memory for the BSS. No actual driver operations are started.
2954 *
2955 * This is similar to hostapd_interface_init(), but for a case where the
2956 * configuration is used to add a single BSS instead of all BSSes for a radio.
2957 */
2958struct hostapd_iface *
2959hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2960 const char *config_fname, int debug)
2961{
2962 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2963 struct hostapd_data *hapd;
2964 int k;
2965 size_t i, bss_idx;
2966
2967 if (!phy || !*phy)
2968 return NULL;
2969
2970 for (i = 0; i < interfaces->count; i++) {
2971 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2972 iface = interfaces->iface[i];
2973 break;
2974 }
2975 }
2976
2977 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2978 config_fname, phy, iface ? "" : " --> new PHY");
2979 if (iface) {
2980 struct hostapd_config *conf;
2981 struct hostapd_bss_config **tmp_conf;
2982 struct hostapd_data **tmp_bss;
2983 struct hostapd_bss_config *bss;
2984 const char *ifname;
2985
2986 /* Add new BSS to existing iface */
2987 conf = interfaces->config_read_cb(config_fname);
2988 if (conf == NULL)
2989 return NULL;
2990 if (conf->num_bss > 1) {
2991 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2992 hostapd_config_free(conf);
2993 return NULL;
2994 }
2995
2996 ifname = conf->bss[0]->iface;
2997 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2998 wpa_printf(MSG_ERROR,
2999 "Interface name %s already in use", ifname);
3000 hostapd_config_free(conf);
3001 return NULL;
3002 }
3003
3004 tmp_conf = os_realloc_array(
3005 iface->conf->bss, iface->conf->num_bss + 1,
3006 sizeof(struct hostapd_bss_config *));
3007 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
3008 sizeof(struct hostapd_data *));
3009 if (tmp_bss)
3010 iface->bss = tmp_bss;
3011 if (tmp_conf) {
3012 iface->conf->bss = tmp_conf;
3013 iface->conf->last_bss = tmp_conf[0];
3014 }
3015 if (tmp_bss == NULL || tmp_conf == NULL) {
3016 hostapd_config_free(conf);
3017 return NULL;
3018 }
3019 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
3020 iface->conf->num_bss++;
3021
3022 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
3023 if (hapd == NULL) {
3024 iface->conf->num_bss--;
3025 hostapd_config_free(conf);
3026 return NULL;
3027 }
3028 iface->conf->last_bss = bss;
3029 iface->bss[iface->num_bss] = hapd;
3030 hapd->msg_ctx = hapd;
3031
3032 bss_idx = iface->num_bss++;
3033 conf->num_bss--;
3034 conf->bss[0] = NULL;
3035 hostapd_config_free(conf);
3036 } else {
3037 /* Add a new iface with the first BSS */
3038 new_iface = iface = hostapd_init(interfaces, config_fname);
3039 if (!iface)
3040 return NULL;
3041 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
3042 iface->interfaces = interfaces;
3043 bss_idx = 0;
3044 }
3045
3046 for (k = 0; k < debug; k++) {
3047 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
3048 iface->bss[bss_idx]->conf->logger_stdout_level--;
3049 }
3050
3051 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
3052 !hostapd_drv_none(iface->bss[bss_idx])) {
3053 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
3054 config_fname);
3055 if (new_iface)
3056 hostapd_interface_deinit_free(new_iface);
3057 return NULL;
3058 }
3059
3060 return iface;
3061}
3062
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003063
3064void hostapd_interface_deinit_free(struct hostapd_iface *iface)
3065{
3066 const struct wpa_driver_ops *driver;
3067 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003068
3069 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003070 if (iface == NULL)
3071 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003072 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
3073 __func__, (unsigned int) iface->num_bss,
3074 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003075 driver = iface->bss[0]->driver;
3076 drv_priv = iface->bss[0]->drv_priv;
3077 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003078 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3079 __func__, driver, drv_priv);
Sunil Ravi88611412024-06-28 17:34:56 +00003080 if (driver && driver->hapd_deinit && drv_priv) {
3081 if (!iface->bss[0]->mld_first_bss)
3082 driver->hapd_deinit(drv_priv);
3083 hostapd_clear_drv_priv(iface->bss[0]);
3084 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003085 hostapd_interface_free(iface);
3086}
3087
3088
Dmitry Shmidt15907092014-03-25 10:42:57 -07003089static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
3090 void *drv_priv,
3091 struct hostapd_iface *hapd_iface)
3092{
3093 size_t j;
3094
3095 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3096 __func__, driver, drv_priv);
3097 if (driver && driver->hapd_deinit && drv_priv) {
Sunil Ravi88611412024-06-28 17:34:56 +00003098 if (!hapd_iface->bss[0]->mld_first_bss)
3099 driver->hapd_deinit(drv_priv);
Dmitry Shmidt15907092014-03-25 10:42:57 -07003100 for (j = 0; j < hapd_iface->num_bss; j++) {
3101 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
3102 __func__, (int) j,
3103 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07003104 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Sunil Ravi88611412024-06-28 17:34:56 +00003105 hostapd_clear_drv_priv(hapd_iface->bss[j]);
Hai Shalom1dc4d202019-04-29 16:22:27 -07003106 hapd_iface->extended_capa = NULL;
3107 hapd_iface->extended_capa_mask = NULL;
3108 hapd_iface->extended_capa_len = 0;
3109 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07003110 }
3111 }
3112}
3113
3114
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003115int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
3116{
Dmitry Shmidt71757432014-06-02 13:50:35 -07003117 size_t j;
3118
Hai Shalom60840252021-02-19 19:02:11 -08003119 if (!hapd_iface)
3120 return -1;
3121
3122 if (hapd_iface->enable_iface_cb)
3123 return hapd_iface->enable_iface_cb(hapd_iface);
3124
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003125 if (hapd_iface->bss[0]->drv_priv != NULL) {
3126 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003127 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003128 return -1;
3129 }
3130
3131 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003132 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003133
Dmitry Shmidt71757432014-06-02 13:50:35 -07003134 for (j = 0; j < hapd_iface->num_bss; j++)
3135 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003136 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
3137 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
3138 return -1;
3139 }
3140
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003141 if (hapd_iface->interfaces == NULL ||
3142 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003143 hapd_iface->interfaces->driver_init(hapd_iface))
3144 return -1;
3145
3146 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07003147 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
3148 hapd_iface->bss[0]->drv_priv,
3149 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003150 return -1;
3151 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003152
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003153 return 0;
3154}
3155
3156
3157int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
3158{
3159 size_t j;
3160
3161 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003162 hapd_iface->conf->bss[0]->iface);
3163 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07003164 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003165 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003166 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
3167 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003168 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003169 hostapd_clear_old(hapd_iface);
3170 for (j = 0; j < hapd_iface->num_bss; j++)
3171 hostapd_reload_bss(hapd_iface->bss[j]);
3172
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003173 return 0;
3174}
3175
3176
Sunil Ravi77d572f2023-01-17 23:58:31 +00003177int hostapd_reload_bss_only(struct hostapd_data *bss)
3178{
3179
3180 wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
3181 hostapd_set_security_params(bss->conf, 1);
3182 if (hostapd_config_check(bss->iconf, 1) < 0) {
3183 wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
3184 return -1;
3185 }
3186 hostapd_clear_old_bss(bss);
3187 hostapd_reload_bss(bss);
3188 return 0;
3189}
3190
3191
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003192int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
3193{
3194 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003195 const struct wpa_driver_ops *driver;
3196 void *drv_priv;
3197
3198 if (hapd_iface == NULL)
3199 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07003200
Hai Shalom60840252021-02-19 19:02:11 -08003201 if (hapd_iface->disable_iface_cb)
3202 return hapd_iface->disable_iface_cb(hapd_iface);
3203
Dmitry Shmidt71757432014-06-02 13:50:35 -07003204 if (hapd_iface->bss[0]->drv_priv == NULL) {
3205 wpa_printf(MSG_INFO, "Interface %s already disabled",
3206 hapd_iface->conf->bss[0]->iface);
3207 return -1;
3208 }
3209
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003210#ifdef CONFIG_IEEE80211BE
3211 if (hapd_iface->bss[0]->conf->mld_ap &&
3212 !hapd_iface->bss[0]->mld_first_bss) {
3213 /* Do not allow mld_first_bss disabling before other BSSs */
3214 for (j = 0; j < hapd_iface->interfaces->count; ++j) {
3215 struct hostapd_iface *h_iface =
3216 hapd_iface->interfaces->iface[j];
3217 struct hostapd_data *h_hapd = h_iface->bss[0];
3218 struct hostapd_bss_config *h_conf = h_hapd->conf;
3219
3220 if (!h_conf->mld_ap ||
3221 h_conf->mld_id !=
3222 hapd_iface->bss[0]->conf->mld_id ||
3223 h_iface == hapd_iface)
3224 continue;
3225
3226 if (h_iface->state != HAPD_IFACE_DISABLED) {
3227 wpa_printf(MSG_INFO,
3228 "Do not allow disable mld_first_bss first");
3229 return -1;
3230 }
3231 }
3232 }
3233#endif /* CONFIG_IEEE80211BE */
3234
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003235 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003236 driver = hapd_iface->bss[0]->driver;
3237 drv_priv = hapd_iface->bss[0]->drv_priv;
3238
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003239 hapd_iface->driver_ap_teardown =
3240 !!(hapd_iface->drv_flags &
3241 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3242
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003243#ifdef NEED_AP_MLME
3244 for (j = 0; j < hapd_iface->num_bss; j++)
3245 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
3246#endif /* NEED_AP_MLME */
3247
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003248 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003249 for (j = 0; j < hapd_iface->num_bss; j++) {
3250 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07003251 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003252 hostapd_free_hapd_data(hapd);
3253 }
3254
Dmitry Shmidt15907092014-03-25 10:42:57 -07003255 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003256
3257 /* From hostapd_cleanup_iface: These were initialized in
3258 * hostapd_setup_interface and hostapd_setup_interface_complete
3259 */
3260 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003261
Dmitry Shmidt56052862013-10-04 10:23:25 -07003262 wpa_printf(MSG_DEBUG, "Interface %s disabled",
3263 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003264 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003265 return 0;
3266}
3267
3268
3269static struct hostapd_iface *
3270hostapd_iface_alloc(struct hapd_interfaces *interfaces)
3271{
3272 struct hostapd_iface **iface, *hapd_iface;
3273
3274 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
3275 sizeof(struct hostapd_iface *));
3276 if (iface == NULL)
3277 return NULL;
3278 interfaces->iface = iface;
3279 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003280 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003281 if (hapd_iface == NULL) {
3282 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3283 "the interface", __func__);
3284 return NULL;
3285 }
3286 interfaces->count++;
3287 hapd_iface->interfaces = interfaces;
3288
3289 return hapd_iface;
3290}
3291
3292
3293static struct hostapd_config *
3294hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003295 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003296{
3297 struct hostapd_bss_config *bss;
3298 struct hostapd_config *conf;
3299
3300 /* Allocates memory for bss and conf */
3301 conf = hostapd_config_defaults();
3302 if (conf == NULL) {
3303 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3304 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08003305 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003306 }
3307
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003308 if (driver) {
3309 int j;
3310
3311 for (j = 0; wpa_drivers[j]; j++) {
3312 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3313 conf->driver = wpa_drivers[j];
3314 goto skip;
3315 }
3316 }
3317
3318 wpa_printf(MSG_ERROR,
3319 "Invalid/unknown driver '%s' - registering the default driver",
3320 driver);
3321 }
3322
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003323 conf->driver = wpa_drivers[0];
3324 if (conf->driver == NULL) {
3325 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3326 hostapd_config_free(conf);
3327 return NULL;
3328 }
3329
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003330skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003331 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003332
3333 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3334 bss->ctrl_interface = os_strdup(ctrl_iface);
3335 if (bss->ctrl_interface == NULL) {
3336 hostapd_config_free(conf);
3337 return NULL;
3338 }
3339
3340 /* Reading configuration file skipped, will be done in SET!
3341 * From reading the configuration till the end has to be done in
3342 * SET
3343 */
3344 return conf;
3345}
3346
3347
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003348static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3349 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003350{
3351 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003352 struct hostapd_data *hapd;
3353
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003354 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003355 sizeof(struct hostapd_data *));
3356 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003357 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003358
3359 for (i = 0; i < conf->num_bss; i++) {
3360 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003361 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003362 if (hapd == NULL) {
3363 while (i > 0) {
3364 i--;
3365 os_free(hapd_iface->bss[i]);
3366 hapd_iface->bss[i] = NULL;
3367 }
3368 os_free(hapd_iface->bss);
3369 hapd_iface->bss = NULL;
3370 return -1;
3371 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003372 hapd->msg_ctx = hapd;
3373 }
3374
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003375 hapd_iface->conf = conf;
3376 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003377
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003378 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003379}
3380
3381
3382int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3383{
3384 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003385 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3386 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003387 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003388 size_t i, j;
3389 const char *conf_file = NULL, *phy_name = NULL;
3390
3391 if (os_strncmp(buf, "bss_config=", 11) == 0) {
3392 char *pos;
3393 phy_name = buf + 11;
3394 pos = os_strchr(phy_name, ':');
3395 if (!pos)
3396 return -1;
3397 *pos++ = '\0';
3398 conf_file = pos;
3399 if (!os_strlen(conf_file))
3400 return -1;
3401
3402 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3403 conf_file, 0);
3404 if (!hapd_iface)
3405 return -1;
3406 for (j = 0; j < interfaces->count; j++) {
3407 if (interfaces->iface[j] == hapd_iface)
3408 break;
3409 }
3410 if (j == interfaces->count) {
3411 struct hostapd_iface **tmp;
3412 tmp = os_realloc_array(interfaces->iface,
3413 interfaces->count + 1,
3414 sizeof(struct hostapd_iface *));
3415 if (!tmp) {
3416 hostapd_interface_deinit_free(hapd_iface);
3417 return -1;
3418 }
3419 interfaces->iface = tmp;
3420 interfaces->iface[interfaces->count++] = hapd_iface;
3421 new_iface = hapd_iface;
3422 }
3423
3424 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003425 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003426 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003427
3428 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003429 hostapd_deinit_driver(
3430 hapd_iface->bss[0]->driver,
3431 hapd_iface->bss[0]->drv_priv,
3432 hapd_iface);
3433 goto fail;
3434 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003435 } else {
3436 /* Assign new BSS with bss[0]'s driver info */
3437 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3438 hapd->driver = hapd_iface->bss[0]->driver;
3439 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3440 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3441 ETH_ALEN);
3442
3443 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003444 (hapd_iface->state == HAPD_IFACE_ENABLED &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00003445 hostapd_setup_bss(hapd, -1, true))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003446 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003447 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003448 hapd_iface->conf->num_bss--;
3449 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003450 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3451 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003452 hostapd_config_free_bss(hapd->conf);
3453 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003454 os_free(hapd);
3455 return -1;
3456 }
3457 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003458 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003459 return 0;
3460 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003461
3462 ptr = os_strchr(buf, ' ');
3463 if (ptr == NULL)
3464 return -1;
3465 *ptr++ = '\0';
3466
Dmitry Shmidt56052862013-10-04 10:23:25 -07003467 if (os_strncmp(ptr, "config=", 7) == 0)
3468 conf_file = ptr + 7;
3469
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003470 for (i = 0; i < interfaces->count; i++) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003471 bool mld_ap = false;
3472
3473#ifdef CONFIG_IEEE80211BE
3474 mld_ap = interfaces->iface[i]->conf->bss[0]->mld_ap;
3475#endif /* CONFIG_IEEE80211BE */
3476
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003477 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003478 buf) && !mld_ap) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003479 wpa_printf(MSG_INFO, "Cannot add interface - it "
3480 "already exists");
3481 return -1;
3482 }
3483 }
3484
3485 hapd_iface = hostapd_iface_alloc(interfaces);
3486 if (hapd_iface == NULL) {
3487 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3488 "for interface", __func__);
3489 goto fail;
3490 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003491 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003492
Dmitry Shmidt56052862013-10-04 10:23:25 -07003493 if (conf_file && interfaces->config_read_cb) {
3494 conf = interfaces->config_read_cb(conf_file);
3495 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003496 os_strlcpy(conf->bss[0]->iface, buf,
3497 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003498 } else {
3499 char *driver = os_strchr(ptr, ' ');
3500
3501 if (driver)
3502 *driver++ = '\0';
3503 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3504 }
3505
Dmitry Shmidt56052862013-10-04 10:23:25 -07003506 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003507 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3508 "for configuration", __func__);
3509 goto fail;
3510 }
3511
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003512 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003513 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3514 "for hostapd", __func__);
3515 goto fail;
3516 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003517 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003518
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003519 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003520 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003521
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003522 wpa_printf(MSG_INFO, "Add interface '%s'",
3523 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003524
3525 return 0;
3526
3527fail:
3528 if (conf)
3529 hostapd_config_free(conf);
3530 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003531 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003532 for (i = 0; i < hapd_iface->num_bss; i++) {
3533 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003534 if (!hapd)
3535 continue;
3536 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003537 hapd_iface->interfaces->ctrl_iface_deinit)
3538 hapd_iface->interfaces->
3539 ctrl_iface_deinit(hapd);
3540 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3541 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003542 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003543 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003544 os_free(hapd);
3545 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003546 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003547 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003548 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003549 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003550 if (new_iface) {
3551 interfaces->count--;
3552 interfaces->iface[interfaces->count] = NULL;
3553 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003554 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003555 }
3556 return -1;
3557}
3558
3559
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003560static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3561{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003562 size_t i;
3563
Dmitry Shmidt54605472013-11-08 11:10:19 -08003564 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003565
Dmitry Shmidt54605472013-11-08 11:10:19 -08003566 /* Remove hostapd_data only if it has already been initialized */
3567 if (idx < iface->num_bss) {
3568 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003569
Dmitry Shmidt54605472013-11-08 11:10:19 -08003570 hostapd_bss_deinit(hapd);
3571 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3572 __func__, hapd, hapd->conf->iface);
3573 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003574 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003575 os_free(hapd);
3576
3577 iface->num_bss--;
3578
3579 for (i = idx; i < iface->num_bss; i++)
3580 iface->bss[i] = iface->bss[i + 1];
3581 } else {
3582 hostapd_config_free_bss(iface->conf->bss[idx]);
3583 iface->conf->bss[idx] = NULL;
3584 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003585
3586 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003587 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003588 iface->conf->bss[i] = iface->conf->bss[i + 1];
3589
3590 return 0;
3591}
3592
3593
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003594int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3595{
3596 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003597 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003598
3599 for (i = 0; i < interfaces->count; i++) {
3600 hapd_iface = interfaces->iface[i];
3601 if (hapd_iface == NULL)
3602 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003603 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003604 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003605 hapd_iface->driver_ap_teardown =
3606 !!(hapd_iface->drv_flags &
3607 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3608
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003609 hostapd_interface_deinit_free(hapd_iface);
3610 k = i;
3611 while (k < (interfaces->count - 1)) {
3612 interfaces->iface[k] =
3613 interfaces->iface[k + 1];
3614 k++;
3615 }
3616 interfaces->count--;
3617 return 0;
3618 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003619
3620 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003621 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3622 hapd_iface->driver_ap_teardown =
3623 !(hapd_iface->drv_flags &
3624 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003625 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003626 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003627 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003628 }
3629 return -1;
3630}
3631
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633/**
3634 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3635 * @hapd: Pointer to BSS data
3636 * @sta: Pointer to the associated STA data
3637 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3638 *
3639 * This function will be called whenever a station associates with the AP. It
3640 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3641 * from drv_callbacks.c based on driver events for drivers that take care of
3642 * management frames (IEEE 802.11 authentication and association) internally.
3643 */
3644void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3645 int reassoc)
3646{
3647 if (hapd->tkip_countermeasures) {
3648 hostapd_drv_sta_deauth(hapd, sta->addr,
3649 WLAN_REASON_MICHAEL_MIC_FAILURE);
3650 return;
3651 }
3652
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003653#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003654 if (ap_sta_is_mld(hapd, sta) &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003655 sta->mld_assoc_link_id != hapd->mld_link_id)
3656 return;
3657#endif /* CONFIG_IEEE80211BE */
3658
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003659 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003660 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662#ifdef CONFIG_P2P
3663 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3664 sta->no_p2p_set = 1;
3665 hapd->num_sta_no_p2p++;
3666 if (hapd->num_sta_no_p2p == 1)
3667 hostapd_p2p_non_p2p_sta_connected(hapd);
3668 }
3669#endif /* CONFIG_P2P */
3670
Hai Shalom81f62d82019-07-22 12:10:00 -07003671 airtime_policy_new_sta(hapd, sta);
3672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3674 * IEEE 802.1X/WPA code will start accounting after the station has
3675 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003676 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3677 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003678 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003681
3682 /* Start IEEE 802.1X authentication process for new stations */
3683 ieee802_1x_new_station(hapd, sta);
3684 if (reassoc) {
3685 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003686 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3687 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3688 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3690 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003691 } else if (!(hapd->iface->drv_flags2 &
3692 WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK)) {
3693 /* The 4-way handshake offloaded case will have this handled
3694 * based on the port authorized event. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003696 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003697
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003698 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3699 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3700 wpa_printf(MSG_DEBUG,
3701 "%s: %s: canceled wired ap_handle_timer timeout for "
3702 MACSTR,
3703 hapd->conf->iface, __func__,
3704 MAC2STR(sta->addr));
3705 }
3706 } else if (!(hapd->iface->drv_flags &
3707 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003708 wpa_printf(MSG_DEBUG,
3709 "%s: %s: reschedule ap_handle_timer timeout for "
3710 MACSTR " (%d seconds - ap_max_inactivity)",
3711 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003712 hapd->conf->ap_max_inactivity);
3713 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3714 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3715 ap_handle_timer, hapd, sta);
3716 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003717
3718#ifdef CONFIG_MACSEC
3719 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3720 hapd->conf->mka_psk_set)
3721 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3722 else
3723 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3724#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003725}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003726
3727
3728const char * hostapd_state_text(enum hostapd_iface_state s)
3729{
3730 switch (s) {
3731 case HAPD_IFACE_UNINITIALIZED:
3732 return "UNINITIALIZED";
3733 case HAPD_IFACE_DISABLED:
3734 return "DISABLED";
3735 case HAPD_IFACE_COUNTRY_UPDATE:
3736 return "COUNTRY_UPDATE";
3737 case HAPD_IFACE_ACS:
3738 return "ACS";
3739 case HAPD_IFACE_HT_SCAN:
3740 return "HT_SCAN";
3741 case HAPD_IFACE_DFS:
3742 return "DFS";
3743 case HAPD_IFACE_ENABLED:
3744 return "ENABLED";
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003745 case HAPD_IFACE_NO_IR:
3746 return "NO_IR";
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003747 }
3748
3749 return "UNKNOWN";
3750}
3751
3752
3753void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3754{
3755 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003756 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3757 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003758 iface->state = s;
3759}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003760
3761
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003762int hostapd_csa_in_progress(struct hostapd_iface *iface)
3763{
3764 unsigned int i;
3765
3766 for (i = 0; i < iface->num_bss; i++)
3767 if (iface->bss[i]->csa_in_progress)
3768 return 1;
3769 return 0;
3770}
3771
3772
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003773#ifdef NEED_AP_MLME
3774
3775static void free_beacon_data(struct beacon_data *beacon)
3776{
3777 os_free(beacon->head);
3778 beacon->head = NULL;
3779 os_free(beacon->tail);
3780 beacon->tail = NULL;
3781 os_free(beacon->probe_resp);
3782 beacon->probe_resp = NULL;
3783 os_free(beacon->beacon_ies);
3784 beacon->beacon_ies = NULL;
3785 os_free(beacon->proberesp_ies);
3786 beacon->proberesp_ies = NULL;
3787 os_free(beacon->assocresp_ies);
3788 beacon->assocresp_ies = NULL;
3789}
3790
3791
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003792static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003793 struct beacon_data *beacon)
3794{
3795 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3796 struct wpa_driver_ap_params params;
3797 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003798
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003799 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003800 ret = ieee802_11_build_ap_params(hapd, &params);
3801 if (ret < 0)
3802 return ret;
3803
3804 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3805 &proberesp_extra,
3806 &assocresp_extra);
3807 if (ret)
3808 goto free_ap_params;
3809
3810 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003811 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003812 if (!beacon->head)
3813 goto free_ap_extra_ies;
3814
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003815 beacon->head_len = params.head_len;
3816
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003817 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003818 if (!beacon->tail)
3819 goto free_beacon;
3820
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003821 beacon->tail_len = params.tail_len;
3822
3823 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003824 beacon->probe_resp = os_memdup(params.proberesp,
3825 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003826 if (!beacon->probe_resp)
3827 goto free_beacon;
3828
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003829 beacon->probe_resp_len = params.proberesp_len;
3830 }
3831
3832 /* copy the extra ies */
3833 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003834 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3835 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003836 if (!beacon->beacon_ies)
3837 goto free_beacon;
3838
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003839 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3840 }
3841
3842 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003843 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3844 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003845 if (!beacon->proberesp_ies)
3846 goto free_beacon;
3847
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003848 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3849 }
3850
3851 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003852 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3853 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003854 if (!beacon->assocresp_ies)
3855 goto free_beacon;
3856
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003857 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3858 }
3859
3860 ret = 0;
3861free_beacon:
3862 /* if the function fails, the caller should not free beacon data */
3863 if (ret)
3864 free_beacon_data(beacon);
3865
3866free_ap_extra_ies:
3867 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3868 assocresp_extra);
3869free_ap_params:
3870 ieee802_11_free_ap_params(&params);
3871 return ret;
3872}
3873
3874
3875/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003876 * TODO: This flow currently supports only changing channel and width within
3877 * the same hw_mode. Any other changes to MAC parameters or provided settings
3878 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003879 */
3880static int hostapd_change_config_freq(struct hostapd_data *hapd,
3881 struct hostapd_config *conf,
3882 struct hostapd_freq_params *params,
3883 struct hostapd_freq_params *old_params)
3884{
3885 int channel;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003886 u8 seg0 = 0, seg1 = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003887 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003888
3889 if (!params->channel) {
3890 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003891 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003892 }
3893
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003894 channel = params->channel;
3895 if (!channel)
3896 return -1;
3897
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003898 hostapd_determine_mode(hapd->iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07003899 mode = hapd->iface->current_mode;
3900
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003901 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003902 if (old_params &&
3903 hostapd_set_freq_params(old_params, conf->hw_mode,
3904 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003905 conf->channel, conf->enable_edmg,
3906 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003907 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07003908 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003909 hostapd_get_oper_chwidth(conf),
3910 hostapd_get_oper_centr_freq_seg0_idx(conf),
3911 hostapd_get_oper_centr_freq_seg1_idx(conf),
3912 conf->vht_capab,
3913 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07003914 NULL,
3915 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Sunil Ravi88611412024-06-28 17:34:56 +00003916 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003917 return -1;
3918
3919 switch (params->bandwidth) {
3920 case 0:
3921 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003922 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3923 break;
3924 case 40:
3925 case 80:
3926 case 160:
Sunil Ravi640215c2023-06-28 23:08:09 +00003927 case 320:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003928 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3929 break;
3930 default:
3931 return -1;
3932 }
3933
3934 switch (params->bandwidth) {
3935 case 0:
3936 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003937 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003938 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003939 break;
3940 case 80:
3941 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003942 hostapd_set_oper_chwidth(conf,
3943 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003944 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003945 hostapd_set_oper_chwidth(conf,
3946 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003947 break;
3948 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003949 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
3950 break;
3951 case 320:
3952 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003953 break;
3954 default:
3955 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003956 }
3957
3958 conf->channel = channel;
3959 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003960 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003961 conf->secondary_channel = params->sec_channel_offset;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003962 if (params->center_freq1 &&
3963 ieee80211_freq_to_chan(params->center_freq1, &seg0) ==
3964 NUM_HOSTAPD_MODES)
3965 return -1;
3966 if (params->center_freq2 &&
3967 ieee80211_freq_to_chan(params->center_freq2,
3968 &seg1) == NUM_HOSTAPD_MODES)
3969 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07003970 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3971 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003972
3973 /* TODO: maybe call here hostapd_config_check here? */
3974
3975 return 0;
3976}
3977
3978
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003979static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003980 struct csa_settings *settings)
3981{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003982 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003983 struct hostapd_freq_params old_freq;
3984 int ret;
Sunil Ravi036cec52023-03-29 11:35:17 -07003985#ifdef CONFIG_IEEE80211BE
3986 u16 old_punct_bitmap;
3987#endif /* CONFIG_IEEE80211BE */
Hai Shalom81f62d82019-07-22 12:10:00 -07003988 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003989
3990 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003991 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003992 return -1;
3993
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003994 switch (settings->freq_params.bandwidth) {
3995 case 80:
3996 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003997 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003998 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003999 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004000 break;
4001 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004002 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004003 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00004004 case 320:
4005 bandwidth = CONF_OPER_CHWIDTH_320MHZ;
4006 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004007 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00004008 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004009 break;
4010 }
4011
4012 if (ieee80211_freq_to_channel_ext(
4013 settings->freq_params.freq,
4014 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004015 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004016 &hapd->iface->cs_oper_class,
4017 &chan) == NUM_HOSTAPD_MODES) {
4018 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07004019 "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 -08004020 settings->freq_params.freq,
4021 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004022 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07004023 settings->freq_params.he_enabled,
4024 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004025 return -1;
4026 }
4027
4028 settings->freq_params.channel = chan;
4029
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004030 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
4031 &settings->freq_params,
4032 &old_freq);
4033 if (ret)
4034 return ret;
4035
Sunil Ravi036cec52023-03-29 11:35:17 -07004036#ifdef CONFIG_IEEE80211BE
4037 old_punct_bitmap = iface->conf->punct_bitmap;
4038 iface->conf->punct_bitmap = settings->punct_bitmap;
4039#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004040 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004041
4042 /* change back the configuration */
Sunil Ravi036cec52023-03-29 11:35:17 -07004043#ifdef CONFIG_IEEE80211BE
4044 iface->conf->punct_bitmap = old_punct_bitmap;
4045#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004046 hostapd_change_config_freq(iface->bss[0], iface->conf,
4047 &old_freq, NULL);
4048
4049 if (ret)
4050 return ret;
4051
4052 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004053 hapd->cs_freq_params = settings->freq_params;
4054 hapd->cs_count = settings->cs_count;
4055 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004056
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004057 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004058 if (ret) {
4059 free_beacon_data(&settings->beacon_after);
4060 return ret;
4061 }
4062
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004063 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
4064 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
4065 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
4066 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004067 settings->link_id = -1;
4068#ifdef CONFIG_IEEE80211BE
4069 if (hapd->conf->mld_ap)
4070 settings->link_id = hapd->mld_link_id;
4071#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004072
4073 return 0;
4074}
4075
4076
4077void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
4078{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004079 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
4080 hapd->cs_count = 0;
4081 hapd->cs_block_tx = 0;
4082 hapd->cs_c_off_beacon = 0;
4083 hapd->cs_c_off_proberesp = 0;
4084 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004085 hapd->cs_c_off_ecsa_beacon = 0;
4086 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004087}
4088
4089
Hai Shalom60840252021-02-19 19:02:11 -08004090void hostapd_chan_switch_config(struct hostapd_data *hapd,
4091 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004092{
Sunil Ravia04bd252022-05-02 22:54:18 -07004093 if (freq_params->eht_enabled)
4094 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
4095 else
4096 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
4097
Hai Shalom60840252021-02-19 19:02:11 -08004098 if (freq_params->he_enabled)
4099 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
4100 else
4101 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
4102
4103 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004104 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
4105 else
4106 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
4107
4108 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08004109 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07004110 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
4111 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08004112 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004113 hapd->iconf->ch_switch_vht_config);
4114}
4115
4116
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004117int hostapd_switch_channel(struct hostapd_data *hapd,
4118 struct csa_settings *settings)
4119{
4120 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004121
4122 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
4123 wpa_printf(MSG_INFO, "CSA is not supported");
4124 return -1;
4125 }
4126
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004127 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004128 if (ret)
4129 return ret;
4130
4131 ret = hostapd_drv_switch_channel(hapd, settings);
4132 free_beacon_data(&settings->beacon_csa);
4133 free_beacon_data(&settings->beacon_after);
4134
4135 if (ret) {
4136 /* if we failed, clean cs parameters */
4137 hostapd_cleanup_cs_params(hapd);
4138 return ret;
4139 }
4140
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004141 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004142 return 0;
4143}
4144
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004145
4146void
4147hostapd_switch_channel_fallback(struct hostapd_iface *iface,
4148 const struct hostapd_freq_params *freq_params)
4149{
Sunil8cd6f4d2022-06-28 18:40:46 +00004150 int seg0_idx = 0, seg1_idx = 0;
4151 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004152
4153 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
4154
4155 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07004156 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004157 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07004158 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004159
4160 switch (freq_params->bandwidth) {
4161 case 0:
4162 case 20:
4163 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00004164 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004165 break;
4166 case 80:
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004167 if (freq_params->center_freq2) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004168 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004169 iface->conf->vht_capab |=
4170 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
4171 } else {
Sunil8cd6f4d2022-06-28 18:40:46 +00004172 bw = CONF_OPER_CHWIDTH_80MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004173 }
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004174 break;
4175 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004176 bw = CONF_OPER_CHWIDTH_160MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004177 iface->conf->vht_capab |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Sunil8cd6f4d2022-06-28 18:40:46 +00004178 break;
4179 case 320:
4180 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004181 break;
4182 default:
4183 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
4184 freq_params->bandwidth);
4185 break;
4186 }
4187
4188 iface->freq = freq_params->freq;
4189 iface->conf->channel = freq_params->channel;
4190 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07004191 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
4192 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
4193 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004194 iface->conf->ieee80211n = freq_params->ht_enabled;
4195 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07004196 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07004197 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004198
4199 /*
4200 * cs_params must not be cleared earlier because the freq_params
4201 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004202 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004203 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004204 hostapd_disable_iface(iface);
4205 hostapd_enable_iface(iface);
4206}
4207
Sunil Ravia04bd252022-05-02 22:54:18 -07004208
4209#ifdef CONFIG_IEEE80211AX
4210
4211void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
4212{
4213 hapd->cca_count = 0;
4214 hapd->cca_color = 0;
4215 hapd->cca_c_off_beacon = 0;
4216 hapd->cca_c_off_proberesp = 0;
4217 hapd->cca_in_progress = false;
4218}
4219
4220
4221static int hostapd_fill_cca_settings(struct hostapd_data *hapd,
4222 struct cca_settings *settings)
4223{
4224 struct hostapd_iface *iface = hapd->iface;
4225 u8 old_color;
4226 int ret;
4227
4228 if (!iface || iface->conf->he_op.he_bss_color_disabled)
4229 return -1;
4230
4231 old_color = iface->conf->he_op.he_bss_color;
4232 iface->conf->he_op.he_bss_color = hapd->cca_color;
4233 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
4234 if (ret)
4235 return ret;
4236
4237 iface->conf->he_op.he_bss_color = old_color;
4238
4239 settings->cca_count = hapd->cca_count;
4240 settings->cca_color = hapd->cca_color,
4241 hapd->cca_in_progress = true;
4242
4243 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
4244 if (ret) {
4245 free_beacon_data(&settings->beacon_after);
4246 return ret;
4247 }
4248
4249 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
4250 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
4251
4252 return 0;
4253}
4254
4255
4256static void hostapd_switch_color_timeout_handler(void *eloop_data,
4257 void *user_ctx)
4258{
4259 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
4260 os_time_t delta_t;
4261 unsigned int b;
4262 int i, r;
4263
4264 /* CCA can be triggered once the handler constantly receives
4265 * color collision events to for at least
4266 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
4267 delta_t = hapd->last_color_collision.sec -
4268 hapd->first_color_collision.sec;
4269 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
4270 return;
4271
4272 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
4273 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004274 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07004275 break;
4276
4277 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
4278 }
4279
4280 if (i == HE_OPERATION_BSS_COLOR_MAX) {
4281 /* There are no free colors so turn BSS coloring off */
4282 wpa_printf(MSG_INFO,
4283 "No free colors left, turning off BSS coloring");
4284 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
4285 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
4286 for (b = 0; b < hapd->iface->num_bss; b++)
4287 ieee802_11_set_beacon(hapd->iface->bss[b]);
4288 return;
4289 }
4290
4291 for (b = 0; b < hapd->iface->num_bss; b++) {
4292 struct hostapd_data *bss = hapd->iface->bss[b];
4293 struct cca_settings settings;
4294 int ret;
4295
4296 hostapd_cleanup_cca_params(bss);
4297 bss->cca_color = r;
4298 bss->cca_count = 10;
4299
4300 if (hostapd_fill_cca_settings(bss, &settings)) {
4301 hostapd_cleanup_cca_params(bss);
4302 continue;
4303 }
4304
4305 ret = hostapd_drv_switch_color(bss, &settings);
4306 if (ret)
4307 hostapd_cleanup_cca_params(bss);
4308
4309 free_beacon_data(&settings.beacon_cca);
4310 free_beacon_data(&settings.beacon_after);
4311 }
4312}
4313
4314
4315void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
4316{
4317 struct os_reltime now;
4318
4319 if (hapd->cca_in_progress)
4320 return;
4321
4322 if (os_get_reltime(&now))
4323 return;
4324
4325 hapd->color_collision_bitmap = bitmap;
4326 hapd->last_color_collision = now;
4327
4328 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
4329 hapd, NULL))
4330 return;
4331
4332 hapd->first_color_collision = now;
4333 /* 10 s window as margin for persistent color collision reporting */
4334 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
4335 hostapd_switch_color_timeout_handler,
4336 hapd, NULL);
4337}
4338
4339#endif /* CONFIG_IEEE80211AX */
4340
Dmitry Shmidte4663042016-04-04 10:07:49 -07004341#endif /* NEED_AP_MLME */
4342
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004343
4344struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
4345 const char *ifname)
4346{
4347 size_t i, j;
4348
4349 for (i = 0; i < interfaces->count; i++) {
4350 struct hostapd_iface *iface = interfaces->iface[i];
4351
4352 for (j = 0; j < iface->num_bss; j++) {
4353 struct hostapd_data *hapd = iface->bss[j];
4354
4355 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4356 return hapd;
4357 }
4358 }
4359
4360 return NULL;
4361}
4362
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004363
4364void hostapd_periodic_iface(struct hostapd_iface *iface)
4365{
4366 size_t i;
4367
4368 ap_list_timer(iface);
4369
4370 for (i = 0; i < iface->num_bss; i++) {
4371 struct hostapd_data *hapd = iface->bss[i];
4372
4373 if (!hapd->started)
4374 continue;
4375
4376#ifndef CONFIG_NO_RADIUS
4377 hostapd_acl_expire(hapd);
4378#endif /* CONFIG_NO_RADIUS */
4379 }
4380}
Hai Shalom899fcc72020-10-19 14:38:18 -07004381
4382
4383#ifdef CONFIG_OCV
4384void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4385{
4386 struct hostapd_data *hapd = eloop_ctx;
4387 struct sta_info *sta;
4388
4389 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4390
4391 for (sta = hapd->sta_list; sta; sta = sta->next) {
4392 if (!sta->post_csa_sa_query)
4393 continue;
4394
4395 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4396 " did not start SA Query after CSA - disconnect",
4397 MAC2STR(sta->addr));
4398 ap_sta_disconnect(hapd, sta, sta->addr,
4399 WLAN_REASON_PREV_AUTH_NOT_VALID);
4400 }
4401}
4402#endif /* CONFIG_OCV */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004403
4404
4405#ifdef CONFIG_IEEE80211BE
4406struct hostapd_data * hostapd_mld_get_link_bss(struct hostapd_data *hapd,
4407 u8 link_id)
4408{
4409 unsigned int i;
4410
4411 for (i = 0; i < hapd->iface->interfaces->count; i++) {
4412 struct hostapd_iface *h = hapd->iface->interfaces->iface[i];
4413 struct hostapd_data *h_hapd = h->bss[0];
Sunil Ravi88611412024-06-28 17:34:56 +00004414 struct hostapd_bss_config *hconf = h_hapd->conf;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004415
Sunil Ravi88611412024-06-28 17:34:56 +00004416 if (!hconf->mld_ap || hconf->mld_id != hapd->conf->mld_id)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004417 continue;
4418
4419 if (h_hapd->mld_link_id == link_id)
4420 return h_hapd;
4421 }
4422
4423 return NULL;
4424}
4425#endif /* CONFIG_IEEE80211BE */